xen-pcifront.c 27 KB

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