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