xenbus.c 18.4 KB
Newer Older
1 2 3 4 5
/*
 * PCI Backend Xenbus Setup - handles setup with frontend and xend
 *
 *   Author: Ryan Wilson <hap9@epoch.ncsc.mil>
 */
J
Joe Perches 已提交
6 7 8

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

9 10 11 12 13 14 15
#include <linux/module.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/vmalloc.h>
#include <linux/workqueue.h>
#include <xen/xenbus.h>
#include <xen/events.h>
16
#include <asm/xen/pci.h>
17 18 19
#include "pciback.h"

#define INVALID_EVTCHN_IRQ  (-1)
20
struct workqueue_struct *xen_pcibk_wq;
21

22
static bool __read_mostly passthrough;
23 24 25 26 27 28 29 30 31 32 33 34 35 36
module_param(passthrough, bool, S_IRUGO);
MODULE_PARM_DESC(passthrough,
	"Option to specify how to export PCI topology to guest:\n"\
	" 0 - (default) Hide the true PCI topology and makes the frontend\n"\
	"   there is a single PCI bus with only the exported devices on it.\n"\
	"   For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
	"   while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
	" 1 - Passthrough provides a real view of the PCI topology to the\n"\
	"   frontend (for example, a device at 06:01.b will still appear at\n"\
	"   06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
	"   exposed PCI devices to its driver domains. This may be required\n"\
	"   for drivers which depend on finding their hardward in certain\n"\
	"   bus/slot locations.");

37
static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
38
{
39
	struct xen_pcibk_device *pdev;
40

41
	pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
42 43 44 45 46 47 48
	if (pdev == NULL)
		goto out;
	dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);

	pdev->xdev = xdev;
	dev_set_drvdata(&xdev->dev, pdev);

49
	mutex_init(&pdev->dev_lock);
50 51 52 53 54

	pdev->sh_info = NULL;
	pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
	pdev->be_watching = 0;

55
	INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
56

57
	if (xen_pcibk_init_devices(pdev)) {
58 59 60 61 62 63 64
		kfree(pdev);
		pdev = NULL;
	}
out:
	return pdev;
}

65
static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
66
{
67
	mutex_lock(&pdev->dev_lock);
68 69 70 71 72 73 74 75
	/* Ensure the guest can't trigger our handler before removing devices */
	if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
		unbind_from_irqhandler(pdev->evtchn_irq, pdev);
		pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
	}

	/* If the driver domain started an op, make sure we complete it
	 * before releasing the shared memory */
76 77

	/* Note, the workqueue does not use spinlocks at all.*/
78
	flush_workqueue(xen_pcibk_wq);
79 80 81 82 83

	if (pdev->sh_info != NULL) {
		xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
		pdev->sh_info = NULL;
	}
84
	mutex_unlock(&pdev->dev_lock);
85 86
}

87
static void free_pdev(struct xen_pcibk_device *pdev)
88
{
89
	if (pdev->be_watching) {
90
		unregister_xenbus_watch(&pdev->be_watch);
91 92
		pdev->be_watching = 0;
	}
93

94
	xen_pcibk_disconnect(pdev);
95

96 97
	/* N.B. This calls pcistub_put_pci_dev which does the FLR on all
	 * of the PCIe devices. */
98
	xen_pcibk_release_devices(pdev);
99 100 101 102 103 104 105

	dev_set_drvdata(&pdev->xdev->dev, NULL);
	pdev->xdev = NULL;

	kfree(pdev);
}

106
static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
			     int remote_evtchn)
{
	int err = 0;
	void *vaddr;

	dev_dbg(&pdev->xdev->dev,
		"Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
		gnt_ref, remote_evtchn);

	err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
	if (err < 0) {
		xenbus_dev_fatal(pdev->xdev, err,
				"Error mapping other domain page in ours.");
		goto out;
	}
122

123 124 125
	pdev->sh_info = vaddr;

	err = bind_interdomain_evtchn_to_irqhandler(
126 127
		pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
		0, DRV_NAME, pdev);
128 129 130 131 132 133 134 135 136 137 138 139 140
	if (err < 0) {
		xenbus_dev_fatal(pdev->xdev, err,
				 "Error binding event channel to IRQ");
		goto out;
	}
	pdev->evtchn_irq = err;
	err = 0;

	dev_dbg(&pdev->xdev->dev, "Attached!\n");
out:
	return err;
}

141
static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
142 143 144 145 146 147
{
	int err = 0;
	int gnt_ref, remote_evtchn;
	char *magic = NULL;


148
	mutex_lock(&pdev->dev_lock);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	/* Make sure we only do this setup once */
	if (xenbus_read_driver_state(pdev->xdev->nodename) !=
	    XenbusStateInitialised)
		goto out;

	/* Wait for frontend to state that it has published the configuration */
	if (xenbus_read_driver_state(pdev->xdev->otherend) !=
	    XenbusStateInitialised)
		goto out;

	dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");

	err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
			    "pci-op-ref", "%u", &gnt_ref,
			    "event-channel", "%u", &remote_evtchn,
			    "magic", NULL, &magic, NULL);
	if (err) {
		/* If configuration didn't get read correctly, wait longer */
		xenbus_dev_fatal(pdev->xdev, err,
				 "Error reading configuration from frontend");
		goto out;
	}

	if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
		xenbus_dev_fatal(pdev->xdev, -EFAULT,
				 "version mismatch (%s/%s) with pcifront - "
175
				 "halting " DRV_NAME,
176
				 magic, XEN_PCI_MAGIC);
177
		err = -EFAULT;
178 179 180
		goto out;
	}

181
	err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
182 183 184 185 186 187 188 189 190 191 192 193
	if (err)
		goto out;

	dev_dbg(&pdev->xdev->dev, "Connecting...\n");

	err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
	if (err)
		xenbus_dev_fatal(pdev->xdev, err,
				 "Error switching to connected state!");

	dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
out:
194
	mutex_unlock(&pdev->dev_lock);
195 196 197 198 199 200

	kfree(magic);

	return err;
}

201
static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
202 203 204 205 206 207 208 209 210 211 212 213 214
				   unsigned int domain, unsigned int bus,
				   unsigned int devfn, unsigned int devid)
{
	int err;
	int len;
	char str[64];

	len = snprintf(str, sizeof(str), "vdev-%d", devid);
	if (unlikely(len >= (sizeof(str) - 1))) {
		err = -ENOMEM;
		goto out;
	}

215
	/* Note: The PV protocol uses %02x, don't change it */
216 217 218 219 220 221 222 223
	err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
			    "%04x:%02x:%02x.%02x", domain, bus,
			    PCI_SLOT(devfn), PCI_FUNC(devfn));

out:
	return err;
}

224
static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
225 226 227 228 229 230 231 232 233 234 235 236 237 238
				 int domain, int bus, int slot, int func,
				 int devid)
{
	struct pci_dev *dev;
	int err = 0;

	dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
		domain, bus, slot, func);

	dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
	if (!dev) {
		err = -EINVAL;
		xenbus_dev_fatal(pdev->xdev, err,
				 "Couldn't locate PCI device "
239
				 "(%04x:%02x:%02x.%d)! "
240 241 242 243 244
				 "perhaps already in-use?",
				 domain, bus, slot, func);
		goto out;
	}

245 246
	err = xen_pcibk_add_pci_dev(pdev, dev, devid,
				    xen_pcibk_publish_pci_dev);
247 248 249
	if (err)
		goto out;

250 251 252
	dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
	if (xen_register_device_domain_owner(dev,
					     pdev->xdev->otherend_id) != 0) {
253 254
		dev_err(&dev->dev, "Stealing ownership from dom%d.\n",
			xen_find_device_domain_owner(dev));
255 256 257 258
		xen_unregister_device_domain_owner(dev);
		xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
	}

259 260 261 262 263 264 265 266 267 268 269 270
	/* TODO: It'd be nice to export a bridge and have all of its children
	 * get exported with it. This may be best done in xend (which will
	 * have to calculate resource usage anyway) but we probably want to
	 * put something in here to ensure that if a bridge gets given to a
	 * driver domain, that all devices under that bridge are not given
	 * to other driver domains (as he who controls the bridge can disable
	 * it and stop the other devices from working).
	 */
out:
	return err;
}

271
static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
272 273 274 275 276 277 278 279
				 int domain, int bus, int slot, int func)
{
	int err = 0;
	struct pci_dev *dev;

	dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
		domain, bus, slot, func);

280
	dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
281 282 283
	if (!dev) {
		err = -EINVAL;
		dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
284
			"(%04x:%02x:%02x.%d)! not owned by this domain\n",
285 286 287 288
			domain, bus, slot, func);
		goto out;
	}

289 290 291
	dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
	xen_unregister_device_domain_owner(dev);

292 293
	/* N.B. This ends up calling pcistub_put_pci_dev which ends up
	 * doing the FLR. */
294
	xen_pcibk_release_pci_dev(pdev, dev);
295 296 297 298 299

out:
	return err;
}

300
static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
				    unsigned int domain, unsigned int bus)
{
	unsigned int d, b;
	int i, root_num, len, err;
	char str[64];

	dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");

	err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
			   "root_num", "%d", &root_num);
	if (err == 0 || err == -ENOENT)
		root_num = 0;
	else if (err < 0)
		goto out;

	/* Verify that we haven't already published this pci root */
	for (i = 0; i < root_num; i++) {
		len = snprintf(str, sizeof(str), "root-%d", i);
		if (unlikely(len >= (sizeof(str) - 1))) {
			err = -ENOMEM;
			goto out;
		}

		err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
				   str, "%x:%x", &d, &b);
		if (err < 0)
			goto out;
		if (err != 2) {
			err = -EINVAL;
			goto out;
		}

		if (d == domain && b == bus) {
			err = 0;
			goto out;
		}
	}

	len = snprintf(str, sizeof(str), "root-%d", root_num);
	if (unlikely(len >= (sizeof(str) - 1))) {
		err = -ENOMEM;
		goto out;
	}

	dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
		root_num, domain, bus);

	err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
			    "%04x:%02x", domain, bus);
	if (err)
		goto out;

	err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
			    "root_num", "%d", (root_num + 1));

out:
	return err;
}

360
static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
361 362 363 364 365 366 367 368 369 370 371 372
{
	int err = 0;
	int num_devs;
	int domain, bus, slot, func;
	int substate;
	int i, len;
	char state_str[64];
	char dev_str[64];


	dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");

373
	mutex_lock(&pdev->dev_lock);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	/* Make sure we only reconfigure once */
	if (xenbus_read_driver_state(pdev->xdev->nodename) !=
	    XenbusStateReconfiguring)
		goto out;

	err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
			   &num_devs);
	if (err != 1) {
		if (err >= 0)
			err = -EINVAL;
		xenbus_dev_fatal(pdev->xdev, err,
				 "Error reading number of devices");
		goto out;
	}

	for (i = 0; i < num_devs; i++) {
		len = snprintf(state_str, sizeof(state_str), "state-%d", i);
		if (unlikely(len >= (sizeof(state_str) - 1))) {
			err = -ENOMEM;
			xenbus_dev_fatal(pdev->xdev, err,
					 "String overflow while reading "
					 "configuration");
			goto out;
		}
		err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
				   "%d", &substate);
		if (err != 1)
			substate = XenbusStateUnknown;

		switch (substate) {
		case XenbusStateInitialising:
			dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);

			len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
			if (unlikely(len >= (sizeof(dev_str) - 1))) {
				err = -ENOMEM;
				xenbus_dev_fatal(pdev->xdev, err,
						 "String overflow while "
						 "reading configuration");
				goto out;
			}
			err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
					   dev_str, "%x:%x:%x.%x",
					   &domain, &bus, &slot, &func);
			if (err < 0) {
				xenbus_dev_fatal(pdev->xdev, err,
						 "Error reading device "
						 "configuration");
				goto out;
			}
			if (err != 4) {
				err = -EINVAL;
				xenbus_dev_fatal(pdev->xdev, err,
						 "Error parsing pci device "
						 "configuration");
				goto out;
			}

432
			err = xen_pcibk_export_device(pdev, domain, bus, slot,
433 434 435 436 437
						    func, i);
			if (err)
				goto out;

			/* Publish pci roots. */
438 439
			err = xen_pcibk_publish_pci_roots(pdev,
						xen_pcibk_publish_pci_root);
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
			if (err) {
				xenbus_dev_fatal(pdev->xdev, err,
						 "Error while publish PCI root"
						 "buses for frontend");
				goto out;
			}

			err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
					    state_str, "%d",
					    XenbusStateInitialised);
			if (err) {
				xenbus_dev_fatal(pdev->xdev, err,
						 "Error switching substate of "
						 "dev-%d\n", i);
				goto out;
			}
			break;

		case XenbusStateClosing:
			dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);

			len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
			if (unlikely(len >= (sizeof(dev_str) - 1))) {
				err = -ENOMEM;
				xenbus_dev_fatal(pdev->xdev, err,
						 "String overflow while "
						 "reading configuration");
				goto out;
			}
			err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
					   dev_str, "%x:%x:%x.%x",
					   &domain, &bus, &slot, &func);
			if (err < 0) {
				xenbus_dev_fatal(pdev->xdev, err,
						 "Error reading device "
						 "configuration");
				goto out;
			}
			if (err != 4) {
				err = -EINVAL;
				xenbus_dev_fatal(pdev->xdev, err,
						 "Error parsing pci device "
						 "configuration");
				goto out;
			}

486
			err = xen_pcibk_remove_device(pdev, domain, bus, slot,
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
						    func);
			if (err)
				goto out;

			/* TODO: If at some point we implement support for pci
			 * root hot-remove on pcifront side, we'll need to
			 * remove unnecessary xenstore nodes of pci roots here.
			 */

			break;

		default:
			break;
		}
	}

	err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
	if (err) {
		xenbus_dev_fatal(pdev->xdev, err,
				 "Error switching to reconfigured state!");
		goto out;
	}

out:
511
	mutex_unlock(&pdev->dev_lock);
512 513 514
	return 0;
}

515
static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
516 517
				     enum xenbus_state fe_state)
{
518
	struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
519 520 521 522 523

	dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);

	switch (fe_state) {
	case XenbusStateInitialised:
524
		xen_pcibk_attach(pdev);
525 526 527
		break;

	case XenbusStateReconfiguring:
528
		xen_pcibk_reconfigure(pdev);
529 530 531 532 533 534 535 536 537 538
		break;

	case XenbusStateConnected:
		/* pcifront switched its state from reconfiguring to connected.
		 * Then switch to connected state.
		 */
		xenbus_switch_state(xdev, XenbusStateConnected);
		break;

	case XenbusStateClosing:
539
		xen_pcibk_disconnect(pdev);
540 541 542 543
		xenbus_switch_state(xdev, XenbusStateClosing);
		break;

	case XenbusStateClosed:
544
		xen_pcibk_disconnect(pdev);
545 546 547 548 549 550 551 552 553 554 555 556 557 558
		xenbus_switch_state(xdev, XenbusStateClosed);
		if (xenbus_dev_is_online(xdev))
			break;
		/* fall through if not online */
	case XenbusStateUnknown:
		dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
		device_unregister(&xdev->dev);
		break;

	default:
		break;
	}
}

559
static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
560 561 562 563 564 565 566 567
{
	/* Get configuration from xend (if available now) */
	int domain, bus, slot, func;
	int err = 0;
	int i, num_devs;
	char dev_str[64];
	char state_str[64];

568
	mutex_lock(&pdev->dev_lock);
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
	/* It's possible we could get the call to setup twice, so make sure
	 * we're not already connected.
	 */
	if (xenbus_read_driver_state(pdev->xdev->nodename) !=
	    XenbusStateInitWait)
		goto out;

	dev_dbg(&pdev->xdev->dev, "getting be setup\n");

	err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
			   &num_devs);
	if (err != 1) {
		if (err >= 0)
			err = -EINVAL;
		xenbus_dev_fatal(pdev->xdev, err,
				 "Error reading number of devices");
		goto out;
	}

	for (i = 0; i < num_devs; i++) {
		int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
		if (unlikely(l >= (sizeof(dev_str) - 1))) {
			err = -ENOMEM;
			xenbus_dev_fatal(pdev->xdev, err,
					 "String overflow while reading "
					 "configuration");
			goto out;
		}

		err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
				   "%x:%x:%x.%x", &domain, &bus, &slot, &func);
		if (err < 0) {
			xenbus_dev_fatal(pdev->xdev, err,
					 "Error reading device configuration");
			goto out;
		}
		if (err != 4) {
			err = -EINVAL;
			xenbus_dev_fatal(pdev->xdev, err,
					 "Error parsing pci device "
					 "configuration");
			goto out;
		}

613
		err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
		if (err)
			goto out;

		/* Switch substate of this device. */
		l = snprintf(state_str, sizeof(state_str), "state-%d", i);
		if (unlikely(l >= (sizeof(state_str) - 1))) {
			err = -ENOMEM;
			xenbus_dev_fatal(pdev->xdev, err,
					 "String overflow while reading "
					 "configuration");
			goto out;
		}
		err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
				    "%d", XenbusStateInitialised);
		if (err) {
			xenbus_dev_fatal(pdev->xdev, err, "Error switching "
					 "substate of dev-%d\n", i);
			goto out;
		}
	}

635
	err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
636 637 638 639 640 641 642 643 644 645 646 647 648
	if (err) {
		xenbus_dev_fatal(pdev->xdev, err,
				 "Error while publish PCI root buses "
				 "for frontend");
		goto out;
	}

	err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
	if (err)
		xenbus_dev_fatal(pdev->xdev, err,
				 "Error switching to initialised state!");

out:
649
	mutex_unlock(&pdev->dev_lock);
650 651
	if (!err)
		/* see if pcifront is already configured (if not, we'll wait) */
652
		xen_pcibk_attach(pdev);
653 654 655
	return err;
}

656
static void xen_pcibk_be_watch(struct xenbus_watch *watch,
657 658
			     const char **vec, unsigned int len)
{
659 660
	struct xen_pcibk_device *pdev =
	    container_of(watch, struct xen_pcibk_device, be_watch);
661 662 663

	switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
	case XenbusStateInitWait:
664
		xen_pcibk_setup_backend(pdev);
665 666 667 668 669 670 671
		break;

	default:
		break;
	}
}

672
static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
673 674 675
				const struct xenbus_device_id *id)
{
	int err = 0;
676
	struct xen_pcibk_device *pdev = alloc_pdev(dev);
677 678 679 680

	if (pdev == NULL) {
		err = -ENOMEM;
		xenbus_dev_fatal(dev, err,
681
				 "Error allocating xen_pcibk_device struct");
682 683 684 685 686 687 688 689 690 691
		goto out;
	}

	/* wait for xend to configure us */
	err = xenbus_switch_state(dev, XenbusStateInitWait);
	if (err)
		goto out;

	/* watch the backend node for backend configuration information */
	err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
692
				xen_pcibk_be_watch);
693 694
	if (err)
		goto out;
695

696 697 698 699 700
	pdev->be_watching = 1;

	/* We need to force a call to our callback here in case
	 * xend already configured us!
	 */
701
	xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
702 703 704 705 706

out:
	return err;
}

707
static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
708
{
709
	struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
710 711 712 713 714 715 716

	if (pdev != NULL)
		free_pdev(pdev);

	return 0;
}

717
static const struct xenbus_device_id xen_pcibk_ids[] = {
718 719 720 721
	{"pci"},
	{""},
};

722
static DEFINE_XENBUS_DRIVER(xen_pcibk, DRV_NAME,
723 724 725
	.probe			= xen_pcibk_xenbus_probe,
	.remove			= xen_pcibk_xenbus_remove,
	.otherend_changed	= xen_pcibk_frontend_changed,
726
);
727

728
const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
729

730
int __init xen_pcibk_xenbus_register(void)
731
{
732 733
	xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
	if (!xen_pcibk_wq) {
J
Joe Perches 已提交
734
		pr_err("%s: create xen_pciback_workqueue failed\n", __func__);
735 736
		return -EFAULT;
	}
737 738 739
	xen_pcibk_backend = &xen_pcibk_vpci_backend;
	if (passthrough)
		xen_pcibk_backend = &xen_pcibk_passthrough_backend;
J
Joe Perches 已提交
740
	pr_info("backend is %s\n", xen_pcibk_backend->name);
741
	return xenbus_register_backend(&xen_pcibk_driver);
742 743
}

744
void __exit xen_pcibk_xenbus_unregister(void)
745
{
746
	destroy_workqueue(xen_pcibk_wq);
747
	xenbus_unregister_driver(&xen_pcibk_driver);
748
}