ia64/linux-2.6.18-xen.hg

view drivers/xen/pciback/pci_stub.c @ 898:ca12928cdafe

pci: fix pcie-aer recovery mechanism defects.

When aer error happening, if the device is not hided or assigned,
exit. If the device is assigned yet not connected by PV guest or is
owned by HVM guest, kill the guest. [sh_info is NULL]

Signed-Off-By: Liping Ke <liping.ke@intel.com>
Signed-Off-By: Yunhong Jiang <yunhong.jiang@intel.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Jun 08 12:23:24 2009 +0100 (2009-06-08)
parents 78d81e85e8cd
children
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/rwsem.h>
10 #include <linux/list.h>
11 #include <linux/spinlock.h>
12 #include <linux/kref.h>
13 #include <linux/pci.h>
14 #include <linux/wait.h>
15 #include <asm/atomic.h>
16 #include <xen/evtchn.h>
17 #include "pciback.h"
18 #include "conf_space.h"
19 #include "conf_space_quirks.h"
21 static char *pci_devs_to_hide = NULL;
22 wait_queue_head_t aer_wait_queue;
23 /*Add sem for sync AER handling and pciback remove/reconfigue ops,
24 * We want to avoid in middle of AER ops, pciback devices is being removed
25 */
26 static DECLARE_RWSEM(pcistub_sem);
27 module_param_named(hide, pci_devs_to_hide, charp, 0444);
29 struct pcistub_device_id {
30 struct list_head slot_list;
31 int domain;
32 unsigned char bus;
33 unsigned int devfn;
34 };
35 static LIST_HEAD(pcistub_device_ids);
36 static DEFINE_SPINLOCK(device_ids_lock);
38 struct pcistub_device {
39 struct kref kref;
40 struct list_head dev_list;
41 spinlock_t lock;
43 struct pci_dev *dev;
44 struct pciback_device *pdev; /* non-NULL if struct pci_dev is in use */
45 };
47 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
48 * flag must be locked with pcistub_devices_lock
49 */
50 static DEFINE_SPINLOCK(pcistub_devices_lock);
51 static LIST_HEAD(pcistub_devices);
53 /* wait for device_initcall before initializing our devices
54 * (see pcistub_init_devices_late)
55 */
56 static int initialize_devices = 0;
57 static LIST_HEAD(seized_devices);
59 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
60 {
61 struct pcistub_device *psdev;
63 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
65 psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
66 if (!psdev)
67 return NULL;
69 psdev->dev = pci_dev_get(dev);
70 if (!psdev->dev) {
71 kfree(psdev);
72 return NULL;
73 }
75 kref_init(&psdev->kref);
76 spin_lock_init(&psdev->lock);
78 return psdev;
79 }
81 /* Don't call this directly as it's called by pcistub_device_put */
82 static void pcistub_device_release(struct kref *kref)
83 {
84 struct pcistub_device *psdev;
86 psdev = container_of(kref, struct pcistub_device, kref);
88 dev_dbg(&psdev->dev->dev, "pcistub_device_release\n");
90 /* Clean-up the device */
91 pciback_reset_device(psdev->dev);
92 pciback_config_free_dyn_fields(psdev->dev);
93 pciback_config_free_dev(psdev->dev);
94 kfree(pci_get_drvdata(psdev->dev));
95 pci_set_drvdata(psdev->dev, NULL);
97 pci_dev_put(psdev->dev);
99 kfree(psdev);
100 }
102 static inline void pcistub_device_get(struct pcistub_device *psdev)
103 {
104 kref_get(&psdev->kref);
105 }
107 static inline void pcistub_device_put(struct pcistub_device *psdev)
108 {
109 kref_put(&psdev->kref, pcistub_device_release);
110 }
112 static struct pcistub_device *pcistub_device_find(int domain, int bus,
113 int slot, int func)
114 {
115 struct pcistub_device *psdev = NULL;
116 unsigned long flags;
118 spin_lock_irqsave(&pcistub_devices_lock, flags);
120 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
121 if (psdev->dev != NULL
122 && domain == pci_domain_nr(psdev->dev->bus)
123 && bus == psdev->dev->bus->number
124 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
125 pcistub_device_get(psdev);
126 goto out;
127 }
128 }
130 /* didn't find it */
131 psdev = NULL;
133 out:
134 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
135 return psdev;
136 }
138 static struct pci_dev *pcistub_device_get_pci_dev(struct pciback_device *pdev,
139 struct pcistub_device *psdev)
140 {
141 struct pci_dev *pci_dev = NULL;
142 unsigned long flags;
144 pcistub_device_get(psdev);
146 spin_lock_irqsave(&psdev->lock, flags);
147 if (!psdev->pdev) {
148 psdev->pdev = pdev;
149 pci_dev = psdev->dev;
150 }
151 spin_unlock_irqrestore(&psdev->lock, flags);
153 if (!pci_dev)
154 pcistub_device_put(psdev);
156 return pci_dev;
157 }
159 struct pci_dev *pcistub_get_pci_dev_by_slot(struct pciback_device *pdev,
160 int domain, int bus,
161 int slot, int func)
162 {
163 struct pcistub_device *psdev;
164 struct pci_dev *found_dev = NULL;
165 unsigned long flags;
167 spin_lock_irqsave(&pcistub_devices_lock, flags);
169 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
170 if (psdev->dev != NULL
171 && domain == pci_domain_nr(psdev->dev->bus)
172 && bus == psdev->dev->bus->number
173 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
174 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
175 break;
176 }
177 }
179 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
180 return found_dev;
181 }
183 struct pci_dev *pcistub_get_pci_dev(struct pciback_device *pdev,
184 struct pci_dev *dev)
185 {
186 struct pcistub_device *psdev;
187 struct pci_dev *found_dev = NULL;
188 unsigned long flags;
190 spin_lock_irqsave(&pcistub_devices_lock, flags);
192 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
193 if (psdev->dev == dev) {
194 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
195 break;
196 }
197 }
199 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
200 return found_dev;
201 }
203 void pcistub_put_pci_dev(struct pci_dev *dev)
204 {
205 struct pcistub_device *psdev, *found_psdev = NULL;
206 unsigned long flags;
208 spin_lock_irqsave(&pcistub_devices_lock, flags);
210 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
211 if (psdev->dev == dev) {
212 found_psdev = psdev;
213 break;
214 }
215 }
217 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
219 /*hold this lock for avoiding breaking link between
220 * pcistub and pciback when AER is in processing
221 */
222 down_write(&pcistub_sem);
223 /* Cleanup our device
224 * (so it's ready for the next domain)
225 */
226 pciback_reset_device(found_psdev->dev);
227 pciback_config_free_dyn_fields(found_psdev->dev);
228 pciback_config_reset_dev(found_psdev->dev);
230 spin_lock_irqsave(&found_psdev->lock, flags);
231 found_psdev->pdev = NULL;
232 spin_unlock_irqrestore(&found_psdev->lock, flags);
234 pcistub_device_put(found_psdev);
235 up_write(&pcistub_sem);
236 }
238 static int __devinit pcistub_match_one(struct pci_dev *dev,
239 struct pcistub_device_id *pdev_id)
240 {
241 /* Match the specified device by domain, bus, slot, func and also if
242 * any of the device's parent bridges match.
243 */
244 for (; dev != NULL; dev = dev->bus->self) {
245 if (pci_domain_nr(dev->bus) == pdev_id->domain
246 && dev->bus->number == pdev_id->bus
247 && dev->devfn == pdev_id->devfn)
248 return 1;
250 /* Sometimes topmost bridge links to itself. */
251 if (dev == dev->bus->self)
252 break;
253 }
255 return 0;
256 }
258 static int __devinit pcistub_match(struct pci_dev *dev)
259 {
260 struct pcistub_device_id *pdev_id;
261 unsigned long flags;
262 int found = 0;
264 spin_lock_irqsave(&device_ids_lock, flags);
265 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
266 if (pcistub_match_one(dev, pdev_id)) {
267 found = 1;
268 break;
269 }
270 }
271 spin_unlock_irqrestore(&device_ids_lock, flags);
273 return found;
274 }
276 static int __devinit pcistub_init_device(struct pci_dev *dev)
277 {
278 struct pciback_dev_data *dev_data;
279 int err = 0;
281 dev_dbg(&dev->dev, "initializing...\n");
283 /* The PCI backend is not intended to be a module (or to work with
284 * removable PCI devices (yet). If it were, pciback_config_free()
285 * would need to be called somewhere to free the memory allocated
286 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
287 */
288 dev_data = kzalloc(sizeof(*dev_data), GFP_ATOMIC);
289 if (!dev_data) {
290 err = -ENOMEM;
291 goto out;
292 }
293 pci_set_drvdata(dev, dev_data);
295 dev_dbg(&dev->dev, "initializing config\n");
297 init_waitqueue_head(&aer_wait_queue);
298 err = pciback_config_init_dev(dev);
299 if (err)
300 goto out;
302 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
303 * must do this here because pcibios_enable_device may specify
304 * the pci device's true irq (and possibly its other resources)
305 * if they differ from what's in the configuration space.
306 * This makes the assumption that the device's resources won't
307 * change after this point (otherwise this code may break!)
308 */
309 dev_dbg(&dev->dev, "enabling device\n");
310 err = pci_enable_device(dev);
311 if (err)
312 goto config_release;
314 /* Now disable the device (this also ensures some private device
315 * data is setup before we export)
316 */
317 dev_dbg(&dev->dev, "reset device\n");
318 pciback_reset_device(dev);
320 return 0;
322 config_release:
323 pciback_config_free_dev(dev);
325 out:
326 pci_set_drvdata(dev, NULL);
327 kfree(dev_data);
328 return err;
329 }
331 /*
332 * Because some initialization still happens on
333 * devices during fs_initcall, we need to defer
334 * full initialization of our devices until
335 * device_initcall.
336 */
337 static int __init pcistub_init_devices_late(void)
338 {
339 struct pcistub_device *psdev;
340 unsigned long flags;
341 int err = 0;
343 pr_debug("pciback: pcistub_init_devices_late\n");
345 spin_lock_irqsave(&pcistub_devices_lock, flags);
347 while (!list_empty(&seized_devices)) {
348 psdev = container_of(seized_devices.next,
349 struct pcistub_device, dev_list);
350 list_del(&psdev->dev_list);
352 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
354 err = pcistub_init_device(psdev->dev);
355 if (err) {
356 dev_err(&psdev->dev->dev,
357 "error %d initializing device\n", err);
358 kfree(psdev);
359 psdev = NULL;
360 }
362 spin_lock_irqsave(&pcistub_devices_lock, flags);
364 if (psdev)
365 list_add_tail(&psdev->dev_list, &pcistub_devices);
366 }
368 initialize_devices = 1;
370 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
372 return 0;
373 }
375 static int __devinit pcistub_seize(struct pci_dev *dev)
376 {
377 struct pcistub_device *psdev;
378 unsigned long flags;
379 int err = 0;
381 psdev = pcistub_device_alloc(dev);
382 if (!psdev)
383 return -ENOMEM;
385 spin_lock_irqsave(&pcistub_devices_lock, flags);
387 if (initialize_devices) {
388 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
390 /* don't want irqs disabled when calling pcistub_init_device */
391 err = pcistub_init_device(psdev->dev);
393 spin_lock_irqsave(&pcistub_devices_lock, flags);
395 if (!err)
396 list_add(&psdev->dev_list, &pcistub_devices);
397 } else {
398 dev_dbg(&dev->dev, "deferring initialization\n");
399 list_add(&psdev->dev_list, &seized_devices);
400 }
402 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
404 if (err)
405 pcistub_device_put(psdev);
407 return err;
408 }
410 static int __devinit pcistub_probe(struct pci_dev *dev,
411 const struct pci_device_id *id)
412 {
413 int err = 0;
415 dev_dbg(&dev->dev, "probing...\n");
417 if (pcistub_match(dev)) {
419 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
420 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
421 dev_err(&dev->dev, "can't export pci devices that "
422 "don't have a normal (0) or bridge (1) "
423 "header type!\n");
424 err = -ENODEV;
425 goto out;
426 }
428 dev_info(&dev->dev, "seizing device\n");
429 err = pcistub_seize(dev);
430 #ifdef CONFIG_PCI_GUESTDEV
431 } else if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
432 if (!pci_is_guestdev(dev)) {
433 err = -ENODEV;
434 goto out;
435 }
437 dev_info(&dev->dev, "seizing device\n");
438 err = pcistub_seize(dev);
439 #endif /* CONFIG_PCI_GUESTDEV */
440 } else
441 /* Didn't find the device */
442 err = -ENODEV;
444 out:
445 return err;
446 }
448 static void pcistub_remove(struct pci_dev *dev)
449 {
450 struct pcistub_device *psdev, *found_psdev = NULL;
451 unsigned long flags;
453 dev_dbg(&dev->dev, "removing\n");
455 spin_lock_irqsave(&pcistub_devices_lock, flags);
457 pciback_config_quirk_release(dev);
459 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
460 if (psdev->dev == dev) {
461 found_psdev = psdev;
462 break;
463 }
464 }
466 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
468 if (found_psdev) {
469 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
470 found_psdev->pdev);
472 if (found_psdev->pdev) {
473 printk(KERN_WARNING "pciback: ****** removing device "
474 "%s while still in-use! ******\n",
475 pci_name(found_psdev->dev));
476 printk(KERN_WARNING "pciback: ****** driver domain may "
477 "still access this device's i/o resources!\n");
478 printk(KERN_WARNING "pciback: ****** shutdown driver "
479 "domain before binding device\n");
480 printk(KERN_WARNING "pciback: ****** to other drivers "
481 "or domains\n");
483 pciback_release_pci_dev(found_psdev->pdev,
484 found_psdev->dev);
485 }
487 spin_lock_irqsave(&pcistub_devices_lock, flags);
488 list_del(&found_psdev->dev_list);
489 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
491 /* the final put for releasing from the list */
492 pcistub_device_put(found_psdev);
493 }
494 }
496 static const struct pci_device_id pcistub_ids[] = {
497 {
498 .vendor = PCI_ANY_ID,
499 .device = PCI_ANY_ID,
500 .subvendor = PCI_ANY_ID,
501 .subdevice = PCI_ANY_ID,
502 },
503 {0,},
504 };
506 static void kill_domain_by_device(struct pcistub_device *psdev)
507 {
508 struct xenbus_transaction xbt;
509 int err;
510 char nodename[1024];
512 if (!psdev)
513 dev_err(&psdev->dev->dev,
514 "device is NULL when do AER recovery/kill_domain\n");
515 sprintf(nodename, "/local/domain/0/backend/pci/%d/0",
516 psdev->pdev->xdev->otherend_id);
517 nodename[strlen(nodename)] = '\0';
519 again:
520 err = xenbus_transaction_start(&xbt);
521 if (err)
522 {
523 dev_err(&psdev->dev->dev,
524 "error %d when start xenbus transaction\n", err);
525 return;
526 }
527 /*PV AER handlers will set this flag*/
528 xenbus_printf(xbt, nodename, "aerState" , "aerfail" );
529 err = xenbus_transaction_end(xbt, 0);
530 if (err)
531 {
532 if (err == -EAGAIN)
533 goto again;
534 dev_err(&psdev->dev->dev,
535 "error %d when end xenbus transaction\n", err);
536 return;
537 }
538 }
540 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
541 * backend need to have cooperation. In pciback, those steps will do similar
542 * jobs: send service request and waiting for front_end response.
543 */
544 static pci_ers_result_t common_process(struct pcistub_device *psdev,
545 pci_channel_state_t state, int aer_cmd, pci_ers_result_t result)
546 {
547 pci_ers_result_t res = result;
548 struct xen_pcie_aer_op *aer_op;
549 int ret;
551 /*with PV AER drivers*/
552 aer_op = &(psdev->pdev->sh_info->aer_op);
553 aer_op->cmd = aer_cmd ;
554 /*useful for error_detected callback*/
555 aer_op->err = state;
556 /*pcifront_end BDF*/
557 ret = pciback_get_pcifront_dev(psdev->dev, psdev->pdev,
558 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
559 if (!ret) {
560 dev_err(&psdev->dev->dev,
561 "pciback: failed to get pcifront device\n");
562 return PCI_ERS_RESULT_NONE;
563 }
564 wmb();
566 dev_dbg(&psdev->dev->dev,
567 "pciback: aer_op %x dom %x bus %x devfn %x\n",
568 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
569 /*local flag to mark there's aer request, pciback callback will use this
570 * flag to judge whether we need to check pci-front give aer service
571 * ack signal
572 */
573 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
575 /*It is possible that a pcifront conf_read_write ops request invokes
576 * the callback which cause the spurious execution of wake_up.
577 * Yet it is harmless and better than a spinlock here
578 */
579 set_bit(_XEN_PCIB_active,
580 (unsigned long *)&psdev->pdev->sh_info->flags);
581 wmb();
582 notify_remote_via_irq(psdev->pdev->evtchn_irq);
584 ret = wait_event_timeout(aer_wait_queue, !(test_bit(_XEN_PCIB_active,
585 (unsigned long *)&psdev->pdev->sh_info->flags)), 300*HZ);
587 if (!ret) {
588 if (test_bit(_XEN_PCIB_active,
589 (unsigned long *)&psdev->pdev->sh_info->flags)) {
590 dev_err(&psdev->dev->dev,
591 "pcifront aer process not responding!\n");
592 clear_bit(_XEN_PCIB_active,
593 (unsigned long *)&psdev->pdev->sh_info->flags);
594 aer_op->err = PCI_ERS_RESULT_NONE;
595 return res;
596 }
597 }
598 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
600 if ( test_bit( _XEN_PCIF_active,
601 (unsigned long*)&psdev->pdev->sh_info->flags)) {
602 dev_dbg(&psdev->dev->dev,
603 "schedule pci_conf service in pciback \n");
604 test_and_schedule_op(psdev->pdev);
605 }
607 res = (pci_ers_result_t)aer_op->err;
608 return res;
609 }
611 /*
612 * pciback_slot_reset: it will send the slot_reset request to pcifront in case
613 * of the device driver could provide this service, and then wait for pcifront
614 * ack.
615 * @dev: pointer to PCI devices
616 * return value is used by aer_core do_recovery policy
617 */
618 static pci_ers_result_t pciback_slot_reset(struct pci_dev *dev)
619 {
620 struct pcistub_device *psdev;
621 pci_ers_result_t result;
623 result = PCI_ERS_RESULT_RECOVERED;
624 dev_dbg(&dev->dev, "pciback_slot_reset(bus:%x,devfn:%x)\n",
625 dev->bus->number, dev->devfn);
627 down_write(&pcistub_sem);
628 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
629 dev->bus->number,
630 PCI_SLOT(dev->devfn),
631 PCI_FUNC(dev->devfn));
633 if ( !psdev || !psdev->pdev )
634 {
635 dev_err(&dev->dev,
636 "pciback device is not found/assigned\n");
637 goto end;
638 }
640 if ( !psdev->pdev->sh_info )
641 {
642 dev_err(&dev->dev, "pciback device is not connected or owned"
643 " by HVM, kill it\n");
644 kill_domain_by_device(psdev);
645 goto release;
646 }
648 if ( !test_bit(_XEN_PCIB_AERHANDLER,
649 (unsigned long *)&psdev->pdev->sh_info->flags) ) {
650 dev_err(&dev->dev,
651 "guest with no AER driver should have been killed\n");
652 goto release;
653 }
654 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
656 if (result == PCI_ERS_RESULT_NONE ||
657 result == PCI_ERS_RESULT_DISCONNECT) {
658 dev_dbg(&dev->dev,
659 "No AER slot_reset service or disconnected!\n");
660 kill_domain_by_device(psdev);
661 }
662 release:
663 pcistub_device_put(psdev);
664 end:
665 up_write(&pcistub_sem);
666 return result;
668 }
671 /*pciback_mmio_enabled: it will send the mmio_enabled request to pcifront
672 * in case of the device driver could provide this service, and then wait
673 * for pcifront ack.
674 * @dev: pointer to PCI devices
675 * return value is used by aer_core do_recovery policy
676 */
678 static pci_ers_result_t pciback_mmio_enabled(struct pci_dev *dev)
679 {
680 struct pcistub_device *psdev;
681 pci_ers_result_t result;
683 result = PCI_ERS_RESULT_RECOVERED;
684 dev_dbg(&dev->dev, "pciback_mmio_enabled(bus:%x,devfn:%x)\n",
685 dev->bus->number, dev->devfn);
687 down_write(&pcistub_sem);
688 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
689 dev->bus->number,
690 PCI_SLOT(dev->devfn),
691 PCI_FUNC(dev->devfn));
693 if ( !psdev || !psdev->pdev )
694 {
695 dev_err(&dev->dev,
696 "pciback device is not found/assigned\n");
697 goto end;
698 }
700 if ( !psdev->pdev->sh_info )
701 {
702 dev_err(&dev->dev, "pciback device is not connected or owned"
703 " by HVM, kill it\n");
704 kill_domain_by_device(psdev);
705 goto release;
706 }
708 if ( !test_bit(_XEN_PCIB_AERHANDLER,
709 (unsigned long *)&psdev->pdev->sh_info->flags) ) {
710 dev_err(&dev->dev,
711 "guest with no AER driver should have been killed\n");
712 goto release;
713 }
714 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
716 if (result == PCI_ERS_RESULT_NONE ||
717 result == PCI_ERS_RESULT_DISCONNECT) {
718 dev_dbg(&dev->dev,
719 "No AER mmio_enabled service or disconnected!\n");
720 kill_domain_by_device(psdev);
721 }
722 release:
723 pcistub_device_put(psdev);
724 end:
725 up_write(&pcistub_sem);
726 return result;
727 }
729 /*pciback_error_detected: it will send the error_detected request to pcifront
730 * in case of the device driver could provide this service, and then wait
731 * for pcifront ack.
732 * @dev: pointer to PCI devices
733 * @error: the current PCI connection state
734 * return value is used by aer_core do_recovery policy
735 */
737 static pci_ers_result_t pciback_error_detected(struct pci_dev *dev,
738 pci_channel_state_t error)
739 {
740 struct pcistub_device *psdev;
741 pci_ers_result_t result;
743 result = PCI_ERS_RESULT_CAN_RECOVER;
744 dev_dbg(&dev->dev, "pciback_error_detected(bus:%x,devfn:%x)\n",
745 dev->bus->number, dev->devfn);
747 down_write(&pcistub_sem);
748 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
749 dev->bus->number,
750 PCI_SLOT(dev->devfn),
751 PCI_FUNC(dev->devfn));
753 if ( !psdev || !psdev->pdev )
754 {
755 dev_err(&dev->dev,
756 "pciback device is not found/assigned\n");
757 goto end;
758 }
760 if ( !psdev->pdev->sh_info )
761 {
762 dev_err(&dev->dev, "pciback device is not connected or owned"
763 " by HVM, kill it\n");
764 kill_domain_by_device(psdev);
765 goto release;
766 }
768 /*Guest owns the device yet no aer handler regiested, kill guest*/
769 if ( !test_bit(_XEN_PCIB_AERHANDLER,
770 (unsigned long *)&psdev->pdev->sh_info->flags) ) {
771 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
772 kill_domain_by_device(psdev);
773 goto release;
774 }
775 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
777 if (result == PCI_ERS_RESULT_NONE ||
778 result == PCI_ERS_RESULT_DISCONNECT) {
779 dev_dbg(&dev->dev,
780 "No AER error_detected service or disconnected!\n");
781 kill_domain_by_device(psdev);
782 }
783 release:
784 pcistub_device_put(psdev);
785 end:
786 up_write(&pcistub_sem);
787 return result;
788 }
790 /*pciback_error_resume: it will send the error_resume request to pcifront
791 * in case of the device driver could provide this service, and then wait
792 * for pcifront ack.
793 * @dev: pointer to PCI devices
794 */
796 static void pciback_error_resume(struct pci_dev *dev)
797 {
798 struct pcistub_device *psdev;
800 dev_dbg(&dev->dev, "pciback_error_resume(bus:%x,devfn:%x)\n",
801 dev->bus->number, dev->devfn);
803 down_write(&pcistub_sem);
804 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
805 dev->bus->number,
806 PCI_SLOT(dev->devfn),
807 PCI_FUNC(dev->devfn));
809 if ( !psdev || !psdev->pdev )
810 {
811 dev_err(&dev->dev,
812 "pciback device is not found/assigned\n");
813 goto end;
814 }
816 if ( !psdev->pdev->sh_info )
817 {
818 dev_err(&dev->dev, "pciback device is not connected or owned"
819 " by HVM, kill it\n");
820 kill_domain_by_device(psdev);
821 goto release;
822 }
824 if ( !test_bit(_XEN_PCIB_AERHANDLER,
825 (unsigned long *)&psdev->pdev->sh_info->flags) ) {
826 dev_err(&dev->dev,
827 "guest with no AER driver should have been killed\n");
828 kill_domain_by_device(psdev);
829 goto release;
830 }
831 common_process(psdev, 1, XEN_PCI_OP_aer_resume, PCI_ERS_RESULT_RECOVERED);
832 release:
833 pcistub_device_put(psdev);
834 end:
835 up_write(&pcistub_sem);
836 return;
837 }
839 /*add pciback AER handling*/
840 static struct pci_error_handlers pciback_error_handler = {
841 .error_detected = pciback_error_detected,
842 .mmio_enabled = pciback_mmio_enabled,
843 .slot_reset = pciback_slot_reset,
844 .resume = pciback_error_resume,
845 };
847 /*
848 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
849 * for a normal device. I don't want it to be loaded automatically.
850 */
852 static struct pci_driver pciback_pci_driver = {
853 .name = "pciback",
854 .id_table = pcistub_ids,
855 .probe = pcistub_probe,
856 .remove = pcistub_remove,
857 .err_handler = &pciback_error_handler,
858 };
860 static inline int str_to_slot(const char *buf, int *domain, int *bus,
861 int *slot, int *func)
862 {
863 int err;
865 err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
866 if (err == 4)
867 return 0;
868 else if (err < 0)
869 return -EINVAL;
871 /* try again without domain */
872 *domain = 0;
873 err = sscanf(buf, " %x:%x.%x", bus, slot, func);
874 if (err == 3)
875 return 0;
877 return -EINVAL;
878 }
880 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
881 *slot, int *func, int *reg, int *size, int *mask)
882 {
883 int err;
885 err =
886 sscanf(buf, " %04x:%02x:%02x.%1x-%08x:%1x:%08x", domain, bus, slot,
887 func, reg, size, mask);
888 if (err == 7)
889 return 0;
890 return -EINVAL;
891 }
893 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
894 {
895 struct pcistub_device_id *pci_dev_id;
896 unsigned long flags;
898 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
899 if (!pci_dev_id)
900 return -ENOMEM;
902 pci_dev_id->domain = domain;
903 pci_dev_id->bus = bus;
904 pci_dev_id->devfn = PCI_DEVFN(slot, func);
906 pr_debug("pciback: wants to seize %04x:%02x:%02x.%01x\n",
907 domain, bus, slot, func);
909 spin_lock_irqsave(&device_ids_lock, flags);
910 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
911 spin_unlock_irqrestore(&device_ids_lock, flags);
913 return 0;
914 }
916 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
917 {
918 struct pcistub_device_id *pci_dev_id, *t;
919 int devfn = PCI_DEVFN(slot, func);
920 int err = -ENOENT;
921 unsigned long flags;
923 spin_lock_irqsave(&device_ids_lock, flags);
924 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids, slot_list) {
926 if (pci_dev_id->domain == domain
927 && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
928 /* Don't break; here because it's possible the same
929 * slot could be in the list more than once
930 */
931 list_del(&pci_dev_id->slot_list);
932 kfree(pci_dev_id);
934 err = 0;
936 pr_debug("pciback: removed %04x:%02x:%02x.%01x from "
937 "seize list\n", domain, bus, slot, func);
938 }
939 }
940 spin_unlock_irqrestore(&device_ids_lock, flags);
942 return err;
943 }
945 static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
946 int size, int mask)
947 {
948 int err = 0;
949 struct pcistub_device *psdev;
950 struct pci_dev *dev;
951 struct config_field *field;
953 psdev = pcistub_device_find(domain, bus, slot, func);
954 if (!psdev || !psdev->dev) {
955 err = -ENODEV;
956 goto out;
957 }
958 dev = psdev->dev;
960 field = kzalloc(sizeof(*field), GFP_ATOMIC);
961 if (!field) {
962 err = -ENOMEM;
963 goto out;
964 }
966 field->offset = reg;
967 field->size = size;
968 field->mask = mask;
969 field->init = NULL;
970 field->reset = NULL;
971 field->release = NULL;
972 field->clean = pciback_config_field_free;
974 err = pciback_config_quirks_add_field(dev, field);
975 if (err)
976 kfree(field);
977 out:
978 return err;
979 }
981 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
982 size_t count)
983 {
984 int domain, bus, slot, func;
985 int err;
987 err = str_to_slot(buf, &domain, &bus, &slot, &func);
988 if (err)
989 goto out;
991 err = pcistub_device_id_add(domain, bus, slot, func);
993 out:
994 if (!err)
995 err = count;
996 return err;
997 }
999 DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
1001 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1002 size_t count)
1004 int domain, bus, slot, func;
1005 int err;
1007 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1008 if (err)
1009 goto out;
1011 err = pcistub_device_id_remove(domain, bus, slot, func);
1013 out:
1014 if (!err)
1015 err = count;
1016 return err;
1019 DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1021 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1023 struct pcistub_device_id *pci_dev_id;
1024 size_t count = 0;
1025 unsigned long flags;
1027 spin_lock_irqsave(&device_ids_lock, flags);
1028 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1029 if (count >= PAGE_SIZE)
1030 break;
1032 count += scnprintf(buf + count, PAGE_SIZE - count,
1033 "%04x:%02x:%02x.%01x\n",
1034 pci_dev_id->domain, pci_dev_id->bus,
1035 PCI_SLOT(pci_dev_id->devfn),
1036 PCI_FUNC(pci_dev_id->devfn));
1038 spin_unlock_irqrestore(&device_ids_lock, flags);
1040 return count;
1043 DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1045 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1046 size_t count)
1048 int domain, bus, slot, func, reg, size, mask;
1049 int err;
1051 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1052 &mask);
1053 if (err)
1054 goto out;
1056 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1058 out:
1059 if (!err)
1060 err = count;
1061 return err;
1064 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1066 int count = 0;
1067 unsigned long flags;
1068 extern struct list_head pciback_quirks;
1069 struct pciback_config_quirk *quirk;
1070 struct pciback_dev_data *dev_data;
1071 const struct config_field *field;
1072 const struct config_field_entry *cfg_entry;
1074 spin_lock_irqsave(&device_ids_lock, flags);
1075 list_for_each_entry(quirk, &pciback_quirks, quirks_list) {
1076 if (count >= PAGE_SIZE)
1077 goto out;
1079 count += scnprintf(buf + count, PAGE_SIZE - count,
1080 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1081 quirk->pdev->bus->number,
1082 PCI_SLOT(quirk->pdev->devfn),
1083 PCI_FUNC(quirk->pdev->devfn),
1084 quirk->devid.vendor, quirk->devid.device,
1085 quirk->devid.subvendor,
1086 quirk->devid.subdevice);
1088 dev_data = pci_get_drvdata(quirk->pdev);
1090 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1091 field = cfg_entry->field;
1092 if (count >= PAGE_SIZE)
1093 goto out;
1095 count += scnprintf(buf + count, PAGE_SIZE - count,
1096 "\t\t%08x:%01x:%08x\n",
1097 cfg_entry->base_offset + field->offset,
1098 field->size, field->mask);
1102 out:
1103 spin_unlock_irqrestore(&device_ids_lock, flags);
1105 return count;
1108 DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show, pcistub_quirk_add);
1110 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1111 size_t count)
1113 int domain, bus, slot, func;
1114 int err;
1115 struct pcistub_device *psdev;
1116 struct pciback_dev_data *dev_data;
1117 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1118 if (err)
1119 goto out;
1120 psdev = pcistub_device_find(domain, bus, slot, func);
1121 if (!psdev) {
1122 err = -ENODEV;
1123 goto out;
1125 if (!psdev->dev) {
1126 err = -ENODEV;
1127 goto release;
1129 dev_data = pci_get_drvdata(psdev->dev);
1130 /* the driver data for a device should never be null at this point */
1131 if (!dev_data) {
1132 err = -ENXIO;
1133 goto release;
1135 if (!dev_data->permissive) {
1136 dev_data->permissive = 1;
1137 /* Let user know that what they're doing could be unsafe */
1138 dev_warn(&psdev->dev->dev,
1139 "enabling permissive mode configuration space accesses!\n");
1140 dev_warn(&psdev->dev->dev,
1141 "permissive mode is potentially unsafe!\n");
1143 release:
1144 pcistub_device_put(psdev);
1145 out:
1146 if (!err)
1147 err = count;
1148 return err;
1151 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1153 struct pcistub_device *psdev;
1154 struct pciback_dev_data *dev_data;
1155 size_t count = 0;
1156 unsigned long flags;
1157 spin_lock_irqsave(&pcistub_devices_lock, flags);
1158 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1159 if (count >= PAGE_SIZE)
1160 break;
1161 if (!psdev->dev)
1162 continue;
1163 dev_data = pci_get_drvdata(psdev->dev);
1164 if (!dev_data || !dev_data->permissive)
1165 continue;
1166 count +=
1167 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1168 pci_name(psdev->dev));
1170 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1171 return count;
1174 DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show, permissive_add);
1176 #ifdef CONFIG_PCI_MSI
1178 int pciback_get_owner(struct pci_dev *dev)
1180 struct pcistub_device *psdev;
1182 psdev = pcistub_device_find(pci_domain_nr(dev->bus), dev->bus->number,
1183 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
1185 if (!psdev || !psdev->pdev)
1186 return -1;
1188 return psdev->pdev->xdev->otherend_id;
1190 #endif
1192 static void pcistub_exit(void)
1194 driver_remove_file(&pciback_pci_driver.driver, &driver_attr_new_slot);
1195 driver_remove_file(&pciback_pci_driver.driver,
1196 &driver_attr_remove_slot);
1197 driver_remove_file(&pciback_pci_driver.driver, &driver_attr_slots);
1198 driver_remove_file(&pciback_pci_driver.driver, &driver_attr_quirks);
1199 driver_remove_file(&pciback_pci_driver.driver, &driver_attr_permissive);
1201 pci_unregister_driver(&pciback_pci_driver);
1202 WARN_ON(unregister_msi_get_owner(pciback_get_owner));
1205 static int __init pcistub_init(void)
1207 int pos = 0;
1208 int err = 0;
1209 int domain, bus, slot, func;
1210 int parsed;
1212 if (pci_devs_to_hide && *pci_devs_to_hide) {
1213 do {
1214 parsed = 0;
1216 err = sscanf(pci_devs_to_hide + pos,
1217 " (%x:%x:%x.%x) %n",
1218 &domain, &bus, &slot, &func, &parsed);
1219 if (err != 4) {
1220 domain = 0;
1221 err = sscanf(pci_devs_to_hide + pos,
1222 " (%x:%x.%x) %n",
1223 &bus, &slot, &func, &parsed);
1224 if (err != 3)
1225 goto parse_error;
1228 err = pcistub_device_id_add(domain, bus, slot, func);
1229 if (err)
1230 goto out;
1232 /* if parsed<=0, we've reached the end of the string */
1233 pos += parsed;
1234 } while (parsed > 0 && pci_devs_to_hide[pos]);
1237 /* If we're the first PCI Device Driver to register, we're the
1238 * first one to get offered PCI devices as they become
1239 * available (and thus we can be the first to grab them)
1240 */
1241 err = pci_register_driver(&pciback_pci_driver);
1242 if (err < 0)
1243 goto out;
1245 err = driver_create_file(&pciback_pci_driver.driver,
1246 &driver_attr_new_slot);
1247 if (!err)
1248 err = driver_create_file(&pciback_pci_driver.driver,
1249 &driver_attr_remove_slot);
1250 if (!err)
1251 err = driver_create_file(&pciback_pci_driver.driver,
1252 &driver_attr_slots);
1253 if (!err)
1254 err = driver_create_file(&pciback_pci_driver.driver,
1255 &driver_attr_quirks);
1256 if (!err)
1257 err = driver_create_file(&pciback_pci_driver.driver,
1258 &driver_attr_permissive);
1260 if (!err)
1261 err = register_msi_get_owner(pciback_get_owner);
1262 if (err)
1263 pcistub_exit();
1265 out:
1266 return err;
1268 parse_error:
1269 printk(KERN_ERR "pciback: Error parsing pci_devs_to_hide at \"%s\"\n",
1270 pci_devs_to_hide + pos);
1271 return -EINVAL;
1274 #ifndef MODULE
1275 /*
1276 * fs_initcall happens before device_initcall
1277 * so pciback *should* get called first (b/c we
1278 * want to suck up any device before other drivers
1279 * get a chance by being the first pci device
1280 * driver to register)
1281 */
1282 fs_initcall(pcistub_init);
1283 #endif
1285 static int __init pciback_init(void)
1287 int err;
1289 err = pciback_config_init();
1290 if (err)
1291 return err;
1293 #ifdef MODULE
1294 err = pcistub_init();
1295 if (err < 0)
1296 return err;
1297 #endif
1299 pcistub_init_devices_late();
1300 err = pciback_xenbus_register();
1301 if (err)
1302 pcistub_exit();
1304 return err;
1307 static void __exit pciback_cleanup(void)
1309 pciback_xenbus_unregister();
1310 pcistub_exit();
1313 module_init(pciback_init);
1314 module_exit(pciback_cleanup);
1316 MODULE_LICENSE("Dual BSD/GPL");