pciback_ops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. pci_write_config_word(dev, PCI_COMMAND, 0);
  113. dev->is_busmaster = 0;
  114. } else {
  115. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  116. if (cmd & (PCI_COMMAND_INVALIDATE)) {
  117. cmd &= ~(PCI_COMMAND_INVALIDATE);
  118. pci_write_config_word(dev, PCI_COMMAND, cmd);
  119. dev->is_busmaster = 0;
  120. }
  121. }
  122. }
  123. #ifdef CONFIG_PCI_MSI
  124. static
  125. int xen_pcibk_enable_msi(struct xen_pcibk_device *pdev,
  126. struct pci_dev *dev, struct xen_pci_op *op)
  127. {
  128. struct xen_pcibk_dev_data *dev_data;
  129. int status;
  130. if (unlikely(verbose_request))
  131. printk(KERN_DEBUG DRV_NAME ": %s: enable MSI\n", pci_name(dev));
  132. if (dev->msi_enabled)
  133. status = -EALREADY;
  134. else if (dev->msix_enabled)
  135. status = -ENXIO;
  136. else
  137. status = pci_enable_msi(dev);
  138. if (status) {
  139. pr_warn_ratelimited("%s: error enabling MSI for guest %u: err %d\n",
  140. pci_name(dev), pdev->xdev->otherend_id,
  141. status);
  142. op->value = 0;
  143. return XEN_PCI_ERR_op_failed;
  144. }
  145. /* The value the guest needs is actually the IDT vector, not the
  146. * the local domain's IRQ number. */
  147. op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
  148. if (unlikely(verbose_request))
  149. printk(KERN_DEBUG DRV_NAME ": %s: MSI: %d\n", pci_name(dev),
  150. op->value);
  151. dev_data = pci_get_drvdata(dev);
  152. if (dev_data)
  153. dev_data->ack_intr = 0;
  154. return 0;
  155. }
  156. static
  157. int xen_pcibk_disable_msi(struct xen_pcibk_device *pdev,
  158. struct pci_dev *dev, struct xen_pci_op *op)
  159. {
  160. if (unlikely(verbose_request))
  161. printk(KERN_DEBUG DRV_NAME ": %s: disable MSI\n",
  162. pci_name(dev));
  163. if (dev->msi_enabled) {
  164. struct xen_pcibk_dev_data *dev_data;
  165. pci_disable_msi(dev);
  166. dev_data = pci_get_drvdata(dev);
  167. if (dev_data)
  168. dev_data->ack_intr = 1;
  169. }
  170. op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
  171. if (unlikely(verbose_request))
  172. printk(KERN_DEBUG DRV_NAME ": %s: MSI: %d\n", pci_name(dev),
  173. op->value);
  174. return 0;
  175. }
  176. static
  177. int xen_pcibk_enable_msix(struct xen_pcibk_device *pdev,
  178. struct pci_dev *dev, struct xen_pci_op *op)
  179. {
  180. struct xen_pcibk_dev_data *dev_data;
  181. int i, result;
  182. struct msix_entry *entries;
  183. u16 cmd;
  184. if (unlikely(verbose_request))
  185. printk(KERN_DEBUG DRV_NAME ": %s: enable MSI-X\n",
  186. pci_name(dev));
  187. if (op->value > SH_INFO_MAX_VEC)
  188. return -EINVAL;
  189. if (dev->msix_enabled)
  190. return -EALREADY;
  191. /*
  192. * PCI_COMMAND_MEMORY must be enabled, otherwise we may not be able
  193. * to access the BARs where the MSI-X entries reside.
  194. * But VF devices are unique in which the PF needs to be checked.
  195. */
  196. pci_read_config_word(pci_physfn(dev), PCI_COMMAND, &cmd);
  197. if (dev->msi_enabled || !(cmd & PCI_COMMAND_MEMORY))
  198. return -ENXIO;
  199. entries = kmalloc_array(op->value, sizeof(*entries), GFP_KERNEL);
  200. if (entries == NULL)
  201. return -ENOMEM;
  202. for (i = 0; i < op->value; i++) {
  203. entries[i].entry = op->msix_entries[i].entry;
  204. entries[i].vector = op->msix_entries[i].vector;
  205. }
  206. result = pci_enable_msix_exact(dev, entries, op->value);
  207. if (result == 0) {
  208. for (i = 0; i < op->value; i++) {
  209. op->msix_entries[i].entry = entries[i].entry;
  210. if (entries[i].vector) {
  211. op->msix_entries[i].vector =
  212. xen_pirq_from_irq(entries[i].vector);
  213. if (unlikely(verbose_request))
  214. printk(KERN_DEBUG DRV_NAME ": %s: " \
  215. "MSI-X[%d]: %d\n",
  216. pci_name(dev), i,
  217. op->msix_entries[i].vector);
  218. }
  219. }
  220. } else
  221. pr_warn_ratelimited("%s: error enabling MSI-X for guest %u: err %d!\n",
  222. pci_name(dev), pdev->xdev->otherend_id,
  223. result);
  224. kfree(entries);
  225. op->value = result;
  226. dev_data = pci_get_drvdata(dev);
  227. if (dev_data)
  228. dev_data->ack_intr = 0;
  229. return result > 0 ? 0 : result;
  230. }
  231. static
  232. int xen_pcibk_disable_msix(struct xen_pcibk_device *pdev,
  233. struct pci_dev *dev, struct xen_pci_op *op)
  234. {
  235. if (unlikely(verbose_request))
  236. printk(KERN_DEBUG DRV_NAME ": %s: disable MSI-X\n",
  237. pci_name(dev));
  238. if (dev->msix_enabled) {
  239. struct xen_pcibk_dev_data *dev_data;
  240. pci_disable_msix(dev);
  241. dev_data = pci_get_drvdata(dev);
  242. if (dev_data)
  243. dev_data->ack_intr = 1;
  244. }
  245. /*
  246. * SR-IOV devices (which don't have any legacy IRQ) have
  247. * an undefined IRQ value of zero.
  248. */
  249. op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
  250. if (unlikely(verbose_request))
  251. printk(KERN_DEBUG DRV_NAME ": %s: MSI-X: %d\n",
  252. pci_name(dev), op->value);
  253. return 0;
  254. }
  255. #endif
  256. /*
  257. * Now the same evtchn is used for both pcifront conf_read_write request
  258. * as well as pcie aer front end ack. We use a new work_queue to schedule
  259. * xen_pcibk conf_read_write service for avoiding confict with aer_core
  260. * do_recovery job which also use the system default work_queue
  261. */
  262. void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
  263. {
  264. /* Check that frontend is requesting an operation and that we are not
  265. * already processing a request */
  266. if (test_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags)
  267. && !test_and_set_bit(_PDEVF_op_active, &pdev->flags)) {
  268. schedule_work(&pdev->op_work);
  269. }
  270. /*_XEN_PCIB_active should have been cleared by pcifront. And also make
  271. sure xen_pcibk is waiting for ack by checking _PCIB_op_pending*/
  272. if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
  273. && test_bit(_PCIB_op_pending, &pdev->flags)) {
  274. wake_up(&xen_pcibk_aer_wait_queue);
  275. }
  276. }
  277. /* Performing the configuration space reads/writes must not be done in atomic
  278. * context because some of the pci_* functions can sleep (mostly due to ACPI
  279. * use of semaphores). This function is intended to be called from a work
  280. * queue in process context taking a struct xen_pcibk_device as a parameter */
  281. void xen_pcibk_do_op(struct work_struct *data)
  282. {
  283. struct xen_pcibk_device *pdev =
  284. container_of(data, struct xen_pcibk_device, op_work);
  285. struct pci_dev *dev;
  286. struct xen_pcibk_dev_data *dev_data = NULL;
  287. struct xen_pci_op *op = &pdev->op;
  288. int test_intx = 0;
  289. #ifdef CONFIG_PCI_MSI
  290. unsigned int nr = 0;
  291. #endif
  292. *op = pdev->sh_info->op;
  293. barrier();
  294. dev = xen_pcibk_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
  295. if (dev == NULL)
  296. op->err = XEN_PCI_ERR_dev_not_found;
  297. else {
  298. dev_data = pci_get_drvdata(dev);
  299. if (dev_data)
  300. test_intx = dev_data->enable_intx;
  301. switch (op->cmd) {
  302. case XEN_PCI_OP_conf_read:
  303. op->err = xen_pcibk_config_read(dev,
  304. op->offset, op->size, &op->value);
  305. break;
  306. case XEN_PCI_OP_conf_write:
  307. op->err = xen_pcibk_config_write(dev,
  308. op->offset, op->size, op->value);
  309. break;
  310. #ifdef CONFIG_PCI_MSI
  311. case XEN_PCI_OP_enable_msi:
  312. op->err = xen_pcibk_enable_msi(pdev, dev, op);
  313. break;
  314. case XEN_PCI_OP_disable_msi:
  315. op->err = xen_pcibk_disable_msi(pdev, dev, op);
  316. break;
  317. case XEN_PCI_OP_enable_msix:
  318. nr = op->value;
  319. op->err = xen_pcibk_enable_msix(pdev, dev, op);
  320. break;
  321. case XEN_PCI_OP_disable_msix:
  322. op->err = xen_pcibk_disable_msix(pdev, dev, op);
  323. break;
  324. #endif
  325. default:
  326. op->err = XEN_PCI_ERR_not_implemented;
  327. break;
  328. }
  329. }
  330. if (!op->err && dev && dev_data) {
  331. /* Transition detected */
  332. if ((dev_data->enable_intx != test_intx))
  333. xen_pcibk_control_isr(dev, 0 /* no reset */);
  334. }
  335. pdev->sh_info->op.err = op->err;
  336. pdev->sh_info->op.value = op->value;
  337. #ifdef CONFIG_PCI_MSI
  338. if (op->cmd == XEN_PCI_OP_enable_msix && op->err == 0) {
  339. unsigned int i;
  340. for (i = 0; i < nr; i++)
  341. pdev->sh_info->op.msix_entries[i].vector =
  342. op->msix_entries[i].vector;
  343. }
  344. #endif
  345. /* Tell the driver domain that we're done. */
  346. wmb();
  347. clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
  348. notify_remote_via_irq(pdev->evtchn_irq);
  349. /* Mark that we're done. */
  350. smp_mb__before_atomic(); /* /after/ clearing PCIF_active */
  351. clear_bit(_PDEVF_op_active, &pdev->flags);
  352. smp_mb__after_atomic(); /* /before/ final check for work */
  353. /* Check to see if the driver domain tried to start another request in
  354. * between clearing _XEN_PCIF_active and clearing _PDEVF_op_active.
  355. */
  356. xen_pcibk_test_and_schedule_op(pdev);
  357. }
  358. irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id)
  359. {
  360. struct xen_pcibk_device *pdev = dev_id;
  361. xen_pcibk_test_and_schedule_op(pdev);
  362. return IRQ_HANDLED;
  363. }
  364. static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id)
  365. {
  366. struct pci_dev *dev = (struct pci_dev *)dev_id;
  367. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  368. if (dev_data->isr_on && dev_data->ack_intr) {
  369. dev_data->handled++;
  370. if ((dev_data->handled % 1000) == 0) {
  371. if (xen_test_irq_shared(irq)) {
  372. pr_info("%s IRQ line is not shared "
  373. "with other domains. Turning ISR off\n",
  374. dev_data->irq_name);
  375. dev_data->ack_intr = 0;
  376. }
  377. }
  378. return IRQ_HANDLED;
  379. }
  380. return IRQ_NONE;
  381. }