2 * PCI Stub Driver - Grabs devices in backend to be exported later
4 * Ryan Wilson <hap9@epoch.ncsc.mil>
5 * Chris Bookholt <hap10@epoch.ncsc.mil>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/list.h>
10 #include <linux/spinlock.h>
11 #include <linux/kref.h>
12 #include <asm/atomic.h>
14 #include "conf_space.h"
15 #include "conf_space_quirks.h"
17 static char *pci_devs_to_hide = NULL;
18 module_param_named(hide, pci_devs_to_hide, charp, 0444);
20 struct pcistub_device_id {
21 struct list_head slot_list;
26 static LIST_HEAD(pcistub_device_ids);
27 static DEFINE_SPINLOCK(device_ids_lock);
29 struct pcistub_device {
31 struct list_head dev_list;
35 struct pciback_device *pdev; /* non-NULL if struct pci_dev is in use */
38 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
39 * flag must be locked with pcistub_devices_lock
41 static DEFINE_SPINLOCK(pcistub_devices_lock);
42 static LIST_HEAD(pcistub_devices);
44 /* wait for device_initcall before initializing our devices
45 * (see pcistub_init_devices_late)
47 static int initialize_devices = 0;
48 static LIST_HEAD(seized_devices);
50 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
52 struct pcistub_device *psdev;
54 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
56 psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
60 psdev->dev = pci_dev_get(dev);
66 kref_init(&psdev->kref);
67 spin_lock_init(&psdev->lock);
72 /* Don't call this directly as it's called by pcistub_device_put */
73 static void pcistub_device_release(struct kref *kref)
75 struct pcistub_device *psdev;
77 psdev = container_of(kref, struct pcistub_device, kref);
79 dev_dbg(&psdev->dev->dev, "pcistub_device_release\n");
81 /* Clean-up the device */
82 pciback_reset_device(psdev->dev);
83 pciback_config_free_dyn_fields(psdev->dev);
84 pciback_config_free_dev(psdev->dev);
85 kfree(pci_get_drvdata(psdev->dev));
86 pci_set_drvdata(psdev->dev, NULL);
88 pci_dev_put(psdev->dev);
93 static inline void pcistub_device_get(struct pcistub_device *psdev)
95 kref_get(&psdev->kref);
98 static inline void pcistub_device_put(struct pcistub_device *psdev)
100 kref_put(&psdev->kref, pcistub_device_release);
103 static struct pcistub_device *pcistub_device_find(int domain, int bus,
106 struct pcistub_device *psdev = NULL;
109 spin_lock_irqsave(&pcistub_devices_lock, flags);
111 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
112 if (psdev->dev != NULL
113 && domain == pci_domain_nr(psdev->dev->bus)
114 && bus == psdev->dev->bus->number
115 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
116 pcistub_device_get(psdev);
125 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
129 static struct pci_dev *pcistub_device_get_pci_dev(struct pciback_device *pdev,
130 struct pcistub_device *psdev)
132 struct pci_dev *pci_dev = NULL;
135 pcistub_device_get(psdev);
137 spin_lock_irqsave(&psdev->lock, flags);
140 pci_dev = psdev->dev;
142 spin_unlock_irqrestore(&psdev->lock, flags);
145 pcistub_device_put(psdev);
150 struct pci_dev *pcistub_get_pci_dev_by_slot(struct pciback_device *pdev,
154 struct pcistub_device *psdev;
155 struct pci_dev *found_dev = NULL;
158 spin_lock_irqsave(&pcistub_devices_lock, flags);
160 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
161 if (psdev->dev != NULL
162 && domain == pci_domain_nr(psdev->dev->bus)
163 && bus == psdev->dev->bus->number
164 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
165 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
170 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
174 struct pci_dev *pcistub_get_pci_dev(struct pciback_device *pdev,
177 struct pcistub_device *psdev;
178 struct pci_dev *found_dev = NULL;
181 spin_lock_irqsave(&pcistub_devices_lock, flags);
183 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
184 if (psdev->dev == dev) {
185 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
190 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
194 void pcistub_put_pci_dev(struct pci_dev *dev)
196 struct pcistub_device *psdev, *found_psdev = NULL;
199 spin_lock_irqsave(&pcistub_devices_lock, flags);
201 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
202 if (psdev->dev == dev) {
208 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
210 /* Cleanup our device
211 * (so it's ready for the next domain)
213 pciback_reset_device(found_psdev->dev);
214 pciback_config_free_dyn_fields(found_psdev->dev);
215 pciback_config_reset_dev(found_psdev->dev);
217 spin_lock_irqsave(&found_psdev->lock, flags);
218 found_psdev->pdev = NULL;
219 spin_unlock_irqrestore(&found_psdev->lock, flags);
221 pcistub_device_put(found_psdev);
224 static int __devinit pcistub_match_one(struct pci_dev *dev,
225 struct pcistub_device_id *pdev_id)
227 /* Match the specified device by domain, bus, slot, func and also if
228 * any of the device's parent bridges match.
230 for (; dev != NULL; dev = dev->bus->self) {
231 if (pci_domain_nr(dev->bus) == pdev_id->domain
232 && dev->bus->number == pdev_id->bus
233 && dev->devfn == pdev_id->devfn)
236 /* Sometimes topmost bridge links to itself. */
237 if (dev == dev->bus->self)
244 static int __devinit pcistub_match(struct pci_dev *dev)
246 struct pcistub_device_id *pdev_id;
250 spin_lock_irqsave(&device_ids_lock, flags);
251 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
252 if (pcistub_match_one(dev, pdev_id)) {
257 spin_unlock_irqrestore(&device_ids_lock, flags);
262 static int __devinit pcistub_init_device(struct pci_dev *dev)
264 struct pciback_dev_data *dev_data;
267 dev_dbg(&dev->dev, "initializing...\n");
269 /* The PCI backend is not intended to be a module (or to work with
270 * removable PCI devices (yet). If it were, pciback_config_free()
271 * would need to be called somewhere to free the memory allocated
272 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
274 dev_data = kzalloc(sizeof(*dev_data), GFP_ATOMIC);
279 pci_set_drvdata(dev, dev_data);
281 dev_dbg(&dev->dev, "initializing config\n");
282 err = pciback_config_init_dev(dev);
286 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
287 * must do this here because pcibios_enable_device may specify
288 * the pci device's true irq (and possibly its other resources)
289 * if they differ from what's in the configuration space.
290 * This makes the assumption that the device's resources won't
291 * change after this point (otherwise this code may break!)
293 dev_dbg(&dev->dev, "enabling device\n");
294 err = pci_enable_device(dev);
298 /* Now disable the device (this also ensures some private device
299 * data is setup before we export)
301 dev_dbg(&dev->dev, "reset device\n");
302 pciback_reset_device(dev);
307 pciback_config_free_dev(dev);
310 pci_set_drvdata(dev, NULL);
316 * Because some initialization still happens on
317 * devices during fs_initcall, we need to defer
318 * full initialization of our devices until
321 static int __init pcistub_init_devices_late(void)
323 struct pcistub_device *psdev;
327 pr_debug("pciback: pcistub_init_devices_late\n");
329 spin_lock_irqsave(&pcistub_devices_lock, flags);
331 while (!list_empty(&seized_devices)) {
332 psdev = container_of(seized_devices.next,
333 struct pcistub_device, dev_list);
334 list_del(&psdev->dev_list);
336 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
338 err = pcistub_init_device(psdev->dev);
340 dev_err(&psdev->dev->dev,
341 "error %d initializing device\n", err);
346 spin_lock_irqsave(&pcistub_devices_lock, flags);
349 list_add_tail(&psdev->dev_list, &pcistub_devices);
352 initialize_devices = 1;
354 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
359 static int __devinit pcistub_seize(struct pci_dev *dev)
361 struct pcistub_device *psdev;
365 psdev = pcistub_device_alloc(dev);
369 spin_lock_irqsave(&pcistub_devices_lock, flags);
371 if (initialize_devices) {
372 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
374 /* don't want irqs disabled when calling pcistub_init_device */
375 err = pcistub_init_device(psdev->dev);
377 spin_lock_irqsave(&pcistub_devices_lock, flags);
380 list_add(&psdev->dev_list, &pcistub_devices);
382 dev_dbg(&dev->dev, "deferring initialization\n");
383 list_add(&psdev->dev_list, &seized_devices);
386 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
389 pcistub_device_put(psdev);
394 static int __devinit pcistub_probe(struct pci_dev *dev,
395 const struct pci_device_id *id)
399 dev_dbg(&dev->dev, "probing...\n");
401 if (pcistub_match(dev)) {
403 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
404 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
405 dev_err(&dev->dev, "can't export pci devices that "
406 "don't have a normal (0) or bridge (1) "
412 dev_info(&dev->dev, "seizing device\n");
413 err = pcistub_seize(dev);
415 /* Didn't find the device */
422 static void pcistub_remove(struct pci_dev *dev)
424 struct pcistub_device *psdev, *found_psdev = NULL;
427 dev_dbg(&dev->dev, "removing\n");
429 spin_lock_irqsave(&pcistub_devices_lock, flags);
431 pciback_config_quirk_release(dev);
433 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
434 if (psdev->dev == dev) {
440 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
443 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
446 if (found_psdev->pdev) {
447 printk(KERN_WARNING "pciback: ****** removing device "
448 "%s while still in-use! ******\n",
449 pci_name(found_psdev->dev));
450 printk(KERN_WARNING "pciback: ****** driver domain may "
451 "still access this device's i/o resources!\n");
452 printk(KERN_WARNING "pciback: ****** shutdown driver "
453 "domain before binding device\n");
454 printk(KERN_WARNING "pciback: ****** to other drivers "
457 pciback_release_pci_dev(found_psdev->pdev,
461 spin_lock_irqsave(&pcistub_devices_lock, flags);
462 list_del(&found_psdev->dev_list);
463 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
465 /* the final put for releasing from the list */
466 pcistub_device_put(found_psdev);
470 static struct pci_device_id pcistub_ids[] = {
472 .vendor = PCI_ANY_ID,
473 .device = PCI_ANY_ID,
474 .subvendor = PCI_ANY_ID,
475 .subdevice = PCI_ANY_ID,
481 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
482 * for a normal device. I don't want it to be loaded automatically.
485 static struct pci_driver pciback_pci_driver = {
487 .id_table = pcistub_ids,
488 .probe = pcistub_probe,
489 .remove = pcistub_remove,
492 static inline int str_to_slot(const char *buf, int *domain, int *bus,
493 int *slot, int *func)
497 err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
503 /* try again without domain */
505 err = sscanf(buf, " %x:%x.%x", bus, slot, func);
512 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
513 *slot, int *func, int *reg, int *size, int *mask)
518 sscanf(buf, " %04x:%02x:%02x.%1x-%08x:%1x:%08x", domain, bus, slot,
519 func, reg, size, mask);
525 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
527 struct pcistub_device_id *pci_dev_id;
530 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
534 pci_dev_id->domain = domain;
535 pci_dev_id->bus = bus;
536 pci_dev_id->devfn = PCI_DEVFN(slot, func);
538 pr_debug("pciback: wants to seize %04x:%02x:%02x.%01x\n",
539 domain, bus, slot, func);
541 spin_lock_irqsave(&device_ids_lock, flags);
542 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
543 spin_unlock_irqrestore(&device_ids_lock, flags);
548 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
550 struct pcistub_device_id *pci_dev_id, *t;
551 int devfn = PCI_DEVFN(slot, func);
555 spin_lock_irqsave(&device_ids_lock, flags);
556 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids, slot_list) {
558 if (pci_dev_id->domain == domain
559 && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
560 /* Don't break; here because it's possible the same
561 * slot could be in the list more than once
563 list_del(&pci_dev_id->slot_list);
568 pr_debug("pciback: removed %04x:%02x:%02x.%01x from "
569 "seize list\n", domain, bus, slot, func);
572 spin_unlock_irqrestore(&device_ids_lock, flags);
577 static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
581 struct pcistub_device *psdev;
583 struct config_field *field;
585 psdev = pcistub_device_find(domain, bus, slot, func);
586 if (!psdev || !psdev->dev) {
592 /* check for duplicate field */
593 if (pciback_field_is_dup(dev, reg))
596 field = kzalloc(sizeof(*field), GFP_ATOMIC);
607 field->release = NULL;
608 field->clean = pciback_config_field_free;
610 err = pciback_config_quirks_add_field(dev, field);
617 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
620 int domain, bus, slot, func;
623 err = str_to_slot(buf, &domain, &bus, &slot, &func);
627 err = pcistub_device_id_add(domain, bus, slot, func);
635 DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
637 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
640 int domain, bus, slot, func;
643 err = str_to_slot(buf, &domain, &bus, &slot, &func);
647 err = pcistub_device_id_remove(domain, bus, slot, func);
655 DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
657 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
659 struct pcistub_device_id *pci_dev_id;
663 spin_lock_irqsave(&device_ids_lock, flags);
664 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
665 if (count >= PAGE_SIZE)
668 count += scnprintf(buf + count, PAGE_SIZE - count,
669 "%04x:%02x:%02x.%01x\n",
670 pci_dev_id->domain, pci_dev_id->bus,
671 PCI_SLOT(pci_dev_id->devfn),
672 PCI_FUNC(pci_dev_id->devfn));
674 spin_unlock_irqrestore(&device_ids_lock, flags);
679 DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
681 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
684 int domain, bus, slot, func, reg, size, mask;
687 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
692 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
700 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
704 extern struct list_head pciback_quirks;
705 struct pciback_config_quirk *quirk;
706 struct pciback_dev_data *dev_data;
707 struct config_field *field;
708 struct config_field_entry *cfg_entry;
710 spin_lock_irqsave(&device_ids_lock, flags);
711 list_for_each_entry(quirk, &pciback_quirks, quirks_list) {
712 if (count >= PAGE_SIZE)
715 count += scnprintf(buf + count, PAGE_SIZE - count,
716 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
717 quirk->pdev->bus->number,
718 PCI_SLOT(quirk->pdev->devfn),
719 PCI_FUNC(quirk->pdev->devfn),
720 quirk->devid.vendor, quirk->devid.device,
721 quirk->devid.subvendor,
722 quirk->devid.subdevice);
724 dev_data = pci_get_drvdata(quirk->pdev);
726 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
727 field = cfg_entry->field;
728 if (count >= PAGE_SIZE)
731 count += scnprintf(buf + count, PAGE_SIZE -
732 count, "\t\t%08x:%01x:%08x\n",
733 field->offset, field->size,
739 spin_unlock_irqrestore(&device_ids_lock, flags);
744 DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show, pcistub_quirk_add);
746 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
749 int domain, bus, slot, func;
751 struct pcistub_device *psdev;
752 struct pciback_dev_data *dev_data;
753 err = str_to_slot(buf, &domain, &bus, &slot, &func);
756 psdev = pcistub_device_find(domain, bus, slot, func);
765 dev_data = pci_get_drvdata(psdev->dev);
766 /* the driver data for a device should never be null at this point */
771 if (!dev_data->permissive) {
772 dev_data->permissive = 1;
773 /* Let user know that what they're doing could be unsafe */
774 dev_warn(&psdev->dev->dev,
775 "enabling permissive mode configuration space accesses!\n");
776 dev_warn(&psdev->dev->dev,
777 "permissive mode is potentially unsafe!\n");
780 pcistub_device_put(psdev);
787 static ssize_t permissive_show(struct device_driver *drv, char *buf)
789 struct pcistub_device *psdev;
790 struct pciback_dev_data *dev_data;
793 spin_lock_irqsave(&pcistub_devices_lock, flags);
794 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
795 if (count >= PAGE_SIZE)
799 dev_data = pci_get_drvdata(psdev->dev);
800 if (!dev_data || !dev_data->permissive)
803 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
804 pci_name(psdev->dev));
806 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
810 DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show, permissive_add);
812 static int __init pcistub_init(void)
816 int domain, bus, slot, func;
819 if (pci_devs_to_hide && *pci_devs_to_hide) {
823 err = sscanf(pci_devs_to_hide + pos,
825 &domain, &bus, &slot, &func, &parsed);
828 err = sscanf(pci_devs_to_hide + pos,
830 &bus, &slot, &func, &parsed);
835 err = pcistub_device_id_add(domain, bus, slot, func);
839 /* if parsed<=0, we've reached the end of the string */
841 } while (parsed > 0 && pci_devs_to_hide[pos]);
844 /* If we're the first PCI Device Driver to register, we're the
845 * first one to get offered PCI devices as they become
846 * available (and thus we can be the first to grab them)
848 err = pci_register_driver(&pciback_pci_driver);
852 driver_create_file(&pciback_pci_driver.driver, &driver_attr_new_slot);
853 driver_create_file(&pciback_pci_driver.driver,
854 &driver_attr_remove_slot);
855 driver_create_file(&pciback_pci_driver.driver, &driver_attr_slots);
856 driver_create_file(&pciback_pci_driver.driver, &driver_attr_quirks);
857 driver_create_file(&pciback_pci_driver.driver, &driver_attr_permissive);
863 printk(KERN_ERR "pciback: Error parsing pci_devs_to_hide at \"%s\"\n",
864 pci_devs_to_hide + pos);
870 * fs_initcall happens before device_initcall
871 * so pciback *should* get called first (b/c we
872 * want to suck up any device before other drivers
873 * get a chance by being the first pci device
874 * driver to register)
876 fs_initcall(pcistub_init);
879 static int __init pciback_init(void)
883 err = pciback_config_init();
888 err = pcistub_init();
893 pcistub_init_devices_late();
894 pciback_xenbus_register();
899 static void __exit pciback_cleanup(void)
901 pciback_xenbus_unregister();
903 driver_remove_file(&pciback_pci_driver.driver, &driver_attr_new_slot);
904 driver_remove_file(&pciback_pci_driver.driver,
905 &driver_attr_remove_slot);
906 driver_remove_file(&pciback_pci_driver.driver, &driver_attr_slots);
907 driver_remove_file(&pciback_pci_driver.driver, &driver_attr_quirks);
908 driver_remove_file(&pciback_pci_driver.driver, &driver_attr_permissive);
910 pci_unregister_driver(&pciback_pci_driver);
913 module_init(pciback_init);
914 module_exit(pciback_cleanup);
916 MODULE_LICENSE("Dual BSD/GPL");