• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/types.h>
34 #include <linux/vmalloc.h>
35 #include <asm/xen/hypervisor.h>
36 #include <xen/interface/xen.h>
37 #include <xen/interface/event_channel.h>
38 #include <xen/events.h>
39 #include <xen/grant_table.h>
40 #include <xen/xenbus.h>
41 
xenbus_strstate(enum xenbus_state state)42 const char *xenbus_strstate(enum xenbus_state state)
43 {
44 	static const char *const name[] = {
45 		[ XenbusStateUnknown      ] = "Unknown",
46 		[ XenbusStateInitialising ] = "Initialising",
47 		[ XenbusStateInitWait     ] = "InitWait",
48 		[ XenbusStateInitialised  ] = "Initialised",
49 		[ XenbusStateConnected    ] = "Connected",
50 		[ XenbusStateClosing      ] = "Closing",
51 		[ XenbusStateClosed	  ] = "Closed",
52 	};
53 	return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
54 }
55 EXPORT_SYMBOL_GPL(xenbus_strstate);
56 
57 /**
58  * xenbus_watch_path - register a watch
59  * @dev: xenbus device
60  * @path: path to watch
61  * @watch: watch to register
62  * @callback: callback to register
63  *
64  * Register a @watch on the given path, using the given xenbus_watch structure
65  * for storage, and the given @callback function as the callback.  Return 0 on
66  * success, or -errno on error.  On success, the given @path will be saved as
67  * @watch->node, and remains the caller's to free.  On error, @watch->node will
68  * be NULL, the device will switch to %XenbusStateClosing, and the error will
69  * be saved in the store.
70  */
xenbus_watch_path(struct xenbus_device * dev,const char * path,struct xenbus_watch * watch,void (* callback)(struct xenbus_watch *,const char **,unsigned int))71 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
72 		      struct xenbus_watch *watch,
73 		      void (*callback)(struct xenbus_watch *,
74 				       const char **, unsigned int))
75 {
76 	int err;
77 
78 	watch->node = path;
79 	watch->callback = callback;
80 
81 	err = register_xenbus_watch(watch);
82 
83 	if (err) {
84 		watch->node = NULL;
85 		watch->callback = NULL;
86 		xenbus_dev_fatal(dev, err, "adding watch on %s", path);
87 	}
88 
89 	return err;
90 }
91 EXPORT_SYMBOL_GPL(xenbus_watch_path);
92 
93 
94 /**
95  * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
96  * @dev: xenbus device
97  * @watch: watch to register
98  * @callback: callback to register
99  * @pathfmt: format of path to watch
100  *
101  * Register a watch on the given @path, using the given xenbus_watch
102  * structure for storage, and the given @callback function as the callback.
103  * Return 0 on success, or -errno on error.  On success, the watched path
104  * (@path/@path2) will be saved as @watch->node, and becomes the caller's to
105  * kfree().  On error, watch->node will be NULL, so the caller has nothing to
106  * free, the device will switch to %XenbusStateClosing, and the error will be
107  * saved in the store.
108  */
xenbus_watch_pathfmt(struct xenbus_device * dev,struct xenbus_watch * watch,void (* callback)(struct xenbus_watch *,const char **,unsigned int),const char * pathfmt,...)109 int xenbus_watch_pathfmt(struct xenbus_device *dev,
110 			 struct xenbus_watch *watch,
111 			 void (*callback)(struct xenbus_watch *,
112 					const char **, unsigned int),
113 			 const char *pathfmt, ...)
114 {
115 	int err;
116 	va_list ap;
117 	char *path;
118 
119 	va_start(ap, pathfmt);
120 	path = kvasprintf(GFP_NOIO | __GFP_HIGH, pathfmt, ap);
121 	va_end(ap);
122 
123 	if (!path) {
124 		xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
125 		return -ENOMEM;
126 	}
127 	err = xenbus_watch_path(dev, path, watch, callback);
128 
129 	if (err)
130 		kfree(path);
131 	return err;
132 }
133 EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);
134 
135 
136 /**
137  * xenbus_switch_state
138  * @dev: xenbus device
139  * @state: new state
140  *
141  * Advertise in the store a change of the given driver to the given new_state.
142  * Return 0 on success, or -errno on error.  On error, the device will switch
143  * to XenbusStateClosing, and the error will be saved in the store.
144  */
xenbus_switch_state(struct xenbus_device * dev,enum xenbus_state state)145 int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
146 {
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.
153 
154 	   Note that, because of this cached value of our state, this function
155 	   will not work inside a Xenstore transaction (something it was
156 	   trying to in the past) because dev->state would not get reset if
157 	   the transaction was aborted.
158 
159 	 */
160 
161 	int current_state;
162 	int err;
163 
164 	if (state == dev->state)
165 		return 0;
166 
167 	err = xenbus_scanf(XBT_NIL, dev->nodename, "state", "%d",
168 			   &current_state);
169 	if (err != 1)
170 		return 0;
171 
172 	err = xenbus_printf(XBT_NIL, dev->nodename, "state", "%d", state);
173 	if (err) {
174 		if (state != XenbusStateClosing) /* Avoid looping */
175 			xenbus_dev_fatal(dev, err, "writing new state");
176 		return err;
177 	}
178 
179 	dev->state = state;
180 
181 	return 0;
182 }
183 EXPORT_SYMBOL_GPL(xenbus_switch_state);
184 
xenbus_frontend_closed(struct xenbus_device * dev)185 int xenbus_frontend_closed(struct xenbus_device *dev)
186 {
187 	xenbus_switch_state(dev, XenbusStateClosed);
188 	complete(&dev->down);
189 	return 0;
190 }
191 EXPORT_SYMBOL_GPL(xenbus_frontend_closed);
192 
193 /**
194  * Return the path to the error node for the given device, or NULL on failure.
195  * If the value returned is non-NULL, then it is the caller's to kfree.
196  */
error_path(struct xenbus_device * dev)197 static char *error_path(struct xenbus_device *dev)
198 {
199 	return kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
200 }
201 
202 
xenbus_va_dev_error(struct xenbus_device * dev,int err,const char * fmt,va_list ap)203 static void xenbus_va_dev_error(struct xenbus_device *dev, int err,
204 				const char *fmt, va_list ap)
205 {
206 	int ret;
207 	unsigned int len;
208 	char *printf_buffer = NULL;
209 	char *path_buffer = NULL;
210 
211 #define PRINTF_BUFFER_SIZE 4096
212 	printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
213 	if (printf_buffer == NULL)
214 		goto fail;
215 
216 	len = sprintf(printf_buffer, "%i ", -err);
217 	ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
218 
219 	BUG_ON(len + ret > PRINTF_BUFFER_SIZE-1);
220 
221 	dev_err(&dev->dev, "%s\n", printf_buffer);
222 
223 	path_buffer = error_path(dev);
224 
225 	if (path_buffer == NULL) {
226 		dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
227 		       dev->nodename, printf_buffer);
228 		goto fail;
229 	}
230 
231 	if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
232 		dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
233 		       dev->nodename, printf_buffer);
234 		goto fail;
235 	}
236 
237 fail:
238 	kfree(printf_buffer);
239 	kfree(path_buffer);
240 }
241 
242 
243 /**
244  * xenbus_dev_error
245  * @dev: xenbus device
246  * @err: error to report
247  * @fmt: error message format
248  *
249  * Report the given negative errno into the store, along with the given
250  * formatted message.
251  */
xenbus_dev_error(struct xenbus_device * dev,int err,const char * fmt,...)252 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
253 {
254 	va_list ap;
255 
256 	va_start(ap, fmt);
257 	xenbus_va_dev_error(dev, err, fmt, ap);
258 	va_end(ap);
259 }
260 EXPORT_SYMBOL_GPL(xenbus_dev_error);
261 
262 /**
263  * xenbus_dev_fatal
264  * @dev: xenbus device
265  * @err: error to report
266  * @fmt: error message format
267  *
268  * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
269  * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
270  * closedown of this driver and its peer.
271  */
272 
xenbus_dev_fatal(struct xenbus_device * dev,int err,const char * fmt,...)273 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
274 {
275 	va_list ap;
276 
277 	va_start(ap, fmt);
278 	xenbus_va_dev_error(dev, err, fmt, ap);
279 	va_end(ap);
280 
281 	xenbus_switch_state(dev, XenbusStateClosing);
282 }
283 EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
284 
285 /**
286  * xenbus_grant_ring
287  * @dev: xenbus device
288  * @ring_mfn: mfn of ring to grant
289 
290  * Grant access to the given @ring_mfn to the peer of the given device.  Return
291  * 0 on success, or -errno on error.  On error, the device will switch to
292  * XenbusStateClosing, and the error will be saved in the store.
293  */
xenbus_grant_ring(struct xenbus_device * dev,unsigned long ring_mfn)294 int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn)
295 {
296 	int err = gnttab_grant_foreign_access(dev->otherend_id, ring_mfn, 0);
297 	if (err < 0)
298 		xenbus_dev_fatal(dev, err, "granting access to ring page");
299 	return err;
300 }
301 EXPORT_SYMBOL_GPL(xenbus_grant_ring);
302 
303 
304 /**
305  * Allocate an event channel for the given xenbus_device, assigning the newly
306  * created local port to *port.  Return 0 on success, or -errno on error.  On
307  * error, the device will switch to XenbusStateClosing, and the error will be
308  * saved in the store.
309  */
xenbus_alloc_evtchn(struct xenbus_device * dev,int * port)310 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
311 {
312 	struct evtchn_alloc_unbound alloc_unbound;
313 	int err;
314 
315 	alloc_unbound.dom = DOMID_SELF;
316 	alloc_unbound.remote_dom = dev->otherend_id;
317 
318 	err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
319 					  &alloc_unbound);
320 	if (err)
321 		xenbus_dev_fatal(dev, err, "allocating event channel");
322 	else
323 		*port = alloc_unbound.port;
324 
325 	return err;
326 }
327 EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
328 
329 
330 /**
331  * Bind to an existing interdomain event channel in another domain. Returns 0
332  * on success and stores the local port in *port. On error, returns -errno,
333  * switches the device to XenbusStateClosing, and saves the error in XenStore.
334  */
xenbus_bind_evtchn(struct xenbus_device * dev,int remote_port,int * port)335 int xenbus_bind_evtchn(struct xenbus_device *dev, int remote_port, int *port)
336 {
337 	struct evtchn_bind_interdomain bind_interdomain;
338 	int err;
339 
340 	bind_interdomain.remote_dom = dev->otherend_id;
341 	bind_interdomain.remote_port = remote_port;
342 
343 	err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
344 					  &bind_interdomain);
345 	if (err)
346 		xenbus_dev_fatal(dev, err,
347 				 "binding to event channel %d from domain %d",
348 				 remote_port, dev->otherend_id);
349 	else
350 		*port = bind_interdomain.local_port;
351 
352 	return err;
353 }
354 EXPORT_SYMBOL_GPL(xenbus_bind_evtchn);
355 
356 
357 /**
358  * Free an existing event channel. Returns 0 on success or -errno on error.
359  */
xenbus_free_evtchn(struct xenbus_device * dev,int port)360 int xenbus_free_evtchn(struct xenbus_device *dev, int port)
361 {
362 	struct evtchn_close close;
363 	int err;
364 
365 	close.port = port;
366 
367 	err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
368 	if (err)
369 		xenbus_dev_error(dev, err, "freeing event channel %d", port);
370 
371 	return err;
372 }
373 EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
374 
375 
376 /**
377  * xenbus_map_ring_valloc
378  * @dev: xenbus device
379  * @gnt_ref: grant reference
380  * @vaddr: pointer to address to be filled out by mapping
381  *
382  * Based on Rusty Russell's skeleton driver's map_page.
383  * Map a page of memory into this domain from another domain's grant table.
384  * xenbus_map_ring_valloc allocates a page of virtual address space, maps the
385  * page to that address, and sets *vaddr to that address.
386  * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
387  * or -ENOMEM on error. If an error is returned, device will switch to
388  * XenbusStateClosing and the error message will be saved in XenStore.
389  */
xenbus_map_ring_valloc(struct xenbus_device * dev,int gnt_ref,void ** vaddr)390 int xenbus_map_ring_valloc(struct xenbus_device *dev, int gnt_ref, void **vaddr)
391 {
392 	struct gnttab_map_grant_ref op = {
393 		.flags = GNTMAP_host_map,
394 		.ref   = gnt_ref,
395 		.dom   = dev->otherend_id,
396 	};
397 	struct vm_struct *area;
398 
399 	*vaddr = NULL;
400 
401 	area = xen_alloc_vm_area(PAGE_SIZE);
402 	if (!area)
403 		return -ENOMEM;
404 
405 	op.host_addr = (unsigned long)area->addr;
406 
407 	if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
408 		BUG();
409 
410 	if (op.status != GNTST_okay) {
411 		xen_free_vm_area(area);
412 		xenbus_dev_fatal(dev, op.status,
413 				 "mapping in shared page %d from domain %d",
414 				 gnt_ref, dev->otherend_id);
415 		return op.status;
416 	}
417 
418 	/* Stuff the handle in an unused field */
419 	area->phys_addr = (unsigned long)op.handle;
420 
421 	*vaddr = area->addr;
422 	return 0;
423 }
424 EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
425 
426 
427 /**
428  * xenbus_map_ring
429  * @dev: xenbus device
430  * @gnt_ref: grant reference
431  * @handle: pointer to grant handle to be filled
432  * @vaddr: address to be mapped to
433  *
434  * Map a page of memory into this domain from another domain's grant table.
435  * xenbus_map_ring does not allocate the virtual address space (you must do
436  * this yourself!). It only maps in the page to the specified address.
437  * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
438  * or -ENOMEM on error. If an error is returned, device will switch to
439  * XenbusStateClosing and the error message will be saved in XenStore.
440  */
xenbus_map_ring(struct xenbus_device * dev,int gnt_ref,grant_handle_t * handle,void * vaddr)441 int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
442 		    grant_handle_t *handle, void *vaddr)
443 {
444 	struct gnttab_map_grant_ref op = {
445 		.host_addr = (unsigned long)vaddr,
446 		.flags     = GNTMAP_host_map,
447 		.ref       = gnt_ref,
448 		.dom       = dev->otherend_id,
449 	};
450 
451 	if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
452 		BUG();
453 
454 	if (op.status != GNTST_okay) {
455 		xenbus_dev_fatal(dev, op.status,
456 				 "mapping in shared page %d from domain %d",
457 				 gnt_ref, dev->otherend_id);
458 	} else
459 		*handle = op.handle;
460 
461 	return op.status;
462 }
463 EXPORT_SYMBOL_GPL(xenbus_map_ring);
464 
465 
466 /**
467  * xenbus_unmap_ring_vfree
468  * @dev: xenbus device
469  * @vaddr: addr to unmap
470  *
471  * Based on Rusty Russell's skeleton driver's unmap_page.
472  * Unmap a page of memory in this domain that was imported from another domain.
473  * Use xenbus_unmap_ring_vfree if you mapped in your memory with
474  * xenbus_map_ring_valloc (it will free the virtual address space).
475  * Returns 0 on success and returns GNTST_* on error
476  * (see xen/include/interface/grant_table.h).
477  */
xenbus_unmap_ring_vfree(struct xenbus_device * dev,void * vaddr)478 int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
479 {
480 	struct vm_struct *area;
481 	struct gnttab_unmap_grant_ref op = {
482 		.host_addr = (unsigned long)vaddr,
483 	};
484 
485 	/* It'd be nice if linux/vmalloc.h provided a find_vm_area(void *addr)
486 	 * method so that we don't have to muck with vmalloc internals here.
487 	 * We could force the user to hang on to their struct vm_struct from
488 	 * xenbus_map_ring_valloc, but these 6 lines considerably simplify
489 	 * this API.
490 	 */
491 	read_lock(&vmlist_lock);
492 	for (area = vmlist; area != NULL; area = area->next) {
493 		if (area->addr == vaddr)
494 			break;
495 	}
496 	read_unlock(&vmlist_lock);
497 
498 	if (!area) {
499 		xenbus_dev_error(dev, -ENOENT,
500 				 "can't find mapped virtual address %p", vaddr);
501 		return GNTST_bad_virt_addr;
502 	}
503 
504 	op.handle = (grant_handle_t)area->phys_addr;
505 
506 	if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
507 		BUG();
508 
509 	if (op.status == GNTST_okay)
510 		xen_free_vm_area(area);
511 	else
512 		xenbus_dev_error(dev, op.status,
513 				 "unmapping page at handle %d error %d",
514 				 (int16_t)area->phys_addr, op.status);
515 
516 	return op.status;
517 }
518 EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
519 
520 
521 /**
522  * xenbus_unmap_ring
523  * @dev: xenbus device
524  * @handle: grant handle
525  * @vaddr: addr to unmap
526  *
527  * Unmap a page of memory in this domain that was imported from another domain.
528  * Returns 0 on success and returns GNTST_* on error
529  * (see xen/include/interface/grant_table.h).
530  */
xenbus_unmap_ring(struct xenbus_device * dev,grant_handle_t handle,void * vaddr)531 int xenbus_unmap_ring(struct xenbus_device *dev,
532 		      grant_handle_t handle, void *vaddr)
533 {
534 	struct gnttab_unmap_grant_ref op = {
535 		.host_addr = (unsigned long)vaddr,
536 		.handle    = handle,
537 	};
538 
539 	if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
540 		BUG();
541 
542 	if (op.status != GNTST_okay)
543 		xenbus_dev_error(dev, op.status,
544 				 "unmapping page at handle %d error %d",
545 				 handle, op.status);
546 
547 	return op.status;
548 }
549 EXPORT_SYMBOL_GPL(xenbus_unmap_ring);
550 
551 
552 /**
553  * xenbus_read_driver_state
554  * @path: path for driver
555  *
556  * Return the state of the driver rooted at the given store path, or
557  * XenbusStateUnknown if no state can be read.
558  */
xenbus_read_driver_state(const char * path)559 enum xenbus_state xenbus_read_driver_state(const char *path)
560 {
561 	enum xenbus_state result;
562 	int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
563 	if (err)
564 		result = XenbusStateUnknown;
565 
566 	return result;
567 }
568 EXPORT_SYMBOL_GPL(xenbus_read_driver_state);
569