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