xen-pcifront.c 27 KB

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