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