xen/pciback: Fine-grain the spinlocks and fix BUG: scheduling while atomic cases.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / xen / xen-pciback / xenbus.c
blob70030c4092128c736f2ab087a21991fc213daf6e
1 /*
2 * PCI Backend Xenbus Setup - handles setup with frontend and xend
4 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
5 */
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>
15 #include "pciback.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);
25 if (pdev == NULL)
26 goto out;
27 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
29 pdev->xdev = xdev;
30 dev_set_drvdata(&xdev->dev, pdev);
32 spin_lock_init(&pdev->dev_lock);
34 pdev->sh_info = NULL;
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)) {
41 kfree(pdev);
42 pdev = NULL;
44 out:
45 return 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);
68 pdev->sh_info = NULL;
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);
86 pdev->xdev = NULL;
88 kfree(pdev);
91 static int pciback_do_attach(struct pciback_device *pdev, int gnt_ref,
92 int remote_evtchn)
94 int err = 0;
95 void *vaddr;
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);
102 if (err < 0) {
103 xenbus_dev_fatal(pdev->xdev, err,
104 "Error mapping other domain page in ours.");
105 goto out;
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,
114 0, "pciback", pdev);
115 if (err < 0) {
116 xenbus_dev_fatal(pdev->xdev, err,
117 "Error binding event channel to IRQ");
118 goto out;
121 spin_lock(&pdev->dev_lock);
122 pdev->evtchn_irq = err;
123 spin_unlock(&pdev->dev_lock);
124 err = 0;
126 dev_dbg(&pdev->xdev->dev, "Attached!\n");
127 out:
128 return err;
131 static int pciback_attach(struct pciback_device *pdev)
133 int err = 0;
134 int gnt_ref, remote_evtchn;
135 char *magic = NULL;
138 /* Make sure we only do this setup once */
139 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
140 XenbusStateInitialised)
141 goto out;
143 /* Wait for frontend to state that it has published the configuration */
144 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
145 XenbusStateInitialised)
146 goto out;
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);
154 if (err) {
155 /* If configuration didn't get read correctly, wait longer */
156 xenbus_dev_fatal(pdev->xdev, err,
157 "Error reading configuration from frontend");
158 goto out;
161 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
162 xenbus_dev_fatal(pdev->xdev, -EFAULT,
163 "version mismatch (%s/%s) with pcifront - "
164 "halting pciback",
165 magic, XEN_PCI_MAGIC);
166 goto out;
169 err = pciback_do_attach(pdev, gnt_ref, remote_evtchn);
170 if (err)
171 goto out;
173 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
175 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
176 if (err)
177 xenbus_dev_fatal(pdev->xdev, err,
178 "Error switching to connected state!");
180 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
181 out:
183 kfree(magic);
185 return 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)
192 int err;
193 int len;
194 char str[64];
196 len = snprintf(str, sizeof(str), "vdev-%d", devid);
197 if (unlikely(len >= (sizeof(str) - 1))) {
198 err = -ENOMEM;
199 goto out;
202 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
203 "%04x:%02x:%02x.%02x", domain, bus,
204 PCI_SLOT(devfn), PCI_FUNC(devfn));
206 out:
207 return err;
210 static int pciback_export_device(struct pciback_device *pdev,
211 int domain, int bus, int slot, int func,
212 int devid)
214 struct pci_dev *dev;
215 int err = 0;
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);
221 if (!dev) {
222 err = -EINVAL;
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);
228 goto out;
231 err = pciback_add_pci_dev(pdev, dev, devid, pciback_publish_pci_dev);
232 if (err)
233 goto out;
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).
252 out:
253 return err;
256 static int pciback_remove_device(struct pciback_device *pdev,
257 int domain, int bus, int slot, int func)
259 int err = 0;
260 struct pci_dev *dev;
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));
266 if (!dev) {
267 err = -EINVAL;
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);
271 goto out;
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);
279 out:
280 return err;
283 static int pciback_publish_pci_root(struct pciback_device *pdev,
284 unsigned int domain, unsigned int bus)
286 unsigned int d, b;
287 int i, root_num, len, err;
288 char str[64];
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)
295 root_num = 0;
296 else if (err < 0)
297 goto out;
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))) {
303 err = -ENOMEM;
304 goto out;
307 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
308 str, "%x:%x", &d, &b);
309 if (err < 0)
310 goto out;
311 if (err != 2) {
312 err = -EINVAL;
313 goto out;
316 if (d == domain && b == bus) {
317 err = 0;
318 goto out;
322 len = snprintf(str, sizeof(str), "root-%d", root_num);
323 if (unlikely(len >= (sizeof(str) - 1))) {
324 err = -ENOMEM;
325 goto out;
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);
333 if (err)
334 goto out;
336 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
337 "root_num", "%d", (root_num + 1));
339 out:
340 return err;
343 static int pciback_reconfigure(struct pciback_device *pdev)
345 int err = 0;
346 int num_devs;
347 int domain, bus, slot, func;
348 int substate;
349 int i, len;
350 char state_str[64];
351 char dev_str[64];
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)
359 goto out;
361 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
362 &num_devs);
363 if (err != 1) {
364 if (err >= 0)
365 err = -EINVAL;
366 xenbus_dev_fatal(pdev->xdev, err,
367 "Error reading number of devices");
368 goto out;
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))) {
374 err = -ENOMEM;
375 xenbus_dev_fatal(pdev->xdev, err,
376 "String overflow while reading "
377 "configuration");
378 goto out;
380 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
381 "%d", &substate);
382 if (err != 1)
383 substate = XenbusStateUnknown;
385 switch (substate) {
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))) {
391 err = -ENOMEM;
392 xenbus_dev_fatal(pdev->xdev, err,
393 "String overflow while "
394 "reading configuration");
395 goto out;
397 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
398 dev_str, "%x:%x:%x.%x",
399 &domain, &bus, &slot, &func);
400 if (err < 0) {
401 xenbus_dev_fatal(pdev->xdev, err,
402 "Error reading device "
403 "configuration");
404 goto out;
406 if (err != 4) {
407 err = -EINVAL;
408 xenbus_dev_fatal(pdev->xdev, err,
409 "Error parsing pci device "
410 "configuration");
411 goto out;
414 err = pciback_export_device(pdev, domain, bus, slot,
415 func, i);
416 if (err)
417 goto out;
419 /* Publish pci roots. */
420 err = pciback_publish_pci_roots(pdev,
421 pciback_publish_pci_root);
422 if (err) {
423 xenbus_dev_fatal(pdev->xdev, err,
424 "Error while publish PCI root"
425 "buses for frontend");
426 goto out;
429 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
430 state_str, "%d",
431 XenbusStateInitialised);
432 if (err) {
433 xenbus_dev_fatal(pdev->xdev, err,
434 "Error switching substate of "
435 "dev-%d\n", i);
436 goto out;
438 break;
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))) {
445 err = -ENOMEM;
446 xenbus_dev_fatal(pdev->xdev, err,
447 "String overflow while "
448 "reading configuration");
449 goto out;
451 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
452 dev_str, "%x:%x:%x.%x",
453 &domain, &bus, &slot, &func);
454 if (err < 0) {
455 xenbus_dev_fatal(pdev->xdev, err,
456 "Error reading device "
457 "configuration");
458 goto out;
460 if (err != 4) {
461 err = -EINVAL;
462 xenbus_dev_fatal(pdev->xdev, err,
463 "Error parsing pci device "
464 "configuration");
465 goto out;
468 err = pciback_remove_device(pdev, domain, bus, slot,
469 func);
470 if (err)
471 goto out;
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.
478 break;
480 default:
481 break;
485 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
486 if (err) {
487 xenbus_dev_fatal(pdev->xdev, err,
488 "Error switching to reconfigured state!");
489 goto out;
492 out:
493 return 0;
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);
503 switch (fe_state) {
504 case XenbusStateInitialised:
505 pciback_attach(pdev);
506 break;
508 case XenbusStateReconfiguring:
509 pciback_reconfigure(pdev);
510 break;
512 case XenbusStateConnected:
513 /* pcifront switched its state from reconfiguring to connected.
514 * Then switch to connected state.
516 xenbus_switch_state(xdev, XenbusStateConnected);
517 break;
519 case XenbusStateClosing:
520 pciback_disconnect(pdev);
521 xenbus_switch_state(xdev, XenbusStateClosing);
522 break;
524 case XenbusStateClosed:
525 pciback_disconnect(pdev);
526 xenbus_switch_state(xdev, XenbusStateClosed);
527 if (xenbus_dev_is_online(xdev))
528 break;
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);
533 break;
535 default:
536 break;
540 static int pciback_setup_backend(struct pciback_device *pdev)
542 /* Get configuration from xend (if available now) */
543 int domain, bus, slot, func;
544 int err = 0;
545 int i, num_devs;
546 char dev_str[64];
547 char state_str[64];
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) !=
553 XenbusStateInitWait)
554 goto out;
556 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
558 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
559 &num_devs);
560 if (err != 1) {
561 if (err >= 0)
562 err = -EINVAL;
563 xenbus_dev_fatal(pdev->xdev, err,
564 "Error reading number of devices");
565 goto out;
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))) {
571 err = -ENOMEM;
572 xenbus_dev_fatal(pdev->xdev, err,
573 "String overflow while reading "
574 "configuration");
575 goto out;
578 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
579 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
580 if (err < 0) {
581 xenbus_dev_fatal(pdev->xdev, err,
582 "Error reading device configuration");
583 goto out;
585 if (err != 4) {
586 err = -EINVAL;
587 xenbus_dev_fatal(pdev->xdev, err,
588 "Error parsing pci device "
589 "configuration");
590 goto out;
593 err = pciback_export_device(pdev, domain, bus, slot, func, i);
594 if (err)
595 goto out;
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))) {
600 err = -ENOMEM;
601 xenbus_dev_fatal(pdev->xdev, err,
602 "String overflow while reading "
603 "configuration");
604 goto out;
606 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
607 "%d", XenbusStateInitialised);
608 if (err) {
609 xenbus_dev_fatal(pdev->xdev, err, "Error switching "
610 "substate of dev-%d\n", i);
611 goto out;
615 err = pciback_publish_pci_roots(pdev, pciback_publish_pci_root);
616 if (err) {
617 xenbus_dev_fatal(pdev->xdev, err,
618 "Error while publish PCI root buses "
619 "for frontend");
620 goto out;
623 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
624 if (err)
625 xenbus_dev_fatal(pdev->xdev, err,
626 "Error switching to initialised state!");
628 out:
629 if (!err)
630 /* see if pcifront is already configured (if not, we'll wait) */
631 pciback_attach(pdev);
633 return err;
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);
645 break;
647 default:
648 break;
652 static int pciback_xenbus_probe(struct xenbus_device *dev,
653 const struct xenbus_device_id *id)
655 int err = 0;
656 struct pciback_device *pdev = alloc_pdev(dev);
658 if (pdev == NULL) {
659 err = -ENOMEM;
660 xenbus_dev_fatal(dev, err,
661 "Error allocating pciback_device struct");
662 goto out;
665 /* wait for xend to configure us */
666 err = xenbus_switch_state(dev, XenbusStateInitWait);
667 if (err)
668 goto out;
670 /* watch the backend node for backend configuration information */
671 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
672 pciback_be_watch);
673 if (err)
674 goto out;
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);
683 out:
684 return err;
687 static int pciback_xenbus_remove(struct xenbus_device *dev)
689 struct pciback_device *pdev = dev_get_drvdata(&dev->dev);
691 if (pdev != NULL)
692 free_pdev(pdev);
694 return 0;
697 static const struct xenbus_device_id xenpci_ids[] = {
698 {"pci"},
699 {""},
702 static struct xenbus_driver xenbus_pciback_driver = {
703 .name = "pciback",
704 .owner = THIS_MODULE,
705 .ids = xenpci_ids,
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");
714 if (!pciback_wq) {
715 printk(KERN_ERR "%s: create"
716 "pciback_workqueue failed\n", __func__);
717 return -EFAULT;
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);