1843e1988Sjohnlev /* 2843e1988Sjohnlev * CDDL HEADER START 3843e1988Sjohnlev * 4843e1988Sjohnlev * The contents of this file are subject to the terms of the 5843e1988Sjohnlev * Common Development and Distribution License (the "License"). 6843e1988Sjohnlev * You may not use this file except in compliance with the License. 7843e1988Sjohnlev * 8843e1988Sjohnlev * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9843e1988Sjohnlev * or http://www.opensolaris.org/os/licensing. 10843e1988Sjohnlev * See the License for the specific language governing permissions 11843e1988Sjohnlev * and limitations under the License. 12843e1988Sjohnlev * 13843e1988Sjohnlev * When distributing Covered Code, include this CDDL HEADER in each 14843e1988Sjohnlev * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15843e1988Sjohnlev * If applicable, add the following below this CDDL HEADER, with the 16843e1988Sjohnlev * fields enclosed by brackets "[]" replaced with your own identifying 17843e1988Sjohnlev * information: Portions Copyright [yyyy] [name of copyright owner] 18843e1988Sjohnlev * 19843e1988Sjohnlev * CDDL HEADER END 20843e1988Sjohnlev */ 21843e1988Sjohnlev 22843e1988Sjohnlev /* 237f0b8309SEdward Pilatowicz * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24843e1988Sjohnlev * Use is subject to license terms. 25843e1988Sjohnlev */ 26843e1988Sjohnlev 27843e1988Sjohnlev /* 28843e1988Sjohnlev * 29843e1988Sjohnlev * xenbus.h (renamed to xenbus_impl.h) 30843e1988Sjohnlev * 31843e1988Sjohnlev * Talks to Xen Store to figure out what devices we have. 32843e1988Sjohnlev * 33843e1988Sjohnlev * Copyright (C) 2005 Rusty Russell, IBM Corporation 34843e1988Sjohnlev * 35843e1988Sjohnlev * This file may be distributed separately from the Linux kernel, or 36843e1988Sjohnlev * incorporated into other software packages, subject to the following license: 37843e1988Sjohnlev * 38843e1988Sjohnlev * Permission is hereby granted, free of charge, to any person obtaining a copy 39843e1988Sjohnlev * of this source file (the "Software"), to deal in the Software without 40843e1988Sjohnlev * restriction, including without limitation the rights to use, copy, modify, 41843e1988Sjohnlev * merge, publish, distribute, sublicense, and/or sell copies of the Software, 42843e1988Sjohnlev * and to permit persons to whom the Software is furnished to do so, subject to 43843e1988Sjohnlev * the following conditions: 44843e1988Sjohnlev * 45843e1988Sjohnlev * The above copyright notice and this permission notice shall be included in 46843e1988Sjohnlev * all copies or substantial portions of the Software. 47843e1988Sjohnlev * 48843e1988Sjohnlev * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 49843e1988Sjohnlev * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 50843e1988Sjohnlev * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 51843e1988Sjohnlev * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 52843e1988Sjohnlev * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 53843e1988Sjohnlev * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 54843e1988Sjohnlev * IN THE SOFTWARE. 55843e1988Sjohnlev */ 56843e1988Sjohnlev 57843e1988Sjohnlev #ifndef _SYS_XENBUS_H 58843e1988Sjohnlev #define _SYS_XENBUS_H 59843e1988Sjohnlev 60843e1988Sjohnlev #include <sys/mutex.h> 61843e1988Sjohnlev #include <sys/list.h> 62843e1988Sjohnlev 63843e1988Sjohnlev #ifdef __cplusplus 64843e1988Sjohnlev extern "C" { 65843e1988Sjohnlev #endif 66843e1988Sjohnlev 67843e1988Sjohnlev #define XBT_NULL 0 68843e1988Sjohnlev 69843e1988Sjohnlev typedef uint32_t xenbus_transaction_t; 70843e1988Sjohnlev 71843e1988Sjohnlev /* Register callback to watch this node. */ 727f0b8309SEdward Pilatowicz struct xenbus_watch; 737f0b8309SEdward Pilatowicz typedef void (*xenbus_watch_cb_t)(struct xenbus_watch *, 747f0b8309SEdward Pilatowicz const char **vec, unsigned int len); 757f0b8309SEdward Pilatowicz struct xenbus_watch { 76*349b53ddSStuart Maybee list_node_t list; 77843e1988Sjohnlev const char *node; /* path being watched */ 787f0b8309SEdward Pilatowicz xenbus_watch_cb_t callback; 79843e1988Sjohnlev struct xenbus_device *dev; 80843e1988Sjohnlev }; 81843e1988Sjohnlev 82843e1988Sjohnlev /* 83843e1988Sjohnlev * Call this function when xenstore is available, i.e. the daemon is 84843e1988Sjohnlev * connected to the xenbus device. 85843e1988Sjohnlev */ 86843e1988Sjohnlev struct xenbus_notify { 87*349b53ddSStuart Maybee list_node_t list; 88843e1988Sjohnlev void (*notify_func) (int); 89843e1988Sjohnlev }; 90843e1988Sjohnlev 91843e1988Sjohnlev /* A xenbus device. */ 92843e1988Sjohnlev struct xenbus_device { 93843e1988Sjohnlev const char *devicetype; 94843e1988Sjohnlev const char *nodename; 95843e1988Sjohnlev const char *otherend; 96843e1988Sjohnlev int otherend_id; 97843e1988Sjohnlev int otherend_state; 98843e1988Sjohnlev struct xenbus_watch otherend_watch; 99843e1988Sjohnlev int has_error; 100843e1988Sjohnlev int frontend; 101843e1988Sjohnlev void (*otherend_changed)(struct xenbus_device *, XenbusState); 102843e1988Sjohnlev void *data; 103843e1988Sjohnlev }; 104843e1988Sjohnlev 1057f0b8309SEdward Pilatowicz typedef void (*xvdi_xb_watch_cb_t)(dev_info_t *dip, const char *path, 1067f0b8309SEdward Pilatowicz void *arg); 1077f0b8309SEdward Pilatowicz 1087f0b8309SEdward Pilatowicz typedef struct xd_xb_watches { 1097f0b8309SEdward Pilatowicz list_node_t xxw_list; 1107f0b8309SEdward Pilatowicz int xxw_ref; 1117f0b8309SEdward Pilatowicz struct xenbus_watch xxw_watch; 1127f0b8309SEdward Pilatowicz struct xendev_ppd *xxw_xppd; 1137f0b8309SEdward Pilatowicz xvdi_xb_watch_cb_t xxw_cb; 1147f0b8309SEdward Pilatowicz void *xxw_arg; 1157f0b8309SEdward Pilatowicz } xd_xb_watches_t; 116843e1988Sjohnlev 117843e1988Sjohnlev extern char **xenbus_directory(xenbus_transaction_t t, const char *dir, 118843e1988Sjohnlev const char *node, unsigned int *num); 119843e1988Sjohnlev extern int xenbus_read(xenbus_transaction_t t, const char *dir, 120843e1988Sjohnlev const char *node, void **rstr, unsigned int *len); 1217f0b8309SEdward Pilatowicz extern int xenbus_read_str(const char *dir, const char *node, char **rstr); 122843e1988Sjohnlev extern int xenbus_write(xenbus_transaction_t t, const char *dir, 123843e1988Sjohnlev const char *node, const char *string); 124843e1988Sjohnlev extern int xenbus_mkdir(xenbus_transaction_t t, const char *dir, 125843e1988Sjohnlev const char *node); 1267f0b8309SEdward Pilatowicz extern boolean_t xenbus_exists(const char *dir, const char *node); 1277f0b8309SEdward Pilatowicz extern boolean_t xenbus_exists_dir(const char *dir, const char *node); 128843e1988Sjohnlev extern int xenbus_rm(xenbus_transaction_t t, const char *dir, 129843e1988Sjohnlev const char *node); 130843e1988Sjohnlev extern int xenbus_transaction_start(xenbus_transaction_t *t); 131843e1988Sjohnlev extern int xenbus_transaction_end(xenbus_transaction_t t, int abort); 132843e1988Sjohnlev 133843e1988Sjohnlev /* Single read and scanf: returns errno or num scanned if > 0. */ 134843e1988Sjohnlev extern int xenbus_scanf(xenbus_transaction_t t, const char *dir, 135843e1988Sjohnlev const char *node, const char *fmt, ...); 136843e1988Sjohnlev 137843e1988Sjohnlev /* Single printf and write: returns errno or 0. */ 138843e1988Sjohnlev extern int xenbus_printf(xenbus_transaction_t t, const char *dir, 139843e1988Sjohnlev const char *node, const char *fmt, ...); 140843e1988Sjohnlev 141843e1988Sjohnlev /* 142843e1988Sjohnlev * Generic read function: NULL-terminated triples of name, 143843e1988Sjohnlev * sprintf-style type string, and pointer. Returns 0 or errno. 144843e1988Sjohnlev */ 145843e1988Sjohnlev extern int xenbus_gather(xenbus_transaction_t t, const char *dir, ...); 146843e1988Sjohnlev 147843e1988Sjohnlev extern int register_xenbus_watch(struct xenbus_watch *watch); 148843e1988Sjohnlev extern void unregister_xenbus_watch(struct xenbus_watch *watch); 149843e1988Sjohnlev extern void reregister_xenbus_watches(void); 150843e1988Sjohnlev 151843e1988Sjohnlev /* Called from xen core code. */ 152843e1988Sjohnlev extern void xenbus_suspend(void); 153843e1988Sjohnlev extern void xenbus_resume(void); 154843e1988Sjohnlev 155843e1988Sjohnlev #define XENBUS_EXIST_ERR(err) ((err) == ENOENT || (err) == ERANGE) 156843e1988Sjohnlev 157843e1988Sjohnlev /* 158843e1988Sjohnlev * Register a watch on the given path, using the given xenbus_watch structure 159843e1988Sjohnlev * for storage, and the given callback function as the callback. Return 0 on 160843e1988Sjohnlev * success, or errno on error. On success, the given path will be saved as 161843e1988Sjohnlev * watch->node, and remains the caller's to free. On error, watch->node will 162843e1988Sjohnlev * be NULL, the device will switch to XenbusStateClosing, and the error will 163843e1988Sjohnlev * be saved in the store. 164843e1988Sjohnlev */ 165843e1988Sjohnlev extern int xenbus_watch_path(struct xenbus_device *dev, const char *path, 166843e1988Sjohnlev struct xenbus_watch *watch, 167843e1988Sjohnlev void (*callback)(struct xenbus_watch *, 168843e1988Sjohnlev const char **, unsigned int)); 169843e1988Sjohnlev 170843e1988Sjohnlev 171843e1988Sjohnlev /* 172843e1988Sjohnlev * Register a watch on the given path/path2, using the given xenbus_watch 173843e1988Sjohnlev * structure for storage, and the given callback function as the callback. 174843e1988Sjohnlev * Return 0 on success, or errno on error. On success, the watched path 175843e1988Sjohnlev * (path/path2) will be saved as watch->node, and becomes the caller's to 176843e1988Sjohnlev * kfree(). On error, watch->node will be NULL, so the caller has nothing to 177843e1988Sjohnlev * free, the device will switch to XenbusStateClosing, and the error will be 178843e1988Sjohnlev * saved in the store. 179843e1988Sjohnlev */ 180843e1988Sjohnlev extern int xenbus_watch_path2(struct xenbus_device *dev, const char *path, 181843e1988Sjohnlev const char *path2, struct xenbus_watch *watch, 182843e1988Sjohnlev void (*callback)(struct xenbus_watch *, 183843e1988Sjohnlev const char **, unsigned int)); 184843e1988Sjohnlev 185843e1988Sjohnlev 186843e1988Sjohnlev /* 187843e1988Sjohnlev * Advertise in the store a change of the given driver to the given new_state. 188843e1988Sjohnlev * Perform the change inside the given transaction xbt. xbt may be NULL, in 189843e1988Sjohnlev * which case this is performed inside its own transaction. Return 0 on 190843e1988Sjohnlev * success, or errno on error. On error, the device will switch to 191843e1988Sjohnlev * XenbusStateClosing, and the error will be saved in the store. 192843e1988Sjohnlev */ 193843e1988Sjohnlev extern int xenbus_switch_state(struct xenbus_device *dev, 194843e1988Sjohnlev xenbus_transaction_t xbt, 195843e1988Sjohnlev XenbusState new_state); 196843e1988Sjohnlev 197843e1988Sjohnlev 198843e1988Sjohnlev /* 199843e1988Sjohnlev * Grant access to the given ring_mfn to the peer of the given device. Return 200843e1988Sjohnlev * 0 on success, or errno on error. On error, the device will switch to 201843e1988Sjohnlev * XenbusStateClosing, and the error will be saved in the store. 202843e1988Sjohnlev */ 203843e1988Sjohnlev extern int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn); 204843e1988Sjohnlev 205843e1988Sjohnlev 206843e1988Sjohnlev /* 207843e1988Sjohnlev * Allocate an event channel for the given xenbus_device, assigning the newly 208843e1988Sjohnlev * created local port to *port. Return 0 on success, or errno on error. On 209843e1988Sjohnlev * error, the device will switch to XenbusStateClosing, and the error will be 210843e1988Sjohnlev * saved in the store. 211843e1988Sjohnlev */ 212843e1988Sjohnlev extern int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port); 213843e1988Sjohnlev 214843e1988Sjohnlev 215843e1988Sjohnlev /* 216843e1988Sjohnlev * Return the state of the driver rooted at the given store path, or 217843e1988Sjohnlev * XenbusStateClosed if no state can be read. 218843e1988Sjohnlev */ 219843e1988Sjohnlev extern XenbusState xenbus_read_driver_state(const char *path); 220843e1988Sjohnlev 221843e1988Sjohnlev 222843e1988Sjohnlev /* 223843e1988Sjohnlev * Report the given negative errno into the store, along with the given 224843e1988Sjohnlev * formatted message. 225843e1988Sjohnlev */ 226843e1988Sjohnlev extern void xenbus_dev_error(struct xenbus_device *dev, int err, 227843e1988Sjohnlev const char *fmt, ...); 228843e1988Sjohnlev 229843e1988Sjohnlev 230843e1988Sjohnlev /* 231843e1988Sjohnlev * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by 232843e1988Sjohnlev * xenbus_switch_state(dev, NULL, XenbusStateClosing) to schedule an orderly 233843e1988Sjohnlev * closedown of this driver and its peer. 234843e1988Sjohnlev */ 235843e1988Sjohnlev extern void xenbus_dev_fatal(struct xenbus_device *dev, 236843e1988Sjohnlev int err, const char *fmt, ...); 237843e1988Sjohnlev 238843e1988Sjohnlev /* Clear any error. */ 239843e1988Sjohnlev extern void xenbus_dev_ok(struct xenbus_device *dev); 240843e1988Sjohnlev 241843e1988Sjohnlev /* 242843e1988Sjohnlev * Set up watches on other end of split device. 243843e1988Sjohnlev */ 244843e1988Sjohnlev extern int talk_to_otherend(struct xenbus_device *dev); 245843e1988Sjohnlev 246843e1988Sjohnlev #define XENSTORE_DOWN 0 /* xenstore is down */ 247843e1988Sjohnlev #define XENSTORE_UP 1 /* xenstore is up */ 248843e1988Sjohnlev 249843e1988Sjohnlev /* 250843e1988Sjohnlev * Register a notify callback function. 251843e1988Sjohnlev */ 252843e1988Sjohnlev extern int xs_register_xenbus_callback(void (*callback)(int)); 253843e1988Sjohnlev 254843e1988Sjohnlev /* 255843e1988Sjohnlev * Notify clients that xenstore is up 256843e1988Sjohnlev */ 257843e1988Sjohnlev extern void xs_notify_xenstore_up(void); 258843e1988Sjohnlev 259843e1988Sjohnlev /* 260843e1988Sjohnlev * Notify clients that xenstore is down 261843e1988Sjohnlev */ 262843e1988Sjohnlev extern void xs_notify_xenstore_down(void); 263843e1988Sjohnlev 264843e1988Sjohnlev struct xsd_sockmsg; 265843e1988Sjohnlev 266843e1988Sjohnlev extern int xenbus_dev_request_and_reply(struct xsd_sockmsg *, void **); 267843e1988Sjohnlev 268843e1988Sjohnlev #ifdef __cplusplus 269843e1988Sjohnlev } 270843e1988Sjohnlev #endif 271843e1988Sjohnlev 272843e1988Sjohnlev #endif /* _SYS_XENBUS_H */ 273