Home | History | Annotate | Line # | Download | only in include
xenbus.h revision 1.20
      1 /* $NetBSD: xenbus.h,v 1.20 2020/04/07 15:40:14 jdolecek Exp $ */
      2 /******************************************************************************
      3  * xenbus.h
      4  *
      5  * Talks to Xen Store to figure out what devices we have.
      6  *
      7  * Copyright (C) 2005 Rusty Russell, IBM Corporation
      8  * Copyright (C) 2005 XenSource Ltd.
      9  *
     10  * This file may be distributed separately from the Linux kernel, or
     11  * incorporated into other software packages, subject to the following license:
     12  *
     13  * Permission is hereby granted, free of charge, to any person obtaining a copy
     14  * of this source file (the "Software"), to deal in the Software without
     15  * restriction, including without limitation the rights to use, copy, modify,
     16  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
     17  * and to permit persons to whom the Software is furnished to do so, subject to
     18  * the following conditions:
     19  *
     20  * The above copyright notice and this permission notice shall be included in
     21  * all copies or substantial portions of the Software.
     22  *
     23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     26  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     29  * IN THE SOFTWARE.
     30  */
     31 
     32 #ifndef _ASM_XEN_XENBUS_H
     33 #define _ASM_XEN_XENBUS_H
     34 
     35 #include <sys/device.h>
     36 #include <sys/queue.h>
     37 #include <xen/include/public/xen.h>
     38 #include <xen/include/public/io/xenbus.h>
     39 #include <xen/include/public/io/xs_wire.h>
     40 #include <xen/include/public/grant_table.h>	/* for grant_ref_t */
     41 
     42 /* xenbus to hypervisor attach */
     43 struct xenbus_attach_args {
     44 	const char 		*xa_device;
     45 	int			xa_handle;
     46 };
     47 
     48 /* devices to xenbus attach */
     49 struct xenbusdev_attach_args {
     50 	const char		*xa_type;
     51 	int 			xa_id;
     52 	struct xenbus_device	*xa_xbusd;
     53 };
     54 
     55 /* Register callback to watch this node. */
     56 struct xenbus_watch {
     57 	SLIST_ENTRY(xenbus_watch) watch_next;
     58 
     59 	/* Path being watched. */
     60 	char *node;
     61 	size_t node_sz;
     62 
     63 	/* Callback (executed in a process context with no locks held). */
     64 	void (*xbw_callback)(struct xenbus_watch *,
     65 			 const char **vec, unsigned int len);
     66 	struct xenbus_device *xbw_dev;
     67 };
     68 
     69 
     70 /*
     71  * A xenbus device. Note that the malloced memory will be larger than
     72  * sizeof(xenbus_device) to have the storage for xbusd_path, so xbusd_path
     73  * has to be the last entry.
     74  */
     75 typedef enum {
     76 	XENBUS_FRONTEND_DEVICE,
     77 	XENBUS_BACKEND_DEVICE
     78 } xenbusdev_type_t;
     79 
     80 struct xenbus_device {
     81 	SLIST_ENTRY(xenbus_device) xbusd_entries;
     82 	char xbusd_otherend[64]; /* the otherend path (size arbitrary) */
     83 	int xbusd_otherend_id; /* the otherend's id */
     84 	/* callback for otherend change */
     85 	void (*xbusd_otherend_changed)(void *, XenbusState);
     86 	xenbusdev_type_t xbusd_type;
     87 	union {
     88 		struct {
     89 			device_t f_dev;
     90 		} f;
     91 		struct {
     92 			void *b_cookie; /* private to backend driver */
     93 			int (*b_detach)(void *);
     94 		} b;
     95 	} xbusd_u;
     96 	int xbusd_has_error;
     97 	/* for xenbus internal use */
     98 	struct xenbus_watch xbusd_otherend_watch;
     99 	size_t xbusd_sz;		/* size of allocated structure */
    100 	const char xbusd_path[1]; /* our path */
    101 };
    102 
    103 /*
    104  * frontend devices use the normal autoconf(9) framework to attach.
    105  * backend drivers need something more clever because we want the
    106  * domain's name or uid in the device's name. Each backend driver registers
    107  * to xenbus.
    108  */
    109 
    110 struct xenbus_backend_driver {
    111 	SLIST_ENTRY(xenbus_backend_driver) xbakd_entries;
    112 	int (*xbakd_create) (struct xenbus_device *); /* called for new devs */
    113 	const char *xbakd_type; /* device type we register for */
    114 };
    115 
    116 void xenbus_backend_register(struct xenbus_backend_driver *);
    117 
    118 struct xenbus_transaction;
    119 
    120 int xenbus_directory(struct xenbus_transaction *t,
    121 			const char *dir, const char *node, unsigned int *num,
    122 			char ***);
    123 int xenbus_read(struct xenbus_transaction *,
    124 		  const char *, const char *, char *, size_t);
    125 int xenbus_read_ul(struct xenbus_transaction *,
    126 		  const char *, const char *, unsigned long *, int);
    127 int xenbus_read_ull(struct xenbus_transaction *,
    128 		  const char *, const char *, unsigned long long *, int);
    129 int xenbus_write(struct xenbus_transaction *t,
    130 		 const char *dir, const char *node, const char *string);
    131 int xenbus_mkdir(struct xenbus_transaction *t,
    132 		 const char *dir, const char *node);
    133 int xenbus_exists(struct xenbus_transaction *t,
    134 		  const char *dir, const char *node);
    135 int xenbus_rm(struct xenbus_transaction *t, const char *dir, const char *node);
    136 struct xenbus_transaction *xenbus_transaction_start(void);
    137 int xenbus_transaction_end(struct xenbus_transaction *t, int abort);
    138 
    139 /* Single printf and write: returns -errno or 0. */
    140 int xenbus_printf(struct xenbus_transaction *t,
    141 		  const char *dir, const char *node, const char *fmt, ...)
    142 	__attribute__((format(printf, 4, 5)));
    143 
    144 /* notifer routines for when the xenstore comes up */
    145 // XXX int register_xenstore_notifier(struct notifier_block *nb);
    146 // XXX void unregister_xenstore_notifier(struct notifier_block *nb);
    147 
    148 int register_xenbus_watch(struct xenbus_watch *watch);
    149 void unregister_xenbus_watch(struct xenbus_watch *watch);
    150 void xs_suspend(void);
    151 void xs_resume(void);
    152 
    153 /* Used by xenbus_dev to borrow kernel's store connection. */
    154 int xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void **);
    155 
    156 void xenbus_probe(void *);
    157 
    158 int xenbus_free_device(struct xenbus_device *);
    159 
    160 #define XENBUS_IS_ERR_READ(str) ({			\
    161 	if (!IS_ERR(str) && strlen(str) == 0) {		\
    162 		kfree(str);				\
    163 		str = ERR_PTR(-ERANGE);			\
    164 	}						\
    165 	IS_ERR(str);					\
    166 })
    167 
    168 #define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
    169 
    170 
    171 /**
    172  * Register a watch on the given path/path2, using the given xenbus_watch
    173  * structure for storage, and the given callback function as the callback.
    174  * Return 0 on success, or -errno on error.  On success, the watched path
    175  * (path/path2) will be saved as watch->node, and becomes the caller's to
    176  * kfree().  On error, watch->node will be NULL, so the caller has nothing to
    177  * free, the device will switch to XenbusStateClosing, and the error will be
    178  * saved in the store.
    179  */
    180 int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
    181 		       const char *path2, struct xenbus_watch *watch,
    182 		       void (*callback)(struct xenbus_watch *,
    183 					const char **, unsigned int));
    184 
    185 /* Unregister the watch, and free associated internal structures. */
    186 void xenbus_unwatch_path(struct xenbus_watch *);
    187 
    188 /**
    189  * Advertise in the store a change of the given driver to the given new_state.
    190  * Perform the change inside the given transaction xbt.  xbt may be NULL, in
    191  * which case this is performed inside its own transaction.  Return 0 on
    192  * success, or -errno on error.  On error, the device will switch to
    193  * XenbusStateClosing, and the error will be saved in the store.
    194  */
    195 int xenbus_switch_state(struct xenbus_device *dev,
    196 			struct xenbus_transaction *xbt,
    197 			XenbusState new_state);
    198 
    199 
    200 /**
    201  * Grant access to the given ring_mfn to the peer of the given device.  Return
    202  * 0 on success, or -errno on error.  On error, the device will switch to
    203  * XenbusStateClosing, and the error will be saved in the store.
    204  */
    205 int xenbus_grant_ring(struct xenbus_device *, paddr_t, grant_ref_t *);
    206 
    207 
    208 /**
    209  * Allocate an event channel for the given xenbus_device, assigning the newly
    210  * created local port to *port.  Return 0 on success, or -errno on error.  On
    211  * error, the device will switch to XenbusStateClosing, and the error will be
    212  * saved in the store.
    213  */
    214 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port);
    215 
    216 
    217 /**
    218  * Return the state of the driver rooted at the given store path, or
    219  * XenbusStateClosed if no state can be read.
    220  */
    221 XenbusState xenbus_read_driver_state(const char *path);
    222 
    223 
    224 /***
    225  * Report the given negative errno into the store, along with the given
    226  * formatted message.
    227  */
    228 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
    229 		      ...);
    230 
    231 
    232 /***
    233  * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
    234  * xenbus_switch_state(dev, NULL, XenbusStateClosing) to schedule an orderly
    235  * closedown of this driver and its peer.
    236  */
    237 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
    238 		      ...);
    239 
    240 bool xenbus_device_suspend(struct xenbus_device *);
    241 bool xenbus_device_resume(struct xenbus_device *);
    242 
    243 #endif /* _ASM_XEN_XENBUS_H */
    244 
    245 /*
    246  * Local variables:
    247  *  c-file-style: "linux"
    248  *  indent-tabs-mode: t
    249  *  c-indent-level: 8
    250  *  c-basic-offset: 8
    251  *  tab-width: 8
    252  * End:
    253  */
    254