pciback_ops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Backend Operations - respond to PCI requests from Frontend
  4. *
  5. * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/moduleparam.h>
  9. #include <linux/wait.h>
  10. #include <linux/bitops.h>
  11. #include <xen/events.h>
  12. #include <linux/sched.h>
  13. #include "pciback.h"
  14. int verbose_request;
  15. module_param(verbose_request, int, 0644);
  16. static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id);
  17. /* Ensure a device is has the fake IRQ handler "turned on/off" and is
  18. * ready to be exported. This MUST be run after xen_pcibk_reset_device
  19. * which does the actual PCI device enable/disable.
  20. */
  21. static void xen_pcibk_control_isr(struct pci_dev *dev, int reset)
  22. {
  23. struct xen_pcibk_dev_data *dev_data;
  24. int rc;
  25. int enable = 0;
  26. dev_data = pci_get_drvdata(dev);
  27. if (!dev_data)
  28. return;
  29. /* We don't deal with bridges */
  30. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  31. return;
  32. if (reset) {
  33. dev_data->enable_intx = 0;
  34. dev_data->ack_intr = 0;
  35. }
  36. enable = dev_data->enable_intx;
  37. /* Asked to disable, but ISR isn't runnig */
  38. if (!enable && !dev_data->isr_on)
  39. return;
  40. /* Squirrel away the IRQs in the dev_data. We need this
  41. * b/c when device transitions to MSI, the dev->irq is
  42. * overwritten with the MSI vector.
  43. */
  44. if (enable)
  45. dev_data->irq = dev->irq;
  46. /*
  47. * SR-IOV devices in all use MSI-X and have no legacy
  48. * interrupts, so inhibit creating a fake IRQ handler for them.
  49. */
  50. if (dev_data->irq == 0)
  51. goto out;
  52. dev_dbg(&dev->dev, "%s: #%d %s %s%s %s-> %s\n",
  53. dev_data->irq_name,
  54. dev_data->irq,
  55. pci_is_enabled(dev) ? "on" : "off",
  56. dev->msi_enabled ? "MSI" : "",
  57. dev->msix_enabled ? "MSI/X" : "",
  58. dev_data->isr_on ? "enable" : "disable",
  59. enable ? "enable" : "disable");
  60. if (enable) {
  61. /*
  62. * The MSI or MSI-X should not have an IRQ handler. Otherwise
  63. * if the guest terminates we BUG_ON in free_msi_irqs.
  64. */
  65. if (dev->msi_enabled || dev->msix_enabled)
  66. goto out;
  67. rc = request_irq(dev_data->irq,
  68. xen_pcibk_guest_interrupt, IRQF_SHARED,
  69. dev_data->irq_name, dev);
  70. if (rc) {
  71. dev_err(&dev->dev, "%s: failed to install fake IRQ " \
  72. "handler for IRQ %d! (rc:%d)\n",
  73. dev_data->irq_name, dev_data->irq, rc);
  74. goto out;
  75. }
  76. } else {
  77. free_irq(dev_data->irq, dev);
  78. dev_data->irq = 0;
  79. }
  80. dev_data->isr_on = enable;
  81. dev_data->ack_intr = enable;
  82. out:
  83. dev_dbg(&dev->dev, "%s: #%d %s %s%s %s\n",
  84. dev_data->irq_name,
  85. dev_data->irq,
  86. pci_is_enabled(dev) ? "on" : "off",
  87. dev->msi_enabled ? "MSI" : "",
  88. dev->msix_enabled ? "MSI/X" : "",
  89. enable ? (dev_data->isr_on ? "enabled" : "failed to enable") :
  90. (dev_data->isr_on ? "failed to disable" : "disabled"));
  91. }
  92. /* Ensure a device is "turned off" and ready to be exported.
  93. * (Also see xen_pcibk_config_reset to ensure virtual configuration space is
  94. * ready to be re-exported)
  95. */
  96. void xen_pcibk_reset_device(struct pci_dev *dev)
  97. {
  98. u16 cmd;
  99. xen_pcibk_control_isr(dev, 1 /* reset device */);
  100. /* Disable devices (but not bridges) */
  101. if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
  102. #ifdef CONFIG_PCI_MSI
  103. /* The guest could have been abruptly killed without
  104. * disabling MSI/MSI-X interrupts.*/
  105. if (dev->msix_enabled)
  106. pci_disable_msix(dev);
  107. if (dev->msi_enabled)
  108. pci_disable_msi(dev);
  109. #endif
  110. if (pci_is_enabled(dev))
  111. pci_disable_device(dev);
  112. dev->is_busmaster = 0;
  113. } else {
  114. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  115. if (cmd & (PCI_COMMAND_INVALIDATE)) {
  116. cmd &= ~(PCI_COMMAND_INVALIDATE);
  117. pci_write_config_word(dev, PCI_COMMAND, cmd);
  118. dev->is_busmaster = 0;
  119. }
  120. }
  121. }
  122. #ifdef CONFIG_PCI_MSI
  123. static
  124. int xen_pcibk_enable_msi(struct xen_pcibk_device *pdev,
  125. struct pci_dev *dev, struct xen_pci_op *op)
  126. {
  127. struct xen_pcibk_dev_data *dev_data;
  128. int status;
  129. if (unlikely(verbose_request))
  130. printk(KERN_DEBUG DRV_NAME ": %s: enable MSI\n", pci_name(dev));
  131. if (dev->msi_enabled)
  132. status = -EALREADY;
  133. else if (dev->msix_enabled)
  134. status = -ENXIO;
  135. else
  136. status = pci_enable_msi(dev);
  137. if (status) {
  138. pr_warn_ratelimited("%s: error enabling MSI for guest %u: err %d\n",
  139. pci_name(dev), pdev->xdev->otherend_id,
  140. status);
  141. op->value = 0;
  142. return XEN_PCI_ERR_op_failed;
  143. }
  144. /* The value the guest needs is actually the IDT vector, not the
  145. * the local domain's IRQ number. */
  146. op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
  147. if (unlikely(verbose_request))
  148. printk(KERN_DEBUG DRV_NAME ": %s: MSI: %d\n", pci_name(dev),
  149. op->value);
  150. dev_data = pci_get_drvdata(dev);
  151. if (dev_data)
  152. dev_data->ack_intr = 0;
  153. return 0;
  154. }
  155. static
  156. int xen_pcibk_disable_msi(struct xen_pcibk_device *pdev,
  157. struct pci_dev *dev, struct xen_pci_op *op)
  158. {
  159. if (unlikely(verbose_request))
  160. printk(KERN_DEBUG DRV_NAME ": %s: disable MSI\n",
  161. pci_name(dev));
  162. if (dev->msi_enabled) {
  163. struct xen_pcibk_dev_data *dev_data;
  164. pci_disable_msi(dev);
  165. dev_data = pci_get_drvdata(dev);
  166. if (dev_data)
  167. dev_data->ack_intr = 1;
  168. }
  169. op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
  170. if (unlikely(verbose_request))
  171. printk(KERN_DEBUG DRV_NAME ": %s: MSI: %d\n", pci_name(dev),
  172. op->value);
  173. return 0;
  174. }
  175. static
  176. int xen_pcibk_enable_msix(struct xen_pcibk_device *pdev,
  177. struct pci_dev *dev, struct xen_pci_op *op)
  178. {
  179. struct xen_pcibk_dev_data *dev_data;
  180. int i, result;
  181. struct msix_entry *entries;
  182. u16 cmd;
  183. if (unlikely(verbose_request))
  184. printk(KERN_DEBUG DRV_NAME ": %s: enable MSI-X\n",
  185. pci_name(dev));
  186. if (op->value > SH_INFO_MAX_VEC)
  187. return -EINVAL;
  188. if (dev->msix_enabled)
  189. return -EALREADY;
  190. /*
  191. * PCI_COMMAND_MEMORY must be enabled, otherwise we may not be able
  192. * to access the BARs where the MSI-X entries reside.
  193. * But VF devices are unique in which the PF needs to be checked.
  194. */
  195. pci_read_config_word(pci_physfn(dev), PCI_COMMAND, &cmd);
  196. if (dev->msi_enabled || !(cmd & PCI_COMMAND_MEMORY))
  197. return -ENXIO;
  198. entries = kmalloc(op->value * sizeof(*entries), GFP_KERNEL);
  199. if (entries == NULL)
  200. return -ENOMEM;
  201. for (i = 0; i < op->value; i++) {
  202. entries[i].entry = op->msix_entries[i].entry;
  203. entries[i].vector = op->msix_entries[i].vector;
  204. }
  205. result = pci_enable_msix_exact(dev, entries, op->value);
  206. if (result == 0) {
  207. for (i = 0; i < op->value; i++) {
  208. op->msix_entries[i].entry = entries[i].entry;
  209. if (entries[i].vector) {
  210. op->msix_entries[i].vector =
  211. xen_pirq_from_irq(entries[i].vector);
  212. if (unlikely(verbose_request))
  213. printk(KERN_DEBUG DRV_NAME ": %s: " \
  214. "MSI-X[%d]: %d\n",
  215. pci_name(dev), i,
  216. op->msix_entries[i].vector);
  217. }
  218. }
  219. } else
  220. pr_warn_ratelimited("%s: error enabling MSI-X for guest %u: err %d!\n",
  221. pci_name(dev), pdev->xdev->otherend_id,
  222. result);
  223. kfree(entries);
  224. op->value = result;
  225. dev_data = pci_get_drvdata(dev);
  226. if (dev_data)
  227. dev_data->ack_intr = 0;
  228. return result > 0 ? 0 : result;
  229. }
  230. static
  231. int xen_pcibk_disable_msix(struct xen_pcibk_device *pdev,
  232. struct pci_dev *dev, struct xen_pci_op *op)
  233. {
  234. if (unlikely(verbose_request))
  235. printk(KERN_DEBUG DRV_NAME ": %s: disable MSI-X\n",
  236. pci_name(dev));
  237. if (dev->msix_enabled) {
  238. struct xen_pcibk_dev_data *dev_data;
  239. pci_disable_msix(dev);
  240. dev_data = pci_get_drvdata(dev);
  241. if (dev_data)
  242. dev_data->ack_intr = 1;
  243. }
  244. /*
  245. * SR-IOV devices (which don't have any legacy IRQ) have
  246. * an undefined IRQ value of zero.
  247. */
  248. op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
  249. if (unlikely(verbose_request))
  250. printk(KERN_DEBUG DRV_NAME ": %s: MSI-X: %d\n",
  251. pci_name(dev), op->value);
  252. return 0;
  253. }
  254. #endif
  255. static inline bool xen_pcibk_test_op_pending(struct xen_pcibk_device *pdev)
  256. {
  257. return test_bit(_XEN_PCIF_active,
  258. (unsigned long *)&pdev->sh_info->flags) &&
  259. !test_and_set_bit(_PDEVF_op_active, &pdev->flags);
  260. }
  261. /*
  262. * Now the same evtchn is used for both pcifront conf_read_write request
  263. * as well as pcie aer front end ack. We use a new work_queue to schedule
  264. * xen_pcibk conf_read_write service for avoiding confict with aer_core
  265. * do_recovery job which also use the system default work_queue
  266. */
  267. static void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
  268. {
  269. bool eoi = true;
  270. /* Check that frontend is requesting an operation and that we are not
  271. * already processing a request */
  272. if (xen_pcibk_test_op_pending(pdev)) {
  273. schedule_work(&pdev->op_work);
  274. eoi = false;
  275. }
  276. /*_XEN_PCIB_active should have been cleared by pcifront. And also make
  277. sure xen_pcibk is waiting for ack by checking _PCIB_op_pending*/
  278. if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
  279. && test_bit(_PCIB_op_pending, &pdev->flags)) {
  280. wake_up(&xen_pcibk_aer_wait_queue);
  281. eoi = false;
  282. }
  283. /* EOI if there was nothing to do. */
  284. if (eoi)
  285. xen_pcibk_lateeoi(pdev, XEN_EOI_FLAG_SPURIOUS);
  286. }
  287. /* Performing the configuration space reads/writes must not be done in atomic
  288. * context because some of the pci_* functions can sleep (mostly due to ACPI
  289. * use of semaphores). This function is intended to be called from a work
  290. * queue in process context taking a struct xen_pcibk_device as a parameter */
  291. static void xen_pcibk_do_one_op(struct xen_pcibk_device *pdev)
  292. {
  293. struct pci_dev *dev;
  294. struct xen_pcibk_dev_data *dev_data = NULL;
  295. struct xen_pci_op *op = &pdev->op;
  296. int test_intx = 0;
  297. #ifdef CONFIG_PCI_MSI
  298. unsigned int nr = 0;
  299. #endif
  300. *op = pdev->sh_info->op;
  301. barrier();
  302. dev = xen_pcibk_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
  303. if (dev == NULL)
  304. op->err = XEN_PCI_ERR_dev_not_found;
  305. else {
  306. dev_data = pci_get_drvdata(dev);
  307. if (dev_data)
  308. test_intx = dev_data->enable_intx;
  309. switch (op->cmd) {
  310. case XEN_PCI_OP_conf_read:
  311. op->err = xen_pcibk_config_read(dev,
  312. op->offset, op->size, &op->value);
  313. break;
  314. case XEN_PCI_OP_conf_write:
  315. op->err = xen_pcibk_config_write(dev,
  316. op->offset, op->size, op->value);
  317. break;
  318. #ifdef CONFIG_PCI_MSI
  319. case XEN_PCI_OP_enable_msi:
  320. op->err = xen_pcibk_enable_msi(pdev, dev, op);
  321. break;
  322. case XEN_PCI_OP_disable_msi:
  323. op->err = xen_pcibk_disable_msi(pdev, dev, op);
  324. break;
  325. case XEN_PCI_OP_enable_msix:
  326. nr = op->value;
  327. op->err = xen_pcibk_enable_msix(pdev, dev, op);
  328. break;
  329. case XEN_PCI_OP_disable_msix:
  330. op->err = xen_pcibk_disable_msix(pdev, dev, op);
  331. break;
  332. #endif
  333. default:
  334. op->err = XEN_PCI_ERR_not_implemented;
  335. break;
  336. }
  337. }
  338. if (!op->err && dev && dev_data) {
  339. /* Transition detected */
  340. if ((dev_data->enable_intx != test_intx))
  341. xen_pcibk_control_isr(dev, 0 /* no reset */);
  342. }
  343. pdev->sh_info->op.err = op->err;
  344. pdev->sh_info->op.value = op->value;
  345. #ifdef CONFIG_PCI_MSI
  346. if (op->cmd == XEN_PCI_OP_enable_msix && op->err == 0) {
  347. unsigned int i;
  348. for (i = 0; i < nr; i++)
  349. pdev->sh_info->op.msix_entries[i].vector =
  350. op->msix_entries[i].vector;
  351. }
  352. #endif
  353. /* Tell the driver domain that we're done. */
  354. wmb();
  355. clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
  356. notify_remote_via_irq(pdev->evtchn_irq);
  357. /* Mark that we're done. */
  358. smp_mb__before_atomic(); /* /after/ clearing PCIF_active */
  359. clear_bit(_PDEVF_op_active, &pdev->flags);
  360. smp_mb__after_atomic(); /* /before/ final check for work */
  361. }
  362. void xen_pcibk_do_op(struct work_struct *data)
  363. {
  364. struct xen_pcibk_device *pdev =
  365. container_of(data, struct xen_pcibk_device, op_work);
  366. do {
  367. xen_pcibk_do_one_op(pdev);
  368. } while (xen_pcibk_test_op_pending(pdev));
  369. xen_pcibk_lateeoi(pdev, 0);
  370. }
  371. irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id)
  372. {
  373. struct xen_pcibk_device *pdev = dev_id;
  374. bool eoi;
  375. /* IRQs might come in before pdev->evtchn_irq is written. */
  376. if (unlikely(pdev->evtchn_irq != irq))
  377. pdev->evtchn_irq = irq;
  378. eoi = test_and_set_bit(_EOI_pending, &pdev->flags);
  379. WARN(eoi, "IRQ while EOI pending\n");
  380. xen_pcibk_test_and_schedule_op(pdev);
  381. return IRQ_HANDLED;
  382. }
  383. static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id)
  384. {
  385. struct pci_dev *dev = (struct pci_dev *)dev_id;
  386. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  387. if (dev_data->isr_on && dev_data->ack_intr) {
  388. dev_data->handled++;
  389. if ((dev_data->handled % 1000) == 0) {
  390. if (xen_test_irq_shared(irq)) {
  391. pr_info("%s IRQ line is not shared "
  392. "with other domains. Turning ISR off\n",
  393. dev_data->irq_name);
  394. dev_data->ack_intr = 0;
  395. }
  396. }
  397. return IRQ_HANDLED;
  398. }
  399. return IRQ_NONE;
  400. }