drm/radeon/kms: Add an MSI quirk for Dell RS690
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / xen / xen-pciback / xenbus.c
blob075525945e36d406c15f2598e7c0b0e4f8e43385
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 "pciback.h"
16 #define INVALID_EVTCHN_IRQ (-1)
17 struct workqueue_struct *xen_pcibk_wq;
19 static int __read_mostly passthrough;
20 module_param(passthrough, bool, S_IRUGO);
21 MODULE_PARM_DESC(passthrough,
22 "Option to specify how to export PCI topology to guest:\n"\
23 " 0 - (default) Hide the true PCI topology and makes the frontend\n"\
24 " there is a single PCI bus with only the exported devices on it.\n"\
25 " For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
26 " while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
27 " 1 - Passthrough provides a real view of the PCI topology to the\n"\
28 " frontend (for example, a device at 06:01.b will still appear at\n"\
29 " 06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
30 " exposed PCI devices to its driver domains. This may be required\n"\
31 " for drivers which depend on finding their hardward in certain\n"\
32 " bus/slot locations.");
34 static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
36 struct xen_pcibk_device *pdev;
38 pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
39 if (pdev == NULL)
40 goto out;
41 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
43 pdev->xdev = xdev;
44 dev_set_drvdata(&xdev->dev, pdev);
46 mutex_init(&pdev->dev_lock);
48 pdev->sh_info = NULL;
49 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
50 pdev->be_watching = 0;
52 INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
54 if (xen_pcibk_init_devices(pdev)) {
55 kfree(pdev);
56 pdev = NULL;
58 out:
59 return pdev;
62 static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
64 mutex_lock(&pdev->dev_lock);
65 /* Ensure the guest can't trigger our handler before removing devices */
66 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
67 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
68 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
71 /* If the driver domain started an op, make sure we complete it
72 * before releasing the shared memory */
74 /* Note, the workqueue does not use spinlocks at all.*/
75 flush_workqueue(xen_pcibk_wq);
77 if (pdev->sh_info != NULL) {
78 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
79 pdev->sh_info = NULL;
81 mutex_unlock(&pdev->dev_lock);
84 static void free_pdev(struct xen_pcibk_device *pdev)
86 if (pdev->be_watching) {
87 unregister_xenbus_watch(&pdev->be_watch);
88 pdev->be_watching = 0;
91 xen_pcibk_disconnect(pdev);
93 xen_pcibk_release_devices(pdev);
95 dev_set_drvdata(&pdev->xdev->dev, NULL);
96 pdev->xdev = NULL;
98 kfree(pdev);
101 static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
102 int remote_evtchn)
104 int err = 0;
105 void *vaddr;
107 dev_dbg(&pdev->xdev->dev,
108 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
109 gnt_ref, remote_evtchn);
111 err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
112 if (err < 0) {
113 xenbus_dev_fatal(pdev->xdev, err,
114 "Error mapping other domain page in ours.");
115 goto out;
118 pdev->sh_info = vaddr;
120 err = bind_interdomain_evtchn_to_irqhandler(
121 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
122 0, DRV_NAME, pdev);
123 if (err < 0) {
124 xenbus_dev_fatal(pdev->xdev, err,
125 "Error binding event channel to IRQ");
126 goto out;
128 pdev->evtchn_irq = err;
129 err = 0;
131 dev_dbg(&pdev->xdev->dev, "Attached!\n");
132 out:
133 return err;
136 static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
138 int err = 0;
139 int gnt_ref, remote_evtchn;
140 char *magic = NULL;
143 mutex_lock(&pdev->dev_lock);
144 /* Make sure we only do this setup once */
145 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
146 XenbusStateInitialised)
147 goto out;
149 /* Wait for frontend to state that it has published the configuration */
150 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
151 XenbusStateInitialised)
152 goto out;
154 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
156 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
157 "pci-op-ref", "%u", &gnt_ref,
158 "event-channel", "%u", &remote_evtchn,
159 "magic", NULL, &magic, NULL);
160 if (err) {
161 /* If configuration didn't get read correctly, wait longer */
162 xenbus_dev_fatal(pdev->xdev, err,
163 "Error reading configuration from frontend");
164 goto out;
167 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
168 xenbus_dev_fatal(pdev->xdev, -EFAULT,
169 "version mismatch (%s/%s) with pcifront - "
170 "halting " DRV_NAME,
171 magic, XEN_PCI_MAGIC);
172 goto out;
175 err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
176 if (err)
177 goto out;
179 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
181 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
182 if (err)
183 xenbus_dev_fatal(pdev->xdev, err,
184 "Error switching to connected state!");
186 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
187 out:
188 mutex_unlock(&pdev->dev_lock);
190 kfree(magic);
192 return err;
195 static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
196 unsigned int domain, unsigned int bus,
197 unsigned int devfn, unsigned int devid)
199 int err;
200 int len;
201 char str[64];
203 len = snprintf(str, sizeof(str), "vdev-%d", devid);
204 if (unlikely(len >= (sizeof(str) - 1))) {
205 err = -ENOMEM;
206 goto out;
209 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
210 "%04x:%02x:%02x.%02x", domain, bus,
211 PCI_SLOT(devfn), PCI_FUNC(devfn));
213 out:
214 return err;
217 static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
218 int domain, int bus, int slot, int func,
219 int devid)
221 struct pci_dev *dev;
222 int err = 0;
224 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
225 domain, bus, slot, func);
227 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
228 if (!dev) {
229 err = -EINVAL;
230 xenbus_dev_fatal(pdev->xdev, err,
231 "Couldn't locate PCI device "
232 "(%04x:%02x:%02x.%01x)! "
233 "perhaps already in-use?",
234 domain, bus, slot, func);
235 goto out;
238 err = xen_pcibk_add_pci_dev(pdev, dev, devid,
239 xen_pcibk_publish_pci_dev);
240 if (err)
241 goto out;
243 dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
244 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
245 if (xen_register_device_domain_owner(dev,
246 pdev->xdev->otherend_id) != 0) {
247 dev_err(&dev->dev, "device has been assigned to another " \
248 "domain! Over-writting the ownership, but beware.\n");
249 xen_unregister_device_domain_owner(dev);
250 xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
253 /* TODO: It'd be nice to export a bridge and have all of its children
254 * get exported with it. This may be best done in xend (which will
255 * have to calculate resource usage anyway) but we probably want to
256 * put something in here to ensure that if a bridge gets given to a
257 * driver domain, that all devices under that bridge are not given
258 * to other driver domains (as he who controls the bridge can disable
259 * it and stop the other devices from working).
261 out:
262 return err;
265 static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
266 int domain, int bus, int slot, int func)
268 int err = 0;
269 struct pci_dev *dev;
271 dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
272 domain, bus, slot, func);
274 dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
275 if (!dev) {
276 err = -EINVAL;
277 dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
278 "(%04x:%02x:%02x.%01x)! not owned by this domain\n",
279 domain, bus, slot, func);
280 goto out;
283 dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
284 dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
285 xen_unregister_device_domain_owner(dev);
287 xen_pcibk_release_pci_dev(pdev, dev);
289 out:
290 return err;
293 static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
294 unsigned int domain, unsigned int bus)
296 unsigned int d, b;
297 int i, root_num, len, err;
298 char str[64];
300 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
302 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
303 "root_num", "%d", &root_num);
304 if (err == 0 || err == -ENOENT)
305 root_num = 0;
306 else if (err < 0)
307 goto out;
309 /* Verify that we haven't already published this pci root */
310 for (i = 0; i < root_num; i++) {
311 len = snprintf(str, sizeof(str), "root-%d", i);
312 if (unlikely(len >= (sizeof(str) - 1))) {
313 err = -ENOMEM;
314 goto out;
317 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
318 str, "%x:%x", &d, &b);
319 if (err < 0)
320 goto out;
321 if (err != 2) {
322 err = -EINVAL;
323 goto out;
326 if (d == domain && b == bus) {
327 err = 0;
328 goto out;
332 len = snprintf(str, sizeof(str), "root-%d", root_num);
333 if (unlikely(len >= (sizeof(str) - 1))) {
334 err = -ENOMEM;
335 goto out;
338 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
339 root_num, domain, bus);
341 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
342 "%04x:%02x", domain, bus);
343 if (err)
344 goto out;
346 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
347 "root_num", "%d", (root_num + 1));
349 out:
350 return err;
353 static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
355 int err = 0;
356 int num_devs;
357 int domain, bus, slot, func;
358 int substate;
359 int i, len;
360 char state_str[64];
361 char dev_str[64];
364 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
366 mutex_lock(&pdev->dev_lock);
367 /* Make sure we only reconfigure once */
368 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
369 XenbusStateReconfiguring)
370 goto out;
372 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
373 &num_devs);
374 if (err != 1) {
375 if (err >= 0)
376 err = -EINVAL;
377 xenbus_dev_fatal(pdev->xdev, err,
378 "Error reading number of devices");
379 goto out;
382 for (i = 0; i < num_devs; i++) {
383 len = snprintf(state_str, sizeof(state_str), "state-%d", i);
384 if (unlikely(len >= (sizeof(state_str) - 1))) {
385 err = -ENOMEM;
386 xenbus_dev_fatal(pdev->xdev, err,
387 "String overflow while reading "
388 "configuration");
389 goto out;
391 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
392 "%d", &substate);
393 if (err != 1)
394 substate = XenbusStateUnknown;
396 switch (substate) {
397 case XenbusStateInitialising:
398 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
400 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
401 if (unlikely(len >= (sizeof(dev_str) - 1))) {
402 err = -ENOMEM;
403 xenbus_dev_fatal(pdev->xdev, err,
404 "String overflow while "
405 "reading configuration");
406 goto out;
408 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
409 dev_str, "%x:%x:%x.%x",
410 &domain, &bus, &slot, &func);
411 if (err < 0) {
412 xenbus_dev_fatal(pdev->xdev, err,
413 "Error reading device "
414 "configuration");
415 goto out;
417 if (err != 4) {
418 err = -EINVAL;
419 xenbus_dev_fatal(pdev->xdev, err,
420 "Error parsing pci device "
421 "configuration");
422 goto out;
425 err = xen_pcibk_export_device(pdev, domain, bus, slot,
426 func, i);
427 if (err)
428 goto out;
430 /* Publish pci roots. */
431 err = xen_pcibk_publish_pci_roots(pdev,
432 xen_pcibk_publish_pci_root);
433 if (err) {
434 xenbus_dev_fatal(pdev->xdev, err,
435 "Error while publish PCI root"
436 "buses for frontend");
437 goto out;
440 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
441 state_str, "%d",
442 XenbusStateInitialised);
443 if (err) {
444 xenbus_dev_fatal(pdev->xdev, err,
445 "Error switching substate of "
446 "dev-%d\n", i);
447 goto out;
449 break;
451 case XenbusStateClosing:
452 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
454 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
455 if (unlikely(len >= (sizeof(dev_str) - 1))) {
456 err = -ENOMEM;
457 xenbus_dev_fatal(pdev->xdev, err,
458 "String overflow while "
459 "reading configuration");
460 goto out;
462 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
463 dev_str, "%x:%x:%x.%x",
464 &domain, &bus, &slot, &func);
465 if (err < 0) {
466 xenbus_dev_fatal(pdev->xdev, err,
467 "Error reading device "
468 "configuration");
469 goto out;
471 if (err != 4) {
472 err = -EINVAL;
473 xenbus_dev_fatal(pdev->xdev, err,
474 "Error parsing pci device "
475 "configuration");
476 goto out;
479 err = xen_pcibk_remove_device(pdev, domain, bus, slot,
480 func);
481 if (err)
482 goto out;
484 /* TODO: If at some point we implement support for pci
485 * root hot-remove on pcifront side, we'll need to
486 * remove unnecessary xenstore nodes of pci roots here.
489 break;
491 default:
492 break;
496 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
497 if (err) {
498 xenbus_dev_fatal(pdev->xdev, err,
499 "Error switching to reconfigured state!");
500 goto out;
503 out:
504 mutex_unlock(&pdev->dev_lock);
505 return 0;
508 static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
509 enum xenbus_state fe_state)
511 struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
513 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
515 switch (fe_state) {
516 case XenbusStateInitialised:
517 xen_pcibk_attach(pdev);
518 break;
520 case XenbusStateReconfiguring:
521 xen_pcibk_reconfigure(pdev);
522 break;
524 case XenbusStateConnected:
525 /* pcifront switched its state from reconfiguring to connected.
526 * Then switch to connected state.
528 xenbus_switch_state(xdev, XenbusStateConnected);
529 break;
531 case XenbusStateClosing:
532 xen_pcibk_disconnect(pdev);
533 xenbus_switch_state(xdev, XenbusStateClosing);
534 break;
536 case XenbusStateClosed:
537 xen_pcibk_disconnect(pdev);
538 xenbus_switch_state(xdev, XenbusStateClosed);
539 if (xenbus_dev_is_online(xdev))
540 break;
541 /* fall through if not online */
542 case XenbusStateUnknown:
543 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
544 device_unregister(&xdev->dev);
545 break;
547 default:
548 break;
552 static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
554 /* Get configuration from xend (if available now) */
555 int domain, bus, slot, func;
556 int err = 0;
557 int i, num_devs;
558 char dev_str[64];
559 char state_str[64];
561 mutex_lock(&pdev->dev_lock);
562 /* It's possible we could get the call to setup twice, so make sure
563 * we're not already connected.
565 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
566 XenbusStateInitWait)
567 goto out;
569 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
571 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
572 &num_devs);
573 if (err != 1) {
574 if (err >= 0)
575 err = -EINVAL;
576 xenbus_dev_fatal(pdev->xdev, err,
577 "Error reading number of devices");
578 goto out;
581 for (i = 0; i < num_devs; i++) {
582 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
583 if (unlikely(l >= (sizeof(dev_str) - 1))) {
584 err = -ENOMEM;
585 xenbus_dev_fatal(pdev->xdev, err,
586 "String overflow while reading "
587 "configuration");
588 goto out;
591 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
592 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
593 if (err < 0) {
594 xenbus_dev_fatal(pdev->xdev, err,
595 "Error reading device configuration");
596 goto out;
598 if (err != 4) {
599 err = -EINVAL;
600 xenbus_dev_fatal(pdev->xdev, err,
601 "Error parsing pci device "
602 "configuration");
603 goto out;
606 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
607 if (err)
608 goto out;
610 /* Switch substate of this device. */
611 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
612 if (unlikely(l >= (sizeof(state_str) - 1))) {
613 err = -ENOMEM;
614 xenbus_dev_fatal(pdev->xdev, err,
615 "String overflow while reading "
616 "configuration");
617 goto out;
619 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
620 "%d", XenbusStateInitialised);
621 if (err) {
622 xenbus_dev_fatal(pdev->xdev, err, "Error switching "
623 "substate of dev-%d\n", i);
624 goto out;
628 err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
629 if (err) {
630 xenbus_dev_fatal(pdev->xdev, err,
631 "Error while publish PCI root buses "
632 "for frontend");
633 goto out;
636 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
637 if (err)
638 xenbus_dev_fatal(pdev->xdev, err,
639 "Error switching to initialised state!");
641 out:
642 mutex_unlock(&pdev->dev_lock);
643 if (!err)
644 /* see if pcifront is already configured (if not, we'll wait) */
645 xen_pcibk_attach(pdev);
646 return err;
649 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
650 const char **vec, unsigned int len)
652 struct xen_pcibk_device *pdev =
653 container_of(watch, struct xen_pcibk_device, be_watch);
655 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
656 case XenbusStateInitWait:
657 xen_pcibk_setup_backend(pdev);
658 break;
660 default:
661 break;
665 static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
666 const struct xenbus_device_id *id)
668 int err = 0;
669 struct xen_pcibk_device *pdev = alloc_pdev(dev);
671 if (pdev == NULL) {
672 err = -ENOMEM;
673 xenbus_dev_fatal(dev, err,
674 "Error allocating xen_pcibk_device struct");
675 goto out;
678 /* wait for xend to configure us */
679 err = xenbus_switch_state(dev, XenbusStateInitWait);
680 if (err)
681 goto out;
683 /* watch the backend node for backend configuration information */
684 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
685 xen_pcibk_be_watch);
686 if (err)
687 goto out;
689 pdev->be_watching = 1;
691 /* We need to force a call to our callback here in case
692 * xend already configured us!
694 xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
696 out:
697 return err;
700 static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
702 struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
704 if (pdev != NULL)
705 free_pdev(pdev);
707 return 0;
710 static const struct xenbus_device_id xenpci_ids[] = {
711 {"pci"},
712 {""},
715 static struct xenbus_driver xenbus_xen_pcibk_driver = {
716 .name = DRV_NAME,
717 .owner = THIS_MODULE,
718 .ids = xenpci_ids,
719 .probe = xen_pcibk_xenbus_probe,
720 .remove = xen_pcibk_xenbus_remove,
721 .otherend_changed = xen_pcibk_frontend_changed,
724 const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
726 int __init xen_pcibk_xenbus_register(void)
728 xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
729 if (!xen_pcibk_wq) {
730 printk(KERN_ERR "%s: create"
731 "xen_pciback_workqueue failed\n", __func__);
732 return -EFAULT;
734 xen_pcibk_backend = &xen_pcibk_vpci_backend;
735 if (passthrough)
736 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
737 pr_info(DRV_NAME ": backend is %s\n", xen_pcibk_backend->name);
738 return xenbus_register_backend(&xenbus_xen_pcibk_driver);
741 void __exit xen_pcibk_xenbus_unregister(void)
743 destroy_workqueue(xen_pcibk_wq);
744 xenbus_unregister_driver(&xenbus_xen_pcibk_driver);