summaryrefslogtreecommitdiffstats
path: root/src/include/ipxe/sanboot.h
blob: e44367cdbbee2d8bc8712dc773ae9b65939b69e3 (plain) (blame)
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
#ifndef _IPXE_SANBOOT_H
#define _IPXE_SANBOOT_H

/** @file
 *
 * iPXE sanboot API
 *
 * The sanboot API provides methods for hooking, unhooking,
 * describing, and booting from SAN devices.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <ipxe/api.h>
#include <ipxe/refcnt.h>
#include <ipxe/list.h>
#include <ipxe/uri.h>
#include <ipxe/retry.h>
#include <ipxe/process.h>
#include <ipxe/blockdev.h>
#include <ipxe/acpi.h>
#include <ipxe/uuid.h>
#include <config/sanboot.h>

/**
 * Default SAN drive number
 *
 * The drive number is an externally defined concept only in a BIOS
 * environment, where it represents the INT13 drive number (0x80 for
 * the first hard disk).  We retain it in other environments to allow
 * for a simple way for iPXE commands to refer to SAN drives.
 */
#define SAN_DEFAULT_DRIVE 0x80

/** A SAN path */
struct san_path {
	/** Containing SAN device */
	struct san_device *sandev;
	/** Path index */
	unsigned int index;
	/** SAN device URI */
	struct uri *uri;
	/** List of open/closed paths */
	struct list_head list;

	/** Underlying block device interface */
	struct interface block;
	/** Process */
	struct process process;
	/** Path status */
	int path_rc;

	/** ACPI descriptor (if applicable) */
	struct acpi_descriptor *desc;
};

/** A SAN device */
struct san_device {
	/** Reference count */
	struct refcnt refcnt;
	/** List of SAN devices */
	struct list_head list;

	/** Drive number */
	unsigned int drive;
	/** Flags */
	unsigned int flags;

	/** Command interface */
	struct interface command;
	/** Command timeout timer */
	struct retry_timer timer;
	/** Command status */
	int command_rc;

	/** Raw block device capacity */
	struct block_device_capacity capacity;
	/** Block size shift
	 *
	 * To allow for emulation of CD-ROM access, this represents
	 * the left-shift required to translate from exposed logical
	 * I/O blocks to underlying blocks.
	 */
	unsigned int blksize_shift;
	/** Drive is a CD-ROM */
	int is_cdrom;

	/** Driver private data */
	void *priv;

	/** Number of paths */
	unsigned int paths;
	/** Current active path */
	struct san_path *active;
	/** List of opened SAN paths */
	struct list_head opened;
	/** List of closed SAN paths */
	struct list_head closed;
	/** SAN paths */
	struct san_path path[0];
};

/** SAN device flags */
enum san_device_flags {
	/** Device should not be included in description tables */
	SAN_NO_DESCRIBE = 0x0001,
};

/** SAN boot configuration parameters */
struct san_boot_config {
	/** Boot filename (or NULL to use default) */
	const char *filename;
	/** Required extra filename (or NULL to ignore) */
	const char *extra;
	/** Filesystem label (or NULL to ignore volume label) */
	const char *label;
	/** UUID (or NULL to ignore UUID) */
	union uuid *uuid;
};

/**
 * Calculate static inline sanboot API function name
 *
 * @v _prefix		Subsystem prefix
 * @v _api_func		API function
 * @ret _subsys_func	Subsystem API function
 */
#define SANBOOT_INLINE( _subsys, _api_func ) \
	SINGLE_API_INLINE ( SANBOOT_PREFIX_ ## _subsys, _api_func )

/**
 * Provide a sanboot API implementation
 *
 * @v _prefix		Subsystem prefix
 * @v _api_func		API function
 * @v _func		Implementing function
 */
#define PROVIDE_SANBOOT( _subsys, _api_func, _func ) \
	PROVIDE_SINGLE_API ( SANBOOT_PREFIX_ ## _subsys, _api_func, _func )

/**
 * Provide a static inline sanboot API implementation
 *
 * @v _prefix		Subsystem prefix
 * @v _api_func		API function
 */
#define PROVIDE_SANBOOT_INLINE( _subsys, _api_func ) \
	PROVIDE_SINGLE_API_INLINE ( SANBOOT_PREFIX_ ## _subsys, _api_func )

/* Include all architecture-independent sanboot API headers */
#include <ipxe/null_sanboot.h>
#include <ipxe/dummy_sanboot.h>
#include <ipxe/efi/efi_block.h>

/* Include all architecture-dependent sanboot API headers */
#include <bits/sanboot.h>

/**
 * Hook SAN device
 *
 * @v drive		Drive number
 * @v uris		List of URIs
 * @v count		Number of URIs
 * @v flags		Flags
 * @ret drive		Drive number, or negative error
 */
int san_hook ( unsigned int drive, struct uri **uris, unsigned int count,
	       unsigned int flags );

/**
 * Unhook SAN device
 *
 * @v drive		Drive number
 */
void san_unhook ( unsigned int drive );

/**
 * Attempt to boot from a SAN device
 *
 * @v drive		Drive number
 * @v config		Boot configuration parameters
 * @ret rc		Return status code
 */
int san_boot ( unsigned int drive, struct san_boot_config *config );

/**
 * Describe SAN devices for SAN-booted operating system
 *
 * @ret rc		Return status code
 */
int san_describe ( void );

extern struct list_head san_devices;

/** Iterate over all SAN devices */
#define for_each_sandev( sandev ) \
	list_for_each_entry ( (sandev), &san_devices, list )

/** There exist some SAN devices
 *
 * @ret existence	Existence of SAN devices
 */
static inline int have_sandevs ( void ) {
	return ( ! list_empty ( &san_devices ) );
}

/**
 * Get reference to SAN device
 *
 * @v sandev		SAN device
 * @ret sandev		SAN device
 */
static inline __attribute__ (( always_inline )) struct san_device *
sandev_get ( struct san_device *sandev ) {
	ref_get ( &sandev->refcnt );
	return sandev;
}

/**
 * Drop reference to SAN device
 *
 * @v sandev		SAN device
 */
static inline __attribute__ (( always_inline )) void
sandev_put ( struct san_device *sandev ) {
	ref_put ( &sandev->refcnt );
}

/**
 * Calculate SAN device block size
 *
 * @v sandev		SAN device
 * @ret blksize		Sector size
 */
static inline size_t sandev_blksize ( struct san_device *sandev ) {
	return ( sandev->capacity.blksize << sandev->blksize_shift );
}

/**
 * Calculate SAN device capacity
 *
 * @v sandev		SAN device
 * @ret blocks		Number of blocks
 */
static inline uint64_t sandev_capacity ( struct san_device *sandev ) {
	return ( sandev->capacity.blocks >> sandev->blksize_shift );
}

/**
 * Check if SAN device needs to be reopened
 *
 * @v sandev		SAN device
 * @ret needs_reopen	SAN device needs to be reopened
 */
static inline int sandev_needs_reopen ( struct san_device *sandev ) {
	return ( sandev->active == NULL );
}

extern struct san_device * sandev_find ( unsigned int drive );
extern struct san_device * sandev_next ( unsigned int drive );
extern int sandev_reopen ( struct san_device *sandev );
extern int sandev_reset ( struct san_device *sandev );
extern int sandev_read ( struct san_device *sandev, uint64_t lba,
			 unsigned int count, userptr_t buffer );
extern int sandev_write ( struct san_device *sandev, uint64_t lba,
			  unsigned int count, userptr_t buffer );
extern struct san_device * alloc_sandev ( struct uri **uris, unsigned int count,
					  size_t priv_size );
extern int register_sandev ( struct san_device *sandev, unsigned int drive,
			     unsigned int flags );
extern void unregister_sandev ( struct san_device *sandev );
extern unsigned int san_default_drive ( void );

#endif /* _IPXE_SANBOOT_H */