xen/pciback: Don't deadlock when unbinding.
[deliverable/linux.git] / drivers / xen / 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
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/rwsem.h>
13 #include <linux/list.h>
14 #include <linux/spinlock.h>
15 #include <linux/kref.h>
16 #include <linux/pci.h>
17 #include <linux/wait.h>
18 #include <linux/sched.h>
19 #include <linux/atomic.h>
20 #include <xen/events.h>
21 #include <asm/xen/pci.h>
22 #include <asm/xen/hypervisor.h>
23 #include <xen/interface/physdev.h>
24 #include "pciback.h"
25 #include "conf_space.h"
26 #include "conf_space_quirks.h"
27
28 static char *pci_devs_to_hide;
29 wait_queue_head_t xen_pcibk_aer_wait_queue;
30 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
31 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
32 */
33 static DECLARE_RWSEM(pcistub_sem);
34 module_param_named(hide, pci_devs_to_hide, charp, 0444);
35
36 struct pcistub_device_id {
37 struct list_head slot_list;
38 int domain;
39 unsigned char bus;
40 unsigned int devfn;
41 };
42 static LIST_HEAD(pcistub_device_ids);
43 static DEFINE_SPINLOCK(device_ids_lock);
44
45 struct pcistub_device {
46 struct kref kref;
47 struct list_head dev_list;
48 spinlock_t lock;
49
50 struct pci_dev *dev;
51 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
52 };
53
54 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
55 * flag must be locked with pcistub_devices_lock
56 */
57 static DEFINE_SPINLOCK(pcistub_devices_lock);
58 static LIST_HEAD(pcistub_devices);
59
60 /* wait for device_initcall before initializing our devices
61 * (see pcistub_init_devices_late)
62 */
63 static int initialize_devices;
64 static LIST_HEAD(seized_devices);
65
66 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
67 {
68 struct pcistub_device *psdev;
69
70 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
71
72 psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
73 if (!psdev)
74 return NULL;
75
76 psdev->dev = pci_dev_get(dev);
77 if (!psdev->dev) {
78 kfree(psdev);
79 return NULL;
80 }
81
82 kref_init(&psdev->kref);
83 spin_lock_init(&psdev->lock);
84
85 return psdev;
86 }
87
88 /* Don't call this directly as it's called by pcistub_device_put */
89 static void pcistub_device_release(struct kref *kref)
90 {
91 struct pcistub_device *psdev;
92 struct pci_dev *dev;
93 struct xen_pcibk_dev_data *dev_data;
94
95 psdev = container_of(kref, struct pcistub_device, kref);
96 dev = psdev->dev;
97 dev_data = pci_get_drvdata(dev);
98
99 dev_dbg(&dev->dev, "pcistub_device_release\n");
100
101 xen_unregister_device_domain_owner(dev);
102
103 /* Call the reset function which does not take lock as this
104 * is called from "unbind" which takes a device_lock mutex.
105 */
106 __pci_reset_function_locked(dev);
107 if (pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
108 dev_dbg(&dev->dev, "Could not reload PCI state\n");
109 else
110 pci_restore_state(dev);
111
112 if (dev->msix_cap) {
113 struct physdev_pci_device ppdev = {
114 .seg = pci_domain_nr(dev->bus),
115 .bus = dev->bus->number,
116 .devfn = dev->devfn
117 };
118 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
119 &ppdev);
120
121 if (err)
122 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
123 err);
124 }
125
126 /* Disable the device */
127 xen_pcibk_reset_device(dev);
128
129 kfree(dev_data);
130 pci_set_drvdata(dev, NULL);
131
132 /* Clean-up the device */
133 xen_pcibk_config_free_dyn_fields(dev);
134 xen_pcibk_config_free_dev(dev);
135
136 pci_clear_dev_assigned(dev);
137 pci_dev_put(dev);
138
139 kfree(psdev);
140 }
141
142 static inline void pcistub_device_get(struct pcistub_device *psdev)
143 {
144 kref_get(&psdev->kref);
145 }
146
147 static inline void pcistub_device_put(struct pcistub_device *psdev)
148 {
149 kref_put(&psdev->kref, pcistub_device_release);
150 }
151
152 static struct pcistub_device *pcistub_device_find(int domain, int bus,
153 int slot, int func)
154 {
155 struct pcistub_device *psdev = 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 && slot == PCI_SLOT(psdev->dev->devfn)
165 && func == PCI_FUNC(psdev->dev->devfn)) {
166 pcistub_device_get(psdev);
167 goto out;
168 }
169 }
170
171 /* didn't find it */
172 psdev = NULL;
173
174 out:
175 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
176 return psdev;
177 }
178
179 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
180 struct pcistub_device *psdev)
181 {
182 struct pci_dev *pci_dev = NULL;
183 unsigned long flags;
184
185 pcistub_device_get(psdev);
186
187 spin_lock_irqsave(&psdev->lock, flags);
188 if (!psdev->pdev) {
189 psdev->pdev = pdev;
190 pci_dev = psdev->dev;
191 }
192 spin_unlock_irqrestore(&psdev->lock, flags);
193
194 if (!pci_dev)
195 pcistub_device_put(psdev);
196
197 return pci_dev;
198 }
199
200 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
201 int domain, int bus,
202 int slot, int func)
203 {
204 struct pcistub_device *psdev;
205 struct pci_dev *found_dev = NULL;
206 unsigned long flags;
207
208 spin_lock_irqsave(&pcistub_devices_lock, flags);
209
210 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
211 if (psdev->dev != NULL
212 && domain == pci_domain_nr(psdev->dev->bus)
213 && bus == psdev->dev->bus->number
214 && slot == PCI_SLOT(psdev->dev->devfn)
215 && func == PCI_FUNC(psdev->dev->devfn)) {
216 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
217 break;
218 }
219 }
220
221 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
222 return found_dev;
223 }
224
225 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
226 struct pci_dev *dev)
227 {
228 struct pcistub_device *psdev;
229 struct pci_dev *found_dev = NULL;
230 unsigned long flags;
231
232 spin_lock_irqsave(&pcistub_devices_lock, flags);
233
234 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
235 if (psdev->dev == dev) {
236 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
237 break;
238 }
239 }
240
241 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
242 return found_dev;
243 }
244
245 /*
246 * Called when:
247 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
248 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
249 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
250 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
251 *
252 * As such we have to be careful.
253 *
254 * To make this easier, the caller has to hold the device lock.
255 */
256 void pcistub_put_pci_dev(struct pci_dev *dev)
257 {
258 struct pcistub_device *psdev, *found_psdev = NULL;
259 unsigned long flags;
260
261 spin_lock_irqsave(&pcistub_devices_lock, flags);
262
263 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
264 if (psdev->dev == dev) {
265 found_psdev = psdev;
266 break;
267 }
268 }
269
270 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
271 if (WARN_ON(!found_psdev))
272 return;
273
274 /*hold this lock for avoiding breaking link between
275 * pcistub and xen_pcibk when AER is in processing
276 */
277 down_write(&pcistub_sem);
278 /* Cleanup our device
279 * (so it's ready for the next domain)
280 */
281 lockdep_assert_held(&dev->dev.mutex);
282 __pci_reset_function_locked(dev);
283 pci_restore_state(dev);
284
285 /* This disables the device. */
286 xen_pcibk_reset_device(dev);
287
288 /* And cleanup up our emulated fields. */
289 xen_pcibk_config_reset_dev(dev);
290 xen_pcibk_config_free_dyn_fields(dev);
291
292 xen_unregister_device_domain_owner(dev);
293
294 spin_lock_irqsave(&found_psdev->lock, flags);
295 found_psdev->pdev = NULL;
296 spin_unlock_irqrestore(&found_psdev->lock, flags);
297
298 pcistub_device_put(found_psdev);
299 up_write(&pcistub_sem);
300 }
301
302 static int pcistub_match_one(struct pci_dev *dev,
303 struct pcistub_device_id *pdev_id)
304 {
305 /* Match the specified device by domain, bus, slot, func and also if
306 * any of the device's parent bridges match.
307 */
308 for (; dev != NULL; dev = dev->bus->self) {
309 if (pci_domain_nr(dev->bus) == pdev_id->domain
310 && dev->bus->number == pdev_id->bus
311 && dev->devfn == pdev_id->devfn)
312 return 1;
313
314 /* Sometimes topmost bridge links to itself. */
315 if (dev == dev->bus->self)
316 break;
317 }
318
319 return 0;
320 }
321
322 static int pcistub_match(struct pci_dev *dev)
323 {
324 struct pcistub_device_id *pdev_id;
325 unsigned long flags;
326 int found = 0;
327
328 spin_lock_irqsave(&device_ids_lock, flags);
329 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
330 if (pcistub_match_one(dev, pdev_id)) {
331 found = 1;
332 break;
333 }
334 }
335 spin_unlock_irqrestore(&device_ids_lock, flags);
336
337 return found;
338 }
339
340 static int pcistub_init_device(struct pci_dev *dev)
341 {
342 struct xen_pcibk_dev_data *dev_data;
343 int err = 0;
344
345 dev_dbg(&dev->dev, "initializing...\n");
346
347 /* The PCI backend is not intended to be a module (or to work with
348 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
349 * would need to be called somewhere to free the memory allocated
350 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
351 */
352 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
353 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
354 if (!dev_data) {
355 err = -ENOMEM;
356 goto out;
357 }
358 pci_set_drvdata(dev, dev_data);
359
360 /*
361 * Setup name for fake IRQ handler. It will only be enabled
362 * once the device is turned on by the guest.
363 */
364 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
365
366 dev_dbg(&dev->dev, "initializing config\n");
367
368 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
369 err = xen_pcibk_config_init_dev(dev);
370 if (err)
371 goto out;
372
373 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
374 * must do this here because pcibios_enable_device may specify
375 * the pci device's true irq (and possibly its other resources)
376 * if they differ from what's in the configuration space.
377 * This makes the assumption that the device's resources won't
378 * change after this point (otherwise this code may break!)
379 */
380 dev_dbg(&dev->dev, "enabling device\n");
381 err = pci_enable_device(dev);
382 if (err)
383 goto config_release;
384
385 if (dev->msix_cap) {
386 struct physdev_pci_device ppdev = {
387 .seg = pci_domain_nr(dev->bus),
388 .bus = dev->bus->number,
389 .devfn = dev->devfn
390 };
391
392 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
393 if (err)
394 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
395 err);
396 }
397
398 /* We need the device active to save the state. */
399 dev_dbg(&dev->dev, "save state of device\n");
400 pci_save_state(dev);
401 dev_data->pci_saved_state = pci_store_saved_state(dev);
402 if (!dev_data->pci_saved_state)
403 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
404 else {
405 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
406 __pci_reset_function_locked(dev);
407 pci_restore_state(dev);
408 }
409 /* Now disable the device (this also ensures some private device
410 * data is setup before we export)
411 */
412 dev_dbg(&dev->dev, "reset device\n");
413 xen_pcibk_reset_device(dev);
414
415 pci_set_dev_assigned(dev);
416 return 0;
417
418 config_release:
419 xen_pcibk_config_free_dev(dev);
420
421 out:
422 pci_set_drvdata(dev, NULL);
423 kfree(dev_data);
424 return err;
425 }
426
427 /*
428 * Because some initialization still happens on
429 * devices during fs_initcall, we need to defer
430 * full initialization of our devices until
431 * device_initcall.
432 */
433 static int __init pcistub_init_devices_late(void)
434 {
435 struct pcistub_device *psdev;
436 unsigned long flags;
437 int err = 0;
438
439 spin_lock_irqsave(&pcistub_devices_lock, flags);
440
441 while (!list_empty(&seized_devices)) {
442 psdev = container_of(seized_devices.next,
443 struct pcistub_device, dev_list);
444 list_del(&psdev->dev_list);
445
446 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
447
448 err = pcistub_init_device(psdev->dev);
449 if (err) {
450 dev_err(&psdev->dev->dev,
451 "error %d initializing device\n", err);
452 kfree(psdev);
453 psdev = NULL;
454 }
455
456 spin_lock_irqsave(&pcistub_devices_lock, flags);
457
458 if (psdev)
459 list_add_tail(&psdev->dev_list, &pcistub_devices);
460 }
461
462 initialize_devices = 1;
463
464 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
465
466 return 0;
467 }
468
469 static int pcistub_seize(struct pci_dev *dev)
470 {
471 struct pcistub_device *psdev;
472 unsigned long flags;
473 int err = 0;
474
475 psdev = pcistub_device_alloc(dev);
476 if (!psdev)
477 return -ENOMEM;
478
479 spin_lock_irqsave(&pcistub_devices_lock, flags);
480
481 if (initialize_devices) {
482 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
483
484 /* don't want irqs disabled when calling pcistub_init_device */
485 err = pcistub_init_device(psdev->dev);
486
487 spin_lock_irqsave(&pcistub_devices_lock, flags);
488
489 if (!err)
490 list_add(&psdev->dev_list, &pcistub_devices);
491 } else {
492 dev_dbg(&dev->dev, "deferring initialization\n");
493 list_add(&psdev->dev_list, &seized_devices);
494 }
495
496 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
497
498 if (err)
499 pcistub_device_put(psdev);
500
501 return err;
502 }
503
504 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
505 * other functions that take the sysfs lock. */
506 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
507 {
508 int err = 0;
509
510 dev_dbg(&dev->dev, "probing...\n");
511
512 if (pcistub_match(dev)) {
513
514 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
515 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
516 dev_err(&dev->dev, "can't export pci devices that "
517 "don't have a normal (0) or bridge (1) "
518 "header type!\n");
519 err = -ENODEV;
520 goto out;
521 }
522
523 dev_info(&dev->dev, "seizing device\n");
524 err = pcistub_seize(dev);
525 } else
526 /* Didn't find the device */
527 err = -ENODEV;
528
529 out:
530 return err;
531 }
532
533 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
534 * other functions that take the sysfs lock. */
535 static void pcistub_remove(struct pci_dev *dev)
536 {
537 struct pcistub_device *psdev, *found_psdev = NULL;
538 unsigned long flags;
539
540 dev_dbg(&dev->dev, "removing\n");
541
542 spin_lock_irqsave(&pcistub_devices_lock, flags);
543
544 xen_pcibk_config_quirk_release(dev);
545
546 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
547 if (psdev->dev == dev) {
548 found_psdev = psdev;
549 break;
550 }
551 }
552
553 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
554
555 if (found_psdev) {
556 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
557 found_psdev->pdev);
558
559 if (found_psdev->pdev) {
560 pr_warn("****** removing device %s while still in-use! ******\n",
561 pci_name(found_psdev->dev));
562 pr_warn("****** driver domain may still access this device's i/o resources!\n");
563 pr_warn("****** shutdown driver domain before binding device\n");
564 pr_warn("****** to other drivers or domains\n");
565
566 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
567 * doing the FLR. */
568 xen_pcibk_release_pci_dev(found_psdev->pdev,
569 found_psdev->dev,
570 false /* caller holds the lock. */);
571 }
572
573 spin_lock_irqsave(&pcistub_devices_lock, flags);
574 list_del(&found_psdev->dev_list);
575 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
576
577 /* the final put for releasing from the list */
578 pcistub_device_put(found_psdev);
579 }
580 }
581
582 static const struct pci_device_id pcistub_ids[] = {
583 {
584 .vendor = PCI_ANY_ID,
585 .device = PCI_ANY_ID,
586 .subvendor = PCI_ANY_ID,
587 .subdevice = PCI_ANY_ID,
588 },
589 {0,},
590 };
591
592 #define PCI_NODENAME_MAX 40
593 static void kill_domain_by_device(struct pcistub_device *psdev)
594 {
595 struct xenbus_transaction xbt;
596 int err;
597 char nodename[PCI_NODENAME_MAX];
598
599 BUG_ON(!psdev);
600 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
601 psdev->pdev->xdev->otherend_id);
602
603 again:
604 err = xenbus_transaction_start(&xbt);
605 if (err) {
606 dev_err(&psdev->dev->dev,
607 "error %d when start xenbus transaction\n", err);
608 return;
609 }
610 /*PV AER handlers will set this flag*/
611 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
612 err = xenbus_transaction_end(xbt, 0);
613 if (err) {
614 if (err == -EAGAIN)
615 goto again;
616 dev_err(&psdev->dev->dev,
617 "error %d when end xenbus transaction\n", err);
618 return;
619 }
620 }
621
622 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
623 * backend need to have cooperation. In xen_pcibk, those steps will do similar
624 * jobs: send service request and waiting for front_end response.
625 */
626 static pci_ers_result_t common_process(struct pcistub_device *psdev,
627 pci_channel_state_t state, int aer_cmd,
628 pci_ers_result_t result)
629 {
630 pci_ers_result_t res = result;
631 struct xen_pcie_aer_op *aer_op;
632 int ret;
633
634 /*with PV AER drivers*/
635 aer_op = &(psdev->pdev->sh_info->aer_op);
636 aer_op->cmd = aer_cmd ;
637 /*useful for error_detected callback*/
638 aer_op->err = state;
639 /*pcifront_end BDF*/
640 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
641 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
642 if (!ret) {
643 dev_err(&psdev->dev->dev,
644 DRV_NAME ": failed to get pcifront device\n");
645 return PCI_ERS_RESULT_NONE;
646 }
647 wmb();
648
649 dev_dbg(&psdev->dev->dev,
650 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
651 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
652 /*local flag to mark there's aer request, xen_pcibk callback will use
653 * this flag to judge whether we need to check pci-front give aer
654 * service ack signal
655 */
656 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
657
658 /*It is possible that a pcifront conf_read_write ops request invokes
659 * the callback which cause the spurious execution of wake_up.
660 * Yet it is harmless and better than a spinlock here
661 */
662 set_bit(_XEN_PCIB_active,
663 (unsigned long *)&psdev->pdev->sh_info->flags);
664 wmb();
665 notify_remote_via_irq(psdev->pdev->evtchn_irq);
666
667 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
668 !(test_bit(_XEN_PCIB_active, (unsigned long *)
669 &psdev->pdev->sh_info->flags)), 300*HZ);
670
671 if (!ret) {
672 if (test_bit(_XEN_PCIB_active,
673 (unsigned long *)&psdev->pdev->sh_info->flags)) {
674 dev_err(&psdev->dev->dev,
675 "pcifront aer process not responding!\n");
676 clear_bit(_XEN_PCIB_active,
677 (unsigned long *)&psdev->pdev->sh_info->flags);
678 aer_op->err = PCI_ERS_RESULT_NONE;
679 return res;
680 }
681 }
682 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
683
684 if (test_bit(_XEN_PCIF_active,
685 (unsigned long *)&psdev->pdev->sh_info->flags)) {
686 dev_dbg(&psdev->dev->dev,
687 "schedule pci_conf service in " DRV_NAME "\n");
688 xen_pcibk_test_and_schedule_op(psdev->pdev);
689 }
690
691 res = (pci_ers_result_t)aer_op->err;
692 return res;
693 }
694
695 /*
696 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
697 * of the device driver could provide this service, and then wait for pcifront
698 * ack.
699 * @dev: pointer to PCI devices
700 * return value is used by aer_core do_recovery policy
701 */
702 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
703 {
704 struct pcistub_device *psdev;
705 pci_ers_result_t result;
706
707 result = PCI_ERS_RESULT_RECOVERED;
708 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
709 dev->bus->number, dev->devfn);
710
711 down_write(&pcistub_sem);
712 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
713 dev->bus->number,
714 PCI_SLOT(dev->devfn),
715 PCI_FUNC(dev->devfn));
716
717 if (!psdev || !psdev->pdev) {
718 dev_err(&dev->dev,
719 DRV_NAME " device is not found/assigned\n");
720 goto end;
721 }
722
723 if (!psdev->pdev->sh_info) {
724 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
725 " by HVM, kill it\n");
726 kill_domain_by_device(psdev);
727 goto end;
728 }
729
730 if (!test_bit(_XEN_PCIB_AERHANDLER,
731 (unsigned long *)&psdev->pdev->sh_info->flags)) {
732 dev_err(&dev->dev,
733 "guest with no AER driver should have been killed\n");
734 goto end;
735 }
736 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
737
738 if (result == PCI_ERS_RESULT_NONE ||
739 result == PCI_ERS_RESULT_DISCONNECT) {
740 dev_dbg(&dev->dev,
741 "No AER slot_reset service or disconnected!\n");
742 kill_domain_by_device(psdev);
743 }
744 end:
745 if (psdev)
746 pcistub_device_put(psdev);
747 up_write(&pcistub_sem);
748 return result;
749
750 }
751
752
753 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
754 * in case of the device driver could provide this service, and then wait
755 * for pcifront ack
756 * @dev: pointer to PCI devices
757 * return value is used by aer_core do_recovery policy
758 */
759
760 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
761 {
762 struct pcistub_device *psdev;
763 pci_ers_result_t result;
764
765 result = PCI_ERS_RESULT_RECOVERED;
766 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
767 dev->bus->number, dev->devfn);
768
769 down_write(&pcistub_sem);
770 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
771 dev->bus->number,
772 PCI_SLOT(dev->devfn),
773 PCI_FUNC(dev->devfn));
774
775 if (!psdev || !psdev->pdev) {
776 dev_err(&dev->dev,
777 DRV_NAME " device is not found/assigned\n");
778 goto end;
779 }
780
781 if (!psdev->pdev->sh_info) {
782 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
783 " by HVM, kill it\n");
784 kill_domain_by_device(psdev);
785 goto end;
786 }
787
788 if (!test_bit(_XEN_PCIB_AERHANDLER,
789 (unsigned long *)&psdev->pdev->sh_info->flags)) {
790 dev_err(&dev->dev,
791 "guest with no AER driver should have been killed\n");
792 goto end;
793 }
794 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
795
796 if (result == PCI_ERS_RESULT_NONE ||
797 result == PCI_ERS_RESULT_DISCONNECT) {
798 dev_dbg(&dev->dev,
799 "No AER mmio_enabled service or disconnected!\n");
800 kill_domain_by_device(psdev);
801 }
802 end:
803 if (psdev)
804 pcistub_device_put(psdev);
805 up_write(&pcistub_sem);
806 return result;
807 }
808
809 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
810 * in case of the device driver could provide this service, and then wait
811 * for pcifront ack.
812 * @dev: pointer to PCI devices
813 * @error: the current PCI connection state
814 * return value is used by aer_core do_recovery policy
815 */
816
817 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
818 pci_channel_state_t error)
819 {
820 struct pcistub_device *psdev;
821 pci_ers_result_t result;
822
823 result = PCI_ERS_RESULT_CAN_RECOVER;
824 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
825 dev->bus->number, dev->devfn);
826
827 down_write(&pcistub_sem);
828 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
829 dev->bus->number,
830 PCI_SLOT(dev->devfn),
831 PCI_FUNC(dev->devfn));
832
833 if (!psdev || !psdev->pdev) {
834 dev_err(&dev->dev,
835 DRV_NAME " device is not found/assigned\n");
836 goto end;
837 }
838
839 if (!psdev->pdev->sh_info) {
840 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
841 " by HVM, kill it\n");
842 kill_domain_by_device(psdev);
843 goto end;
844 }
845
846 /*Guest owns the device yet no aer handler regiested, kill guest*/
847 if (!test_bit(_XEN_PCIB_AERHANDLER,
848 (unsigned long *)&psdev->pdev->sh_info->flags)) {
849 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
850 kill_domain_by_device(psdev);
851 goto end;
852 }
853 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
854
855 if (result == PCI_ERS_RESULT_NONE ||
856 result == PCI_ERS_RESULT_DISCONNECT) {
857 dev_dbg(&dev->dev,
858 "No AER error_detected service or disconnected!\n");
859 kill_domain_by_device(psdev);
860 }
861 end:
862 if (psdev)
863 pcistub_device_put(psdev);
864 up_write(&pcistub_sem);
865 return result;
866 }
867
868 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
869 * in case of the device driver could provide this service, and then wait
870 * for pcifront ack.
871 * @dev: pointer to PCI devices
872 */
873
874 static void xen_pcibk_error_resume(struct pci_dev *dev)
875 {
876 struct pcistub_device *psdev;
877
878 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
879 dev->bus->number, dev->devfn);
880
881 down_write(&pcistub_sem);
882 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
883 dev->bus->number,
884 PCI_SLOT(dev->devfn),
885 PCI_FUNC(dev->devfn));
886
887 if (!psdev || !psdev->pdev) {
888 dev_err(&dev->dev,
889 DRV_NAME " device is not found/assigned\n");
890 goto end;
891 }
892
893 if (!psdev->pdev->sh_info) {
894 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
895 " by HVM, kill it\n");
896 kill_domain_by_device(psdev);
897 goto end;
898 }
899
900 if (!test_bit(_XEN_PCIB_AERHANDLER,
901 (unsigned long *)&psdev->pdev->sh_info->flags)) {
902 dev_err(&dev->dev,
903 "guest with no AER driver should have been killed\n");
904 kill_domain_by_device(psdev);
905 goto end;
906 }
907 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
908 PCI_ERS_RESULT_RECOVERED);
909 end:
910 if (psdev)
911 pcistub_device_put(psdev);
912 up_write(&pcistub_sem);
913 return;
914 }
915
916 /*add xen_pcibk AER handling*/
917 static const struct pci_error_handlers xen_pcibk_error_handler = {
918 .error_detected = xen_pcibk_error_detected,
919 .mmio_enabled = xen_pcibk_mmio_enabled,
920 .slot_reset = xen_pcibk_slot_reset,
921 .resume = xen_pcibk_error_resume,
922 };
923
924 /*
925 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
926 * for a normal device. I don't want it to be loaded automatically.
927 */
928
929 static struct pci_driver xen_pcibk_pci_driver = {
930 /* The name should be xen_pciback, but until the tools are updated
931 * we will keep it as pciback. */
932 .name = "pciback",
933 .id_table = pcistub_ids,
934 .probe = pcistub_probe,
935 .remove = pcistub_remove,
936 .err_handler = &xen_pcibk_error_handler,
937 };
938
939 static inline int str_to_slot(const char *buf, int *domain, int *bus,
940 int *slot, int *func)
941 {
942 int parsed = 0;
943
944 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
945 &parsed)) {
946 case 3:
947 *func = -1;
948 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
949 break;
950 case 2:
951 *slot = *func = -1;
952 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
953 break;
954 }
955 if (parsed && !buf[parsed])
956 return 0;
957
958 /* try again without domain */
959 *domain = 0;
960 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
961 case 2:
962 *func = -1;
963 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
964 break;
965 case 1:
966 *slot = *func = -1;
967 sscanf(buf, " %x:*.* %n", bus, &parsed);
968 break;
969 }
970 if (parsed && !buf[parsed])
971 return 0;
972
973 return -EINVAL;
974 }
975
976 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
977 *slot, int *func, int *reg, int *size, int *mask)
978 {
979 int parsed = 0;
980
981 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
982 reg, size, mask, &parsed);
983 if (parsed && !buf[parsed])
984 return 0;
985
986 /* try again without domain */
987 *domain = 0;
988 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
989 mask, &parsed);
990 if (parsed && !buf[parsed])
991 return 0;
992
993 return -EINVAL;
994 }
995
996 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
997 {
998 struct pcistub_device_id *pci_dev_id;
999 unsigned long flags;
1000 int rc = 0, devfn = PCI_DEVFN(slot, func);
1001
1002 if (slot < 0) {
1003 for (slot = 0; !rc && slot < 32; ++slot)
1004 rc = pcistub_device_id_add(domain, bus, slot, func);
1005 return rc;
1006 }
1007
1008 if (func < 0) {
1009 for (func = 0; !rc && func < 8; ++func)
1010 rc = pcistub_device_id_add(domain, bus, slot, func);
1011 return rc;
1012 }
1013
1014 if ((
1015 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1016 || !defined(CONFIG_PCI_DOMAINS)
1017 !pci_domains_supported ? domain :
1018 #endif
1019 domain < 0 || domain > 0xffff)
1020 || bus < 0 || bus > 0xff
1021 || PCI_SLOT(devfn) != slot
1022 || PCI_FUNC(devfn) != func)
1023 return -EINVAL;
1024
1025 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1026 if (!pci_dev_id)
1027 return -ENOMEM;
1028
1029 pci_dev_id->domain = domain;
1030 pci_dev_id->bus = bus;
1031 pci_dev_id->devfn = devfn;
1032
1033 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1034 domain, bus, slot, func);
1035
1036 spin_lock_irqsave(&device_ids_lock, flags);
1037 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
1038 spin_unlock_irqrestore(&device_ids_lock, flags);
1039
1040 return 0;
1041 }
1042
1043 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1044 {
1045 struct pcistub_device_id *pci_dev_id, *t;
1046 int err = -ENOENT;
1047 unsigned long flags;
1048
1049 spin_lock_irqsave(&device_ids_lock, flags);
1050 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1051 slot_list) {
1052 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1053 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1054 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1055 /* Don't break; here because it's possible the same
1056 * slot could be in the list more than once
1057 */
1058 list_del(&pci_dev_id->slot_list);
1059 kfree(pci_dev_id);
1060
1061 err = 0;
1062
1063 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1064 domain, bus, slot, func);
1065 }
1066 }
1067 spin_unlock_irqrestore(&device_ids_lock, flags);
1068
1069 return err;
1070 }
1071
1072 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1073 unsigned int reg, unsigned int size,
1074 unsigned int mask)
1075 {
1076 int err = 0;
1077 struct pcistub_device *psdev;
1078 struct pci_dev *dev;
1079 struct config_field *field;
1080
1081 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1082 return -EINVAL;
1083
1084 psdev = pcistub_device_find(domain, bus, slot, func);
1085 if (!psdev) {
1086 err = -ENODEV;
1087 goto out;
1088 }
1089 dev = psdev->dev;
1090
1091 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1092 if (!field) {
1093 err = -ENOMEM;
1094 goto out;
1095 }
1096
1097 field->offset = reg;
1098 field->size = size;
1099 field->mask = mask;
1100 field->init = NULL;
1101 field->reset = NULL;
1102 field->release = NULL;
1103 field->clean = xen_pcibk_config_field_free;
1104
1105 err = xen_pcibk_config_quirks_add_field(dev, field);
1106 if (err)
1107 kfree(field);
1108 out:
1109 if (psdev)
1110 pcistub_device_put(psdev);
1111 return err;
1112 }
1113
1114 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1115 size_t count)
1116 {
1117 int domain, bus, slot, func;
1118 int err;
1119
1120 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1121 if (err)
1122 goto out;
1123
1124 err = pcistub_device_id_add(domain, bus, slot, func);
1125
1126 out:
1127 if (!err)
1128 err = count;
1129 return err;
1130 }
1131 static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
1132
1133 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1134 size_t count)
1135 {
1136 int domain, bus, slot, func;
1137 int err;
1138
1139 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1140 if (err)
1141 goto out;
1142
1143 err = pcistub_device_id_remove(domain, bus, slot, func);
1144
1145 out:
1146 if (!err)
1147 err = count;
1148 return err;
1149 }
1150 static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1151
1152 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1153 {
1154 struct pcistub_device_id *pci_dev_id;
1155 size_t count = 0;
1156 unsigned long flags;
1157
1158 spin_lock_irqsave(&device_ids_lock, flags);
1159 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1160 if (count >= PAGE_SIZE)
1161 break;
1162
1163 count += scnprintf(buf + count, PAGE_SIZE - count,
1164 "%04x:%02x:%02x.%d\n",
1165 pci_dev_id->domain, pci_dev_id->bus,
1166 PCI_SLOT(pci_dev_id->devfn),
1167 PCI_FUNC(pci_dev_id->devfn));
1168 }
1169 spin_unlock_irqrestore(&device_ids_lock, flags);
1170
1171 return count;
1172 }
1173 static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1174
1175 static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1176 {
1177 struct pcistub_device *psdev;
1178 struct xen_pcibk_dev_data *dev_data;
1179 size_t count = 0;
1180 unsigned long flags;
1181
1182 spin_lock_irqsave(&pcistub_devices_lock, flags);
1183 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1184 if (count >= PAGE_SIZE)
1185 break;
1186 if (!psdev->dev)
1187 continue;
1188 dev_data = pci_get_drvdata(psdev->dev);
1189 if (!dev_data)
1190 continue;
1191 count +=
1192 scnprintf(buf + count, PAGE_SIZE - count,
1193 "%s:%s:%sing:%ld\n",
1194 pci_name(psdev->dev),
1195 dev_data->isr_on ? "on" : "off",
1196 dev_data->ack_intr ? "ack" : "not ack",
1197 dev_data->handled);
1198 }
1199 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1200 return count;
1201 }
1202 static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1203
1204 static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1205 const char *buf,
1206 size_t count)
1207 {
1208 struct pcistub_device *psdev;
1209 struct xen_pcibk_dev_data *dev_data;
1210 int domain, bus, slot, func;
1211 int err;
1212
1213 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1214 if (err)
1215 return err;
1216
1217 psdev = pcistub_device_find(domain, bus, slot, func);
1218 if (!psdev) {
1219 err = -ENOENT;
1220 goto out;
1221 }
1222
1223 dev_data = pci_get_drvdata(psdev->dev);
1224 if (!dev_data) {
1225 err = -ENOENT;
1226 goto out;
1227 }
1228
1229 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1230 dev_data->irq_name, dev_data->isr_on,
1231 !dev_data->isr_on);
1232
1233 dev_data->isr_on = !(dev_data->isr_on);
1234 if (dev_data->isr_on)
1235 dev_data->ack_intr = 1;
1236 out:
1237 if (psdev)
1238 pcistub_device_put(psdev);
1239 if (!err)
1240 err = count;
1241 return err;
1242 }
1243 static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1244 pcistub_irq_handler_switch);
1245
1246 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1247 size_t count)
1248 {
1249 int domain, bus, slot, func, reg, size, mask;
1250 int err;
1251
1252 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1253 &mask);
1254 if (err)
1255 goto out;
1256
1257 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1258
1259 out:
1260 if (!err)
1261 err = count;
1262 return err;
1263 }
1264
1265 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1266 {
1267 int count = 0;
1268 unsigned long flags;
1269 struct xen_pcibk_config_quirk *quirk;
1270 struct xen_pcibk_dev_data *dev_data;
1271 const struct config_field *field;
1272 const struct config_field_entry *cfg_entry;
1273
1274 spin_lock_irqsave(&device_ids_lock, flags);
1275 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1276 if (count >= PAGE_SIZE)
1277 goto out;
1278
1279 count += scnprintf(buf + count, PAGE_SIZE - count,
1280 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1281 quirk->pdev->bus->number,
1282 PCI_SLOT(quirk->pdev->devfn),
1283 PCI_FUNC(quirk->pdev->devfn),
1284 quirk->devid.vendor, quirk->devid.device,
1285 quirk->devid.subvendor,
1286 quirk->devid.subdevice);
1287
1288 dev_data = pci_get_drvdata(quirk->pdev);
1289
1290 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1291 field = cfg_entry->field;
1292 if (count >= PAGE_SIZE)
1293 goto out;
1294
1295 count += scnprintf(buf + count, PAGE_SIZE - count,
1296 "\t\t%08x:%01x:%08x\n",
1297 cfg_entry->base_offset +
1298 field->offset, field->size,
1299 field->mask);
1300 }
1301 }
1302
1303 out:
1304 spin_unlock_irqrestore(&device_ids_lock, flags);
1305
1306 return count;
1307 }
1308 static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1309 pcistub_quirk_add);
1310
1311 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1312 size_t count)
1313 {
1314 int domain, bus, slot, func;
1315 int err;
1316 struct pcistub_device *psdev;
1317 struct xen_pcibk_dev_data *dev_data;
1318
1319 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1320 if (err)
1321 goto out;
1322
1323 psdev = pcistub_device_find(domain, bus, slot, func);
1324 if (!psdev) {
1325 err = -ENODEV;
1326 goto out;
1327 }
1328
1329 dev_data = pci_get_drvdata(psdev->dev);
1330 /* the driver data for a device should never be null at this point */
1331 if (!dev_data) {
1332 err = -ENXIO;
1333 goto release;
1334 }
1335 if (!dev_data->permissive) {
1336 dev_data->permissive = 1;
1337 /* Let user know that what they're doing could be unsafe */
1338 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1339 "configuration space accesses!\n");
1340 dev_warn(&psdev->dev->dev,
1341 "permissive mode is potentially unsafe!\n");
1342 }
1343 release:
1344 pcistub_device_put(psdev);
1345 out:
1346 if (!err)
1347 err = count;
1348 return err;
1349 }
1350
1351 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1352 {
1353 struct pcistub_device *psdev;
1354 struct xen_pcibk_dev_data *dev_data;
1355 size_t count = 0;
1356 unsigned long flags;
1357 spin_lock_irqsave(&pcistub_devices_lock, flags);
1358 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1359 if (count >= PAGE_SIZE)
1360 break;
1361 if (!psdev->dev)
1362 continue;
1363 dev_data = pci_get_drvdata(psdev->dev);
1364 if (!dev_data || !dev_data->permissive)
1365 continue;
1366 count +=
1367 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1368 pci_name(psdev->dev));
1369 }
1370 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1371 return count;
1372 }
1373 static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1374 permissive_add);
1375
1376 static void pcistub_exit(void)
1377 {
1378 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1379 driver_remove_file(&xen_pcibk_pci_driver.driver,
1380 &driver_attr_remove_slot);
1381 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1382 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1383 driver_remove_file(&xen_pcibk_pci_driver.driver,
1384 &driver_attr_permissive);
1385 driver_remove_file(&xen_pcibk_pci_driver.driver,
1386 &driver_attr_irq_handlers);
1387 driver_remove_file(&xen_pcibk_pci_driver.driver,
1388 &driver_attr_irq_handler_state);
1389 pci_unregister_driver(&xen_pcibk_pci_driver);
1390 }
1391
1392 static int __init pcistub_init(void)
1393 {
1394 int pos = 0;
1395 int err = 0;
1396 int domain, bus, slot, func;
1397 int parsed;
1398
1399 if (pci_devs_to_hide && *pci_devs_to_hide) {
1400 do {
1401 parsed = 0;
1402
1403 err = sscanf(pci_devs_to_hide + pos,
1404 " (%x:%x:%x.%x) %n",
1405 &domain, &bus, &slot, &func, &parsed);
1406 switch (err) {
1407 case 3:
1408 func = -1;
1409 sscanf(pci_devs_to_hide + pos,
1410 " (%x:%x:%x.*) %n",
1411 &domain, &bus, &slot, &parsed);
1412 break;
1413 case 2:
1414 slot = func = -1;
1415 sscanf(pci_devs_to_hide + pos,
1416 " (%x:%x:*.*) %n",
1417 &domain, &bus, &parsed);
1418 break;
1419 }
1420
1421 if (!parsed) {
1422 domain = 0;
1423 err = sscanf(pci_devs_to_hide + pos,
1424 " (%x:%x.%x) %n",
1425 &bus, &slot, &func, &parsed);
1426 switch (err) {
1427 case 2:
1428 func = -1;
1429 sscanf(pci_devs_to_hide + pos,
1430 " (%x:%x.*) %n",
1431 &bus, &slot, &parsed);
1432 break;
1433 case 1:
1434 slot = func = -1;
1435 sscanf(pci_devs_to_hide + pos,
1436 " (%x:*.*) %n",
1437 &bus, &parsed);
1438 break;
1439 }
1440 }
1441
1442 if (parsed <= 0)
1443 goto parse_error;
1444
1445 err = pcistub_device_id_add(domain, bus, slot, func);
1446 if (err)
1447 goto out;
1448
1449 pos += parsed;
1450 } while (pci_devs_to_hide[pos]);
1451 }
1452
1453 /* If we're the first PCI Device Driver to register, we're the
1454 * first one to get offered PCI devices as they become
1455 * available (and thus we can be the first to grab them)
1456 */
1457 err = pci_register_driver(&xen_pcibk_pci_driver);
1458 if (err < 0)
1459 goto out;
1460
1461 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1462 &driver_attr_new_slot);
1463 if (!err)
1464 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1465 &driver_attr_remove_slot);
1466 if (!err)
1467 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1468 &driver_attr_slots);
1469 if (!err)
1470 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1471 &driver_attr_quirks);
1472 if (!err)
1473 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1474 &driver_attr_permissive);
1475
1476 if (!err)
1477 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1478 &driver_attr_irq_handlers);
1479 if (!err)
1480 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1481 &driver_attr_irq_handler_state);
1482 if (err)
1483 pcistub_exit();
1484
1485 out:
1486 return err;
1487
1488 parse_error:
1489 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1490 pci_devs_to_hide + pos);
1491 return -EINVAL;
1492 }
1493
1494 #ifndef MODULE
1495 /*
1496 * fs_initcall happens before device_initcall
1497 * so xen_pcibk *should* get called first (b/c we
1498 * want to suck up any device before other drivers
1499 * get a chance by being the first pci device
1500 * driver to register)
1501 */
1502 fs_initcall(pcistub_init);
1503 #endif
1504
1505 static int __init xen_pcibk_init(void)
1506 {
1507 int err;
1508
1509 if (!xen_initial_domain())
1510 return -ENODEV;
1511
1512 err = xen_pcibk_config_init();
1513 if (err)
1514 return err;
1515
1516 #ifdef MODULE
1517 err = pcistub_init();
1518 if (err < 0)
1519 return err;
1520 #endif
1521
1522 pcistub_init_devices_late();
1523 err = xen_pcibk_xenbus_register();
1524 if (err)
1525 pcistub_exit();
1526
1527 return err;
1528 }
1529
1530 static void __exit xen_pcibk_cleanup(void)
1531 {
1532 xen_pcibk_xenbus_unregister();
1533 pcistub_exit();
1534 }
1535
1536 module_init(xen_pcibk_init);
1537 module_exit(xen_pcibk_cleanup);
1538
1539 MODULE_LICENSE("Dual BSD/GPL");
1540 MODULE_ALIAS("xen-backend:pci");
This page took 0.218278 seconds and 5 git commands to generate.