iPXE
xenbus.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ipxe/malloc.h>
30 #include <ipxe/device.h>
31 #include <ipxe/timer.h>
32 #include <ipxe/nap.h>
33 #include <ipxe/xen.h>
34 #include <ipxe/xenstore.h>
35 #include <ipxe/xenbus.h>
36 
37 /** @file
38  *
39  * Xen device bus
40  *
41  */
42 
43 /* Disambiguate the various error causes */
44 #define ETIMEDOUT_UNKNOWN \
45  __einfo_error ( EINFO_ETIMEDOUT_UNKNOWN )
46 #define EINFO_ETIMEDOUT_UNKNOWN \
47  __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateUnknown, \
48  "Unknown" )
49 #define ETIMEDOUT_INITIALISING \
50  __einfo_error ( EINFO_ETIMEDOUT_INITIALISING )
51 #define EINFO_ETIMEDOUT_INITIALISING \
52  __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateInitialising, \
53  "Initialising" )
54 #define ETIMEDOUT_INITWAIT \
55  __einfo_error ( EINFO_ETIMEDOUT_INITWAIT )
56 #define EINFO_ETIMEDOUT_INITWAIT \
57  __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateInitWait, \
58  "InitWait" )
59 #define ETIMEDOUT_INITIALISED \
60  __einfo_error ( EINFO_ETIMEDOUT_INITIALISED )
61 #define EINFO_ETIMEDOUT_INITIALISED \
62  __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateInitialised, \
63  "Initialised" )
64 #define ETIMEDOUT_CONNECTED \
65  __einfo_error ( EINFO_ETIMEDOUT_CONNECTED )
66 #define EINFO_ETIMEDOUT_CONNECTED \
67  __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateConnected, \
68  "Connected" )
69 #define ETIMEDOUT_CLOSING \
70  __einfo_error ( EINFO_ETIMEDOUT_CLOSING )
71 #define EINFO_ETIMEDOUT_CLOSING \
72  __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateClosing, \
73  "Closing" )
74 #define ETIMEDOUT_CLOSED \
75  __einfo_error ( EINFO_ETIMEDOUT_CLOSED )
76 #define EINFO_ETIMEDOUT_CLOSED \
77  __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateClosed, \
78  "Closed" )
79 #define ETIMEDOUT_RECONFIGURING \
80  __einfo_error ( EINFO_ETIMEDOUT_RECONFIGURING )
81 #define EINFO_ETIMEDOUT_RECONFIGURING \
82  __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateReconfiguring, \
83  "Reconfiguring" )
84 #define ETIMEDOUT_RECONFIGURED \
85  __einfo_error ( EINFO_ETIMEDOUT_RECONFIGURED )
86 #define EINFO_ETIMEDOUT_RECONFIGURED \
87  __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateReconfigured, \
88  "Reconfigured" )
89 #define ETIMEDOUT_STATE( state ) \
90  EUNIQ ( EINFO_ETIMEDOUT, (state), ETIMEDOUT_UNKNOWN, \
91  ETIMEDOUT_INITIALISING, ETIMEDOUT_INITWAIT, \
92  ETIMEDOUT_INITIALISED, ETIMEDOUT_CONNECTED, \
93  ETIMEDOUT_CLOSING, ETIMEDOUT_CLOSED, \
94  ETIMEDOUT_RECONFIGURING, ETIMEDOUT_RECONFIGURED )
95 
96 /** Maximum time to wait for backend to reach a given state, in ticks */
97 #define XENBUS_BACKEND_TIMEOUT ( 5 * TICKS_PER_SEC )
98 
99 /**
100  * Set device state
101  *
102  * @v xendev Xen device
103  * @v state New state
104  * @ret rc Return status code
105  */
106 int xenbus_set_state ( struct xen_device *xendev, int state ) {
107  int rc;
108 
109  /* Attempt to set state */
110  if ( ( rc = xenstore_write_num ( xendev->xen, state, xendev->key,
111  "state", NULL ) ) != 0 ) {
112  DBGC ( xendev, "XENBUS %s could not set state=\"%d\": %s\n",
113  xendev->key, state, strerror ( rc ) );
114  return rc;
115  }
116 
117  return 0;
118 }
119 
120 /**
121  * Get backend state
122  *
123  * @v xendev Xen device
124  * @ret state Backend state, or negative error
125  */
126 int xenbus_backend_state ( struct xen_device *xendev ) {
127  unsigned long state;
128  int rc;
129 
130  /* Attempt to get backend state */
131  if ( ( rc = xenstore_read_num ( xendev->xen, &state, xendev->backend,
132  "state", NULL ) ) != 0 ) {
133  DBGC ( xendev, "XENBUS %s could not read %s/state: %s\n",
134  xendev->key, xendev->backend, strerror ( rc ) );
135  return rc;
136  }
137 
138  return state;
139 }
140 
141 /**
142  * Wait for backend to reach a given state
143  *
144  * @v xendev Xen device
145  * @v state Desired backend state
146  * @ret rc Return status code
147  */
148 int xenbus_backend_wait ( struct xen_device *xendev, int state ) {
149  unsigned long started = currticks();
150  unsigned long elapsed;
151  unsigned int attempts = 0;
152  int current_state;
153  int rc;
154 
155  /* Wait for backend to reach this state */
156  do {
157 
158  /* Get current backend state */
159  current_state = xenbus_backend_state ( xendev );
160  if ( current_state < 0 ) {
161  rc = current_state;
162  return rc;
163  }
164  if ( current_state == state )
165  return 0;
166 
167  /* Allow time for backend to react */
168  cpu_nap();
169 
170  /* XenStore is a very slow interface; any fixed delay
171  * time would be dwarfed by the XenStore access time.
172  * We therefore use wall clock to time out this
173  * operation.
174  */
175  elapsed = ( currticks() - started );
176  attempts++;
177 
178  } while ( elapsed < XENBUS_BACKEND_TIMEOUT );
179 
180  /* Construct status code from current backend state */
181  rc = -ETIMEDOUT_STATE ( current_state );
182  DBGC ( xendev, "XENBUS %s timed out after %d attempts waiting for "
183  "%s/state=\"%d\": %s\n", xendev->key, attempts, xendev->backend,
184  state, strerror ( rc ) );
185 
186  return rc;
187 }
188 
189 /**
190  * Find driver for Xen device
191  *
192  * @v type Device type
193  * @ret driver Driver, or NULL
194  */
195 static struct xen_driver * xenbus_find_driver ( const char *type ) {
196  struct xen_driver *xendrv;
197 
198  for_each_table_entry ( xendrv, XEN_DRIVERS ) {
199  if ( strcmp ( xendrv->type, type ) == 0 )
200  return xendrv;
201  }
202  return NULL;
203 }
204 
205 /**
206  * Probe Xen device
207  *
208  * @v xen Xen hypervisor
209  * @v parent Parent device
210  * @v instance Device instance
211  * @v driver Device driver
212  * @ret rc Return status code
213  */
214 static int xenbus_probe_device ( struct xen_hypervisor *xen,
215  struct device *parent, const char *instance,
216  struct xen_driver *driver ) {
217  const char *type = driver->type;
218  struct xen_device *xendev;
219  size_t key_len;
220  int rc;
221 
222  /* Allocate and initialise structure */
223  key_len = ( 7 /* "device/" */ + strlen ( type ) + 1 /* "/" */ +
224  strlen ( instance ) + 1 /* NUL */ );
225  xendev = zalloc ( sizeof ( *xendev ) + key_len );
226  if ( ! xendev ) {
227  rc = -ENOMEM;
228  goto err_alloc;
229  }
230  snprintf ( xendev->dev.name, sizeof ( xendev->dev.name ), "%s/%s",
231  type, instance );
232  xendev->dev.desc.bus_type = BUS_TYPE_XEN;
233  INIT_LIST_HEAD ( &xendev->dev.children );
234  list_add_tail ( &xendev->dev.siblings, &parent->children );
235  xendev->dev.parent = parent;
236  xendev->xen = xen;
237  xendev->key = ( ( void * ) ( xendev + 1 ) );
238  snprintf ( xendev->key, key_len, "device/%s/%s", type, instance );
239  xendev->driver = driver;
240  xendev->dev.driver_name = driver->name;
241  DBGC ( xendev, "XENBUS %s has driver \"%s\"\n", xendev->key,
242  xendev->driver->name );
243 
244  /* Read backend key */
245  if ( ( rc = xenstore_read ( xen, &xendev->backend, xendev->key,
246  "backend", NULL ) ) != 0 ) {
247  DBGC ( xendev, "XENBUS %s could not read backend: %s\n",
248  xendev->key, strerror ( rc ) );
249  goto err_read_backend;
250  }
251 
252  /* Read backend domain ID */
253  if ( ( rc = xenstore_read_num ( xen, &xendev->backend_id, xendev->key,
254  "backend-id", NULL ) ) != 0 ) {
255  DBGC ( xendev, "XENBUS %s could not read backend-id: %s\n",
256  xendev->key, strerror ( rc ) );
257  goto err_read_backend_id;
258  }
259  DBGC ( xendev, "XENBUS %s backend=\"%s\" in domain %ld\n",
260  xendev->key, xendev->backend, xendev->backend_id );
261 
262  /* Probe driver */
263  if ( ( rc = xendev->driver->probe ( xendev ) ) != 0 ) {
264  DBGC ( xendev, "XENBUS could not probe %s: %s\n",
265  xendev->key, strerror ( rc ) );
266  goto err_probe;
267  }
268 
269  return 0;
270 
271  xendev->driver->remove ( xendev );
272  err_probe:
273  err_read_backend_id:
274  free ( xendev->backend );
275  err_read_backend:
276  list_del ( &xendev->dev.siblings );
277  free ( xendev );
278  err_alloc:
279  return rc;
280 }
281 
282 /**
283  * Remove Xen device
284  *
285  * @v xendev Xen device
286  */
287 static void xenbus_remove_device ( struct xen_device *xendev ) {
288 
289  /* Remove device */
290  xendev->driver->remove ( xendev );
291  free ( xendev->backend );
292  list_del ( &xendev->dev.siblings );
293  free ( xendev );
294 }
295 
296 /**
297  * Probe Xen devices of a given type
298  *
299  * @v xen Xen hypervisor
300  * @v parent Parent device
301  * @v type Device type
302  * @ret rc Return status code
303  */
304 static int xenbus_probe_type ( struct xen_hypervisor *xen,
305  struct device *parent, const char *type ) {
306  struct xen_driver *driver;
307  char *children;
308  char *child;
309  size_t len;
310  int rc;
311 
312  /* Look for a driver */
313  driver = xenbus_find_driver ( type );
314  if ( ! driver ) {
315  DBGC ( xen, "XENBUS has no driver for \"%s\" devices\n", type );
316  /* Not a fatal error */
317  rc = 0;
318  goto err_no_driver;
319  }
320 
321  /* Get children of this key */
322  if ( ( rc = xenstore_directory ( xen, &children, &len, "device",
323  type, NULL ) ) != 0 ) {
324  DBGC ( xen, "XENBUS could not list \"%s\" devices: %s\n",
325  type, strerror ( rc ) );
326  goto err_directory;
327  }
328 
329  /* Probe each child */
330  for ( child = children ; child < ( children + len ) ;
331  child += ( strlen ( child ) + 1 /* NUL */ ) ) {
332  if ( ( rc = xenbus_probe_device ( xen, parent, child,
333  driver ) ) != 0 )
334  goto err_probe_device;
335  }
336 
337  free ( children );
338  return 0;
339 
340  err_probe_device:
341  free ( children );
342  err_directory:
343  err_no_driver:
344  return rc;
345 }
346 
347 /**
348  * Probe Xen bus
349  *
350  * @v xen Xen hypervisor
351  * @v parent Parent device
352  * @ret rc Return status code
353  */
354 int xenbus_probe ( struct xen_hypervisor *xen, struct device *parent ) {
355  char *types;
356  char *type;
357  size_t len;
358  int rc;
359 
360  /* Get children of "device" key */
361  if ( ( rc = xenstore_directory ( xen, &types, &len, "device",
362  NULL ) ) != 0 ) {
363  DBGC ( xen, "XENBUS could not list device types: %s\n",
364  strerror ( rc ) );
365  goto err_directory;
366  }
367 
368  /* Probe each child type */
369  for ( type = types ; type < ( types + len ) ;
370  type += ( strlen ( type ) + 1 /* NUL */ ) ) {
371  if ( ( rc = xenbus_probe_type ( xen, parent, type ) ) != 0 )
372  goto err_probe_type;
373  }
374 
375  free ( types );
376  return 0;
377 
378  xenbus_remove ( xen, parent );
379  err_probe_type:
380  free ( types );
381  err_directory:
382  return rc;
383 }
384 
385 /**
386  * Remove Xen bus
387  *
388  * @v xen Xen hypervisor
389  * @v parent Parent device
390  */
392  struct device *parent ) {
393  struct xen_device *xendev;
394  struct xen_device *tmp;
395 
396  /* Remove devices */
397  list_for_each_entry_safe ( xendev, tmp, &parent->children,
398  dev.siblings ) {
399  xenbus_remove_device ( xendev );
400  }
401 }
CPU sleeping.
char * backend
Backend XenStore key.
Definition: xenbus.h:26
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define XEN_DRIVERS
Xen device driver table.
Definition: xenbus.h:55
uint8_t state
State.
Definition: eth_slow.h:47
int xenbus_backend_wait(struct xen_device *xendev, int state)
Wait for backend to reach a given state.
Definition: xenbus.c:148
Error codes.
uint32_t type
Operating system type.
Definition: ena.h:12
#define DBGC(...)
Definition: compiler.h:505
char name[40]
Name.
Definition: device.h:78
const char * type
Device type.
Definition: xenbus.h:40
iPXE timers
int xenstore_read(struct xen_hypervisor *xen, char **value,...)
Read XenStore value.
Definition: xenstore.c:371
static int xenbus_probe_type(struct xen_hypervisor *xen, struct device *parent, const char *type)
Probe Xen devices of a given type.
Definition: xenbus.c:304
int xenstore_write_num(struct xen_hypervisor *xen, unsigned long num,...)
Write XenStore numeric value.
Definition: xenstore.c:460
struct device * parent
Bus device.
Definition: device.h:88
Dynamic memory allocation.
A Xen hypervisor.
Definition: xen.h:50
int xenbus_probe(struct xen_hypervisor *xen, struct device *parent)
Probe Xen bus.
Definition: xenbus.c:354
Xen interface.
unsigned long tmp
Definition: linux_pci.h:64
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
A Xen device.
Definition: xenbus.h:18
#define ENOMEM
Not enough space.
Definition: errno.h:534
A hardware device.
Definition: device.h:76
char unsigned long const char unsigned long char ** children
Definition: xenstore.h:25
static int started
"startup() has been called" flag
Definition: init.c:37
struct xen_driver * driver
Driver.
Definition: xenbus.h:30
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
ring len
Length.
Definition: dwmac.h:231
const char * name
Name.
Definition: xenbus.h:38
const char * driver_name
Driver name.
Definition: device.h:80
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition: list.h:458
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
static struct xen_driver * xenbus_find_driver(const char *type)
Find driver for Xen device.
Definition: xenbus.c:195
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:661
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
int xenstore_directory(struct xen_hypervisor *xen, char **children, size_t *len,...)
Read XenStore directory.
Definition: xenstore.c:503
Xen device bus.
struct list_head siblings
Devices on the same bus.
Definition: device.h:84
unsigned long backend_id
Backend domain ID.
Definition: xenbus.h:28
int xenstore_read_num(struct xen_hypervisor *xen, unsigned long *num,...)
Read XenStore numeric value.
Definition: xenstore.c:390
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
#define ETIMEDOUT_STATE(state)
Definition: xenbus.c:89
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
int(* probe)(struct xen_device *xendev)
Probe device.
Definition: xenbus.h:46
unsigned int bus_type
Bus type.
Definition: device.h:24
void cpu_nap(void)
Sleep with interrupts enabled until next CPU interrupt.
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
#define XENBUS_BACKEND_TIMEOUT
Maximum time to wait for backend to reach a given state, in ticks.
Definition: xenbus.c:97
void(* remove)(struct xen_device *xendev)
Remove device.
Definition: xenbus.h:51
struct list_head children
Devices attached to this device.
Definition: device.h:86
char * key
XenStore key.
Definition: xenbus.h:24
struct device_description desc
Device description.
Definition: device.h:82
static int xenbus_probe_device(struct xen_hypervisor *xen, struct device *parent, const char *instance, struct xen_driver *driver)
Probe Xen device.
Definition: xenbus.c:214
void xenbus_remove(struct xen_hypervisor *xen __unused, struct device *parent)
Remove Xen bus.
Definition: xenbus.c:391
Device model.
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
struct device dev
Generic iPXE device.
Definition: xenbus.h:20
A Xen device driver.
Definition: xenbus.h:36
#define BUS_TYPE_XEN
Xen bus type.
Definition: device.h:64
unsigned long currticks(void)
Get current system time in ticks.
Definition: timer.c:42
int xenbus_backend_state(struct xen_device *xendev)
Get backend state.
Definition: xenbus.c:126
int xenbus_set_state(struct xen_device *xendev, int state)
Set device state.
Definition: xenbus.c:106
XenStore interface.
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22
static void xenbus_remove_device(struct xen_device *xendev)
Remove Xen device.
Definition: xenbus.c:287