xref: /linux/drivers/xen/xenbus/xenbus_client.c (revision e0bf6c5ca2d3281f231c5f0c9bf145e9513644de)
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 <asm/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_probe.h"
51 
52 struct xenbus_map_node {
53 	struct list_head next;
54 	union {
55 		struct vm_struct *area; /* PV */
56 		struct page *page;     /* HVM */
57 	};
58 	grant_handle_t handle;
59 };
60 
61 static DEFINE_SPINLOCK(xenbus_valloc_lock);
62 static LIST_HEAD(xenbus_valloc_pages);
63 
64 struct xenbus_ring_ops {
65 	int (*map)(struct xenbus_device *dev, int gnt, void **vaddr);
66 	int (*unmap)(struct xenbus_device *dev, void *vaddr);
67 };
68 
69 static const struct xenbus_ring_ops *ring_ops __read_mostly;
70 
71 const char *xenbus_strstate(enum xenbus_state state)
72 {
73 	static const char *const name[] = {
74 		[ XenbusStateUnknown      ] = "Unknown",
75 		[ XenbusStateInitialising ] = "Initialising",
76 		[ XenbusStateInitWait     ] = "InitWait",
77 		[ XenbusStateInitialised  ] = "Initialised",
78 		[ XenbusStateConnected    ] = "Connected",
79 		[ XenbusStateClosing      ] = "Closing",
80 		[ XenbusStateClosed	  ] = "Closed",
81 		[XenbusStateReconfiguring] = "Reconfiguring",
82 		[XenbusStateReconfigured] = "Reconfigured",
83 	};
84 	return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
85 }
86 EXPORT_SYMBOL_GPL(xenbus_strstate);
87 
88 /**
89  * xenbus_watch_path - register a watch
90  * @dev: xenbus device
91  * @path: path to watch
92  * @watch: watch to register
93  * @callback: callback to register
94  *
95  * Register a @watch on the given path, using the given xenbus_watch structure
96  * for storage, and the given @callback function as the callback.  Return 0 on
97  * success, or -errno on error.  On success, the given @path will be saved as
98  * @watch->node, and remains the caller's to free.  On error, @watch->node will
99  * be NULL, the device will switch to %XenbusStateClosing, and the error will
100  * be saved in the store.
101  */
102 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
103 		      struct xenbus_watch *watch,
104 		      void (*callback)(struct xenbus_watch *,
105 				       const char **, unsigned int))
106 {
107 	int err;
108 
109 	watch->node = path;
110 	watch->callback = callback;
111 
112 	err = register_xenbus_watch(watch);
113 
114 	if (err) {
115 		watch->node = NULL;
116 		watch->callback = NULL;
117 		xenbus_dev_fatal(dev, err, "adding watch on %s", path);
118 	}
119 
120 	return err;
121 }
122 EXPORT_SYMBOL_GPL(xenbus_watch_path);
123 
124 
125 /**
126  * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
127  * @dev: xenbus device
128  * @watch: watch to register
129  * @callback: callback to register
130  * @pathfmt: format of path to watch
131  *
132  * Register a watch on the given @path, using the given xenbus_watch
133  * structure for storage, and the given @callback function as the callback.
134  * Return 0 on success, or -errno on error.  On success, the watched path
135  * (@path/@path2) will be saved as @watch->node, and becomes the caller's to
136  * kfree().  On error, watch->node will be NULL, so the caller has nothing to
137  * free, the device will switch to %XenbusStateClosing, and the error will be
138  * saved in the store.
139  */
140 int xenbus_watch_pathfmt(struct xenbus_device *dev,
141 			 struct xenbus_watch *watch,
142 			 void (*callback)(struct xenbus_watch *,
143 					const char **, unsigned int),
144 			 const char *pathfmt, ...)
145 {
146 	int err;
147 	va_list ap;
148 	char *path;
149 
150 	va_start(ap, pathfmt);
151 	path = kvasprintf(GFP_NOIO | __GFP_HIGH, pathfmt, ap);
152 	va_end(ap);
153 
154 	if (!path) {
155 		xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
156 		return -ENOMEM;
157 	}
158 	err = xenbus_watch_path(dev, path, watch, callback);
159 
160 	if (err)
161 		kfree(path);
162 	return err;
163 }
164 EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);
165 
166 static void xenbus_switch_fatal(struct xenbus_device *, int, int,
167 				const char *, ...);
168 
169 static int
170 __xenbus_switch_state(struct xenbus_device *dev,
171 		      enum xenbus_state state, int depth)
172 {
173 	/* We check whether the state is currently set to the given value, and
174 	   if not, then the state is set.  We don't want to unconditionally
175 	   write the given state, because we don't want to fire watches
176 	   unnecessarily.  Furthermore, if the node has gone, we don't write
177 	   to it, as the device will be tearing down, and we don't want to
178 	   resurrect that directory.
179 
180 	   Note that, because of this cached value of our state, this
181 	   function will not take a caller's Xenstore transaction
182 	   (something it was trying to in the past) because dev->state
183 	   would not get reset if the transaction was aborted.
184 	 */
185 
186 	struct xenbus_transaction xbt;
187 	int current_state;
188 	int err, abort;
189 
190 	if (state == dev->state)
191 		return 0;
192 
193 again:
194 	abort = 1;
195 
196 	err = xenbus_transaction_start(&xbt);
197 	if (err) {
198 		xenbus_switch_fatal(dev, depth, err, "starting transaction");
199 		return 0;
200 	}
201 
202 	err = xenbus_scanf(xbt, dev->nodename, "state", "%d", &current_state);
203 	if (err != 1)
204 		goto abort;
205 
206 	err = xenbus_printf(xbt, dev->nodename, "state", "%d", state);
207 	if (err) {
208 		xenbus_switch_fatal(dev, depth, err, "writing new state");
209 		goto abort;
210 	}
211 
212 	abort = 0;
213 abort:
214 	err = xenbus_transaction_end(xbt, abort);
215 	if (err) {
216 		if (err == -EAGAIN && !abort)
217 			goto again;
218 		xenbus_switch_fatal(dev, depth, err, "ending transaction");
219 	} else
220 		dev->state = state;
221 
222 	return 0;
223 }
224 
225 /**
226  * xenbus_switch_state
227  * @dev: xenbus device
228  * @state: new state
229  *
230  * Advertise in the store a change of the given driver to the given new_state.
231  * Return 0 on success, or -errno on error.  On error, the device will switch
232  * to XenbusStateClosing, and the error will be saved in the store.
233  */
234 int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
235 {
236 	return __xenbus_switch_state(dev, state, 0);
237 }
238 
239 EXPORT_SYMBOL_GPL(xenbus_switch_state);
240 
241 int xenbus_frontend_closed(struct xenbus_device *dev)
242 {
243 	xenbus_switch_state(dev, XenbusStateClosed);
244 	complete(&dev->down);
245 	return 0;
246 }
247 EXPORT_SYMBOL_GPL(xenbus_frontend_closed);
248 
249 /**
250  * Return the path to the error node for the given device, or NULL on failure.
251  * If the value returned is non-NULL, then it is the caller's to kfree.
252  */
253 static char *error_path(struct xenbus_device *dev)
254 {
255 	return kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
256 }
257 
258 
259 static void xenbus_va_dev_error(struct xenbus_device *dev, int err,
260 				const char *fmt, va_list ap)
261 {
262 	unsigned int len;
263 	char *printf_buffer = NULL;
264 	char *path_buffer = NULL;
265 
266 #define PRINTF_BUFFER_SIZE 4096
267 	printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
268 	if (printf_buffer == NULL)
269 		goto fail;
270 
271 	len = sprintf(printf_buffer, "%i ", -err);
272 	vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
273 
274 	dev_err(&dev->dev, "%s\n", printf_buffer);
275 
276 	path_buffer = error_path(dev);
277 
278 	if (path_buffer == NULL) {
279 		dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
280 		       dev->nodename, printf_buffer);
281 		goto fail;
282 	}
283 
284 	if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
285 		dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
286 		       dev->nodename, printf_buffer);
287 		goto fail;
288 	}
289 
290 fail:
291 	kfree(printf_buffer);
292 	kfree(path_buffer);
293 }
294 
295 
296 /**
297  * xenbus_dev_error
298  * @dev: xenbus device
299  * @err: error to report
300  * @fmt: error message format
301  *
302  * Report the given negative errno into the store, along with the given
303  * formatted message.
304  */
305 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
306 {
307 	va_list ap;
308 
309 	va_start(ap, fmt);
310 	xenbus_va_dev_error(dev, err, fmt, ap);
311 	va_end(ap);
312 }
313 EXPORT_SYMBOL_GPL(xenbus_dev_error);
314 
315 /**
316  * xenbus_dev_fatal
317  * @dev: xenbus device
318  * @err: error to report
319  * @fmt: error message format
320  *
321  * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
322  * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
323  * closedown of this driver and its peer.
324  */
325 
326 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
327 {
328 	va_list ap;
329 
330 	va_start(ap, fmt);
331 	xenbus_va_dev_error(dev, err, fmt, ap);
332 	va_end(ap);
333 
334 	xenbus_switch_state(dev, XenbusStateClosing);
335 }
336 EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
337 
338 /**
339  * Equivalent to xenbus_dev_fatal(dev, err, fmt, args), but helps
340  * avoiding recursion within xenbus_switch_state.
341  */
342 static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,
343 				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 	if (!depth)
352 		__xenbus_switch_state(dev, XenbusStateClosing, 1);
353 }
354 
355 /**
356  * xenbus_grant_ring
357  * @dev: xenbus device
358  * @ring_mfn: mfn of ring to grant
359 
360  * Grant access to the given @ring_mfn to the peer of the given device.  Return
361  * a grant reference on success, or -errno on error. On error, the device will
362  * switch to XenbusStateClosing, and the error will be saved in the store.
363  */
364 int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn)
365 {
366 	int err = gnttab_grant_foreign_access(dev->otherend_id, ring_mfn, 0);
367 	if (err < 0)
368 		xenbus_dev_fatal(dev, err, "granting access to ring page");
369 	return err;
370 }
371 EXPORT_SYMBOL_GPL(xenbus_grant_ring);
372 
373 
374 /**
375  * Allocate an event channel for the given xenbus_device, assigning the newly
376  * created local port to *port.  Return 0 on success, or -errno on error.  On
377  * error, the device will switch to XenbusStateClosing, and the error will be
378  * saved in the store.
379  */
380 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
381 {
382 	struct evtchn_alloc_unbound alloc_unbound;
383 	int err;
384 
385 	alloc_unbound.dom = DOMID_SELF;
386 	alloc_unbound.remote_dom = dev->otherend_id;
387 
388 	err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
389 					  &alloc_unbound);
390 	if (err)
391 		xenbus_dev_fatal(dev, err, "allocating event channel");
392 	else
393 		*port = alloc_unbound.port;
394 
395 	return err;
396 }
397 EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
398 
399 
400 /**
401  * Free an existing event channel. Returns 0 on success or -errno on error.
402  */
403 int xenbus_free_evtchn(struct xenbus_device *dev, int port)
404 {
405 	struct evtchn_close close;
406 	int err;
407 
408 	close.port = port;
409 
410 	err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
411 	if (err)
412 		xenbus_dev_error(dev, err, "freeing event channel %d", port);
413 
414 	return err;
415 }
416 EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
417 
418 
419 /**
420  * xenbus_map_ring_valloc
421  * @dev: xenbus device
422  * @gnt_ref: grant reference
423  * @vaddr: pointer to address to be filled out by mapping
424  *
425  * Based on Rusty Russell's skeleton driver's map_page.
426  * Map a page of memory into this domain from another domain's grant table.
427  * xenbus_map_ring_valloc allocates a page of virtual address space, maps the
428  * page to that address, and sets *vaddr to that address.
429  * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
430  * or -ENOMEM on error. If an error is returned, device will switch to
431  * XenbusStateClosing and the error message will be saved in XenStore.
432  */
433 int xenbus_map_ring_valloc(struct xenbus_device *dev, int gnt_ref, void **vaddr)
434 {
435 	return ring_ops->map(dev, gnt_ref, vaddr);
436 }
437 EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
438 
439 static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
440 				     int gnt_ref, void **vaddr)
441 {
442 	struct gnttab_map_grant_ref op = {
443 		.flags = GNTMAP_host_map | GNTMAP_contains_pte,
444 		.ref   = gnt_ref,
445 		.dom   = dev->otherend_id,
446 	};
447 	struct xenbus_map_node *node;
448 	struct vm_struct *area;
449 	pte_t *pte;
450 
451 	*vaddr = NULL;
452 
453 	node = kzalloc(sizeof(*node), GFP_KERNEL);
454 	if (!node)
455 		return -ENOMEM;
456 
457 	area = alloc_vm_area(PAGE_SIZE, &pte);
458 	if (!area) {
459 		kfree(node);
460 		return -ENOMEM;
461 	}
462 
463 	op.host_addr = arbitrary_virt_to_machine(pte).maddr;
464 
465 	gnttab_batch_map(&op, 1);
466 
467 	if (op.status != GNTST_okay) {
468 		free_vm_area(area);
469 		kfree(node);
470 		xenbus_dev_fatal(dev, op.status,
471 				 "mapping in shared page %d from domain %d",
472 				 gnt_ref, dev->otherend_id);
473 		return op.status;
474 	}
475 
476 	node->handle = op.handle;
477 	node->area = area;
478 
479 	spin_lock(&xenbus_valloc_lock);
480 	list_add(&node->next, &xenbus_valloc_pages);
481 	spin_unlock(&xenbus_valloc_lock);
482 
483 	*vaddr = area->addr;
484 	return 0;
485 }
486 
487 static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev,
488 				      int gnt_ref, void **vaddr)
489 {
490 	struct xenbus_map_node *node;
491 	int err;
492 	void *addr;
493 
494 	*vaddr = NULL;
495 
496 	node = kzalloc(sizeof(*node), GFP_KERNEL);
497 	if (!node)
498 		return -ENOMEM;
499 
500 	err = alloc_xenballooned_pages(1, &node->page, false /* lowmem */);
501 	if (err)
502 		goto out_err;
503 
504 	addr = pfn_to_kaddr(page_to_pfn(node->page));
505 
506 	err = xenbus_map_ring(dev, gnt_ref, &node->handle, addr);
507 	if (err)
508 		goto out_err_free_ballooned_pages;
509 
510 	spin_lock(&xenbus_valloc_lock);
511 	list_add(&node->next, &xenbus_valloc_pages);
512 	spin_unlock(&xenbus_valloc_lock);
513 
514 	*vaddr = addr;
515 	return 0;
516 
517  out_err_free_ballooned_pages:
518 	free_xenballooned_pages(1, &node->page);
519  out_err:
520 	kfree(node);
521 	return err;
522 }
523 
524 
525 /**
526  * xenbus_map_ring
527  * @dev: xenbus device
528  * @gnt_ref: grant reference
529  * @handle: pointer to grant handle to be filled
530  * @vaddr: address to be mapped to
531  *
532  * Map a page of memory into this domain from another domain's grant table.
533  * xenbus_map_ring does not allocate the virtual address space (you must do
534  * this yourself!). It only maps in the page to the specified address.
535  * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
536  * or -ENOMEM on error. If an error is returned, device will switch to
537  * XenbusStateClosing and the error message will be saved in XenStore.
538  */
539 int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
540 		    grant_handle_t *handle, void *vaddr)
541 {
542 	struct gnttab_map_grant_ref op;
543 
544 	gnttab_set_map_op(&op, (unsigned long)vaddr, GNTMAP_host_map, gnt_ref,
545 			  dev->otherend_id);
546 
547 	gnttab_batch_map(&op, 1);
548 
549 	if (op.status != GNTST_okay) {
550 		xenbus_dev_fatal(dev, op.status,
551 				 "mapping in shared page %d from domain %d",
552 				 gnt_ref, dev->otherend_id);
553 	} else
554 		*handle = op.handle;
555 
556 	return op.status;
557 }
558 EXPORT_SYMBOL_GPL(xenbus_map_ring);
559 
560 
561 /**
562  * xenbus_unmap_ring_vfree
563  * @dev: xenbus device
564  * @vaddr: addr to unmap
565  *
566  * Based on Rusty Russell's skeleton driver's unmap_page.
567  * Unmap a page of memory in this domain that was imported from another domain.
568  * Use xenbus_unmap_ring_vfree if you mapped in your memory with
569  * xenbus_map_ring_valloc (it will free the virtual address space).
570  * Returns 0 on success and returns GNTST_* on error
571  * (see xen/include/interface/grant_table.h).
572  */
573 int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
574 {
575 	return ring_ops->unmap(dev, vaddr);
576 }
577 EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
578 
579 static int xenbus_unmap_ring_vfree_pv(struct xenbus_device *dev, void *vaddr)
580 {
581 	struct xenbus_map_node *node;
582 	struct gnttab_unmap_grant_ref op = {
583 		.host_addr = (unsigned long)vaddr,
584 	};
585 	unsigned int level;
586 
587 	spin_lock(&xenbus_valloc_lock);
588 	list_for_each_entry(node, &xenbus_valloc_pages, next) {
589 		if (node->area->addr == vaddr) {
590 			list_del(&node->next);
591 			goto found;
592 		}
593 	}
594 	node = NULL;
595  found:
596 	spin_unlock(&xenbus_valloc_lock);
597 
598 	if (!node) {
599 		xenbus_dev_error(dev, -ENOENT,
600 				 "can't find mapped virtual address %p", vaddr);
601 		return GNTST_bad_virt_addr;
602 	}
603 
604 	op.handle = node->handle;
605 	op.host_addr = arbitrary_virt_to_machine(
606 		lookup_address((unsigned long)vaddr, &level)).maddr;
607 
608 	if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
609 		BUG();
610 
611 	if (op.status == GNTST_okay)
612 		free_vm_area(node->area);
613 	else
614 		xenbus_dev_error(dev, op.status,
615 				 "unmapping page at handle %d error %d",
616 				 node->handle, op.status);
617 
618 	kfree(node);
619 	return op.status;
620 }
621 
622 static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr)
623 {
624 	int rv;
625 	struct xenbus_map_node *node;
626 	void *addr;
627 
628 	spin_lock(&xenbus_valloc_lock);
629 	list_for_each_entry(node, &xenbus_valloc_pages, next) {
630 		addr = pfn_to_kaddr(page_to_pfn(node->page));
631 		if (addr == vaddr) {
632 			list_del(&node->next);
633 			goto found;
634 		}
635 	}
636 	node = addr = NULL;
637  found:
638 	spin_unlock(&xenbus_valloc_lock);
639 
640 	if (!node) {
641 		xenbus_dev_error(dev, -ENOENT,
642 				 "can't find mapped virtual address %p", vaddr);
643 		return GNTST_bad_virt_addr;
644 	}
645 
646 	rv = xenbus_unmap_ring(dev, node->handle, addr);
647 
648 	if (!rv)
649 		free_xenballooned_pages(1, &node->page);
650 	else
651 		WARN(1, "Leaking %p\n", vaddr);
652 
653 	kfree(node);
654 	return rv;
655 }
656 
657 /**
658  * xenbus_unmap_ring
659  * @dev: xenbus device
660  * @handle: grant handle
661  * @vaddr: addr to unmap
662  *
663  * Unmap a page of memory in this domain that was imported from another domain.
664  * Returns 0 on success and returns GNTST_* on error
665  * (see xen/include/interface/grant_table.h).
666  */
667 int xenbus_unmap_ring(struct xenbus_device *dev,
668 		      grant_handle_t handle, void *vaddr)
669 {
670 	struct gnttab_unmap_grant_ref op;
671 
672 	gnttab_set_unmap_op(&op, (unsigned long)vaddr, GNTMAP_host_map, handle);
673 
674 	if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
675 		BUG();
676 
677 	if (op.status != GNTST_okay)
678 		xenbus_dev_error(dev, op.status,
679 				 "unmapping page at handle %d error %d",
680 				 handle, op.status);
681 
682 	return op.status;
683 }
684 EXPORT_SYMBOL_GPL(xenbus_unmap_ring);
685 
686 
687 /**
688  * xenbus_read_driver_state
689  * @path: path for driver
690  *
691  * Return the state of the driver rooted at the given store path, or
692  * XenbusStateUnknown if no state can be read.
693  */
694 enum xenbus_state xenbus_read_driver_state(const char *path)
695 {
696 	enum xenbus_state result;
697 	int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
698 	if (err)
699 		result = XenbusStateUnknown;
700 
701 	return result;
702 }
703 EXPORT_SYMBOL_GPL(xenbus_read_driver_state);
704 
705 static const struct xenbus_ring_ops ring_ops_pv = {
706 	.map = xenbus_map_ring_valloc_pv,
707 	.unmap = xenbus_unmap_ring_vfree_pv,
708 };
709 
710 static const struct xenbus_ring_ops ring_ops_hvm = {
711 	.map = xenbus_map_ring_valloc_hvm,
712 	.unmap = xenbus_unmap_ring_vfree_hvm,
713 };
714 
715 void __init xenbus_ring_ops_init(void)
716 {
717 	if (!xen_feature(XENFEAT_auto_translated_physmap))
718 		ring_ops = &ring_ops_pv;
719 	else
720 		ring_ops = &ring_ops_hvm;
721 }
722