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 */
65 /* device can use 48bit addr (ATA/ATAPI v7) */
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 */
76 uint32_t mbr_sig; /* MBR integer signature */
77 efi_guid_t guid_sig; /* GPT GUID Signature */
79 #if CONFIG_IS_ENABLED(BLK)
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.
87 unsigned long (*block_read)(struct blk_desc *block_dev,
91 unsigned long (*block_write)(struct blk_desc *block_dev,
95 unsigned long (*block_erase)(struct blk_desc *block_dev,
98 void *priv; /* driver private struct pointer */
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))
106 #if CONFIG_IS_ENABLED(BLOCK_CACHE)
109 * blkcache_init() - initialize the block cache list pointers
111 int blkcache_init(void);
114 * blkcache_read() - attempt to read a set of blocks from cache
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
123 * Return: - 1 if block returned from cache, 0 otherwise.
125 int blkcache_read(int iftype, int dev,
126 lbaint_t start, lbaint_t blkcnt,
127 unsigned long blksz, void *buffer);
130 * blkcache_fill() - make data read from a block device available
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
141 void blkcache_fill(int iftype, int dev,
142 lbaint_t start, lbaint_t blkcnt,
143 unsigned long blksz, void const *buffer);
146 * blkcache_invalidate() - discard the cache for a set of blocks
147 * because of a write or device (re)initialization.
149 * @iftype - UCLASS_ID_ for type of device, or -1 for any
150 * @dev - device index of particular type, if @iftype is not -1
152 void blkcache_invalidate(int iftype, int dev);
155 * blkcache_configure() - configure block cache
157 * @param blocks - maximum blocks per entry
158 * @param entries - maximum entries in cache
160 void blkcache_configure(unsigned blocks, unsigned entries);
163 * statistics of the block cache
165 struct block_cache_stats {
168 unsigned entries; /* current entry count */
169 unsigned max_blocks_per_entry;
170 unsigned max_entries;
174 * get_blkcache_stats() - return statistics and reset
176 * @param stats - statistics are copied here
178 void blkcache_stats(struct block_cache_stats *stats);
180 /** blkcache_free() - free all memory allocated to the block cache */
181 void blkcache_free(void);
185 static inline int blkcache_read(int iftype, int dev,
186 lbaint_t start, lbaint_t blkcnt,
187 unsigned long blksz, void *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) {}
196 static inline void blkcache_invalidate(int iftype, int dev) {}
198 static inline void blkcache_free(void) {}
202 #if CONFIG_IS_ENABLED(BLK)
205 /* Operations on block devices */
208 * read() - read from a block device
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
217 unsigned long (*read)(struct udevice *dev, lbaint_t start,
218 lbaint_t blkcnt, void *buffer);
221 * write() - write to a block device
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
230 unsigned long (*write)(struct udevice *dev, lbaint_t start,
231 lbaint_t blkcnt, const void *buffer);
234 * erase() - erase a section of a block device
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
242 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
246 * select_hwpart() - select a particular hardware partition
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.
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).
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
262 int (*select_hwpart)(struct udevice *dev, int hwpart);
265 #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
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.
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,
281 * blk_read() - Read from a block device
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
290 long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
294 * blk_write() - Write to a block device
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
303 long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
307 * blk_erase() - Erase part of a block device
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
315 long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
318 * blk_find_device() - Find a block device
320 * This function does not activate the device. The device will be returned
321 * whether or not it is activated.
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
328 int blk_find_device(int uclass_id, int devnum, struct udevice **devp);
331 * blk_get_device() - Find and probe a block device ready for use
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
338 int blk_get_device(int uclass_id, int devnum, struct udevice **devp);
341 * blk_first_device() - Find the first device for a given interface
343 * The device is probed ready for use
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
349 int blk_first_device(int uclass_id, struct udevice **devp);
352 * blk_next_device() - Find the next device for a given interface
354 * This can be called repeatedly after blk_first_device() to iterate through
355 * all devices of the given interface type.
357 * The device is probed ready for use
359 * @devp: On entry, the previous device returned. On exit, the next
361 * Return: 0 if found, -ENODEV if no device, or other -ve error value
363 int blk_next_device(struct udevice **devp);
366 * blk_create_device() - Create a new block device
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)
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);
383 * blk_create_devicef() - Create a new named block device
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)
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);
400 * blk_probe_or_unbind() - Try to probe
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
406 * @dev: The device to probe
407 * Return: 0 if OK, -ve on error
409 int blk_probe_or_unbind(struct udevice *dev);
412 * blk_unbind_all() - Unbind all device of the given interface type
414 * The devices are removed and then unbound.
416 * @uclass_id: Interface type to unbind
417 * Return: 0 if OK, -ve on error
419 int blk_unbind_all(int uclass_id);
422 * blk_find_max_devnum() - find the maximum device number for an interface type
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.
427 * @uclass_id: Interface type to scan
428 * Return: maximum device number found, or -ENODEV if none, or other -ve on
431 int blk_find_max_devnum(enum uclass_id uclass_id);
434 * blk_next_free_devnum() - get the next device number for an interface type
436 * Finds the next number that is safe to use for a newly allocated device for
437 * an interface type @uclass_id.
439 * @uclass_id: Interface type to scan
440 * Return: next device number safe to use, or -ve on error
442 int blk_next_free_devnum(enum uclass_id uclass_id);
445 * blk_select_hwpart() - select a hardware partition
447 * Select a hardware partition if the device supports it (typically MMC does)
449 * @dev: Device to update
450 * @hwpart: Partition number to select
451 * Return: 0 if OK, -ve on error
453 int blk_select_hwpart(struct udevice *dev, int hwpart);
456 * blk_find_from_parent() - find a block device by looking up its parent
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
462 * The block device is not activated by this function. See
463 * blk_get_from_parent() for that.
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
470 int blk_find_from_parent(struct udevice *parent, struct udevice **devp);
473 * blk_get_from_parent() - obtain a block device by looking up its parent
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
479 * The block device is probed and ready for use.
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
486 int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
489 * blk_get_devtype() - Get the device type of a block device
491 * @dev: Block device to check
492 * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc"
494 const char *blk_get_devtype(struct udevice *dev);
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)
500 * Return: With block device descriptor on success , NULL if there is no such
503 struct blk_desc *blk_get_by_device(struct udevice *dev);
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.
513 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
514 lbaint_t blkcnt, void *buffer)
517 if (blkcache_read(block_dev->uclass_id, block_dev->devnum,
518 start, blkcnt, block_dev->blksz, buffer))
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.
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);
534 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
535 lbaint_t blkcnt, const void *buffer)
537 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
538 return block_dev->block_write(block_dev, start, blkcnt, buffer);
541 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
544 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
545 return block_dev->block_erase(block_dev, start, blkcnt);
549 * struct blk_driver - Driver for block interface types
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.
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
562 const char *uclass_idname;
563 enum uclass_id uclass_id;
565 struct blk_desc *desc;
567 * get_dev() - get a pointer to a block device given its number
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.
574 * @devnum: Device number (0 for first device on that interface,
576 * @descp: Returns pointer to the block device on success
577 * @return 0 if OK, -ve on error
579 int (*get_dev)(int devnum, struct blk_desc **descp);
582 * select_hwpart() - Select a hardware partition
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.
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.
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
598 * @return 0 if OK, other value for an error
600 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
604 * Declare a new U-Boot legacy block driver. New drivers should use driver
605 * model (UCLASS_BLK).
607 #define U_BOOT_LEGACY_BLK(__name) \
608 ll_entry_declare(struct blk_driver, __name, blk_driver)
610 struct blk_driver *blk_driver_lookup_type(int uclass_id);
612 #endif /* !CONFIG_BLK */
615 * blk_get_devnum_by_uclass_idname() - Get a block device by type and number
617 * This looks through the available block devices of the given type, returning
618 * the one with the given @devnum.
620 * @uclass_id: Block device type
621 * @devnum: Device number
622 * Return: point to block device descriptor, or NULL if not found
624 struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum);
627 * blk_get_devnum_by_uclass_id() - Get a block device by type name, and number
629 * This looks up the block device type based on @uclass_idname, then calls
630 * blk_get_devnum_by_uclass_id().
632 * @uclass_idname: Block device type name
633 * @devnum: Device number
634 * Return: point to block device descriptor, or NULL if not found
636 struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname,
640 * blk_dselect_hwpart() - select a hardware partition
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.
646 * @desc: Block device descriptor for the device to select
647 * @hwpart: Partition number to select
648 * Return: 0 if OK, -ve on error
650 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
653 * blk_list_part() - list the partitions for block devices of a given type
655 * This looks up the partition type for each block device of type @uclass_id,
656 * then displays a list of partitions.
658 * @uclass_id: Block device type
659 * Return: 0 if OK, -ENODEV if there is none of that type
661 int blk_list_part(enum uclass_id uclass_id);
664 * blk_list_devices() - list the block devices of a given type
666 * This lists each block device of the type @uclass_id, showing the capacity
667 * as well as type-specific information.
669 * @uclass_id: Block device type
671 void blk_list_devices(enum uclass_id uclass_id);
674 * blk_show_device() - show information about a given block device
676 * This shows the block device capacity as well as type-specific information.
678 * @uclass_id: Block device type
679 * @devnum: Device number
680 * Return: 0 if OK, -ENODEV for invalid device number
682 int blk_show_device(enum uclass_id uclass_id, int devnum);
685 * blk_print_device_num() - show information about a given block device
687 * This is similar to blk_show_device() but returns an error if the block
688 * device type is unknown.
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
695 int blk_print_device_num(enum uclass_id uclass_id, int devnum);
698 * blk_print_part_devnum() - print the partition information for a device
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
705 int blk_print_part_devnum(enum uclass_id uclass_id, int devnum);
708 * blk_read_devnum() - read blocks from a device
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
717 ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
718 lbaint_t blkcnt, void *buffer);
721 * blk_write_devnum() - write blocks to a device
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
730 ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
731 lbaint_t blkcnt, const void *buffer);
734 * blk_select_hwpart_devnum() - select a hardware partition
736 * This is similar to blk_dselect_hwpart() but it looks up the interface and
739 * @uclass_id: Block device type
740 * @devnum: Device number
741 * @hwpart: Partition number to select
742 * Return: 0 if OK, -ve on error
744 int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart);
747 * blk_get_uclass_name() - Get the name of an interface type
749 * @uclass_id: Interface type to check
750 * Return: name of interface, or NULL if none
752 const char *blk_get_uclass_name(enum uclass_id uclass_id);
755 * blk_common_cmd() - handle common commands with block devices
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
763 int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
768 BLKF_REMOVABLE = 1 << 1,
769 BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE,
773 * blk_first_device_err() - Get the first block device
775 * The device returned is probed if necessary, and ready for use
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
781 int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
784 * blk_next_device_err() - Get the next block device
786 * The device returned is probed if necessary, and ready for use
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
794 int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
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
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.
804 * Note that some devices are considered removable until they have been probed
806 * @return 0 if found, -ENODEV if not found
808 int blk_find_first(enum blk_flag_t flags, struct udevice **devp);
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
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.
819 * Note that some devices are considered removable until they have been probed
821 * @return 0 if found, -ENODEV if not found
823 int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
826 * blk_foreach() - iterate through block devices
828 * This creates a for() loop which works through the available block devices in
829 * order from start to end.
831 * If for some reason the uclass cannot be found, this does nothing.
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.
837 #define blk_foreach(_flags, _pos) \
838 for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \
839 _ret = blk_find_next(_flags, &_pos))
842 * blk_foreach_probe() - Helper function to iteration through block devices
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,
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.
852 #define blk_foreach_probe(flags, pos) \
853 for (int _ret = blk_first_device_err(flags, &(pos)); \
855 _ret = blk_next_device_err(flags, &(pos)))
858 * blk_count_devices() - count the number of devices of a particular type
860 * @flags: Indicates type of device to find
861 * Return: number of devices matching those flags
863 int blk_count_devices(enum blk_flag_t flag);