1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 #include <dm/uclass-id.h>
13 #ifdef CONFIG_SYS_64BIT_LBA
14 typedef uint64_t lbaint_t;
15 #define LBAFlength "ll"
17 typedef ulong lbaint_t;
18 #define LBAFlength "l"
20 #define LBAF "%" LBAFlength "x"
21 #define LBAFU "%" LBAFlength "u"
25 static inline bool blk_enabled(void)
27 return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_SPL_LEGACY_BLOCK);
30 #define BLK_VEN_SIZE 40
31 #define BLK_PRD_SIZE 20
32 #define BLK_REV_SIZE 8
34 #define PART_FORMAT_PCAT 0x1
35 #define PART_FORMAT_GPT 0x2
38 * Identifies the partition table type (ie. MBR vs GPT GUID) signature
45 SIG_TYPE_COUNT /* Number of signature types */
49 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
50 * with dev_get_uclass_plat(dev)
54 * TODO: With driver model we should be able to use the parent
55 * device's uclass instead.
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 */
66 /* device can use 48bit addr (ATA/ATAPI v7) */
69 unsigned char atapi; /* Use ATAPI protocol */
70 lbaint_t lba; /* number of blocks */
71 unsigned long blksz; /* block size */
72 int log2blksz; /* for convenience: log2(blksz) */
73 char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
74 char product[BLK_PRD_SIZE + 1]; /* device product number */
75 char revision[BLK_REV_SIZE + 1]; /* firmware revision */
76 enum sig_type sig_type; /* Partition table signature type */
78 uint32_t mbr_sig; /* MBR integer signature */
79 efi_guid_t guid_sig; /* GPT GUID Signature */
81 #if CONFIG_IS_ENABLED(BLK)
83 * For now we have a few functions which take struct blk_desc as a
84 * parameter. This field allows them to look up the associated
85 * device. Once these functions are removed we can drop this field.
89 unsigned long (*block_read)(struct blk_desc *block_dev,
93 unsigned long (*block_write)(struct blk_desc *block_dev,
97 unsigned long (*block_erase)(struct blk_desc *block_dev,
100 void *priv; /* driver private struct pointer */
104 #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
105 #define PAD_TO_BLOCKSIZE(size, blk_desc) \
106 (PAD_SIZE(size, blk_desc->blksz))
108 #if CONFIG_IS_ENABLED(BLOCK_CACHE)
111 * blkcache_init() - initialize the block cache list pointers
113 int blkcache_init(void);
116 * blkcache_read() - attempt to read a set of blocks from cache
118 * @param iftype - uclass_id_x for type of device
119 * @param dev - device index of particular type
120 * @param start - starting block number
121 * @param blkcnt - number of blocks to read
122 * @param blksz - size in bytes of each block
123 * @param buffer - buffer to contain cached data
125 * Return: - 1 if block returned from cache, 0 otherwise.
127 int blkcache_read(int iftype, int dev,
128 lbaint_t start, lbaint_t blkcnt,
129 unsigned long blksz, void *buffer);
132 * blkcache_fill() - make data read from a block device available
135 * @param iftype - uclass_id_x for type of device
136 * @param dev - device index of particular type
137 * @param start - starting block number
138 * @param blkcnt - number of blocks available
139 * @param blksz - size in bytes of each block
140 * @param buffer - buffer containing data to cache
143 void blkcache_fill(int iftype, int dev,
144 lbaint_t start, lbaint_t blkcnt,
145 unsigned long blksz, void const *buffer);
148 * blkcache_invalidate() - discard the cache for a set of blocks
149 * because of a write or device (re)initialization.
151 * @iftype - UCLASS_ID_ for type of device, or -1 for any
152 * @dev - device index of particular type, if @iftype is not -1
154 void blkcache_invalidate(int iftype, int dev);
157 * blkcache_configure() - configure block cache
159 * @param blocks - maximum blocks per entry
160 * @param entries - maximum entries in cache
162 void blkcache_configure(unsigned blocks, unsigned entries);
165 * statistics of the block cache
167 struct block_cache_stats {
170 unsigned entries; /* current entry count */
171 unsigned max_blocks_per_entry;
172 unsigned max_entries;
176 * get_blkcache_stats() - return statistics and reset
178 * @param stats - statistics are copied here
180 void blkcache_stats(struct block_cache_stats *stats);
182 /** blkcache_free() - free all memory allocated to the block cache */
183 void blkcache_free(void);
187 static inline int blkcache_read(int iftype, int dev,
188 lbaint_t start, lbaint_t blkcnt,
189 unsigned long blksz, void *buffer)
194 static inline void blkcache_fill(int iftype, int dev,
195 lbaint_t start, lbaint_t blkcnt,
196 unsigned long blksz, void const *buffer) {}
198 static inline void blkcache_invalidate(int iftype, int dev) {}
200 static inline void blkcache_free(void) {}
204 #if CONFIG_IS_ENABLED(BLK)
207 /* Operations on block devices */
210 * read() - read from a block device
212 * @dev: Device to read from
213 * @start: Start block number to read (0=first)
214 * @blkcnt: Number of blocks to read
215 * @buffer: Destination buffer for data read
216 * @return number of blocks read, or -ve error number (see the
217 * IS_ERR_VALUE() macro
219 unsigned long (*read)(struct udevice *dev, lbaint_t start,
220 lbaint_t blkcnt, void *buffer);
223 * write() - write to a block device
225 * @dev: Device to write to
226 * @start: Start block number to write (0=first)
227 * @blkcnt: Number of blocks to write
228 * @buffer: Source buffer for data to write
229 * @return number of blocks written, or -ve error number (see the
230 * IS_ERR_VALUE() macro
232 unsigned long (*write)(struct udevice *dev, lbaint_t start,
233 lbaint_t blkcnt, const void *buffer);
236 * erase() - erase a section of a block device
238 * @dev: Device to (partially) erase
239 * @start: Start block number to erase (0=first)
240 * @blkcnt: Number of blocks to erase
241 * @return number of blocks erased, or -ve error number (see the
242 * IS_ERR_VALUE() macro
244 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
248 * select_hwpart() - select a particular hardware partition
250 * Some devices (e.g. MMC) can support partitioning at the hardware
251 * level. This is quite separate from the normal idea of
252 * software-based partitions. MMC hardware partitions must be
253 * explicitly selected. Once selected only the region of the device
254 * covered by that partition is accessible.
256 * The MMC standard provides for two boot partitions (numbered 1 and 2),
257 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
259 * @dev: Block device to update
260 * @hwpart: Hardware partition number to select. 0 means the raw
261 * device, 1 is the first partition, 2 is the second, etc.
262 * @return 0 if OK, -ve on error
264 int (*select_hwpart)(struct udevice *dev, int hwpart);
267 #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
270 * These functions should take struct udevice instead of struct blk_desc,
271 * but this is convenient for migration to driver model. Add a 'd' prefix
272 * to the function operations, so that blk_read(), etc. can be reserved for
273 * functions with the correct arguments.
275 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
276 lbaint_t blkcnt, void *buffer);
277 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
278 lbaint_t blkcnt, const void *buffer);
279 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
283 * blk_read() - Read from a block device
285 * @dev: Device to read from
286 * @start: Start block for the read
287 * @blkcnt: Number of blocks to read
288 * @buf: Place to put the data
289 * @return number of blocks read (which may be less than @blkcnt),
290 * or -ve on error. This never returns 0 unless @blkcnt is 0
292 long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
296 * blk_write() - Write to a block device
298 * @dev: Device to write to
299 * @start: Start block for the write
300 * @blkcnt: Number of blocks to write
301 * @buf: Data to write
302 * @return number of blocks written (which may be less than @blkcnt),
303 * or -ve on error. This never returns 0 unless @blkcnt is 0
305 long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
309 * blk_erase() - Erase part of a block device
311 * @dev: Device to erase
312 * @start: Start block for the erase
313 * @blkcnt: Number of blocks to erase
314 * @return number of blocks erased (which may be less than @blkcnt),
315 * or -ve on error. This never returns 0 unless @blkcnt is 0
317 long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
320 * blk_find_device() - Find a block device
322 * This function does not activate the device. The device will be returned
323 * whether or not it is activated.
325 * @uclass_id: Interface type (enum uclass_id_t)
326 * @devnum: Device number (specific to each interface type)
327 * @devp: the device, if found
328 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
330 int blk_find_device(int uclass_id, int devnum, struct udevice **devp);
333 * blk_get_device() - Find and probe a block device ready for use
335 * @uclass_id: Interface type (enum uclass_id_t)
336 * @devnum: Device number (specific to each interface type)
337 * @devp: the device, if found
338 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
340 int blk_get_device(int uclass_id, int devnum, struct udevice **devp);
343 * blk_first_device() - Find the first device for a given interface
345 * The device is probed ready for use
347 * @devnum: Device number (specific to each interface type)
348 * @devp: the device, if found
349 * Return: 0 if found, -ENODEV if no device, or other -ve error value
351 int blk_first_device(int uclass_id, struct udevice **devp);
354 * blk_next_device() - Find the next device for a given interface
356 * This can be called repeatedly after blk_first_device() to iterate through
357 * all devices of the given interface type.
359 * The device is probed ready for use
361 * @devp: On entry, the previous device returned. On exit, the next
363 * Return: 0 if found, -ENODEV if no device, or other -ve error value
365 int blk_next_device(struct udevice **devp);
368 * blk_create_device() - Create a new block device
370 * @parent: Parent of the new device
371 * @drv_name: Driver name to use for the block device
372 * @name: Name for the device
373 * @uclass_id: Interface type (enum uclass_id_t)
374 * @devnum: Device number, specific to the interface type, or -1 to
375 * allocate the next available number
376 * @blksz: Block size of the device in bytes (typically 512)
377 * @lba: Total number of blocks of the device
378 * @devp: the new device (which has not been probed)
380 int blk_create_device(struct udevice *parent, const char *drv_name,
381 const char *name, int uclass_id, int devnum, int blksz,
382 lbaint_t lba, struct udevice **devp);
385 * blk_create_devicef() - Create a new named block device
387 * @parent: Parent of the new device
388 * @drv_name: Driver name to use for the block device
389 * @name: Name for the device (parent name is prepended)
390 * @uclass_id: Interface type (enum uclass_id_t)
391 * @devnum: Device number, specific to the interface type, or -1 to
392 * allocate the next available number
393 * @blksz: Block size of the device in bytes (typically 512)
394 * @lba: Total number of blocks of the device
395 * @devp: the new device (which has not been probed)
397 int blk_create_devicef(struct udevice *parent, const char *drv_name,
398 const char *name, int uclass_id, int devnum, int blksz,
399 lbaint_t lba, struct udevice **devp);
402 * blk_probe_or_unbind() - Try to probe
404 * Try to probe the device, primarily for enumerating partitions.
405 * If it fails, the device itself is unbound since it means that it won't
408 * @dev: The device to probe
409 * Return: 0 if OK, -ve on error
411 int blk_probe_or_unbind(struct udevice *dev);
414 * blk_unbind_all() - Unbind all device of the given interface type
416 * The devices are removed and then unbound.
418 * @uclass_id: Interface type to unbind
419 * Return: 0 if OK, -ve on error
421 int blk_unbind_all(int uclass_id);
424 * blk_find_max_devnum() - find the maximum device number for an interface type
426 * Finds the last allocated device number for an interface type @uclass_id. The
427 * next number is safe to use for a newly allocated device.
429 * @uclass_id: Interface type to scan
430 * Return: maximum device number found, or -ENODEV if none, or other -ve on
433 int blk_find_max_devnum(enum uclass_id uclass_id);
436 * blk_next_free_devnum() - get the next device number for an interface type
438 * Finds the next number that is safe to use for a newly allocated device for
439 * an interface type @uclass_id.
441 * @uclass_id: Interface type to scan
442 * Return: next device number safe to use, or -ve on error
444 int blk_next_free_devnum(enum uclass_id uclass_id);
447 * blk_select_hwpart() - select a hardware partition
449 * Select a hardware partition if the device supports it (typically MMC does)
451 * @dev: Device to update
452 * @hwpart: Partition number to select
453 * Return: 0 if OK, -ve on error
455 int blk_select_hwpart(struct udevice *dev, int hwpart);
458 * blk_find_from_parent() - find a block device by looking up its parent
460 * All block devices have a parent 'media' device which provides the block
461 * driver for the block device, ensuring that access to the underlying medium
464 * The block device is not activated by this function. See
465 * blk_get_from_parent() for that.
467 * @parent: Media device
468 * @devp: Returns the associated block device, if any
469 * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
472 int blk_find_from_parent(struct udevice *parent, struct udevice **devp);
475 * blk_get_from_parent() - obtain a block device by looking up its parent
477 * All block devices have a parent 'media' device which provides the block
478 * driver for the block device, ensuring that access to the underlying medium
481 * The block device is probed and ready for use.
483 * @parent: Media device
484 * @devp: Returns the associated block device, if any
485 * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
488 int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
491 * blk_get_devtype() - Get the device type of a block device
493 * @dev: Block device to check
494 * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc"
496 const char *blk_get_devtype(struct udevice *dev);
499 * blk_get_by_device() - Get the block device descriptor for the given device
500 * @dev: Instance of a storage device (the parent of the block device)
502 * Return: With block device descriptor on success , NULL if there is no such
505 struct blk_desc *blk_get_by_device(struct udevice *dev);
510 * These functions should take struct udevice instead of struct blk_desc,
511 * but this is convenient for migration to driver model. Add a 'd' prefix
512 * to the function operations, so that blk_read(), etc. can be reserved for
513 * functions with the correct arguments.
515 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
516 lbaint_t blkcnt, void *buffer)
519 if (blkcache_read(block_dev->uclass_id, block_dev->devnum,
520 start, blkcnt, block_dev->blksz, buffer))
524 * We could check if block_read is NULL and return -ENOSYS. But this
525 * bloats the code slightly (cause some board to fail to build), and
526 * it would be an error to try an operation that does not exist.
528 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
529 if (blks_read == blkcnt)
530 blkcache_fill(block_dev->uclass_id, block_dev->devnum,
531 start, blkcnt, block_dev->blksz, buffer);
536 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
537 lbaint_t blkcnt, const void *buffer)
539 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
540 return block_dev->block_write(block_dev, start, blkcnt, buffer);
543 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
546 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
547 return block_dev->block_erase(block_dev, start, blkcnt);
551 * struct blk_driver - Driver for block interface types
553 * This provides access to the block devices for each interface type. One
554 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
555 * type that is to be supported.
557 * @uclass_idname: Interface type name
558 * @uclass_id: Interface type
559 * @max_devs: Maximum number of devices supported
560 * @desc: Pointer to list of devices for this interface type,
561 * or NULL to use @get_dev() instead
564 const char *uclass_idname;
565 enum uclass_id uclass_id;
567 struct blk_desc *desc;
569 * get_dev() - get a pointer to a block device given its number
571 * Each interface allocates its own devices and typically
572 * struct blk_desc is contained with the interface's data structure.
573 * There is no global numbering for block devices. This method allows
574 * the device for an interface type to be obtained when @desc is NULL.
576 * @devnum: Device number (0 for first device on that interface,
578 * @descp: Returns pointer to the block device on success
579 * @return 0 if OK, -ve on error
581 int (*get_dev)(int devnum, struct blk_desc **descp);
584 * select_hwpart() - Select a hardware partition
586 * Some devices (e.g. MMC) can support partitioning at the hardware
587 * level. This is quite separate from the normal idea of
588 * software-based partitions. MMC hardware partitions must be
589 * explicitly selected. Once selected only the region of the device
590 * covered by that partition is accessible.
592 * The MMC standard provides for two boot partitions (numbered 1 and 2),
593 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
594 * Partition 0 is the main user-data partition.
596 * @desc: Block device descriptor
597 * @hwpart: Hardware partition number to select. 0 means the main
598 * user-data partition, 1 is the first partition, 2 is
600 * @return 0 if OK, other value for an error
602 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
606 * Declare a new U-Boot legacy block driver. New drivers should use driver
607 * model (UCLASS_BLK).
609 #define U_BOOT_LEGACY_BLK(__name) \
610 ll_entry_declare(struct blk_driver, __name, blk_driver)
612 struct blk_driver *blk_driver_lookup_type(int uclass_id);
614 #endif /* !CONFIG_BLK */
617 * blk_get_devnum_by_uclass_idname() - Get a block device by type and number
619 * This looks through the available block devices of the given type, returning
620 * the one with the given @devnum.
622 * @uclass_id: Block device type
623 * @devnum: Device number
624 * Return: point to block device descriptor, or NULL if not found
626 struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum);
629 * blk_get_devnum_by_uclass_id() - Get a block device by type name, and number
631 * This looks up the block device type based on @uclass_idname, then calls
632 * blk_get_devnum_by_uclass_id().
634 * @uclass_idname: Block device type name
635 * @devnum: Device number
636 * Return: point to block device descriptor, or NULL if not found
638 struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname,
642 * blk_dselect_hwpart() - select a hardware partition
644 * This selects a hardware partition (such as is supported by MMC). The block
645 * device size may change as this effectively points the block device to a
646 * partition at the hardware level. See the select_hwpart() method above.
648 * @desc: Block device descriptor for the device to select
649 * @hwpart: Partition number to select
650 * Return: 0 if OK, -ve on error
652 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
655 * blk_list_part() - list the partitions for block devices of a given type
657 * This looks up the partition type for each block device of type @uclass_id,
658 * then displays a list of partitions.
660 * @uclass_id: Block device type
661 * Return: 0 if OK, -ENODEV if there is none of that type
663 int blk_list_part(enum uclass_id uclass_id);
666 * blk_list_devices() - list the block devices of a given type
668 * This lists each block device of the type @uclass_id, showing the capacity
669 * as well as type-specific information.
671 * @uclass_id: Block device type
673 void blk_list_devices(enum uclass_id uclass_id);
676 * blk_show_device() - show information about a given block device
678 * This shows the block device capacity as well as type-specific information.
680 * @uclass_id: Block device type
681 * @devnum: Device number
682 * Return: 0 if OK, -ENODEV for invalid device number
684 int blk_show_device(enum uclass_id uclass_id, int devnum);
687 * blk_print_device_num() - show information about a given block device
689 * This is similar to blk_show_device() but returns an error if the block
690 * device type is unknown.
692 * @uclass_id: Block device type
693 * @devnum: Device number
694 * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
695 * device is not connected
697 int blk_print_device_num(enum uclass_id uclass_id, int devnum);
700 * blk_print_part_devnum() - print the partition information for a device
702 * @uclass_id: Block device type
703 * @devnum: Device number
704 * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
705 * the interface type is not supported, other -ve on other error
707 int blk_print_part_devnum(enum uclass_id uclass_id, int devnum);
710 * blk_read_devnum() - read blocks from a device
712 * @uclass_id: Block device type
713 * @devnum: Device number
714 * @start: Start block number to read (0=first)
715 * @blkcnt: Number of blocks to read
716 * @buffer: Address to write data to
717 * Return: number of blocks read, or -ve error number on error
719 ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
720 lbaint_t blkcnt, void *buffer);
723 * blk_write_devnum() - write blocks to a device
725 * @uclass_id: Block device type
726 * @devnum: Device number
727 * @start: Start block number to write (0=first)
728 * @blkcnt: Number of blocks to write
729 * @buffer: Address to read data from
730 * Return: number of blocks written, or -ve error number on error
732 ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
733 lbaint_t blkcnt, const void *buffer);
736 * blk_select_hwpart_devnum() - select a hardware partition
738 * This is similar to blk_dselect_hwpart() but it looks up the interface and
741 * @uclass_id: Block device type
742 * @devnum: Device number
743 * @hwpart: Partition number to select
744 * Return: 0 if OK, -ve on error
746 int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart);
749 * blk_get_uclass_name() - Get the name of an interface type
751 * @uclass_id: Interface type to check
752 * Return: name of interface, or NULL if none
754 const char *blk_get_uclass_name(enum uclass_id uclass_id);
757 * blk_common_cmd() - handle common commands with block devices
759 * @args: Number of arguments to the command (argv[0] is the command itself)
760 * @argv: Command arguments
761 * @uclass_id: Interface type
762 * @cur_devnump: Current device number for this interface type
763 * Return: 0 if OK, CMD_RET_ERROR on error
765 int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
770 BLKF_REMOVABLE = 1 << 1,
771 BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE,
775 * blk_first_device_err() - Get the first block device
777 * The device returned is probed if necessary, and ready for use
779 * @flags: Indicates type of device to return
780 * @devp: Returns pointer to the first device in that uclass, or NULL if none
781 * Return: 0 if found, -ENODEV if not found, other -ve on error
783 int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
786 * blk_next_device_err() - Get the next block device
788 * The device returned is probed if necessary, and ready for use
790 * @flags: Indicates type of device to return
791 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
792 * to the next device in the uclass if no error occurred, or -ENODEV if
793 * there is no next device.
794 * Return: 0 if found, -ENODEV if not found, other -ve on error
796 int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
799 * blk_find_first() - Return the first matching block device
800 * @flags: Indicates type of device to return
801 * @devp: Returns pointer to device, or NULL on error
803 * The device is not prepared for use - this is an internal function.
804 * The function uclass_get_device_tail() can be used to probe the device.
806 * Note that some devices are considered removable until they have been probed
808 * @return 0 if found, -ENODEV if not found
810 int blk_find_first(enum blk_flag_t flags, struct udevice **devp);
813 * blk_find_next() - Return the next matching block device
814 * @flags: Indicates type of device to return
815 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
816 * to the next device in the same uclass, or NULL if none
818 * The device is not prepared for use - this is an internal function.
819 * The function uclass_get_device_tail() can be used to probe the device.
821 * Note that some devices are considered removable until they have been probed
823 * @return 0 if found, -ENODEV if not found
825 int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
828 * blk_foreach() - iterate through block devices
830 * This creates a for() loop which works through the available block devices in
831 * order from start to end.
833 * If for some reason the uclass cannot be found, this does nothing.
835 * @_flags: Indicates type of device to return
836 * @_pos: struct udevice * to hold the current device. Set to NULL when there
837 * are no more devices.
839 #define blk_foreach(_flags, _pos) \
840 for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \
841 _ret = blk_find_next(_flags, &_pos))
844 * blk_foreach_probe() - Helper function to iteration through block devices
846 * This creates a for() loop which works through the available devices in
847 * a uclass in order from start to end. Devices are probed if necessary,
850 * @flags: Indicates type of device to return
851 * @dev: struct udevice * to hold the current device. Set to NULL when there
852 * are no more devices.
854 #define blk_foreach_probe(flags, pos) \
855 for (int _ret = blk_first_device_err(flags, &(pos)); \
857 _ret = blk_next_device_err(flags, &(pos)))
860 * blk_count_devices() - count the number of devices of a particular type
862 * @flags: Indicates type of device to find
863 * Return: number of devices matching those flags
865 int blk_count_devices(enum blk_flag_t flag);