1
2
3
4
5
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
28static char *pci_devs_to_hide;
29wait_queue_head_t xen_pcibk_aer_wait_queue;
30
31
32
33static DECLARE_RWSEM(pcistub_sem);
34module_param_named(hide, pci_devs_to_hide, charp, 0444);
35
36struct pcistub_device_id {
37 struct list_head slot_list;
38 int domain;
39 unsigned char bus;
40 unsigned int devfn;
41};
42static LIST_HEAD(pcistub_device_ids);
43static DEFINE_SPINLOCK(device_ids_lock);
44
45struct 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;
52};
53
54
55
56
57static DEFINE_SPINLOCK(pcistub_devices_lock);
58static LIST_HEAD(pcistub_devices);
59
60
61
62
63static int initialize_devices;
64static LIST_HEAD(seized_devices);
65
66static 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
89static 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
104
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
127 xen_pcibk_reset_device(dev);
128
129 kfree(dev_data);
130 pci_set_drvdata(dev, NULL);
131
132
133 xen_pcibk_config_free_dyn_fields(dev);
134 xen_pcibk_config_free_dev(dev);
135
136 dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
137 pci_dev_put(dev);
138
139 kfree(psdev);
140}
141
142static inline void pcistub_device_get(struct pcistub_device *psdev)
143{
144 kref_get(&psdev->kref);
145}
146
147static inline void pcistub_device_put(struct pcistub_device *psdev)
148{
149 kref_put(&psdev->kref, pcistub_device_release);
150}
151
152static 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
172 psdev = NULL;
173
174out:
175 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
176 return psdev;
177}
178
179static 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
200struct 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
225struct 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
245void pcistub_put_pci_dev(struct pci_dev *dev)
246{
247 struct pcistub_device *psdev, *found_psdev = NULL;
248 unsigned long flags;
249
250 spin_lock_irqsave(&pcistub_devices_lock, flags);
251
252 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
253 if (psdev->dev == dev) {
254 found_psdev = psdev;
255 break;
256 }
257 }
258
259 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
260 if (WARN_ON(!found_psdev))
261 return;
262
263
264
265
266 down_write(&pcistub_sem);
267
268
269
270
271
272
273
274 pci_reset_function(dev);
275 pci_restore_state(psdev->dev);
276
277
278 xen_pcibk_reset_device(found_psdev->dev);
279
280
281 xen_pcibk_config_free_dyn_fields(found_psdev->dev);
282 xen_pcibk_config_reset_dev(found_psdev->dev);
283
284 xen_unregister_device_domain_owner(found_psdev->dev);
285
286 spin_lock_irqsave(&found_psdev->lock, flags);
287 found_psdev->pdev = NULL;
288 spin_unlock_irqrestore(&found_psdev->lock, flags);
289
290 pcistub_device_put(found_psdev);
291 up_write(&pcistub_sem);
292}
293
294static int pcistub_match_one(struct pci_dev *dev,
295 struct pcistub_device_id *pdev_id)
296{
297
298
299
300 for (; dev != NULL; dev = dev->bus->self) {
301 if (pci_domain_nr(dev->bus) == pdev_id->domain
302 && dev->bus->number == pdev_id->bus
303 && dev->devfn == pdev_id->devfn)
304 return 1;
305
306
307 if (dev == dev->bus->self)
308 break;
309 }
310
311 return 0;
312}
313
314static int pcistub_match(struct pci_dev *dev)
315{
316 struct pcistub_device_id *pdev_id;
317 unsigned long flags;
318 int found = 0;
319
320 spin_lock_irqsave(&device_ids_lock, flags);
321 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
322 if (pcistub_match_one(dev, pdev_id)) {
323 found = 1;
324 break;
325 }
326 }
327 spin_unlock_irqrestore(&device_ids_lock, flags);
328
329 return found;
330}
331
332static int pcistub_init_device(struct pci_dev *dev)
333{
334 struct xen_pcibk_dev_data *dev_data;
335 int err = 0;
336
337 dev_dbg(&dev->dev, "initializing...\n");
338
339
340
341
342
343
344 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
345 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
346 if (!dev_data) {
347 err = -ENOMEM;
348 goto out;
349 }
350 pci_set_drvdata(dev, dev_data);
351
352
353
354
355
356 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
357
358 dev_dbg(&dev->dev, "initializing config\n");
359
360 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
361 err = xen_pcibk_config_init_dev(dev);
362 if (err)
363 goto out;
364
365
366
367
368
369
370
371
372 dev_dbg(&dev->dev, "enabling device\n");
373 err = pci_enable_device(dev);
374 if (err)
375 goto config_release;
376
377 if (dev->msix_cap) {
378 struct physdev_pci_device ppdev = {
379 .seg = pci_domain_nr(dev->bus),
380 .bus = dev->bus->number,
381 .devfn = dev->devfn
382 };
383
384 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
385 if (err)
386 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
387 err);
388 }
389
390
391 dev_dbg(&dev->dev, "save state of device\n");
392 pci_save_state(dev);
393 dev_data->pci_saved_state = pci_store_saved_state(dev);
394 if (!dev_data->pci_saved_state)
395 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
396 else {
397 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
398 __pci_reset_function_locked(dev);
399 pci_restore_state(dev);
400 }
401
402
403
404 dev_dbg(&dev->dev, "reset device\n");
405 xen_pcibk_reset_device(dev);
406
407 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
408 return 0;
409
410config_release:
411 xen_pcibk_config_free_dev(dev);
412
413out:
414 pci_set_drvdata(dev, NULL);
415 kfree(dev_data);
416 return err;
417}
418
419
420
421
422
423
424
425static int __init pcistub_init_devices_late(void)
426{
427 struct pcistub_device *psdev;
428 unsigned long flags;
429 int err = 0;
430
431 spin_lock_irqsave(&pcistub_devices_lock, flags);
432
433 while (!list_empty(&seized_devices)) {
434 psdev = container_of(seized_devices.next,
435 struct pcistub_device, dev_list);
436 list_del(&psdev->dev_list);
437
438 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
439
440 err = pcistub_init_device(psdev->dev);
441 if (err) {
442 dev_err(&psdev->dev->dev,
443 "error %d initializing device\n", err);
444 kfree(psdev);
445 psdev = NULL;
446 }
447
448 spin_lock_irqsave(&pcistub_devices_lock, flags);
449
450 if (psdev)
451 list_add_tail(&psdev->dev_list, &pcistub_devices);
452 }
453
454 initialize_devices = 1;
455
456 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
457
458 return 0;
459}
460
461static int pcistub_seize(struct pci_dev *dev)
462{
463 struct pcistub_device *psdev;
464 unsigned long flags;
465 int err = 0;
466
467 psdev = pcistub_device_alloc(dev);
468 if (!psdev)
469 return -ENOMEM;
470
471 spin_lock_irqsave(&pcistub_devices_lock, flags);
472
473 if (initialize_devices) {
474 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
475
476
477 err = pcistub_init_device(psdev->dev);
478
479 spin_lock_irqsave(&pcistub_devices_lock, flags);
480
481 if (!err)
482 list_add(&psdev->dev_list, &pcistub_devices);
483 } else {
484 dev_dbg(&dev->dev, "deferring initialization\n");
485 list_add(&psdev->dev_list, &seized_devices);
486 }
487
488 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
489
490 if (err)
491 pcistub_device_put(psdev);
492
493 return err;
494}
495
496static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
497{
498 int err = 0;
499
500 dev_dbg(&dev->dev, "probing...\n");
501
502 if (pcistub_match(dev)) {
503
504 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
505 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
506 dev_err(&dev->dev, "can't export pci devices that "
507 "don't have a normal (0) or bridge (1) "
508 "header type!\n");
509 err = -ENODEV;
510 goto out;
511 }
512
513 dev_info(&dev->dev, "seizing device\n");
514 err = pcistub_seize(dev);
515 } else
516
517 err = -ENODEV;
518
519out:
520 return err;
521}
522
523static void pcistub_remove(struct pci_dev *dev)
524{
525 struct pcistub_device *psdev, *found_psdev = NULL;
526 unsigned long flags;
527
528 dev_dbg(&dev->dev, "removing\n");
529
530 spin_lock_irqsave(&pcistub_devices_lock, flags);
531
532 xen_pcibk_config_quirk_release(dev);
533
534 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
535 if (psdev->dev == dev) {
536 found_psdev = psdev;
537 break;
538 }
539 }
540
541 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
542
543 if (found_psdev) {
544 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
545 found_psdev->pdev);
546
547 if (found_psdev->pdev) {
548 pr_warn("****** removing device %s while still in-use! ******\n",
549 pci_name(found_psdev->dev));
550 pr_warn("****** driver domain may still access this device's i/o resources!\n");
551 pr_warn("****** shutdown driver domain before binding device\n");
552 pr_warn("****** to other drivers or domains\n");
553
554 xen_pcibk_release_pci_dev(found_psdev->pdev,
555 found_psdev->dev);
556 }
557
558 spin_lock_irqsave(&pcistub_devices_lock, flags);
559 list_del(&found_psdev->dev_list);
560 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
561
562
563 pcistub_device_put(found_psdev);
564 }
565}
566
567static const struct pci_device_id pcistub_ids[] = {
568 {
569 .vendor = PCI_ANY_ID,
570 .device = PCI_ANY_ID,
571 .subvendor = PCI_ANY_ID,
572 .subdevice = PCI_ANY_ID,
573 },
574 {0,},
575};
576
577#define PCI_NODENAME_MAX 40
578static void kill_domain_by_device(struct pcistub_device *psdev)
579{
580 struct xenbus_transaction xbt;
581 int err;
582 char nodename[PCI_NODENAME_MAX];
583
584 BUG_ON(!psdev);
585 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
586 psdev->pdev->xdev->otherend_id);
587
588again:
589 err = xenbus_transaction_start(&xbt);
590 if (err) {
591 dev_err(&psdev->dev->dev,
592 "error %d when start xenbus transaction\n", err);
593 return;
594 }
595
596 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
597 err = xenbus_transaction_end(xbt, 0);
598 if (err) {
599 if (err == -EAGAIN)
600 goto again;
601 dev_err(&psdev->dev->dev,
602 "error %d when end xenbus transaction\n", err);
603 return;
604 }
605}
606
607
608
609
610
611static pci_ers_result_t common_process(struct pcistub_device *psdev,
612 pci_channel_state_t state, int aer_cmd,
613 pci_ers_result_t result)
614{
615 pci_ers_result_t res = result;
616 struct xen_pcie_aer_op *aer_op;
617 int ret;
618
619
620 aer_op = &(psdev->pdev->sh_info->aer_op);
621 aer_op->cmd = aer_cmd ;
622
623 aer_op->err = state;
624
625 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
626 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
627 if (!ret) {
628 dev_err(&psdev->dev->dev,
629 DRV_NAME ": failed to get pcifront device\n");
630 return PCI_ERS_RESULT_NONE;
631 }
632 wmb();
633
634 dev_dbg(&psdev->dev->dev,
635 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
636 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
637
638
639
640
641 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
642
643
644
645
646
647 set_bit(_XEN_PCIB_active,
648 (unsigned long *)&psdev->pdev->sh_info->flags);
649 wmb();
650 notify_remote_via_irq(psdev->pdev->evtchn_irq);
651
652 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
653 !(test_bit(_XEN_PCIB_active, (unsigned long *)
654 &psdev->pdev->sh_info->flags)), 300*HZ);
655
656 if (!ret) {
657 if (test_bit(_XEN_PCIB_active,
658 (unsigned long *)&psdev->pdev->sh_info->flags)) {
659 dev_err(&psdev->dev->dev,
660 "pcifront aer process not responding!\n");
661 clear_bit(_XEN_PCIB_active,
662 (unsigned long *)&psdev->pdev->sh_info->flags);
663 aer_op->err = PCI_ERS_RESULT_NONE;
664 return res;
665 }
666 }
667 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
668
669 if (test_bit(_XEN_PCIF_active,
670 (unsigned long *)&psdev->pdev->sh_info->flags)) {
671 dev_dbg(&psdev->dev->dev,
672 "schedule pci_conf service in " DRV_NAME "\n");
673 xen_pcibk_test_and_schedule_op(psdev->pdev);
674 }
675
676 res = (pci_ers_result_t)aer_op->err;
677 return res;
678}
679
680
681
682
683
684
685
686
687static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
688{
689 struct pcistub_device *psdev;
690 pci_ers_result_t result;
691
692 result = PCI_ERS_RESULT_RECOVERED;
693 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
694 dev->bus->number, dev->devfn);
695
696 down_write(&pcistub_sem);
697 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
698 dev->bus->number,
699 PCI_SLOT(dev->devfn),
700 PCI_FUNC(dev->devfn));
701
702 if (!psdev || !psdev->pdev) {
703 dev_err(&dev->dev,
704 DRV_NAME " device is not found/assigned\n");
705 goto end;
706 }
707
708 if (!psdev->pdev->sh_info) {
709 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
710 " by HVM, kill it\n");
711 kill_domain_by_device(psdev);
712 goto end;
713 }
714
715 if (!test_bit(_XEN_PCIB_AERHANDLER,
716 (unsigned long *)&psdev->pdev->sh_info->flags)) {
717 dev_err(&dev->dev,
718 "guest with no AER driver should have been killed\n");
719 goto end;
720 }
721 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
722
723 if (result == PCI_ERS_RESULT_NONE ||
724 result == PCI_ERS_RESULT_DISCONNECT) {
725 dev_dbg(&dev->dev,
726 "No AER slot_reset service or disconnected!\n");
727 kill_domain_by_device(psdev);
728 }
729end:
730 if (psdev)
731 pcistub_device_put(psdev);
732 up_write(&pcistub_sem);
733 return result;
734
735}
736
737
738
739
740
741
742
743
744
745static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
746{
747 struct pcistub_device *psdev;
748 pci_ers_result_t result;
749
750 result = PCI_ERS_RESULT_RECOVERED;
751 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
752 dev->bus->number, dev->devfn);
753
754 down_write(&pcistub_sem);
755 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
756 dev->bus->number,
757 PCI_SLOT(dev->devfn),
758 PCI_FUNC(dev->devfn));
759
760 if (!psdev || !psdev->pdev) {
761 dev_err(&dev->dev,
762 DRV_NAME " device is not found/assigned\n");
763 goto end;
764 }
765
766 if (!psdev->pdev->sh_info) {
767 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
768 " by HVM, kill it\n");
769 kill_domain_by_device(psdev);
770 goto end;
771 }
772
773 if (!test_bit(_XEN_PCIB_AERHANDLER,
774 (unsigned long *)&psdev->pdev->sh_info->flags)) {
775 dev_err(&dev->dev,
776 "guest with no AER driver should have been killed\n");
777 goto end;
778 }
779 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
780
781 if (result == PCI_ERS_RESULT_NONE ||
782 result == PCI_ERS_RESULT_DISCONNECT) {
783 dev_dbg(&dev->dev,
784 "No AER mmio_enabled service or disconnected!\n");
785 kill_domain_by_device(psdev);
786 }
787end:
788 if (psdev)
789 pcistub_device_put(psdev);
790 up_write(&pcistub_sem);
791 return result;
792}
793
794
795
796
797
798
799
800
801
802static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
803 pci_channel_state_t error)
804{
805 struct pcistub_device *psdev;
806 pci_ers_result_t result;
807
808 result = PCI_ERS_RESULT_CAN_RECOVER;
809 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
810 dev->bus->number, dev->devfn);
811
812 down_write(&pcistub_sem);
813 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
814 dev->bus->number,
815 PCI_SLOT(dev->devfn),
816 PCI_FUNC(dev->devfn));
817
818 if (!psdev || !psdev->pdev) {
819 dev_err(&dev->dev,
820 DRV_NAME " device is not found/assigned\n");
821 goto end;
822 }
823
824 if (!psdev->pdev->sh_info) {
825 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
826 " by HVM, kill it\n");
827 kill_domain_by_device(psdev);
828 goto end;
829 }
830
831
832 if (!test_bit(_XEN_PCIB_AERHANDLER,
833 (unsigned long *)&psdev->pdev->sh_info->flags)) {
834 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
835 kill_domain_by_device(psdev);
836 goto end;
837 }
838 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
839
840 if (result == PCI_ERS_RESULT_NONE ||
841 result == PCI_ERS_RESULT_DISCONNECT) {
842 dev_dbg(&dev->dev,
843 "No AER error_detected service or disconnected!\n");
844 kill_domain_by_device(psdev);
845 }
846end:
847 if (psdev)
848 pcistub_device_put(psdev);
849 up_write(&pcistub_sem);
850 return result;
851}
852
853
854
855
856
857
858
859static void xen_pcibk_error_resume(struct pci_dev *dev)
860{
861 struct pcistub_device *psdev;
862
863 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
864 dev->bus->number, dev->devfn);
865
866 down_write(&pcistub_sem);
867 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
868 dev->bus->number,
869 PCI_SLOT(dev->devfn),
870 PCI_FUNC(dev->devfn));
871
872 if (!psdev || !psdev->pdev) {
873 dev_err(&dev->dev,
874 DRV_NAME " device is not found/assigned\n");
875 goto end;
876 }
877
878 if (!psdev->pdev->sh_info) {
879 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
880 " by HVM, kill it\n");
881 kill_domain_by_device(psdev);
882 goto end;
883 }
884
885 if (!test_bit(_XEN_PCIB_AERHANDLER,
886 (unsigned long *)&psdev->pdev->sh_info->flags)) {
887 dev_err(&dev->dev,
888 "guest with no AER driver should have been killed\n");
889 kill_domain_by_device(psdev);
890 goto end;
891 }
892 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
893 PCI_ERS_RESULT_RECOVERED);
894end:
895 if (psdev)
896 pcistub_device_put(psdev);
897 up_write(&pcistub_sem);
898 return;
899}
900
901
902static const struct pci_error_handlers xen_pcibk_error_handler = {
903 .error_detected = xen_pcibk_error_detected,
904 .mmio_enabled = xen_pcibk_mmio_enabled,
905 .slot_reset = xen_pcibk_slot_reset,
906 .resume = xen_pcibk_error_resume,
907};
908
909
910
911
912
913
914static struct pci_driver xen_pcibk_pci_driver = {
915
916
917 .name = "pciback",
918 .id_table = pcistub_ids,
919 .probe = pcistub_probe,
920 .remove = pcistub_remove,
921 .err_handler = &xen_pcibk_error_handler,
922};
923
924static inline int str_to_slot(const char *buf, int *domain, int *bus,
925 int *slot, int *func)
926{
927 int parsed = 0;
928
929 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
930 &parsed)) {
931 case 3:
932 *func = -1;
933 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
934 break;
935 case 2:
936 *slot = *func = -1;
937 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
938 break;
939 }
940 if (parsed && !buf[parsed])
941 return 0;
942
943
944 *domain = 0;
945 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
946 case 2:
947 *func = -1;
948 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
949 break;
950 case 1:
951 *slot = *func = -1;
952 sscanf(buf, " %x:*.* %n", bus, &parsed);
953 break;
954 }
955 if (parsed && !buf[parsed])
956 return 0;
957
958 return -EINVAL;
959}
960
961static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
962 *slot, int *func, int *reg, int *size, int *mask)
963{
964 int parsed = 0;
965
966 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
967 reg, size, mask, &parsed);
968 if (parsed && !buf[parsed])
969 return 0;
970
971
972 *domain = 0;
973 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
974 mask, &parsed);
975 if (parsed && !buf[parsed])
976 return 0;
977
978 return -EINVAL;
979}
980
981static int pcistub_device_id_add(int domain, int bus, int slot, int func)
982{
983 struct pcistub_device_id *pci_dev_id;
984 unsigned long flags;
985 int rc = 0, devfn = PCI_DEVFN(slot, func);
986
987 if (slot < 0) {
988 for (slot = 0; !rc && slot < 32; ++slot)
989 rc = pcistub_device_id_add(domain, bus, slot, func);
990 return rc;
991 }
992
993 if (func < 0) {
994 for (func = 0; !rc && func < 8; ++func)
995 rc = pcistub_device_id_add(domain, bus, slot, func);
996 return rc;
997 }
998
999 if ((
1000#if !defined(MODULE) \
1001 || !defined(CONFIG_PCI_DOMAINS)
1002 !pci_domains_supported ? domain :
1003#endif
1004 domain < 0 || domain > 0xffff)
1005 || bus < 0 || bus > 0xff
1006 || PCI_SLOT(devfn) != slot
1007 || PCI_FUNC(devfn) != func)
1008 return -EINVAL;
1009
1010 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1011 if (!pci_dev_id)
1012 return -ENOMEM;
1013
1014 pci_dev_id->domain = domain;
1015 pci_dev_id->bus = bus;
1016 pci_dev_id->devfn = devfn;
1017
1018 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1019 domain, bus, slot, func);
1020
1021 spin_lock_irqsave(&device_ids_lock, flags);
1022 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
1023 spin_unlock_irqrestore(&device_ids_lock, flags);
1024
1025 return 0;
1026}
1027
1028static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1029{
1030 struct pcistub_device_id *pci_dev_id, *t;
1031 int err = -ENOENT;
1032 unsigned long flags;
1033
1034 spin_lock_irqsave(&device_ids_lock, flags);
1035 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1036 slot_list) {
1037 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1038 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1039 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1040
1041
1042
1043 list_del(&pci_dev_id->slot_list);
1044 kfree(pci_dev_id);
1045
1046 err = 0;
1047
1048 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1049 domain, bus, slot, func);
1050 }
1051 }
1052 spin_unlock_irqrestore(&device_ids_lock, flags);
1053
1054 return err;
1055}
1056
1057static int pcistub_reg_add(int domain, int bus, int slot, int func,
1058 unsigned int reg, unsigned int size,
1059 unsigned int mask)
1060{
1061 int err = 0;
1062 struct pcistub_device *psdev;
1063 struct pci_dev *dev;
1064 struct config_field *field;
1065
1066 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1067 return -EINVAL;
1068
1069 psdev = pcistub_device_find(domain, bus, slot, func);
1070 if (!psdev) {
1071 err = -ENODEV;
1072 goto out;
1073 }
1074 dev = psdev->dev;
1075
1076 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1077 if (!field) {
1078 err = -ENOMEM;
1079 goto out;
1080 }
1081
1082 field->offset = reg;
1083 field->size = size;
1084 field->mask = mask;
1085 field->init = NULL;
1086 field->reset = NULL;
1087 field->release = NULL;
1088 field->clean = xen_pcibk_config_field_free;
1089
1090 err = xen_pcibk_config_quirks_add_field(dev, field);
1091 if (err)
1092 kfree(field);
1093out:
1094 if (psdev)
1095 pcistub_device_put(psdev);
1096 return err;
1097}
1098
1099static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1100 size_t count)
1101{
1102 int domain, bus, slot, func;
1103 int err;
1104
1105 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1106 if (err)
1107 goto out;
1108
1109 err = pcistub_device_id_add(domain, bus, slot, func);
1110
1111out:
1112 if (!err)
1113 err = count;
1114 return err;
1115}
1116static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
1117
1118static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1119 size_t count)
1120{
1121 int domain, bus, slot, func;
1122 int err;
1123
1124 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1125 if (err)
1126 goto out;
1127
1128 err = pcistub_device_id_remove(domain, bus, slot, func);
1129
1130out:
1131 if (!err)
1132 err = count;
1133 return err;
1134}
1135static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1136
1137static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1138{
1139 struct pcistub_device_id *pci_dev_id;
1140 size_t count = 0;
1141 unsigned long flags;
1142
1143 spin_lock_irqsave(&device_ids_lock, flags);
1144 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1145 if (count >= PAGE_SIZE)
1146 break;
1147
1148 count += scnprintf(buf + count, PAGE_SIZE - count,
1149 "%04x:%02x:%02x.%d\n",
1150 pci_dev_id->domain, pci_dev_id->bus,
1151 PCI_SLOT(pci_dev_id->devfn),
1152 PCI_FUNC(pci_dev_id->devfn));
1153 }
1154 spin_unlock_irqrestore(&device_ids_lock, flags);
1155
1156 return count;
1157}
1158static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1159
1160static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1161{
1162 struct pcistub_device *psdev;
1163 struct xen_pcibk_dev_data *dev_data;
1164 size_t count = 0;
1165 unsigned long flags;
1166
1167 spin_lock_irqsave(&pcistub_devices_lock, flags);
1168 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1169 if (count >= PAGE_SIZE)
1170 break;
1171 if (!psdev->dev)
1172 continue;
1173 dev_data = pci_get_drvdata(psdev->dev);
1174 if (!dev_data)
1175 continue;
1176 count +=
1177 scnprintf(buf + count, PAGE_SIZE - count,
1178 "%s:%s:%sing:%ld\n",
1179 pci_name(psdev->dev),
1180 dev_data->isr_on ? "on" : "off",
1181 dev_data->ack_intr ? "ack" : "not ack",
1182 dev_data->handled);
1183 }
1184 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1185 return count;
1186}
1187static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1188
1189static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1190 const char *buf,
1191 size_t count)
1192{
1193 struct pcistub_device *psdev;
1194 struct xen_pcibk_dev_data *dev_data;
1195 int domain, bus, slot, func;
1196 int err = -ENOENT;
1197
1198 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1199 if (err)
1200 return err;
1201
1202 psdev = pcistub_device_find(domain, bus, slot, func);
1203 if (!psdev)
1204 goto out;
1205
1206 dev_data = pci_get_drvdata(psdev->dev);
1207 if (!dev_data)
1208 goto out;
1209
1210 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1211 dev_data->irq_name, dev_data->isr_on,
1212 !dev_data->isr_on);
1213
1214 dev_data->isr_on = !(dev_data->isr_on);
1215 if (dev_data->isr_on)
1216 dev_data->ack_intr = 1;
1217out:
1218 if (psdev)
1219 pcistub_device_put(psdev);
1220 if (!err)
1221 err = count;
1222 return err;
1223}
1224static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1225 pcistub_irq_handler_switch);
1226
1227static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1228 size_t count)
1229{
1230 int domain, bus, slot, func, reg, size, mask;
1231 int err;
1232
1233 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1234 &mask);
1235 if (err)
1236 goto out;
1237
1238 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1239
1240out:
1241 if (!err)
1242 err = count;
1243 return err;
1244}
1245
1246static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1247{
1248 int count = 0;
1249 unsigned long flags;
1250 struct xen_pcibk_config_quirk *quirk;
1251 struct xen_pcibk_dev_data *dev_data;
1252 const struct config_field *field;
1253 const struct config_field_entry *cfg_entry;
1254
1255 spin_lock_irqsave(&device_ids_lock, flags);
1256 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1257 if (count >= PAGE_SIZE)
1258 goto out;
1259
1260 count += scnprintf(buf + count, PAGE_SIZE - count,
1261 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1262 quirk->pdev->bus->number,
1263 PCI_SLOT(quirk->pdev->devfn),
1264 PCI_FUNC(quirk->pdev->devfn),
1265 quirk->devid.vendor, quirk->devid.device,
1266 quirk->devid.subvendor,
1267 quirk->devid.subdevice);
1268
1269 dev_data = pci_get_drvdata(quirk->pdev);
1270
1271 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1272 field = cfg_entry->field;
1273 if (count >= PAGE_SIZE)
1274 goto out;
1275
1276 count += scnprintf(buf + count, PAGE_SIZE - count,
1277 "\t\t%08x:%01x:%08x\n",
1278 cfg_entry->base_offset +
1279 field->offset, field->size,
1280 field->mask);
1281 }
1282 }
1283
1284out:
1285 spin_unlock_irqrestore(&device_ids_lock, flags);
1286
1287 return count;
1288}
1289static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1290 pcistub_quirk_add);
1291
1292static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1293 size_t count)
1294{
1295 int domain, bus, slot, func;
1296 int err;
1297 struct pcistub_device *psdev;
1298 struct xen_pcibk_dev_data *dev_data;
1299
1300 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1301 if (err)
1302 goto out;
1303
1304 psdev = pcistub_device_find(domain, bus, slot, func);
1305 if (!psdev) {
1306 err = -ENODEV;
1307 goto out;
1308 }
1309
1310 dev_data = pci_get_drvdata(psdev->dev);
1311
1312 if (!dev_data) {
1313 err = -ENXIO;
1314 goto release;
1315 }
1316 if (!dev_data->permissive) {
1317 dev_data->permissive = 1;
1318
1319 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1320 "configuration space accesses!\n");
1321 dev_warn(&psdev->dev->dev,
1322 "permissive mode is potentially unsafe!\n");
1323 }
1324release:
1325 pcistub_device_put(psdev);
1326out:
1327 if (!err)
1328 err = count;
1329 return err;
1330}
1331
1332static ssize_t permissive_show(struct device_driver *drv, char *buf)
1333{
1334 struct pcistub_device *psdev;
1335 struct xen_pcibk_dev_data *dev_data;
1336 size_t count = 0;
1337 unsigned long flags;
1338 spin_lock_irqsave(&pcistub_devices_lock, flags);
1339 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1340 if (count >= PAGE_SIZE)
1341 break;
1342 if (!psdev->dev)
1343 continue;
1344 dev_data = pci_get_drvdata(psdev->dev);
1345 if (!dev_data || !dev_data->permissive)
1346 continue;
1347 count +=
1348 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1349 pci_name(psdev->dev));
1350 }
1351 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1352 return count;
1353}
1354static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1355 permissive_add);
1356
1357static void pcistub_exit(void)
1358{
1359 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1360 driver_remove_file(&xen_pcibk_pci_driver.driver,
1361 &driver_attr_remove_slot);
1362 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1363 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1364 driver_remove_file(&xen_pcibk_pci_driver.driver,
1365 &driver_attr_permissive);
1366 driver_remove_file(&xen_pcibk_pci_driver.driver,
1367 &driver_attr_irq_handlers);
1368 driver_remove_file(&xen_pcibk_pci_driver.driver,
1369 &driver_attr_irq_handler_state);
1370 pci_unregister_driver(&xen_pcibk_pci_driver);
1371}
1372
1373static int __init pcistub_init(void)
1374{
1375 int pos = 0;
1376 int err = 0;
1377 int domain, bus, slot, func;
1378 int parsed;
1379
1380 if (pci_devs_to_hide && *pci_devs_to_hide) {
1381 do {
1382 parsed = 0;
1383
1384 err = sscanf(pci_devs_to_hide + pos,
1385 " (%x:%x:%x.%x) %n",
1386 &domain, &bus, &slot, &func, &parsed);
1387 switch (err) {
1388 case 3:
1389 func = -1;
1390 sscanf(pci_devs_to_hide + pos,
1391 " (%x:%x:%x.*) %n",
1392 &domain, &bus, &slot, &parsed);
1393 break;
1394 case 2:
1395 slot = func = -1;
1396 sscanf(pci_devs_to_hide + pos,
1397 " (%x:%x:*.*) %n",
1398 &domain, &bus, &parsed);
1399 break;
1400 }
1401
1402 if (!parsed) {
1403 domain = 0;
1404 err = sscanf(pci_devs_to_hide + pos,
1405 " (%x:%x.%x) %n",
1406 &bus, &slot, &func, &parsed);
1407 switch (err) {
1408 case 2:
1409 func = -1;
1410 sscanf(pci_devs_to_hide + pos,
1411 " (%x:%x.*) %n",
1412 &bus, &slot, &parsed);
1413 break;
1414 case 1:
1415 slot = func = -1;
1416 sscanf(pci_devs_to_hide + pos,
1417 " (%x:*.*) %n",
1418 &bus, &parsed);
1419 break;
1420 }
1421 }
1422
1423 if (parsed <= 0)
1424 goto parse_error;
1425
1426 err = pcistub_device_id_add(domain, bus, slot, func);
1427 if (err)
1428 goto out;
1429
1430 pos += parsed;
1431 } while (pci_devs_to_hide[pos]);
1432 }
1433
1434
1435
1436
1437
1438 err = pci_register_driver(&xen_pcibk_pci_driver);
1439 if (err < 0)
1440 goto out;
1441
1442 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1443 &driver_attr_new_slot);
1444 if (!err)
1445 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1446 &driver_attr_remove_slot);
1447 if (!err)
1448 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1449 &driver_attr_slots);
1450 if (!err)
1451 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1452 &driver_attr_quirks);
1453 if (!err)
1454 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1455 &driver_attr_permissive);
1456
1457 if (!err)
1458 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1459 &driver_attr_irq_handlers);
1460 if (!err)
1461 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1462 &driver_attr_irq_handler_state);
1463 if (err)
1464 pcistub_exit();
1465
1466out:
1467 return err;
1468
1469parse_error:
1470 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1471 pci_devs_to_hide + pos);
1472 return -EINVAL;
1473}
1474
1475#ifndef MODULE
1476
1477
1478
1479
1480
1481
1482
1483fs_initcall(pcistub_init);
1484#endif
1485
1486static int __init xen_pcibk_init(void)
1487{
1488 int err;
1489
1490 if (!xen_initial_domain())
1491 return -ENODEV;
1492
1493 err = xen_pcibk_config_init();
1494 if (err)
1495 return err;
1496
1497#ifdef MODULE
1498 err = pcistub_init();
1499 if (err < 0)
1500 return err;
1501#endif
1502
1503 pcistub_init_devices_late();
1504 err = xen_pcibk_xenbus_register();
1505 if (err)
1506 pcistub_exit();
1507
1508 return err;
1509}
1510
1511static void __exit xen_pcibk_cleanup(void)
1512{
1513 xen_pcibk_xenbus_unregister();
1514 pcistub_exit();
1515}
1516
1517module_init(xen_pcibk_init);
1518module_exit(xen_pcibk_cleanup);
1519
1520MODULE_LICENSE("Dual BSD/GPL");
1521MODULE_ALIAS("xen-backend:pci");
1522