xen-pcifront.c 27 KB

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