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