]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
1a73661b SG |
2 | /* |
3 | * (C) Copyright 2000-2004 | |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
1a73661b SG |
5 | */ |
6 | ||
7 | #ifndef BLK_H | |
8 | #define BLK_H | |
9 | ||
ff98cb90 PJ |
10 | #include <efi.h> |
11 | ||
1a73661b SG |
12 | #ifdef CONFIG_SYS_64BIT_LBA |
13 | typedef uint64_t lbaint_t; | |
14 | #define LBAFlength "ll" | |
15 | #else | |
16 | typedef ulong lbaint_t; | |
17 | #define LBAFlength "l" | |
18 | #endif | |
19 | #define LBAF "%" LBAFlength "x" | |
20 | #define LBAFU "%" LBAFlength "u" | |
21 | ||
96f37b09 SG |
22 | struct udevice; |
23 | ||
a51eb8de SG |
24 | static inline bool blk_enabled(void) |
25 | { | |
26 | return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_HAVE_BLOCK_DEVICE); | |
27 | } | |
28 | ||
1a73661b | 29 | /* Interface types: */ |
5ec4f1a5 SG |
30 | enum if_type { |
31 | IF_TYPE_UNKNOWN = 0, | |
32 | IF_TYPE_IDE, | |
33 | IF_TYPE_SCSI, | |
34 | IF_TYPE_ATAPI, | |
35 | IF_TYPE_USB, | |
36 | IF_TYPE_DOC, | |
37 | IF_TYPE_MMC, | |
38 | IF_TYPE_SD, | |
39 | IF_TYPE_SATA, | |
40 | IF_TYPE_HOST, | |
ffab6945 | 41 | IF_TYPE_NVME, |
2abd8d1c | 42 | IF_TYPE_EFI_LOADER, |
722bc5b5 | 43 | IF_TYPE_PVBLOCK, |
4ad54ec4 | 44 | IF_TYPE_VIRTIO, |
42b7f421 | 45 | IF_TYPE_EFI_MEDIA, |
5ec4f1a5 SG |
46 | |
47 | IF_TYPE_COUNT, /* Number of interface types */ | |
48 | }; | |
1a73661b | 49 | |
eb81b1a4 BM |
50 | #define BLK_VEN_SIZE 40 |
51 | #define BLK_PRD_SIZE 20 | |
52 | #define BLK_REV_SIZE 8 | |
53 | ||
ce3dbc5d MK |
54 | #define PART_FORMAT_PCAT 0x1 |
55 | #define PART_FORMAT_GPT 0x2 | |
56 | ||
ff98cb90 PJ |
57 | /* |
58 | * Identifies the partition table type (ie. MBR vs GPT GUID) signature | |
59 | */ | |
60 | enum sig_type { | |
61 | SIG_TYPE_NONE, | |
62 | SIG_TYPE_MBR, | |
63 | SIG_TYPE_GUID, | |
64 | ||
65 | SIG_TYPE_COUNT /* Number of signature types */ | |
66 | }; | |
67 | ||
09d71aac SG |
68 | /* |
69 | * With driver model (CONFIG_BLK) this is uclass platform data, accessible | |
caa4daa2 | 70 | * with dev_get_uclass_plat(dev) |
09d71aac | 71 | */ |
1a73661b | 72 | struct blk_desc { |
09d71aac SG |
73 | /* |
74 | * TODO: With driver model we should be able to use the parent | |
75 | * device's uclass instead. | |
76 | */ | |
5ec4f1a5 | 77 | enum if_type if_type; /* type of the interface */ |
bcce53d0 | 78 | int devnum; /* device number */ |
1a73661b SG |
79 | unsigned char part_type; /* partition type */ |
80 | unsigned char target; /* target SCSI ID */ | |
81 | unsigned char lun; /* target LUN */ | |
82 | unsigned char hwpart; /* HW partition, e.g. for eMMC */ | |
83 | unsigned char type; /* device type */ | |
84 | unsigned char removable; /* removable device */ | |
85 | #ifdef CONFIG_LBA48 | |
86 | /* device can use 48bit addr (ATA/ATAPI v7) */ | |
87 | unsigned char lba48; | |
88 | #endif | |
89 | lbaint_t lba; /* number of blocks */ | |
90 | unsigned long blksz; /* block size */ | |
91 | int log2blksz; /* for convenience: log2(blksz) */ | |
eb81b1a4 BM |
92 | char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */ |
93 | char product[BLK_PRD_SIZE + 1]; /* device product number */ | |
94 | char revision[BLK_REV_SIZE + 1]; /* firmware revision */ | |
ff98cb90 PJ |
95 | enum sig_type sig_type; /* Partition table signature type */ |
96 | union { | |
97 | uint32_t mbr_sig; /* MBR integer signature */ | |
98 | efi_guid_t guid_sig; /* GPT GUID Signature */ | |
99 | }; | |
c4d660d4 | 100 | #if CONFIG_IS_ENABLED(BLK) |
b6694a33 SG |
101 | /* |
102 | * For now we have a few functions which take struct blk_desc as a | |
103 | * parameter. This field allows them to look up the associated | |
104 | * device. Once these functions are removed we can drop this field. | |
105 | */ | |
09d71aac SG |
106 | struct udevice *bdev; |
107 | #else | |
1a73661b SG |
108 | unsigned long (*block_read)(struct blk_desc *block_dev, |
109 | lbaint_t start, | |
110 | lbaint_t blkcnt, | |
111 | void *buffer); | |
112 | unsigned long (*block_write)(struct blk_desc *block_dev, | |
113 | lbaint_t start, | |
114 | lbaint_t blkcnt, | |
115 | const void *buffer); | |
116 | unsigned long (*block_erase)(struct blk_desc *block_dev, | |
117 | lbaint_t start, | |
118 | lbaint_t blkcnt); | |
119 | void *priv; /* driver private struct pointer */ | |
09d71aac | 120 | #endif |
1a73661b SG |
121 | }; |
122 | ||
123 | #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz)) | |
124 | #define PAD_TO_BLOCKSIZE(size, blk_desc) \ | |
125 | (PAD_SIZE(size, blk_desc->blksz)) | |
126 | ||
6fef62cc | 127 | #if CONFIG_IS_ENABLED(BLOCK_CACHE) |
1526bcce AD |
128 | |
129 | /** | |
130 | * blkcache_init() - initialize the block cache list pointers | |
131 | */ | |
132 | int blkcache_init(void); | |
133 | ||
e40cf34a EN |
134 | /** |
135 | * blkcache_read() - attempt to read a set of blocks from cache | |
136 | * | |
137 | * @param iftype - IF_TYPE_x for type of device | |
138 | * @param dev - device index of particular type | |
139 | * @param start - starting block number | |
140 | * @param blkcnt - number of blocks to read | |
141 | * @param blksz - size in bytes of each block | |
142 | * @param buf - buffer to contain cached data | |
143 | * | |
185f812c | 144 | * Return: - 1 if block returned from cache, 0 otherwise. |
e40cf34a | 145 | */ |
c8e4d2a8 EN |
146 | int blkcache_read(int iftype, int dev, |
147 | lbaint_t start, lbaint_t blkcnt, | |
148 | unsigned long blksz, void *buffer); | |
e40cf34a EN |
149 | |
150 | /** | |
151 | * blkcache_fill() - make data read from a block device available | |
152 | * to the block cache | |
153 | * | |
154 | * @param iftype - IF_TYPE_x for type of device | |
155 | * @param dev - device index of particular type | |
156 | * @param start - starting block number | |
157 | * @param blkcnt - number of blocks available | |
158 | * @param blksz - size in bytes of each block | |
159 | * @param buf - buffer containing data to cache | |
160 | * | |
161 | */ | |
c8e4d2a8 EN |
162 | void blkcache_fill(int iftype, int dev, |
163 | lbaint_t start, lbaint_t blkcnt, | |
164 | unsigned long blksz, void const *buffer); | |
e40cf34a EN |
165 | |
166 | /** | |
167 | * blkcache_invalidate() - discard the cache for a set of blocks | |
168 | * because of a write or device (re)initialization. | |
169 | * | |
170 | * @param iftype - IF_TYPE_x for type of device | |
171 | * @param dev - device index of particular type | |
172 | */ | |
c8e4d2a8 | 173 | void blkcache_invalidate(int iftype, int dev); |
e40cf34a EN |
174 | |
175 | /** | |
176 | * blkcache_configure() - configure block cache | |
177 | * | |
178 | * @param blocks - maximum blocks per entry | |
179 | * @param entries - maximum entries in cache | |
180 | */ | |
181 | void blkcache_configure(unsigned blocks, unsigned entries); | |
182 | ||
183 | /* | |
184 | * statistics of the block cache | |
185 | */ | |
186 | struct block_cache_stats { | |
187 | unsigned hits; | |
188 | unsigned misses; | |
189 | unsigned entries; /* current entry count */ | |
190 | unsigned max_blocks_per_entry; | |
191 | unsigned max_entries; | |
192 | }; | |
193 | ||
194 | /** | |
195 | * get_blkcache_stats() - return statistics and reset | |
196 | * | |
197 | * @param stats - statistics are copied here | |
198 | */ | |
199 | void blkcache_stats(struct block_cache_stats *stats); | |
200 | ||
201 | #else | |
202 | ||
c8e4d2a8 EN |
203 | static inline int blkcache_read(int iftype, int dev, |
204 | lbaint_t start, lbaint_t blkcnt, | |
205 | unsigned long blksz, void *buffer) | |
e40cf34a EN |
206 | { |
207 | return 0; | |
208 | } | |
209 | ||
c8e4d2a8 EN |
210 | static inline void blkcache_fill(int iftype, int dev, |
211 | lbaint_t start, lbaint_t blkcnt, | |
212 | unsigned long blksz, void const *buffer) {} | |
e40cf34a | 213 | |
c8e4d2a8 | 214 | static inline void blkcache_invalidate(int iftype, int dev) {} |
e40cf34a EN |
215 | |
216 | #endif | |
217 | ||
c4d660d4 | 218 | #if CONFIG_IS_ENABLED(BLK) |
09d71aac SG |
219 | struct udevice; |
220 | ||
221 | /* Operations on block devices */ | |
222 | struct blk_ops { | |
223 | /** | |
224 | * read() - read from a block device | |
225 | * | |
226 | * @dev: Device to read from | |
227 | * @start: Start block number to read (0=first) | |
228 | * @blkcnt: Number of blocks to read | |
229 | * @buffer: Destination buffer for data read | |
230 | * @return number of blocks read, or -ve error number (see the | |
231 | * IS_ERR_VALUE() macro | |
232 | */ | |
233 | unsigned long (*read)(struct udevice *dev, lbaint_t start, | |
234 | lbaint_t blkcnt, void *buffer); | |
235 | ||
236 | /** | |
237 | * write() - write to a block device | |
238 | * | |
239 | * @dev: Device to write to | |
240 | * @start: Start block number to write (0=first) | |
241 | * @blkcnt: Number of blocks to write | |
242 | * @buffer: Source buffer for data to write | |
243 | * @return number of blocks written, or -ve error number (see the | |
244 | * IS_ERR_VALUE() macro | |
245 | */ | |
246 | unsigned long (*write)(struct udevice *dev, lbaint_t start, | |
247 | lbaint_t blkcnt, const void *buffer); | |
248 | ||
249 | /** | |
250 | * erase() - erase a section of a block device | |
251 | * | |
252 | * @dev: Device to (partially) erase | |
253 | * @start: Start block number to erase (0=first) | |
254 | * @blkcnt: Number of blocks to erase | |
255 | * @return number of blocks erased, or -ve error number (see the | |
256 | * IS_ERR_VALUE() macro | |
257 | */ | |
258 | unsigned long (*erase)(struct udevice *dev, lbaint_t start, | |
259 | lbaint_t blkcnt); | |
cd0fb55b SG |
260 | |
261 | /** | |
262 | * select_hwpart() - select a particular hardware partition | |
263 | * | |
264 | * Some devices (e.g. MMC) can support partitioning at the hardware | |
265 | * level. This is quite separate from the normal idea of | |
266 | * software-based partitions. MMC hardware partitions must be | |
267 | * explicitly selected. Once selected only the region of the device | |
268 | * covered by that partition is accessible. | |
269 | * | |
270 | * The MMC standard provides for two boot partitions (numbered 1 and 2), | |
271 | * rpmb (3), and up to 4 addition general-purpose partitions (4-7). | |
272 | * | |
273 | * @desc: Block device to update | |
274 | * @hwpart: Hardware partition number to select. 0 means the raw | |
275 | * device, 1 is the first partition, 2 is the second, etc. | |
276 | * @return 0 if OK, -ve on error | |
277 | */ | |
278 | int (*select_hwpart)(struct udevice *dev, int hwpart); | |
09d71aac SG |
279 | }; |
280 | ||
281 | #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops) | |
282 | ||
283 | /* | |
284 | * These functions should take struct udevice instead of struct blk_desc, | |
285 | * but this is convenient for migration to driver model. Add a 'd' prefix | |
286 | * to the function operations, so that blk_read(), etc. can be reserved for | |
287 | * functions with the correct arguments. | |
288 | */ | |
289 | unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start, | |
290 | lbaint_t blkcnt, void *buffer); | |
291 | unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start, | |
292 | lbaint_t blkcnt, const void *buffer); | |
293 | unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start, | |
294 | lbaint_t blkcnt); | |
295 | ||
6139281a SG |
296 | /** |
297 | * blk_find_device() - Find a block device | |
298 | * | |
299 | * This function does not activate the device. The device will be returned | |
300 | * whether or not it is activated. | |
301 | * | |
302 | * @if_type: Interface type (enum if_type_t) | |
303 | * @devnum: Device number (specific to each interface type) | |
304 | * @devp: the device, if found | |
185f812c | 305 | * Return: 0 if found, -ENODEV if no device found, or other -ve error value |
6139281a SG |
306 | */ |
307 | int blk_find_device(int if_type, int devnum, struct udevice **devp); | |
308 | ||
09d71aac SG |
309 | /** |
310 | * blk_get_device() - Find and probe a block device ready for use | |
311 | * | |
312 | * @if_type: Interface type (enum if_type_t) | |
313 | * @devnum: Device number (specific to each interface type) | |
314 | * @devp: the device, if found | |
185f812c | 315 | * Return: 0 if found, -ENODEV if no device found, or other -ve error value |
09d71aac SG |
316 | */ |
317 | int blk_get_device(int if_type, int devnum, struct udevice **devp); | |
318 | ||
319 | /** | |
320 | * blk_first_device() - Find the first device for a given interface | |
321 | * | |
322 | * The device is probed ready for use | |
323 | * | |
324 | * @devnum: Device number (specific to each interface type) | |
325 | * @devp: the device, if found | |
185f812c | 326 | * Return: 0 if found, -ENODEV if no device, or other -ve error value |
09d71aac SG |
327 | */ |
328 | int blk_first_device(int if_type, struct udevice **devp); | |
329 | ||
330 | /** | |
331 | * blk_next_device() - Find the next device for a given interface | |
332 | * | |
333 | * This can be called repeatedly after blk_first_device() to iterate through | |
334 | * all devices of the given interface type. | |
335 | * | |
336 | * The device is probed ready for use | |
337 | * | |
338 | * @devp: On entry, the previous device returned. On exit, the next | |
339 | * device, if found | |
185f812c | 340 | * Return: 0 if found, -ENODEV if no device, or other -ve error value |
09d71aac SG |
341 | */ |
342 | int blk_next_device(struct udevice **devp); | |
343 | ||
344 | /** | |
345 | * blk_create_device() - Create a new block device | |
346 | * | |
347 | * @parent: Parent of the new device | |
348 | * @drv_name: Driver name to use for the block device | |
349 | * @name: Name for the device | |
350 | * @if_type: Interface type (enum if_type_t) | |
52138fd4 SG |
351 | * @devnum: Device number, specific to the interface type, or -1 to |
352 | * allocate the next available number | |
09d71aac | 353 | * @blksz: Block size of the device in bytes (typically 512) |
5fe7702e | 354 | * @lba: Total number of blocks of the device |
09d71aac SG |
355 | * @devp: the new device (which has not been probed) |
356 | */ | |
357 | int blk_create_device(struct udevice *parent, const char *drv_name, | |
358 | const char *name, int if_type, int devnum, int blksz, | |
5fe7702e | 359 | lbaint_t lba, struct udevice **devp); |
09d71aac | 360 | |
9107c973 SG |
361 | /** |
362 | * blk_create_devicef() - Create a new named block device | |
363 | * | |
364 | * @parent: Parent of the new device | |
365 | * @drv_name: Driver name to use for the block device | |
366 | * @name: Name for the device (parent name is prepended) | |
367 | * @if_type: Interface type (enum if_type_t) | |
368 | * @devnum: Device number, specific to the interface type, or -1 to | |
369 | * allocate the next available number | |
370 | * @blksz: Block size of the device in bytes (typically 512) | |
5fe7702e | 371 | * @lba: Total number of blocks of the device |
9107c973 SG |
372 | * @devp: the new device (which has not been probed) |
373 | */ | |
374 | int blk_create_devicef(struct udevice *parent, const char *drv_name, | |
375 | const char *name, int if_type, int devnum, int blksz, | |
5fe7702e | 376 | lbaint_t lba, struct udevice **devp); |
9107c973 | 377 | |
19b241c6 AT |
378 | /** |
379 | * blk_probe_or_unbind() - Try to probe | |
380 | * | |
381 | * Try to probe the device, primarily for enumerating partitions. | |
382 | * If it fails, the device itself is unbound since it means that it won't | |
383 | * work any more. | |
384 | * | |
385 | * @dev: The device to probe | |
386 | * Return: 0 if OK, -ve on error | |
387 | */ | |
388 | int blk_probe_or_unbind(struct udevice *dev); | |
389 | ||
09d71aac SG |
390 | /** |
391 | * blk_unbind_all() - Unbind all device of the given interface type | |
392 | * | |
393 | * The devices are removed and then unbound. | |
394 | * | |
395 | * @if_type: Interface type to unbind | |
185f812c | 396 | * Return: 0 if OK, -ve on error |
09d71aac SG |
397 | */ |
398 | int blk_unbind_all(int if_type); | |
399 | ||
52138fd4 SG |
400 | /** |
401 | * blk_find_max_devnum() - find the maximum device number for an interface type | |
402 | * | |
403 | * Finds the last allocated device number for an interface type @if_type. The | |
404 | * next number is safe to use for a newly allocated device. | |
405 | * | |
406 | * @if_type: Interface type to scan | |
185f812c | 407 | * Return: maximum device number found, or -ENODEV if none, or other -ve on |
52138fd4 SG |
408 | * error |
409 | */ | |
410 | int blk_find_max_devnum(enum if_type if_type); | |
411 | ||
c879eeb7 BM |
412 | /** |
413 | * blk_next_free_devnum() - get the next device number for an interface type | |
414 | * | |
415 | * Finds the next number that is safe to use for a newly allocated device for | |
416 | * an interface type @if_type. | |
417 | * | |
418 | * @if_type: Interface type to scan | |
185f812c | 419 | * Return: next device number safe to use, or -ve on error |
c879eeb7 BM |
420 | */ |
421 | int blk_next_free_devnum(enum if_type if_type); | |
422 | ||
cd0fb55b SG |
423 | /** |
424 | * blk_select_hwpart() - select a hardware partition | |
425 | * | |
426 | * Select a hardware partition if the device supports it (typically MMC does) | |
427 | * | |
428 | * @dev: Device to update | |
429 | * @hwpart: Partition number to select | |
185f812c | 430 | * Return: 0 if OK, -ve on error |
cd0fb55b SG |
431 | */ |
432 | int blk_select_hwpart(struct udevice *dev, int hwpart); | |
433 | ||
4bdb49a7 TR |
434 | /** |
435 | * blk_get_from_parent() - obtain a block device by looking up its parent | |
436 | * | |
437 | * All devices with | |
438 | */ | |
439 | int blk_get_from_parent(struct udevice *parent, struct udevice **devp); | |
440 | ||
87571b7f SG |
441 | /** |
442 | * blk_get_devtype() - Get the device type of a block device | |
443 | * | |
444 | * @dev: Block device to check | |
445 | * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc" | |
446 | */ | |
447 | const char *blk_get_devtype(struct udevice *dev); | |
448 | ||
bc53d263 TFC |
449 | /** |
450 | * blk_get_by_device() - Get the block device descriptor for the given device | |
451 | * @dev: Instance of a storage device | |
452 | * | |
453 | * Return: With block device descriptor on success , NULL if there is no such | |
454 | * block device. | |
455 | */ | |
456 | struct blk_desc *blk_get_by_device(struct udevice *dev); | |
457 | ||
09d71aac SG |
458 | #else |
459 | #include <errno.h> | |
2a981dc2 SG |
460 | /* |
461 | * These functions should take struct udevice instead of struct blk_desc, | |
462 | * but this is convenient for migration to driver model. Add a 'd' prefix | |
463 | * to the function operations, so that blk_read(), etc. can be reserved for | |
464 | * functions with the correct arguments. | |
465 | */ | |
466 | static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start, | |
467 | lbaint_t blkcnt, void *buffer) | |
468 | { | |
e40cf34a EN |
469 | ulong blks_read; |
470 | if (blkcache_read(block_dev->if_type, block_dev->devnum, | |
471 | start, blkcnt, block_dev->blksz, buffer)) | |
472 | return blkcnt; | |
473 | ||
2a981dc2 SG |
474 | /* |
475 | * We could check if block_read is NULL and return -ENOSYS. But this | |
476 | * bloats the code slightly (cause some board to fail to build), and | |
477 | * it would be an error to try an operation that does not exist. | |
478 | */ | |
e40cf34a EN |
479 | blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer); |
480 | if (blks_read == blkcnt) | |
481 | blkcache_fill(block_dev->if_type, block_dev->devnum, | |
482 | start, blkcnt, block_dev->blksz, buffer); | |
483 | ||
484 | return blks_read; | |
2a981dc2 SG |
485 | } |
486 | ||
487 | static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start, | |
488 | lbaint_t blkcnt, const void *buffer) | |
489 | { | |
e40cf34a | 490 | blkcache_invalidate(block_dev->if_type, block_dev->devnum); |
2a981dc2 SG |
491 | return block_dev->block_write(block_dev, start, blkcnt, buffer); |
492 | } | |
493 | ||
494 | static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start, | |
495 | lbaint_t blkcnt) | |
496 | { | |
e40cf34a | 497 | blkcache_invalidate(block_dev->if_type, block_dev->devnum); |
2a981dc2 SG |
498 | return block_dev->block_erase(block_dev, start, blkcnt); |
499 | } | |
6eef6eac SG |
500 | |
501 | /** | |
502 | * struct blk_driver - Driver for block interface types | |
503 | * | |
504 | * This provides access to the block devices for each interface type. One | |
505 | * driver should be provided using U_BOOT_LEGACY_BLK() for each interface | |
506 | * type that is to be supported. | |
507 | * | |
508 | * @if_typename: Interface type name | |
509 | * @if_type: Interface type | |
510 | * @max_devs: Maximum number of devices supported | |
511 | * @desc: Pointer to list of devices for this interface type, | |
512 | * or NULL to use @get_dev() instead | |
513 | */ | |
514 | struct blk_driver { | |
515 | const char *if_typename; | |
516 | enum if_type if_type; | |
517 | int max_devs; | |
518 | struct blk_desc *desc; | |
519 | /** | |
520 | * get_dev() - get a pointer to a block device given its number | |
521 | * | |
522 | * Each interface allocates its own devices and typically | |
523 | * struct blk_desc is contained with the interface's data structure. | |
524 | * There is no global numbering for block devices. This method allows | |
525 | * the device for an interface type to be obtained when @desc is NULL. | |
526 | * | |
527 | * @devnum: Device number (0 for first device on that interface, | |
528 | * 1 for second, etc. | |
529 | * @descp: Returns pointer to the block device on success | |
530 | * @return 0 if OK, -ve on error | |
531 | */ | |
532 | int (*get_dev)(int devnum, struct blk_desc **descp); | |
533 | ||
534 | /** | |
535 | * select_hwpart() - Select a hardware partition | |
536 | * | |
537 | * Some devices (e.g. MMC) can support partitioning at the hardware | |
538 | * level. This is quite separate from the normal idea of | |
539 | * software-based partitions. MMC hardware partitions must be | |
540 | * explicitly selected. Once selected only the region of the device | |
541 | * covered by that partition is accessible. | |
542 | * | |
543 | * The MMC standard provides for two boot partitions (numbered 1 and 2), | |
544 | * rpmb (3), and up to 4 addition general-purpose partitions (4-7). | |
545 | * Partition 0 is the main user-data partition. | |
546 | * | |
547 | * @desc: Block device descriptor | |
548 | * @hwpart: Hardware partition number to select. 0 means the main | |
549 | * user-data partition, 1 is the first partition, 2 is | |
550 | * the second, etc. | |
551 | * @return 0 if OK, other value for an error | |
552 | */ | |
553 | int (*select_hwpart)(struct blk_desc *desc, int hwpart); | |
554 | }; | |
555 | ||
556 | /* | |
557 | * Declare a new U-Boot legacy block driver. New drivers should use driver | |
558 | * model (UCLASS_BLK). | |
559 | */ | |
560 | #define U_BOOT_LEGACY_BLK(__name) \ | |
561 | ll_entry_declare(struct blk_driver, __name, blk_driver) | |
562 | ||
563 | struct blk_driver *blk_driver_lookup_type(int if_type); | |
564 | ||
09d71aac | 565 | #endif /* !CONFIG_BLK */ |
2a981dc2 | 566 | |
6eef6eac SG |
567 | /** |
568 | * blk_get_devnum_by_typename() - Get a block device by type and number | |
569 | * | |
570 | * This looks through the available block devices of the given type, returning | |
571 | * the one with the given @devnum. | |
572 | * | |
573 | * @if_type: Block device type | |
574 | * @devnum: Device number | |
185f812c | 575 | * Return: point to block device descriptor, or NULL if not found |
6eef6eac SG |
576 | */ |
577 | struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum); | |
578 | ||
579 | /** | |
580 | * blk_get_devnum_by_type() - Get a block device by type name, and number | |
581 | * | |
582 | * This looks up the block device type based on @if_typename, then calls | |
583 | * blk_get_devnum_by_type(). | |
584 | * | |
585 | * @if_typename: Block device type name | |
586 | * @devnum: Device number | |
185f812c | 587 | * Return: point to block device descriptor, or NULL if not found |
6eef6eac SG |
588 | */ |
589 | struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, | |
590 | int devnum); | |
591 | ||
592 | /** | |
593 | * blk_dselect_hwpart() - select a hardware partition | |
594 | * | |
595 | * This selects a hardware partition (such as is supported by MMC). The block | |
596 | * device size may change as this effectively points the block device to a | |
597 | * partition at the hardware level. See the select_hwpart() method above. | |
598 | * | |
599 | * @desc: Block device descriptor for the device to select | |
600 | * @hwpart: Partition number to select | |
185f812c | 601 | * Return: 0 if OK, -ve on error |
6eef6eac SG |
602 | */ |
603 | int blk_dselect_hwpart(struct blk_desc *desc, int hwpart); | |
604 | ||
605 | /** | |
606 | * blk_list_part() - list the partitions for block devices of a given type | |
607 | * | |
608 | * This looks up the partition type for each block device of type @if_type, | |
609 | * then displays a list of partitions. | |
610 | * | |
611 | * @if_type: Block device type | |
185f812c | 612 | * Return: 0 if OK, -ENODEV if there is none of that type |
6eef6eac SG |
613 | */ |
614 | int blk_list_part(enum if_type if_type); | |
615 | ||
616 | /** | |
617 | * blk_list_devices() - list the block devices of a given type | |
618 | * | |
619 | * This lists each block device of the type @if_type, showing the capacity | |
620 | * as well as type-specific information. | |
621 | * | |
622 | * @if_type: Block device type | |
623 | */ | |
624 | void blk_list_devices(enum if_type if_type); | |
625 | ||
626 | /** | |
627 | * blk_show_device() - show information about a given block device | |
628 | * | |
629 | * This shows the block device capacity as well as type-specific information. | |
630 | * | |
631 | * @if_type: Block device type | |
632 | * @devnum: Device number | |
185f812c | 633 | * Return: 0 if OK, -ENODEV for invalid device number |
6eef6eac SG |
634 | */ |
635 | int blk_show_device(enum if_type if_type, int devnum); | |
636 | ||
637 | /** | |
638 | * blk_print_device_num() - show information about a given block device | |
639 | * | |
640 | * This is similar to blk_show_device() but returns an error if the block | |
641 | * device type is unknown. | |
642 | * | |
643 | * @if_type: Block device type | |
644 | * @devnum: Device number | |
185f812c | 645 | * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block |
6eef6eac SG |
646 | * device is not connected |
647 | */ | |
648 | int blk_print_device_num(enum if_type if_type, int devnum); | |
649 | ||
650 | /** | |
651 | * blk_print_part_devnum() - print the partition information for a device | |
652 | * | |
653 | * @if_type: Block device type | |
654 | * @devnum: Device number | |
185f812c | 655 | * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if |
6eef6eac SG |
656 | * the interface type is not supported, other -ve on other error |
657 | */ | |
658 | int blk_print_part_devnum(enum if_type if_type, int devnum); | |
659 | ||
660 | /** | |
661 | * blk_read_devnum() - read blocks from a device | |
662 | * | |
663 | * @if_type: Block device type | |
664 | * @devnum: Device number | |
665 | * @blkcnt: Number of blocks to read | |
666 | * @buffer: Address to write data to | |
185f812c | 667 | * Return: number of blocks read, or -ve error number on error |
6eef6eac SG |
668 | */ |
669 | ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start, | |
670 | lbaint_t blkcnt, void *buffer); | |
671 | ||
672 | /** | |
673 | * blk_write_devnum() - write blocks to a device | |
674 | * | |
675 | * @if_type: Block device type | |
676 | * @devnum: Device number | |
677 | * @blkcnt: Number of blocks to write | |
678 | * @buffer: Address to read data from | |
185f812c | 679 | * Return: number of blocks written, or -ve error number on error |
6eef6eac SG |
680 | */ |
681 | ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start, | |
682 | lbaint_t blkcnt, const void *buffer); | |
683 | ||
684 | /** | |
685 | * blk_select_hwpart_devnum() - select a hardware partition | |
686 | * | |
687 | * This is similar to blk_dselect_hwpart() but it looks up the interface and | |
688 | * device number. | |
689 | * | |
690 | * @if_type: Block device type | |
691 | * @devnum: Device number | |
692 | * @hwpart: Partition number to select | |
185f812c | 693 | * Return: 0 if OK, -ve on error |
6eef6eac SG |
694 | */ |
695 | int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart); | |
696 | ||
6faa4ed7 SG |
697 | /** |
698 | * blk_get_if_type_name() - Get the name of an interface type | |
699 | * | |
700 | * @if_type: Interface type to check | |
185f812c | 701 | * Return: name of interface, or NULL if none |
6faa4ed7 SG |
702 | */ |
703 | const char *blk_get_if_type_name(enum if_type if_type); | |
704 | ||
4395f667 SG |
705 | /** |
706 | * blk_common_cmd() - handle common commands with block devices | |
707 | * | |
708 | * @args: Number of arguments to the command (argv[0] is the command itself) | |
709 | * @argv: Command arguments | |
710 | * @if_type: Interface type | |
711 | * @cur_devnump: Current device number for this interface type | |
185f812c | 712 | * Return: 0 if OK, CMD_RET_ERROR on error |
4395f667 | 713 | */ |
09140113 | 714 | int blk_common_cmd(int argc, char *const argv[], enum if_type if_type, |
4395f667 SG |
715 | int *cur_devnump); |
716 | ||
96f37b09 SG |
717 | enum blk_flag_t { |
718 | BLKF_FIXED = 1 << 0, | |
719 | BLKF_REMOVABLE = 1 << 1, | |
720 | BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE, | |
721 | }; | |
722 | ||
723 | /** | |
724 | * blk_first_device_err() - Get the first block device | |
725 | * | |
726 | * The device returned is probed if necessary, and ready for use | |
727 | * | |
728 | * @flags: Indicates type of device to return | |
729 | * @devp: Returns pointer to the first device in that uclass, or NULL if none | |
185f812c | 730 | * Return: 0 if found, -ENODEV if not found, other -ve on error |
96f37b09 SG |
731 | */ |
732 | int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp); | |
733 | ||
734 | /** | |
735 | * blk_next_device_err() - Get the next block device | |
736 | * | |
737 | * The device returned is probed if necessary, and ready for use | |
738 | * | |
739 | * @flags: Indicates type of device to return | |
740 | * @devp: On entry, pointer to device to lookup. On exit, returns pointer | |
741 | * to the next device in the uclass if no error occurred, or -ENODEV if | |
742 | * there is no next device. | |
185f812c | 743 | * Return: 0 if found, -ENODEV if not found, other -ve on error |
96f37b09 SG |
744 | */ |
745 | int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp); | |
746 | ||
49e86681 SG |
747 | /** |
748 | * blk_find_first() - Return the first matching block device | |
749 | * @flags: Indicates type of device to return | |
750 | * @devp: Returns pointer to device, or NULL on error | |
751 | * | |
752 | * The device is not prepared for use - this is an internal function. | |
753 | * The function uclass_get_device_tail() can be used to probe the device. | |
754 | * | |
755 | * Note that some devices are considered removable until they have been probed | |
756 | * | |
757 | * @return 0 if found, -ENODEV if not found | |
758 | */ | |
759 | int blk_find_first(enum blk_flag_t flags, struct udevice **devp); | |
760 | ||
761 | /** | |
762 | * blk_find_next() - Return the next matching block device | |
763 | * @flags: Indicates type of device to return | |
764 | * @devp: On entry, pointer to device to lookup. On exit, returns pointer | |
765 | * to the next device in the same uclass, or NULL if none | |
766 | * | |
767 | * The device is not prepared for use - this is an internal function. | |
768 | * The function uclass_get_device_tail() can be used to probe the device. | |
769 | * | |
770 | * Note that some devices are considered removable until they have been probed | |
771 | * | |
772 | * @return 0 if found, -ENODEV if not found | |
773 | */ | |
774 | int blk_find_next(enum blk_flag_t flags, struct udevice **devp); | |
775 | ||
776 | /** | |
777 | * blk_foreach() - iterate through block devices | |
778 | * | |
779 | * This creates a for() loop which works through the available block devices in | |
780 | * order from start to end. | |
781 | * | |
782 | * If for some reason the uclass cannot be found, this does nothing. | |
783 | * | |
784 | * @_flags: Indicates type of device to return | |
785 | * @_pos: struct udevice * to hold the current device. Set to NULL when there | |
786 | * are no more devices. | |
787 | */ | |
788 | #define blk_foreach(_flags, _pos) \ | |
789 | for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \ | |
790 | _ret = blk_find_next(_flags, &_pos)) | |
791 | ||
96f37b09 SG |
792 | /** |
793 | * blk_foreach_probe() - Helper function to iteration through block devices | |
794 | * | |
795 | * This creates a for() loop which works through the available devices in | |
796 | * a uclass in order from start to end. Devices are probed if necessary, | |
797 | * and ready for use. | |
798 | * | |
799 | * @flags: Indicates type of device to return | |
800 | * @dev: struct udevice * to hold the current device. Set to NULL when there | |
801 | * are no more devices. | |
802 | */ | |
803 | #define blk_foreach_probe(flags, pos) \ | |
804 | for (int _ret = blk_first_device_err(flags, &(pos)); \ | |
805 | !_ret && pos; \ | |
806 | _ret = blk_next_device_err(flags, &(pos))) | |
807 | ||
808 | /** | |
809 | * blk_count_devices() - count the number of devices of a particular type | |
810 | * | |
811 | * @flags: Indicates type of device to find | |
185f812c | 812 | * Return: number of devices matching those flags |
96f37b09 SG |
813 | */ |
814 | int blk_count_devices(enum blk_flag_t flag); | |
815 | ||
1a73661b | 816 | #endif |