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