blk.h 18 KB

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