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/module.h>
0012 #include <linux/init.h>
0013 #include <linux/rwsem.h>
0014 #include <linux/list.h>
0015 #include <linux/spinlock.h>
0016 #include <linux/kref.h>
0017 #include <linux/pci.h>
0018 #include <linux/wait.h>
0019 #include <linux/sched.h>
0020 #include <linux/atomic.h>
0021 #include <xen/events.h>
0022 #include <xen/pci.h>
0023 #include <xen/xen.h>
0024 #include <asm/xen/hypervisor.h>
0025 #include <xen/interface/physdev.h>
0026 #include "pciback.h"
0027 #include "conf_space.h"
0028 #include "conf_space_quirks.h"
0029
0030 #define PCISTUB_DRIVER_NAME "pciback"
0031
0032 static char *pci_devs_to_hide;
0033 wait_queue_head_t xen_pcibk_aer_wait_queue;
0034
0035
0036
0037 static DECLARE_RWSEM(pcistub_sem);
0038 module_param_named(hide, pci_devs_to_hide, charp, 0444);
0039
0040 struct pcistub_device_id {
0041 struct list_head slot_list;
0042 int domain;
0043 unsigned char bus;
0044 unsigned int devfn;
0045 };
0046 static LIST_HEAD(pcistub_device_ids);
0047 static DEFINE_SPINLOCK(device_ids_lock);
0048
0049 struct pcistub_device {
0050 struct kref kref;
0051 struct list_head dev_list;
0052 spinlock_t lock;
0053
0054 struct pci_dev *dev;
0055 struct xen_pcibk_device *pdev;
0056 };
0057
0058
0059
0060
0061 static DEFINE_SPINLOCK(pcistub_devices_lock);
0062 static LIST_HEAD(pcistub_devices);
0063
0064
0065
0066
0067 static int initialize_devices;
0068 static LIST_HEAD(seized_devices);
0069
0070 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
0071 {
0072 struct pcistub_device *psdev;
0073
0074 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
0075
0076 psdev = kzalloc(sizeof(*psdev), GFP_KERNEL);
0077 if (!psdev)
0078 return NULL;
0079
0080 psdev->dev = pci_dev_get(dev);
0081 if (!psdev->dev) {
0082 kfree(psdev);
0083 return NULL;
0084 }
0085
0086 kref_init(&psdev->kref);
0087 spin_lock_init(&psdev->lock);
0088
0089 return psdev;
0090 }
0091
0092
0093 static void pcistub_device_release(struct kref *kref)
0094 {
0095 struct pcistub_device *psdev;
0096 struct pci_dev *dev;
0097 struct xen_pcibk_dev_data *dev_data;
0098
0099 psdev = container_of(kref, struct pcistub_device, kref);
0100 dev = psdev->dev;
0101 dev_data = pci_get_drvdata(dev);
0102
0103 dev_dbg(&dev->dev, "pcistub_device_release\n");
0104
0105 xen_unregister_device_domain_owner(dev);
0106
0107
0108
0109
0110 __pci_reset_function_locked(dev);
0111 if (dev_data &&
0112 pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
0113 dev_info(&dev->dev, "Could not reload PCI state\n");
0114 else
0115 pci_restore_state(dev);
0116
0117 if (dev->msix_cap) {
0118 struct physdev_pci_device ppdev = {
0119 .seg = pci_domain_nr(dev->bus),
0120 .bus = dev->bus->number,
0121 .devfn = dev->devfn
0122 };
0123 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
0124 &ppdev);
0125
0126 if (err && err != -ENOSYS)
0127 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
0128 err);
0129 }
0130
0131
0132 xen_pcibk_reset_device(dev);
0133
0134 kfree(dev_data);
0135 pci_set_drvdata(dev, NULL);
0136
0137
0138 xen_pcibk_config_free_dyn_fields(dev);
0139 xen_pcibk_config_free_dev(dev);
0140
0141 pci_clear_dev_assigned(dev);
0142 pci_dev_put(dev);
0143
0144 kfree(psdev);
0145 }
0146
0147 static inline void pcistub_device_get(struct pcistub_device *psdev)
0148 {
0149 kref_get(&psdev->kref);
0150 }
0151
0152 static inline void pcistub_device_put(struct pcistub_device *psdev)
0153 {
0154 kref_put(&psdev->kref, pcistub_device_release);
0155 }
0156
0157 static struct pcistub_device *pcistub_device_find_locked(int domain, int bus,
0158 int slot, int func)
0159 {
0160 struct pcistub_device *psdev;
0161
0162 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
0163 if (psdev->dev != NULL
0164 && domain == pci_domain_nr(psdev->dev->bus)
0165 && bus == psdev->dev->bus->number
0166 && slot == PCI_SLOT(psdev->dev->devfn)
0167 && func == PCI_FUNC(psdev->dev->devfn)) {
0168 return psdev;
0169 }
0170 }
0171
0172 return NULL;
0173 }
0174
0175 static struct pcistub_device *pcistub_device_find(int domain, int bus,
0176 int slot, int func)
0177 {
0178 struct pcistub_device *psdev;
0179 unsigned long flags;
0180
0181 spin_lock_irqsave(&pcistub_devices_lock, flags);
0182
0183 psdev = pcistub_device_find_locked(domain, bus, slot, func);
0184 if (psdev)
0185 pcistub_device_get(psdev);
0186
0187 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
0188 return psdev;
0189 }
0190
0191 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
0192 struct pcistub_device *psdev)
0193 {
0194 struct pci_dev *pci_dev = NULL;
0195 unsigned long flags;
0196
0197 pcistub_device_get(psdev);
0198
0199 spin_lock_irqsave(&psdev->lock, flags);
0200 if (!psdev->pdev) {
0201 psdev->pdev = pdev;
0202 pci_dev = psdev->dev;
0203 }
0204 spin_unlock_irqrestore(&psdev->lock, flags);
0205
0206 if (!pci_dev)
0207 pcistub_device_put(psdev);
0208
0209 return pci_dev;
0210 }
0211
0212 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
0213 int domain, int bus,
0214 int slot, int func)
0215 {
0216 struct pcistub_device *psdev;
0217 struct pci_dev *found_dev = NULL;
0218 unsigned long flags;
0219
0220 spin_lock_irqsave(&pcistub_devices_lock, flags);
0221
0222 psdev = pcistub_device_find_locked(domain, bus, slot, func);
0223 if (psdev)
0224 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
0225
0226 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
0227 return found_dev;
0228 }
0229
0230 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
0231 struct pci_dev *dev)
0232 {
0233 struct pcistub_device *psdev;
0234 struct pci_dev *found_dev = NULL;
0235 unsigned long flags;
0236
0237 spin_lock_irqsave(&pcistub_devices_lock, flags);
0238
0239 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
0240 if (psdev->dev == dev) {
0241 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
0242 break;
0243 }
0244 }
0245
0246 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
0247 return found_dev;
0248 }
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261 void pcistub_put_pci_dev(struct pci_dev *dev)
0262 {
0263 struct pcistub_device *psdev, *found_psdev = NULL;
0264 unsigned long flags;
0265 struct xen_pcibk_dev_data *dev_data;
0266 int ret;
0267
0268 spin_lock_irqsave(&pcistub_devices_lock, flags);
0269
0270 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
0271 if (psdev->dev == dev) {
0272 found_psdev = psdev;
0273 break;
0274 }
0275 }
0276
0277 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
0278 if (WARN_ON(!found_psdev))
0279 return;
0280
0281
0282
0283
0284 down_write(&pcistub_sem);
0285
0286
0287
0288 device_lock_assert(&dev->dev);
0289 __pci_reset_function_locked(dev);
0290
0291 dev_data = pci_get_drvdata(dev);
0292 ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
0293 if (!ret) {
0294
0295
0296
0297
0298
0299 pci_restore_state(dev);
0300 } else
0301 dev_info(&dev->dev, "Could not reload PCI state\n");
0302
0303 xen_pcibk_reset_device(dev);
0304
0305
0306 xen_pcibk_config_reset_dev(dev);
0307 xen_pcibk_config_free_dyn_fields(dev);
0308
0309 dev_data->allow_interrupt_control = 0;
0310
0311 xen_unregister_device_domain_owner(dev);
0312
0313 spin_lock_irqsave(&found_psdev->lock, flags);
0314 found_psdev->pdev = NULL;
0315 spin_unlock_irqrestore(&found_psdev->lock, flags);
0316
0317 pcistub_device_put(found_psdev);
0318 up_write(&pcistub_sem);
0319 }
0320
0321 static int pcistub_match_one(struct pci_dev *dev,
0322 struct pcistub_device_id *pdev_id)
0323 {
0324
0325
0326
0327 for (; dev != NULL; dev = dev->bus->self) {
0328 if (pci_domain_nr(dev->bus) == pdev_id->domain
0329 && dev->bus->number == pdev_id->bus
0330 && dev->devfn == pdev_id->devfn)
0331 return 1;
0332
0333
0334 if (dev == dev->bus->self)
0335 break;
0336 }
0337
0338 return 0;
0339 }
0340
0341 static int pcistub_match(struct pci_dev *dev)
0342 {
0343 struct pcistub_device_id *pdev_id;
0344 unsigned long flags;
0345 int found = 0;
0346
0347 spin_lock_irqsave(&device_ids_lock, flags);
0348 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
0349 if (pcistub_match_one(dev, pdev_id)) {
0350 found = 1;
0351 break;
0352 }
0353 }
0354 spin_unlock_irqrestore(&device_ids_lock, flags);
0355
0356 return found;
0357 }
0358
0359 static int pcistub_init_device(struct pci_dev *dev)
0360 {
0361 struct xen_pcibk_dev_data *dev_data;
0362 int err = 0;
0363
0364 dev_dbg(&dev->dev, "initializing...\n");
0365
0366
0367
0368
0369
0370
0371 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
0372 + strlen(pci_name(dev)) + 1, GFP_KERNEL);
0373 if (!dev_data) {
0374 err = -ENOMEM;
0375 goto out;
0376 }
0377 pci_set_drvdata(dev, dev_data);
0378
0379
0380
0381
0382
0383 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
0384
0385 dev_dbg(&dev->dev, "initializing config\n");
0386
0387 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
0388 err = xen_pcibk_config_init_dev(dev);
0389 if (err)
0390 goto out;
0391
0392
0393
0394
0395
0396
0397
0398
0399 dev_dbg(&dev->dev, "enabling device\n");
0400 err = pci_enable_device(dev);
0401 if (err)
0402 goto config_release;
0403
0404 if (dev->msix_cap) {
0405 struct physdev_pci_device ppdev = {
0406 .seg = pci_domain_nr(dev->bus),
0407 .bus = dev->bus->number,
0408 .devfn = dev->devfn
0409 };
0410
0411 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
0412 if (err && err != -ENOSYS)
0413 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
0414 err);
0415 }
0416
0417
0418 dev_dbg(&dev->dev, "save state of device\n");
0419 pci_save_state(dev);
0420 dev_data->pci_saved_state = pci_store_saved_state(dev);
0421 if (!dev_data->pci_saved_state)
0422 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
0423 else {
0424 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
0425 __pci_reset_function_locked(dev);
0426 pci_restore_state(dev);
0427 }
0428
0429
0430
0431 dev_dbg(&dev->dev, "reset device\n");
0432 xen_pcibk_reset_device(dev);
0433
0434 pci_set_dev_assigned(dev);
0435 return 0;
0436
0437 config_release:
0438 xen_pcibk_config_free_dev(dev);
0439
0440 out:
0441 pci_set_drvdata(dev, NULL);
0442 kfree(dev_data);
0443 return err;
0444 }
0445
0446
0447
0448
0449
0450
0451
0452 static int __init pcistub_init_devices_late(void)
0453 {
0454 struct pcistub_device *psdev;
0455 unsigned long flags;
0456 int err = 0;
0457
0458 spin_lock_irqsave(&pcistub_devices_lock, flags);
0459
0460 while (!list_empty(&seized_devices)) {
0461 psdev = container_of(seized_devices.next,
0462 struct pcistub_device, dev_list);
0463 list_del(&psdev->dev_list);
0464
0465 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
0466
0467 err = pcistub_init_device(psdev->dev);
0468 if (err) {
0469 dev_err(&psdev->dev->dev,
0470 "error %d initializing device\n", err);
0471 kfree(psdev);
0472 psdev = NULL;
0473 }
0474
0475 spin_lock_irqsave(&pcistub_devices_lock, flags);
0476
0477 if (psdev)
0478 list_add_tail(&psdev->dev_list, &pcistub_devices);
0479 }
0480
0481 initialize_devices = 1;
0482
0483 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
0484
0485 return 0;
0486 }
0487
0488 static void pcistub_device_id_add_list(struct pcistub_device_id *new,
0489 int domain, int bus, unsigned int devfn)
0490 {
0491 struct pcistub_device_id *pci_dev_id;
0492 unsigned long flags;
0493 int found = 0;
0494
0495 spin_lock_irqsave(&device_ids_lock, flags);
0496
0497 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
0498 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
0499 pci_dev_id->devfn == devfn) {
0500 found = 1;
0501 break;
0502 }
0503 }
0504
0505 if (!found) {
0506 new->domain = domain;
0507 new->bus = bus;
0508 new->devfn = devfn;
0509 list_add_tail(&new->slot_list, &pcistub_device_ids);
0510 }
0511
0512 spin_unlock_irqrestore(&device_ids_lock, flags);
0513
0514 if (found)
0515 kfree(new);
0516 }
0517
0518 static int pcistub_seize(struct pci_dev *dev,
0519 struct pcistub_device_id *pci_dev_id)
0520 {
0521 struct pcistub_device *psdev;
0522 unsigned long flags;
0523 int err = 0;
0524
0525 psdev = pcistub_device_alloc(dev);
0526 if (!psdev) {
0527 kfree(pci_dev_id);
0528 return -ENOMEM;
0529 }
0530
0531 spin_lock_irqsave(&pcistub_devices_lock, flags);
0532
0533 if (initialize_devices) {
0534 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
0535
0536
0537 err = pcistub_init_device(psdev->dev);
0538
0539 spin_lock_irqsave(&pcistub_devices_lock, flags);
0540
0541 if (!err)
0542 list_add(&psdev->dev_list, &pcistub_devices);
0543 } else {
0544 dev_dbg(&dev->dev, "deferring initialization\n");
0545 list_add(&psdev->dev_list, &seized_devices);
0546 }
0547
0548 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
0549
0550 if (err) {
0551 kfree(pci_dev_id);
0552 pcistub_device_put(psdev);
0553 } else if (pci_dev_id)
0554 pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus),
0555 dev->bus->number, dev->devfn);
0556
0557 return err;
0558 }
0559
0560
0561
0562 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
0563 {
0564 int err = 0, match;
0565 struct pcistub_device_id *pci_dev_id = NULL;
0566
0567 dev_dbg(&dev->dev, "probing...\n");
0568
0569 match = pcistub_match(dev);
0570
0571 if ((dev->driver_override &&
0572 !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
0573 match) {
0574
0575 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
0576 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
0577 dev_err(&dev->dev, "can't export pci devices that "
0578 "don't have a normal (0) or bridge (1) "
0579 "header type!\n");
0580 err = -ENODEV;
0581 goto out;
0582 }
0583
0584 if (!match) {
0585 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
0586 if (!pci_dev_id) {
0587 err = -ENOMEM;
0588 goto out;
0589 }
0590 }
0591
0592 dev_info(&dev->dev, "seizing device\n");
0593 err = pcistub_seize(dev, pci_dev_id);
0594 } else
0595
0596 err = -ENODEV;
0597
0598 out:
0599 return err;
0600 }
0601
0602
0603
0604 static void pcistub_remove(struct pci_dev *dev)
0605 {
0606 struct pcistub_device *psdev, *found_psdev = NULL;
0607 unsigned long flags;
0608
0609 dev_dbg(&dev->dev, "removing\n");
0610
0611 spin_lock_irqsave(&pcistub_devices_lock, flags);
0612
0613 xen_pcibk_config_quirk_release(dev);
0614
0615 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
0616 if (psdev->dev == dev) {
0617 found_psdev = psdev;
0618 break;
0619 }
0620 }
0621
0622 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
0623
0624 if (found_psdev) {
0625 dev_dbg(&dev->dev, "found device to remove %s\n",
0626 found_psdev->pdev ? "- in-use" : "");
0627
0628 if (found_psdev->pdev) {
0629 int domid = xen_find_device_domain_owner(dev);
0630
0631 dev_warn(&dev->dev, "****** removing device %s while still in-use by domain %d! ******\n",
0632 pci_name(found_psdev->dev), domid);
0633 dev_warn(&dev->dev, "****** driver domain may still access this device's i/o resources!\n");
0634 dev_warn(&dev->dev, "****** shutdown driver domain before binding device\n");
0635 dev_warn(&dev->dev, "****** to other drivers or domains\n");
0636
0637
0638
0639 xen_pcibk_release_pci_dev(found_psdev->pdev,
0640 found_psdev->dev,
0641 false );
0642 }
0643
0644 spin_lock_irqsave(&pcistub_devices_lock, flags);
0645 list_del(&found_psdev->dev_list);
0646 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
0647
0648
0649 pcistub_device_put(found_psdev);
0650 }
0651 }
0652
0653 static const struct pci_device_id pcistub_ids[] = {
0654 {
0655 .vendor = PCI_ANY_ID,
0656 .device = PCI_ANY_ID,
0657 .subvendor = PCI_ANY_ID,
0658 .subdevice = PCI_ANY_ID,
0659 },
0660 {0,},
0661 };
0662
0663 #define PCI_NODENAME_MAX 40
0664 static void kill_domain_by_device(struct pcistub_device *psdev)
0665 {
0666 struct xenbus_transaction xbt;
0667 int err;
0668 char nodename[PCI_NODENAME_MAX];
0669
0670 BUG_ON(!psdev);
0671 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
0672 psdev->pdev->xdev->otherend_id);
0673
0674 again:
0675 err = xenbus_transaction_start(&xbt);
0676 if (err) {
0677 dev_err(&psdev->dev->dev,
0678 "error %d when start xenbus transaction\n", err);
0679 return;
0680 }
0681
0682 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
0683 err = xenbus_transaction_end(xbt, 0);
0684 if (err) {
0685 if (err == -EAGAIN)
0686 goto again;
0687 dev_err(&psdev->dev->dev,
0688 "error %d when end xenbus transaction\n", err);
0689 return;
0690 }
0691 }
0692
0693
0694
0695
0696
0697 static pci_ers_result_t common_process(struct pcistub_device *psdev,
0698 pci_channel_state_t state, int aer_cmd,
0699 pci_ers_result_t result)
0700 {
0701 pci_ers_result_t res = result;
0702 struct xen_pcie_aer_op *aer_op;
0703 struct xen_pcibk_device *pdev = psdev->pdev;
0704 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
0705 int ret;
0706
0707
0708 aer_op = &(sh_info->aer_op);
0709 aer_op->cmd = aer_cmd ;
0710
0711 aer_op->err = state;
0712
0713 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
0714 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
0715 if (!ret) {
0716 dev_err(&psdev->dev->dev, "failed to get pcifront device\n");
0717 return PCI_ERS_RESULT_NONE;
0718 }
0719 wmb();
0720
0721 dev_dbg(&psdev->dev->dev, "aer_op %x dom %x bus %x devfn %x\n",
0722 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
0723
0724
0725
0726
0727 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
0728
0729
0730
0731
0732
0733 set_bit(_XEN_PCIB_active,
0734 (unsigned long *)&sh_info->flags);
0735 wmb();
0736 notify_remote_via_irq(pdev->evtchn_irq);
0737
0738
0739 xen_pcibk_lateeoi(pdev, 0);
0740
0741 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
0742 !(test_bit(_XEN_PCIB_active, (unsigned long *)
0743 &sh_info->flags)), 300*HZ);
0744
0745
0746 if (!test_bit(_PDEVF_op_active, &pdev->flags))
0747 xen_pcibk_lateeoi(pdev, 0);
0748
0749 if (!ret) {
0750 if (test_bit(_XEN_PCIB_active,
0751 (unsigned long *)&sh_info->flags)) {
0752 dev_err(&psdev->dev->dev,
0753 "pcifront aer process not responding!\n");
0754 clear_bit(_XEN_PCIB_active,
0755 (unsigned long *)&sh_info->flags);
0756 aer_op->err = PCI_ERS_RESULT_NONE;
0757 return res;
0758 }
0759 }
0760 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
0761
0762 res = (pci_ers_result_t)aer_op->err;
0763 return res;
0764 }
0765
0766
0767
0768
0769
0770
0771
0772
0773 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
0774 {
0775 struct pcistub_device *psdev;
0776 pci_ers_result_t result;
0777
0778 result = PCI_ERS_RESULT_RECOVERED;
0779 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
0780 dev->bus->number, dev->devfn);
0781
0782 down_write(&pcistub_sem);
0783 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
0784 dev->bus->number,
0785 PCI_SLOT(dev->devfn),
0786 PCI_FUNC(dev->devfn));
0787
0788 if (!psdev || !psdev->pdev) {
0789 dev_err(&dev->dev, "device is not found/assigned\n");
0790 goto end;
0791 }
0792
0793 if (!psdev->pdev->sh_info) {
0794 dev_err(&dev->dev, "device is not connected or owned"
0795 " by HVM, kill it\n");
0796 kill_domain_by_device(psdev);
0797 goto end;
0798 }
0799
0800 if (!test_bit(_XEN_PCIB_AERHANDLER,
0801 (unsigned long *)&psdev->pdev->sh_info->flags)) {
0802 dev_err(&dev->dev,
0803 "guest with no AER driver should have been killed\n");
0804 goto end;
0805 }
0806 result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_slotreset, result);
0807
0808 if (result == PCI_ERS_RESULT_NONE ||
0809 result == PCI_ERS_RESULT_DISCONNECT) {
0810 dev_dbg(&dev->dev,
0811 "No AER slot_reset service or disconnected!\n");
0812 kill_domain_by_device(psdev);
0813 }
0814 end:
0815 if (psdev)
0816 pcistub_device_put(psdev);
0817 up_write(&pcistub_sem);
0818 return result;
0819
0820 }
0821
0822
0823
0824
0825
0826
0827
0828
0829
0830 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
0831 {
0832 struct pcistub_device *psdev;
0833 pci_ers_result_t result;
0834
0835 result = PCI_ERS_RESULT_RECOVERED;
0836 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
0837 dev->bus->number, dev->devfn);
0838
0839 down_write(&pcistub_sem);
0840 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
0841 dev->bus->number,
0842 PCI_SLOT(dev->devfn),
0843 PCI_FUNC(dev->devfn));
0844
0845 if (!psdev || !psdev->pdev) {
0846 dev_err(&dev->dev, "device is not found/assigned\n");
0847 goto end;
0848 }
0849
0850 if (!psdev->pdev->sh_info) {
0851 dev_err(&dev->dev, "device is not connected or owned"
0852 " by HVM, kill it\n");
0853 kill_domain_by_device(psdev);
0854 goto end;
0855 }
0856
0857 if (!test_bit(_XEN_PCIB_AERHANDLER,
0858 (unsigned long *)&psdev->pdev->sh_info->flags)) {
0859 dev_err(&dev->dev,
0860 "guest with no AER driver should have been killed\n");
0861 goto end;
0862 }
0863 result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_mmio, result);
0864
0865 if (result == PCI_ERS_RESULT_NONE ||
0866 result == PCI_ERS_RESULT_DISCONNECT) {
0867 dev_dbg(&dev->dev,
0868 "No AER mmio_enabled service or disconnected!\n");
0869 kill_domain_by_device(psdev);
0870 }
0871 end:
0872 if (psdev)
0873 pcistub_device_put(psdev);
0874 up_write(&pcistub_sem);
0875 return result;
0876 }
0877
0878
0879
0880
0881
0882
0883
0884
0885
0886 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
0887 pci_channel_state_t error)
0888 {
0889 struct pcistub_device *psdev;
0890 pci_ers_result_t result;
0891
0892 result = PCI_ERS_RESULT_CAN_RECOVER;
0893 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
0894 dev->bus->number, dev->devfn);
0895
0896 down_write(&pcistub_sem);
0897 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
0898 dev->bus->number,
0899 PCI_SLOT(dev->devfn),
0900 PCI_FUNC(dev->devfn));
0901
0902 if (!psdev || !psdev->pdev) {
0903 dev_err(&dev->dev, "device is not found/assigned\n");
0904 goto end;
0905 }
0906
0907 if (!psdev->pdev->sh_info) {
0908 dev_err(&dev->dev, "device is not connected or owned"
0909 " by HVM, kill it\n");
0910 kill_domain_by_device(psdev);
0911 goto end;
0912 }
0913
0914
0915 if (!test_bit(_XEN_PCIB_AERHANDLER,
0916 (unsigned long *)&psdev->pdev->sh_info->flags)) {
0917 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
0918 kill_domain_by_device(psdev);
0919 goto end;
0920 }
0921 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
0922
0923 if (result == PCI_ERS_RESULT_NONE ||
0924 result == PCI_ERS_RESULT_DISCONNECT) {
0925 dev_dbg(&dev->dev,
0926 "No AER error_detected service or disconnected!\n");
0927 kill_domain_by_device(psdev);
0928 }
0929 end:
0930 if (psdev)
0931 pcistub_device_put(psdev);
0932 up_write(&pcistub_sem);
0933 return result;
0934 }
0935
0936
0937
0938
0939
0940
0941
0942 static void xen_pcibk_error_resume(struct pci_dev *dev)
0943 {
0944 struct pcistub_device *psdev;
0945
0946 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
0947 dev->bus->number, dev->devfn);
0948
0949 down_write(&pcistub_sem);
0950 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
0951 dev->bus->number,
0952 PCI_SLOT(dev->devfn),
0953 PCI_FUNC(dev->devfn));
0954
0955 if (!psdev || !psdev->pdev) {
0956 dev_err(&dev->dev, "device is not found/assigned\n");
0957 goto end;
0958 }
0959
0960 if (!psdev->pdev->sh_info) {
0961 dev_err(&dev->dev, "device is not connected or owned"
0962 " by HVM, kill it\n");
0963 kill_domain_by_device(psdev);
0964 goto end;
0965 }
0966
0967 if (!test_bit(_XEN_PCIB_AERHANDLER,
0968 (unsigned long *)&psdev->pdev->sh_info->flags)) {
0969 dev_err(&dev->dev,
0970 "guest with no AER driver should have been killed\n");
0971 kill_domain_by_device(psdev);
0972 goto end;
0973 }
0974 common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_resume,
0975 PCI_ERS_RESULT_RECOVERED);
0976 end:
0977 if (psdev)
0978 pcistub_device_put(psdev);
0979 up_write(&pcistub_sem);
0980 return;
0981 }
0982
0983
0984 static const struct pci_error_handlers xen_pcibk_error_handler = {
0985 .error_detected = xen_pcibk_error_detected,
0986 .mmio_enabled = xen_pcibk_mmio_enabled,
0987 .slot_reset = xen_pcibk_slot_reset,
0988 .resume = xen_pcibk_error_resume,
0989 };
0990
0991
0992
0993
0994
0995
0996 static struct pci_driver xen_pcibk_pci_driver = {
0997
0998
0999 .name = PCISTUB_DRIVER_NAME,
1000 .id_table = pcistub_ids,
1001 .probe = pcistub_probe,
1002 .remove = pcistub_remove,
1003 .err_handler = &xen_pcibk_error_handler,
1004 };
1005
1006 static inline int str_to_slot(const char *buf, int *domain, int *bus,
1007 int *slot, int *func)
1008 {
1009 int parsed = 0;
1010
1011 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1012 &parsed)) {
1013 case 3:
1014 *func = -1;
1015 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
1016 break;
1017 case 2:
1018 *slot = *func = -1;
1019 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
1020 break;
1021 }
1022 if (parsed && !buf[parsed])
1023 return 0;
1024
1025
1026 *domain = 0;
1027 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
1028 case 2:
1029 *func = -1;
1030 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
1031 break;
1032 case 1:
1033 *slot = *func = -1;
1034 sscanf(buf, " %x:*.* %n", bus, &parsed);
1035 break;
1036 }
1037 if (parsed && !buf[parsed])
1038 return 0;
1039
1040 return -EINVAL;
1041 }
1042
1043 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1044 *slot, int *func, int *reg, int *size, int *mask)
1045 {
1046 int parsed = 0;
1047
1048 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
1049 reg, size, mask, &parsed);
1050 if (parsed && !buf[parsed])
1051 return 0;
1052
1053
1054 *domain = 0;
1055 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1056 mask, &parsed);
1057 if (parsed && !buf[parsed])
1058 return 0;
1059
1060 return -EINVAL;
1061 }
1062
1063 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1064 {
1065 struct pcistub_device_id *pci_dev_id;
1066 int rc = 0, devfn = PCI_DEVFN(slot, func);
1067
1068 if (slot < 0) {
1069 for (slot = 0; !rc && slot < 32; ++slot)
1070 rc = pcistub_device_id_add(domain, bus, slot, func);
1071 return rc;
1072 }
1073
1074 if (func < 0) {
1075 for (func = 0; !rc && func < 8; ++func)
1076 rc = pcistub_device_id_add(domain, bus, slot, func);
1077 return rc;
1078 }
1079
1080 if ((
1081 #if !defined(MODULE) \
1082 || !defined(CONFIG_PCI_DOMAINS)
1083 !pci_domains_supported ? domain :
1084 #endif
1085 domain < 0 || domain > 0xffff)
1086 || bus < 0 || bus > 0xff
1087 || PCI_SLOT(devfn) != slot
1088 || PCI_FUNC(devfn) != func)
1089 return -EINVAL;
1090
1091 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1092 if (!pci_dev_id)
1093 return -ENOMEM;
1094
1095 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1096 domain, bus, slot, func);
1097
1098 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
1099
1100 return 0;
1101 }
1102
1103 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1104 {
1105 struct pcistub_device_id *pci_dev_id, *t;
1106 int err = -ENOENT;
1107 unsigned long flags;
1108
1109 spin_lock_irqsave(&device_ids_lock, flags);
1110 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1111 slot_list) {
1112 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1113 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1114 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1115
1116
1117
1118 list_del(&pci_dev_id->slot_list);
1119 kfree(pci_dev_id);
1120
1121 err = 0;
1122
1123 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1124 domain, bus, slot, func);
1125 }
1126 }
1127 spin_unlock_irqrestore(&device_ids_lock, flags);
1128
1129 return err;
1130 }
1131
1132 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1133 unsigned int reg, unsigned int size,
1134 unsigned int mask)
1135 {
1136 int err = 0;
1137 struct pcistub_device *psdev;
1138 struct pci_dev *dev;
1139 struct config_field *field;
1140
1141 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1142 return -EINVAL;
1143
1144 psdev = pcistub_device_find(domain, bus, slot, func);
1145 if (!psdev) {
1146 err = -ENODEV;
1147 goto out;
1148 }
1149 dev = psdev->dev;
1150
1151 field = kzalloc(sizeof(*field), GFP_KERNEL);
1152 if (!field) {
1153 err = -ENOMEM;
1154 goto out;
1155 }
1156
1157 field->offset = reg;
1158 field->size = size;
1159 field->mask = mask;
1160 field->init = NULL;
1161 field->reset = NULL;
1162 field->release = NULL;
1163 field->clean = xen_pcibk_config_field_free;
1164
1165 err = xen_pcibk_config_quirks_add_field(dev, field);
1166 if (err)
1167 kfree(field);
1168 out:
1169 if (psdev)
1170 pcistub_device_put(psdev);
1171 return err;
1172 }
1173
1174 static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1175 size_t count)
1176 {
1177 int domain, bus, slot, func;
1178 int err;
1179
1180 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1181 if (err)
1182 goto out;
1183
1184 err = pcistub_device_id_add(domain, bus, slot, func);
1185
1186 out:
1187 if (!err)
1188 err = count;
1189 return err;
1190 }
1191 static DRIVER_ATTR_WO(new_slot);
1192
1193 static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1194 size_t count)
1195 {
1196 int domain, bus, slot, func;
1197 int err;
1198
1199 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1200 if (err)
1201 goto out;
1202
1203 err = pcistub_device_id_remove(domain, bus, slot, func);
1204
1205 out:
1206 if (!err)
1207 err = count;
1208 return err;
1209 }
1210 static DRIVER_ATTR_WO(remove_slot);
1211
1212 static ssize_t slots_show(struct device_driver *drv, char *buf)
1213 {
1214 struct pcistub_device_id *pci_dev_id;
1215 size_t count = 0;
1216 unsigned long flags;
1217
1218 spin_lock_irqsave(&device_ids_lock, flags);
1219 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1220 if (count >= PAGE_SIZE)
1221 break;
1222
1223 count += scnprintf(buf + count, PAGE_SIZE - count,
1224 "%04x:%02x:%02x.%d\n",
1225 pci_dev_id->domain, pci_dev_id->bus,
1226 PCI_SLOT(pci_dev_id->devfn),
1227 PCI_FUNC(pci_dev_id->devfn));
1228 }
1229 spin_unlock_irqrestore(&device_ids_lock, flags);
1230
1231 return count;
1232 }
1233 static DRIVER_ATTR_RO(slots);
1234
1235 static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
1236 {
1237 struct pcistub_device *psdev;
1238 struct xen_pcibk_dev_data *dev_data;
1239 size_t count = 0;
1240 unsigned long flags;
1241
1242 spin_lock_irqsave(&pcistub_devices_lock, flags);
1243 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1244 if (count >= PAGE_SIZE)
1245 break;
1246 if (!psdev->dev)
1247 continue;
1248 dev_data = pci_get_drvdata(psdev->dev);
1249 if (!dev_data)
1250 continue;
1251 count +=
1252 scnprintf(buf + count, PAGE_SIZE - count,
1253 "%s:%s:%sing:%ld\n",
1254 pci_name(psdev->dev),
1255 dev_data->isr_on ? "on" : "off",
1256 dev_data->ack_intr ? "ack" : "not ack",
1257 dev_data->handled);
1258 }
1259 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1260 return count;
1261 }
1262 static DRIVER_ATTR_RO(irq_handlers);
1263
1264 static ssize_t irq_handler_state_store(struct device_driver *drv,
1265 const char *buf, size_t count)
1266 {
1267 struct pcistub_device *psdev;
1268 struct xen_pcibk_dev_data *dev_data;
1269 int domain, bus, slot, func;
1270 int err;
1271
1272 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1273 if (err)
1274 return err;
1275
1276 psdev = pcistub_device_find(domain, bus, slot, func);
1277 if (!psdev) {
1278 err = -ENOENT;
1279 goto out;
1280 }
1281
1282 dev_data = pci_get_drvdata(psdev->dev);
1283 if (!dev_data) {
1284 err = -ENOENT;
1285 goto out;
1286 }
1287
1288 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1289 dev_data->irq_name, dev_data->isr_on,
1290 !dev_data->isr_on);
1291
1292 dev_data->isr_on = !(dev_data->isr_on);
1293 if (dev_data->isr_on)
1294 dev_data->ack_intr = 1;
1295 out:
1296 if (psdev)
1297 pcistub_device_put(psdev);
1298 if (!err)
1299 err = count;
1300 return err;
1301 }
1302 static DRIVER_ATTR_WO(irq_handler_state);
1303
1304 static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1305 size_t count)
1306 {
1307 int domain, bus, slot, func, reg, size, mask;
1308 int err;
1309
1310 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1311 &mask);
1312 if (err)
1313 goto out;
1314
1315 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1316
1317 out:
1318 if (!err)
1319 err = count;
1320 return err;
1321 }
1322
1323 static ssize_t quirks_show(struct device_driver *drv, char *buf)
1324 {
1325 int count = 0;
1326 unsigned long flags;
1327 struct xen_pcibk_config_quirk *quirk;
1328 struct xen_pcibk_dev_data *dev_data;
1329 const struct config_field *field;
1330 const struct config_field_entry *cfg_entry;
1331
1332 spin_lock_irqsave(&device_ids_lock, flags);
1333 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1334 if (count >= PAGE_SIZE)
1335 goto out;
1336
1337 count += scnprintf(buf + count, PAGE_SIZE - count,
1338 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1339 quirk->pdev->bus->number,
1340 PCI_SLOT(quirk->pdev->devfn),
1341 PCI_FUNC(quirk->pdev->devfn),
1342 quirk->devid.vendor, quirk->devid.device,
1343 quirk->devid.subvendor,
1344 quirk->devid.subdevice);
1345
1346 dev_data = pci_get_drvdata(quirk->pdev);
1347
1348 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1349 field = cfg_entry->field;
1350 if (count >= PAGE_SIZE)
1351 goto out;
1352
1353 count += scnprintf(buf + count, PAGE_SIZE - count,
1354 "\t\t%08x:%01x:%08x\n",
1355 cfg_entry->base_offset +
1356 field->offset, field->size,
1357 field->mask);
1358 }
1359 }
1360
1361 out:
1362 spin_unlock_irqrestore(&device_ids_lock, flags);
1363
1364 return count;
1365 }
1366 static DRIVER_ATTR_RW(quirks);
1367
1368 static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1369 size_t count)
1370 {
1371 int domain, bus, slot, func;
1372 int err;
1373 struct pcistub_device *psdev;
1374 struct xen_pcibk_dev_data *dev_data;
1375
1376 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1377 if (err)
1378 goto out;
1379
1380 psdev = pcistub_device_find(domain, bus, slot, func);
1381 if (!psdev) {
1382 err = -ENODEV;
1383 goto out;
1384 }
1385
1386 dev_data = pci_get_drvdata(psdev->dev);
1387
1388 if (!dev_data) {
1389 err = -ENXIO;
1390 goto release;
1391 }
1392 if (!dev_data->permissive) {
1393 dev_data->permissive = 1;
1394
1395 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1396 "configuration space accesses!\n");
1397 dev_warn(&psdev->dev->dev,
1398 "permissive mode is potentially unsafe!\n");
1399 }
1400 release:
1401 pcistub_device_put(psdev);
1402 out:
1403 if (!err)
1404 err = count;
1405 return err;
1406 }
1407
1408 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1409 {
1410 struct pcistub_device *psdev;
1411 struct xen_pcibk_dev_data *dev_data;
1412 size_t count = 0;
1413 unsigned long flags;
1414 spin_lock_irqsave(&pcistub_devices_lock, flags);
1415 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1416 if (count >= PAGE_SIZE)
1417 break;
1418 if (!psdev->dev)
1419 continue;
1420 dev_data = pci_get_drvdata(psdev->dev);
1421 if (!dev_data || !dev_data->permissive)
1422 continue;
1423 count +=
1424 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1425 pci_name(psdev->dev));
1426 }
1427 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1428 return count;
1429 }
1430 static DRIVER_ATTR_RW(permissive);
1431
1432 static ssize_t allow_interrupt_control_store(struct device_driver *drv,
1433 const char *buf, size_t count)
1434 {
1435 int domain, bus, slot, func;
1436 int err;
1437 struct pcistub_device *psdev;
1438 struct xen_pcibk_dev_data *dev_data;
1439
1440 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1441 if (err)
1442 goto out;
1443
1444 psdev = pcistub_device_find(domain, bus, slot, func);
1445 if (!psdev) {
1446 err = -ENODEV;
1447 goto out;
1448 }
1449
1450 dev_data = pci_get_drvdata(psdev->dev);
1451
1452 if (!dev_data) {
1453 err = -ENXIO;
1454 goto release;
1455 }
1456 dev_data->allow_interrupt_control = 1;
1457 release:
1458 pcistub_device_put(psdev);
1459 out:
1460 if (!err)
1461 err = count;
1462 return err;
1463 }
1464
1465 static ssize_t allow_interrupt_control_show(struct device_driver *drv,
1466 char *buf)
1467 {
1468 struct pcistub_device *psdev;
1469 struct xen_pcibk_dev_data *dev_data;
1470 size_t count = 0;
1471 unsigned long flags;
1472
1473 spin_lock_irqsave(&pcistub_devices_lock, flags);
1474 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1475 if (count >= PAGE_SIZE)
1476 break;
1477 if (!psdev->dev)
1478 continue;
1479 dev_data = pci_get_drvdata(psdev->dev);
1480 if (!dev_data || !dev_data->allow_interrupt_control)
1481 continue;
1482 count +=
1483 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1484 pci_name(psdev->dev));
1485 }
1486 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1487 return count;
1488 }
1489 static DRIVER_ATTR_RW(allow_interrupt_control);
1490
1491 static void pcistub_exit(void)
1492 {
1493 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1494 driver_remove_file(&xen_pcibk_pci_driver.driver,
1495 &driver_attr_remove_slot);
1496 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1497 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1498 driver_remove_file(&xen_pcibk_pci_driver.driver,
1499 &driver_attr_permissive);
1500 driver_remove_file(&xen_pcibk_pci_driver.driver,
1501 &driver_attr_allow_interrupt_control);
1502 driver_remove_file(&xen_pcibk_pci_driver.driver,
1503 &driver_attr_irq_handlers);
1504 driver_remove_file(&xen_pcibk_pci_driver.driver,
1505 &driver_attr_irq_handler_state);
1506 pci_unregister_driver(&xen_pcibk_pci_driver);
1507 }
1508
1509 static int __init pcistub_init(void)
1510 {
1511 int pos = 0;
1512 int err = 0;
1513 int domain, bus, slot, func;
1514 int parsed;
1515
1516 if (pci_devs_to_hide && *pci_devs_to_hide) {
1517 do {
1518 parsed = 0;
1519
1520 err = sscanf(pci_devs_to_hide + pos,
1521 " (%x:%x:%x.%x) %n",
1522 &domain, &bus, &slot, &func, &parsed);
1523 switch (err) {
1524 case 3:
1525 func = -1;
1526 sscanf(pci_devs_to_hide + pos,
1527 " (%x:%x:%x.*) %n",
1528 &domain, &bus, &slot, &parsed);
1529 break;
1530 case 2:
1531 slot = func = -1;
1532 sscanf(pci_devs_to_hide + pos,
1533 " (%x:%x:*.*) %n",
1534 &domain, &bus, &parsed);
1535 break;
1536 }
1537
1538 if (!parsed) {
1539 domain = 0;
1540 err = sscanf(pci_devs_to_hide + pos,
1541 " (%x:%x.%x) %n",
1542 &bus, &slot, &func, &parsed);
1543 switch (err) {
1544 case 2:
1545 func = -1;
1546 sscanf(pci_devs_to_hide + pos,
1547 " (%x:%x.*) %n",
1548 &bus, &slot, &parsed);
1549 break;
1550 case 1:
1551 slot = func = -1;
1552 sscanf(pci_devs_to_hide + pos,
1553 " (%x:*.*) %n",
1554 &bus, &parsed);
1555 break;
1556 }
1557 }
1558
1559 if (parsed <= 0)
1560 goto parse_error;
1561
1562 err = pcistub_device_id_add(domain, bus, slot, func);
1563 if (err)
1564 goto out;
1565
1566 pos += parsed;
1567 } while (pci_devs_to_hide[pos]);
1568 }
1569
1570
1571
1572
1573
1574 err = pci_register_driver(&xen_pcibk_pci_driver);
1575 if (err < 0)
1576 goto out;
1577
1578 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1579 &driver_attr_new_slot);
1580 if (!err)
1581 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1582 &driver_attr_remove_slot);
1583 if (!err)
1584 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1585 &driver_attr_slots);
1586 if (!err)
1587 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1588 &driver_attr_quirks);
1589 if (!err)
1590 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1591 &driver_attr_permissive);
1592 if (!err)
1593 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1594 &driver_attr_allow_interrupt_control);
1595
1596 if (!err)
1597 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1598 &driver_attr_irq_handlers);
1599 if (!err)
1600 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1601 &driver_attr_irq_handler_state);
1602 if (err)
1603 pcistub_exit();
1604
1605 out:
1606 return err;
1607
1608 parse_error:
1609 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1610 pci_devs_to_hide + pos);
1611 return -EINVAL;
1612 }
1613
1614 #ifndef MODULE
1615
1616
1617
1618
1619
1620
1621
1622 fs_initcall(pcistub_init);
1623 #endif
1624
1625 #ifdef CONFIG_PCI_IOV
1626 static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1627 {
1628 struct pcistub_device *psdev = NULL;
1629 unsigned long flags;
1630 bool found = false;
1631
1632 spin_lock_irqsave(&pcistub_devices_lock, flags);
1633 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1634 if (!psdev->pdev && psdev->dev != pdev
1635 && pci_physfn(psdev->dev) == pdev) {
1636 found = true;
1637 break;
1638 }
1639 }
1640 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1641 if (found)
1642 return psdev;
1643 return NULL;
1644 }
1645
1646 static int pci_stub_notifier(struct notifier_block *nb,
1647 unsigned long action, void *data)
1648 {
1649 struct device *dev = data;
1650 const struct pci_dev *pdev = to_pci_dev(dev);
1651
1652 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1653 return NOTIFY_DONE;
1654
1655 if (!pdev->is_physfn)
1656 return NOTIFY_DONE;
1657
1658 for (;;) {
1659 struct pcistub_device *psdev = find_vfs(pdev);
1660 if (!psdev)
1661 break;
1662 device_release_driver(&psdev->dev->dev);
1663 }
1664 return NOTIFY_DONE;
1665 }
1666
1667 static struct notifier_block pci_stub_nb = {
1668 .notifier_call = pci_stub_notifier,
1669 };
1670 #endif
1671
1672 static int __init xen_pcibk_init(void)
1673 {
1674 int err;
1675
1676 if (!xen_initial_domain())
1677 return -ENODEV;
1678
1679 err = xen_pcibk_config_init();
1680 if (err)
1681 return err;
1682
1683 #ifdef MODULE
1684 err = pcistub_init();
1685 if (err < 0)
1686 return err;
1687 #endif
1688
1689 pcistub_init_devices_late();
1690 err = xen_pcibk_xenbus_register();
1691 if (err)
1692 pcistub_exit();
1693 #ifdef CONFIG_PCI_IOV
1694 else
1695 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1696 #endif
1697
1698 return err;
1699 }
1700
1701 static void __exit xen_pcibk_cleanup(void)
1702 {
1703 #ifdef CONFIG_PCI_IOV
1704 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1705 #endif
1706 xen_pcibk_xenbus_unregister();
1707 pcistub_exit();
1708 }
1709
1710 module_init(xen_pcibk_init);
1711 module_exit(xen_pcibk_cleanup);
1712
1713 MODULE_LICENSE("Dual BSD/GPL");
1714 MODULE_ALIAS("xen-backend:pci");