xen-pcifront.c 27 KB

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