xen-pcifront.c 27 KB

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