Home | History | Annotate | Line # | Download | only in xenbus
      1 /* $NetBSD: xenbus_client.c,v 1.17 2020/04/07 15:16:52 jdolecek Exp $ */
      2 /******************************************************************************
      3  * Client-facing interface for the Xenbus driver.  In other words, the
      4  * interface between the Xenbus and the device-specific code, be it the
      5  * frontend or the backend of that driver.
      6  *
      7  * Copyright (C) 2005 XenSource Ltd
      8  *
      9  * This file may be distributed separately from the Linux kernel, or
     10  * incorporated into other software packages, subject to the following license:
     11  *
     12  * Permission is hereby granted, free of charge, to any person obtaining a copy
     13  * of this source file (the "Software"), to deal in the Software without
     14  * restriction, including without limitation the rights to use, copy, modify,
     15  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
     16  * and to permit persons to whom the Software is furnished to do so, subject to
     17  * the following conditions:
     18  *
     19  * The above copyright notice and this permission notice shall be included in
     20  * all copies or substantial portions of the Software.
     21  *
     22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     28  * IN THE SOFTWARE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: xenbus_client.c,v 1.17 2020/04/07 15:16:52 jdolecek Exp $");
     33 
     34 #if 0
     35 #define DPRINTK(fmt, args...) \
     36     printk("xenbus_client (%s:%d) " fmt ".\n", __func__, __LINE__, ##args)
     37 #else
     38 #define DPRINTK(fmt, args...) ((void)0)
     39 #endif
     40 
     41 #include <sys/types.h>
     42 #include <sys/null.h>
     43 #include <sys/errno.h>
     44 #include <sys/kmem.h>
     45 #include <sys/systm.h>
     46 
     47 #include <xen/xen.h>
     48 #include <xen/hypervisor.h>
     49 #include <xen/evtchn.h>
     50 #include <xen/xenbus.h>
     51 #include <xen/granttables.h>
     52 
     53 int
     54 xenbus_watch_path2(struct xenbus_device *dev, const char *path,
     55 		       const char *path2, struct xenbus_watch *watch,
     56 		       void (*callback)(struct xenbus_watch *,
     57 					const char **, unsigned int))
     58 {
     59 	int err;
     60 	char *state;
     61 
     62 	DPRINTK("xenbus_watch_path2 path %s path2 %s\n", path, path2);
     63 
     64 	watch->node_sz = strlen(path) + 1 + strlen(path2) + 1;
     65 	state = kmem_alloc(watch->node_sz, KM_SLEEP);
     66 	strcpy(state, path);
     67 	strcat(state, "/");
     68 	strcat(state, path2);
     69 
     70 	watch->node = state;
     71 	watch->xbw_callback = callback;
     72 
     73 	err = register_xenbus_watch(watch);
     74 
     75 	if (err) {
     76 		watch->node = NULL;
     77 		watch->node_sz = 0;
     78 		watch->xbw_callback = NULL;
     79 		xenbus_dev_fatal(dev, err, "adding watch on %s", state);
     80 		kmem_free(state, watch->node_sz);
     81 	}
     82 	return err;
     83 }
     84 
     85 void
     86 xenbus_unwatch_path(struct xenbus_watch *watch)
     87 {
     88 	if (watch->node != NULL) {
     89 		unregister_xenbus_watch(watch);
     90 		kmem_free(watch->node, watch->node_sz);
     91 		watch->node = NULL;
     92 	}
     93 }
     94 
     95 int
     96 xenbus_switch_state(struct xenbus_device *dev,
     97 			struct xenbus_transaction *xbt,
     98 			XenbusState state)
     99 {
    100 	/* We check whether the state is currently set to the given value, and
    101 	   if not, then the state is set.  We don't want to unconditionally
    102 	   write the given state, because we don't want to fire watches
    103 	   unnecessarily.  Furthermore, if the node has gone, we don't write
    104 	   to it, as the device will be tearing down, and we don't want to
    105 	   resurrect that directory.
    106 	 */
    107 
    108 	u_long current_state;
    109 
    110 	int err = xenbus_read_ul(xbt, dev->xbusd_path, "state",
    111 	    &current_state, 10);
    112 	if (err)
    113 		return 0;
    114 
    115 	if ((XenbusState)current_state == state)
    116 		return 0;
    117 
    118 	err = xenbus_printf(xbt, dev->xbusd_path, "state", "%d", state);
    119 	if (err) {
    120 		xenbus_dev_fatal(dev, err, "writing new state");
    121 		return err;
    122 	}
    123 	return 0;
    124 }
    125 
    126 /**
    127  * Return the path to the error node for the given device, or NULL on failure.
    128  * If the value returned is non-NULL, then it is the caller's to kfree.
    129  */
    130 static char *
    131 error_path(struct xenbus_device *dev, size_t *len)
    132 {
    133 	*len = strlen("error/") + strlen(dev->xbusd_path) + 1;
    134 	char *path_buffer = kmem_alloc(*len, KM_NOSLEEP);
    135 	if (path_buffer == NULL)
    136 		return NULL;
    137 
    138 	strcpy(path_buffer, "error/");
    139 	strcpy(path_buffer + strlen("error/"), dev->xbusd_path);
    140 
    141 	return path_buffer;
    142 }
    143 
    144 
    145 static void
    146 _dev_error(struct xenbus_device *dev, int err, const char *fmt,
    147 		va_list ap)
    148 {
    149 	int ret __diagused;
    150 	unsigned int len;
    151 	char *printf_buffer = NULL, *path_buffer = NULL;
    152 	size_t path_buffer_sz = 0;
    153 
    154 #define PRINTF_BUFFER_SIZE 4096
    155 	printf_buffer = kmem_alloc(PRINTF_BUFFER_SIZE, KM_NOSLEEP);
    156 	if (printf_buffer == NULL)
    157 		goto fail;
    158 
    159 	len = snprintf(printf_buffer, PRINTF_BUFFER_SIZE, "%i ", -err);
    160 	KASSERT(len < PRINTF_BUFFER_SIZE);
    161 	ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
    162 	KASSERT(len + ret < PRINTF_BUFFER_SIZE);
    163 	dev->xbusd_has_error = 1;
    164 
    165 	path_buffer = error_path(dev, &path_buffer_sz);
    166 	if (path_buffer == NULL) {
    167 		printk("xenbus: failed to write error node for %s (%s)\n",
    168 		       dev->xbusd_path, printf_buffer);
    169 		goto fail;
    170 	}
    171 
    172 	if (xenbus_write(NULL, path_buffer, "error", printf_buffer) != 0) {
    173 		printk("xenbus: failed to write error node for %s (%s)\n",
    174 		       dev->xbusd_path, printf_buffer);
    175 		goto fail;
    176 	}
    177 
    178 fail:
    179 	if (printf_buffer)
    180 		kmem_free(printf_buffer, PRINTF_BUFFER_SIZE);
    181 	if (path_buffer)
    182 		kmem_free(path_buffer, path_buffer_sz);
    183 }
    184 
    185 
    186 void
    187 xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
    188 		      ...)
    189 {
    190 	va_list ap;
    191 
    192 	va_start(ap, fmt);
    193 	_dev_error(dev, err, fmt, ap);
    194 	va_end(ap);
    195 }
    196 
    197 
    198 void
    199 xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
    200 		      ...)
    201 {
    202 	va_list ap;
    203 
    204 	va_start(ap, fmt);
    205 	_dev_error(dev, err, fmt, ap);
    206 	va_end(ap);
    207 
    208 	xenbus_switch_state(dev, NULL, XenbusStateClosing);
    209 }
    210 
    211 
    212 int
    213 xenbus_grant_ring(struct xenbus_device *dev, paddr_t ring_pa,
    214     grant_ref_t *entryp)
    215 {
    216 	int err = xengnt_grant_access(dev->xbusd_otherend_id, ring_pa,
    217 	    0, entryp);
    218 	if (err != 0)
    219 		xenbus_dev_fatal(dev, err, "granting access to ring page");
    220 	return err;
    221 }
    222 
    223 
    224 int
    225 xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
    226 {
    227 	evtchn_op_t op = {
    228 		.cmd = EVTCHNOP_alloc_unbound,
    229 		.u.alloc_unbound = {
    230 			.dom = DOMID_SELF,
    231 			.remote_dom = dev->xbusd_otherend_id,
    232 			.port = 0
    233 		}
    234 	};
    235 
    236 	int err = HYPERVISOR_event_channel_op(&op);
    237 	if (err)
    238 		xenbus_dev_fatal(dev, err, "allocating event channel");
    239 	else
    240 		*port = op.u.alloc_unbound.port;
    241 	return err;
    242 }
    243 
    244 
    245 XenbusState
    246 xenbus_read_driver_state(const char *path)
    247 {
    248 	u_long result;
    249 
    250 	int err = xenbus_read_ul(NULL, path, "state", &result, 10);
    251 	if (err)
    252 		result = XenbusStateClosed;
    253 
    254 	return result;
    255 }
    256 
    257 
    258 /*
    259  * Local variables:
    260  *  c-file-style: "linux"
    261  *  indent-tabs-mode: t
    262  *  c-indent-level: 8
    263  *  c-basic-offset: 8
    264  *  tab-width: 8
    265  * End:
    266  */
    267