xen-pcifront.c 27 KB

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