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