xen-pcifront.c 26 KB

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