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 <xen/xenbus.h>
11 #include <xen/evtchn.h>
14 #define INVALID_EVTCHN_IRQ (-1)
16 static struct pciback_device *alloc_pdev(struct xenbus_device *xdev)
18 struct pciback_device *pdev;
20 pdev = kzalloc(sizeof(struct pciback_device), GFP_KERNEL);
23 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
26 xdev->dev.driver_data = pdev;
28 spin_lock_init(&pdev->dev_lock);
32 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
33 pdev->be_watching = 0;
35 INIT_DELAYED_WORK(&pdev->op_work, pciback_do_op);
37 if (pciback_init_devices(pdev)) {
45 static void free_pdev(struct pciback_device *pdev)
47 if (pdev->be_watching)
48 unregister_xenbus_watch(&pdev->be_watch);
50 /* Ensure the guest can't trigger our handler before removing devices */
51 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ)
52 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
54 /* If the driver domain started an op, make sure we complete it or
55 * delete it before releasing the shared memory */
56 cancel_delayed_work(&pdev->op_work);
57 flush_scheduled_work();
60 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_area);
62 pciback_release_devices(pdev);
64 pdev->xdev->dev.driver_data = NULL;
70 static int pciback_do_attach(struct pciback_device *pdev, int gnt_ref,
75 struct vm_struct *area;
77 dev_dbg(&pdev->xdev->dev,
78 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
79 gnt_ref, remote_evtchn);
81 area = xenbus_map_ring_valloc(pdev->xdev, gnt_ref);
87 pdev->sh_info = area->addr;
89 err = xenbus_bind_evtchn(pdev->xdev, remote_evtchn, &evtchn);
93 err = bind_evtchn_to_irqhandler(evtchn, pciback_handle_event,
94 SA_SAMPLE_RANDOM, "pciback", pdev);
96 xenbus_dev_fatal(pdev->xdev, err,
97 "Error binding event channel to IRQ");
100 pdev->evtchn_irq = err;
103 dev_dbg(&pdev->xdev->dev, "Attached!\n");
108 static int pciback_attach(struct pciback_device *pdev)
111 int gnt_ref, remote_evtchn;
114 spin_lock(&pdev->dev_lock);
116 /* Make sure we only do this setup once */
117 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
118 XenbusStateInitialised)
121 /* Wait for frontend to state that it has published the configuration */
122 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
123 XenbusStateInitialised)
126 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
128 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
129 "pci-op-ref", "%u", &gnt_ref,
130 "event-channel", "%u", &remote_evtchn,
131 "magic", NULL, &magic, NULL);
133 /* If configuration didn't get read correctly, wait longer */
134 xenbus_dev_fatal(pdev->xdev, err,
135 "Error reading configuration from frontend");
139 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
140 xenbus_dev_fatal(pdev->xdev, -EFAULT,
141 "version mismatch (%s/%s) with pcifront - "
143 magic, XEN_PCI_MAGIC);
147 err = pciback_do_attach(pdev, gnt_ref, remote_evtchn);
151 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
153 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
155 xenbus_dev_fatal(pdev->xdev, err,
156 "Error switching to connected state!");
158 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
160 spin_unlock(&pdev->dev_lock);
168 static void pciback_frontend_changed(struct xenbus_device *xdev,
169 enum xenbus_state fe_state)
171 struct pciback_device *pdev = xdev->dev.driver_data;
173 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
176 case XenbusStateInitialised:
177 pciback_attach(pdev);
180 case XenbusStateClosing:
181 xenbus_switch_state(xdev, XenbusStateClosing);
184 case XenbusStateUnknown:
185 case XenbusStateClosed:
186 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
187 device_unregister(&xdev->dev);
195 static int pciback_publish_pci_root(struct pciback_device *pdev,
196 unsigned int domain, unsigned int bus)
199 int i, root_num, len, err;
202 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
204 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
205 "root_num", "%d", &root_num);
206 if (err == 0 || err == -ENOENT)
211 /* Verify that we haven't already published this pci root */
212 for (i = 0; i < root_num; i++) {
213 len = snprintf(str, sizeof(str), "root-%d", i);
214 if (unlikely(len >= (sizeof(str) - 1))) {
219 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
220 str, "%x:%x", &d, &b);
228 if (d == domain && b == bus) {
234 len = snprintf(str, sizeof(str), "root-%d", root_num);
235 if (unlikely(len >= (sizeof(str) - 1))) {
240 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
241 root_num, domain, bus);
243 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
244 "%04x:%02x", domain, bus);
248 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
249 "root_num", "%d", (root_num + 1));
255 static int pciback_export_device(struct pciback_device *pdev,
256 int domain, int bus, int slot, int func)
261 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
262 domain, bus, slot, func);
264 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
267 xenbus_dev_fatal(pdev->xdev, err,
268 "Couldn't locate PCI device "
269 "(%04x:%02x:%02x.%01x)! "
270 "perhaps already in-use?",
271 domain, bus, slot, func);
275 err = pciback_add_pci_dev(pdev, dev);
279 /* TODO: It'd be nice to export a bridge and have all of its children
280 * get exported with it. This may be best done in xend (which will
281 * have to calculate resource usage anyway) but we probably want to
282 * put something in here to ensure that if a bridge gets given to a
283 * driver domain, that all devices under that bridge are not given
284 * to other driver domains (as he who controls the bridge can disable
285 * it and stop the other devices from working).
291 static int pciback_setup_backend(struct pciback_device *pdev)
293 /* Get configuration from xend (if available now) */
294 int domain, bus, slot, func;
299 spin_lock(&pdev->dev_lock);
301 /* It's possible we could get the call to setup twice, so make sure
302 * we're not already connected.
304 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
308 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
310 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
315 xenbus_dev_fatal(pdev->xdev, err,
316 "Error reading number of devices");
320 for (i = 0; i < num_devs; i++) {
321 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
322 if (unlikely(l >= (sizeof(dev_str) - 1))) {
324 xenbus_dev_fatal(pdev->xdev, err,
325 "String overflow while reading "
330 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
331 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
333 xenbus_dev_fatal(pdev->xdev, err,
334 "Error reading device configuration");
339 xenbus_dev_fatal(pdev->xdev, err,
340 "Error parsing pci device "
345 err = pciback_export_device(pdev, domain, bus, slot, func);
350 err = pciback_publish_pci_roots(pdev, pciback_publish_pci_root);
352 xenbus_dev_fatal(pdev->xdev, err,
353 "Error while publish PCI root buses "
358 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
360 xenbus_dev_fatal(pdev->xdev, err,
361 "Error switching to initialised state!");
364 spin_unlock(&pdev->dev_lock);
367 /* see if pcifront is already configured (if not, we'll wait) */
368 pciback_attach(pdev);
373 static void pciback_be_watch(struct xenbus_watch *watch,
374 const char **vec, unsigned int len)
376 struct pciback_device *pdev =
377 container_of(watch, struct pciback_device, be_watch);
379 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
380 case XenbusStateInitWait:
381 pciback_setup_backend(pdev);
389 static int pciback_xenbus_probe(struct xenbus_device *dev,
390 const struct xenbus_device_id *id)
393 struct pciback_device *pdev = alloc_pdev(dev);
397 xenbus_dev_fatal(dev, err,
398 "Error allocating pciback_device struct");
402 /* wait for xend to configure us */
403 err = xenbus_switch_state(dev, XenbusStateInitWait);
407 /* watch the backend node for backend configuration information */
408 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
412 pdev->be_watching = 1;
414 /* We need to force a call to our callback here in case
415 * xend already configured us!
417 pciback_be_watch(&pdev->be_watch, NULL, 0);
423 static int pciback_xenbus_remove(struct xenbus_device *dev)
425 struct pciback_device *pdev = dev->dev.driver_data;
433 static struct xenbus_device_id xenpci_ids[] = {
438 static struct xenbus_driver xenbus_pciback_driver = {
440 .owner = THIS_MODULE,
442 .probe = pciback_xenbus_probe,
443 .remove = pciback_xenbus_remove,
444 .otherend_changed = pciback_frontend_changed,
447 int __init pciback_xenbus_register(void)
449 if (!is_running_on_xen())
452 return xenbus_register_backend(&xenbus_pciback_driver);
455 void __exit pciback_xenbus_unregister(void)
457 xenbus_unregister_driver(&xenbus_pciback_driver);