pciback_ops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. #include <linux/ratelimit.h>
  13. #include <linux/printk.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(DRV_NAME ": %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. */
  194. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  195. if (dev->msi_enabled || !(cmd & PCI_COMMAND_MEMORY))
  196. return -ENXIO;
  197. entries = kmalloc(op->value * sizeof(*entries), GFP_KERNEL);
  198. if (entries == NULL)
  199. return -ENOMEM;
  200. for (i = 0; i < op->value; i++) {
  201. entries[i].entry = op->msix_entries[i].entry;
  202. entries[i].vector = op->msix_entries[i].vector;
  203. }
  204. result = pci_enable_msix(dev, entries, op->value);
  205. if (result == 0) {
  206. for (i = 0; i < op->value; i++) {
  207. op->msix_entries[i].entry = entries[i].entry;
  208. if (entries[i].vector)
  209. op->msix_entries[i].vector =
  210. xen_pirq_from_irq(entries[i].vector);
  211. if (unlikely(verbose_request))
  212. printk(KERN_DEBUG DRV_NAME ": %s: " \
  213. "MSI-X[%d]: %d\n",
  214. pci_name(dev), i,
  215. op->msix_entries[i].vector);
  216. }
  217. } else
  218. pr_warn_ratelimited(DRV_NAME ": %s: error enabling MSI-X for guest %u: err %d!\n",
  219. pci_name(dev), pdev->xdev->otherend_id,
  220. result);
  221. kfree(entries);
  222. op->value = result;
  223. dev_data = pci_get_drvdata(dev);
  224. if (dev_data)
  225. dev_data->ack_intr = 0;
  226. return result > 0 ? 0 : result;
  227. }
  228. static
  229. int xen_pcibk_disable_msix(struct xen_pcibk_device *pdev,
  230. struct pci_dev *dev, struct xen_pci_op *op)
  231. {
  232. if (unlikely(verbose_request))
  233. printk(KERN_DEBUG DRV_NAME ": %s: disable MSI-X\n",
  234. pci_name(dev));
  235. if (dev->msix_enabled) {
  236. struct xen_pcibk_dev_data *dev_data;
  237. pci_disable_msix(dev);
  238. dev_data = pci_get_drvdata(dev);
  239. if (dev_data)
  240. dev_data->ack_intr = 1;
  241. }
  242. /*
  243. * SR-IOV devices (which don't have any legacy IRQ) have
  244. * an undefined IRQ value of zero.
  245. */
  246. op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
  247. if (unlikely(verbose_request))
  248. printk(KERN_DEBUG DRV_NAME ": %s: MSI-X: %d\n",
  249. pci_name(dev), op->value);
  250. return 0;
  251. }
  252. #endif
  253. /*
  254. * Now the same evtchn is used for both pcifront conf_read_write request
  255. * as well as pcie aer front end ack. We use a new work_queue to schedule
  256. * xen_pcibk conf_read_write service for avoiding confict with aer_core
  257. * do_recovery job which also use the system default work_queue
  258. */
  259. void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
  260. {
  261. /* Check that frontend is requesting an operation and that we are not
  262. * already processing a request */
  263. if (test_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags)
  264. && !test_and_set_bit(_PDEVF_op_active, &pdev->flags)) {
  265. queue_work(xen_pcibk_wq, &pdev->op_work);
  266. }
  267. /*_XEN_PCIB_active should have been cleared by pcifront. And also make
  268. sure xen_pcibk is waiting for ack by checking _PCIB_op_pending*/
  269. if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
  270. && test_bit(_PCIB_op_pending, &pdev->flags)) {
  271. wake_up(&xen_pcibk_aer_wait_queue);
  272. }
  273. }
  274. /* Performing the configuration space reads/writes must not be done in atomic
  275. * context because some of the pci_* functions can sleep (mostly due to ACPI
  276. * use of semaphores). This function is intended to be called from a work
  277. * queue in process context taking a struct xen_pcibk_device as a parameter */
  278. void xen_pcibk_do_op(struct work_struct *data)
  279. {
  280. struct xen_pcibk_device *pdev =
  281. container_of(data, struct xen_pcibk_device, op_work);
  282. struct pci_dev *dev;
  283. struct xen_pcibk_dev_data *dev_data = NULL;
  284. struct xen_pci_op *op = &pdev->op;
  285. int test_intx = 0;
  286. #ifdef CONFIG_PCI_MSI
  287. unsigned int nr = 0;
  288. #endif
  289. *op = pdev->sh_info->op;
  290. barrier();
  291. dev = xen_pcibk_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
  292. if (dev == NULL)
  293. op->err = XEN_PCI_ERR_dev_not_found;
  294. else {
  295. dev_data = pci_get_drvdata(dev);
  296. if (dev_data)
  297. test_intx = dev_data->enable_intx;
  298. switch (op->cmd) {
  299. case XEN_PCI_OP_conf_read:
  300. op->err = xen_pcibk_config_read(dev,
  301. op->offset, op->size, &op->value);
  302. break;
  303. case XEN_PCI_OP_conf_write:
  304. op->err = xen_pcibk_config_write(dev,
  305. op->offset, op->size, op->value);
  306. break;
  307. #ifdef CONFIG_PCI_MSI
  308. case XEN_PCI_OP_enable_msi:
  309. op->err = xen_pcibk_enable_msi(pdev, dev, op);
  310. break;
  311. case XEN_PCI_OP_disable_msi:
  312. op->err = xen_pcibk_disable_msi(pdev, dev, op);
  313. break;
  314. case XEN_PCI_OP_enable_msix:
  315. nr = op->value;
  316. op->err = xen_pcibk_enable_msix(pdev, dev, op);
  317. break;
  318. case XEN_PCI_OP_disable_msix:
  319. op->err = xen_pcibk_disable_msix(pdev, dev, op);
  320. break;
  321. #endif
  322. default:
  323. op->err = XEN_PCI_ERR_not_implemented;
  324. break;
  325. }
  326. }
  327. if (!op->err && dev && dev_data) {
  328. /* Transition detected */
  329. if ((dev_data->enable_intx != test_intx))
  330. xen_pcibk_control_isr(dev, 0 /* no reset */);
  331. }
  332. pdev->sh_info->op.err = op->err;
  333. pdev->sh_info->op.value = op->value;
  334. #ifdef CONFIG_PCI_MSI
  335. if (op->cmd == XEN_PCI_OP_enable_msix && op->err == 0) {
  336. unsigned int i;
  337. for (i = 0; i < nr; i++)
  338. pdev->sh_info->op.msix_entries[i].vector =
  339. op->msix_entries[i].vector;
  340. }
  341. #endif
  342. /* Tell the driver domain that we're done. */
  343. wmb();
  344. clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
  345. notify_remote_via_irq(pdev->evtchn_irq);
  346. /* Mark that we're done. */
  347. smp_mb__before_clear_bit(); /* /after/ clearing PCIF_active */
  348. clear_bit(_PDEVF_op_active, &pdev->flags);
  349. smp_mb__after_clear_bit(); /* /before/ final check for work */
  350. /* Check to see if the driver domain tried to start another request in
  351. * between clearing _XEN_PCIF_active and clearing _PDEVF_op_active.
  352. */
  353. xen_pcibk_test_and_schedule_op(pdev);
  354. }
  355. irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id)
  356. {
  357. struct xen_pcibk_device *pdev = dev_id;
  358. xen_pcibk_test_and_schedule_op(pdev);
  359. return IRQ_HANDLED;
  360. }
  361. static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id)
  362. {
  363. struct pci_dev *dev = (struct pci_dev *)dev_id;
  364. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  365. if (dev_data->isr_on && dev_data->ack_intr) {
  366. dev_data->handled++;
  367. if ((dev_data->handled % 1000) == 0) {
  368. if (xen_test_irq_shared(irq)) {
  369. printk(KERN_INFO "%s IRQ line is not shared "
  370. "with other domains. Turning ISR off\n",
  371. dev_data->irq_name);
  372. dev_data->ack_intr = 0;
  373. }
  374. }
  375. return IRQ_HANDLED;
  376. }
  377. return IRQ_NONE;
  378. }