pciback_ops.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /* Ensure a device is "turned off" and ready to be exported.
  15. * (Also see pciback_config_reset to ensure virtual configuration space is
  16. * ready to be re-exported)
  17. */
  18. void pciback_reset_device(struct pci_dev *dev)
  19. {
  20. u16 cmd;
  21. /* Disable devices (but not bridges) */
  22. if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
  23. #ifdef CONFIG_PCI_MSI
  24. /* The guest could have been abruptly killed without
  25. * disabling MSI/MSI-X interrupts.*/
  26. if (dev->msix_enabled)
  27. pci_disable_msix(dev);
  28. if (dev->msi_enabled)
  29. pci_disable_msi(dev);
  30. #endif
  31. pci_disable_device(dev);
  32. pci_write_config_word(dev, PCI_COMMAND, 0);
  33. dev->is_busmaster = 0;
  34. } else {
  35. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  36. if (cmd & (PCI_COMMAND_INVALIDATE)) {
  37. cmd &= ~(PCI_COMMAND_INVALIDATE);
  38. pci_write_config_word(dev, PCI_COMMAND, cmd);
  39. dev->is_busmaster = 0;
  40. }
  41. }
  42. }
  43. /*
  44. * Now the same evtchn is used for both pcifront conf_read_write request
  45. * as well as pcie aer front end ack. We use a new work_queue to schedule
  46. * pciback conf_read_write service for avoiding confict with aer_core
  47. * do_recovery job which also use the system default work_queue
  48. */
  49. void test_and_schedule_op(struct pciback_device *pdev)
  50. {
  51. /* Check that frontend is requesting an operation and that we are not
  52. * already processing a request */
  53. if (test_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags)
  54. && !test_and_set_bit(_PDEVF_op_active, &pdev->flags)) {
  55. queue_work(pciback_wq, &pdev->op_work);
  56. }
  57. /*_XEN_PCIB_active should have been cleared by pcifront. And also make
  58. sure pciback is waiting for ack by checking _PCIB_op_pending*/
  59. if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
  60. && test_bit(_PCIB_op_pending, &pdev->flags)) {
  61. wake_up(&aer_wait_queue);
  62. }
  63. }
  64. /* Performing the configuration space reads/writes must not be done in atomic
  65. * context because some of the pci_* functions can sleep (mostly due to ACPI
  66. * use of semaphores). This function is intended to be called from a work
  67. * queue in process context taking a struct pciback_device as a parameter */
  68. void pciback_do_op(struct work_struct *data)
  69. {
  70. struct pciback_device *pdev =
  71. container_of(data, struct pciback_device, op_work);
  72. struct pci_dev *dev;
  73. struct xen_pci_op *op = &pdev->sh_info->op;
  74. dev = pciback_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
  75. if (dev == NULL)
  76. op->err = XEN_PCI_ERR_dev_not_found;
  77. else {
  78. switch (op->cmd) {
  79. case XEN_PCI_OP_conf_read:
  80. op->err = pciback_config_read(dev,
  81. op->offset, op->size, &op->value);
  82. break;
  83. case XEN_PCI_OP_conf_write:
  84. op->err = pciback_config_write(dev,
  85. op->offset, op->size, op->value);
  86. break;
  87. #ifdef CONFIG_PCI_MSI
  88. case XEN_PCI_OP_enable_msi:
  89. op->err = pciback_enable_msi(pdev, dev, op);
  90. break;
  91. case XEN_PCI_OP_disable_msi:
  92. op->err = pciback_disable_msi(pdev, dev, op);
  93. break;
  94. case XEN_PCI_OP_enable_msix:
  95. op->err = pciback_enable_msix(pdev, dev, op);
  96. break;
  97. case XEN_PCI_OP_disable_msix:
  98. op->err = pciback_disable_msix(pdev, dev, op);
  99. break;
  100. #endif
  101. default:
  102. op->err = XEN_PCI_ERR_not_implemented;
  103. break;
  104. }
  105. }
  106. /* Tell the driver domain that we're done. */
  107. wmb();
  108. clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
  109. notify_remote_via_irq(pdev->evtchn_irq);
  110. /* Mark that we're done. */
  111. smp_mb__before_clear_bit(); /* /after/ clearing PCIF_active */
  112. clear_bit(_PDEVF_op_active, &pdev->flags);
  113. smp_mb__after_clear_bit(); /* /before/ final check for work */
  114. /* Check to see if the driver domain tried to start another request in
  115. * between clearing _XEN_PCIF_active and clearing _PDEVF_op_active.
  116. */
  117. test_and_schedule_op(pdev);
  118. }
  119. irqreturn_t pciback_handle_event(int irq, void *dev_id)
  120. {
  121. struct pciback_device *pdev = dev_id;
  122. test_and_schedule_op(pdev);
  123. return IRQ_HANDLED;
  124. }