xen-pcifront.c 27 KB

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