xen-pcifront.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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. if (pcidev)
  505. pci_dev_put(pcidev);
  506. return result;
  507. }
  508. pdrv = pcidev->driver;
  509. if (pdrv) {
  510. if (pdrv->err_handler && pdrv->err_handler->error_detected) {
  511. dev_dbg(&pcidev->dev,
  512. "trying to call AER service\n");
  513. if (pcidev) {
  514. flag = 1;
  515. switch (cmd) {
  516. case XEN_PCI_OP_aer_detected:
  517. result = pdrv->err_handler->
  518. error_detected(pcidev, state);
  519. break;
  520. case XEN_PCI_OP_aer_mmio:
  521. result = pdrv->err_handler->
  522. mmio_enabled(pcidev);
  523. break;
  524. case XEN_PCI_OP_aer_slotreset:
  525. result = pdrv->err_handler->
  526. slot_reset(pcidev);
  527. break;
  528. case XEN_PCI_OP_aer_resume:
  529. pdrv->err_handler->resume(pcidev);
  530. break;
  531. default:
  532. dev_err(&pdev->xdev->dev,
  533. "bad request in aer recovery "
  534. "operation!\n");
  535. }
  536. }
  537. }
  538. }
  539. if (!flag)
  540. result = PCI_ERS_RESULT_NONE;
  541. return result;
  542. }
  543. static void pcifront_do_aer(struct work_struct *data)
  544. {
  545. struct pcifront_device *pdev =
  546. container_of(data, struct pcifront_device, op_work);
  547. int cmd = pdev->sh_info->aer_op.cmd;
  548. pci_channel_state_t state =
  549. (pci_channel_state_t)pdev->sh_info->aer_op.err;
  550. /*If a pci_conf op is in progress,
  551. we have to wait until it is done before service aer op*/
  552. dev_dbg(&pdev->xdev->dev,
  553. "pcifront service aer bus %x devfn %x\n",
  554. pdev->sh_info->aer_op.bus, pdev->sh_info->aer_op.devfn);
  555. pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
  556. /* Post the operation to the guest. */
  557. wmb();
  558. clear_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags);
  559. notify_remote_via_evtchn(pdev->evtchn);
  560. /*in case of we lost an aer request in four lines time_window*/
  561. smp_mb__before_atomic();
  562. clear_bit(_PDEVB_op_active, &pdev->flags);
  563. smp_mb__after_atomic();
  564. schedule_pcifront_aer_op(pdev);
  565. }
  566. static irqreturn_t pcifront_handler_aer(int irq, void *dev)
  567. {
  568. struct pcifront_device *pdev = dev;
  569. schedule_pcifront_aer_op(pdev);
  570. return IRQ_HANDLED;
  571. }
  572. static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
  573. {
  574. int err = 0;
  575. spin_lock(&pcifront_dev_lock);
  576. if (!pcifront_dev) {
  577. dev_info(&pdev->xdev->dev, "Installing PCI frontend\n");
  578. pcifront_dev = pdev;
  579. } else
  580. err = -EEXIST;
  581. spin_unlock(&pcifront_dev_lock);
  582. if (!err && !swiotlb_nr_tbl()) {
  583. err = pci_xen_swiotlb_init_late();
  584. if (err)
  585. dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n");
  586. }
  587. return err;
  588. }
  589. static void pcifront_disconnect(struct pcifront_device *pdev)
  590. {
  591. spin_lock(&pcifront_dev_lock);
  592. if (pdev == pcifront_dev) {
  593. dev_info(&pdev->xdev->dev,
  594. "Disconnecting PCI Frontend Buses\n");
  595. pcifront_dev = NULL;
  596. }
  597. spin_unlock(&pcifront_dev_lock);
  598. }
  599. static struct pcifront_device *alloc_pdev(struct xenbus_device *xdev)
  600. {
  601. struct pcifront_device *pdev;
  602. pdev = kzalloc(sizeof(struct pcifront_device), GFP_KERNEL);
  603. if (pdev == NULL)
  604. goto out;
  605. pdev->sh_info =
  606. (struct xen_pci_sharedinfo *)__get_free_page(GFP_KERNEL);
  607. if (pdev->sh_info == NULL) {
  608. kfree(pdev);
  609. pdev = NULL;
  610. goto out;
  611. }
  612. pdev->sh_info->flags = 0;
  613. /*Flag for registering PV AER handler*/
  614. set_bit(_XEN_PCIB_AERHANDLER, (void *)&pdev->sh_info->flags);
  615. dev_set_drvdata(&xdev->dev, pdev);
  616. pdev->xdev = xdev;
  617. INIT_LIST_HEAD(&pdev->root_buses);
  618. spin_lock_init(&pdev->sh_info_lock);
  619. pdev->evtchn = INVALID_EVTCHN;
  620. pdev->gnt_ref = INVALID_GRANT_REF;
  621. pdev->irq = -1;
  622. INIT_WORK(&pdev->op_work, pcifront_do_aer);
  623. dev_dbg(&xdev->dev, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n",
  624. pdev, pdev->sh_info);
  625. out:
  626. return pdev;
  627. }
  628. static void free_pdev(struct pcifront_device *pdev)
  629. {
  630. dev_dbg(&pdev->xdev->dev, "freeing pdev @ 0x%p\n", pdev);
  631. pcifront_free_roots(pdev);
  632. cancel_work_sync(&pdev->op_work);
  633. if (pdev->irq >= 0)
  634. unbind_from_irqhandler(pdev->irq, pdev);
  635. if (pdev->evtchn != INVALID_EVTCHN)
  636. xenbus_free_evtchn(pdev->xdev, pdev->evtchn);
  637. if (pdev->gnt_ref != INVALID_GRANT_REF)
  638. gnttab_end_foreign_access(pdev->gnt_ref, 0 /* r/w page */,
  639. (unsigned long)pdev->sh_info);
  640. else
  641. free_page((unsigned long)pdev->sh_info);
  642. dev_set_drvdata(&pdev->xdev->dev, NULL);
  643. kfree(pdev);
  644. }
  645. static int pcifront_publish_info(struct pcifront_device *pdev)
  646. {
  647. int err = 0;
  648. struct xenbus_transaction trans;
  649. err = xenbus_grant_ring(pdev->xdev, virt_to_mfn(pdev->sh_info));
  650. if (err < 0)
  651. goto out;
  652. pdev->gnt_ref = err;
  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. num_roots = 0;
  721. } else if (err != 1) {
  722. if (err == 0)
  723. err = -EINVAL;
  724. xenbus_dev_fatal(pdev->xdev, err,
  725. "Error reading number of PCI roots");
  726. goto out;
  727. }
  728. for (i = 0; i < num_roots; i++) {
  729. len = snprintf(str, sizeof(str), "root-%d", i);
  730. if (unlikely(len >= (sizeof(str) - 1))) {
  731. err = -ENOMEM;
  732. goto out;
  733. }
  734. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
  735. "%x:%x", &domain, &bus);
  736. if (err != 2) {
  737. if (err >= 0)
  738. err = -EINVAL;
  739. xenbus_dev_fatal(pdev->xdev, err,
  740. "Error reading PCI root %d", i);
  741. goto out;
  742. }
  743. err = pcifront_scan_root(pdev, domain, bus);
  744. if (err) {
  745. xenbus_dev_fatal(pdev->xdev, err,
  746. "Error scanning PCI root %04x:%02x",
  747. domain, bus);
  748. goto out;
  749. }
  750. }
  751. err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
  752. out:
  753. return err;
  754. }
  755. static int pcifront_try_disconnect(struct pcifront_device *pdev)
  756. {
  757. int err = 0;
  758. enum xenbus_state prev_state;
  759. prev_state = xenbus_read_driver_state(pdev->xdev->nodename);
  760. if (prev_state >= XenbusStateClosing)
  761. goto out;
  762. if (prev_state == XenbusStateConnected) {
  763. pcifront_free_roots(pdev);
  764. pcifront_disconnect(pdev);
  765. }
  766. err = xenbus_switch_state(pdev->xdev, XenbusStateClosed);
  767. out:
  768. return err;
  769. }
  770. static int pcifront_attach_devices(struct pcifront_device *pdev)
  771. {
  772. int err = -EFAULT;
  773. int i, num_roots, len;
  774. unsigned int domain, bus;
  775. char str[64];
  776. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  777. XenbusStateReconfiguring)
  778. goto out;
  779. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
  780. "root_num", "%d", &num_roots);
  781. if (err == -ENOENT) {
  782. xenbus_dev_error(pdev->xdev, err,
  783. "No PCI Roots found, trying 0000:00");
  784. err = pcifront_rescan_root(pdev, 0, 0);
  785. num_roots = 0;
  786. } else if (err != 1) {
  787. if (err == 0)
  788. err = -EINVAL;
  789. xenbus_dev_fatal(pdev->xdev, err,
  790. "Error reading number of PCI roots");
  791. goto out;
  792. }
  793. for (i = 0; i < num_roots; i++) {
  794. len = snprintf(str, sizeof(str), "root-%d", i);
  795. if (unlikely(len >= (sizeof(str) - 1))) {
  796. err = -ENOMEM;
  797. goto out;
  798. }
  799. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
  800. "%x:%x", &domain, &bus);
  801. if (err != 2) {
  802. if (err >= 0)
  803. err = -EINVAL;
  804. xenbus_dev_fatal(pdev->xdev, err,
  805. "Error reading PCI root %d", i);
  806. goto out;
  807. }
  808. err = pcifront_rescan_root(pdev, domain, bus);
  809. if (err) {
  810. xenbus_dev_fatal(pdev->xdev, err,
  811. "Error scanning PCI root %04x:%02x",
  812. domain, bus);
  813. goto out;
  814. }
  815. }
  816. xenbus_switch_state(pdev->xdev, XenbusStateConnected);
  817. out:
  818. return err;
  819. }
  820. static int pcifront_detach_devices(struct pcifront_device *pdev)
  821. {
  822. int err = 0;
  823. int i, num_devs;
  824. unsigned int domain, bus, slot, func;
  825. struct pci_dev *pci_dev;
  826. char str[64];
  827. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  828. XenbusStateConnected)
  829. goto out;
  830. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "num_devs", "%d",
  831. &num_devs);
  832. if (err != 1) {
  833. if (err >= 0)
  834. err = -EINVAL;
  835. xenbus_dev_fatal(pdev->xdev, err,
  836. "Error reading number of PCI devices");
  837. goto out;
  838. }
  839. /* Find devices being detached and remove them. */
  840. for (i = 0; i < num_devs; i++) {
  841. int l, state;
  842. l = snprintf(str, sizeof(str), "state-%d", i);
  843. if (unlikely(l >= (sizeof(str) - 1))) {
  844. err = -ENOMEM;
  845. goto out;
  846. }
  847. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, "%d",
  848. &state);
  849. if (err != 1)
  850. state = XenbusStateUnknown;
  851. if (state != XenbusStateClosing)
  852. continue;
  853. /* Remove device. */
  854. l = snprintf(str, sizeof(str), "vdev-%d", i);
  855. if (unlikely(l >= (sizeof(str) - 1))) {
  856. err = -ENOMEM;
  857. goto out;
  858. }
  859. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
  860. "%x:%x:%x.%x", &domain, &bus, &slot, &func);
  861. if (err != 4) {
  862. if (err >= 0)
  863. err = -EINVAL;
  864. xenbus_dev_fatal(pdev->xdev, err,
  865. "Error reading PCI device %d", i);
  866. goto out;
  867. }
  868. pci_dev = pci_get_domain_bus_and_slot(domain, bus,
  869. PCI_DEVFN(slot, func));
  870. if (!pci_dev) {
  871. dev_dbg(&pdev->xdev->dev,
  872. "Cannot get PCI device %04x:%02x:%02x.%d\n",
  873. domain, bus, slot, func);
  874. continue;
  875. }
  876. pci_lock_rescan_remove();
  877. pci_stop_and_remove_bus_device(pci_dev);
  878. pci_dev_put(pci_dev);
  879. pci_unlock_rescan_remove();
  880. dev_dbg(&pdev->xdev->dev,
  881. "PCI device %04x:%02x:%02x.%d removed.\n",
  882. domain, bus, slot, func);
  883. }
  884. err = xenbus_switch_state(pdev->xdev, XenbusStateReconfiguring);
  885. out:
  886. return err;
  887. }
  888. static void __init_refok pcifront_backend_changed(struct xenbus_device *xdev,
  889. enum xenbus_state be_state)
  890. {
  891. struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
  892. switch (be_state) {
  893. case XenbusStateUnknown:
  894. case XenbusStateInitialising:
  895. case XenbusStateInitWait:
  896. case XenbusStateInitialised:
  897. break;
  898. case XenbusStateConnected:
  899. pcifront_try_connect(pdev);
  900. break;
  901. case XenbusStateClosed:
  902. if (xdev->state == XenbusStateClosed)
  903. break;
  904. /* Missed the backend's CLOSING state -- fallthrough */
  905. case XenbusStateClosing:
  906. dev_warn(&xdev->dev, "backend going away!\n");
  907. pcifront_try_disconnect(pdev);
  908. break;
  909. case XenbusStateReconfiguring:
  910. pcifront_detach_devices(pdev);
  911. break;
  912. case XenbusStateReconfigured:
  913. pcifront_attach_devices(pdev);
  914. break;
  915. }
  916. }
  917. static int pcifront_xenbus_probe(struct xenbus_device *xdev,
  918. const struct xenbus_device_id *id)
  919. {
  920. int err = 0;
  921. struct pcifront_device *pdev = alloc_pdev(xdev);
  922. if (pdev == NULL) {
  923. err = -ENOMEM;
  924. xenbus_dev_fatal(xdev, err,
  925. "Error allocating pcifront_device struct");
  926. goto out;
  927. }
  928. err = pcifront_publish_info(pdev);
  929. if (err)
  930. free_pdev(pdev);
  931. out:
  932. return err;
  933. }
  934. static int pcifront_xenbus_remove(struct xenbus_device *xdev)
  935. {
  936. struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
  937. if (pdev)
  938. free_pdev(pdev);
  939. return 0;
  940. }
  941. static const struct xenbus_device_id xenpci_ids[] = {
  942. {"pci"},
  943. {""},
  944. };
  945. static DEFINE_XENBUS_DRIVER(xenpci, "pcifront",
  946. .probe = pcifront_xenbus_probe,
  947. .remove = pcifront_xenbus_remove,
  948. .otherend_changed = pcifront_backend_changed,
  949. );
  950. static int __init pcifront_init(void)
  951. {
  952. if (!xen_pv_domain() || xen_initial_domain())
  953. return -ENODEV;
  954. if (!xen_has_pv_devices())
  955. return -ENODEV;
  956. pci_frontend_registrar(1 /* enable */);
  957. return xenbus_register_frontend(&xenpci_driver);
  958. }
  959. static void __exit pcifront_cleanup(void)
  960. {
  961. xenbus_unregister_driver(&xenpci_driver);
  962. pci_frontend_registrar(0 /* disable */);
  963. }
  964. module_init(pcifront_init);
  965. module_exit(pcifront_cleanup);
  966. MODULE_DESCRIPTION("Xen PCI passthrough frontend.");
  967. MODULE_LICENSE("GPL");
  968. MODULE_ALIAS("xen:pci");