blk.h 19 KB

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