pciback_ops.c 10 KB

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