Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 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 | |
| 13 | int verbose_request; |
| 14 | module_param(verbose_request, int, 0644); |
| 15 | |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame^] | 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 pciback_reset_device |
| 18 | * which does the actual PCI device enable/disable. |
| 19 | */ |
| 20 | void pciback_control_isr(struct pci_dev *dev, int reset) |
| 21 | { |
| 22 | struct pciback_dev_data *dev_data; |
| 23 | int rc; |
| 24 | int enable = 0; |
| 25 | |
| 26 | dev_data = pci_get_drvdata(dev); |
| 27 | if (!dev_data) |
| 28 | return; |
| 29 | |
| 30 | /* We don't deal with bridges */ |
| 31 | if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) |
| 32 | return; |
| 33 | |
| 34 | if (reset) { |
| 35 | dev_data->enable_intx = 0; |
| 36 | dev_data->ack_intr = 0; |
| 37 | } |
| 38 | enable = dev_data->enable_intx; |
| 39 | |
| 40 | /* Asked to disable, but ISR isn't runnig */ |
| 41 | if (!enable && !dev_data->isr_on) |
| 42 | return; |
| 43 | |
| 44 | /* Squirrel away the IRQs in the dev_data. We need this |
| 45 | * b/c when device transitions to MSI, the dev->irq is |
| 46 | * overwritten with the MSI vector. |
| 47 | */ |
| 48 | if (enable) |
| 49 | dev_data->irq = dev->irq; |
| 50 | |
| 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 | |
| 60 | if (enable) { |
| 61 | rc = request_irq(dev_data->irq, |
| 62 | pciback_guest_interrupt, IRQF_SHARED, |
| 63 | dev_data->irq_name, dev); |
| 64 | if (rc) { |
| 65 | dev_err(&dev->dev, "%s: failed to install fake IRQ " \ |
| 66 | "handler for IRQ %d! (rc:%d)\n", |
| 67 | dev_data->irq_name, dev_data->irq, rc); |
| 68 | goto out; |
| 69 | } |
| 70 | } else { |
| 71 | free_irq(dev_data->irq, dev); |
| 72 | dev_data->irq = 0; |
| 73 | } |
| 74 | dev_data->isr_on = enable; |
| 75 | dev_data->ack_intr = enable; |
| 76 | out: |
| 77 | dev_dbg(&dev->dev, "%s: #%d %s %s%s %s\n", |
| 78 | dev_data->irq_name, |
| 79 | dev_data->irq, |
| 80 | pci_is_enabled(dev) ? "on" : "off", |
| 81 | dev->msi_enabled ? "MSI" : "", |
| 82 | dev->msix_enabled ? "MSI/X" : "", |
| 83 | enable ? (dev_data->isr_on ? "enabled" : "failed to enable") : |
| 84 | (dev_data->isr_on ? "failed to disable" : "disabled")); |
| 85 | } |
| 86 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 87 | /* Ensure a device is "turned off" and ready to be exported. |
| 88 | * (Also see pciback_config_reset to ensure virtual configuration space is |
| 89 | * ready to be re-exported) |
| 90 | */ |
| 91 | void pciback_reset_device(struct pci_dev *dev) |
| 92 | { |
| 93 | u16 cmd; |
| 94 | |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame^] | 95 | pciback_control_isr(dev, 1 /* reset device */); |
| 96 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 97 | /* Disable devices (but not bridges) */ |
| 98 | if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) { |
Konrad Rzeszutek Wilk | a2be65fd | 2010-03-03 13:38:43 -0500 | [diff] [blame] | 99 | #ifdef CONFIG_PCI_MSI |
| 100 | /* The guest could have been abruptly killed without |
| 101 | * disabling MSI/MSI-X interrupts.*/ |
| 102 | if (dev->msix_enabled) |
| 103 | pci_disable_msix(dev); |
| 104 | if (dev->msi_enabled) |
| 105 | pci_disable_msi(dev); |
| 106 | #endif |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 107 | pci_disable_device(dev); |
| 108 | |
| 109 | pci_write_config_word(dev, PCI_COMMAND, 0); |
| 110 | |
| 111 | dev->is_busmaster = 0; |
| 112 | } else { |
| 113 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 114 | if (cmd & (PCI_COMMAND_INVALIDATE)) { |
| 115 | cmd &= ~(PCI_COMMAND_INVALIDATE); |
| 116 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 117 | |
| 118 | dev->is_busmaster = 0; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | /* |
| 123 | * Now the same evtchn is used for both pcifront conf_read_write request |
| 124 | * as well as pcie aer front end ack. We use a new work_queue to schedule |
| 125 | * pciback conf_read_write service for avoiding confict with aer_core |
| 126 | * do_recovery job which also use the system default work_queue |
| 127 | */ |
| 128 | void test_and_schedule_op(struct pciback_device *pdev) |
| 129 | { |
| 130 | /* Check that frontend is requesting an operation and that we are not |
| 131 | * already processing a request */ |
| 132 | if (test_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags) |
| 133 | && !test_and_set_bit(_PDEVF_op_active, &pdev->flags)) { |
| 134 | queue_work(pciback_wq, &pdev->op_work); |
| 135 | } |
| 136 | /*_XEN_PCIB_active should have been cleared by pcifront. And also make |
| 137 | sure pciback is waiting for ack by checking _PCIB_op_pending*/ |
| 138 | if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags) |
| 139 | && test_bit(_PCIB_op_pending, &pdev->flags)) { |
| 140 | wake_up(&aer_wait_queue); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* Performing the configuration space reads/writes must not be done in atomic |
| 145 | * context because some of the pci_* functions can sleep (mostly due to ACPI |
| 146 | * use of semaphores). This function is intended to be called from a work |
| 147 | * queue in process context taking a struct pciback_device as a parameter */ |
| 148 | |
| 149 | void pciback_do_op(struct work_struct *data) |
| 150 | { |
| 151 | struct pciback_device *pdev = |
| 152 | container_of(data, struct pciback_device, op_work); |
| 153 | struct pci_dev *dev; |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame^] | 154 | struct pciback_dev_data *dev_data = NULL; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 155 | struct xen_pci_op *op = &pdev->sh_info->op; |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame^] | 156 | int test_intx = 0; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 157 | |
| 158 | dev = pciback_get_pci_dev(pdev, op->domain, op->bus, op->devfn); |
| 159 | |
| 160 | if (dev == NULL) |
| 161 | op->err = XEN_PCI_ERR_dev_not_found; |
| 162 | else { |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame^] | 163 | dev_data = pci_get_drvdata(dev); |
| 164 | if (dev_data) |
| 165 | test_intx = dev_data->enable_intx; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 166 | switch (op->cmd) { |
| 167 | case XEN_PCI_OP_conf_read: |
| 168 | op->err = pciback_config_read(dev, |
| 169 | op->offset, op->size, &op->value); |
| 170 | break; |
| 171 | case XEN_PCI_OP_conf_write: |
| 172 | op->err = pciback_config_write(dev, |
| 173 | op->offset, op->size, op->value); |
| 174 | break; |
| 175 | #ifdef CONFIG_PCI_MSI |
| 176 | case XEN_PCI_OP_enable_msi: |
| 177 | op->err = pciback_enable_msi(pdev, dev, op); |
| 178 | break; |
| 179 | case XEN_PCI_OP_disable_msi: |
| 180 | op->err = pciback_disable_msi(pdev, dev, op); |
| 181 | break; |
| 182 | case XEN_PCI_OP_enable_msix: |
| 183 | op->err = pciback_enable_msix(pdev, dev, op); |
| 184 | break; |
| 185 | case XEN_PCI_OP_disable_msix: |
| 186 | op->err = pciback_disable_msix(pdev, dev, op); |
| 187 | break; |
| 188 | #endif |
| 189 | default: |
| 190 | op->err = XEN_PCI_ERR_not_implemented; |
| 191 | break; |
| 192 | } |
| 193 | } |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame^] | 194 | if (!op->err && dev && dev_data) { |
| 195 | /* Transition detected */ |
| 196 | if ((dev_data->enable_intx != test_intx)) |
| 197 | pciback_control_isr(dev, 0 /* no reset */); |
| 198 | } |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 199 | /* Tell the driver domain that we're done. */ |
| 200 | wmb(); |
| 201 | clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags); |
| 202 | notify_remote_via_irq(pdev->evtchn_irq); |
| 203 | |
| 204 | /* Mark that we're done. */ |
| 205 | smp_mb__before_clear_bit(); /* /after/ clearing PCIF_active */ |
| 206 | clear_bit(_PDEVF_op_active, &pdev->flags); |
| 207 | smp_mb__after_clear_bit(); /* /before/ final check for work */ |
| 208 | |
| 209 | /* Check to see if the driver domain tried to start another request in |
| 210 | * between clearing _XEN_PCIF_active and clearing _PDEVF_op_active. |
| 211 | */ |
| 212 | test_and_schedule_op(pdev); |
| 213 | } |
| 214 | |
| 215 | irqreturn_t pciback_handle_event(int irq, void *dev_id) |
| 216 | { |
| 217 | struct pciback_device *pdev = dev_id; |
| 218 | |
| 219 | test_and_schedule_op(pdev); |
| 220 | |
| 221 | return IRQ_HANDLED; |
| 222 | } |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame^] | 223 | irqreturn_t pciback_guest_interrupt(int irq, void *dev_id) |
| 224 | { |
| 225 | struct pci_dev *dev = (struct pci_dev *)dev_id; |
| 226 | struct pciback_dev_data *dev_data = pci_get_drvdata(dev); |
| 227 | |
| 228 | if (dev_data->isr_on && dev_data->ack_intr) { |
| 229 | dev_data->handled++; |
| 230 | if ((dev_data->handled % 1000) == 0) { |
| 231 | if (xen_test_irq_shared(irq)) { |
| 232 | printk(KERN_INFO "%s IRQ line is not shared " |
| 233 | "with other domains. Turning ISR off\n", |
| 234 | dev_data->irq_name); |
| 235 | dev_data->ack_intr = 0; |
| 236 | } |
| 237 | } |
| 238 | return IRQ_HANDLED; |
| 239 | } |
| 240 | return IRQ_NONE; |
| 241 | } |