xen-pcifront.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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. *(*vector+i) = op.msix_entries[i].vector;
  238. return 0;
  239. } else {
  240. printk(KERN_DEBUG "enable msix get value %x\n",
  241. op.value);
  242. return op.value;
  243. }
  244. } else {
  245. dev_err(&dev->dev, "enable msix get err %x\n", err);
  246. return err;
  247. }
  248. }
  249. static void pci_frontend_disable_msix(struct pci_dev *dev)
  250. {
  251. int err;
  252. struct xen_pci_op op = {
  253. .cmd = XEN_PCI_OP_disable_msix,
  254. .domain = pci_domain_nr(dev->bus),
  255. .bus = dev->bus->number,
  256. .devfn = dev->devfn,
  257. };
  258. struct pcifront_sd *sd = dev->bus->sysdata;
  259. struct pcifront_device *pdev = pcifront_get_pdev(sd);
  260. err = do_pci_op(pdev, &op);
  261. /* What should do for error ? */
  262. if (err)
  263. dev_err(&dev->dev, "pci_disable_msix get err %x\n", err);
  264. }
  265. static int pci_frontend_enable_msi(struct pci_dev *dev, int **vector)
  266. {
  267. int err;
  268. struct xen_pci_op op = {
  269. .cmd = XEN_PCI_OP_enable_msi,
  270. .domain = pci_domain_nr(dev->bus),
  271. .bus = dev->bus->number,
  272. .devfn = dev->devfn,
  273. };
  274. struct pcifront_sd *sd = dev->bus->sysdata;
  275. struct pcifront_device *pdev = pcifront_get_pdev(sd);
  276. err = do_pci_op(pdev, &op);
  277. if (likely(!err)) {
  278. *(*vector) = op.value;
  279. } else {
  280. dev_err(&dev->dev, "pci frontend enable msi failed for dev "
  281. "%x:%x\n", op.bus, op.devfn);
  282. err = -EINVAL;
  283. }
  284. return err;
  285. }
  286. static void pci_frontend_disable_msi(struct pci_dev *dev)
  287. {
  288. int err;
  289. struct xen_pci_op op = {
  290. .cmd = XEN_PCI_OP_disable_msi,
  291. .domain = pci_domain_nr(dev->bus),
  292. .bus = dev->bus->number,
  293. .devfn = dev->devfn,
  294. };
  295. struct pcifront_sd *sd = dev->bus->sysdata;
  296. struct pcifront_device *pdev = pcifront_get_pdev(sd);
  297. err = do_pci_op(pdev, &op);
  298. if (err == XEN_PCI_ERR_dev_not_found) {
  299. /* XXX No response from backend, what shall we do? */
  300. printk(KERN_DEBUG "get no response from backend for disable MSI\n");
  301. return;
  302. }
  303. if (err)
  304. /* how can pciback notify us fail? */
  305. printk(KERN_DEBUG "get fake response frombackend\n");
  306. }
  307. static struct xen_pci_frontend_ops pci_frontend_ops = {
  308. .enable_msi = pci_frontend_enable_msi,
  309. .disable_msi = pci_frontend_disable_msi,
  310. .enable_msix = pci_frontend_enable_msix,
  311. .disable_msix = pci_frontend_disable_msix,
  312. };
  313. static void pci_frontend_registrar(int enable)
  314. {
  315. if (enable)
  316. xen_pci_frontend = &pci_frontend_ops;
  317. else
  318. xen_pci_frontend = NULL;
  319. };
  320. #else
  321. static inline void pci_frontend_registrar(int enable) { };
  322. #endif /* CONFIG_PCI_MSI */
  323. /* Claim resources for the PCI frontend as-is, backend won't allow changes */
  324. static int pcifront_claim_resource(struct pci_dev *dev, void *data)
  325. {
  326. struct pcifront_device *pdev = data;
  327. int i;
  328. struct resource *r;
  329. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  330. r = &dev->resource[i];
  331. if (!r->parent && r->start && r->flags) {
  332. dev_info(&pdev->xdev->dev, "claiming resource %s/%d\n",
  333. pci_name(dev), i);
  334. if (pci_claim_resource(dev, i)) {
  335. dev_err(&pdev->xdev->dev, "Could not claim "
  336. "resource %s/%d! Device offline. Try "
  337. "giving less than 4GB to domain.\n",
  338. pci_name(dev), i);
  339. }
  340. }
  341. }
  342. return 0;
  343. }
  344. static int __devinit pcifront_scan_bus(struct pcifront_device *pdev,
  345. unsigned int domain, unsigned int bus,
  346. struct pci_bus *b)
  347. {
  348. struct pci_dev *d;
  349. unsigned int devfn;
  350. /* Scan the bus for functions and add.
  351. * We omit handling of PCI bridge attachment because pciback prevents
  352. * bridges from being exported.
  353. */
  354. for (devfn = 0; devfn < 0x100; devfn++) {
  355. d = pci_get_slot(b, devfn);
  356. if (d) {
  357. /* Device is already known. */
  358. pci_dev_put(d);
  359. continue;
  360. }
  361. d = pci_scan_single_device(b, devfn);
  362. if (d)
  363. dev_info(&pdev->xdev->dev, "New device on "
  364. "%04x:%02x:%02x.%02x found.\n", domain, bus,
  365. PCI_SLOT(devfn), PCI_FUNC(devfn));
  366. }
  367. return 0;
  368. }
  369. static int __devinit pcifront_scan_root(struct pcifront_device *pdev,
  370. unsigned int domain, unsigned int bus)
  371. {
  372. struct pci_bus *b;
  373. struct pcifront_sd *sd = NULL;
  374. struct pci_bus_entry *bus_entry = NULL;
  375. int err = 0;
  376. #ifndef CONFIG_PCI_DOMAINS
  377. if (domain != 0) {
  378. dev_err(&pdev->xdev->dev,
  379. "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
  380. dev_err(&pdev->xdev->dev,
  381. "Please compile with CONFIG_PCI_DOMAINS\n");
  382. err = -EINVAL;
  383. goto err_out;
  384. }
  385. #endif
  386. dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n",
  387. domain, bus);
  388. bus_entry = kmalloc(sizeof(*bus_entry), GFP_KERNEL);
  389. sd = kmalloc(sizeof(*sd), GFP_KERNEL);
  390. if (!bus_entry || !sd) {
  391. err = -ENOMEM;
  392. goto err_out;
  393. }
  394. pcifront_init_sd(sd, domain, bus, pdev);
  395. b = pci_scan_bus_parented(&pdev->xdev->dev, bus,
  396. &pcifront_bus_ops, sd);
  397. if (!b) {
  398. dev_err(&pdev->xdev->dev,
  399. "Error creating PCI Frontend Bus!\n");
  400. err = -ENOMEM;
  401. goto err_out;
  402. }
  403. bus_entry->bus = b;
  404. list_add(&bus_entry->list, &pdev->root_buses);
  405. /* pci_scan_bus_parented skips devices which do not have a have
  406. * devfn==0. The pcifront_scan_bus enumerates all devfn. */
  407. err = pcifront_scan_bus(pdev, domain, bus, b);
  408. /* Claim resources before going "live" with our devices */
  409. pci_walk_bus(b, pcifront_claim_resource, pdev);
  410. /* Create SysFS and notify udev of the devices. Aka: "going live" */
  411. pci_bus_add_devices(b);
  412. return err;
  413. err_out:
  414. kfree(bus_entry);
  415. kfree(sd);
  416. return err;
  417. }
  418. static int __devinit pcifront_rescan_root(struct pcifront_device *pdev,
  419. unsigned int domain, unsigned int bus)
  420. {
  421. int err;
  422. struct pci_bus *b;
  423. #ifndef CONFIG_PCI_DOMAINS
  424. if (domain != 0) {
  425. dev_err(&pdev->xdev->dev,
  426. "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
  427. dev_err(&pdev->xdev->dev,
  428. "Please compile with CONFIG_PCI_DOMAINS\n");
  429. return -EINVAL;
  430. }
  431. #endif
  432. dev_info(&pdev->xdev->dev, "Rescanning PCI Frontend Bus %04x:%02x\n",
  433. domain, bus);
  434. b = pci_find_bus(domain, bus);
  435. if (!b)
  436. /* If the bus is unknown, create it. */
  437. return pcifront_scan_root(pdev, domain, bus);
  438. err = pcifront_scan_bus(pdev, domain, bus, b);
  439. /* Claim resources before going "live" with our devices */
  440. pci_walk_bus(b, pcifront_claim_resource, pdev);
  441. /* Create SysFS and notify udev of the devices. Aka: "going live" */
  442. pci_bus_add_devices(b);
  443. return err;
  444. }
  445. static void free_root_bus_devs(struct pci_bus *bus)
  446. {
  447. struct pci_dev *dev;
  448. while (!list_empty(&bus->devices)) {
  449. dev = container_of(bus->devices.next, struct pci_dev,
  450. bus_list);
  451. dev_dbg(&dev->dev, "removing device\n");
  452. pci_remove_bus_device(dev);
  453. }
  454. }
  455. static void pcifront_free_roots(struct pcifront_device *pdev)
  456. {
  457. struct pci_bus_entry *bus_entry, *t;
  458. dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n");
  459. list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) {
  460. list_del(&bus_entry->list);
  461. free_root_bus_devs(bus_entry->bus);
  462. kfree(bus_entry->bus->sysdata);
  463. device_unregister(bus_entry->bus->bridge);
  464. pci_remove_bus(bus_entry->bus);
  465. kfree(bus_entry);
  466. }
  467. }
  468. static pci_ers_result_t pcifront_common_process(int cmd,
  469. struct pcifront_device *pdev,
  470. pci_channel_state_t state)
  471. {
  472. pci_ers_result_t result;
  473. struct pci_driver *pdrv;
  474. int bus = pdev->sh_info->aer_op.bus;
  475. int devfn = pdev->sh_info->aer_op.devfn;
  476. struct pci_dev *pcidev;
  477. int flag = 0;
  478. dev_dbg(&pdev->xdev->dev,
  479. "pcifront AER process: cmd %x (bus:%x, devfn%x)",
  480. cmd, bus, devfn);
  481. result = PCI_ERS_RESULT_NONE;
  482. pcidev = pci_get_bus_and_slot(bus, devfn);
  483. if (!pcidev || !pcidev->driver) {
  484. dev_err(&pdev->xdev->dev, "device or AER driver is NULL\n");
  485. if (pcidev)
  486. pci_dev_put(pcidev);
  487. return result;
  488. }
  489. pdrv = pcidev->driver;
  490. if (get_driver(&pdrv->driver)) {
  491. if (pdrv->err_handler && pdrv->err_handler->error_detected) {
  492. dev_dbg(&pcidev->dev,
  493. "trying to call AER service\n");
  494. if (pcidev) {
  495. flag = 1;
  496. switch (cmd) {
  497. case XEN_PCI_OP_aer_detected:
  498. result = pdrv->err_handler->
  499. error_detected(pcidev, state);
  500. break;
  501. case XEN_PCI_OP_aer_mmio:
  502. result = pdrv->err_handler->
  503. mmio_enabled(pcidev);
  504. break;
  505. case XEN_PCI_OP_aer_slotreset:
  506. result = pdrv->err_handler->
  507. slot_reset(pcidev);
  508. break;
  509. case XEN_PCI_OP_aer_resume:
  510. pdrv->err_handler->resume(pcidev);
  511. break;
  512. default:
  513. dev_err(&pdev->xdev->dev,
  514. "bad request in aer recovery "
  515. "operation!\n");
  516. }
  517. }
  518. }
  519. put_driver(&pdrv->driver);
  520. }
  521. if (!flag)
  522. result = PCI_ERS_RESULT_NONE;
  523. return result;
  524. }
  525. static void pcifront_do_aer(struct work_struct *data)
  526. {
  527. struct pcifront_device *pdev =
  528. container_of(data, struct pcifront_device, op_work);
  529. int cmd = pdev->sh_info->aer_op.cmd;
  530. pci_channel_state_t state =
  531. (pci_channel_state_t)pdev->sh_info->aer_op.err;
  532. /*If a pci_conf op is in progress,
  533. we have to wait until it is done before service aer op*/
  534. dev_dbg(&pdev->xdev->dev,
  535. "pcifront service aer bus %x devfn %x\n",
  536. pdev->sh_info->aer_op.bus, pdev->sh_info->aer_op.devfn);
  537. pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
  538. /* Post the operation to the guest. */
  539. wmb();
  540. clear_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags);
  541. notify_remote_via_evtchn(pdev->evtchn);
  542. /*in case of we lost an aer request in four lines time_window*/
  543. smp_mb__before_clear_bit();
  544. clear_bit(_PDEVB_op_active, &pdev->flags);
  545. smp_mb__after_clear_bit();
  546. schedule_pcifront_aer_op(pdev);
  547. }
  548. static irqreturn_t pcifront_handler_aer(int irq, void *dev)
  549. {
  550. struct pcifront_device *pdev = dev;
  551. schedule_pcifront_aer_op(pdev);
  552. return IRQ_HANDLED;
  553. }
  554. static int pcifront_connect(struct pcifront_device *pdev)
  555. {
  556. int err = 0;
  557. spin_lock(&pcifront_dev_lock);
  558. if (!pcifront_dev) {
  559. dev_info(&pdev->xdev->dev, "Installing PCI frontend\n");
  560. pcifront_dev = pdev;
  561. } else {
  562. dev_err(&pdev->xdev->dev, "PCI frontend already installed!\n");
  563. err = -EEXIST;
  564. }
  565. spin_unlock(&pcifront_dev_lock);
  566. return err;
  567. }
  568. static void pcifront_disconnect(struct pcifront_device *pdev)
  569. {
  570. spin_lock(&pcifront_dev_lock);
  571. if (pdev == pcifront_dev) {
  572. dev_info(&pdev->xdev->dev,
  573. "Disconnecting PCI Frontend Buses\n");
  574. pcifront_dev = NULL;
  575. }
  576. spin_unlock(&pcifront_dev_lock);
  577. }
  578. static struct pcifront_device *alloc_pdev(struct xenbus_device *xdev)
  579. {
  580. struct pcifront_device *pdev;
  581. pdev = kzalloc(sizeof(struct pcifront_device), GFP_KERNEL);
  582. if (pdev == NULL)
  583. goto out;
  584. pdev->sh_info =
  585. (struct xen_pci_sharedinfo *)__get_free_page(GFP_KERNEL);
  586. if (pdev->sh_info == NULL) {
  587. kfree(pdev);
  588. pdev = NULL;
  589. goto out;
  590. }
  591. pdev->sh_info->flags = 0;
  592. /*Flag for registering PV AER handler*/
  593. set_bit(_XEN_PCIB_AERHANDLER, (void *)&pdev->sh_info->flags);
  594. dev_set_drvdata(&xdev->dev, pdev);
  595. pdev->xdev = xdev;
  596. INIT_LIST_HEAD(&pdev->root_buses);
  597. spin_lock_init(&pdev->sh_info_lock);
  598. pdev->evtchn = INVALID_EVTCHN;
  599. pdev->gnt_ref = INVALID_GRANT_REF;
  600. pdev->irq = -1;
  601. INIT_WORK(&pdev->op_work, pcifront_do_aer);
  602. dev_dbg(&xdev->dev, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n",
  603. pdev, pdev->sh_info);
  604. out:
  605. return pdev;
  606. }
  607. static void free_pdev(struct pcifront_device *pdev)
  608. {
  609. dev_dbg(&pdev->xdev->dev, "freeing pdev @ 0x%p\n", pdev);
  610. pcifront_free_roots(pdev);
  611. cancel_work_sync(&pdev->op_work);
  612. if (pdev->irq >= 0)
  613. unbind_from_irqhandler(pdev->irq, pdev);
  614. if (pdev->evtchn != INVALID_EVTCHN)
  615. xenbus_free_evtchn(pdev->xdev, pdev->evtchn);
  616. if (pdev->gnt_ref != INVALID_GRANT_REF)
  617. gnttab_end_foreign_access(pdev->gnt_ref, 0 /* r/w page */,
  618. (unsigned long)pdev->sh_info);
  619. else
  620. free_page((unsigned long)pdev->sh_info);
  621. dev_set_drvdata(&pdev->xdev->dev, NULL);
  622. kfree(pdev);
  623. }
  624. static int pcifront_publish_info(struct pcifront_device *pdev)
  625. {
  626. int err = 0;
  627. struct xenbus_transaction trans;
  628. err = xenbus_grant_ring(pdev->xdev, virt_to_mfn(pdev->sh_info));
  629. if (err < 0)
  630. goto out;
  631. pdev->gnt_ref = err;
  632. err = xenbus_alloc_evtchn(pdev->xdev, &pdev->evtchn);
  633. if (err)
  634. goto out;
  635. err = bind_evtchn_to_irqhandler(pdev->evtchn, pcifront_handler_aer,
  636. 0, "pcifront", pdev);
  637. if (err < 0)
  638. return err;
  639. pdev->irq = err;
  640. do_publish:
  641. err = xenbus_transaction_start(&trans);
  642. if (err) {
  643. xenbus_dev_fatal(pdev->xdev, err,
  644. "Error writing configuration for backend "
  645. "(start transaction)");
  646. goto out;
  647. }
  648. err = xenbus_printf(trans, pdev->xdev->nodename,
  649. "pci-op-ref", "%u", pdev->gnt_ref);
  650. if (!err)
  651. err = xenbus_printf(trans, pdev->xdev->nodename,
  652. "event-channel", "%u", pdev->evtchn);
  653. if (!err)
  654. err = xenbus_printf(trans, pdev->xdev->nodename,
  655. "magic", XEN_PCI_MAGIC);
  656. if (err) {
  657. xenbus_transaction_end(trans, 1);
  658. xenbus_dev_fatal(pdev->xdev, err,
  659. "Error writing configuration for backend");
  660. goto out;
  661. } else {
  662. err = xenbus_transaction_end(trans, 0);
  663. if (err == -EAGAIN)
  664. goto do_publish;
  665. else if (err) {
  666. xenbus_dev_fatal(pdev->xdev, err,
  667. "Error completing transaction "
  668. "for backend");
  669. goto out;
  670. }
  671. }
  672. xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
  673. dev_dbg(&pdev->xdev->dev, "publishing successful!\n");
  674. out:
  675. return err;
  676. }
  677. static int __devinit pcifront_try_connect(struct pcifront_device *pdev)
  678. {
  679. int err = -EFAULT;
  680. int i, num_roots, len;
  681. char str[64];
  682. unsigned int domain, bus;
  683. /* Only connect once */
  684. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  685. XenbusStateInitialised)
  686. goto out;
  687. err = pcifront_connect(pdev);
  688. if (err) {
  689. xenbus_dev_fatal(pdev->xdev, err,
  690. "Error connecting PCI Frontend");
  691. goto out;
  692. }
  693. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
  694. "root_num", "%d", &num_roots);
  695. if (err == -ENOENT) {
  696. xenbus_dev_error(pdev->xdev, err,
  697. "No PCI Roots found, trying 0000:00");
  698. err = pcifront_scan_root(pdev, 0, 0);
  699. num_roots = 0;
  700. } else if (err != 1) {
  701. if (err == 0)
  702. err = -EINVAL;
  703. xenbus_dev_fatal(pdev->xdev, err,
  704. "Error reading number of PCI roots");
  705. goto out;
  706. }
  707. for (i = 0; i < num_roots; i++) {
  708. len = snprintf(str, sizeof(str), "root-%d", i);
  709. if (unlikely(len >= (sizeof(str) - 1))) {
  710. err = -ENOMEM;
  711. goto out;
  712. }
  713. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
  714. "%x:%x", &domain, &bus);
  715. if (err != 2) {
  716. if (err >= 0)
  717. err = -EINVAL;
  718. xenbus_dev_fatal(pdev->xdev, err,
  719. "Error reading PCI root %d", i);
  720. goto out;
  721. }
  722. err = pcifront_scan_root(pdev, domain, bus);
  723. if (err) {
  724. xenbus_dev_fatal(pdev->xdev, err,
  725. "Error scanning PCI root %04x:%02x",
  726. domain, bus);
  727. goto out;
  728. }
  729. }
  730. err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
  731. out:
  732. return err;
  733. }
  734. static int pcifront_try_disconnect(struct pcifront_device *pdev)
  735. {
  736. int err = 0;
  737. enum xenbus_state prev_state;
  738. prev_state = xenbus_read_driver_state(pdev->xdev->nodename);
  739. if (prev_state >= XenbusStateClosing)
  740. goto out;
  741. if (prev_state == XenbusStateConnected) {
  742. pcifront_free_roots(pdev);
  743. pcifront_disconnect(pdev);
  744. }
  745. err = xenbus_switch_state(pdev->xdev, XenbusStateClosed);
  746. out:
  747. return err;
  748. }
  749. static int __devinit pcifront_attach_devices(struct pcifront_device *pdev)
  750. {
  751. int err = -EFAULT;
  752. int i, num_roots, len;
  753. unsigned int domain, bus;
  754. char str[64];
  755. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  756. XenbusStateReconfiguring)
  757. goto out;
  758. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
  759. "root_num", "%d", &num_roots);
  760. if (err == -ENOENT) {
  761. xenbus_dev_error(pdev->xdev, err,
  762. "No PCI Roots found, trying 0000:00");
  763. err = pcifront_rescan_root(pdev, 0, 0);
  764. num_roots = 0;
  765. } else if (err != 1) {
  766. if (err == 0)
  767. err = -EINVAL;
  768. xenbus_dev_fatal(pdev->xdev, err,
  769. "Error reading number of PCI roots");
  770. goto out;
  771. }
  772. for (i = 0; i < num_roots; i++) {
  773. len = snprintf(str, sizeof(str), "root-%d", i);
  774. if (unlikely(len >= (sizeof(str) - 1))) {
  775. err = -ENOMEM;
  776. goto out;
  777. }
  778. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
  779. "%x:%x", &domain, &bus);
  780. if (err != 2) {
  781. if (err >= 0)
  782. err = -EINVAL;
  783. xenbus_dev_fatal(pdev->xdev, err,
  784. "Error reading PCI root %d", i);
  785. goto out;
  786. }
  787. err = pcifront_rescan_root(pdev, domain, bus);
  788. if (err) {
  789. xenbus_dev_fatal(pdev->xdev, err,
  790. "Error scanning PCI root %04x:%02x",
  791. domain, bus);
  792. goto out;
  793. }
  794. }
  795. xenbus_switch_state(pdev->xdev, XenbusStateConnected);
  796. out:
  797. return err;
  798. }
  799. static int pcifront_detach_devices(struct pcifront_device *pdev)
  800. {
  801. int err = 0;
  802. int i, num_devs;
  803. unsigned int domain, bus, slot, func;
  804. struct pci_bus *pci_bus;
  805. struct pci_dev *pci_dev;
  806. char str[64];
  807. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  808. XenbusStateConnected)
  809. goto out;
  810. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "num_devs", "%d",
  811. &num_devs);
  812. if (err != 1) {
  813. if (err >= 0)
  814. err = -EINVAL;
  815. xenbus_dev_fatal(pdev->xdev, err,
  816. "Error reading number of PCI devices");
  817. goto out;
  818. }
  819. /* Find devices being detached and remove them. */
  820. for (i = 0; i < num_devs; i++) {
  821. int l, state;
  822. l = snprintf(str, sizeof(str), "state-%d", i);
  823. if (unlikely(l >= (sizeof(str) - 1))) {
  824. err = -ENOMEM;
  825. goto out;
  826. }
  827. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, "%d",
  828. &state);
  829. if (err != 1)
  830. state = XenbusStateUnknown;
  831. if (state != XenbusStateClosing)
  832. continue;
  833. /* Remove device. */
  834. l = snprintf(str, sizeof(str), "vdev-%d", i);
  835. if (unlikely(l >= (sizeof(str) - 1))) {
  836. err = -ENOMEM;
  837. goto out;
  838. }
  839. err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
  840. "%x:%x:%x.%x", &domain, &bus, &slot, &func);
  841. if (err != 4) {
  842. if (err >= 0)
  843. err = -EINVAL;
  844. xenbus_dev_fatal(pdev->xdev, err,
  845. "Error reading PCI device %d", i);
  846. goto out;
  847. }
  848. pci_bus = pci_find_bus(domain, bus);
  849. if (!pci_bus) {
  850. dev_dbg(&pdev->xdev->dev, "Cannot get bus %04x:%02x\n",
  851. domain, bus);
  852. continue;
  853. }
  854. pci_dev = pci_get_slot(pci_bus, PCI_DEVFN(slot, func));
  855. if (!pci_dev) {
  856. dev_dbg(&pdev->xdev->dev,
  857. "Cannot get PCI device %04x:%02x:%02x.%02x\n",
  858. domain, bus, slot, func);
  859. continue;
  860. }
  861. pci_remove_bus_device(pci_dev);
  862. pci_dev_put(pci_dev);
  863. dev_dbg(&pdev->xdev->dev,
  864. "PCI device %04x:%02x:%02x.%02x removed.\n",
  865. domain, bus, slot, func);
  866. }
  867. err = xenbus_switch_state(pdev->xdev, XenbusStateReconfiguring);
  868. out:
  869. return err;
  870. }
  871. static void __init_refok pcifront_backend_changed(struct xenbus_device *xdev,
  872. enum xenbus_state be_state)
  873. {
  874. struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
  875. switch (be_state) {
  876. case XenbusStateUnknown:
  877. case XenbusStateInitialising:
  878. case XenbusStateInitWait:
  879. case XenbusStateInitialised:
  880. case XenbusStateClosed:
  881. break;
  882. case XenbusStateConnected:
  883. pcifront_try_connect(pdev);
  884. break;
  885. case XenbusStateClosing:
  886. dev_warn(&xdev->dev, "backend going away!\n");
  887. pcifront_try_disconnect(pdev);
  888. break;
  889. case XenbusStateReconfiguring:
  890. pcifront_detach_devices(pdev);
  891. break;
  892. case XenbusStateReconfigured:
  893. pcifront_attach_devices(pdev);
  894. break;
  895. }
  896. }
  897. static int pcifront_xenbus_probe(struct xenbus_device *xdev,
  898. const struct xenbus_device_id *id)
  899. {
  900. int err = 0;
  901. struct pcifront_device *pdev = alloc_pdev(xdev);
  902. if (pdev == NULL) {
  903. err = -ENOMEM;
  904. xenbus_dev_fatal(xdev, err,
  905. "Error allocating pcifront_device struct");
  906. goto out;
  907. }
  908. err = pcifront_publish_info(pdev);
  909. if (err)
  910. free_pdev(pdev);
  911. out:
  912. return err;
  913. }
  914. static int pcifront_xenbus_remove(struct xenbus_device *xdev)
  915. {
  916. struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
  917. if (pdev)
  918. free_pdev(pdev);
  919. return 0;
  920. }
  921. static const struct xenbus_device_id xenpci_ids[] = {
  922. {"pci"},
  923. {""},
  924. };
  925. static struct xenbus_driver xenbus_pcifront_driver = {
  926. .name = "pcifront",
  927. .owner = THIS_MODULE,
  928. .ids = xenpci_ids,
  929. .probe = pcifront_xenbus_probe,
  930. .remove = pcifront_xenbus_remove,
  931. .otherend_changed = pcifront_backend_changed,
  932. };
  933. static int __init pcifront_init(void)
  934. {
  935. if (!xen_pv_domain() || xen_initial_domain())
  936. return -ENODEV;
  937. pci_frontend_registrar(1 /* enable */);
  938. return xenbus_register_frontend(&xenbus_pcifront_driver);
  939. }
  940. static void __exit pcifront_cleanup(void)
  941. {
  942. xenbus_unregister_driver(&xenbus_pcifront_driver);
  943. pci_frontend_registrar(0 /* disable */);
  944. }
  945. module_init(pcifront_init);
  946. module_exit(pcifront_cleanup);
  947. MODULE_DESCRIPTION("Xen PCI passthrough frontend.");
  948. MODULE_LICENSE("GPL");
  949. MODULE_ALIAS("xen:pci");