1*4882a593Smuzhiyun /******************************************************************************
2*4882a593Smuzhiyun * xenbus.h
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Talks to Xen Store to figure out what devices we have.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2005 Rusty Russell, IBM Corporation
7*4882a593Smuzhiyun * Copyright (C) 2005 XenSource Ltd.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or
10*4882a593Smuzhiyun * modify it under the terms of the GNU General Public License version 2
11*4882a593Smuzhiyun * as published by the Free Software Foundation; or, when distributed
12*4882a593Smuzhiyun * separately from the Linux kernel or incorporated into other
13*4882a593Smuzhiyun * software packages, subject to the following license:
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a copy
16*4882a593Smuzhiyun * of this source file (the "Software"), to deal in the Software without
17*4882a593Smuzhiyun * restriction, including without limitation the rights to use, copy, modify,
18*4882a593Smuzhiyun * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19*4882a593Smuzhiyun * and to permit persons to whom the Software is furnished to do so, subject to
20*4882a593Smuzhiyun * the following conditions:
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be included in
23*4882a593Smuzhiyun * all copies or substantial portions of the Software.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28*4882a593Smuzhiyun * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29*4882a593Smuzhiyun * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30*4882a593Smuzhiyun * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31*4882a593Smuzhiyun * IN THE SOFTWARE.
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #ifndef _XEN_XENBUS_H
35*4882a593Smuzhiyun #define _XEN_XENBUS_H
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #include <linux/device.h>
38*4882a593Smuzhiyun #include <linux/notifier.h>
39*4882a593Smuzhiyun #include <linux/mutex.h>
40*4882a593Smuzhiyun #include <linux/export.h>
41*4882a593Smuzhiyun #include <linux/fs.h>
42*4882a593Smuzhiyun #include <linux/completion.h>
43*4882a593Smuzhiyun #include <linux/init.h>
44*4882a593Smuzhiyun #include <linux/slab.h>
45*4882a593Smuzhiyun #include <linux/semaphore.h>
46*4882a593Smuzhiyun #include <xen/interface/xen.h>
47*4882a593Smuzhiyun #include <xen/interface/grant_table.h>
48*4882a593Smuzhiyun #include <xen/interface/io/xenbus.h>
49*4882a593Smuzhiyun #include <xen/interface/io/xs_wire.h>
50*4882a593Smuzhiyun #include <xen/interface/event_channel.h>
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #define XENBUS_MAX_RING_GRANT_ORDER 4
53*4882a593Smuzhiyun #define XENBUS_MAX_RING_GRANTS (1U << XENBUS_MAX_RING_GRANT_ORDER)
54*4882a593Smuzhiyun #define INVALID_GRANT_HANDLE (~0U)
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /* Register callback to watch this node. */
57*4882a593Smuzhiyun struct xenbus_watch
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun struct list_head list;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /* Path being watched. */
62*4882a593Smuzhiyun const char *node;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun unsigned int nr_pending;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun * Called just before enqueing new event while a spinlock is held.
68*4882a593Smuzhiyun * The event will be discarded if this callback returns false.
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun bool (*will_handle)(struct xenbus_watch *,
71*4882a593Smuzhiyun const char *path, const char *token);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* Callback (executed in a process context with no locks held). */
74*4882a593Smuzhiyun void (*callback)(struct xenbus_watch *,
75*4882a593Smuzhiyun const char *path, const char *token);
76*4882a593Smuzhiyun };
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /* A xenbus device. */
80*4882a593Smuzhiyun struct xenbus_device {
81*4882a593Smuzhiyun const char *devicetype;
82*4882a593Smuzhiyun const char *nodename;
83*4882a593Smuzhiyun const char *otherend;
84*4882a593Smuzhiyun int otherend_id;
85*4882a593Smuzhiyun struct xenbus_watch otherend_watch;
86*4882a593Smuzhiyun struct device dev;
87*4882a593Smuzhiyun enum xenbus_state state;
88*4882a593Smuzhiyun struct completion down;
89*4882a593Smuzhiyun struct work_struct work;
90*4882a593Smuzhiyun struct semaphore reclaim_sem;
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun
to_xenbus_device(struct device * dev)93*4882a593Smuzhiyun static inline struct xenbus_device *to_xenbus_device(struct device *dev)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun return container_of(dev, struct xenbus_device, dev);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun struct xenbus_device_id
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun /* .../device/<device_type>/<identifier> */
101*4882a593Smuzhiyun char devicetype[32]; /* General class of device. */
102*4882a593Smuzhiyun };
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /* A xenbus driver. */
105*4882a593Smuzhiyun struct xenbus_driver {
106*4882a593Smuzhiyun const char *name; /* defaults to ids[0].devicetype */
107*4882a593Smuzhiyun const struct xenbus_device_id *ids;
108*4882a593Smuzhiyun bool allow_rebind; /* avoid setting xenstore closed during remove */
109*4882a593Smuzhiyun int (*probe)(struct xenbus_device *dev,
110*4882a593Smuzhiyun const struct xenbus_device_id *id);
111*4882a593Smuzhiyun void (*otherend_changed)(struct xenbus_device *dev,
112*4882a593Smuzhiyun enum xenbus_state backend_state);
113*4882a593Smuzhiyun int (*remove)(struct xenbus_device *dev);
114*4882a593Smuzhiyun int (*suspend)(struct xenbus_device *dev);
115*4882a593Smuzhiyun int (*resume)(struct xenbus_device *dev);
116*4882a593Smuzhiyun int (*uevent)(struct xenbus_device *, struct kobj_uevent_env *);
117*4882a593Smuzhiyun struct device_driver driver;
118*4882a593Smuzhiyun int (*read_otherend_details)(struct xenbus_device *dev);
119*4882a593Smuzhiyun int (*is_ready)(struct xenbus_device *dev);
120*4882a593Smuzhiyun void (*reclaim_memory)(struct xenbus_device *dev);
121*4882a593Smuzhiyun };
122*4882a593Smuzhiyun
to_xenbus_driver(struct device_driver * drv)123*4882a593Smuzhiyun static inline struct xenbus_driver *to_xenbus_driver(struct device_driver *drv)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun return container_of(drv, struct xenbus_driver, driver);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun int __must_check __xenbus_register_frontend(struct xenbus_driver *drv,
129*4882a593Smuzhiyun struct module *owner,
130*4882a593Smuzhiyun const char *mod_name);
131*4882a593Smuzhiyun int __must_check __xenbus_register_backend(struct xenbus_driver *drv,
132*4882a593Smuzhiyun struct module *owner,
133*4882a593Smuzhiyun const char *mod_name);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun #define xenbus_register_frontend(drv) \
136*4882a593Smuzhiyun __xenbus_register_frontend(drv, THIS_MODULE, KBUILD_MODNAME)
137*4882a593Smuzhiyun #define xenbus_register_backend(drv) \
138*4882a593Smuzhiyun __xenbus_register_backend(drv, THIS_MODULE, KBUILD_MODNAME)
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun void xenbus_unregister_driver(struct xenbus_driver *drv);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun struct xenbus_transaction
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun u32 id;
145*4882a593Smuzhiyun };
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /* Nil transaction ID. */
148*4882a593Smuzhiyun #define XBT_NIL ((struct xenbus_transaction) { 0 })
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun char **xenbus_directory(struct xenbus_transaction t,
151*4882a593Smuzhiyun const char *dir, const char *node, unsigned int *num);
152*4882a593Smuzhiyun void *xenbus_read(struct xenbus_transaction t,
153*4882a593Smuzhiyun const char *dir, const char *node, unsigned int *len);
154*4882a593Smuzhiyun int xenbus_write(struct xenbus_transaction t,
155*4882a593Smuzhiyun const char *dir, const char *node, const char *string);
156*4882a593Smuzhiyun int xenbus_mkdir(struct xenbus_transaction t,
157*4882a593Smuzhiyun const char *dir, const char *node);
158*4882a593Smuzhiyun int xenbus_exists(struct xenbus_transaction t,
159*4882a593Smuzhiyun const char *dir, const char *node);
160*4882a593Smuzhiyun int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node);
161*4882a593Smuzhiyun int xenbus_transaction_start(struct xenbus_transaction *t);
162*4882a593Smuzhiyun int xenbus_transaction_end(struct xenbus_transaction t, int abort);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /* Single read and scanf: returns -errno or num scanned if > 0. */
165*4882a593Smuzhiyun __scanf(4, 5)
166*4882a593Smuzhiyun int xenbus_scanf(struct xenbus_transaction t,
167*4882a593Smuzhiyun const char *dir, const char *node, const char *fmt, ...);
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /* Read an (optional) unsigned value. */
170*4882a593Smuzhiyun unsigned int xenbus_read_unsigned(const char *dir, const char *node,
171*4882a593Smuzhiyun unsigned int default_val);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /* Single printf and write: returns -errno or 0. */
174*4882a593Smuzhiyun __printf(4, 5)
175*4882a593Smuzhiyun int xenbus_printf(struct xenbus_transaction t,
176*4882a593Smuzhiyun const char *dir, const char *node, const char *fmt, ...);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /* Generic read function: NULL-terminated triples of name,
179*4882a593Smuzhiyun * sprintf-style type string, and pointer. Returns 0 or errno.*/
180*4882a593Smuzhiyun int xenbus_gather(struct xenbus_transaction t, const char *dir, ...);
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /* notifer routines for when the xenstore comes up */
183*4882a593Smuzhiyun extern int xenstored_ready;
184*4882a593Smuzhiyun int register_xenstore_notifier(struct notifier_block *nb);
185*4882a593Smuzhiyun void unregister_xenstore_notifier(struct notifier_block *nb);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun int register_xenbus_watch(struct xenbus_watch *watch);
188*4882a593Smuzhiyun void unregister_xenbus_watch(struct xenbus_watch *watch);
189*4882a593Smuzhiyun void xs_suspend(void);
190*4882a593Smuzhiyun void xs_resume(void);
191*4882a593Smuzhiyun void xs_suspend_cancel(void);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun struct work_struct;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun #define XENBUS_IS_ERR_READ(str) ({ \
196*4882a593Smuzhiyun if (!IS_ERR(str) && strlen(str) == 0) { \
197*4882a593Smuzhiyun kfree(str); \
198*4882a593Smuzhiyun str = ERR_PTR(-ERANGE); \
199*4882a593Smuzhiyun } \
200*4882a593Smuzhiyun IS_ERR(str); \
201*4882a593Smuzhiyun })
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun #define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun int xenbus_watch_path(struct xenbus_device *dev, const char *path,
206*4882a593Smuzhiyun struct xenbus_watch *watch,
207*4882a593Smuzhiyun bool (*will_handle)(struct xenbus_watch *,
208*4882a593Smuzhiyun const char *, const char *),
209*4882a593Smuzhiyun void (*callback)(struct xenbus_watch *,
210*4882a593Smuzhiyun const char *, const char *));
211*4882a593Smuzhiyun __printf(5, 6)
212*4882a593Smuzhiyun int xenbus_watch_pathfmt(struct xenbus_device *dev, struct xenbus_watch *watch,
213*4882a593Smuzhiyun bool (*will_handle)(struct xenbus_watch *,
214*4882a593Smuzhiyun const char *, const char *),
215*4882a593Smuzhiyun void (*callback)(struct xenbus_watch *,
216*4882a593Smuzhiyun const char *, const char *),
217*4882a593Smuzhiyun const char *pathfmt, ...);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state new_state);
220*4882a593Smuzhiyun int xenbus_grant_ring(struct xenbus_device *dev, void *vaddr,
221*4882a593Smuzhiyun unsigned int nr_pages, grant_ref_t *grefs);
222*4882a593Smuzhiyun int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t *gnt_refs,
223*4882a593Smuzhiyun unsigned int nr_grefs, void **vaddr);
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr);
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun int xenbus_alloc_evtchn(struct xenbus_device *dev, evtchn_port_t *port);
228*4882a593Smuzhiyun int xenbus_free_evtchn(struct xenbus_device *dev, evtchn_port_t port);
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun enum xenbus_state xenbus_read_driver_state(const char *path);
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun __printf(3, 4)
233*4882a593Smuzhiyun void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...);
234*4882a593Smuzhiyun __printf(3, 4)
235*4882a593Smuzhiyun void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...);
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun const char *xenbus_strstate(enum xenbus_state state);
238*4882a593Smuzhiyun int xenbus_dev_is_online(struct xenbus_device *dev);
239*4882a593Smuzhiyun int xenbus_frontend_closed(struct xenbus_device *dev);
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun extern const struct file_operations xen_xenbus_fops;
242*4882a593Smuzhiyun extern struct xenstore_domain_interface *xen_store_interface;
243*4882a593Smuzhiyun extern int xen_store_evtchn;
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun #endif /* _XEN_XENBUS_H */
246