0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 #define dev_fmt pr_fmt
0010
0011 #include <linux/moduleparam.h>
0012 #include <linux/wait.h>
0013 #include <linux/bitops.h>
0014 #include <xen/events.h>
0015 #include <linux/sched.h>
0016 #include "pciback.h"
0017
0018 static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id);
0019
0020
0021
0022
0023
0024 static void xen_pcibk_control_isr(struct pci_dev *dev, int reset)
0025 {
0026 struct xen_pcibk_dev_data *dev_data;
0027 int rc;
0028 int enable = 0;
0029
0030 dev_data = pci_get_drvdata(dev);
0031 if (!dev_data)
0032 return;
0033
0034
0035 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
0036 return;
0037
0038 if (reset) {
0039 dev_data->enable_intx = 0;
0040 dev_data->ack_intr = 0;
0041 }
0042 enable = dev_data->enable_intx;
0043
0044
0045 if (!enable && !dev_data->isr_on)
0046 return;
0047
0048
0049
0050
0051
0052 if (enable)
0053 dev_data->irq = dev->irq;
0054
0055
0056
0057
0058
0059 if (dev_data->irq == 0)
0060 goto out;
0061
0062 dev_dbg(&dev->dev, "%s: #%d %s %s%s %s-> %s\n",
0063 dev_data->irq_name,
0064 dev_data->irq,
0065 pci_is_enabled(dev) ? "on" : "off",
0066 dev->msi_enabled ? "MSI" : "",
0067 dev->msix_enabled ? "MSI/X" : "",
0068 dev_data->isr_on ? "enable" : "disable",
0069 enable ? "enable" : "disable");
0070
0071 if (enable) {
0072
0073
0074
0075
0076 if (dev->msi_enabled || dev->msix_enabled)
0077 goto out;
0078
0079 rc = request_irq(dev_data->irq,
0080 xen_pcibk_guest_interrupt, IRQF_SHARED,
0081 dev_data->irq_name, dev);
0082 if (rc) {
0083 dev_err(&dev->dev, "%s: failed to install fake IRQ " \
0084 "handler for IRQ %d! (rc:%d)\n",
0085 dev_data->irq_name, dev_data->irq, rc);
0086 goto out;
0087 }
0088 } else {
0089 free_irq(dev_data->irq, dev);
0090 dev_data->irq = 0;
0091 }
0092 dev_data->isr_on = enable;
0093 dev_data->ack_intr = enable;
0094 out:
0095 dev_dbg(&dev->dev, "%s: #%d %s %s%s %s\n",
0096 dev_data->irq_name,
0097 dev_data->irq,
0098 pci_is_enabled(dev) ? "on" : "off",
0099 dev->msi_enabled ? "MSI" : "",
0100 dev->msix_enabled ? "MSI/X" : "",
0101 enable ? (dev_data->isr_on ? "enabled" : "failed to enable") :
0102 (dev_data->isr_on ? "failed to disable" : "disabled"));
0103 }
0104
0105
0106
0107
0108
0109 void xen_pcibk_reset_device(struct pci_dev *dev)
0110 {
0111 u16 cmd;
0112
0113 xen_pcibk_control_isr(dev, 1 );
0114
0115
0116 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
0117 #ifdef CONFIG_PCI_MSI
0118
0119
0120 if (dev->msix_enabled)
0121 pci_disable_msix(dev);
0122 if (dev->msi_enabled)
0123 pci_disable_msi(dev);
0124 #endif
0125 if (pci_is_enabled(dev))
0126 pci_disable_device(dev);
0127
0128 dev->is_busmaster = 0;
0129 } else {
0130 pci_read_config_word(dev, PCI_COMMAND, &cmd);
0131 if (cmd & (PCI_COMMAND_INVALIDATE)) {
0132 cmd &= ~(PCI_COMMAND_INVALIDATE);
0133 pci_write_config_word(dev, PCI_COMMAND, cmd);
0134
0135 dev->is_busmaster = 0;
0136 }
0137 }
0138 }
0139
0140 #ifdef CONFIG_PCI_MSI
0141 static
0142 int xen_pcibk_enable_msi(struct xen_pcibk_device *pdev,
0143 struct pci_dev *dev, struct xen_pci_op *op)
0144 {
0145 struct xen_pcibk_dev_data *dev_data;
0146 int status;
0147
0148 if (dev->msi_enabled)
0149 status = -EALREADY;
0150 else if (dev->msix_enabled)
0151 status = -ENXIO;
0152 else
0153 status = pci_enable_msi(dev);
0154
0155 if (status) {
0156 dev_warn_ratelimited(&dev->dev, "error enabling MSI for guest %u: err %d\n",
0157 pdev->xdev->otherend_id, status);
0158 op->value = 0;
0159 return XEN_PCI_ERR_op_failed;
0160 }
0161
0162
0163
0164
0165 op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
0166
0167 dev_dbg(&dev->dev, "MSI: %d\n", op->value);
0168
0169 dev_data = pci_get_drvdata(dev);
0170 if (dev_data)
0171 dev_data->ack_intr = 0;
0172
0173 return 0;
0174 }
0175
0176 static
0177 int xen_pcibk_disable_msi(struct xen_pcibk_device *pdev,
0178 struct pci_dev *dev, struct xen_pci_op *op)
0179 {
0180 if (dev->msi_enabled) {
0181 struct xen_pcibk_dev_data *dev_data;
0182
0183 pci_disable_msi(dev);
0184
0185 dev_data = pci_get_drvdata(dev);
0186 if (dev_data)
0187 dev_data->ack_intr = 1;
0188 }
0189 op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
0190
0191 dev_dbg(&dev->dev, "MSI: %d\n", op->value);
0192
0193 return 0;
0194 }
0195
0196 static
0197 int xen_pcibk_enable_msix(struct xen_pcibk_device *pdev,
0198 struct pci_dev *dev, struct xen_pci_op *op)
0199 {
0200 struct xen_pcibk_dev_data *dev_data;
0201 int i, result;
0202 struct msix_entry *entries;
0203 u16 cmd;
0204
0205 dev_dbg(&dev->dev, "enable MSI-X\n");
0206
0207 if (op->value > SH_INFO_MAX_VEC)
0208 return -EINVAL;
0209
0210 if (dev->msix_enabled)
0211 return -EALREADY;
0212
0213
0214
0215
0216
0217
0218 pci_read_config_word(pci_physfn(dev), PCI_COMMAND, &cmd);
0219 if (dev->msi_enabled || !(cmd & PCI_COMMAND_MEMORY))
0220 return -ENXIO;
0221
0222 entries = kmalloc_array(op->value, sizeof(*entries), GFP_KERNEL);
0223 if (entries == NULL)
0224 return -ENOMEM;
0225
0226 for (i = 0; i < op->value; i++) {
0227 entries[i].entry = op->msix_entries[i].entry;
0228 entries[i].vector = op->msix_entries[i].vector;
0229 }
0230
0231 result = pci_enable_msix_exact(dev, entries, op->value);
0232 if (result == 0) {
0233 for (i = 0; i < op->value; i++) {
0234 op->msix_entries[i].entry = entries[i].entry;
0235 if (entries[i].vector) {
0236 op->msix_entries[i].vector =
0237 xen_pirq_from_irq(entries[i].vector);
0238 dev_dbg(&dev->dev, "MSI-X[%d]: %d\n", i,
0239 op->msix_entries[i].vector);
0240 }
0241 }
0242 } else
0243 dev_warn_ratelimited(&dev->dev, "error enabling MSI-X for guest %u: err %d!\n",
0244 pdev->xdev->otherend_id, result);
0245 kfree(entries);
0246
0247 op->value = result;
0248 dev_data = pci_get_drvdata(dev);
0249 if (dev_data)
0250 dev_data->ack_intr = 0;
0251
0252 return result > 0 ? 0 : result;
0253 }
0254
0255 static
0256 int xen_pcibk_disable_msix(struct xen_pcibk_device *pdev,
0257 struct pci_dev *dev, struct xen_pci_op *op)
0258 {
0259 if (dev->msix_enabled) {
0260 struct xen_pcibk_dev_data *dev_data;
0261
0262 pci_disable_msix(dev);
0263
0264 dev_data = pci_get_drvdata(dev);
0265 if (dev_data)
0266 dev_data->ack_intr = 1;
0267 }
0268
0269
0270
0271
0272 op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
0273
0274 dev_dbg(&dev->dev, "MSI-X: %d\n", op->value);
0275
0276 return 0;
0277 }
0278 #endif
0279
0280 static inline bool xen_pcibk_test_op_pending(struct xen_pcibk_device *pdev)
0281 {
0282 return test_bit(_XEN_PCIF_active,
0283 (unsigned long *)&pdev->sh_info->flags) &&
0284 !test_and_set_bit(_PDEVF_op_active, &pdev->flags);
0285 }
0286
0287
0288
0289
0290
0291
0292
0293 static void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
0294 {
0295 bool eoi = true;
0296
0297
0298
0299 if (xen_pcibk_test_op_pending(pdev)) {
0300 schedule_work(&pdev->op_work);
0301 eoi = false;
0302 }
0303
0304
0305 if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
0306 && test_bit(_PCIB_op_pending, &pdev->flags)) {
0307 wake_up(&xen_pcibk_aer_wait_queue);
0308 eoi = false;
0309 }
0310
0311
0312 if (eoi)
0313 xen_pcibk_lateeoi(pdev, XEN_EOI_FLAG_SPURIOUS);
0314 }
0315
0316
0317
0318
0319
0320
0321 static void xen_pcibk_do_one_op(struct xen_pcibk_device *pdev)
0322 {
0323 struct pci_dev *dev;
0324 struct xen_pcibk_dev_data *dev_data = NULL;
0325 struct xen_pci_op *op = &pdev->op;
0326 int test_intx = 0;
0327 #ifdef CONFIG_PCI_MSI
0328 unsigned int nr = 0;
0329 #endif
0330
0331 *op = pdev->sh_info->op;
0332 barrier();
0333 dev = xen_pcibk_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
0334
0335 if (dev == NULL)
0336 op->err = XEN_PCI_ERR_dev_not_found;
0337 else {
0338 dev_data = pci_get_drvdata(dev);
0339 if (dev_data)
0340 test_intx = dev_data->enable_intx;
0341 switch (op->cmd) {
0342 case XEN_PCI_OP_conf_read:
0343 op->err = xen_pcibk_config_read(dev,
0344 op->offset, op->size, &op->value);
0345 break;
0346 case XEN_PCI_OP_conf_write:
0347 op->err = xen_pcibk_config_write(dev,
0348 op->offset, op->size, op->value);
0349 break;
0350 #ifdef CONFIG_PCI_MSI
0351 case XEN_PCI_OP_enable_msi:
0352 op->err = xen_pcibk_enable_msi(pdev, dev, op);
0353 break;
0354 case XEN_PCI_OP_disable_msi:
0355 op->err = xen_pcibk_disable_msi(pdev, dev, op);
0356 break;
0357 case XEN_PCI_OP_enable_msix:
0358 nr = op->value;
0359 op->err = xen_pcibk_enable_msix(pdev, dev, op);
0360 break;
0361 case XEN_PCI_OP_disable_msix:
0362 op->err = xen_pcibk_disable_msix(pdev, dev, op);
0363 break;
0364 #endif
0365 default:
0366 op->err = XEN_PCI_ERR_not_implemented;
0367 break;
0368 }
0369 }
0370 if (!op->err && dev && dev_data) {
0371
0372 if ((dev_data->enable_intx != test_intx))
0373 xen_pcibk_control_isr(dev, 0 );
0374 }
0375 pdev->sh_info->op.err = op->err;
0376 pdev->sh_info->op.value = op->value;
0377 #ifdef CONFIG_PCI_MSI
0378 if (op->cmd == XEN_PCI_OP_enable_msix && op->err == 0) {
0379 unsigned int i;
0380
0381 for (i = 0; i < nr; i++)
0382 pdev->sh_info->op.msix_entries[i].vector =
0383 op->msix_entries[i].vector;
0384 }
0385 #endif
0386
0387 wmb();
0388 clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
0389 notify_remote_via_irq(pdev->evtchn_irq);
0390
0391
0392 smp_mb__before_atomic();
0393 clear_bit(_PDEVF_op_active, &pdev->flags);
0394 smp_mb__after_atomic();
0395 }
0396
0397 void xen_pcibk_do_op(struct work_struct *data)
0398 {
0399 struct xen_pcibk_device *pdev =
0400 container_of(data, struct xen_pcibk_device, op_work);
0401
0402 do {
0403 xen_pcibk_do_one_op(pdev);
0404 } while (xen_pcibk_test_op_pending(pdev));
0405
0406 xen_pcibk_lateeoi(pdev, 0);
0407 }
0408
0409 irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id)
0410 {
0411 struct xen_pcibk_device *pdev = dev_id;
0412 bool eoi;
0413
0414
0415 if (unlikely(pdev->evtchn_irq != irq))
0416 pdev->evtchn_irq = irq;
0417
0418 eoi = test_and_set_bit(_EOI_pending, &pdev->flags);
0419 WARN(eoi, "IRQ while EOI pending\n");
0420
0421 xen_pcibk_test_and_schedule_op(pdev);
0422
0423 return IRQ_HANDLED;
0424 }
0425 static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id)
0426 {
0427 struct pci_dev *dev = (struct pci_dev *)dev_id;
0428 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
0429
0430 if (dev_data->isr_on && dev_data->ack_intr) {
0431 dev_data->handled++;
0432 if ((dev_data->handled % 1000) == 0) {
0433 if (xen_test_irq_shared(irq)) {
0434 dev_info(&dev->dev, "%s IRQ line is not shared "
0435 "with other domains. Turning ISR off\n",
0436 dev_data->irq_name);
0437 dev_data->ack_intr = 0;
0438 }
0439 }
0440 return IRQ_HANDLED;
0441 }
0442 return IRQ_NONE;
0443 }