pciback_ops.c 10 KB

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