xen-pcifront.c 26 KB

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