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.
6 * Copyright (C) 2005 XenSource Ltd
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:
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:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
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
33 #include <linux/slab.h>
34 #include <linux/types.h>
35 #include <linux/vmalloc.h>
36 #include <linux/export.h>
37 #include <asm/xen/hypervisor.h>
38 #include <asm/xen/page.h>
39 #include <xen/interface/xen.h>
40 #include <xen/interface/event_channel.h>
41 #include <xen/events.h>
42 #include <xen/grant_table.h>
43 #include <xen/xenbus.h>
45 const char *xenbus_strstate(enum xenbus_state state
)
47 static const char *const name
[] = {
48 [ XenbusStateUnknown
] = "Unknown",
49 [ XenbusStateInitialising
] = "Initialising",
50 [ XenbusStateInitWait
] = "InitWait",
51 [ XenbusStateInitialised
] = "Initialised",
52 [ XenbusStateConnected
] = "Connected",
53 [ XenbusStateClosing
] = "Closing",
54 [ XenbusStateClosed
] = "Closed",
55 [XenbusStateReconfiguring
] = "Reconfiguring",
56 [XenbusStateReconfigured
] = "Reconfigured",
58 return (state
< ARRAY_SIZE(name
)) ? name
[state
] : "INVALID";
60 EXPORT_SYMBOL_GPL(xenbus_strstate
);
63 * xenbus_watch_path - register a watch
65 * @path: path to watch
66 * @watch: watch to register
67 * @callback: callback to register
69 * Register a @watch on the given path, using the given xenbus_watch structure
70 * for storage, and the given @callback function as the callback. Return 0 on
71 * success, or -errno on error. On success, the given @path will be saved as
72 * @watch->node, and remains the caller's to free. On error, @watch->node will
73 * be NULL, the device will switch to %XenbusStateClosing, and the error will
74 * be saved in the store.
76 int xenbus_watch_path(struct xenbus_device
*dev
, const char *path
,
77 struct xenbus_watch
*watch
,
78 void (*callback
)(struct xenbus_watch
*,
79 const char **, unsigned int))
84 watch
->callback
= callback
;
86 err
= register_xenbus_watch(watch
);
90 watch
->callback
= NULL
;
91 xenbus_dev_fatal(dev
, err
, "adding watch on %s", path
);
96 EXPORT_SYMBOL_GPL(xenbus_watch_path
);
100 * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
101 * @dev: xenbus device
102 * @watch: watch to register
103 * @callback: callback to register
104 * @pathfmt: format of path to watch
106 * Register a watch on the given @path, using the given xenbus_watch
107 * structure for storage, and the given @callback function as the callback.
108 * Return 0 on success, or -errno on error. On success, the watched path
109 * (@path/@path2) will be saved as @watch->node, and becomes the caller's to
110 * kfree(). On error, watch->node will be NULL, so the caller has nothing to
111 * free, the device will switch to %XenbusStateClosing, and the error will be
112 * saved in the store.
114 int xenbus_watch_pathfmt(struct xenbus_device
*dev
,
115 struct xenbus_watch
*watch
,
116 void (*callback
)(struct xenbus_watch
*,
117 const char **, unsigned int),
118 const char *pathfmt
, ...)
124 va_start(ap
, pathfmt
);
125 path
= kvasprintf(GFP_NOIO
| __GFP_HIGH
, pathfmt
, ap
);
129 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating path for watch");
132 err
= xenbus_watch_path(dev
, path
, watch
, callback
);
138 EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt
);
140 static void xenbus_switch_fatal(struct xenbus_device
*, int, int,
144 __xenbus_switch_state(struct xenbus_device
*dev
,
145 enum xenbus_state state
, int depth
)
147 /* We check whether the state is currently set to the given value, and
148 if not, then the state is set. We don't want to unconditionally
149 write the given state, because we don't want to fire watches
150 unnecessarily. Furthermore, if the node has gone, we don't write
151 to it, as the device will be tearing down, and we don't want to
152 resurrect that directory.
154 Note that, because of this cached value of our state, this
155 function will not take a caller's Xenstore transaction
156 (something it was trying to in the past) because dev->state
157 would not get reset if the transaction was aborted.
160 struct xenbus_transaction xbt
;
164 if (state
== dev
->state
)
170 err
= xenbus_transaction_start(&xbt
);
172 xenbus_switch_fatal(dev
, depth
, err
, "starting transaction");
176 err
= xenbus_scanf(xbt
, dev
->nodename
, "state", "%d", ¤t_state
);
180 err
= xenbus_printf(xbt
, dev
->nodename
, "state", "%d", state
);
182 xenbus_switch_fatal(dev
, depth
, err
, "writing new state");
188 err
= xenbus_transaction_end(xbt
, abort
);
190 if (err
== -EAGAIN
&& !abort
)
192 xenbus_switch_fatal(dev
, depth
, err
, "ending transaction");
200 * xenbus_switch_state
201 * @dev: xenbus device
204 * Advertise in the store a change of the given driver to the given new_state.
205 * Return 0 on success, or -errno on error. On error, the device will switch
206 * to XenbusStateClosing, and the error will be saved in the store.
208 int xenbus_switch_state(struct xenbus_device
*dev
, enum xenbus_state state
)
210 return __xenbus_switch_state(dev
, state
, 0);
213 EXPORT_SYMBOL_GPL(xenbus_switch_state
);
215 int xenbus_frontend_closed(struct xenbus_device
*dev
)
217 xenbus_switch_state(dev
, XenbusStateClosed
);
218 complete(&dev
->down
);
221 EXPORT_SYMBOL_GPL(xenbus_frontend_closed
);
224 * Return the path to the error node for the given device, or NULL on failure.
225 * If the value returned is non-NULL, then it is the caller's to kfree.
227 static char *error_path(struct xenbus_device
*dev
)
229 return kasprintf(GFP_KERNEL
, "error/%s", dev
->nodename
);
233 static void xenbus_va_dev_error(struct xenbus_device
*dev
, int err
,
234 const char *fmt
, va_list ap
)
238 char *printf_buffer
= NULL
;
239 char *path_buffer
= NULL
;
241 #define PRINTF_BUFFER_SIZE 4096
242 printf_buffer
= kmalloc(PRINTF_BUFFER_SIZE
, GFP_KERNEL
);
243 if (printf_buffer
== NULL
)
246 len
= sprintf(printf_buffer
, "%i ", -err
);
247 ret
= vsnprintf(printf_buffer
+len
, PRINTF_BUFFER_SIZE
-len
, fmt
, ap
);
249 BUG_ON(len
+ ret
> PRINTF_BUFFER_SIZE
-1);
251 dev_err(&dev
->dev
, "%s\n", printf_buffer
);
253 path_buffer
= error_path(dev
);
255 if (path_buffer
== NULL
) {
256 dev_err(&dev
->dev
, "failed to write error node for %s (%s)\n",
257 dev
->nodename
, printf_buffer
);
261 if (xenbus_write(XBT_NIL
, path_buffer
, "error", printf_buffer
) != 0) {
262 dev_err(&dev
->dev
, "failed to write error node for %s (%s)\n",
263 dev
->nodename
, printf_buffer
);
268 kfree(printf_buffer
);
275 * @dev: xenbus device
276 * @err: error to report
277 * @fmt: error message format
279 * Report the given negative errno into the store, along with the given
282 void xenbus_dev_error(struct xenbus_device
*dev
, int err
, const char *fmt
, ...)
287 xenbus_va_dev_error(dev
, err
, fmt
, ap
);
290 EXPORT_SYMBOL_GPL(xenbus_dev_error
);
294 * @dev: xenbus device
295 * @err: error to report
296 * @fmt: error message format
298 * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
299 * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
300 * closedown of this driver and its peer.
303 void xenbus_dev_fatal(struct xenbus_device
*dev
, int err
, const char *fmt
, ...)
308 xenbus_va_dev_error(dev
, err
, fmt
, ap
);
311 xenbus_switch_state(dev
, XenbusStateClosing
);
313 EXPORT_SYMBOL_GPL(xenbus_dev_fatal
);
316 * Equivalent to xenbus_dev_fatal(dev, err, fmt, args), but helps
317 * avoiding recursion within xenbus_switch_state.
319 static void xenbus_switch_fatal(struct xenbus_device
*dev
, int depth
, int err
,
320 const char *fmt
, ...)
325 xenbus_va_dev_error(dev
, err
, fmt
, ap
);
329 __xenbus_switch_state(dev
, XenbusStateClosing
, 1);
334 * @dev: xenbus device
335 * @ring_mfn: mfn of ring to grant
337 * Grant access to the given @ring_mfn to the peer of the given device. Return
338 * 0 on success, or -errno on error. On error, the device will switch to
339 * XenbusStateClosing, and the error will be saved in the store.
341 int xenbus_grant_ring(struct xenbus_device
*dev
, unsigned long ring_mfn
)
343 int err
= gnttab_grant_foreign_access(dev
->otherend_id
, ring_mfn
, 0);
345 xenbus_dev_fatal(dev
, err
, "granting access to ring page");
348 EXPORT_SYMBOL_GPL(xenbus_grant_ring
);
352 * Allocate an event channel for the given xenbus_device, assigning the newly
353 * created local port to *port. Return 0 on success, or -errno on error. On
354 * error, the device will switch to XenbusStateClosing, and the error will be
355 * saved in the store.
357 int xenbus_alloc_evtchn(struct xenbus_device
*dev
, int *port
)
359 struct evtchn_alloc_unbound alloc_unbound
;
362 alloc_unbound
.dom
= DOMID_SELF
;
363 alloc_unbound
.remote_dom
= dev
->otherend_id
;
365 err
= HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound
,
368 xenbus_dev_fatal(dev
, err
, "allocating event channel");
370 *port
= alloc_unbound
.port
;
374 EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn
);
378 * Bind to an existing interdomain event channel in another domain. Returns 0
379 * on success and stores the local port in *port. On error, returns -errno,
380 * switches the device to XenbusStateClosing, and saves the error in XenStore.
382 int xenbus_bind_evtchn(struct xenbus_device
*dev
, int remote_port
, int *port
)
384 struct evtchn_bind_interdomain bind_interdomain
;
387 bind_interdomain
.remote_dom
= dev
->otherend_id
;
388 bind_interdomain
.remote_port
= remote_port
;
390 err
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain
,
393 xenbus_dev_fatal(dev
, err
,
394 "binding to event channel %d from domain %d",
395 remote_port
, dev
->otherend_id
);
397 *port
= bind_interdomain
.local_port
;
401 EXPORT_SYMBOL_GPL(xenbus_bind_evtchn
);
405 * Free an existing event channel. Returns 0 on success or -errno on error.
407 int xenbus_free_evtchn(struct xenbus_device
*dev
, int port
)
409 struct evtchn_close close
;
414 err
= HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
);
416 xenbus_dev_error(dev
, err
, "freeing event channel %d", port
);
420 EXPORT_SYMBOL_GPL(xenbus_free_evtchn
);
424 * xenbus_map_ring_valloc
425 * @dev: xenbus device
426 * @gnt_ref: grant reference
427 * @vaddr: pointer to address to be filled out by mapping
429 * Based on Rusty Russell's skeleton driver's map_page.
430 * Map a page of memory into this domain from another domain's grant table.
431 * xenbus_map_ring_valloc allocates a page of virtual address space, maps the
432 * page to that address, and sets *vaddr to that address.
433 * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
434 * or -ENOMEM on error. If an error is returned, device will switch to
435 * XenbusStateClosing and the error message will be saved in XenStore.
437 int xenbus_map_ring_valloc(struct xenbus_device
*dev
, int gnt_ref
, void **vaddr
)
439 struct gnttab_map_grant_ref op
= {
440 .flags
= GNTMAP_host_map
| GNTMAP_contains_pte
,
442 .dom
= dev
->otherend_id
,
444 struct vm_struct
*area
;
449 area
= alloc_vm_area(PAGE_SIZE
, &pte
);
453 op
.host_addr
= arbitrary_virt_to_machine(pte
).maddr
;
455 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref
, &op
, 1))
458 if (op
.status
!= GNTST_okay
) {
460 xenbus_dev_fatal(dev
, op
.status
,
461 "mapping in shared page %d from domain %d",
462 gnt_ref
, dev
->otherend_id
);
466 /* Stuff the handle in an unused field */
467 area
->phys_addr
= (unsigned long)op
.handle
;
472 EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc
);
477 * @dev: xenbus device
478 * @gnt_ref: grant reference
479 * @handle: pointer to grant handle to be filled
480 * @vaddr: address to be mapped to
482 * Map a page of memory into this domain from another domain's grant table.
483 * xenbus_map_ring does not allocate the virtual address space (you must do
484 * this yourself!). It only maps in the page to the specified address.
485 * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
486 * or -ENOMEM on error. If an error is returned, device will switch to
487 * XenbusStateClosing and the error message will be saved in XenStore.
489 int xenbus_map_ring(struct xenbus_device
*dev
, int gnt_ref
,
490 grant_handle_t
*handle
, void *vaddr
)
492 struct gnttab_map_grant_ref op
= {
493 .host_addr
= (unsigned long)vaddr
,
494 .flags
= GNTMAP_host_map
,
496 .dom
= dev
->otherend_id
,
499 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref
, &op
, 1))
502 if (op
.status
!= GNTST_okay
) {
503 xenbus_dev_fatal(dev
, op
.status
,
504 "mapping in shared page %d from domain %d",
505 gnt_ref
, dev
->otherend_id
);
511 EXPORT_SYMBOL_GPL(xenbus_map_ring
);
515 * xenbus_unmap_ring_vfree
516 * @dev: xenbus device
517 * @vaddr: addr to unmap
519 * Based on Rusty Russell's skeleton driver's unmap_page.
520 * Unmap a page of memory in this domain that was imported from another domain.
521 * Use xenbus_unmap_ring_vfree if you mapped in your memory with
522 * xenbus_map_ring_valloc (it will free the virtual address space).
523 * Returns 0 on success and returns GNTST_* on error
524 * (see xen/include/interface/grant_table.h).
526 int xenbus_unmap_ring_vfree(struct xenbus_device
*dev
, void *vaddr
)
528 struct vm_struct
*area
;
529 struct gnttab_unmap_grant_ref op
= {
530 .host_addr
= (unsigned long)vaddr
,
534 /* It'd be nice if linux/vmalloc.h provided a find_vm_area(void *addr)
535 * method so that we don't have to muck with vmalloc internals here.
536 * We could force the user to hang on to their struct vm_struct from
537 * xenbus_map_ring_valloc, but these 6 lines considerably simplify
540 read_lock(&vmlist_lock
);
541 for (area
= vmlist
; area
!= NULL
; area
= area
->next
) {
542 if (area
->addr
== vaddr
)
545 read_unlock(&vmlist_lock
);
548 xenbus_dev_error(dev
, -ENOENT
,
549 "can't find mapped virtual address %p", vaddr
);
550 return GNTST_bad_virt_addr
;
553 op
.handle
= (grant_handle_t
)area
->phys_addr
;
554 op
.host_addr
= arbitrary_virt_to_machine(
555 lookup_address((unsigned long)vaddr
, &level
)).maddr
;
557 if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref
, &op
, 1))
560 if (op
.status
== GNTST_okay
)
563 xenbus_dev_error(dev
, op
.status
,
564 "unmapping page at handle %d error %d",
565 (int16_t)area
->phys_addr
, op
.status
);
569 EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree
);
574 * @dev: xenbus device
575 * @handle: grant handle
576 * @vaddr: addr to unmap
578 * Unmap a page of memory in this domain that was imported from another domain.
579 * Returns 0 on success and returns GNTST_* on error
580 * (see xen/include/interface/grant_table.h).
582 int xenbus_unmap_ring(struct xenbus_device
*dev
,
583 grant_handle_t handle
, void *vaddr
)
585 struct gnttab_unmap_grant_ref op
= {
586 .host_addr
= (unsigned long)vaddr
,
590 if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref
, &op
, 1))
593 if (op
.status
!= GNTST_okay
)
594 xenbus_dev_error(dev
, op
.status
,
595 "unmapping page at handle %d error %d",
600 EXPORT_SYMBOL_GPL(xenbus_unmap_ring
);
604 * xenbus_read_driver_state
605 * @path: path for driver
607 * Return the state of the driver rooted at the given store path, or
608 * XenbusStateUnknown if no state can be read.
610 enum xenbus_state
xenbus_read_driver_state(const char *path
)
612 enum xenbus_state result
;
613 int err
= xenbus_gather(XBT_NIL
, path
, "state", "%d", &result
, NULL
);
615 result
= XenbusStateUnknown
;
619 EXPORT_SYMBOL_GPL(xenbus_read_driver_state
);