2 * PCI Backend Xenbus Setup - handles setup with frontend and xend
4 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/list.h>
9 #include <linux/vmalloc.h>
10 #include <linux/workqueue.h>
11 #include <xen/xenbus.h>
12 #include <xen/events.h>
13 #include <asm/xen/pci.h>
14 #include <linux/workqueue.h>
17 #define INVALID_EVTCHN_IRQ (-1)
18 struct workqueue_struct
*pciback_wq
;
20 static struct pciback_device
*alloc_pdev(struct xenbus_device
*xdev
)
22 struct pciback_device
*pdev
;
24 pdev
= kzalloc(sizeof(struct pciback_device
), GFP_KERNEL
);
27 dev_dbg(&xdev
->dev
, "allocated pdev @ 0x%p\n", pdev
);
30 dev_set_drvdata(&xdev
->dev
, pdev
);
32 spin_lock_init(&pdev
->dev_lock
);
35 pdev
->evtchn_irq
= INVALID_EVTCHN_IRQ
;
36 pdev
->be_watching
= 0;
38 INIT_WORK(&pdev
->op_work
, pciback_do_op
);
40 if (pciback_init_devices(pdev
)) {
48 static void pciback_disconnect(struct pciback_device
*pdev
)
50 spin_lock(&pdev
->dev_lock
);
52 /* Ensure the guest can't trigger our handler before removing devices */
53 if (pdev
->evtchn_irq
!= INVALID_EVTCHN_IRQ
) {
54 unbind_from_irqhandler(pdev
->evtchn_irq
, pdev
);
55 pdev
->evtchn_irq
= INVALID_EVTCHN_IRQ
;
57 spin_unlock(&pdev
->dev_lock
);
59 /* If the driver domain started an op, make sure we complete it
60 * before releasing the shared memory */
62 /* Note, the workqueue does not use spinlocks at all.*/
63 flush_workqueue(pciback_wq
);
65 spin_lock(&pdev
->dev_lock
);
66 if (pdev
->sh_info
!= NULL
) {
67 xenbus_unmap_ring_vfree(pdev
->xdev
, pdev
->sh_info
);
70 spin_unlock(&pdev
->dev_lock
);
74 static void free_pdev(struct pciback_device
*pdev
)
76 if (pdev
->be_watching
) {
77 unregister_xenbus_watch(&pdev
->be_watch
);
78 pdev
->be_watching
= 0;
81 pciback_disconnect(pdev
);
83 pciback_release_devices(pdev
);
85 dev_set_drvdata(&pdev
->xdev
->dev
, NULL
);
91 static int pciback_do_attach(struct pciback_device
*pdev
, int gnt_ref
,
97 dev_dbg(&pdev
->xdev
->dev
,
98 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
99 gnt_ref
, remote_evtchn
);
101 err
= xenbus_map_ring_valloc(pdev
->xdev
, gnt_ref
, &vaddr
);
103 xenbus_dev_fatal(pdev
->xdev
, err
,
104 "Error mapping other domain page in ours.");
108 spin_lock(&pdev
->dev_lock
);
109 pdev
->sh_info
= vaddr
;
110 spin_unlock(&pdev
->dev_lock
);
112 err
= bind_interdomain_evtchn_to_irqhandler(
113 pdev
->xdev
->otherend_id
, remote_evtchn
, pciback_handle_event
,
116 xenbus_dev_fatal(pdev
->xdev
, err
,
117 "Error binding event channel to IRQ");
121 spin_lock(&pdev
->dev_lock
);
122 pdev
->evtchn_irq
= err
;
123 spin_unlock(&pdev
->dev_lock
);
126 dev_dbg(&pdev
->xdev
->dev
, "Attached!\n");
131 static int pciback_attach(struct pciback_device
*pdev
)
134 int gnt_ref
, remote_evtchn
;
138 /* Make sure we only do this setup once */
139 if (xenbus_read_driver_state(pdev
->xdev
->nodename
) !=
140 XenbusStateInitialised
)
143 /* Wait for frontend to state that it has published the configuration */
144 if (xenbus_read_driver_state(pdev
->xdev
->otherend
) !=
145 XenbusStateInitialised
)
148 dev_dbg(&pdev
->xdev
->dev
, "Reading frontend config\n");
150 err
= xenbus_gather(XBT_NIL
, pdev
->xdev
->otherend
,
151 "pci-op-ref", "%u", &gnt_ref
,
152 "event-channel", "%u", &remote_evtchn
,
153 "magic", NULL
, &magic
, NULL
);
155 /* If configuration didn't get read correctly, wait longer */
156 xenbus_dev_fatal(pdev
->xdev
, err
,
157 "Error reading configuration from frontend");
161 if (magic
== NULL
|| strcmp(magic
, XEN_PCI_MAGIC
) != 0) {
162 xenbus_dev_fatal(pdev
->xdev
, -EFAULT
,
163 "version mismatch (%s/%s) with pcifront - "
165 magic
, XEN_PCI_MAGIC
);
169 err
= pciback_do_attach(pdev
, gnt_ref
, remote_evtchn
);
173 dev_dbg(&pdev
->xdev
->dev
, "Connecting...\n");
175 err
= xenbus_switch_state(pdev
->xdev
, XenbusStateConnected
);
177 xenbus_dev_fatal(pdev
->xdev
, err
,
178 "Error switching to connected state!");
180 dev_dbg(&pdev
->xdev
->dev
, "Connected? %d\n", err
);
188 static int pciback_publish_pci_dev(struct pciback_device
*pdev
,
189 unsigned int domain
, unsigned int bus
,
190 unsigned int devfn
, unsigned int devid
)
196 len
= snprintf(str
, sizeof(str
), "vdev-%d", devid
);
197 if (unlikely(len
>= (sizeof(str
) - 1))) {
202 err
= xenbus_printf(XBT_NIL
, pdev
->xdev
->nodename
, str
,
203 "%04x:%02x:%02x.%02x", domain
, bus
,
204 PCI_SLOT(devfn
), PCI_FUNC(devfn
));
210 static int pciback_export_device(struct pciback_device
*pdev
,
211 int domain
, int bus
, int slot
, int func
,
217 dev_dbg(&pdev
->xdev
->dev
, "exporting dom %x bus %x slot %x func %x\n",
218 domain
, bus
, slot
, func
);
220 dev
= pcistub_get_pci_dev_by_slot(pdev
, domain
, bus
, slot
, func
);
223 xenbus_dev_fatal(pdev
->xdev
, err
,
224 "Couldn't locate PCI device "
225 "(%04x:%02x:%02x.%01x)! "
226 "perhaps already in-use?",
227 domain
, bus
, slot
, func
);
231 err
= pciback_add_pci_dev(pdev
, dev
, devid
, pciback_publish_pci_dev
);
235 dev_dbg(&dev
->dev
, "registering for %d\n", pdev
->xdev
->otherend_id
);
236 if (xen_register_device_domain_owner(dev
,
237 pdev
->xdev
->otherend_id
) != 0) {
238 dev_err(&dev
->dev
, "device has been assigned to another " \
239 "domain! Over-writting the ownership, but beware.\n");
240 xen_unregister_device_domain_owner(dev
);
241 xen_register_device_domain_owner(dev
, pdev
->xdev
->otherend_id
);
244 /* TODO: It'd be nice to export a bridge and have all of its children
245 * get exported with it. This may be best done in xend (which will
246 * have to calculate resource usage anyway) but we probably want to
247 * put something in here to ensure that if a bridge gets given to a
248 * driver domain, that all devices under that bridge are not given
249 * to other driver domains (as he who controls the bridge can disable
250 * it and stop the other devices from working).
256 static int pciback_remove_device(struct pciback_device
*pdev
,
257 int domain
, int bus
, int slot
, int func
)
262 dev_dbg(&pdev
->xdev
->dev
, "removing dom %x bus %x slot %x func %x\n",
263 domain
, bus
, slot
, func
);
265 dev
= pciback_get_pci_dev(pdev
, domain
, bus
, PCI_DEVFN(slot
, func
));
268 dev_dbg(&pdev
->xdev
->dev
, "Couldn't locate PCI device "
269 "(%04x:%02x:%02x.%01x)! not owned by this domain\n",
270 domain
, bus
, slot
, func
);
274 dev_dbg(&dev
->dev
, "unregistering for %d\n", pdev
->xdev
->otherend_id
);
275 xen_unregister_device_domain_owner(dev
);
277 pciback_release_pci_dev(pdev
, dev
);
283 static int pciback_publish_pci_root(struct pciback_device
*pdev
,
284 unsigned int domain
, unsigned int bus
)
287 int i
, root_num
, len
, err
;
290 dev_dbg(&pdev
->xdev
->dev
, "Publishing pci roots\n");
292 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->nodename
,
293 "root_num", "%d", &root_num
);
294 if (err
== 0 || err
== -ENOENT
)
299 /* Verify that we haven't already published this pci root */
300 for (i
= 0; i
< root_num
; i
++) {
301 len
= snprintf(str
, sizeof(str
), "root-%d", i
);
302 if (unlikely(len
>= (sizeof(str
) - 1))) {
307 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->nodename
,
308 str
, "%x:%x", &d
, &b
);
316 if (d
== domain
&& b
== bus
) {
322 len
= snprintf(str
, sizeof(str
), "root-%d", root_num
);
323 if (unlikely(len
>= (sizeof(str
) - 1))) {
328 dev_dbg(&pdev
->xdev
->dev
, "writing root %d at %04x:%02x\n",
329 root_num
, domain
, bus
);
331 err
= xenbus_printf(XBT_NIL
, pdev
->xdev
->nodename
, str
,
332 "%04x:%02x", domain
, bus
);
336 err
= xenbus_printf(XBT_NIL
, pdev
->xdev
->nodename
,
337 "root_num", "%d", (root_num
+ 1));
343 static int pciback_reconfigure(struct pciback_device
*pdev
)
347 int domain
, bus
, slot
, func
;
354 dev_dbg(&pdev
->xdev
->dev
, "Reconfiguring device ...\n");
356 /* Make sure we only reconfigure once */
357 if (xenbus_read_driver_state(pdev
->xdev
->nodename
) !=
358 XenbusStateReconfiguring
)
361 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->nodename
, "num_devs", "%d",
366 xenbus_dev_fatal(pdev
->xdev
, err
,
367 "Error reading number of devices");
371 for (i
= 0; i
< num_devs
; i
++) {
372 len
= snprintf(state_str
, sizeof(state_str
), "state-%d", i
);
373 if (unlikely(len
>= (sizeof(state_str
) - 1))) {
375 xenbus_dev_fatal(pdev
->xdev
, err
,
376 "String overflow while reading "
380 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->nodename
, state_str
,
383 substate
= XenbusStateUnknown
;
386 case XenbusStateInitialising
:
387 dev_dbg(&pdev
->xdev
->dev
, "Attaching dev-%d ...\n", i
);
389 len
= snprintf(dev_str
, sizeof(dev_str
), "dev-%d", i
);
390 if (unlikely(len
>= (sizeof(dev_str
) - 1))) {
392 xenbus_dev_fatal(pdev
->xdev
, err
,
393 "String overflow while "
394 "reading configuration");
397 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->nodename
,
398 dev_str
, "%x:%x:%x.%x",
399 &domain
, &bus
, &slot
, &func
);
401 xenbus_dev_fatal(pdev
->xdev
, err
,
402 "Error reading device "
408 xenbus_dev_fatal(pdev
->xdev
, err
,
409 "Error parsing pci device "
414 err
= pciback_export_device(pdev
, domain
, bus
, slot
,
419 /* Publish pci roots. */
420 err
= pciback_publish_pci_roots(pdev
,
421 pciback_publish_pci_root
);
423 xenbus_dev_fatal(pdev
->xdev
, err
,
424 "Error while publish PCI root"
425 "buses for frontend");
429 err
= xenbus_printf(XBT_NIL
, pdev
->xdev
->nodename
,
431 XenbusStateInitialised
);
433 xenbus_dev_fatal(pdev
->xdev
, err
,
434 "Error switching substate of "
440 case XenbusStateClosing
:
441 dev_dbg(&pdev
->xdev
->dev
, "Detaching dev-%d ...\n", i
);
443 len
= snprintf(dev_str
, sizeof(dev_str
), "vdev-%d", i
);
444 if (unlikely(len
>= (sizeof(dev_str
) - 1))) {
446 xenbus_dev_fatal(pdev
->xdev
, err
,
447 "String overflow while "
448 "reading configuration");
451 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->nodename
,
452 dev_str
, "%x:%x:%x.%x",
453 &domain
, &bus
, &slot
, &func
);
455 xenbus_dev_fatal(pdev
->xdev
, err
,
456 "Error reading device "
462 xenbus_dev_fatal(pdev
->xdev
, err
,
463 "Error parsing pci device "
468 err
= pciback_remove_device(pdev
, domain
, bus
, slot
,
473 /* TODO: If at some point we implement support for pci
474 * root hot-remove on pcifront side, we'll need to
475 * remove unnecessary xenstore nodes of pci roots here.
485 err
= xenbus_switch_state(pdev
->xdev
, XenbusStateReconfigured
);
487 xenbus_dev_fatal(pdev
->xdev
, err
,
488 "Error switching to reconfigured state!");
496 static void pciback_frontend_changed(struct xenbus_device
*xdev
,
497 enum xenbus_state fe_state
)
499 struct pciback_device
*pdev
= dev_get_drvdata(&xdev
->dev
);
501 dev_dbg(&xdev
->dev
, "fe state changed %d\n", fe_state
);
504 case XenbusStateInitialised
:
505 pciback_attach(pdev
);
508 case XenbusStateReconfiguring
:
509 pciback_reconfigure(pdev
);
512 case XenbusStateConnected
:
513 /* pcifront switched its state from reconfiguring to connected.
514 * Then switch to connected state.
516 xenbus_switch_state(xdev
, XenbusStateConnected
);
519 case XenbusStateClosing
:
520 pciback_disconnect(pdev
);
521 xenbus_switch_state(xdev
, XenbusStateClosing
);
524 case XenbusStateClosed
:
525 pciback_disconnect(pdev
);
526 xenbus_switch_state(xdev
, XenbusStateClosed
);
527 if (xenbus_dev_is_online(xdev
))
529 /* fall through if not online */
530 case XenbusStateUnknown
:
531 dev_dbg(&xdev
->dev
, "frontend is gone! unregister device\n");
532 device_unregister(&xdev
->dev
);
540 static int pciback_setup_backend(struct pciback_device
*pdev
)
542 /* Get configuration from xend (if available now) */
543 int domain
, bus
, slot
, func
;
549 /* It's possible we could get the call to setup twice, so make sure
550 * we're not already connected.
552 if (xenbus_read_driver_state(pdev
->xdev
->nodename
) !=
556 dev_dbg(&pdev
->xdev
->dev
, "getting be setup\n");
558 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->nodename
, "num_devs", "%d",
563 xenbus_dev_fatal(pdev
->xdev
, err
,
564 "Error reading number of devices");
568 for (i
= 0; i
< num_devs
; i
++) {
569 int l
= snprintf(dev_str
, sizeof(dev_str
), "dev-%d", i
);
570 if (unlikely(l
>= (sizeof(dev_str
) - 1))) {
572 xenbus_dev_fatal(pdev
->xdev
, err
,
573 "String overflow while reading "
578 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->nodename
, dev_str
,
579 "%x:%x:%x.%x", &domain
, &bus
, &slot
, &func
);
581 xenbus_dev_fatal(pdev
->xdev
, err
,
582 "Error reading device configuration");
587 xenbus_dev_fatal(pdev
->xdev
, err
,
588 "Error parsing pci device "
593 err
= pciback_export_device(pdev
, domain
, bus
, slot
, func
, i
);
597 /* Switch substate of this device. */
598 l
= snprintf(state_str
, sizeof(state_str
), "state-%d", i
);
599 if (unlikely(l
>= (sizeof(state_str
) - 1))) {
601 xenbus_dev_fatal(pdev
->xdev
, err
,
602 "String overflow while reading "
606 err
= xenbus_printf(XBT_NIL
, pdev
->xdev
->nodename
, state_str
,
607 "%d", XenbusStateInitialised
);
609 xenbus_dev_fatal(pdev
->xdev
, err
, "Error switching "
610 "substate of dev-%d\n", i
);
615 err
= pciback_publish_pci_roots(pdev
, pciback_publish_pci_root
);
617 xenbus_dev_fatal(pdev
->xdev
, err
,
618 "Error while publish PCI root buses "
623 err
= xenbus_switch_state(pdev
->xdev
, XenbusStateInitialised
);
625 xenbus_dev_fatal(pdev
->xdev
, err
,
626 "Error switching to initialised state!");
630 /* see if pcifront is already configured (if not, we'll wait) */
631 pciback_attach(pdev
);
636 static void pciback_be_watch(struct xenbus_watch
*watch
,
637 const char **vec
, unsigned int len
)
639 struct pciback_device
*pdev
=
640 container_of(watch
, struct pciback_device
, be_watch
);
642 switch (xenbus_read_driver_state(pdev
->xdev
->nodename
)) {
643 case XenbusStateInitWait
:
644 pciback_setup_backend(pdev
);
652 static int pciback_xenbus_probe(struct xenbus_device
*dev
,
653 const struct xenbus_device_id
*id
)
656 struct pciback_device
*pdev
= alloc_pdev(dev
);
660 xenbus_dev_fatal(dev
, err
,
661 "Error allocating pciback_device struct");
665 /* wait for xend to configure us */
666 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
670 /* watch the backend node for backend configuration information */
671 err
= xenbus_watch_path(dev
, dev
->nodename
, &pdev
->be_watch
,
676 pdev
->be_watching
= 1;
678 /* We need to force a call to our callback here in case
679 * xend already configured us!
681 pciback_be_watch(&pdev
->be_watch
, NULL
, 0);
687 static int pciback_xenbus_remove(struct xenbus_device
*dev
)
689 struct pciback_device
*pdev
= dev_get_drvdata(&dev
->dev
);
697 static const struct xenbus_device_id xenpci_ids
[] = {
702 static struct xenbus_driver xenbus_pciback_driver
= {
704 .owner
= THIS_MODULE
,
706 .probe
= pciback_xenbus_probe
,
707 .remove
= pciback_xenbus_remove
,
708 .otherend_changed
= pciback_frontend_changed
,
711 int __init
pciback_xenbus_register(void)
713 pciback_wq
= create_workqueue("pciback_workqueue");
715 printk(KERN_ERR
"%s: create"
716 "pciback_workqueue failed\n", __func__
);
719 return xenbus_register_backend(&xenbus_pciback_driver
);
722 void __exit
pciback_xenbus_unregister(void)
724 destroy_workqueue(pciback_wq
);
725 xenbus_unregister_driver(&xenbus_pciback_driver
);