pciback_ops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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/moduleparam.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. /*
  61. * The MSI or MSI-X should not have an IRQ handler. Otherwise
  62. * if the guest terminates we BUG_ON in free_msi_irqs.
  63. */
  64. if (dev->msi_enabled || dev->msix_enabled)
  65. goto out;
  66. rc = request_irq(dev_data->irq,
  67. xen_pcibk_guest_interrupt, IRQF_SHARED,
  68. dev_data->irq_name, dev);
  69. if (rc) {
  70. dev_err(&dev->dev, "%s: failed to install fake IRQ " \
  71. "handler for IRQ %d! (rc:%d)\n",
  72. dev_data->irq_name, dev_data->irq, rc);
  73. goto out;
  74. }
  75. } else {
  76. free_irq(dev_data->irq, dev);
  77. dev_data->irq = 0;
  78. }
  79. dev_data->isr_on = enable;
  80. dev_data->ack_intr = enable;
  81. out:
  82. dev_dbg(&dev->dev, "%s: #%d %s %s%s %s\n",
  83. dev_data->irq_name,
  84. dev_data->irq,
  85. pci_is_enabled(dev) ? "on" : "off",
  86. dev->msi_enabled ? "MSI" : "",
  87. dev->msix_enabled ? "MSI/X" : "",
  88. enable ? (dev_data->isr_on ? "enabled" : "failed to enable") :
  89. (dev_data->isr_on ? "failed to disable" : "disabled"));
  90. }
  91. /* Ensure a device is "turned off" and ready to be exported.
  92. * (Also see xen_pcibk_config_reset to ensure virtual configuration space is
  93. * ready to be re-exported)
  94. */
  95. void xen_pcibk_reset_device(struct pci_dev *dev)
  96. {
  97. u16 cmd;
  98. xen_pcibk_control_isr(dev, 1 /* reset device */);
  99. /* Disable devices (but not bridges) */
  100. if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
  101. #ifdef CONFIG_PCI_MSI
  102. /* The guest could have been abruptly killed without
  103. * disabling MSI/MSI-X interrupts.*/
  104. if (dev->msix_enabled)
  105. pci_disable_msix(dev);
  106. if (dev->msi_enabled)
  107. pci_disable_msi(dev);
  108. #endif
  109. if (pci_is_enabled(dev))
  110. pci_disable_device(dev);
  111. pci_write_config_word(dev, PCI_COMMAND, 0);
  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("%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. * But VF devices are unique in which the PF needs to be checked.
  194. */
  195. pci_read_config_word(pci_physfn(dev), PCI_COMMAND, &cmd);
  196. if (dev->msi_enabled || !(cmd & PCI_COMMAND_MEMORY))
  197. return -ENXIO;
  198. entries = kmalloc(op->value * sizeof(*entries), GFP_KERNEL);
  199. if (entries == NULL)
  200. return -ENOMEM;
  201. for (i = 0; i < op->value; i++) {
  202. entries[i].entry = op->msix_entries[i].entry;
  203. entries[i].vector = op->msix_entries[i].vector;
  204. }
  205. result = pci_enable_msix_exact(dev, entries, op->value);
  206. if (result == 0) {
  207. for (i = 0; i < op->value; i++) {
  208. op->msix_entries[i].entry = entries[i].entry;
  209. if (entries[i].vector) {
  210. op->msix_entries[i].vector =
  211. xen_pirq_from_irq(entries[i].vector);
  212. if (unlikely(verbose_request))
  213. printk(KERN_DEBUG DRV_NAME ": %s: " \
  214. "MSI-X[%d]: %d\n",
  215. pci_name(dev), i,
  216. op->msix_entries[i].vector);
  217. }
  218. }
  219. } else
  220. pr_warn_ratelimited("%s: error enabling MSI-X for guest %u: err %d!\n",
  221. pci_name(dev), pdev->xdev->otherend_id,
  222. result);
  223. kfree(entries);
  224. op->value = result;
  225. dev_data = pci_get_drvdata(dev);
  226. if (dev_data)
  227. dev_data->ack_intr = 0;
  228. return result > 0 ? 0 : result;
  229. }
  230. static
  231. int xen_pcibk_disable_msix(struct xen_pcibk_device *pdev,
  232. struct pci_dev *dev, struct xen_pci_op *op)
  233. {
  234. if (unlikely(verbose_request))
  235. printk(KERN_DEBUG DRV_NAME ": %s: disable MSI-X\n",
  236. pci_name(dev));
  237. if (dev->msix_enabled) {
  238. struct xen_pcibk_dev_data *dev_data;
  239. pci_disable_msix(dev);
  240. dev_data = pci_get_drvdata(dev);
  241. if (dev_data)
  242. dev_data->ack_intr = 1;
  243. }
  244. /*
  245. * SR-IOV devices (which don't have any legacy IRQ) have
  246. * an undefined IRQ value of zero.
  247. */
  248. op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
  249. if (unlikely(verbose_request))
  250. printk(KERN_DEBUG DRV_NAME ": %s: MSI-X: %d\n",
  251. pci_name(dev), op->value);
  252. return 0;
  253. }
  254. #endif
  255. /*
  256. * Now the same evtchn is used for both pcifront conf_read_write request
  257. * as well as pcie aer front end ack. We use a new work_queue to schedule
  258. * xen_pcibk conf_read_write service for avoiding confict with aer_core
  259. * do_recovery job which also use the system default work_queue
  260. */
  261. void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
  262. {
  263. /* Check that frontend is requesting an operation and that we are not
  264. * already processing a request */
  265. if (test_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags)
  266. && !test_and_set_bit(_PDEVF_op_active, &pdev->flags)) {
  267. schedule_work(&pdev->op_work);
  268. }
  269. /*_XEN_PCIB_active should have been cleared by pcifront. And also make
  270. sure xen_pcibk is waiting for ack by checking _PCIB_op_pending*/
  271. if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
  272. && test_bit(_PCIB_op_pending, &pdev->flags)) {
  273. wake_up(&xen_pcibk_aer_wait_queue);
  274. }
  275. }
  276. /* Performing the configuration space reads/writes must not be done in atomic
  277. * context because some of the pci_* functions can sleep (mostly due to ACPI
  278. * use of semaphores). This function is intended to be called from a work
  279. * queue in process context taking a struct xen_pcibk_device as a parameter */
  280. void xen_pcibk_do_op(struct work_struct *data)
  281. {
  282. struct xen_pcibk_device *pdev =
  283. container_of(data, struct xen_pcibk_device, op_work);
  284. struct pci_dev *dev;
  285. struct xen_pcibk_dev_data *dev_data = NULL;
  286. struct xen_pci_op *op = &pdev->op;
  287. int test_intx = 0;
  288. #ifdef CONFIG_PCI_MSI
  289. unsigned int nr = 0;
  290. #endif
  291. *op = pdev->sh_info->op;
  292. barrier();
  293. dev = xen_pcibk_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
  294. if (dev == NULL)
  295. op->err = XEN_PCI_ERR_dev_not_found;
  296. else {
  297. dev_data = pci_get_drvdata(dev);
  298. if (dev_data)
  299. test_intx = dev_data->enable_intx;
  300. switch (op->cmd) {
  301. case XEN_PCI_OP_conf_read:
  302. op->err = xen_pcibk_config_read(dev,
  303. op->offset, op->size, &op->value);
  304. break;
  305. case XEN_PCI_OP_conf_write:
  306. op->err = xen_pcibk_config_write(dev,
  307. op->offset, op->size, op->value);
  308. break;
  309. #ifdef CONFIG_PCI_MSI
  310. case XEN_PCI_OP_enable_msi:
  311. op->err = xen_pcibk_enable_msi(pdev, dev, op);
  312. break;
  313. case XEN_PCI_OP_disable_msi:
  314. op->err = xen_pcibk_disable_msi(pdev, dev, op);
  315. break;
  316. case XEN_PCI_OP_enable_msix:
  317. nr = op->value;
  318. op->err = xen_pcibk_enable_msix(pdev, dev, op);
  319. break;
  320. case XEN_PCI_OP_disable_msix:
  321. op->err = xen_pcibk_disable_msix(pdev, dev, op);
  322. break;
  323. #endif
  324. default:
  325. op->err = XEN_PCI_ERR_not_implemented;
  326. break;
  327. }
  328. }
  329. if (!op->err && dev && dev_data) {
  330. /* Transition detected */
  331. if ((dev_data->enable_intx != test_intx))
  332. xen_pcibk_control_isr(dev, 0 /* no reset */);
  333. }
  334. pdev->sh_info->op.err = op->err;
  335. pdev->sh_info->op.value = op->value;
  336. #ifdef CONFIG_PCI_MSI
  337. if (op->cmd == XEN_PCI_OP_enable_msix && op->err == 0) {
  338. unsigned int i;
  339. for (i = 0; i < nr; i++)
  340. pdev->sh_info->op.msix_entries[i].vector =
  341. op->msix_entries[i].vector;
  342. }
  343. #endif
  344. /* Tell the driver domain that we're done. */
  345. wmb();
  346. clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
  347. notify_remote_via_irq(pdev->evtchn_irq);
  348. /* Mark that we're done. */
  349. smp_mb__before_atomic(); /* /after/ clearing PCIF_active */
  350. clear_bit(_PDEVF_op_active, &pdev->flags);
  351. smp_mb__after_atomic(); /* /before/ final check for work */
  352. /* Check to see if the driver domain tried to start another request in
  353. * between clearing _XEN_PCIF_active and clearing _PDEVF_op_active.
  354. */
  355. xen_pcibk_test_and_schedule_op(pdev);
  356. }
  357. irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id)
  358. {
  359. struct xen_pcibk_device *pdev = dev_id;
  360. xen_pcibk_test_and_schedule_op(pdev);
  361. return IRQ_HANDLED;
  362. }
  363. static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id)
  364. {
  365. struct pci_dev *dev = (struct pci_dev *)dev_id;
  366. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  367. if (dev_data->isr_on && dev_data->ack_intr) {
  368. dev_data->handled++;
  369. if ((dev_data->handled % 1000) == 0) {
  370. if (xen_test_irq_shared(irq)) {
  371. pr_info("%s IRQ line is not shared "
  372. "with other domains. Turning ISR off\n",
  373. dev_data->irq_name);
  374. dev_data->ack_intr = 0;
  375. }
  376. }
  377. return IRQ_HANDLED;
  378. }
  379. return IRQ_NONE;
  380. }