blob: 850a43bd69d32b8da48162b5430e2ff2b50673ef [file] [log] [blame]
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -07001/******************************************************************************
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>
Paul Gortmaker63c97442011-07-10 13:22:07 -040040#include <linux/export.h>
Juergen Gross332f7912017-02-09 14:39:56 +010041#include <linux/fs.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070042#include <linux/completion.h>
43#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Juergen Gross2f69a112020-03-05 11:03:23 +010045#include <linux/semaphore.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070046#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
Julien Grall9cce2912015-10-13 17:50:11 +010051#define XENBUS_MAX_RING_GRANT_ORDER 4
52#define XENBUS_MAX_RING_GRANTS (1U << XENBUS_MAX_RING_GRANT_ORDER)
Wei Liuccc9d902015-04-03 14:44:59 +080053#define INVALID_GRANT_HANDLE (~0U)
54
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070055/* Register callback to watch this node. */
56struct xenbus_watch
57{
58 struct list_head list;
59
60 /* Path being watched. */
61 const char *node;
62
63 /* Callback (executed in a process context with no locks held). */
64 void (*callback)(struct xenbus_watch *,
Juergen Gross5584ea22017-02-09 14:39:57 +010065 const char *path, const char *token);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070066};
67
68
69/* A xenbus device. */
70struct xenbus_device {
71 const char *devicetype;
72 const char *nodename;
73 const char *otherend;
74 int otherend_id;
75 struct xenbus_watch otherend_watch;
76 struct device dev;
77 enum xenbus_state state;
78 struct completion down;
Aurelien Chartier2abb2742013-05-28 18:09:56 +010079 struct work_struct work;
Juergen Gross2f69a112020-03-05 11:03:23 +010080 struct semaphore reclaim_sem;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070081};
82
83static inline struct xenbus_device *to_xenbus_device(struct device *dev)
84{
85 return container_of(dev, struct xenbus_device, dev);
86}
87
88struct xenbus_device_id
89{
90 /* .../device/<device_type>/<identifier> */
91 char devicetype[32]; /* General class of device. */
92};
93
94/* A xenbus driver. */
95struct xenbus_driver {
David Vrabel95afae42014-09-08 17:30:41 +010096 const char *name; /* defaults to ids[0].devicetype */
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070097 const struct xenbus_device_id *ids;
Paul Durrant672b7762019-12-11 15:29:54 +000098 bool allow_rebind; /* avoid setting xenstore closed during remove */
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070099 int (*probe)(struct xenbus_device *dev,
100 const struct xenbus_device_id *id);
101 void (*otherend_changed)(struct xenbus_device *dev,
102 enum xenbus_state backend_state);
103 int (*remove)(struct xenbus_device *dev);
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800104 int (*suspend)(struct xenbus_device *dev);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700105 int (*resume)(struct xenbus_device *dev);
Ian Campbelldf660252009-02-09 12:05:51 -0800106 int (*uevent)(struct xenbus_device *, struct kobj_uevent_env *);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700107 struct device_driver driver;
108 int (*read_otherend_details)(struct xenbus_device *dev);
Christian Limpach1d78d702008-04-02 10:54:04 -0700109 int (*is_ready)(struct xenbus_device *dev);
SeongJae Park8a105672020-01-27 09:18:08 +0100110 void (*reclaim_memory)(struct xenbus_device *dev);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700111};
112
113static inline struct xenbus_driver *to_xenbus_driver(struct device_driver *drv)
114{
115 return container_of(drv, struct xenbus_driver, driver);
116}
117
David Vrabel95afae42014-09-08 17:30:41 +0100118int __must_check __xenbus_register_frontend(struct xenbus_driver *drv,
119 struct module *owner,
120 const char *mod_name);
121int __must_check __xenbus_register_backend(struct xenbus_driver *drv,
122 struct module *owner,
123 const char *mod_name);
124
125#define xenbus_register_frontend(drv) \
Yuval Shaia604b91f2015-03-01 02:57:38 -0800126 __xenbus_register_frontend(drv, THIS_MODULE, KBUILD_MODNAME)
David Vrabel95afae42014-09-08 17:30:41 +0100127#define xenbus_register_backend(drv) \
Yuval Shaia604b91f2015-03-01 02:57:38 -0800128 __xenbus_register_backend(drv, THIS_MODULE, KBUILD_MODNAME)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700129
130void xenbus_unregister_driver(struct xenbus_driver *drv);
131
132struct xenbus_transaction
133{
134 u32 id;
135};
136
137/* Nil transaction ID. */
138#define XBT_NIL ((struct xenbus_transaction) { 0 })
139
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700140char **xenbus_directory(struct xenbus_transaction t,
141 const char *dir, const char *node, unsigned int *num);
142void *xenbus_read(struct xenbus_transaction t,
143 const char *dir, const char *node, unsigned int *len);
144int xenbus_write(struct xenbus_transaction t,
145 const char *dir, const char *node, const char *string);
146int xenbus_mkdir(struct xenbus_transaction t,
147 const char *dir, const char *node);
148int xenbus_exists(struct xenbus_transaction t,
149 const char *dir, const char *node);
150int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node);
151int xenbus_transaction_start(struct xenbus_transaction *t);
152int xenbus_transaction_end(struct xenbus_transaction t, int abort);
153
154/* Single read and scanf: returns -errno or num scanned if > 0. */
Joe Perches6061d942012-03-23 15:02:16 -0700155__scanf(4, 5)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700156int xenbus_scanf(struct xenbus_transaction t,
Joe Perches6061d942012-03-23 15:02:16 -0700157 const char *dir, const char *node, const char *fmt, ...);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700158
Juergen Gross9c53a172016-10-31 14:58:40 +0100159/* Read an (optional) unsigned value. */
160unsigned int xenbus_read_unsigned(const char *dir, const char *node,
161 unsigned int default_val);
162
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700163/* Single printf and write: returns -errno or 0. */
Joe Perchesb9075fa2011-10-31 17:11:33 -0700164__printf(4, 5)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700165int xenbus_printf(struct xenbus_transaction t,
Joe Perchesb9075fa2011-10-31 17:11:33 -0700166 const char *dir, const char *node, const char *fmt, ...);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700167
168/* Generic read function: NULL-terminated triples of name,
169 * sprintf-style type string, and pointer. Returns 0 or errno.*/
170int xenbus_gather(struct xenbus_transaction t, const char *dir, ...);
171
172/* notifer routines for when the xenstore comes up */
173extern int xenstored_ready;
174int register_xenstore_notifier(struct notifier_block *nb);
175void unregister_xenstore_notifier(struct notifier_block *nb);
176
177int register_xenbus_watch(struct xenbus_watch *watch);
178void unregister_xenbus_watch(struct xenbus_watch *watch);
179void xs_suspend(void);
180void xs_resume(void);
181void xs_suspend_cancel(void);
182
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700183struct work_struct;
184
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700185void xenbus_probe(struct work_struct *);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700186
187#define XENBUS_IS_ERR_READ(str) ({ \
188 if (!IS_ERR(str) && strlen(str) == 0) { \
189 kfree(str); \
190 str = ERR_PTR(-ERANGE); \
191 } \
192 IS_ERR(str); \
193})
194
195#define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
196
197int xenbus_watch_path(struct xenbus_device *dev, const char *path,
198 struct xenbus_watch *watch,
199 void (*callback)(struct xenbus_watch *,
Juergen Gross5584ea22017-02-09 14:39:57 +0100200 const char *, const char *));
Joe Perchesb9075fa2011-10-31 17:11:33 -0700201__printf(4, 5)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700202int xenbus_watch_pathfmt(struct xenbus_device *dev, struct xenbus_watch *watch,
203 void (*callback)(struct xenbus_watch *,
Juergen Gross5584ea22017-02-09 14:39:57 +0100204 const char *, const char *),
Joe Perchesb9075fa2011-10-31 17:11:33 -0700205 const char *pathfmt, ...);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700206
207int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state new_state);
Wei Liuccc9d902015-04-03 14:44:59 +0800208int xenbus_grant_ring(struct xenbus_device *dev, void *vaddr,
209 unsigned int nr_pages, grant_ref_t *grefs);
210int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t *gnt_refs,
211 unsigned int nr_grefs, void **vaddr);
212int xenbus_map_ring(struct xenbus_device *dev,
213 grant_ref_t *gnt_refs, unsigned int nr_grefs,
214 grant_handle_t *handles, unsigned long *vaddrs,
215 bool *leaked);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700216
217int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr);
218int xenbus_unmap_ring(struct xenbus_device *dev,
Wei Liuccc9d902015-04-03 14:44:59 +0800219 grant_handle_t *handles, unsigned int nr_handles,
220 unsigned long *vaddrs);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700221
222int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700223int xenbus_free_evtchn(struct xenbus_device *dev, int port);
224
225enum xenbus_state xenbus_read_driver_state(const char *path);
226
Joe Perchesb9075fa2011-10-31 17:11:33 -0700227__printf(3, 4)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700228void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...);
Joe Perchesb9075fa2011-10-31 17:11:33 -0700229__printf(3, 4)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700230void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...);
231
232const char *xenbus_strstate(enum xenbus_state state);
233int xenbus_dev_is_online(struct xenbus_device *dev);
234int xenbus_frontend_closed(struct xenbus_device *dev);
235
Juergen Gross332f7912017-02-09 14:39:56 +0100236extern const struct file_operations xen_xenbus_fops;
237extern struct xenstore_domain_interface *xen_store_interface;
238extern int xen_store_evtchn;
239
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700240#endif /* _XEN_XENBUS_H */