summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/interface/xen/xenbus.c
blob: ffc8aba3e804836c46a0f97897d5a18aaa2e584f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdio.h>
#include <errno.h>
#include <ipxe/malloc.h>
#include <ipxe/device.h>
#include <ipxe/timer.h>
#include <ipxe/nap.h>
#include <ipxe/xen.h>
#include <ipxe/xenstore.h>
#include <ipxe/xenbus.h>

/** @file
 *
 * Xen device bus
 *
 */

/* Disambiguate the various error causes */
#define ETIMEDOUT_UNKNOWN						\
	__einfo_error ( EINFO_ETIMEDOUT_UNKNOWN )
#define EINFO_ETIMEDOUT_UNKNOWN						\
	__einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateUnknown,		\
			  "Unknown" )
#define ETIMEDOUT_INITIALISING						\
	__einfo_error ( EINFO_ETIMEDOUT_INITIALISING )
#define EINFO_ETIMEDOUT_INITIALISING					\
	__einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateInitialising,	\
			  "Initialising" )
#define ETIMEDOUT_INITWAIT						\
	__einfo_error ( EINFO_ETIMEDOUT_INITWAIT )
#define EINFO_ETIMEDOUT_INITWAIT					\
	__einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateInitWait,		\
			  "InitWait" )
#define ETIMEDOUT_INITIALISED						\
	__einfo_error ( EINFO_ETIMEDOUT_INITIALISED )
#define EINFO_ETIMEDOUT_INITIALISED					\
	__einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateInitialised,	\
			  "Initialised" )
#define ETIMEDOUT_CONNECTED						\
	__einfo_error ( EINFO_ETIMEDOUT_CONNECTED )
#define EINFO_ETIMEDOUT_CONNECTED					\
	__einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateConnected,	\
			  "Connected" )
#define ETIMEDOUT_CLOSING						\
	__einfo_error ( EINFO_ETIMEDOUT_CLOSING )
#define EINFO_ETIMEDOUT_CLOSING						\
	__einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateClosing,		\
			  "Closing" )
#define ETIMEDOUT_CLOSED						\
	__einfo_error ( EINFO_ETIMEDOUT_CLOSED )
#define EINFO_ETIMEDOUT_CLOSED						\
	__einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateClosed,		\
			  "Closed" )
#define ETIMEDOUT_RECONFIGURING						\
	__einfo_error ( EINFO_ETIMEDOUT_RECONFIGURING )
#define EINFO_ETIMEDOUT_RECONFIGURING					\
	__einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateReconfiguring,	\
			  "Reconfiguring" )
#define ETIMEDOUT_RECONFIGURED						\
	__einfo_error ( EINFO_ETIMEDOUT_RECONFIGURED )
#define EINFO_ETIMEDOUT_RECONFIGURED					\
	__einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateReconfigured,	\
			  "Reconfigured" )
#define ETIMEDOUT_STATE( state )					\
	EUNIQ ( EINFO_ETIMEDOUT, (state), ETIMEDOUT_UNKNOWN,		\
		ETIMEDOUT_INITIALISING, ETIMEDOUT_INITWAIT,		\
		ETIMEDOUT_INITIALISED, ETIMEDOUT_CONNECTED,		\
		ETIMEDOUT_CLOSING, ETIMEDOUT_CLOSED,			\
		ETIMEDOUT_RECONFIGURING, ETIMEDOUT_RECONFIGURED )

/** Maximum time to wait for backend to reach a given state, in ticks */
#define XENBUS_BACKEND_TIMEOUT ( 5 * TICKS_PER_SEC )

/**
 * Set device state
 *
 * @v xendev		Xen device
 * @v state		New state
 * @ret rc		Return status code
 */
int xenbus_set_state ( struct xen_device *xendev, int state ) {
	int rc;

	/* Attempt to set state */
	if ( ( rc = xenstore_write_num ( xendev->xen, state, xendev->key,
					 "state", NULL ) ) != 0 ) {
		DBGC ( xendev, "XENBUS %s could not set state=\"%d\": %s\n",
		       xendev->key, state, strerror ( rc ) );
		return rc;
	}

	return 0;
}

/**
 * Get backend state
 *
 * @v xendev		Xen device
 * @ret state		Backend state, or negative error
 */
int xenbus_backend_state ( struct xen_device *xendev ) {
	unsigned long state;
	int rc;

	/* Attempt to get backend state */
	if ( ( rc = xenstore_read_num ( xendev->xen, &state, xendev->backend,
					"state", NULL ) ) != 0 ) {
		DBGC ( xendev, "XENBUS %s could not read %s/state: %s\n",
		       xendev->key, xendev->backend, strerror ( rc ) );
		return rc;
	}

	return state;
}

/**
 * Wait for backend to reach a given state
 *
 * @v xendev		Xen device
 * @v state		Desired backend state
 * @ret rc		Return status code
 */
int xenbus_backend_wait ( struct xen_device *xendev, int state ) {
	unsigned long started = currticks();
	unsigned long elapsed;
	unsigned int attempts = 0;
	int current_state;
	int rc;

	/* Wait for backend to reach this state */
	do {

		/* Get current backend state */
		current_state = xenbus_backend_state ( xendev );
		if ( current_state < 0 ) {
			rc = current_state;
			return rc;
		}
		if ( current_state == state )
			return 0;

		/* Allow time for backend to react */
		cpu_nap();

		/* XenStore is a very slow interface; any fixed delay
		 * time would be dwarfed by the XenStore access time.
		 * We therefore use wall clock to time out this
		 * operation.
		 */
		elapsed = ( currticks() - started );
		attempts++;

	} while ( elapsed < XENBUS_BACKEND_TIMEOUT );

	/* Construct status code from current backend state */
	rc = -ETIMEDOUT_STATE ( current_state );
	DBGC ( xendev, "XENBUS %s timed out after %d attempts waiting for "
	       "%s/state=\"%d\": %s\n", xendev->key, attempts, xendev->backend,
	       state, strerror ( rc ) );

	return rc;
}

/**
 * Find driver for Xen device
 *
 * @v type		Device type
 * @ret driver		Driver, or NULL
 */
static struct xen_driver * xenbus_find_driver ( const char *type ) {
	struct xen_driver *xendrv;

	for_each_table_entry ( xendrv, XEN_DRIVERS ) {
		if ( strcmp ( xendrv->type, type ) == 0 )
			return xendrv;
	}
	return NULL;
}

/**
 * Probe Xen device
 *
 * @v xen		Xen hypervisor
 * @v parent		Parent device
 * @v type		Device type
 * @v instance		Device instance
 * @ret rc		Return status code
 */
static int xenbus_probe_device ( struct xen_hypervisor *xen,
				 struct device *parent, const char *type,
				 const char *instance ) {
	struct xen_device *xendev;
	size_t key_len;
	int rc;

	/* Allocate and initialise structure */
	key_len = ( 7 /* "device/" */ + strlen ( type ) + 1 /* "/" */ +
		    strlen ( instance ) + 1 /* NUL */ );
	xendev = zalloc ( sizeof ( *xendev ) + key_len );
	if ( ! xendev ) {
		rc = -ENOMEM;
		goto err_alloc;
	}
	snprintf ( xendev->dev.name, sizeof ( xendev->dev.name ), "%s/%s",
		   type, instance );
	xendev->dev.desc.bus_type = BUS_TYPE_XEN;
	INIT_LIST_HEAD ( &xendev->dev.children );
	list_add_tail ( &xendev->dev.siblings, &parent->children );
	xendev->dev.parent = parent;
	xendev->xen = xen;
	xendev->key = ( ( void * ) ( xendev + 1 ) );
	snprintf ( xendev->key, key_len, "device/%s/%s", type, instance );

	/* Read backend key */
	if ( ( rc = xenstore_read ( xen, &xendev->backend, xendev->key,
				    "backend", NULL ) ) != 0 ) {
		DBGC ( xendev, "XENBUS %s could not read backend: %s\n",
		       xendev->key, strerror ( rc ) );
		goto err_read_backend;
	}

	/* Read backend domain ID */
	if ( ( rc = xenstore_read_num ( xen, &xendev->backend_id, xendev->key,
					"backend-id", NULL ) ) != 0 ) {
		DBGC ( xendev, "XENBUS %s could not read backend-id: %s\n",
		       xendev->key, strerror ( rc ) );
		goto err_read_backend_id;
	}
	DBGC ( xendev, "XENBUS %s backend=\"%s\" in domain %ld\n",
	       xendev->key, xendev->backend, xendev->backend_id );

	/* Look for a driver */
	xendev->driver = xenbus_find_driver ( type );
	if ( ! xendev->driver ) {
		DBGC ( xendev, "XENBUS %s has no driver\n", xendev->key );
		/* Not a fatal error */
		rc = 0;
		goto err_no_driver;
	}
	xendev->dev.driver_name = xendev->driver->name;
	DBGC ( xendev, "XENBUS %s has driver \"%s\"\n", xendev->key,
	       xendev->driver->name );

	/* Probe driver */
	if ( ( rc = xendev->driver->probe ( xendev ) ) != 0 ) {
		DBGC ( xendev, "XENBUS could not probe %s: %s\n",
		       xendev->key, strerror ( rc ) );
		goto err_probe;
	}

	return 0;

	xendev->driver->remove ( xendev );
 err_probe:
 err_no_driver:
 err_read_backend_id:
	free ( xendev->backend );
 err_read_backend:
	list_del ( &xendev->dev.siblings );
	free ( xendev );
 err_alloc:
	return rc;
}

/**
 * Remove Xen device
 *
 * @v xendev		Xen device
 */
static void xenbus_remove_device ( struct xen_device *xendev ) {

	/* Remove device */
	xendev->driver->remove ( xendev );
	free ( xendev->backend );
	list_del ( &xendev->dev.siblings );
	free ( xendev );
}

/**
 * Probe Xen devices of a given type
 *
 * @v xen		Xen hypervisor
 * @v parent		Parent device
 * @v type		Device type
 * @ret rc		Return status code
 */
static int xenbus_probe_type ( struct xen_hypervisor *xen,
			       struct device *parent, const char *type ) {
	char *children;
	char *child;
	size_t len;
	int rc;

	/* Get children of this key */
	if ( ( rc = xenstore_directory ( xen, &children, &len, "device",
					 type, NULL ) ) != 0 ) {
		DBGC ( xen, "XENBUS could not list \"%s\" devices: %s\n",
		       type, strerror ( rc ) );
		goto err_directory;
	}

	/* Probe each child */
	for ( child = children ; child < ( children + len ) ;
	      child += ( strlen ( child ) + 1 /* NUL */ ) ) {
		if ( ( rc = xenbus_probe_device ( xen, parent, type,
						  child ) ) != 0 )
			goto err_probe_device;
	}

	free ( children );
	return 0;

 err_probe_device:
	free ( children );
 err_directory:
	return rc;
}

/**
 * Probe Xen bus
 *
 * @v xen		Xen hypervisor
 * @v parent		Parent device
 * @ret rc		Return status code
 */
int xenbus_probe ( struct xen_hypervisor *xen, struct device *parent ) {
	char *types;
	char *type;
	size_t len;
	int rc;

	/* Get children of "device" key */
	if ( ( rc = xenstore_directory ( xen, &types, &len, "device",
					 NULL ) ) != 0 ) {
		DBGC ( xen, "XENBUS could not list device types: %s\n",
		       strerror ( rc ) );
		goto err_directory;
	}

	/* Probe each child type */
	for ( type = types ; type < ( types + len ) ;
	      type += ( strlen ( type ) + 1 /* NUL */ ) ) {
		if ( ( rc = xenbus_probe_type ( xen, parent, type ) ) != 0 )
			goto err_probe_type;
	}

	free ( types );
	return 0;

	xenbus_remove ( xen, parent );
 err_probe_type:
	free ( types );
 err_directory:
	return rc;
}

/**
 * Remove Xen bus
 *
 * @v xen		Xen hypervisor
 * @v parent		Parent device
 */
void xenbus_remove ( struct xen_hypervisor *xen __unused,
		     struct device *parent ) {
	struct xen_device *xendev;
	struct xen_device *tmp;

	/* Remove devices */
	list_for_each_entry_safe ( xendev, tmp, &parent->children,
				   dev.siblings ) {
		xenbus_remove_device ( xendev );
	}
}