1 /* $NetBSD: xenbus_client.c,v 1.14 2019/11/10 21:16:34 chs 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.14 2019/11/10 21:16:34 chs 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/malloc.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 54 int 55 xenbus_watch_path(struct xenbus_device *dev, char *path, 56 struct xenbus_watch *watch, 57 void (*callback)(struct xenbus_watch *, 58 const char **, unsigned int)) 59 { 60 int err; 61 62 watch->node = path; 63 watch->xbw_callback = callback; 64 65 err = register_xenbus_watch(watch); 66 67 if (err) { 68 watch->node = NULL; 69 watch->xbw_callback = NULL; 70 xenbus_dev_fatal(dev, err, "adding watch on %s", path); 71 } 72 err = 0; 73 74 return err; 75 } 76 77 int 78 xenbus_watch_path2(struct xenbus_device *dev, const char *path, 79 const char *path2, struct xenbus_watch *watch, 80 void (*callback)(struct xenbus_watch *, 81 const char **, unsigned int)) 82 { 83 int err; 84 char *state; 85 86 DPRINTK("xenbus_watch_path2 path %s path2 %s\n", path, path2); 87 state = 88 malloc(strlen(path) + 1 + strlen(path2) + 1, M_DEVBUF, 89 M_WAITOK); 90 strcpy(state, path); 91 strcat(state, "/"); 92 strcat(state, path2); 93 94 err = xenbus_watch_path(dev, state, watch, callback); 95 96 if (err) { 97 free(state, M_DEVBUF); 98 } 99 return err; 100 } 101 102 103 int 104 xenbus_switch_state(struct xenbus_device *dev, 105 struct xenbus_transaction *xbt, 106 XenbusState state) 107 { 108 /* We check whether the state is currently set to the given value, and 109 if not, then the state is set. We don't want to unconditionally 110 write the given state, because we don't want to fire watches 111 unnecessarily. Furthermore, if the node has gone, we don't write 112 to it, as the device will be tearing down, and we don't want to 113 resurrect that directory. 114 */ 115 116 u_long current_state; 117 118 int err = xenbus_read_ul(xbt, dev->xbusd_path, "state", 119 ¤t_state, 10); 120 if (err) 121 return 0; 122 123 if ((XenbusState)current_state == state) 124 return 0; 125 126 err = xenbus_printf(xbt, dev->xbusd_path, "state", "%d", state); 127 if (err) { 128 xenbus_dev_fatal(dev, err, "writing new state"); 129 return err; 130 } 131 return 0; 132 } 133 134 /** 135 * Return the path to the error node for the given device, or NULL on failure. 136 * If the value returned is non-NULL, then it is the caller's to kfree. 137 */ 138 static char * 139 error_path(struct xenbus_device *dev) 140 { 141 char *path_buffer = malloc(strlen("error/") + strlen(dev->xbusd_path) + 142 1, M_DEVBUF, M_NOWAIT); 143 if (path_buffer == NULL) { 144 return NULL; 145 } 146 147 strcpy(path_buffer, "error/"); 148 strcpy(path_buffer + strlen("error/"), dev->xbusd_path); 149 150 return path_buffer; 151 } 152 153 154 static void 155 _dev_error(struct xenbus_device *dev, int err, const char *fmt, 156 va_list ap) 157 { 158 int ret __diagused; 159 unsigned int len; 160 char *printf_buffer = NULL, *path_buffer = NULL; 161 162 #define PRINTF_BUFFER_SIZE 4096 163 printf_buffer = malloc(PRINTF_BUFFER_SIZE, M_DEVBUF, M_NOWAIT); 164 if (printf_buffer == NULL) 165 goto fail; 166 167 len = snprintf(printf_buffer, PRINTF_BUFFER_SIZE, "%i ", -err); 168 KASSERT(len < PRINTF_BUFFER_SIZE); 169 ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap); 170 KASSERT(len + ret < PRINTF_BUFFER_SIZE); 171 dev->xbusd_has_error = 1; 172 173 path_buffer = error_path(dev); 174 175 if (path_buffer == NULL) { 176 printk("xenbus: failed to write error node for %s (%s)\n", 177 dev->xbusd_path, printf_buffer); 178 goto fail; 179 } 180 181 if (xenbus_write(NULL, path_buffer, "error", printf_buffer) != 0) { 182 printk("xenbus: failed to write error node for %s (%s)\n", 183 dev->xbusd_path, printf_buffer); 184 goto fail; 185 } 186 187 fail: 188 if (printf_buffer) 189 free(printf_buffer, M_DEVBUF); 190 if (path_buffer) 191 free(path_buffer, M_DEVBUF); 192 } 193 194 195 void 196 xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, 197 ...) 198 { 199 va_list ap; 200 201 va_start(ap, fmt); 202 _dev_error(dev, err, fmt, ap); 203 va_end(ap); 204 } 205 206 207 void 208 xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, 209 ...) 210 { 211 va_list ap; 212 213 va_start(ap, fmt); 214 _dev_error(dev, err, fmt, ap); 215 va_end(ap); 216 217 xenbus_switch_state(dev, NULL, XenbusStateClosing); 218 } 219 220 221 int 222 xenbus_grant_ring(struct xenbus_device *dev, paddr_t ring_pa, 223 grant_ref_t *entryp) 224 { 225 int err = xengnt_grant_access(dev->xbusd_otherend_id, ring_pa, 226 0, entryp); 227 if (err != 0) 228 xenbus_dev_fatal(dev, err, "granting access to ring page"); 229 return err; 230 } 231 232 233 int 234 xenbus_alloc_evtchn(struct xenbus_device *dev, int *port) 235 { 236 evtchn_op_t op = { 237 .cmd = EVTCHNOP_alloc_unbound, 238 .u.alloc_unbound = { 239 .dom = DOMID_SELF, 240 .remote_dom = dev->xbusd_otherend_id, 241 .port = 0 242 } 243 }; 244 245 int err = HYPERVISOR_event_channel_op(&op); 246 if (err) 247 xenbus_dev_fatal(dev, err, "allocating event channel"); 248 else 249 *port = op.u.alloc_unbound.port; 250 return err; 251 } 252 253 254 XenbusState 255 xenbus_read_driver_state(const char *path) 256 { 257 u_long result; 258 259 int err = xenbus_read_ul(NULL, path, "state", &result, 10); 260 if (err) 261 result = XenbusStateClosed; 262 263 return result; 264 } 265 266 267 /* 268 * Local variables: 269 * c-file-style: "linux" 270 * indent-tabs-mode: t 271 * c-indent-level: 8 272 * c-basic-offset: 8 273 * tab-width: 8 274 * End: 275 */ 276