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