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