pciback_ops.c 10 KB

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