blk.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. #if CONFIG_IS_ENABLED(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. #if CONFIG_IS_ENABLED(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_find_device() - Find a block device
  232. *
  233. * This function does not activate the device. The device will be returned
  234. * whether or not it is activated.
  235. *
  236. * @if_type: Interface type (enum if_type_t)
  237. * @devnum: Device number (specific to each interface type)
  238. * @devp: the device, if found
  239. * @return 0 if found, -ENODEV if no device found, or other -ve error value
  240. */
  241. int blk_find_device(int if_type, int devnum, struct udevice **devp);
  242. /**
  243. * blk_get_device() - Find and probe a block device ready for use
  244. *
  245. * @if_type: Interface type (enum if_type_t)
  246. * @devnum: Device number (specific to each interface type)
  247. * @devp: the device, if found
  248. * @return 0 if found, -ENODEV if no device found, or other -ve error value
  249. */
  250. int blk_get_device(int if_type, int devnum, struct udevice **devp);
  251. /**
  252. * blk_first_device() - Find the first device for a given interface
  253. *
  254. * The device is probed ready for use
  255. *
  256. * @devnum: Device number (specific to each interface type)
  257. * @devp: the device, if found
  258. * @return 0 if found, -ENODEV if no device, or other -ve error value
  259. */
  260. int blk_first_device(int if_type, struct udevice **devp);
  261. /**
  262. * blk_next_device() - Find the next device for a given interface
  263. *
  264. * This can be called repeatedly after blk_first_device() to iterate through
  265. * all devices of the given interface type.
  266. *
  267. * The device is probed ready for use
  268. *
  269. * @devp: On entry, the previous device returned. On exit, the next
  270. * device, if found
  271. * @return 0 if found, -ENODEV if no device, or other -ve error value
  272. */
  273. int blk_next_device(struct udevice **devp);
  274. /**
  275. * blk_create_device() - Create a new block device
  276. *
  277. * @parent: Parent of the new device
  278. * @drv_name: Driver name to use for the block device
  279. * @name: Name for the device
  280. * @if_type: Interface type (enum if_type_t)
  281. * @devnum: Device number, specific to the interface type, or -1 to
  282. * allocate the next available number
  283. * @blksz: Block size of the device in bytes (typically 512)
  284. * @size: Total size of the device in bytes
  285. * @devp: the new device (which has not been probed)
  286. */
  287. int blk_create_device(struct udevice *parent, const char *drv_name,
  288. const char *name, int if_type, int devnum, int blksz,
  289. lbaint_t size, struct udevice **devp);
  290. /**
  291. * blk_create_devicef() - Create a new named block device
  292. *
  293. * @parent: Parent of the new device
  294. * @drv_name: Driver name to use for the block device
  295. * @name: Name for the device (parent name is prepended)
  296. * @if_type: Interface type (enum if_type_t)
  297. * @devnum: Device number, specific to the interface type, or -1 to
  298. * allocate the next available number
  299. * @blksz: Block size of the device in bytes (typically 512)
  300. * @size: Total size of the device in bytes
  301. * @devp: the new device (which has not been probed)
  302. */
  303. int blk_create_devicef(struct udevice *parent, const char *drv_name,
  304. const char *name, int if_type, int devnum, int blksz,
  305. lbaint_t size, struct udevice **devp);
  306. /**
  307. * blk_prepare_device() - Prepare a block device for use
  308. *
  309. * This reads partition information from the device if supported.
  310. *
  311. * @dev: Device to prepare
  312. * @return 0 if ok, -ve on error
  313. */
  314. int blk_prepare_device(struct udevice *dev);
  315. /**
  316. * blk_unbind_all() - Unbind all device of the given interface type
  317. *
  318. * The devices are removed and then unbound.
  319. *
  320. * @if_type: Interface type to unbind
  321. * @return 0 if OK, -ve on error
  322. */
  323. int blk_unbind_all(int if_type);
  324. /**
  325. * blk_find_max_devnum() - find the maximum device number for an interface type
  326. *
  327. * Finds the last allocated device number for an interface type @if_type. The
  328. * next number is safe to use for a newly allocated device.
  329. *
  330. * @if_type: Interface type to scan
  331. * @return maximum device number found, or -ENODEV if none, or other -ve on
  332. * error
  333. */
  334. int blk_find_max_devnum(enum if_type if_type);
  335. /**
  336. * blk_select_hwpart() - select a hardware partition
  337. *
  338. * Select a hardware partition if the device supports it (typically MMC does)
  339. *
  340. * @dev: Device to update
  341. * @hwpart: Partition number to select
  342. * @return 0 if OK, -ve on error
  343. */
  344. int blk_select_hwpart(struct udevice *dev, int hwpart);
  345. /**
  346. * blk_get_from_parent() - obtain a block device by looking up its parent
  347. *
  348. * All devices with
  349. */
  350. int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
  351. #else
  352. #include <errno.h>
  353. /*
  354. * These functions should take struct udevice instead of struct blk_desc,
  355. * but this is convenient for migration to driver model. Add a 'd' prefix
  356. * to the function operations, so that blk_read(), etc. can be reserved for
  357. * functions with the correct arguments.
  358. */
  359. static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
  360. lbaint_t blkcnt, void *buffer)
  361. {
  362. ulong blks_read;
  363. if (blkcache_read(block_dev->if_type, block_dev->devnum,
  364. start, blkcnt, block_dev->blksz, buffer))
  365. return blkcnt;
  366. /*
  367. * We could check if block_read is NULL and return -ENOSYS. But this
  368. * bloats the code slightly (cause some board to fail to build), and
  369. * it would be an error to try an operation that does not exist.
  370. */
  371. blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
  372. if (blks_read == blkcnt)
  373. blkcache_fill(block_dev->if_type, block_dev->devnum,
  374. start, blkcnt, block_dev->blksz, buffer);
  375. return blks_read;
  376. }
  377. static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
  378. lbaint_t blkcnt, const void *buffer)
  379. {
  380. blkcache_invalidate(block_dev->if_type, block_dev->devnum);
  381. return block_dev->block_write(block_dev, start, blkcnt, buffer);
  382. }
  383. static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
  384. lbaint_t blkcnt)
  385. {
  386. blkcache_invalidate(block_dev->if_type, block_dev->devnum);
  387. return block_dev->block_erase(block_dev, start, blkcnt);
  388. }
  389. /**
  390. * struct blk_driver - Driver for block interface types
  391. *
  392. * This provides access to the block devices for each interface type. One
  393. * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
  394. * type that is to be supported.
  395. *
  396. * @if_typename: Interface type name
  397. * @if_type: Interface type
  398. * @max_devs: Maximum number of devices supported
  399. * @desc: Pointer to list of devices for this interface type,
  400. * or NULL to use @get_dev() instead
  401. */
  402. struct blk_driver {
  403. const char *if_typename;
  404. enum if_type if_type;
  405. int max_devs;
  406. struct blk_desc *desc;
  407. /**
  408. * get_dev() - get a pointer to a block device given its number
  409. *
  410. * Each interface allocates its own devices and typically
  411. * struct blk_desc is contained with the interface's data structure.
  412. * There is no global numbering for block devices. This method allows
  413. * the device for an interface type to be obtained when @desc is NULL.
  414. *
  415. * @devnum: Device number (0 for first device on that interface,
  416. * 1 for second, etc.
  417. * @descp: Returns pointer to the block device on success
  418. * @return 0 if OK, -ve on error
  419. */
  420. int (*get_dev)(int devnum, struct blk_desc **descp);
  421. /**
  422. * select_hwpart() - Select a hardware partition
  423. *
  424. * Some devices (e.g. MMC) can support partitioning at the hardware
  425. * level. This is quite separate from the normal idea of
  426. * software-based partitions. MMC hardware partitions must be
  427. * explicitly selected. Once selected only the region of the device
  428. * covered by that partition is accessible.
  429. *
  430. * The MMC standard provides for two boot partitions (numbered 1 and 2),
  431. * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
  432. * Partition 0 is the main user-data partition.
  433. *
  434. * @desc: Block device descriptor
  435. * @hwpart: Hardware partition number to select. 0 means the main
  436. * user-data partition, 1 is the first partition, 2 is
  437. * the second, etc.
  438. * @return 0 if OK, other value for an error
  439. */
  440. int (*select_hwpart)(struct blk_desc *desc, int hwpart);
  441. };
  442. /*
  443. * Declare a new U-Boot legacy block driver. New drivers should use driver
  444. * model (UCLASS_BLK).
  445. */
  446. #define U_BOOT_LEGACY_BLK(__name) \
  447. ll_entry_declare(struct blk_driver, __name, blk_driver)
  448. struct blk_driver *blk_driver_lookup_type(int if_type);
  449. #endif /* !CONFIG_BLK */
  450. /**
  451. * blk_get_devnum_by_typename() - Get a block device by type and number
  452. *
  453. * This looks through the available block devices of the given type, returning
  454. * the one with the given @devnum.
  455. *
  456. * @if_type: Block device type
  457. * @devnum: Device number
  458. * @return point to block device descriptor, or NULL if not found
  459. */
  460. struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
  461. /**
  462. * blk_get_devnum_by_type() - Get a block device by type name, and number
  463. *
  464. * This looks up the block device type based on @if_typename, then calls
  465. * blk_get_devnum_by_type().
  466. *
  467. * @if_typename: Block device type name
  468. * @devnum: Device number
  469. * @return point to block device descriptor, or NULL if not found
  470. */
  471. struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
  472. int devnum);
  473. /**
  474. * blk_dselect_hwpart() - select a hardware partition
  475. *
  476. * This selects a hardware partition (such as is supported by MMC). The block
  477. * device size may change as this effectively points the block device to a
  478. * partition at the hardware level. See the select_hwpart() method above.
  479. *
  480. * @desc: Block device descriptor for the device to select
  481. * @hwpart: Partition number to select
  482. * @return 0 if OK, -ve on error
  483. */
  484. int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
  485. /**
  486. * blk_list_part() - list the partitions for block devices of a given type
  487. *
  488. * This looks up the partition type for each block device of type @if_type,
  489. * then displays a list of partitions.
  490. *
  491. * @if_type: Block device type
  492. * @return 0 if OK, -ENODEV if there is none of that type
  493. */
  494. int blk_list_part(enum if_type if_type);
  495. /**
  496. * blk_list_devices() - list the block devices of a given type
  497. *
  498. * This lists each block device of the type @if_type, showing the capacity
  499. * as well as type-specific information.
  500. *
  501. * @if_type: Block device type
  502. */
  503. void blk_list_devices(enum if_type if_type);
  504. /**
  505. * blk_show_device() - show information about a given block device
  506. *
  507. * This shows the block device capacity as well as type-specific information.
  508. *
  509. * @if_type: Block device type
  510. * @devnum: Device number
  511. * @return 0 if OK, -ENODEV for invalid device number
  512. */
  513. int blk_show_device(enum if_type if_type, int devnum);
  514. /**
  515. * blk_print_device_num() - show information about a given block device
  516. *
  517. * This is similar to blk_show_device() but returns an error if the block
  518. * device type is unknown.
  519. *
  520. * @if_type: Block device type
  521. * @devnum: Device number
  522. * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
  523. * device is not connected
  524. */
  525. int blk_print_device_num(enum if_type if_type, int devnum);
  526. /**
  527. * blk_print_part_devnum() - print the partition information for a device
  528. *
  529. * @if_type: Block device type
  530. * @devnum: Device number
  531. * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
  532. * the interface type is not supported, other -ve on other error
  533. */
  534. int blk_print_part_devnum(enum if_type if_type, int devnum);
  535. /**
  536. * blk_read_devnum() - read blocks from a device
  537. *
  538. * @if_type: Block device type
  539. * @devnum: Device number
  540. * @blkcnt: Number of blocks to read
  541. * @buffer: Address to write data to
  542. * @return number of blocks read, or -ve error number on error
  543. */
  544. ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
  545. lbaint_t blkcnt, void *buffer);
  546. /**
  547. * blk_write_devnum() - write blocks to a device
  548. *
  549. * @if_type: Block device type
  550. * @devnum: Device number
  551. * @blkcnt: Number of blocks to write
  552. * @buffer: Address to read data from
  553. * @return number of blocks written, or -ve error number on error
  554. */
  555. ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
  556. lbaint_t blkcnt, const void *buffer);
  557. /**
  558. * blk_select_hwpart_devnum() - select a hardware partition
  559. *
  560. * This is similar to blk_dselect_hwpart() but it looks up the interface and
  561. * device number.
  562. *
  563. * @if_type: Block device type
  564. * @devnum: Device number
  565. * @hwpart: Partition number to select
  566. * @return 0 if OK, -ve on error
  567. */
  568. int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
  569. #endif