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