ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/pciback/pci_stub.c @ 10843:4f6d858ea570

[PCI] Per-device permissive flag (replaces global permissive flag).
Signed-off-by: Chris Bookholt <hap10@tycho.ncsc.mil>
author kfraser@localhost.localdomain
date Fri Jul 28 12:56:10 2006 +0100 (2006-07-28)
parents 4c97599398fe
children 10dc10473c3f
line source
1 /*
2 * PCI Stub Driver - Grabs devices in backend to be exported later
3 *
4 * Ryan Wilson <hap9@epoch.ncsc.mil>
5 * Chris Bookholt <hap10@epoch.ncsc.mil>
6 */
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>
13 #include "pciback.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;
22 int domain;
23 unsigned char bus;
24 unsigned int devfn;
25 };
26 static LIST_HEAD(pcistub_device_ids);
27 static DEFINE_SPINLOCK(device_ids_lock);
29 struct pcistub_device {
30 struct kref kref;
31 struct list_head dev_list;
32 spinlock_t lock;
34 struct pci_dev *dev;
35 struct pciback_device *pdev; /* non-NULL if struct pci_dev is in use */
36 };
38 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
39 * flag must be locked with pcistub_devices_lock
40 */
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)
46 */
47 static int initialize_devices = 0;
48 static LIST_HEAD(seized_devices);
50 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
51 {
52 struct pcistub_device *psdev;
54 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
56 psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
57 if (!psdev)
58 return NULL;
60 psdev->dev = pci_dev_get(dev);
61 if (!psdev->dev) {
62 kfree(psdev);
63 return NULL;
64 }
66 kref_init(&psdev->kref);
67 spin_lock_init(&psdev->lock);
69 return psdev;
70 }
72 /* Don't call this directly as it's called by pcistub_device_put */
73 static void pcistub_device_release(struct kref *kref)
74 {
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);
90 kfree(psdev);
91 }
93 static inline void pcistub_device_get(struct pcistub_device *psdev)
94 {
95 kref_get(&psdev->kref);
96 }
98 static inline void pcistub_device_put(struct pcistub_device *psdev)
99 {
100 kref_put(&psdev->kref, pcistub_device_release);
101 }
103 static struct pcistub_device *pcistub_device_find(int domain, int bus,
104 int slot, int func)
105 {
106 struct pcistub_device *psdev = NULL;
107 unsigned long flags;
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);
117 goto out;
118 }
119 }
121 /* didn't find it */
122 psdev = NULL;
124 out:
125 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
126 return psdev;
127 }
129 static struct pci_dev *pcistub_device_get_pci_dev(struct pciback_device *pdev,
130 struct pcistub_device *psdev)
131 {
132 struct pci_dev *pci_dev = NULL;
133 unsigned long flags;
135 pcistub_device_get(psdev);
137 spin_lock_irqsave(&psdev->lock, flags);
138 if (!psdev->pdev) {
139 psdev->pdev = pdev;
140 pci_dev = psdev->dev;
141 }
142 spin_unlock_irqrestore(&psdev->lock, flags);
144 if (!pci_dev)
145 pcistub_device_put(psdev);
147 return pci_dev;
148 }
150 struct pci_dev *pcistub_get_pci_dev_by_slot(struct pciback_device *pdev,
151 int domain, int bus,
152 int slot, int func)
153 {
154 struct pcistub_device *psdev;
155 struct pci_dev *found_dev = NULL;
156 unsigned long flags;
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);
166 break;
167 }
168 }
170 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
171 return found_dev;
172 }
174 struct pci_dev *pcistub_get_pci_dev(struct pciback_device *pdev,
175 struct pci_dev *dev)
176 {
177 struct pcistub_device *psdev;
178 struct pci_dev *found_dev = NULL;
179 unsigned long flags;
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);
186 break;
187 }
188 }
190 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
191 return found_dev;
192 }
194 void pcistub_put_pci_dev(struct pci_dev *dev)
195 {
196 struct pcistub_device *psdev, *found_psdev = NULL;
197 unsigned long flags;
199 spin_lock_irqsave(&pcistub_devices_lock, flags);
201 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
202 if (psdev->dev == dev) {
203 found_psdev = psdev;
204 break;
205 }
206 }
208 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
210 /* Cleanup our device
211 * (so it's ready for the next domain)
212 */
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);
222 }
224 static int __devinit pcistub_match_one(struct pci_dev *dev,
225 struct pcistub_device_id *pdev_id)
226 {
227 /* Match the specified device by domain, bus, slot, func and also if
228 * any of the device's parent bridges match.
229 */
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)
234 return 1;
235 }
237 return 0;
238 }
240 static int __devinit pcistub_match(struct pci_dev *dev)
241 {
242 struct pcistub_device_id *pdev_id;
243 unsigned long flags;
244 int found = 0;
246 spin_lock_irqsave(&device_ids_lock, flags);
247 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
248 if (pcistub_match_one(dev, pdev_id)) {
249 found = 1;
250 break;
251 }
252 }
253 spin_unlock_irqrestore(&device_ids_lock, flags);
255 return found;
256 }
258 static int __devinit pcistub_init_device(struct pci_dev *dev)
259 {
260 struct pciback_dev_data *dev_data;
261 int err = 0;
263 dev_dbg(&dev->dev, "initializing...\n");
265 /* The PCI backend is not intended to be a module (or to work with
266 * removable PCI devices (yet). If it were, pciback_config_free()
267 * would need to be called somewhere to free the memory allocated
268 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
269 */
270 dev_data = kzalloc(sizeof(*dev_data), GFP_ATOMIC);
271 if (!dev_data) {
272 err = -ENOMEM;
273 goto out;
274 }
275 pci_set_drvdata(dev, dev_data);
277 dev_dbg(&dev->dev, "initializing config\n");
278 err = pciback_config_init_dev(dev);
279 if (err)
280 goto out;
282 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
283 * must do this here because pcibios_enable_device may specify
284 * the pci device's true irq (and possibly its other resources)
285 * if they differ from what's in the configuration space.
286 * This makes the assumption that the device's resources won't
287 * change after this point (otherwise this code may break!)
288 */
289 dev_dbg(&dev->dev, "enabling device\n");
290 err = pci_enable_device(dev);
291 if (err)
292 goto config_release;
294 /* Now disable the device (this also ensures some private device
295 * data is setup before we export)
296 */
297 dev_dbg(&dev->dev, "reset device\n");
298 pciback_reset_device(dev);
300 return 0;
302 config_release:
303 pciback_config_free_dev(dev);
305 out:
306 pci_set_drvdata(dev, NULL);
307 kfree(dev_data);
308 return err;
309 }
311 /*
312 * Because some initialization still happens on
313 * devices during fs_initcall, we need to defer
314 * full initialization of our devices until
315 * device_initcall.
316 */
317 static int __init pcistub_init_devices_late(void)
318 {
319 struct pcistub_device *psdev;
320 unsigned long flags;
321 int err = 0;
323 pr_debug("pciback: pcistub_init_devices_late\n");
325 spin_lock_irqsave(&pcistub_devices_lock, flags);
327 while (!list_empty(&seized_devices)) {
328 psdev = container_of(seized_devices.next,
329 struct pcistub_device, dev_list);
330 list_del(&psdev->dev_list);
332 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
334 err = pcistub_init_device(psdev->dev);
335 if (err) {
336 dev_err(&psdev->dev->dev,
337 "error %d initializing device\n", err);
338 kfree(psdev);
339 psdev = NULL;
340 }
342 spin_lock_irqsave(&pcistub_devices_lock, flags);
344 if (psdev)
345 list_add_tail(&psdev->dev_list, &pcistub_devices);
346 }
348 initialize_devices = 1;
350 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
352 return 0;
353 }
355 static int __devinit pcistub_seize(struct pci_dev *dev)
356 {
357 struct pcistub_device *psdev;
358 unsigned long flags;
359 int err = 0;
361 psdev = pcistub_device_alloc(dev);
362 if (!psdev)
363 return -ENOMEM;
365 spin_lock_irqsave(&pcistub_devices_lock, flags);
367 if (initialize_devices) {
368 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
370 /* don't want irqs disabled when calling pcistub_init_device */
371 err = pcistub_init_device(psdev->dev);
373 spin_lock_irqsave(&pcistub_devices_lock, flags);
375 if (!err)
376 list_add(&psdev->dev_list, &pcistub_devices);
377 } else {
378 dev_dbg(&dev->dev, "deferring initialization\n");
379 list_add(&psdev->dev_list, &seized_devices);
380 }
382 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
384 if (err)
385 pcistub_device_put(psdev);
387 return err;
388 }
390 static int __devinit pcistub_probe(struct pci_dev *dev,
391 const struct pci_device_id *id)
392 {
393 int err = 0;
395 dev_dbg(&dev->dev, "probing...\n");
397 if (pcistub_match(dev)) {
399 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
400 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
401 dev_err(&dev->dev, "can't export pci devices that "
402 "don't have a normal (0) or bridge (1) "
403 "header type!\n");
404 err = -ENODEV;
405 goto out;
406 }
408 dev_info(&dev->dev, "seizing device\n");
409 err = pcistub_seize(dev);
410 } else
411 /* Didn't find the device */
412 err = -ENODEV;
414 out:
415 return err;
416 }
418 static void pcistub_remove(struct pci_dev *dev)
419 {
420 struct pcistub_device *psdev, *found_psdev = NULL;
421 unsigned long flags;
423 dev_dbg(&dev->dev, "removing\n");
425 spin_lock_irqsave(&pcistub_devices_lock, flags);
427 pciback_config_quirk_release(dev);
429 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
430 if (psdev->dev == dev) {
431 found_psdev = psdev;
432 break;
433 }
434 }
436 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
438 if (found_psdev) {
439 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
440 found_psdev->pdev);
442 if (found_psdev->pdev) {
443 printk(KERN_WARNING "pciback: ****** removing device "
444 "%s while still in-use! ******\n",
445 pci_name(found_psdev->dev));
446 printk(KERN_WARNING "pciback: ****** driver domain may "
447 "still access this device's i/o resources!\n");
448 printk(KERN_WARNING "pciback: ****** shutdown driver "
449 "domain before binding device\n");
450 printk(KERN_WARNING "pciback: ****** to other drivers "
451 "or domains\n");
453 pciback_release_pci_dev(found_psdev->pdev,
454 found_psdev->dev);
455 }
457 spin_lock_irqsave(&pcistub_devices_lock, flags);
458 list_del(&found_psdev->dev_list);
459 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
461 /* the final put for releasing from the list */
462 pcistub_device_put(found_psdev);
463 }
464 }
466 static struct pci_device_id pcistub_ids[] = {
467 {
468 .vendor = PCI_ANY_ID,
469 .device = PCI_ANY_ID,
470 .subvendor = PCI_ANY_ID,
471 .subdevice = PCI_ANY_ID,
472 },
473 {0,},
474 };
476 /*
477 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
478 * for a normal device. I don't want it to be loaded automatically.
479 */
481 static struct pci_driver pciback_pci_driver = {
482 .name = "pciback",
483 .id_table = pcistub_ids,
484 .probe = pcistub_probe,
485 .remove = pcistub_remove,
486 };
488 static inline int str_to_slot(const char *buf, int *domain, int *bus,
489 int *slot, int *func)
490 {
491 int err;
493 err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
494 if (err == 4)
495 return 0;
496 else if (err < 0)
497 return -EINVAL;
499 /* try again without domain */
500 *domain = 0;
501 err = sscanf(buf, " %x:%x.%x", bus, slot, func);
502 if (err == 3)
503 return 0;
505 return -EINVAL;
506 }
508 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
509 *slot, int *func, int *reg, int *size, int *mask)
510 {
511 int err;
513 err =
514 sscanf(buf, " %04x:%02x:%02x.%1x-%08x:%1x:%08x", domain, bus, slot,
515 func, reg, size, mask);
516 if (err == 7)
517 return 0;
518 return -EINVAL;
519 }
521 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
522 {
523 struct pcistub_device_id *pci_dev_id;
524 unsigned long flags;
526 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
527 if (!pci_dev_id)
528 return -ENOMEM;
530 pci_dev_id->domain = domain;
531 pci_dev_id->bus = bus;
532 pci_dev_id->devfn = PCI_DEVFN(slot, func);
534 pr_debug("pciback: wants to seize %04x:%02x:%02x.%01x\n",
535 domain, bus, slot, func);
537 spin_lock_irqsave(&device_ids_lock, flags);
538 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
539 spin_unlock_irqrestore(&device_ids_lock, flags);
541 return 0;
542 }
544 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
545 {
546 struct pcistub_device_id *pci_dev_id, *t;
547 int devfn = PCI_DEVFN(slot, func);
548 int err = -ENOENT;
549 unsigned long flags;
551 spin_lock_irqsave(&device_ids_lock, flags);
552 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids, slot_list) {
554 if (pci_dev_id->domain == domain
555 && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
556 /* Don't break; here because it's possible the same
557 * slot could be in the list more than once
558 */
559 list_del(&pci_dev_id->slot_list);
560 kfree(pci_dev_id);
562 err = 0;
564 pr_debug("pciback: removed %04x:%02x:%02x.%01x from "
565 "seize list\n", domain, bus, slot, func);
566 }
567 }
568 spin_unlock_irqrestore(&device_ids_lock, flags);
570 return err;
571 }
573 static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
574 int size, int mask)
575 {
576 int err = 0;
577 struct pcistub_device *psdev;
578 struct pci_dev *dev;
579 struct config_field *field;
581 psdev = pcistub_device_find(domain, bus, slot, func);
582 if (!psdev || !psdev->dev) {
583 err = -ENODEV;
584 goto out;
585 }
586 dev = psdev->dev;
588 /* check for duplicate field */
589 if (pciback_field_is_dup(dev, reg))
590 goto out;
592 field = kzalloc(sizeof(*field), GFP_ATOMIC);
593 if (!field) {
594 err = -ENOMEM;
595 goto out;
596 }
598 field->offset = reg;
599 field->size = size;
600 field->mask = mask;
601 field->init = NULL;
602 field->reset = NULL;
603 field->release = NULL;
604 field->clean = pciback_config_field_free;
606 err = pciback_config_quirks_add_field(dev, field);
607 if (err)
608 kfree(field);
609 out:
610 return err;
611 }
613 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
614 size_t count)
615 {
616 int domain, bus, slot, func;
617 int err;
619 err = str_to_slot(buf, &domain, &bus, &slot, &func);
620 if (err)
621 goto out;
623 err = pcistub_device_id_add(domain, bus, slot, func);
625 out:
626 if (!err)
627 err = count;
628 return err;
629 }
631 DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
633 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
634 size_t count)
635 {
636 int domain, bus, slot, func;
637 int err;
639 err = str_to_slot(buf, &domain, &bus, &slot, &func);
640 if (err)
641 goto out;
643 err = pcistub_device_id_remove(domain, bus, slot, func);
645 out:
646 if (!err)
647 err = count;
648 return err;
649 }
651 DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
653 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
654 {
655 struct pcistub_device_id *pci_dev_id;
656 size_t count = 0;
657 unsigned long flags;
659 spin_lock_irqsave(&device_ids_lock, flags);
660 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
661 if (count >= PAGE_SIZE)
662 break;
664 count += scnprintf(buf + count, PAGE_SIZE - count,
665 "%04x:%02x:%02x.%01x\n",
666 pci_dev_id->domain, pci_dev_id->bus,
667 PCI_SLOT(pci_dev_id->devfn),
668 PCI_FUNC(pci_dev_id->devfn));
669 }
670 spin_unlock_irqrestore(&device_ids_lock, flags);
672 return count;
673 }
675 DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
677 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
678 size_t count)
679 {
680 int domain, bus, slot, func, reg, size, mask;
681 int err;
683 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
684 &mask);
685 if (err)
686 goto out;
688 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
690 out:
691 if (!err)
692 err = count;
693 return err;
694 }
696 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
697 {
698 int count = 0;
699 unsigned long flags;
700 extern struct list_head pciback_quirks;
701 struct pciback_config_quirk *quirk;
702 struct pciback_dev_data *dev_data;
703 struct config_field *field;
704 struct config_field_entry *cfg_entry;
706 spin_lock_irqsave(&device_ids_lock, flags);
707 list_for_each_entry(quirk, &pciback_quirks, quirks_list) {
708 if (count >= PAGE_SIZE)
709 goto out;
711 count += scnprintf(buf + count, PAGE_SIZE - count,
712 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
713 quirk->pdev->bus->number,
714 PCI_SLOT(quirk->pdev->devfn),
715 PCI_FUNC(quirk->pdev->devfn),
716 quirk->devid.vendor, quirk->devid.device,
717 quirk->devid.subvendor,
718 quirk->devid.subdevice);
720 dev_data = pci_get_drvdata(quirk->pdev);
722 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
723 field = cfg_entry->field;
724 if (count >= PAGE_SIZE)
725 goto out;
727 count += scnprintf(buf + count, PAGE_SIZE -
728 count, "\t\t%08x:%01x:%08x\n",
729 field->offset, field->size,
730 field->mask);
731 }
732 }
734 out:
735 spin_unlock_irqrestore(&device_ids_lock, flags);
737 return count;
738 }
740 DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show, pcistub_quirk_add);
742 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
743 size_t count)
744 {
745 int domain, bus, slot, func;
746 int err;
747 struct pcistub_device *psdev;
748 struct pciback_dev_data *dev_data;
749 err = str_to_slot(buf, &domain, &bus, &slot, &func);
750 if (err)
751 goto out;
752 psdev = pcistub_device_find(domain, bus, slot, func);
753 if (!psdev) {
754 err = -ENODEV;
755 goto out;
756 }
757 if (!psdev->dev) {
758 err = -ENODEV;
759 goto release;
760 }
761 dev_data = pci_get_drvdata(psdev->dev);
762 /* the driver data for a device should never be null at this point */
763 if (!dev_data) {
764 err = -ENXIO;
765 goto release;
766 }
767 if (!dev_data->permissive) {
768 dev_data->permissive = 1;
769 /* Let user know that what they're doing could be unsafe */
770 dev_warn(&psdev->dev->dev,
771 "enabling permissive mode configuration space accesses!\n");
772 dev_warn(&psdev->dev->dev,
773 "permissive mode is potentially unsafe!\n");
774 }
775 release:
776 pcistub_device_put(psdev);
777 out:
778 if (!err)
779 err = count;
780 return err;
781 }
783 static ssize_t permissive_show(struct device_driver *drv, char *buf)
784 {
785 struct pcistub_device *psdev;
786 struct pciback_dev_data *dev_data;
787 size_t count = 0;
788 unsigned long flags;
789 spin_lock_irqsave(&pcistub_devices_lock, flags);
790 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
791 if (count >= PAGE_SIZE)
792 break;
793 if (!psdev->dev)
794 continue;
795 dev_data = pci_get_drvdata(psdev->dev);
796 if (!dev_data || !dev_data->permissive)
797 continue;
798 count +=
799 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
800 pci_name(psdev->dev));
801 }
802 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
803 return count;
804 }
806 DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show, permissive_add);
808 static int __init pcistub_init(void)
809 {
810 int pos = 0;
811 int err = 0;
812 int domain, bus, slot, func;
813 int parsed;
815 if (pci_devs_to_hide && *pci_devs_to_hide) {
816 do {
817 parsed = 0;
819 err = sscanf(pci_devs_to_hide + pos,
820 " (%x:%x:%x.%x) %n",
821 &domain, &bus, &slot, &func, &parsed);
822 if (err != 4) {
823 domain = 0;
824 err = sscanf(pci_devs_to_hide + pos,
825 " (%x:%x.%x) %n",
826 &bus, &slot, &func, &parsed);
827 if (err != 3)
828 goto parse_error;
829 }
831 err = pcistub_device_id_add(domain, bus, slot, func);
832 if (err)
833 goto out;
835 /* if parsed<=0, we've reached the end of the string */
836 pos += parsed;
837 } while (parsed > 0 && pci_devs_to_hide[pos]);
838 }
840 /* If we're the first PCI Device Driver to register, we're the
841 * first one to get offered PCI devices as they become
842 * available (and thus we can be the first to grab them)
843 */
844 err = pci_register_driver(&pciback_pci_driver);
845 if (err < 0)
846 goto out;
848 driver_create_file(&pciback_pci_driver.driver, &driver_attr_new_slot);
849 driver_create_file(&pciback_pci_driver.driver,
850 &driver_attr_remove_slot);
851 driver_create_file(&pciback_pci_driver.driver, &driver_attr_slots);
852 driver_create_file(&pciback_pci_driver.driver, &driver_attr_quirks);
853 driver_create_file(&pciback_pci_driver.driver, &driver_attr_permissive);
855 out:
856 return err;
858 parse_error:
859 printk(KERN_ERR "pciback: Error parsing pci_devs_to_hide at \"%s\"\n",
860 pci_devs_to_hide + pos);
861 return -EINVAL;
862 }
864 #ifndef MODULE
865 /*
866 * fs_initcall happens before device_initcall
867 * so pciback *should* get called first (b/c we
868 * want to suck up any device before other drivers
869 * get a chance by being the first pci device
870 * driver to register)
871 */
872 fs_initcall(pcistub_init);
873 #endif
875 static int __init pciback_init(void)
876 {
877 int err;
879 err = pciback_config_init();
880 if (err)
881 return err;
883 #ifdef MODULE
884 err = pcistub_init();
885 if (err < 0)
886 return err;
887 #endif
889 pcistub_init_devices_late();
890 pciback_xenbus_register();
892 return 0;
893 }
895 static void __exit pciback_cleanup(void)
896 {
897 pciback_xenbus_unregister();
899 driver_remove_file(&pciback_pci_driver.driver, &driver_attr_new_slot);
900 driver_remove_file(&pciback_pci_driver.driver,
901 &driver_attr_remove_slot);
902 driver_remove_file(&pciback_pci_driver.driver, &driver_attr_slots);
903 driver_remove_file(&pciback_pci_driver.driver, &driver_attr_quirks);
904 driver_remove_file(&pciback_pci_driver.driver, &driver_attr_permissive);
906 pci_unregister_driver(&pciback_pci_driver);
907 }
909 module_init(pciback_init);
910 module_exit(pciback_cleanup);
912 MODULE_LICENSE("Dual BSD/GPL");