xref: /linux/drivers/xen/xenbus/xenbus_client.c (revision 4db102dcb0396a4ccf89b1eac0f4eb3fd167a080)
1 /******************************************************************************
2  * Client-facing interface for the Xenbus driver.  In other words, the
3  * interface between the Xenbus and the device-specific code, be it the
4  * frontend or the backend of that driver.
5  *
6  * Copyright (C) 2005 XenSource Ltd
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation; or, when distributed
11  * separately from the Linux kernel or incorporated into other
12  * software packages, subject to the following license:
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this source file (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy, modify,
17  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * IN THE SOFTWARE.
31  */
32 
33 #include <linux/mm.h>
34 #include <linux/slab.h>
35 #include <linux/types.h>
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 #include <xen/interface/xen.h>
42 #include <xen/interface/event_channel.h>
43 #include <xen/balloon.h>
44 #include <xen/events.h>
45 #include <xen/grant_table.h>
46 #include <xen/xenbus.h>
47 #include <xen/xen.h>
48 #include <xen/features.h>
49 
50 #include "xenbus.h"
51 
52 #define XENBUS_PAGES(_grants)	(DIV_ROUND_UP(_grants, XEN_PFN_PER_PAGE))
53 
54 #define XENBUS_MAX_RING_PAGES	(XENBUS_PAGES(XENBUS_MAX_RING_GRANTS))
55 
56 struct xenbus_map_node {
57 	struct list_head next;
58 	union {
59 		struct {
60 			struct vm_struct *area;
61 		} pv;
62 		struct {
63 			struct page *pages[XENBUS_MAX_RING_PAGES];
64 			unsigned long addrs[XENBUS_MAX_RING_GRANTS];
65 			void *addr;
66 		} hvm;
67 	};
68 	grant_handle_t handles[XENBUS_MAX_RING_GRANTS];
69 	unsigned int   nr_handles;
70 };
71 
72 struct map_ring_valloc {
73 	struct xenbus_map_node *node;
74 
75 	/* Why do we need two arrays? See comment of __xenbus_map_ring */
76 	unsigned long addrs[XENBUS_MAX_RING_GRANTS];
77 	phys_addr_t phys_addrs[XENBUS_MAX_RING_GRANTS];
78 
79 	struct gnttab_map_grant_ref map[XENBUS_MAX_RING_GRANTS];
80 	struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
81 
82 	unsigned int idx;
83 };
84 
85 static DEFINE_SPINLOCK(xenbus_valloc_lock);
86 static LIST_HEAD(xenbus_valloc_pages);
87 
88 struct xenbus_ring_ops {
89 	int (*map)(struct xenbus_device *dev, struct map_ring_valloc *info,
90 		   grant_ref_t *gnt_refs, unsigned int nr_grefs,
91 		   void **vaddr);
92 	int (*unmap)(struct xenbus_device *dev, void *vaddr);
93 };
94 
95 static const struct xenbus_ring_ops *ring_ops __read_mostly;
96 
97 const char *xenbus_strstate(enum xenbus_state state)
98 {
99 	static const char *const name[] = {
100 		[ XenbusStateUnknown      ] = "Unknown",
101 		[ XenbusStateInitialising ] = "Initialising",
102 		[ XenbusStateInitWait     ] = "InitWait",
103 		[ XenbusStateInitialised  ] = "Initialised",
104 		[ XenbusStateConnected    ] = "Connected",
105 		[ XenbusStateClosing      ] = "Closing",
106 		[ XenbusStateClosed	  ] = "Closed",
107 		[XenbusStateReconfiguring] = "Reconfiguring",
108 		[XenbusStateReconfigured] = "Reconfigured",
109 	};
110 	return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
111 }
112 EXPORT_SYMBOL_GPL(xenbus_strstate);
113 
114 /**
115  * xenbus_watch_path - register a watch
116  * @dev: xenbus device
117  * @path: path to watch
118  * @watch: watch to register
119  * @callback: callback to register
120  *
121  * Register a @watch on the given path, using the given xenbus_watch structure
122  * for storage, and the given @callback function as the callback.  On success,
123  * the given @path will be saved as @watch->node, and remains the
124  * caller's to free.  On error, @watch->node will
125  * be NULL, the device will switch to %XenbusStateClosing, and the error will
126  * be saved in the store.
127  *
128  * Returns: %0 on success or -errno on error
129  */
130 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
131 		      struct xenbus_watch *watch,
132 		      bool (*will_handle)(struct xenbus_watch *,
133 					  const char *, const char *),
134 		      void (*callback)(struct xenbus_watch *,
135 				       const char *, const char *))
136 {
137 	int err;
138 
139 	watch->node = path;
140 	watch->will_handle = will_handle;
141 	watch->callback = callback;
142 
143 	err = register_xenbus_watch(watch);
144 
145 	if (err) {
146 		watch->node = NULL;
147 		watch->will_handle = NULL;
148 		watch->callback = NULL;
149 		xenbus_dev_fatal(dev, err, "adding watch on %s", path);
150 	}
151 
152 	return err;
153 }
154 EXPORT_SYMBOL_GPL(xenbus_watch_path);
155 
156 
157 /**
158  * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
159  * @dev: xenbus device
160  * @watch: watch to register
161  * @callback: callback to register
162  * @pathfmt: format of path to watch
163  *
164  * Register a watch on the given @path, using the given xenbus_watch
165  * structure for storage, and the given @callback function as the
166  * callback.  On success, the watched path (@path/@path2) will be saved
167  * as @watch->node, and becomes the caller's to kfree().
168  * On error, watch->node will be NULL, so the caller has nothing to
169  * free, the device will switch to %XenbusStateClosing, and the error will be
170  * saved in the store.
171  *
172  * Returns: %0 on success or -errno on error
173  */
174 int xenbus_watch_pathfmt(struct xenbus_device *dev,
175 			 struct xenbus_watch *watch,
176 			 bool (*will_handle)(struct xenbus_watch *,
177 					const char *, const char *),
178 			 void (*callback)(struct xenbus_watch *,
179 					  const char *, const char *),
180 			 const char *pathfmt, ...)
181 {
182 	int err;
183 	va_list ap;
184 	char *path;
185 
186 	va_start(ap, pathfmt);
187 	path = kvasprintf(GFP_NOIO | __GFP_HIGH, pathfmt, ap);
188 	va_end(ap);
189 
190 	if (!path) {
191 		xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
192 		return -ENOMEM;
193 	}
194 	err = xenbus_watch_path(dev, path, watch, will_handle, callback);
195 
196 	if (err)
197 		kfree(path);
198 	return err;
199 }
200 EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);
201 
202 static void xenbus_switch_fatal(struct xenbus_device *, int, int,
203 				const char *, ...);
204 
205 static int
206 __xenbus_switch_state(struct xenbus_device *dev,
207 		      enum xenbus_state state, int depth)
208 {
209 	/* We check whether the state is currently set to the given value, and
210 	   if not, then the state is set.  We don't want to unconditionally
211 	   write the given state, because we don't want to fire watches
212 	   unnecessarily.  Furthermore, if the node has gone, we don't write
213 	   to it, as the device will be tearing down, and we don't want to
214 	   resurrect that directory.
215 
216 	   Note that, because of this cached value of our state, this
217 	   function will not take a caller's Xenstore transaction
218 	   (something it was trying to in the past) because dev->state
219 	   would not get reset if the transaction was aborted.
220 	 */
221 
222 	struct xenbus_transaction xbt;
223 	int current_state;
224 	int err, abort;
225 
226 	if (state == dev->state)
227 		return 0;
228 
229 again:
230 	abort = 1;
231 
232 	err = xenbus_transaction_start(&xbt);
233 	if (err) {
234 		xenbus_switch_fatal(dev, depth, err, "starting transaction");
235 		return 0;
236 	}
237 
238 	err = xenbus_scanf(xbt, dev->nodename, "state", "%d", &current_state);
239 	if (err != 1)
240 		goto abort;
241 
242 	err = xenbus_printf(xbt, dev->nodename, "state", "%d", state);
243 	if (err) {
244 		xenbus_switch_fatal(dev, depth, err, "writing new state");
245 		goto abort;
246 	}
247 
248 	abort = 0;
249 abort:
250 	err = xenbus_transaction_end(xbt, abort);
251 	if (err) {
252 		if (err == -EAGAIN && !abort)
253 			goto again;
254 		xenbus_switch_fatal(dev, depth, err, "ending transaction");
255 	} else
256 		dev->state = state;
257 
258 	return 0;
259 }
260 
261 /**
262  * xenbus_switch_state - save the new state of a driver
263  * @dev: xenbus device
264  * @state: new state
265  *
266  * Advertise in the store a change of the given driver to the given new_state.
267  * On error, the device will switch to XenbusStateClosing, and the error
268  * will be saved in the store.
269  *
270  * Returns: %0 on success or -errno on error
271  */
272 int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
273 {
274 	return __xenbus_switch_state(dev, state, 0);
275 }
276 
277 EXPORT_SYMBOL_GPL(xenbus_switch_state);
278 
279 int xenbus_frontend_closed(struct xenbus_device *dev)
280 {
281 	xenbus_switch_state(dev, XenbusStateClosed);
282 	complete(&dev->down);
283 	return 0;
284 }
285 EXPORT_SYMBOL_GPL(xenbus_frontend_closed);
286 
287 static void xenbus_va_dev_error(struct xenbus_device *dev, int err,
288 				const char *fmt, va_list ap)
289 {
290 	unsigned int len;
291 	char *printf_buffer;
292 	char *path_buffer;
293 
294 #define PRINTF_BUFFER_SIZE 4096
295 
296 	printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
297 	if (!printf_buffer)
298 		return;
299 
300 	len = sprintf(printf_buffer, "%i ", -err);
301 	vsnprintf(printf_buffer + len, PRINTF_BUFFER_SIZE - len, fmt, ap);
302 
303 	dev_err(&dev->dev, "%s\n", printf_buffer);
304 
305 	path_buffer = kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
306 	if (path_buffer)
307 		xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer);
308 
309 	kfree(printf_buffer);
310 	kfree(path_buffer);
311 }
312 
313 /**
314  * xenbus_dev_error - place an error message into the store
315  * @dev: xenbus device
316  * @err: error to report
317  * @fmt: error message format
318  *
319  * Report the given negative errno into the store, along with the given
320  * formatted message.
321  */
322 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
323 {
324 	va_list ap;
325 
326 	va_start(ap, fmt);
327 	xenbus_va_dev_error(dev, err, fmt, ap);
328 	va_end(ap);
329 }
330 EXPORT_SYMBOL_GPL(xenbus_dev_error);
331 
332 /**
333  * xenbus_dev_fatal - put an error messages into the store and then shutdown
334  * @dev: xenbus device
335  * @err: error to report
336  * @fmt: error message format
337  *
338  * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
339  * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
340  * closedown of this driver and its peer.
341  */
342 
343 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
344 {
345 	va_list ap;
346 
347 	va_start(ap, fmt);
348 	xenbus_va_dev_error(dev, err, fmt, ap);
349 	va_end(ap);
350 
351 	xenbus_switch_state(dev, XenbusStateClosing);
352 }
353 EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
354 
355 /*
356  * Equivalent to xenbus_dev_fatal(dev, err, fmt, args), but helps
357  * avoiding recursion within xenbus_switch_state.
358  */
359 static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,
360 				const char *fmt, ...)
361 {
362 	va_list ap;
363 
364 	va_start(ap, fmt);
365 	xenbus_va_dev_error(dev, err, fmt, ap);
366 	va_end(ap);
367 
368 	if (!depth)
369 		__xenbus_switch_state(dev, XenbusStateClosing, 1);
370 }
371 
372 /*
373  * xenbus_setup_ring
374  * @dev: xenbus device
375  * @vaddr: pointer to starting virtual address of the ring
376  * @nr_pages: number of pages to be granted
377  * @grefs: grant reference array to be filled in
378  *
379  * Allocate physically contiguous pages for a shared ring buffer and grant it
380  * to the peer of the given device. The ring buffer is initially filled with
381  * zeroes. The virtual address of the ring is stored at @vaddr and the
382  * grant references are stored in the @grefs array. In case of error @vaddr
383  * will be set to NULL and @grefs will be filled with INVALID_GRANT_REF.
384  */
385 int xenbus_setup_ring(struct xenbus_device *dev, gfp_t gfp, void **vaddr,
386 		      unsigned int nr_pages, grant_ref_t *grefs)
387 {
388 	unsigned long ring_size = nr_pages * XEN_PAGE_SIZE;
389 	grant_ref_t gref_head;
390 	unsigned int i;
391 	void *addr;
392 	int ret;
393 
394 	addr = *vaddr = alloc_pages_exact(ring_size, gfp | __GFP_ZERO);
395 	if (!*vaddr) {
396 		ret = -ENOMEM;
397 		goto err;
398 	}
399 
400 	ret = gnttab_alloc_grant_references(nr_pages, &gref_head);
401 	if (ret) {
402 		xenbus_dev_fatal(dev, ret, "granting access to %u ring pages",
403 				 nr_pages);
404 		goto err;
405 	}
406 
407 	for (i = 0; i < nr_pages; i++) {
408 		unsigned long gfn;
409 
410 		if (is_vmalloc_addr(*vaddr))
411 			gfn = pfn_to_gfn(vmalloc_to_pfn(addr));
412 		else
413 			gfn = virt_to_gfn(addr);
414 
415 		grefs[i] = gnttab_claim_grant_reference(&gref_head);
416 		gnttab_grant_foreign_access_ref(grefs[i], dev->otherend_id,
417 						gfn, 0);
418 
419 		addr += XEN_PAGE_SIZE;
420 	}
421 
422 	return 0;
423 
424  err:
425 	if (*vaddr)
426 		free_pages_exact(*vaddr, ring_size);
427 	for (i = 0; i < nr_pages; i++)
428 		grefs[i] = INVALID_GRANT_REF;
429 	*vaddr = NULL;
430 
431 	return ret;
432 }
433 EXPORT_SYMBOL_GPL(xenbus_setup_ring);
434 
435 /*
436  * xenbus_teardown_ring
437  * @vaddr: starting virtual address of the ring
438  * @nr_pages: number of pages
439  * @grefs: grant reference array
440  *
441  * Remove grants for the shared ring buffer and free the associated memory.
442  * On return the grant reference array is filled with INVALID_GRANT_REF.
443  */
444 void xenbus_teardown_ring(void **vaddr, unsigned int nr_pages,
445 			  grant_ref_t *grefs)
446 {
447 	unsigned int i;
448 
449 	for (i = 0; i < nr_pages; i++) {
450 		if (grefs[i] != INVALID_GRANT_REF) {
451 			gnttab_end_foreign_access(grefs[i], NULL);
452 			grefs[i] = INVALID_GRANT_REF;
453 		}
454 	}
455 
456 	if (*vaddr)
457 		free_pages_exact(*vaddr, nr_pages * XEN_PAGE_SIZE);
458 	*vaddr = NULL;
459 }
460 EXPORT_SYMBOL_GPL(xenbus_teardown_ring);
461 
462 /*
463  * Allocate an event channel for the given xenbus_device, assigning the newly
464  * created local port to *port.  Return 0 on success, or -errno on error.  On
465  * error, the device will switch to XenbusStateClosing, and the error will be
466  * saved in the store.
467  */
468 int xenbus_alloc_evtchn(struct xenbus_device *dev, evtchn_port_t *port)
469 {
470 	struct evtchn_alloc_unbound alloc_unbound;
471 	int err;
472 
473 	alloc_unbound.dom = DOMID_SELF;
474 	alloc_unbound.remote_dom = dev->otherend_id;
475 
476 	err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
477 					  &alloc_unbound);
478 	if (err)
479 		xenbus_dev_fatal(dev, err, "allocating event channel");
480 	else
481 		*port = alloc_unbound.port;
482 
483 	return err;
484 }
485 EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
486 
487 
488 /*
489  * Free an existing event channel. Returns 0 on success or -errno on error.
490  */
491 int xenbus_free_evtchn(struct xenbus_device *dev, evtchn_port_t port)
492 {
493 	struct evtchn_close close;
494 	int err;
495 
496 	close.port = port;
497 
498 	err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
499 	if (err)
500 		xenbus_dev_error(dev, err, "freeing event channel %u", port);
501 
502 	return err;
503 }
504 EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
505 
506 
507 /**
508  * xenbus_map_ring_valloc - allocate & map pages of VA space
509  * @dev: xenbus device
510  * @gnt_refs: grant reference array
511  * @nr_grefs: number of grant references
512  * @vaddr: pointer to address to be filled out by mapping
513  *
514  * Map @nr_grefs pages of memory into this domain from another
515  * domain's grant table.  xenbus_map_ring_valloc allocates @nr_grefs
516  * pages of virtual address space, maps the pages to that address, and sets
517  * *vaddr to that address.  If an error is returned, device will switch to
518  * XenbusStateClosing and the error message will be saved in XenStore.
519  *
520  * Returns: %0 on success or -errno on error
521  */
522 int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t *gnt_refs,
523 			   unsigned int nr_grefs, void **vaddr)
524 {
525 	int err;
526 	struct map_ring_valloc *info;
527 
528 	*vaddr = NULL;
529 
530 	if (nr_grefs > XENBUS_MAX_RING_GRANTS)
531 		return -EINVAL;
532 
533 	info = kzalloc(sizeof(*info), GFP_KERNEL);
534 	if (!info)
535 		return -ENOMEM;
536 
537 	info->node = kzalloc(sizeof(*info->node), GFP_KERNEL);
538 	if (!info->node)
539 		err = -ENOMEM;
540 	else
541 		err = ring_ops->map(dev, info, gnt_refs, nr_grefs, vaddr);
542 
543 	kfree(info->node);
544 	kfree(info);
545 	return err;
546 }
547 EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
548 
549 /* N.B. sizeof(phys_addr_t) doesn't always equal to sizeof(unsigned
550  * long), e.g. 32-on-64.  Caller is responsible for preparing the
551  * right array to feed into this function */
552 static int __xenbus_map_ring(struct xenbus_device *dev,
553 			     grant_ref_t *gnt_refs,
554 			     unsigned int nr_grefs,
555 			     grant_handle_t *handles,
556 			     struct map_ring_valloc *info,
557 			     unsigned int flags,
558 			     bool *leaked)
559 {
560 	int i, j;
561 
562 	if (nr_grefs > XENBUS_MAX_RING_GRANTS)
563 		return -EINVAL;
564 
565 	for (i = 0; i < nr_grefs; i++) {
566 		gnttab_set_map_op(&info->map[i], info->phys_addrs[i], flags,
567 				  gnt_refs[i], dev->otherend_id);
568 		handles[i] = INVALID_GRANT_HANDLE;
569 	}
570 
571 	gnttab_batch_map(info->map, i);
572 
573 	for (i = 0; i < nr_grefs; i++) {
574 		if (info->map[i].status != GNTST_okay) {
575 			xenbus_dev_fatal(dev, info->map[i].status,
576 					 "mapping in shared page %d from domain %d",
577 					 gnt_refs[i], dev->otherend_id);
578 			goto fail;
579 		} else
580 			handles[i] = info->map[i].handle;
581 	}
582 
583 	return 0;
584 
585  fail:
586 	for (i = j = 0; i < nr_grefs; i++) {
587 		if (handles[i] != INVALID_GRANT_HANDLE) {
588 			gnttab_set_unmap_op(&info->unmap[j],
589 					    info->phys_addrs[i],
590 					    GNTMAP_host_map, handles[i]);
591 			j++;
592 		}
593 	}
594 
595 	BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, info->unmap, j));
596 
597 	*leaked = false;
598 	for (i = 0; i < j; i++) {
599 		if (info->unmap[i].status != GNTST_okay) {
600 			*leaked = true;
601 			break;
602 		}
603 	}
604 
605 	return -ENOENT;
606 }
607 
608 /**
609  * xenbus_unmap_ring - unmap memory from another domain
610  * @dev: xenbus device
611  * @handles: grant handle array
612  * @nr_handles: number of handles in the array
613  * @vaddrs: addresses to unmap
614  *
615  * Unmap memory in this domain that was imported from another domain.
616  *
617  * Returns: %0 on success or GNTST_* on error
618  * (see xen/include/interface/grant_table.h).
619  */
620 static int xenbus_unmap_ring(struct xenbus_device *dev, grant_handle_t *handles,
621 			     unsigned int nr_handles, unsigned long *vaddrs)
622 {
623 	struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
624 	int i;
625 	int err;
626 
627 	if (nr_handles > XENBUS_MAX_RING_GRANTS)
628 		return -EINVAL;
629 
630 	for (i = 0; i < nr_handles; i++)
631 		gnttab_set_unmap_op(&unmap[i], vaddrs[i],
632 				    GNTMAP_host_map, handles[i]);
633 
634 	BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i));
635 
636 	err = GNTST_okay;
637 	for (i = 0; i < nr_handles; i++) {
638 		if (unmap[i].status != GNTST_okay) {
639 			xenbus_dev_error(dev, unmap[i].status,
640 					 "unmapping page at handle %d error %d",
641 					 handles[i], unmap[i].status);
642 			err = unmap[i].status;
643 			break;
644 		}
645 	}
646 
647 	return err;
648 }
649 
650 static void xenbus_map_ring_setup_grant_hvm(unsigned long gfn,
651 					    unsigned int goffset,
652 					    unsigned int len,
653 					    void *data)
654 {
655 	struct map_ring_valloc *info = data;
656 	unsigned long vaddr = (unsigned long)gfn_to_virt(gfn);
657 
658 	info->phys_addrs[info->idx] = vaddr;
659 	info->addrs[info->idx] = vaddr;
660 
661 	info->idx++;
662 }
663 
664 static int xenbus_map_ring_hvm(struct xenbus_device *dev,
665 			       struct map_ring_valloc *info,
666 			       grant_ref_t *gnt_ref,
667 			       unsigned int nr_grefs,
668 			       void **vaddr)
669 {
670 	struct xenbus_map_node *node = info->node;
671 	int err;
672 	void *addr;
673 	bool leaked = false;
674 	unsigned int nr_pages = XENBUS_PAGES(nr_grefs);
675 
676 	err = xen_alloc_unpopulated_pages(nr_pages, node->hvm.pages);
677 	if (err)
678 		goto out_err;
679 
680 	gnttab_foreach_grant(node->hvm.pages, nr_grefs,
681 			     xenbus_map_ring_setup_grant_hvm,
682 			     info);
683 
684 	err = __xenbus_map_ring(dev, gnt_ref, nr_grefs, node->handles,
685 				info, GNTMAP_host_map, &leaked);
686 	node->nr_handles = nr_grefs;
687 
688 	if (err)
689 		goto out_free_ballooned_pages;
690 
691 	addr = vmap(node->hvm.pages, nr_pages, VM_MAP | VM_IOREMAP,
692 		    PAGE_KERNEL);
693 	if (!addr) {
694 		err = -ENOMEM;
695 		goto out_xenbus_unmap_ring;
696 	}
697 
698 	node->hvm.addr = addr;
699 
700 	spin_lock(&xenbus_valloc_lock);
701 	list_add(&node->next, &xenbus_valloc_pages);
702 	spin_unlock(&xenbus_valloc_lock);
703 
704 	*vaddr = addr;
705 	info->node = NULL;
706 
707 	return 0;
708 
709  out_xenbus_unmap_ring:
710 	if (!leaked)
711 		xenbus_unmap_ring(dev, node->handles, nr_grefs, info->addrs);
712 	else
713 		pr_alert("leaking %p size %u page(s)",
714 			 addr, nr_pages);
715  out_free_ballooned_pages:
716 	if (!leaked)
717 		xen_free_unpopulated_pages(nr_pages, node->hvm.pages);
718  out_err:
719 	return err;
720 }
721 
722 /**
723  * xenbus_unmap_ring_vfree - unmap a page of memory from another domain
724  * @dev: xenbus device
725  * @vaddr: addr to unmap
726  *
727  * Based on Rusty Russell's skeleton driver's unmap_page.
728  * Unmap a page of memory in this domain that was imported from another domain.
729  * Use xenbus_unmap_ring_vfree if you mapped in your memory with
730  * xenbus_map_ring_valloc (it will free the virtual address space).
731  *
732  * Returns: %0 on success or GNTST_* on error
733  * (see xen/include/interface/grant_table.h).
734  */
735 int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
736 {
737 	return ring_ops->unmap(dev, vaddr);
738 }
739 EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
740 
741 #ifdef CONFIG_XEN_PV
742 static int map_ring_apply(pte_t *pte, unsigned long addr, void *data)
743 {
744 	struct map_ring_valloc *info = data;
745 
746 	info->phys_addrs[info->idx++] = arbitrary_virt_to_machine(pte).maddr;
747 	return 0;
748 }
749 
750 static int xenbus_map_ring_pv(struct xenbus_device *dev,
751 			      struct map_ring_valloc *info,
752 			      grant_ref_t *gnt_refs,
753 			      unsigned int nr_grefs,
754 			      void **vaddr)
755 {
756 	struct xenbus_map_node *node = info->node;
757 	struct vm_struct *area;
758 	bool leaked = false;
759 	int err = -ENOMEM;
760 
761 	area = get_vm_area(XEN_PAGE_SIZE * nr_grefs, VM_IOREMAP);
762 	if (!area)
763 		return -ENOMEM;
764 	if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
765 				XEN_PAGE_SIZE * nr_grefs, map_ring_apply, info))
766 		goto failed;
767 	err = __xenbus_map_ring(dev, gnt_refs, nr_grefs, node->handles,
768 				info, GNTMAP_host_map | GNTMAP_contains_pte,
769 				&leaked);
770 	if (err)
771 		goto failed;
772 
773 	node->nr_handles = nr_grefs;
774 	node->pv.area = area;
775 
776 	spin_lock(&xenbus_valloc_lock);
777 	list_add(&node->next, &xenbus_valloc_pages);
778 	spin_unlock(&xenbus_valloc_lock);
779 
780 	*vaddr = area->addr;
781 	info->node = NULL;
782 
783 	return 0;
784 
785 failed:
786 	if (!leaked)
787 		free_vm_area(area);
788 	else
789 		pr_alert("leaking VM area %p size %u page(s)", area, nr_grefs);
790 
791 	return err;
792 }
793 
794 static int xenbus_unmap_ring_pv(struct xenbus_device *dev, void *vaddr)
795 {
796 	struct xenbus_map_node *node;
797 	struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
798 	unsigned int level;
799 	int i;
800 	bool leaked = false;
801 	int err;
802 
803 	spin_lock(&xenbus_valloc_lock);
804 	list_for_each_entry(node, &xenbus_valloc_pages, next) {
805 		if (node->pv.area->addr == vaddr) {
806 			list_del(&node->next);
807 			goto found;
808 		}
809 	}
810 	node = NULL;
811  found:
812 	spin_unlock(&xenbus_valloc_lock);
813 
814 	if (!node) {
815 		xenbus_dev_error(dev, -ENOENT,
816 				 "can't find mapped virtual address %p", vaddr);
817 		return GNTST_bad_virt_addr;
818 	}
819 
820 	for (i = 0; i < node->nr_handles; i++) {
821 		unsigned long addr;
822 
823 		memset(&unmap[i], 0, sizeof(unmap[i]));
824 		addr = (unsigned long)vaddr + (XEN_PAGE_SIZE * i);
825 		unmap[i].host_addr = arbitrary_virt_to_machine(
826 			lookup_address(addr, &level)).maddr;
827 		unmap[i].dev_bus_addr = 0;
828 		unmap[i].handle = node->handles[i];
829 	}
830 
831 	BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i));
832 
833 	err = GNTST_okay;
834 	leaked = false;
835 	for (i = 0; i < node->nr_handles; i++) {
836 		if (unmap[i].status != GNTST_okay) {
837 			leaked = true;
838 			xenbus_dev_error(dev, unmap[i].status,
839 					 "unmapping page at handle %d error %d",
840 					 node->handles[i], unmap[i].status);
841 			err = unmap[i].status;
842 			break;
843 		}
844 	}
845 
846 	if (!leaked)
847 		free_vm_area(node->pv.area);
848 	else
849 		pr_alert("leaking VM area %p size %u page(s)",
850 			 node->pv.area, node->nr_handles);
851 
852 	kfree(node);
853 	return err;
854 }
855 
856 static const struct xenbus_ring_ops ring_ops_pv = {
857 	.map = xenbus_map_ring_pv,
858 	.unmap = xenbus_unmap_ring_pv,
859 };
860 #endif
861 
862 struct unmap_ring_hvm
863 {
864 	unsigned int idx;
865 	unsigned long addrs[XENBUS_MAX_RING_GRANTS];
866 };
867 
868 static void xenbus_unmap_ring_setup_grant_hvm(unsigned long gfn,
869 					      unsigned int goffset,
870 					      unsigned int len,
871 					      void *data)
872 {
873 	struct unmap_ring_hvm *info = data;
874 
875 	info->addrs[info->idx] = (unsigned long)gfn_to_virt(gfn);
876 
877 	info->idx++;
878 }
879 
880 static int xenbus_unmap_ring_hvm(struct xenbus_device *dev, void *vaddr)
881 {
882 	int rv;
883 	struct xenbus_map_node *node;
884 	void *addr;
885 	struct unmap_ring_hvm info = {
886 		.idx = 0,
887 	};
888 	unsigned int nr_pages;
889 
890 	spin_lock(&xenbus_valloc_lock);
891 	list_for_each_entry(node, &xenbus_valloc_pages, next) {
892 		addr = node->hvm.addr;
893 		if (addr == vaddr) {
894 			list_del(&node->next);
895 			goto found;
896 		}
897 	}
898 	node = addr = NULL;
899  found:
900 	spin_unlock(&xenbus_valloc_lock);
901 
902 	if (!node) {
903 		xenbus_dev_error(dev, -ENOENT,
904 				 "can't find mapped virtual address %p", vaddr);
905 		return GNTST_bad_virt_addr;
906 	}
907 
908 	nr_pages = XENBUS_PAGES(node->nr_handles);
909 
910 	gnttab_foreach_grant(node->hvm.pages, node->nr_handles,
911 			     xenbus_unmap_ring_setup_grant_hvm,
912 			     &info);
913 
914 	rv = xenbus_unmap_ring(dev, node->handles, node->nr_handles,
915 			       info.addrs);
916 	if (!rv) {
917 		vunmap(vaddr);
918 		xen_free_unpopulated_pages(nr_pages, node->hvm.pages);
919 	}
920 	else
921 		WARN(1, "Leaking %p, size %u page(s)\n", vaddr, nr_pages);
922 
923 	kfree(node);
924 	return rv;
925 }
926 
927 /**
928  * xenbus_read_driver_state - read state from a store path
929  * @path: path for driver
930  *
931  * Returns: the state of the driver rooted at the given store path, or
932  * XenbusStateUnknown if no state can be read.
933  */
934 enum xenbus_state xenbus_read_driver_state(const char *path)
935 {
936 	enum xenbus_state result;
937 	int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
938 	if (err)
939 		result = XenbusStateUnknown;
940 
941 	return result;
942 }
943 EXPORT_SYMBOL_GPL(xenbus_read_driver_state);
944 
945 static const struct xenbus_ring_ops ring_ops_hvm = {
946 	.map = xenbus_map_ring_hvm,
947 	.unmap = xenbus_unmap_ring_hvm,
948 };
949 
950 void __init xenbus_ring_ops_init(void)
951 {
952 #ifdef CONFIG_XEN_PV
953 	if (!xen_feature(XENFEAT_auto_translated_physmap))
954 		ring_ops = &ring_ops_pv;
955 	else
956 #endif
957 		ring_ops = &ring_ops_hvm;
958 }
959