xen-pcifront.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /*
  2. * Xen PCI Frontend.
  3. *
  4. * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/mm.h>
  9. #include <xen/xenbus.h>
  10. #include <xen/events.h>
  11. #include <xen/grant_table.h>
  12. #include <xen/page.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/pci.h>
  15. #include <linux/msi.h>
  16. #include <xen/interface/io/pciif.h>
  17. #include <asm/xen/pci.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/atomic.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/bitops.h>
  22. #include <linux/time.h>
  23. #include <xen/platform_pci.h>
  24. #include <asm/xen/swiotlb-xen.h>
  25. #define INVALID_GRANT_REF (0)
  26. #define INVALID_EVTCHN (-1)
  27. struct pci_bus_entry {
  28. struct list_head list;
  29. struct pci_bus *bus;
  30. };
  31. #define _PDEVB_op_active (0)
  32. #define PDEVB_op_active (1 << (_PDEVB_op_active))
  33. struct pcifront_device {
  34. struct xenbus_device *xdev;
  35. struct list_head root_buses;
  36. int evtchn;
  37. int gnt_ref;
  38. int irq;
  39. /* Lock this when doing any operations in sh_info */
  40. spinlock_t sh_info_lock;
  41. struct xen_pci_sharedinfo *sh_info;
  42. struct work_struct op_work;
  43. unsigned long flags;
  44. };
  45. struct pcifront_sd {
  46. int domain;
  47. struct pcifront_device *pdev;
  48. };
  49. static inline struct pcifront_device *
  50. pcifront_get_pdev(struct pcifront_sd *sd)
  51. {
  52. return sd->pdev;
  53. }
  54. static inline void pcifront_init_sd(struct pcifront_sd *sd,
  55. unsigned int domain, unsigned int bus,
  56. struct pcifront_device *pdev)
  57. {
  58. sd->domain = domain;
  59. sd->pdev = pdev;
  60. }
  61. static DEFINE_SPINLOCK(pcifront_dev_lock);
  62. static struct pcifront_device *pcifront_dev;
  63. static int verbose_request;
  64. module_param(verbose_request, int, 0644);
  65. static int errno_to_pcibios_err(int errno)
  66. {
  67. switch (errno) {
  68. case XEN_PCI_ERR_success:
  69. return PCIBIOS_SUCCESSFUL;
  70. case XEN_PCI_ERR_dev_not_found:
  71. return PCIBIOS_DEVICE_NOT_FOUND;
  72. case XEN_PCI_ERR_invalid_offset:
  73. case XEN_PCI_ERR_op_failed:
  74. return PCIBIOS_BAD_REGISTER_NUMBER;
  75. case XEN_PCI_ERR_not_implemented:
  76. return PCIBIOS_FUNC_NOT_SUPPORTED;
  77. case XEN_PCI_ERR_access_denied:
  78. return PCIBIOS_SET_FAILED;
  79. }
  80. return errno;
  81. }
  82. static inline void schedule_pcifront_aer_op(struct pcifront_device *pdev)
  83. {
  84. if (test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
  85. && !test_and_set_bit(_PDEVB_op_active, &pdev->flags)) {
  86. dev_dbg(&pdev->xdev->dev, "schedule aer frontend job\n");
  87. schedule_work(&pdev->op_work);
  88. }
  89. }
  90. static int do_pci_op(struct pcifront_device *pdev, struct xen_pci_op *op)
  91. {
  92. int err = 0;
  93. struct xen_pci_op *active_op = &pdev->sh_info->op;
  94. unsigned long irq_flags;
  95. evtchn_port_t port = pdev->evtchn;
  96. unsigned irq = pdev->irq;
  97. s64 ns, ns_timeout;
  98. struct timeval tv;
  99. spin_lock_irqsave(&pdev->sh_info_lock, irq_flags);
  100. memcpy(active_op, op, sizeof(struct xen_pci_op));
  101. /* Go */
  102. wmb();
  103. set_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
  104. notify_remote_via_evtchn(port);
  105. /*
  106. * We set a poll timeout of 3 seconds but give up on return after
  107. * 2 seconds. It is better to time out too late rather than too early
  108. * (in the latter case we end up continually re-executing poll() with a
  109. * timeout in the past). 1s difference gives plenty of slack for error.
  110. */
  111. do_gettimeofday(&tv);
  112. ns_timeout = timeval_to_ns(&tv) + 2 * (s64)NSEC_PER_SEC;
  113. xen_clear_irq_pending(irq);
  114. while (test_bit(_XEN_PCIF_active,
  115. (unsigned long *)&pdev->sh_info->flags)) {
  116. xen_poll_irq_timeout(irq, jiffies + 3*HZ);
  117. xen_clear_irq_pending(irq);
  118. do_gettimeofday(&tv);
  119. ns = timeval_to_ns(&tv);
  120. if (ns > ns_timeout) {
  121. dev_err(&pdev->xdev->dev,
  122. "pciback not responding!!!\n");
  123. clear_bit(_XEN_PCIF_active,
  124. (unsigned long *)&pdev->sh_info->flags);
  125. err = XEN_PCI_ERR_dev_not_found;
  126. goto out;
  127. }
  128. }
  129. /*
  130. * We might lose backend service request since we
  131. * reuse same evtchn with pci_conf backend response. So re-schedule
  132. * aer pcifront service.
  133. */
  134. if (test_bit(_XEN_PCIB_active,
  135. (unsigned long *)&pdev->sh_info->flags)) {
  136. dev_err(&pdev->xdev->dev,
  137. "schedule aer pcifront service\n");
  138. schedule_pcifront_aer_op(pdev);
  139. }
  140. memcpy(op, active_op, sizeof(struct xen_pci_op));
  141. err = op->err;
  142. out:
  143. spin_unlock_irqrestore(&pdev->sh_info_lock, irq_flags);
  144. return err;
  145. }
  146. /* Access to this function is spinlocked in drivers/pci/access.c */
  147. static int pcifront_bus_read(struct pci_bus *bus, unsigned int devfn,
  148. int where, int size, u32 *val)
  149. {
  150. int err = 0;
  151. struct xen_pci_op op = {
  152. .cmd = XEN_PCI_OP_conf_read,
  153. .domain = pci_domain_nr(bus),
  154. .bus = bus->number,
  155. .devfn = devfn,
  156. .offset = where,
  157. .size = size,
  158. };
  159. struct pcifront_sd *sd = bus->sysdata;
  160. struct pcifront_device *pdev = pcifront_get_pdev(sd);
  161. if (verbose_request)
  162. dev_info(&pdev->xdev->dev,
  163. "read dev=%04x:%02x:%02x.%d - offset %x size %d\n",
  164. pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
  165. PCI_FUNC(devfn), where, size);
  166. err = do_pci_op(pdev, &op);
  167. if (likely(!err)) {
  168. if (verbose_request)
  169. dev_info(&pdev->xdev->dev, "read got back value %x\n",
  170. op.value);
  171. *val = op.value;
  172. } else if (err == -ENODEV) {
  173. /* No device here, pretend that it just returned 0 */
  174. err = 0;
  175. *val = 0;
  176. }
  177. return errno_to_pcibios_err(err);
  178. }
  179. /* Access to this function is spinlocked in drivers/pci/access.c */
  180. static int pcifront_bus_write(struct pci_bus *bus, unsigned int devfn,
  181. int where, int size, u32 val)
  182. {
  183. struct xen_pci_op op = {
  184. .cmd = XEN_PCI_OP_conf_write,
  185. .domain = pci_domain_nr(bus),
  186. .bus = bus->number,
  187. .devfn = devfn,
  188. .offset = where,
  189. .size = size,
  190. .value = val,
  191. };
  192. struct pcifront_sd *sd = bus->sysdata;
  193. struct pcifront_device *pdev = pcifront_get_pdev(sd);
  194. if (verbose_request)
  195. dev_info(&pdev->xdev->dev,
  196. "write dev=%04x:%02x:%02x.%d - "
  197. "offset %x size %d val %x\n",
  198. pci_domain_nr(bus), bus->number,
  199. PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val);
  200. return errno_to_pcibios_err(do_pci_op(pdev, &op));
  201. }
  202. static struct pci_ops pcifront_bus_ops = {
  203. .read = pcifront_bus_read,
  204. .write = pcifront_bus_write,
  205. };
  206. #ifdef CONFIG_PCI_MSI
  207. static int pci_frontend_enable_msix(struct pci_dev *dev,
  208. int vector[], int nvec)
  209. {
  210. int err;
  211. int i;
  212. struct xen_pci_op op = {
  213. .cmd = XEN_PCI_OP_enable_msix,
  214. .domain = pci_domain_nr(dev->bus),
  215. .bus = dev->bus->number,
  216. .devfn = dev->devfn,
  217. .value = nvec,
  218. };
  219. struct pcifront_sd *sd = dev->bus->sysdata;
  220. struct pcifront_device *pdev = pcifront_get_pdev(sd);
  221. struct msi_desc *entry;
  222. if (nvec > SH_INFO_MAX_VEC) {
  223. dev_err(&dev->dev, "too much vector for pci frontend: %x."
  224. " Increase SH_INFO_MAX_VEC.\n", nvec);
  225. return -EINVAL;
  226. }
  227. i = 0;
  228. list_for_each_entry(entry, &dev->msi_list, list) {
  229. op.msix_entries[i].entry = entry->msi_attrib.entry_nr;
  230. /* Vector is useless at this point. */
  231. op.msix_entries[i].vector = -1;
  232. i++;
  233. }
  234. err = do_pci_op(pdev, &op);
  235. if (likely(!err)) {
  236. if (likely(!op.value)) {
  237. /* we get the result */
  238. for (i = 0; i < nvec; i++) {
  239. if (op.msix_entries[i].vector <= 0) {
  240. dev_warn(&dev->dev, "MSI-X entry %d is invalid: %d!\n",
  241. i, op.msix_entries[i].vector);
  242. err = -EINVAL;
  243. vector[i] = -1;
  244. continue;
  245. }
  246. vector[i] = op.msix_entries[i].vector;
  247. }
  248. } else {
  249. printk(KERN_DEBUG "enable msix get value %x\n",
  250. op.value);
  251. err = op.value;
  252. }
  253. } else {
  254. dev_err(&dev->dev, "enable msix get err %x\n", err);
  255. }
  256. return err;
  257. }
  258. static void pci_frontend_disable_msix(struct pci_dev *dev)
  259. {
  260. int err;
  261. struct xen_pci_op op = {
  262. .cmd = XEN_PCI_OP_disable_msix,
  263. .domain = pci_domain_nr(dev->bus),
  264. .bus = dev->bus->number,
  265. .devfn = dev->devfn,
  266. };
  267. struct pcifront_sd *sd = dev->bus->sysdata;
  268. struct pcifront_device *pdev = pcifront_get_pdev(sd);
  269. err = do_pci_op(pdev, &op);
  270. /* What should do for error ? */
  271. if (err)
  272. dev_err(&dev->dev, "pci_disable_msix get err %x\n", err);
  273. }
  274. static int pci_frontend_enable_msi(struct pci_dev *dev, int vector[])
  275. {
  276. int err;
  277. struct xen_pci_op op = {
  278. .cmd = XEN_PCI_OP_enable_msi,
  279. .domain = pci_domain_nr(dev->bus),
  280. .bus = dev->bus->number,
  281. .devfn = dev->devfn,
  282. };
  283. struct pcifront_sd *sd = dev->bus->sysdata;
  284. struct pcifront_device *pdev = pcifront_get_pdev(sd);
  285. err = do_pci_op(pdev, &op);
  286. if (likely(!err)) {
  287. vector[0] = op.value;
  288. if (op.value <= 0) {
  289. dev_warn(&dev->dev, "MSI entry is invalid: %d!\n",
  290. op.value);
  291. err = -EINVAL;
  292. vector[0] = -1;
  293. }
  294. } else {
  295. dev_err(&dev->dev, "pci frontend enable msi failed for dev "
  296. "%x:%x\n", op.bus, op.devfn);
  297. err = -EINVAL;
  298. }
  299. return err;
  300. }
  301. static void pci_frontend_disable_msi(struct pci_dev *dev)
  302. {
  303. int err;
  304. struct xen_pci_op op = {
  305. .cmd = XEN_PCI_OP_disable_msi,
  306. .domain = pci_domain_nr(dev->bus),
  307. .bus = dev->bus->number,
  308. .devfn = dev->devfn,
  309. };
  310. struct pcifront_sd *sd = dev->bus->sysdata;
  311. struct pcifront_device *pdev = pcifront_get_pdev(sd);
  312. err = do_pci_op(pdev, &op);
  313. if (err == XEN_PCI_ERR_dev_not_found) {
  314. /* XXX No response from backend, what shall we do? */
  315. printk(KERN_DEBUG "get no response from backend for disable MSI\n");
  316. return;
  317. }
  318. if (err)
  319. /* how can pciback notify us fail? */
  320. printk(KERN_DEBUG "get fake response frombackend\n");
  321. }
  322. static struct xen_pci_frontend_ops pci_frontend_ops = {
  323. .enable_msi = pci_frontend_enable_msi,
  324. .disable_msi = pci_frontend_disable_msi,
  325. .enable_msix = pci_frontend_enable_msix,
  326. .disable_msix = pci_frontend_disable_msix,
  327. };
  328. static void pci_frontend_registrar(int enable)
  329. {
  330. if (enable)
  331. xen_pci_frontend = &pci_frontend_ops;
  332. else
  333. xen_pci_frontend = NULL;
  334. };
  335. #else
  336. static inline void pci_frontend_registrar(int enable) { };
  337. #endif /* CONFIG_PCI_MSI */
  338. /* Claim resources for the PCI frontend as-is, backend won't allow changes */
  339. static int pcifront_claim_resource(struct pci_dev *dev, void *data)
  340. {
  341. struct pcifront_device *pdev = data;
  342. int i;
  343. struct resource *r;
  344. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  345. r = &dev->resource[i];
  346. if (!r->parent && r->start && r->flags) {
  347. dev_info(&pdev->xdev->dev, "claiming resource %s/%d\n",
  348. pci_name(dev), i);
  349. if (pci_claim_resource(dev, i)) {
  350. dev_err(&pdev->xdev->dev, "Could not claim resource %s/%d! "
  351. "Device offline. Try using e820_host=1 in the guest config.\n",
  352. pci_name(dev), i);
  353. }
  354. }
  355. }
  356. return 0;
  357. }
  358. static int pcifront_scan_bus(struct pcifront_device *pdev,
  359. unsigned int domain, unsigned int bus,
  360. struct pci_bus *b)
  361. {
  362. struct pci_dev *d;
  363. unsigned int devfn;
  364. /* Scan the bus for functions and add.
  365. * We omit handling of PCI bridge attachment because pciback prevents
  366. * bridges from being exported.
  367. */
  368. for (devfn = 0; devfn < 0x100; devfn++) {
  369. d = pci_get_slot(b, devfn);
  370. if (d) {
  371. /* Device is already known. */
  372. pci_dev_put(d);
  373. continue;
  374. }
  375. d = pci_scan_single_device(b, devfn);
  376. if (d)
  377. dev_info(&pdev->xdev->dev, "New device on "
  378. "%04x:%02x:%02x.%d found.\n", domain, bus,
  379. PCI_SLOT(devfn), PCI_FUNC(devfn));
  380. }
  381. return 0;
  382. }
  383. static int pcifront_scan_root(struct pcifront_device *pdev,
  384. unsigned int domain, unsigned int bus)
  385. {
  386. struct pci_bus *b;
  387. struct pcifront_sd *sd = NULL;
  388. struct pci_bus_entry *bus_entry = NULL;
  389. int err = 0;
  390. #ifndef CONFIG_PCI_DOMAINS
  391. if (domain != 0) {
  392. dev_err(&pdev->xdev->dev,
  393. "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
  394. dev_err(&pdev->xdev->dev,
  395. "Please compile with CONFIG_PCI_DOMAINS\n");
  396. err = -EINVAL;
  397. goto err_out;
  398. }
  399. #endif
  400. dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n",
  401. domain, bus);
  402. bus_entry = kmalloc(sizeof(*bus_entry), GFP_KERNEL);
  403. sd = kmalloc(sizeof(*sd), GFP_KERNEL);
  404. if (!bus_entry || !sd) {
  405. err = -ENOMEM;
  406. goto err_out;
  407. }
  408. pcifront_init_sd(sd, domain, bus, pdev);
  409. pci_lock_rescan_remove();
  410. b = pci_scan_bus_parented(&pdev->xdev->dev, bus,
  411. &pcifront_bus_ops, sd);
  412. if (!b) {
  413. dev_err(&pdev->xdev->dev,
  414. "Error creating PCI Frontend Bus!\n");
  415. err = -ENOMEM;
  416. pci_unlock_rescan_remove();
  417. goto err_out;
  418. }
  419. bus_entry->bus = b;
  420. list_add(&bus_entry->list, &pdev->root_buses);
  421. /* pci_scan_bus_parented skips devices which do not have a have
  422. * devfn==0. The pcifront_scan_bus enumerates all devfn. */
  423. err = pcifront_scan_bus(pdev, domain, bus, b);
  424. /* Claim resources before going "live" with our devices */
  425. pci_walk_bus(b, pcifront_claim_resource, pdev);
  426. /* Create SysFS and notify udev of the devices. Aka: "going live" */
  427. pci_bus_add_devices(b);
  428. pci_unlock_rescan_remove();
  429. return err;
  430. err_out:
  431. kfree(bus_entry);
  432. kfree(sd);
  433. return err;
  434. }
  435. static int pcifront_rescan_root(struct pcifront_device *pdev,
  436. unsigned int domain, unsigned int bus)
  437. {
  438. int err;
  439. struct pci_bus *b;
  440. #ifndef CONFIG_PCI_DOMAINS
  441. if (domain != 0) {
  442. dev_err(&pdev->xdev->dev,
  443. "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
  444. dev_err(&pdev->xdev->dev,
  445. "Please compile with CONFIG_PCI_DOMAINS\n");
  446. return -EINVAL;
  447. }
  448. #endif
  449. dev_info(&pdev->xdev->dev, "Rescanning PCI Frontend Bus %04x:%02x\n",
  450. domain, bus);
  451. b = pci_find_bus(domain, bus);
  452. if (!b)
  453. /* If the bus is unknown, create it. */
  454. return pcifront_scan_root(pdev, domain, bus);
  455. err = pcifront_scan_bus(pdev, domain, bus, b);
  456. /* Claim resources before going "live" with our devices */
  457. pci_walk_bus(b, pcifront_claim_resource, pdev);
  458. /* Create SysFS and notify udev of the devices. Aka: "going live" */
  459. pci_bus_add_devices(b);
  460. return err;
  461. }
  462. static void free_root_bus_devs(struct pci_bus *bus)
  463. {
  464. struct pci_dev *dev;
  465. while (!list_empty(&bus->devices)) {
  466. dev = container_of(bus->devices.next, struct pci_dev,
  467. bus_list);
  468. dev_dbg(&dev->dev, "removing device\n");
  469. pci_stop_and_remove_bus_device(dev);
  470. }
  471. }
  472. static void pcifront_free_roots(struct pcifront_device *pdev)
  473. {
  474. struct pci_bus_entry *bus_entry, *t;
  475. dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n");
  476. pci_lock_rescan_remove();
  477. list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) {
  478. list_del(&bus_entry->list);
  479. free_root_bus_devs(bus_entry->bus);
  480. kfree(bus_entry->bus->sysdata);
  481. device_unregister(bus_entry->bus->bridge);
  482. pci_remove_bus(bus_entry->bus);
  483. kfree(bus_entry);
  484. }
  485. pci_unlock_rescan_remove();
  486. }
  487. static pci_ers_result_t pcifront_common_process(int cmd,
  488. struct pcifront_device *pdev,
  489. pci_channel_state_t state)
  490. {
  491. pci_ers_result_t result;
  492. struct pci_driver *pdrv;
  493. int bus = pdev->sh_info->aer_op.bus;
  494. int devfn = pdev->sh_info->aer_op.devfn;
  495. struct pci_dev *pcidev;
  496. int flag = 0;
  497. dev_dbg(&pdev->xdev->dev,
  498. "pcifront AER process: cmd %x (bus:%x, devfn%x)",
  499. cmd, bus, devfn);
  500. result = PCI_ERS_RESULT_NONE;
  501. pcidev = pci_get_bus_and_slot(bus, devfn);
  502. if (!pcidev || !pcidev->driver) {
  503. dev_err(&pdev->xdev->dev, "device or AER driver is NULL\n");
  504. pci_dev_put(pcidev);
  505. return result;
  506. }
  507. pdrv = pcidev->driver;
  508. if (pdrv) {
  509. if (pdrv->err_handler && pdrv->err_handler->error_detected) {
  510. dev_dbg(&pcidev->dev,
  511. "trying to call AER service\n");
  512. if (pcidev) {
  513. flag = 1;
  514. switch (cmd) {
  515. case XEN_PCI_OP_aer_detected:
  516. result = pdrv->err_handler->
  517. error_detected(pcidev, state);
  518. break;
  519. case XEN_PCI_OP_aer_mmio:
  520. result = pdrv->err_handler->
  521. mmio_enabled(pcidev);
  522. break;
  523. case XEN_PCI_OP_aer_slotreset:
  524. result = pdrv->err_handler->
  525. slot_reset(pcidev);
  526. break;
  527. case XEN_PCI_OP_aer_resume:
  528. pdrv->err_handler->resume(pcidev);
  529. break;
  530. default:
  531. dev_err(&pdev->xdev->dev,
  532. "bad request in aer recovery "
  533. "operation!\n");
  534. }
  535. }
  536. }
  537. }
  538. if (!flag)
  539. result = PCI_ERS_RESULT_NONE;
  540. return result;
  541. }
  542. static void pcifront_do_aer(struct work_struct *data)
  543. {
  544. struct pcifront_device *pdev =
  545. container_of(data, struct pcifront_device, op_work);
  546. int cmd = pdev->sh_info->aer_op.cmd;
  547. pci_channel_state_t state =
  548. (pci_channel_state_t)pdev->sh_info->aer_op.err;
  549. /*If a pci_conf op is in progress,
  550. we have to wait until it is done before service aer op*/
  551. dev_dbg(&pdev->xdev->dev,
  552. "pcifront service aer bus %x devfn %x\n",
  553. pdev->sh_info->aer_op.bus, pdev->sh_info->aer_op.devfn);
  554. pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
  555. /* Post the operation to the guest. */
  556. wmb();
  557. clear_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags);
  558. notify_remote_via_evtchn(pdev->evtchn);
  559. /*in case of we lost an aer request in four lines time_window*/
  560. smp_mb__before_atomic();
  561. clear_bit(_PDEVB_op_active, &pdev->flags);
  562. smp_mb__after_atomic();
  563. schedule_pcifront_aer_op(pdev);
  564. }
  565. static irqreturn_t pcifront_handler_aer(int irq, void *dev)
  566. {
  567. struct pcifront_device *pdev = dev;
  568. schedule_pcifront_aer_op(pdev);
  569. return IRQ_HANDLED;
  570. }
  571. static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
  572. {
  573. int err = 0;
  574. spin_lock(&pcifront_dev_lock);
  575. if (!pcifront_dev) {
  576. dev_info(&pdev->xdev->dev, "Installing PCI frontend\n");
  577. pcifront_dev = pdev;
  578. } else
  579. err = -EEXIST;
  580. spin_unlock(&pcifront_dev_lock);
  581. if (!err && !swiotlb_nr_tbl()) {
  582. err = pci_xen_swiotlb_init_late();
  583. if (err)
  584. dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n");
  585. }
  586. return err;
  587. }
  588. static void pcifront_disconnect(struct pcifront_device *pdev)
  589. {
  590. spin_lock(&pcifront_dev_lock);
  591. if (pdev == pcifront_dev) {
  592. dev_info(&pdev->xdev->dev,
  593. "Disconnecting PCI Frontend Buses\n");
  594. pcifront_dev = NULL;
  595. }
  596. spin_unlock(&pcifront_dev_lock);
  597. }
  598. static struct pcifront_device *alloc_pdev(struct xenbus_device *xdev)
  599. {
  600. struct pcifront_device *pdev;
  601. pdev = kzalloc(sizeof(struct pcifront_device), GFP_KERNEL);
  602. if (pdev == NULL)
  603. goto out;
  604. pdev->sh_info =
  605. (struct xen_pci_sharedinfo *)__get_free_page(GFP_KERNEL);
  606. if (pdev->sh_info == NULL) {
  607. kfree(pdev);
  608. pdev = NULL;
  609. goto out;
  610. }
  611. pdev->sh_info->flags = 0;
  612. /*Flag for registering PV AER handler*/
  613. set_bit(_XEN_PCIB_AERHANDLER, (void *)&pdev->sh_info->flags);
  614. dev_set_drvdata(&xdev->dev, pdev);
  615. pdev->xdev = xdev;
  616. INIT_LIST_HEAD(&pdev->root_buses);
  617. spin_lock_init(&pdev->sh_info_lock);
  618. pdev->evtchn = INVALID_EVTCHN;
  619. pdev->gnt_ref = INVALID_GRANT_REF;
  620. pdev->irq = -1;
  621. INIT_WORK(&pdev->op_work, pcifront_do_aer);
  622. dev_dbg(&xdev->dev, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n",
  623. pdev, pdev->sh_info);
  624. out:
  625. return pdev;
  626. }
  627. static void free_pdev(struct pcifront_device *pdev)
  628. {
  629. dev_dbg(&pdev->xdev->dev, "freeing pdev @ 0x%p\n", pdev);
  630. pcifront_free_roots(pdev);
  631. cancel_work_sync(&pdev->op_work);
  632. if (pdev->irq >= 0)
  633. unbind_from_irqhandler(pdev->irq, pdev);
  634. if (pdev->evtchn != INVALID_EVTCHN)
  635. xenbus_free_evtchn(pdev->xdev, pdev->evtchn);
  636. if (pdev->gnt_ref != INVALID_GRANT_REF)
  637. gnttab_end_foreign_access(pdev->gnt_ref, 0 /* r/w page */,
  638. (unsigned long)pdev->sh_info);
  639. else
  640. free_page((unsigned long)pdev->sh_info);
  641. dev_set_drvdata(&pdev->xdev->dev, NULL);
  642. kfree(pdev);
  643. }
  644. static int pcifront_publish_info(struct pcifront_device *pdev)
  645. {
  646. int err = 0;
  647. struct xenbus_transaction trans;
  648. grant_ref_t gref;
  649. err = xenbus_grant_ring(pdev->xdev, pdev->sh_info, 1, &gref);
  650. if (err < 0)
  651. goto out;
  652. pdev->gnt_ref = gref;
  653. err = xenbus_alloc_evtchn(pdev->xdev, &pdev->evtchn);
  654. if (err)
  655. goto out;
  656. err = bind_evtchn_to_irqhandler(pdev->evtchn, pcifront_handler_aer,
  657. 0, "pcifront", pdev);
  658. if (err < 0)
  659. return err;
  660. pdev->irq = err;
  661. do_publish:
  662. err = xenbus_transaction_start(&trans);
  663. if (err) {
  664. xenbus_dev_fatal(pdev->xdev, err,
  665. "Error writing configuration for backend "
  666. "(start transaction)");
  667. goto out;
  668. }
  669. err = xenbus_printf(trans, pdev->xdev->nodename,
  670. "pci-op-ref", "%u", pdev->gnt_ref);
  671. if (!err)
  672. err = xenbus_printf(trans, pdev->xdev->nodename,
  673. "event-channel", "%u", pdev->evtchn);
  674. if (!err)
  675. err = xenbus_printf(trans, pdev->xdev->nodename,
  676. "magic", XEN_PCI_MAGIC);
  677. if (err) {
  678. xenbus_transaction_end(trans, 1);
  679. xenbus_dev_fatal(pdev->xdev, err,
  680. "Error writing configuration for backend");
  681. goto out;
  682. } else {
  683. err = xenbus_transaction_end(trans, 0);
  684. if (err == -EAGAIN)
  685. goto do_publish;
  686. else if (err) {
  687. xenbus_dev_fatal(pdev->xdev, err,
  688. "Error completing transaction "
  689. "for backend");
  690. goto out;
  691. }
  692. }
  693. xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
  694. dev_dbg(&pdev->xdev->dev, "publishing successful!\n");
  695. out:
  696. return err;
  697. }
  698. static int pcifront_try_connect(struct pcifront_device *pdev)
  699. {
  700. int err = -EFAULT;
  701. int i, num_roots, len;
  702. char str[64];
  703. unsigned int domain, bus;
  704. /* Only connect once */
  705. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  706. XenbusStateInitialised)
  707. goto out;
  708. err = pcifront_connect_and_init_dma(pdev);
  709. if (err && err != -EEXIST) {
  710. xenbus_dev_fatal(pdev->xdev, err,
  711. "Error setting up PCI Frontend");
  712. goto out;
  713. }
  714. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
  715. "root_num", "%d", &num_roots);
  716. if (err == -ENOENT) {
  717. xenbus_dev_error(pdev->xdev, err,
  718. "No PCI Roots found, trying 0000:00");
  719. err = pcifront_scan_root(pdev, 0, 0);
  720. if (err) {
  721. xenbus_dev_fatal(pdev->xdev, err,
  722. "Error scanning PCI root 0000:00");
  723. goto out;
  724. }
  725. num_roots = 0;
  726. } else if (err != 1) {
  727. if (err == 0)
  728. err = -EINVAL;
  729. xenbus_dev_fatal(pdev->xdev, err,
  730. "Error reading number of PCI roots");
  731. goto out;
  732. }
  733. for (i = 0; i < num_roots; i++) {
  734. len = snprintf(str, sizeof(str), "root-%d", i);
  735. if (unlikely(len >= (sizeof(str) - 1))) {
  736. err = -ENOMEM;
  737. goto out;
  738. }
  739. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
  740. "%x:%x", &domain, &bus);
  741. if (err != 2) {
  742. if (err >= 0)
  743. err = -EINVAL;
  744. xenbus_dev_fatal(pdev->xdev, err,
  745. "Error reading PCI root %d", i);
  746. goto out;
  747. }
  748. err = pcifront_scan_root(pdev, domain, bus);
  749. if (err) {
  750. xenbus_dev_fatal(pdev->xdev, err,
  751. "Error scanning PCI root %04x:%02x",
  752. domain, bus);
  753. goto out;
  754. }
  755. }
  756. err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
  757. out:
  758. return err;
  759. }
  760. static int pcifront_try_disconnect(struct pcifront_device *pdev)
  761. {
  762. int err = 0;
  763. enum xenbus_state prev_state;
  764. prev_state = xenbus_read_driver_state(pdev->xdev->nodename);
  765. if (prev_state >= XenbusStateClosing)
  766. goto out;
  767. if (prev_state == XenbusStateConnected) {
  768. pcifront_free_roots(pdev);
  769. pcifront_disconnect(pdev);
  770. }
  771. err = xenbus_switch_state(pdev->xdev, XenbusStateClosed);
  772. out:
  773. return err;
  774. }
  775. static int pcifront_attach_devices(struct pcifront_device *pdev)
  776. {
  777. int err = -EFAULT;
  778. int i, num_roots, len;
  779. unsigned int domain, bus;
  780. char str[64];
  781. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  782. XenbusStateReconfiguring)
  783. goto out;
  784. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
  785. "root_num", "%d", &num_roots);
  786. if (err == -ENOENT) {
  787. xenbus_dev_error(pdev->xdev, err,
  788. "No PCI Roots found, trying 0000:00");
  789. err = pcifront_rescan_root(pdev, 0, 0);
  790. if (err) {
  791. xenbus_dev_fatal(pdev->xdev, err,
  792. "Error scanning PCI root 0000:00");
  793. goto out;
  794. }
  795. num_roots = 0;
  796. } else if (err != 1) {
  797. if (err == 0)
  798. err = -EINVAL;
  799. xenbus_dev_fatal(pdev->xdev, err,
  800. "Error reading number of PCI roots");
  801. goto out;
  802. }
  803. for (i = 0; i < num_roots; i++) {
  804. len = snprintf(str, sizeof(str), "root-%d", i);
  805. if (unlikely(len >= (sizeof(str) - 1))) {
  806. err = -ENOMEM;
  807. goto out;
  808. }
  809. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
  810. "%x:%x", &domain, &bus);
  811. if (err != 2) {
  812. if (err >= 0)
  813. err = -EINVAL;
  814. xenbus_dev_fatal(pdev->xdev, err,
  815. "Error reading PCI root %d", i);
  816. goto out;
  817. }
  818. err = pcifront_rescan_root(pdev, domain, bus);
  819. if (err) {
  820. xenbus_dev_fatal(pdev->xdev, err,
  821. "Error scanning PCI root %04x:%02x",
  822. domain, bus);
  823. goto out;
  824. }
  825. }
  826. xenbus_switch_state(pdev->xdev, XenbusStateConnected);
  827. out:
  828. return err;
  829. }
  830. static int pcifront_detach_devices(struct pcifront_device *pdev)
  831. {
  832. int err = 0;
  833. int i, num_devs;
  834. unsigned int domain, bus, slot, func;
  835. struct pci_dev *pci_dev;
  836. char str[64];
  837. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  838. XenbusStateConnected)
  839. goto out;
  840. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "num_devs", "%d",
  841. &num_devs);
  842. if (err != 1) {
  843. if (err >= 0)
  844. err = -EINVAL;
  845. xenbus_dev_fatal(pdev->xdev, err,
  846. "Error reading number of PCI devices");
  847. goto out;
  848. }
  849. /* Find devices being detached and remove them. */
  850. for (i = 0; i < num_devs; i++) {
  851. int l, state;
  852. l = snprintf(str, sizeof(str), "state-%d", i);
  853. if (unlikely(l >= (sizeof(str) - 1))) {
  854. err = -ENOMEM;
  855. goto out;
  856. }
  857. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, "%d",
  858. &state);
  859. if (err != 1)
  860. state = XenbusStateUnknown;
  861. if (state != XenbusStateClosing)
  862. continue;
  863. /* Remove device. */
  864. l = snprintf(str, sizeof(str), "vdev-%d", i);
  865. if (unlikely(l >= (sizeof(str) - 1))) {
  866. err = -ENOMEM;
  867. goto out;
  868. }
  869. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
  870. "%x:%x:%x.%x", &domain, &bus, &slot, &func);
  871. if (err != 4) {
  872. if (err >= 0)
  873. err = -EINVAL;
  874. xenbus_dev_fatal(pdev->xdev, err,
  875. "Error reading PCI device %d", i);
  876. goto out;
  877. }
  878. pci_dev = pci_get_domain_bus_and_slot(domain, bus,
  879. PCI_DEVFN(slot, func));
  880. if (!pci_dev) {
  881. dev_dbg(&pdev->xdev->dev,
  882. "Cannot get PCI device %04x:%02x:%02x.%d\n",
  883. domain, bus, slot, func);
  884. continue;
  885. }
  886. pci_lock_rescan_remove();
  887. pci_stop_and_remove_bus_device(pci_dev);
  888. pci_dev_put(pci_dev);
  889. pci_unlock_rescan_remove();
  890. dev_dbg(&pdev->xdev->dev,
  891. "PCI device %04x:%02x:%02x.%d removed.\n",
  892. domain, bus, slot, func);
  893. }
  894. err = xenbus_switch_state(pdev->xdev, XenbusStateReconfiguring);
  895. out:
  896. return err;
  897. }
  898. static void __init_refok pcifront_backend_changed(struct xenbus_device *xdev,
  899. enum xenbus_state be_state)
  900. {
  901. struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
  902. switch (be_state) {
  903. case XenbusStateUnknown:
  904. case XenbusStateInitialising:
  905. case XenbusStateInitWait:
  906. case XenbusStateInitialised:
  907. break;
  908. case XenbusStateConnected:
  909. pcifront_try_connect(pdev);
  910. break;
  911. case XenbusStateClosed:
  912. if (xdev->state == XenbusStateClosed)
  913. break;
  914. /* Missed the backend's CLOSING state -- fallthrough */
  915. case XenbusStateClosing:
  916. dev_warn(&xdev->dev, "backend going away!\n");
  917. pcifront_try_disconnect(pdev);
  918. break;
  919. case XenbusStateReconfiguring:
  920. pcifront_detach_devices(pdev);
  921. break;
  922. case XenbusStateReconfigured:
  923. pcifront_attach_devices(pdev);
  924. break;
  925. }
  926. }
  927. static int pcifront_xenbus_probe(struct xenbus_device *xdev,
  928. const struct xenbus_device_id *id)
  929. {
  930. int err = 0;
  931. struct pcifront_device *pdev = alloc_pdev(xdev);
  932. if (pdev == NULL) {
  933. err = -ENOMEM;
  934. xenbus_dev_fatal(xdev, err,
  935. "Error allocating pcifront_device struct");
  936. goto out;
  937. }
  938. err = pcifront_publish_info(pdev);
  939. if (err)
  940. free_pdev(pdev);
  941. out:
  942. return err;
  943. }
  944. static int pcifront_xenbus_remove(struct xenbus_device *xdev)
  945. {
  946. struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
  947. if (pdev)
  948. free_pdev(pdev);
  949. return 0;
  950. }
  951. static const struct xenbus_device_id xenpci_ids[] = {
  952. {"pci"},
  953. {""},
  954. };
  955. static struct xenbus_driver xenpci_driver = {
  956. .name = "pcifront",
  957. .ids = xenpci_ids,
  958. .probe = pcifront_xenbus_probe,
  959. .remove = pcifront_xenbus_remove,
  960. .otherend_changed = pcifront_backend_changed,
  961. };
  962. static int __init pcifront_init(void)
  963. {
  964. if (!xen_pv_domain() || xen_initial_domain())
  965. return -ENODEV;
  966. if (!xen_has_pv_devices())
  967. return -ENODEV;
  968. pci_frontend_registrar(1 /* enable */);
  969. return xenbus_register_frontend(&xenpci_driver);
  970. }
  971. static void __exit pcifront_cleanup(void)
  972. {
  973. xenbus_unregister_driver(&xenpci_driver);
  974. pci_frontend_registrar(0 /* disable */);
  975. }
  976. module_init(pcifront_init);
  977. module_exit(pcifront_cleanup);
  978. MODULE_DESCRIPTION("Xen PCI passthrough frontend.");
  979. MODULE_LICENSE("GPL");
  980. MODULE_ALIAS("xen:pci");