xenbus_client.c 22.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/******************************************************************************
 * Client-facing interface for the Xenbus driver.  In other words, the
 * interface between the Xenbus and the device-specific code, be it the
 * frontend or the backend of that driver.
 *
 * Copyright (C) 2005 XenSource Ltd
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation; or, when distributed
 * separately from the Linux kernel or incorporated into other
 * software packages, subject to the following license:
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this source file (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

33
#include <linux/mm.h>
34
#include <linux/slab.h>
35
#include <linux/types.h>
D
Daniel De Graaf 已提交
36
#include <linux/spinlock.h>
37
#include <linux/vmalloc.h>
38
#include <linux/export.h>
39
#include <asm/xen/hypervisor.h>
40
#include <xen/page.h>
41 42
#include <xen/interface/xen.h>
#include <xen/interface/event_channel.h>
D
Daniel De Graaf 已提交
43
#include <xen/balloon.h>
44 45 46
#include <xen/events.h>
#include <xen/grant_table.h>
#include <xen/xenbus.h>
D
Daniel De Graaf 已提交
47
#include <xen/xen.h>
48
#include <xen/features.h>
D
Daniel De Graaf 已提交
49

50
#include "xenbus.h"
D
Daniel De Graaf 已提交
51

52 53 54 55
#define XENBUS_PAGES(_grants)	(DIV_ROUND_UP(_grants, XEN_PFN_PER_PAGE))

#define XENBUS_MAX_RING_PAGES	(XENBUS_PAGES(XENBUS_MAX_RING_GRANTS))

D
Daniel De Graaf 已提交
56 57 58
struct xenbus_map_node {
	struct list_head next;
	union {
59 60 61 62
		struct {
			struct vm_struct *area;
		} pv;
		struct {
63 64
			struct page *pages[XENBUS_MAX_RING_PAGES];
			unsigned long addrs[XENBUS_MAX_RING_GRANTS];
65 66
			void *addr;
		} hvm;
D
Daniel De Graaf 已提交
67
	};
68
	grant_handle_t handles[XENBUS_MAX_RING_GRANTS];
69
	unsigned int   nr_handles;
D
Daniel De Graaf 已提交
70 71
};

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
struct map_ring_valloc {
	struct xenbus_map_node *node;

	/* Why do we need two arrays? See comment of __xenbus_map_ring */
	union {
		unsigned long addrs[XENBUS_MAX_RING_GRANTS];
		pte_t *ptes[XENBUS_MAX_RING_GRANTS];
	};
	phys_addr_t phys_addrs[XENBUS_MAX_RING_GRANTS];

	struct gnttab_map_grant_ref map[XENBUS_MAX_RING_GRANTS];
	struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];

	unsigned int idx;	/* HVM only. */
};

D
Daniel De Graaf 已提交
88 89 90 91
static DEFINE_SPINLOCK(xenbus_valloc_lock);
static LIST_HEAD(xenbus_valloc_pages);

struct xenbus_ring_ops {
92
	int (*map)(struct xenbus_device *dev, struct map_ring_valloc *info,
93 94
		   grant_ref_t *gnt_refs, unsigned int nr_grefs,
		   void **vaddr);
D
Daniel De Graaf 已提交
95 96 97 98
	int (*unmap)(struct xenbus_device *dev, void *vaddr);
};

static const struct xenbus_ring_ops *ring_ops __read_mostly;
99 100 101 102 103 104 105 106 107 108 109

const char *xenbus_strstate(enum xenbus_state state)
{
	static const char *const name[] = {
		[ XenbusStateUnknown      ] = "Unknown",
		[ XenbusStateInitialising ] = "Initialising",
		[ XenbusStateInitWait     ] = "InitWait",
		[ XenbusStateInitialised  ] = "Initialised",
		[ XenbusStateConnected    ] = "Connected",
		[ XenbusStateClosing      ] = "Closing",
		[ XenbusStateClosed	  ] = "Closed",
110 111
		[XenbusStateReconfiguring] = "Reconfiguring",
		[XenbusStateReconfigured] = "Reconfigured",
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	};
	return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
}
EXPORT_SYMBOL_GPL(xenbus_strstate);

/**
 * xenbus_watch_path - register a watch
 * @dev: xenbus device
 * @path: path to watch
 * @watch: watch to register
 * @callback: callback to register
 *
 * Register a @watch on the given path, using the given xenbus_watch structure
 * for storage, and the given @callback function as the callback.  Return 0 on
 * success, or -errno on error.  On success, the given @path will be saved as
 * @watch->node, and remains the caller's to free.  On error, @watch->node will
 * be NULL, the device will switch to %XenbusStateClosing, and the error will
 * be saved in the store.
 */
int xenbus_watch_path(struct xenbus_device *dev, const char *path,
		      struct xenbus_watch *watch,
		      void (*callback)(struct xenbus_watch *,
134
				       const char *, const char *))
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
{
	int err;

	watch->node = path;
	watch->callback = callback;

	err = register_xenbus_watch(watch);

	if (err) {
		watch->node = NULL;
		watch->callback = NULL;
		xenbus_dev_fatal(dev, err, "adding watch on %s", path);
	}

	return err;
}
EXPORT_SYMBOL_GPL(xenbus_watch_path);


/**
 * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
 * @dev: xenbus device
 * @watch: watch to register
 * @callback: callback to register
 * @pathfmt: format of path to watch
 *
 * Register a watch on the given @path, using the given xenbus_watch
 * structure for storage, and the given @callback function as the callback.
 * Return 0 on success, or -errno on error.  On success, the watched path
 * (@path/@path2) will be saved as @watch->node, and becomes the caller's to
 * kfree().  On error, watch->node will be NULL, so the caller has nothing to
 * free, the device will switch to %XenbusStateClosing, and the error will be
 * saved in the store.
 */
int xenbus_watch_pathfmt(struct xenbus_device *dev,
			 struct xenbus_watch *watch,
			 void (*callback)(struct xenbus_watch *,
172
					  const char *, const char *),
173 174 175 176 177 178 179
			 const char *pathfmt, ...)
{
	int err;
	va_list ap;
	char *path;

	va_start(ap, pathfmt);
180
	path = kvasprintf(GFP_NOIO | __GFP_HIGH, pathfmt, ap);
181 182 183 184 185 186 187 188 189 190 191 192 193 194
	va_end(ap);

	if (!path) {
		xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
		return -ENOMEM;
	}
	err = xenbus_watch_path(dev, path, watch, callback);

	if (err)
		kfree(path);
	return err;
}
EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);

195 196
static void xenbus_switch_fatal(struct xenbus_device *, int, int,
				const char *, ...);
197

198 199 200
static int
__xenbus_switch_state(struct xenbus_device *dev,
		      enum xenbus_state state, int depth)
201 202 203 204 205 206 207 208
{
	/* We check whether the state is currently set to the given value, and
	   if not, then the state is set.  We don't want to unconditionally
	   write the given state, because we don't want to fire watches
	   unnecessarily.  Furthermore, if the node has gone, we don't write
	   to it, as the device will be tearing down, and we don't want to
	   resurrect that directory.

209 210 211 212
	   Note that, because of this cached value of our state, this
	   function will not take a caller's Xenstore transaction
	   (something it was trying to in the past) because dev->state
	   would not get reset if the transaction was aborted.
213 214
	 */

215
	struct xenbus_transaction xbt;
216
	int current_state;
217
	int err, abort;
218 219 220 221

	if (state == dev->state)
		return 0;

222 223 224 225 226 227
again:
	abort = 1;

	err = xenbus_transaction_start(&xbt);
	if (err) {
		xenbus_switch_fatal(dev, depth, err, "starting transaction");
228
		return 0;
229
	}
230

231 232 233 234 235
	err = xenbus_scanf(xbt, dev->nodename, "state", "%d", &current_state);
	if (err != 1)
		goto abort;

	err = xenbus_printf(xbt, dev->nodename, "state", "%d", state);
236
	if (err) {
237 238
		xenbus_switch_fatal(dev, depth, err, "writing new state");
		goto abort;
239 240
	}

241 242 243 244 245 246 247 248 249
	abort = 0;
abort:
	err = xenbus_transaction_end(xbt, abort);
	if (err) {
		if (err == -EAGAIN && !abort)
			goto again;
		xenbus_switch_fatal(dev, depth, err, "ending transaction");
	} else
		dev->state = state;
250 251 252

	return 0;
}
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

/**
 * xenbus_switch_state
 * @dev: xenbus device
 * @state: new state
 *
 * Advertise in the store a change of the given driver to the given new_state.
 * Return 0 on success, or -errno on error.  On error, the device will switch
 * to XenbusStateClosing, and the error will be saved in the store.
 */
int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
{
	return __xenbus_switch_state(dev, state, 0);
}

268 269 270 271 272 273 274 275 276 277 278 279 280 281
EXPORT_SYMBOL_GPL(xenbus_switch_state);

int xenbus_frontend_closed(struct xenbus_device *dev)
{
	xenbus_switch_state(dev, XenbusStateClosed);
	complete(&dev->down);
	return 0;
}
EXPORT_SYMBOL_GPL(xenbus_frontend_closed);

static void xenbus_va_dev_error(struct xenbus_device *dev, int err,
				const char *fmt, va_list ap)
{
	unsigned int len;
J
Joe Perches 已提交
282 283
	char *printf_buffer;
	char *path_buffer;
284 285

#define PRINTF_BUFFER_SIZE 4096
J
Joe Perches 已提交
286

287
	printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
J
Joe Perches 已提交
288 289
	if (!printf_buffer)
		return;
290 291

	len = sprintf(printf_buffer, "%i ", -err);
J
Joe Perches 已提交
292
	vsnprintf(printf_buffer + len, PRINTF_BUFFER_SIZE - len, fmt, ap);
293 294 295

	dev_err(&dev->dev, "%s\n", printf_buffer);

J
Joe Perches 已提交
296
	path_buffer = kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
297 298
	if (path_buffer)
		xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer);
299 300 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

	kfree(printf_buffer);
	kfree(path_buffer);
}

/**
 * xenbus_dev_error
 * @dev: xenbus device
 * @err: error to report
 * @fmt: error message format
 *
 * Report the given negative errno into the store, along with the given
 * formatted message.
 */
void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	xenbus_va_dev_error(dev, err, fmt, ap);
	va_end(ap);
}
EXPORT_SYMBOL_GPL(xenbus_dev_error);

/**
 * xenbus_dev_fatal
 * @dev: xenbus device
 * @err: error to report
 * @fmt: error message format
 *
 * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
330
 * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
 * closedown of this driver and its peer.
 */

void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	xenbus_va_dev_error(dev, err, fmt, ap);
	va_end(ap);

	xenbus_switch_state(dev, XenbusStateClosing);
}
EXPORT_SYMBOL_GPL(xenbus_dev_fatal);

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
/**
 * Equivalent to xenbus_dev_fatal(dev, err, fmt, args), but helps
 * avoiding recursion within xenbus_switch_state.
 */
static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,
				const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	xenbus_va_dev_error(dev, err, fmt, ap);
	va_end(ap);

	if (!depth)
		__xenbus_switch_state(dev, XenbusStateClosing, 1);
}

363 364 365
/**
 * xenbus_grant_ring
 * @dev: xenbus device
366 367 368 369 370 371 372 373
 * @vaddr: starting virtual address of the ring
 * @nr_pages: number of pages to be granted
 * @grefs: grant reference array to be filled in
 *
 * Grant access to the given @vaddr to the peer of the given device.
 * Then fill in @grefs with grant references.  Return 0 on success, or
 * -errno on error.  On error, the device will switch to
 * XenbusStateClosing, and the error will be saved in the store.
374
 */
375 376
int xenbus_grant_ring(struct xenbus_device *dev, void *vaddr,
		      unsigned int nr_pages, grant_ref_t *grefs)
377
{
378 379 380 381 382
	int err;
	int i, j;

	for (i = 0; i < nr_pages; i++) {
		err = gnttab_grant_foreign_access(dev->otherend_id,
383
						  virt_to_gfn(vaddr), 0);
384 385 386 387 388 389
		if (err < 0) {
			xenbus_dev_fatal(dev, err,
					 "granting access to ring page");
			goto fail;
		}
		grefs[i] = err;
390

J
Julien Grall 已提交
391
		vaddr = vaddr + XEN_PAGE_SIZE;
392 393 394 395 396 397 398
	}

	return 0;

fail:
	for (j = 0; j < i; j++)
		gnttab_end_foreign_access_ref(grefs[j], 0);
399 400 401 402 403 404 405 406 407 408 409
	return err;
}
EXPORT_SYMBOL_GPL(xenbus_grant_ring);


/**
 * Allocate an event channel for the given xenbus_device, assigning the newly
 * created local port to *port.  Return 0 on success, or -errno on error.  On
 * error, the device will switch to XenbusStateClosing, and the error will be
 * saved in the store.
 */
410
int xenbus_alloc_evtchn(struct xenbus_device *dev, evtchn_port_t *port)
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
{
	struct evtchn_alloc_unbound alloc_unbound;
	int err;

	alloc_unbound.dom = DOMID_SELF;
	alloc_unbound.remote_dom = dev->otherend_id;

	err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
					  &alloc_unbound);
	if (err)
		xenbus_dev_fatal(dev, err, "allocating event channel");
	else
		*port = alloc_unbound.port;

	return err;
}
EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);


/**
 * Free an existing event channel. Returns 0 on success or -errno on error.
 */
433
int xenbus_free_evtchn(struct xenbus_device *dev, evtchn_port_t port)
434 435 436 437 438 439 440 441
{
	struct evtchn_close close;
	int err;

	close.port = port;

	err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
	if (err)
442
		xenbus_dev_error(dev, err, "freeing event channel %u", port);
443 444 445 446 447 448 449 450 451

	return err;
}
EXPORT_SYMBOL_GPL(xenbus_free_evtchn);


/**
 * xenbus_map_ring_valloc
 * @dev: xenbus device
452 453
 * @gnt_refs: grant reference array
 * @nr_grefs: number of grant references
454 455
 * @vaddr: pointer to address to be filled out by mapping
 *
456 457 458
 * Map @nr_grefs pages of memory into this domain from another
 * domain's grant table.  xenbus_map_ring_valloc allocates @nr_grefs
 * pages of virtual address space, maps the pages to that address, and
459
 * sets *vaddr to that address.  Returns 0 on success, and -errno on
460
 * error. If an error is returned, device will switch to
461 462
 * XenbusStateClosing and the error message will be saved in XenStore.
 */
463 464
int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t *gnt_refs,
			   unsigned int nr_grefs, void **vaddr)
D
Daniel De Graaf 已提交
465
{
466
	int err;
467 468 469 470 471 472 473 474 475 476 477 478
	struct map_ring_valloc *info;

	*vaddr = NULL;

	if (nr_grefs > XENBUS_MAX_RING_GRANTS)
		return -EINVAL;

	info = kzalloc(sizeof(*info), GFP_KERNEL);
	if (!info)
		return -ENOMEM;

	info->node = kzalloc(sizeof(*info->node), GFP_KERNEL);
479
	if (!info->node)
480
		err = -ENOMEM;
481 482
	else
		err = ring_ops->map(dev, info, gnt_refs, nr_grefs, vaddr);
483

484 485
	kfree(info->node);
	kfree(info);
486
	return err;
D
Daniel De Graaf 已提交
487 488 489
}
EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);

490 491 492 493 494 495 496
/* N.B. sizeof(phys_addr_t) doesn't always equal to sizeof(unsigned
 * long), e.g. 32-on-64.  Caller is responsible for preparing the
 * right array to feed into this function */
static int __xenbus_map_ring(struct xenbus_device *dev,
			     grant_ref_t *gnt_refs,
			     unsigned int nr_grefs,
			     grant_handle_t *handles,
497
			     struct map_ring_valloc *info,
498 499 500 501 502
			     unsigned int flags,
			     bool *leaked)
{
	int i, j;

503
	if (nr_grefs > XENBUS_MAX_RING_GRANTS)
504 505 506
		return -EINVAL;

	for (i = 0; i < nr_grefs; i++) {
507 508
		gnttab_set_map_op(&info->map[i], info->phys_addrs[i], flags,
				  gnt_refs[i], dev->otherend_id);
509 510 511
		handles[i] = INVALID_GRANT_HANDLE;
	}

512
	gnttab_batch_map(info->map, i);
513 514

	for (i = 0; i < nr_grefs; i++) {
515 516
		if (info->map[i].status != GNTST_okay) {
			xenbus_dev_fatal(dev, info->map[i].status,
517 518 519 520
					 "mapping in shared page %d from domain %d",
					 gnt_refs[i], dev->otherend_id);
			goto fail;
		} else
521
			handles[i] = info->map[i].handle;
522 523
	}

524
	return 0;
525 526 527 528

 fail:
	for (i = j = 0; i < nr_grefs; i++) {
		if (handles[i] != INVALID_GRANT_HANDLE) {
529 530
			gnttab_set_unmap_op(&info->unmap[j],
					    info->phys_addrs[i],
531 532 533 534 535
					    GNTMAP_host_map, handles[i]);
			j++;
		}
	}

536
	if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, info->unmap, j))
537 538 539 540
		BUG();

	*leaked = false;
	for (i = 0; i < j; i++) {
541
		if (info->unmap[i].status != GNTST_okay) {
542 543 544 545 546
			*leaked = true;
			break;
		}
	}

547
	return -ENOENT;
548 549
}

550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
/**
 * xenbus_unmap_ring
 * @dev: xenbus device
 * @handles: grant handle array
 * @nr_handles: number of handles in the array
 * @vaddrs: addresses to unmap
 *
 * Unmap memory in this domain that was imported from another domain.
 * Returns 0 on success and returns GNTST_* on error
 * (see xen/include/interface/grant_table.h).
 */
static int xenbus_unmap_ring(struct xenbus_device *dev, grant_handle_t *handles,
			     unsigned int nr_handles, unsigned long *vaddrs)
{
	struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
	int i;
	int err;

	if (nr_handles > XENBUS_MAX_RING_GRANTS)
		return -EINVAL;

	for (i = 0; i < nr_handles; i++)
		gnttab_set_unmap_op(&unmap[i], vaddrs[i],
				    GNTMAP_host_map, handles[i]);

	if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i))
		BUG();

	err = GNTST_okay;
	for (i = 0; i < nr_handles; i++) {
		if (unmap[i].status != GNTST_okay) {
			xenbus_dev_error(dev, unmap[i].status,
					 "unmapping page at handle %d error %d",
					 handles[i], unmap[i].status);
			err = unmap[i].status;
			break;
		}
	}

	return err;
}

592 593 594 595 596
static void xenbus_map_ring_setup_grant_hvm(unsigned long gfn,
					    unsigned int goffset,
					    unsigned int len,
					    void *data)
{
597
	struct map_ring_valloc *info = data;
598 599 600 601 602 603 604 605
	unsigned long vaddr = (unsigned long)gfn_to_virt(gfn);

	info->phys_addrs[info->idx] = vaddr;
	info->addrs[info->idx] = vaddr;

	info->idx++;
}

606 607 608 609 610
static int xenbus_map_ring_hvm(struct xenbus_device *dev,
			       struct map_ring_valloc *info,
			       grant_ref_t *gnt_ref,
			       unsigned int nr_grefs,
			       void **vaddr)
D
Daniel De Graaf 已提交
611
{
612
	struct xenbus_map_node *node = info->node;
D
Daniel De Graaf 已提交
613 614
	int err;
	void *addr;
615
	bool leaked = false;
616
	unsigned int nr_pages = XENBUS_PAGES(nr_grefs);
617

618
	err = alloc_xenballooned_pages(nr_pages, node->hvm.pages);
D
Daniel De Graaf 已提交
619 620 621
	if (err)
		goto out_err;

622 623
	gnttab_foreach_grant(node->hvm.pages, nr_grefs,
			     xenbus_map_ring_setup_grant_hvm,
624
			     info);
625 626

	err = __xenbus_map_ring(dev, gnt_ref, nr_grefs, node->handles,
627
				info, GNTMAP_host_map, &leaked);
628
	node->nr_handles = nr_grefs;
D
Daniel De Graaf 已提交
629 630

	if (err)
631 632
		goto out_free_ballooned_pages;

633
	addr = vmap(node->hvm.pages, nr_pages, VM_MAP | VM_IOREMAP,
634 635 636 637 638 639 640
		    PAGE_KERNEL);
	if (!addr) {
		err = -ENOMEM;
		goto out_xenbus_unmap_ring;
	}

	node->hvm.addr = addr;
D
Daniel De Graaf 已提交
641 642 643 644 645 646

	spin_lock(&xenbus_valloc_lock);
	list_add(&node->next, &xenbus_valloc_pages);
	spin_unlock(&xenbus_valloc_lock);

	*vaddr = addr;
647 648
	info->node = NULL;

D
Daniel De Graaf 已提交
649 650
	return 0;

651 652
 out_xenbus_unmap_ring:
	if (!leaked)
653
		xenbus_unmap_ring(dev, node->handles, nr_grefs, info->addrs);
654 655
	else
		pr_alert("leaking %p size %u page(s)",
656
			 addr, nr_pages);
657 658
 out_free_ballooned_pages:
	if (!leaked)
659
		free_xenballooned_pages(nr_pages, node->hvm.pages);
660
 out_err:
D
Daniel De Graaf 已提交
661 662
	return err;
}
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

/**
 * xenbus_unmap_ring_vfree
 * @dev: xenbus device
 * @vaddr: addr to unmap
 *
 * Based on Rusty Russell's skeleton driver's unmap_page.
 * Unmap a page of memory in this domain that was imported from another domain.
 * Use xenbus_unmap_ring_vfree if you mapped in your memory with
 * xenbus_map_ring_valloc (it will free the virtual address space).
 * Returns 0 on success and returns GNTST_* on error
 * (see xen/include/interface/grant_table.h).
 */
int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
{
D
Daniel De Graaf 已提交
678 679 680 681
	return ring_ops->unmap(dev, vaddr);
}
EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);

682
#ifdef CONFIG_XEN_PV
683 684 685 686 687
static int xenbus_map_ring_pv(struct xenbus_device *dev,
			      struct map_ring_valloc *info,
			      grant_ref_t *gnt_refs,
			      unsigned int nr_grefs,
			      void **vaddr)
688
{
689
	struct xenbus_map_node *node = info->node;
690 691 692 693 694
	struct vm_struct *area;
	int err = GNTST_okay;
	int i;
	bool leaked;

695
	area = alloc_vm_area(XEN_PAGE_SIZE * nr_grefs, info->ptes);
696 697 698 699 700 701
	if (!area) {
		kfree(node);
		return -ENOMEM;
	}

	for (i = 0; i < nr_grefs; i++)
702 703
		info->phys_addrs[i] =
			arbitrary_virt_to_machine(info->ptes[i]).maddr;
704 705

	err = __xenbus_map_ring(dev, gnt_refs, nr_grefs, node->handles,
706
				info, GNTMAP_host_map | GNTMAP_contains_pte,
707 708 709 710 711 712 713 714 715 716 717 718
				&leaked);
	if (err)
		goto failed;

	node->nr_handles = nr_grefs;
	node->pv.area = area;

	spin_lock(&xenbus_valloc_lock);
	list_add(&node->next, &xenbus_valloc_pages);
	spin_unlock(&xenbus_valloc_lock);

	*vaddr = area->addr;
719 720
	info->node = NULL;

721 722 723 724 725 726 727 728 729 730 731
	return 0;

failed:
	if (!leaked)
		free_vm_area(area);
	else
		pr_alert("leaking VM area %p size %u page(s)", area, nr_grefs);

	return err;
}

732
static int xenbus_unmap_ring_pv(struct xenbus_device *dev, void *vaddr)
D
Daniel De Graaf 已提交
733 734
{
	struct xenbus_map_node *node;
735
	struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
736
	unsigned int level;
737 738 739
	int i;
	bool leaked = false;
	int err;
740

D
Daniel De Graaf 已提交
741 742
	spin_lock(&xenbus_valloc_lock);
	list_for_each_entry(node, &xenbus_valloc_pages, next) {
743
		if (node->pv.area->addr == vaddr) {
D
Daniel De Graaf 已提交
744 745 746
			list_del(&node->next);
			goto found;
		}
747
	}
D
Daniel De Graaf 已提交
748 749 750
	node = NULL;
 found:
	spin_unlock(&xenbus_valloc_lock);
751

D
Daniel De Graaf 已提交
752
	if (!node) {
753 754 755 756 757
		xenbus_dev_error(dev, -ENOENT,
				 "can't find mapped virtual address %p", vaddr);
		return GNTST_bad_virt_addr;
	}

758 759 760 761
	for (i = 0; i < node->nr_handles; i++) {
		unsigned long addr;

		memset(&unmap[i], 0, sizeof(unmap[i]));
J
Julien Grall 已提交
762
		addr = (unsigned long)vaddr + (XEN_PAGE_SIZE * i);
763 764 765 766 767
		unmap[i].host_addr = arbitrary_virt_to_machine(
			lookup_address(addr, &level)).maddr;
		unmap[i].dev_bus_addr = 0;
		unmap[i].handle = node->handles[i];
	}
768

769
	if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i))
770 771
		BUG();

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
	err = GNTST_okay;
	leaked = false;
	for (i = 0; i < node->nr_handles; i++) {
		if (unmap[i].status != GNTST_okay) {
			leaked = true;
			xenbus_dev_error(dev, unmap[i].status,
					 "unmapping page at handle %d error %d",
					 node->handles[i], unmap[i].status);
			err = unmap[i].status;
			break;
		}
	}

	if (!leaked)
		free_vm_area(node->pv.area);
787
	else
788 789
		pr_alert("leaking VM area %p size %u page(s)",
			 node->pv.area, node->nr_handles);
790

D
Daniel De Graaf 已提交
791
	kfree(node);
792
	return err;
793 794
}

795
static const struct xenbus_ring_ops ring_ops_pv = {
796 797
	.map = xenbus_map_ring_pv,
	.unmap = xenbus_unmap_ring_pv,
798 799 800
};
#endif

801
struct unmap_ring_hvm
802 803 804 805 806 807 808 809 810 811
{
	unsigned int idx;
	unsigned long addrs[XENBUS_MAX_RING_GRANTS];
};

static void xenbus_unmap_ring_setup_grant_hvm(unsigned long gfn,
					      unsigned int goffset,
					      unsigned int len,
					      void *data)
{
812
	struct unmap_ring_hvm *info = data;
813 814 815 816 817 818

	info->addrs[info->idx] = (unsigned long)gfn_to_virt(gfn);

	info->idx++;
}

819
static int xenbus_unmap_ring_hvm(struct xenbus_device *dev, void *vaddr)
D
Daniel De Graaf 已提交
820 821 822 823
{
	int rv;
	struct xenbus_map_node *node;
	void *addr;
824
	struct unmap_ring_hvm info = {
825 826 827
		.idx = 0,
	};
	unsigned int nr_pages;
D
Daniel De Graaf 已提交
828 829 830

	spin_lock(&xenbus_valloc_lock);
	list_for_each_entry(node, &xenbus_valloc_pages, next) {
831
		addr = node->hvm.addr;
D
Daniel De Graaf 已提交
832 833 834 835 836
		if (addr == vaddr) {
			list_del(&node->next);
			goto found;
		}
	}
J
Jan Beulich 已提交
837
	node = addr = NULL;
D
Daniel De Graaf 已提交
838 839 840 841 842 843 844 845 846
 found:
	spin_unlock(&xenbus_valloc_lock);

	if (!node) {
		xenbus_dev_error(dev, -ENOENT,
				 "can't find mapped virtual address %p", vaddr);
		return GNTST_bad_virt_addr;
	}

847 848 849 850 851
	nr_pages = XENBUS_PAGES(node->nr_handles);

	gnttab_foreach_grant(node->hvm.pages, node->nr_handles,
			     xenbus_unmap_ring_setup_grant_hvm,
			     &info);
D
Daniel De Graaf 已提交
852

853
	rv = xenbus_unmap_ring(dev, node->handles, node->nr_handles,
854
			       info.addrs);
855
	if (!rv) {
856
		vunmap(vaddr);
857
		free_xenballooned_pages(nr_pages, node->hvm.pages);
858
	}
D
Daniel De Graaf 已提交
859
	else
860
		WARN(1, "Leaking %p, size %u page(s)\n", vaddr, nr_pages);
D
Daniel De Graaf 已提交
861 862 863 864

	kfree(node);
	return rv;
}
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882

/**
 * xenbus_read_driver_state
 * @path: path for driver
 *
 * Return the state of the driver rooted at the given store path, or
 * XenbusStateUnknown if no state can be read.
 */
enum xenbus_state xenbus_read_driver_state(const char *path)
{
	enum xenbus_state result;
	int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
	if (err)
		result = XenbusStateUnknown;

	return result;
}
EXPORT_SYMBOL_GPL(xenbus_read_driver_state);
D
Daniel De Graaf 已提交
883 884

static const struct xenbus_ring_ops ring_ops_hvm = {
885 886
	.map = xenbus_map_ring_hvm,
	.unmap = xenbus_unmap_ring_hvm,
D
Daniel De Graaf 已提交
887 888 889 890
};

void __init xenbus_ring_ops_init(void)
{
891
#ifdef CONFIG_XEN_PV
892
	if (!xen_feature(XENFEAT_auto_translated_physmap))
D
Daniel De Graaf 已提交
893 894
		ring_ops = &ring_ops_pv;
	else
895
#endif
D
Daniel De Graaf 已提交
896 897
		ring_ops = &ring_ops_hvm;
}