pciback_ops.c 3.9 KB

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