xen-pcifront.c 26 KB

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