This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / xen / pciback / pci_stub.c
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"
16
17 static char *pci_devs_to_hide = NULL;
18 module_param_named(hide, pci_devs_to_hide, charp, 0444);
19
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);
28
29 struct pcistub_device {
30         struct kref kref;
31         struct list_head dev_list;
32         spinlock_t lock;
33
34         struct pci_dev *dev;
35         struct pciback_device *pdev;    /* non-NULL if struct pci_dev is in use */
36 };
37
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);
43
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);
49
50 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
51 {
52         struct pcistub_device *psdev;
53
54         dev_dbg(&dev->dev, "pcistub_device_alloc\n");
55
56         psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
57         if (!psdev)
58                 return NULL;
59
60         psdev->dev = pci_dev_get(dev);
61         if (!psdev->dev) {
62                 kfree(psdev);
63                 return NULL;
64         }
65
66         kref_init(&psdev->kref);
67         spin_lock_init(&psdev->lock);
68
69         return psdev;
70 }
71
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;
76
77         psdev = container_of(kref, struct pcistub_device, kref);
78
79         dev_dbg(&psdev->dev->dev, "pcistub_device_release\n");
80
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);
87
88         pci_dev_put(psdev->dev);
89
90         kfree(psdev);
91 }
92
93 static inline void pcistub_device_get(struct pcistub_device *psdev)
94 {
95         kref_get(&psdev->kref);
96 }
97
98 static inline void pcistub_device_put(struct pcistub_device *psdev)
99 {
100         kref_put(&psdev->kref, pcistub_device_release);
101 }
102
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;
108
109         spin_lock_irqsave(&pcistub_devices_lock, flags);
110
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         }
120
121         /* didn't find it */
122         psdev = NULL;
123
124       out:
125         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
126         return psdev;
127 }
128
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;
134
135         pcistub_device_get(psdev);
136
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);
143
144         if (!pci_dev)
145                 pcistub_device_put(psdev);
146
147         return pci_dev;
148 }
149
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;
157
158         spin_lock_irqsave(&pcistub_devices_lock, flags);
159
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         }
169
170         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
171         return found_dev;
172 }
173
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;
180
181         spin_lock_irqsave(&pcistub_devices_lock, flags);
182
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         }
189
190         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
191         return found_dev;
192 }
193
194 void pcistub_put_pci_dev(struct pci_dev *dev)
195 {
196         struct pcistub_device *psdev, *found_psdev = NULL;
197         unsigned long flags;
198
199         spin_lock_irqsave(&pcistub_devices_lock, flags);
200
201         list_for_each_entry(psdev, &pcistub_devices, dev_list) {
202                 if (psdev->dev == dev) {
203                         found_psdev = psdev;
204                         break;
205                 }
206         }
207
208         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
209
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);
216
217         spin_lock_irqsave(&found_psdev->lock, flags);
218         found_psdev->pdev = NULL;
219         spin_unlock_irqrestore(&found_psdev->lock, flags);
220
221         pcistub_device_put(found_psdev);
222 }
223
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
236                 /* Sometimes topmost bridge links to itself. */
237                 if (dev == dev->bus->self)
238                         break;
239         }
240
241         return 0;
242 }
243
244 static int __devinit pcistub_match(struct pci_dev *dev)
245 {
246         struct pcistub_device_id *pdev_id;
247         unsigned long flags;
248         int found = 0;
249
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)) {
253                         found = 1;
254                         break;
255                 }
256         }
257         spin_unlock_irqrestore(&device_ids_lock, flags);
258
259         return found;
260 }
261
262 static int __devinit pcistub_init_device(struct pci_dev *dev)
263 {
264         struct pciback_dev_data *dev_data;
265         int err = 0;
266
267         dev_dbg(&dev->dev, "initializing...\n");
268
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)).
273          */
274         dev_data = kzalloc(sizeof(*dev_data), GFP_ATOMIC);
275         if (!dev_data) {
276                 err = -ENOMEM;
277                 goto out;
278         }
279         pci_set_drvdata(dev, dev_data);
280
281         dev_dbg(&dev->dev, "initializing config\n");
282         err = pciback_config_init_dev(dev);
283         if (err)
284                 goto out;
285
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!)
292          */
293         dev_dbg(&dev->dev, "enabling device\n");
294         err = pci_enable_device(dev);
295         if (err)
296                 goto config_release;
297
298         /* Now disable the device (this also ensures some private device
299          * data is setup before we export)
300          */
301         dev_dbg(&dev->dev, "reset device\n");
302         pciback_reset_device(dev);
303
304         return 0;
305
306       config_release:
307         pciback_config_free_dev(dev);
308
309       out:
310         pci_set_drvdata(dev, NULL);
311         kfree(dev_data);
312         return err;
313 }
314
315 /*
316  * Because some initialization still happens on
317  * devices during fs_initcall, we need to defer
318  * full initialization of our devices until
319  * device_initcall.
320  */
321 static int __init pcistub_init_devices_late(void)
322 {
323         struct pcistub_device *psdev;
324         unsigned long flags;
325         int err = 0;
326
327         pr_debug("pciback: pcistub_init_devices_late\n");
328
329         spin_lock_irqsave(&pcistub_devices_lock, flags);
330
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);
335
336                 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
337
338                 err = pcistub_init_device(psdev->dev);
339                 if (err) {
340                         dev_err(&psdev->dev->dev,
341                                 "error %d initializing device\n", err);
342                         kfree(psdev);
343                         psdev = NULL;
344                 }
345
346                 spin_lock_irqsave(&pcistub_devices_lock, flags);
347
348                 if (psdev)
349                         list_add_tail(&psdev->dev_list, &pcistub_devices);
350         }
351
352         initialize_devices = 1;
353
354         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
355
356         return 0;
357 }
358
359 static int __devinit pcistub_seize(struct pci_dev *dev)
360 {
361         struct pcistub_device *psdev;
362         unsigned long flags;
363         int err = 0;
364
365         psdev = pcistub_device_alloc(dev);
366         if (!psdev)
367                 return -ENOMEM;
368
369         spin_lock_irqsave(&pcistub_devices_lock, flags);
370
371         if (initialize_devices) {
372                 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
373
374                 /* don't want irqs disabled when calling pcistub_init_device */
375                 err = pcistub_init_device(psdev->dev);
376
377                 spin_lock_irqsave(&pcistub_devices_lock, flags);
378
379                 if (!err)
380                         list_add(&psdev->dev_list, &pcistub_devices);
381         } else {
382                 dev_dbg(&dev->dev, "deferring initialization\n");
383                 list_add(&psdev->dev_list, &seized_devices);
384         }
385
386         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
387
388         if (err)
389                 pcistub_device_put(psdev);
390
391         return err;
392 }
393
394 static int __devinit pcistub_probe(struct pci_dev *dev,
395                                    const struct pci_device_id *id)
396 {
397         int err = 0;
398
399         dev_dbg(&dev->dev, "probing...\n");
400
401         if (pcistub_match(dev)) {
402
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) "
407                                 "header type!\n");
408                         err = -ENODEV;
409                         goto out;
410                 }
411
412                 dev_info(&dev->dev, "seizing device\n");
413                 err = pcistub_seize(dev);
414         } else
415                 /* Didn't find the device */
416                 err = -ENODEV;
417
418       out:
419         return err;
420 }
421
422 static void pcistub_remove(struct pci_dev *dev)
423 {
424         struct pcistub_device *psdev, *found_psdev = NULL;
425         unsigned long flags;
426
427         dev_dbg(&dev->dev, "removing\n");
428
429         spin_lock_irqsave(&pcistub_devices_lock, flags);
430
431         pciback_config_quirk_release(dev);
432
433         list_for_each_entry(psdev, &pcistub_devices, dev_list) {
434                 if (psdev->dev == dev) {
435                         found_psdev = psdev;
436                         break;
437                 }
438         }
439
440         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
441
442         if (found_psdev) {
443                 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
444                         found_psdev->pdev);
445
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 "
455                                "or domains\n");
456
457                         pciback_release_pci_dev(found_psdev->pdev,
458                                                 found_psdev->dev);
459                 }
460
461                 spin_lock_irqsave(&pcistub_devices_lock, flags);
462                 list_del(&found_psdev->dev_list);
463                 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
464
465                 /* the final put for releasing from the list */
466                 pcistub_device_put(found_psdev);
467         }
468 }
469
470 static struct pci_device_id pcistub_ids[] = {
471         {
472          .vendor = PCI_ANY_ID,
473          .device = PCI_ANY_ID,
474          .subvendor = PCI_ANY_ID,
475          .subdevice = PCI_ANY_ID,
476          },
477         {0,},
478 };
479
480 /*
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.
483  */
484
485 static struct pci_driver pciback_pci_driver = {
486         .name = "pciback",
487         .id_table = pcistub_ids,
488         .probe = pcistub_probe,
489         .remove = pcistub_remove,
490 };
491
492 static inline int str_to_slot(const char *buf, int *domain, int *bus,
493                               int *slot, int *func)
494 {
495         int err;
496
497         err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
498         if (err == 4)
499                 return 0;
500         else if (err < 0)
501                 return -EINVAL;
502
503         /* try again without domain */
504         *domain = 0;
505         err = sscanf(buf, " %x:%x.%x", bus, slot, func);
506         if (err == 3)
507                 return 0;
508
509         return -EINVAL;
510 }
511
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)
514 {
515         int err;
516
517         err =
518             sscanf(buf, " %04x:%02x:%02x.%1x-%08x:%1x:%08x", domain, bus, slot,
519                    func, reg, size, mask);
520         if (err == 7)
521                 return 0;
522         return -EINVAL;
523 }
524
525 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
526 {
527         struct pcistub_device_id *pci_dev_id;
528         unsigned long flags;
529
530         pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
531         if (!pci_dev_id)
532                 return -ENOMEM;
533
534         pci_dev_id->domain = domain;
535         pci_dev_id->bus = bus;
536         pci_dev_id->devfn = PCI_DEVFN(slot, func);
537
538         pr_debug("pciback: wants to seize %04x:%02x:%02x.%01x\n",
539                  domain, bus, slot, func);
540
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);
544
545         return 0;
546 }
547
548 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
549 {
550         struct pcistub_device_id *pci_dev_id, *t;
551         int devfn = PCI_DEVFN(slot, func);
552         int err = -ENOENT;
553         unsigned long flags;
554
555         spin_lock_irqsave(&device_ids_lock, flags);
556         list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids, slot_list) {
557
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
562                          */
563                         list_del(&pci_dev_id->slot_list);
564                         kfree(pci_dev_id);
565
566                         err = 0;
567
568                         pr_debug("pciback: removed %04x:%02x:%02x.%01x from "
569                                  "seize list\n", domain, bus, slot, func);
570                 }
571         }
572         spin_unlock_irqrestore(&device_ids_lock, flags);
573
574         return err;
575 }
576
577 static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
578                            int size, int mask)
579 {
580         int err = 0;
581         struct pcistub_device *psdev;
582         struct pci_dev *dev;
583         struct config_field *field;
584
585         psdev = pcistub_device_find(domain, bus, slot, func);
586         if (!psdev || !psdev->dev) {
587                 err = -ENODEV;
588                 goto out;
589         }
590         dev = psdev->dev;
591
592         /* check for duplicate field */
593         if (pciback_field_is_dup(dev, reg))
594                 goto out;
595
596         field = kzalloc(sizeof(*field), GFP_ATOMIC);
597         if (!field) {
598                 err = -ENOMEM;
599                 goto out;
600         }
601
602         field->offset = reg;
603         field->size = size;
604         field->mask = mask;
605         field->init = NULL;
606         field->reset = NULL;
607         field->release = NULL;
608         field->clean = pciback_config_field_free;
609
610         err = pciback_config_quirks_add_field(dev, field);
611         if (err)
612                 kfree(field);
613       out:
614         return err;
615 }
616
617 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
618                                 size_t count)
619 {
620         int domain, bus, slot, func;
621         int err;
622
623         err = str_to_slot(buf, &domain, &bus, &slot, &func);
624         if (err)
625                 goto out;
626
627         err = pcistub_device_id_add(domain, bus, slot, func);
628
629       out:
630         if (!err)
631                 err = count;
632         return err;
633 }
634
635 DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
636
637 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
638                                    size_t count)
639 {
640         int domain, bus, slot, func;
641         int err;
642
643         err = str_to_slot(buf, &domain, &bus, &slot, &func);
644         if (err)
645                 goto out;
646
647         err = pcistub_device_id_remove(domain, bus, slot, func);
648
649       out:
650         if (!err)
651                 err = count;
652         return err;
653 }
654
655 DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
656
657 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
658 {
659         struct pcistub_device_id *pci_dev_id;
660         size_t count = 0;
661         unsigned long flags;
662
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)
666                         break;
667
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));
673         }
674         spin_unlock_irqrestore(&device_ids_lock, flags);
675
676         return count;
677 }
678
679 DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
680
681 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
682                                  size_t count)
683 {
684         int domain, bus, slot, func, reg, size, mask;
685         int err;
686
687         err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
688                            &mask);
689         if (err)
690                 goto out;
691
692         err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
693
694       out:
695         if (!err)
696                 err = count;
697         return err;
698 }
699
700 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
701 {
702         int count = 0;
703         unsigned long flags;
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;
709
710         spin_lock_irqsave(&device_ids_lock, flags);
711         list_for_each_entry(quirk, &pciback_quirks, quirks_list) {
712                 if (count >= PAGE_SIZE)
713                         goto out;
714
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);
723
724                 dev_data = pci_get_drvdata(quirk->pdev);
725
726                 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
727                         field = cfg_entry->field;
728                         if (count >= PAGE_SIZE)
729                                 goto out;
730
731                         count += scnprintf(buf + count, PAGE_SIZE -
732                                            count, "\t\t%08x:%01x:%08x\n",
733                                            field->offset, field->size,
734                                            field->mask);
735                 }
736         }
737
738       out:
739         spin_unlock_irqrestore(&device_ids_lock, flags);
740
741         return count;
742 }
743
744 DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show, pcistub_quirk_add);
745
746 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
747                               size_t count)
748 {
749         int domain, bus, slot, func;
750         int err;
751         struct pcistub_device *psdev;
752         struct pciback_dev_data *dev_data;
753         err = str_to_slot(buf, &domain, &bus, &slot, &func);
754         if (err)
755                 goto out;
756         psdev = pcistub_device_find(domain, bus, slot, func);
757         if (!psdev) {
758                 err = -ENODEV;
759                 goto out;
760         }
761         if (!psdev->dev) {
762                 err = -ENODEV;
763                 goto release;
764         }
765         dev_data = pci_get_drvdata(psdev->dev);
766         /* the driver data for a device should never be null at this point */
767         if (!dev_data) {
768                 err = -ENXIO;
769                 goto release;
770         }
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");
778         }
779       release:
780         pcistub_device_put(psdev);
781       out:
782         if (!err)
783                 err = count;
784         return err;
785 }
786
787 static ssize_t permissive_show(struct device_driver *drv, char *buf)
788 {
789         struct pcistub_device *psdev;
790         struct pciback_dev_data *dev_data;
791         size_t count = 0;
792         unsigned long flags;
793         spin_lock_irqsave(&pcistub_devices_lock, flags);
794         list_for_each_entry(psdev, &pcistub_devices, dev_list) {
795                 if (count >= PAGE_SIZE)
796                         break;
797                 if (!psdev->dev)
798                         continue;
799                 dev_data = pci_get_drvdata(psdev->dev);
800                 if (!dev_data || !dev_data->permissive)
801                         continue;
802                 count +=
803                     scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
804                               pci_name(psdev->dev));
805         }
806         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
807         return count;
808 }
809
810 DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show, permissive_add);
811
812 static int __init pcistub_init(void)
813 {
814         int pos = 0;
815         int err = 0;
816         int domain, bus, slot, func;
817         int parsed;
818
819         if (pci_devs_to_hide && *pci_devs_to_hide) {
820                 do {
821                         parsed = 0;
822
823                         err = sscanf(pci_devs_to_hide + pos,
824                                      " (%x:%x:%x.%x) %n",
825                                      &domain, &bus, &slot, &func, &parsed);
826                         if (err != 4) {
827                                 domain = 0;
828                                 err = sscanf(pci_devs_to_hide + pos,
829                                              " (%x:%x.%x) %n",
830                                              &bus, &slot, &func, &parsed);
831                                 if (err != 3)
832                                         goto parse_error;
833                         }
834
835                         err = pcistub_device_id_add(domain, bus, slot, func);
836                         if (err)
837                                 goto out;
838
839                         /* if parsed<=0, we've reached the end of the string */
840                         pos += parsed;
841                 } while (parsed > 0 && pci_devs_to_hide[pos]);
842         }
843
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)
847          */
848         err = pci_register_driver(&pciback_pci_driver);
849         if (err < 0)
850                 goto out;
851
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);
858
859       out:
860         return err;
861
862       parse_error:
863         printk(KERN_ERR "pciback: Error parsing pci_devs_to_hide at \"%s\"\n",
864                pci_devs_to_hide + pos);
865         return -EINVAL;
866 }
867
868 #ifndef MODULE
869 /*
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)
875  */
876 fs_initcall(pcistub_init);
877 #endif
878
879 static int __init pciback_init(void)
880 {
881         int err;
882
883         err = pciback_config_init();
884         if (err)
885                 return err;
886
887 #ifdef MODULE
888         err = pcistub_init();
889         if (err < 0)
890                 return err;
891 #endif
892
893         pcistub_init_devices_late();
894         pciback_xenbus_register();
895
896         return 0;
897 }
898
899 static void __exit pciback_cleanup(void)
900 {
901         pciback_xenbus_unregister();
902
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);
909
910         pci_unregister_driver(&pciback_pci_driver);
911 }
912
913 module_init(pciback_init);
914 module_exit(pciback_cleanup);
915
916 MODULE_LICENSE("Dual BSD/GPL");