blob: 5bdffe74015200b1e155b612bd73e736c36b1688 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassac8162f2016-02-29 15:25:39 -07002/*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glassac8162f2016-02-29 15:25:39 -07005 */
6
7#ifndef BLK_H
8#define BLK_H
9
Peter Jonesc27e1c12017-09-13 18:05:25 -040010#include <efi.h>
11
Simon Glassac8162f2016-02-29 15:25:39 -070012#ifdef CONFIG_SYS_64BIT_LBA
13typedef uint64_t lbaint_t;
14#define LBAFlength "ll"
15#else
16typedef ulong lbaint_t;
17#define LBAFlength "l"
18#endif
19#define LBAF "%" LBAFlength "x"
20#define LBAFU "%" LBAFlength "u"
21
Simon Glassfc7a7442021-07-05 16:32:59 -060022struct udevice;
23
Simon Glassf5ac3032022-08-11 19:34:45 -060024static inline bool blk_enabled(void)
25{
Simon Glass3bf7d7a2022-08-11 19:34:48 -060026 return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_SPL_LEGACY_BLOCK);
Simon Glassf5ac3032022-08-11 19:34:45 -060027}
28
Simon Glassac8162f2016-02-29 15:25:39 -070029/* Interface types: */
Simon Glass83b783f2016-02-29 15:25:40 -070030enum if_type {
31 IF_TYPE_UNKNOWN = 0,
32 IF_TYPE_IDE,
33 IF_TYPE_SCSI,
34 IF_TYPE_ATAPI,
35 IF_TYPE_USB,
36 IF_TYPE_DOC,
37 IF_TYPE_MMC,
38 IF_TYPE_SD,
39 IF_TYPE_SATA,
40 IF_TYPE_HOST,
Zhikang Zhang182fccd2017-08-03 02:30:56 -070041 IF_TYPE_NVME,
Simon Glass15c4d672021-12-04 08:56:30 -070042 IF_TYPE_EFI_LOADER,
Anastasiia Lukianenko4fec7f82020-08-06 12:42:55 +030043 IF_TYPE_PVBLOCK,
Tuomas Tynkkynend4580062018-10-15 02:21:10 -070044 IF_TYPE_VIRTIO,
Simon Glass507ab962021-12-04 08:56:31 -070045 IF_TYPE_EFI_MEDIA,
Simon Glass83b783f2016-02-29 15:25:40 -070046
47 IF_TYPE_COUNT, /* Number of interface types */
48};
Simon Glassac8162f2016-02-29 15:25:39 -070049
Bin Mengcf406d22017-09-10 05:12:50 -070050#define BLK_VEN_SIZE 40
51#define BLK_PRD_SIZE 20
52#define BLK_REV_SIZE 8
53
Masahisa Kojima6460c3e2021-10-26 17:27:25 +090054#define PART_FORMAT_PCAT 0x1
55#define PART_FORMAT_GPT 0x2
56
Simon Glasscceee552016-02-29 15:25:55 -070057/*
Peter Jonesc27e1c12017-09-13 18:05:25 -040058 * Identifies the partition table type (ie. MBR vs GPT GUID) signature
59 */
60enum sig_type {
61 SIG_TYPE_NONE,
62 SIG_TYPE_MBR,
63 SIG_TYPE_GUID,
64
65 SIG_TYPE_COUNT /* Number of signature types */
66};
67
68/*
Simon Glasscceee552016-02-29 15:25:55 -070069 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
Simon Glass71fa5b42020-12-03 16:55:18 -070070 * with dev_get_uclass_plat(dev)
Simon Glasscceee552016-02-29 15:25:55 -070071 */
Simon Glassac8162f2016-02-29 15:25:39 -070072struct blk_desc {
Simon Glasscceee552016-02-29 15:25:55 -070073 /*
74 * TODO: With driver model we should be able to use the parent
75 * device's uclass instead.
76 */
Simon Glass83b783f2016-02-29 15:25:40 -070077 enum if_type if_type; /* type of the interface */
Simon Glass2f26fff2016-02-29 15:25:51 -070078 int devnum; /* device number */
Simon Glassac8162f2016-02-29 15:25:39 -070079 unsigned char part_type; /* partition type */
80 unsigned char target; /* target SCSI ID */
81 unsigned char lun; /* target LUN */
82 unsigned char hwpart; /* HW partition, e.g. for eMMC */
83 unsigned char type; /* device type */
84 unsigned char removable; /* removable device */
85#ifdef CONFIG_LBA48
86 /* device can use 48bit addr (ATA/ATAPI v7) */
87 unsigned char lba48;
88#endif
89 lbaint_t lba; /* number of blocks */
90 unsigned long blksz; /* block size */
91 int log2blksz; /* for convenience: log2(blksz) */
Bin Mengcf406d22017-09-10 05:12:50 -070092 char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
93 char product[BLK_PRD_SIZE + 1]; /* device product number */
94 char revision[BLK_REV_SIZE + 1]; /* firmware revision */
Peter Jonesc27e1c12017-09-13 18:05:25 -040095 enum sig_type sig_type; /* Partition table signature type */
96 union {
97 uint32_t mbr_sig; /* MBR integer signature */
98 efi_guid_t guid_sig; /* GPT GUID Signature */
99 };
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600100#if CONFIG_IS_ENABLED(BLK)
Simon Glass8f5f7222016-05-01 13:52:33 -0600101 /*
102 * For now we have a few functions which take struct blk_desc as a
103 * parameter. This field allows them to look up the associated
104 * device. Once these functions are removed we can drop this field.
105 */
Simon Glasscceee552016-02-29 15:25:55 -0700106 struct udevice *bdev;
107#else
Simon Glassac8162f2016-02-29 15:25:39 -0700108 unsigned long (*block_read)(struct blk_desc *block_dev,
109 lbaint_t start,
110 lbaint_t blkcnt,
111 void *buffer);
112 unsigned long (*block_write)(struct blk_desc *block_dev,
113 lbaint_t start,
114 lbaint_t blkcnt,
115 const void *buffer);
116 unsigned long (*block_erase)(struct blk_desc *block_dev,
117 lbaint_t start,
118 lbaint_t blkcnt);
119 void *priv; /* driver private struct pointer */
Simon Glasscceee552016-02-29 15:25:55 -0700120#endif
Simon Glassac8162f2016-02-29 15:25:39 -0700121};
122
123#define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
124#define PAD_TO_BLOCKSIZE(size, blk_desc) \
125 (PAD_SIZE(size, blk_desc->blksz))
126
Adam Fordd693fb92018-06-11 17:17:48 -0500127#if CONFIG_IS_ENABLED(BLOCK_CACHE)
Angelo Durgehello3d8e4c12020-01-21 10:37:27 +0100128
129/**
130 * blkcache_init() - initialize the block cache list pointers
131 */
132int blkcache_init(void);
133
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700134/**
135 * blkcache_read() - attempt to read a set of blocks from cache
136 *
137 * @param iftype - IF_TYPE_x for type of device
138 * @param dev - device index of particular type
139 * @param start - starting block number
140 * @param blkcnt - number of blocks to read
141 * @param blksz - size in bytes of each block
142 * @param buf - buffer to contain cached data
143 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100144 * Return: - 1 if block returned from cache, 0 otherwise.
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700145 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700146int blkcache_read(int iftype, int dev,
147 lbaint_t start, lbaint_t blkcnt,
148 unsigned long blksz, void *buffer);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700149
150/**
151 * blkcache_fill() - make data read from a block device available
152 * to the block cache
153 *
154 * @param iftype - IF_TYPE_x for type of device
155 * @param dev - device index of particular type
156 * @param start - starting block number
157 * @param blkcnt - number of blocks available
158 * @param blksz - size in bytes of each block
159 * @param buf - buffer containing data to cache
160 *
161 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700162void blkcache_fill(int iftype, int dev,
163 lbaint_t start, lbaint_t blkcnt,
164 unsigned long blksz, void const *buffer);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700165
166/**
167 * blkcache_invalidate() - discard the cache for a set of blocks
168 * because of a write or device (re)initialization.
169 *
170 * @param iftype - IF_TYPE_x for type of device
171 * @param dev - device index of particular type
172 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700173void blkcache_invalidate(int iftype, int dev);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700174
175/**
176 * blkcache_configure() - configure block cache
177 *
178 * @param blocks - maximum blocks per entry
179 * @param entries - maximum entries in cache
180 */
181void blkcache_configure(unsigned blocks, unsigned entries);
182
183/*
184 * statistics of the block cache
185 */
186struct block_cache_stats {
187 unsigned hits;
188 unsigned misses;
189 unsigned entries; /* current entry count */
190 unsigned max_blocks_per_entry;
191 unsigned max_entries;
192};
193
194/**
195 * get_blkcache_stats() - return statistics and reset
196 *
197 * @param stats - statistics are copied here
198 */
199void blkcache_stats(struct block_cache_stats *stats);
200
201#else
202
Eric Nelsonf201f232016-04-02 07:37:14 -0700203static inline int blkcache_read(int iftype, int dev,
204 lbaint_t start, lbaint_t blkcnt,
205 unsigned long blksz, void *buffer)
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700206{
207 return 0;
208}
209
Eric Nelsonf201f232016-04-02 07:37:14 -0700210static inline void blkcache_fill(int iftype, int dev,
211 lbaint_t start, lbaint_t blkcnt,
212 unsigned long blksz, void const *buffer) {}
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700213
Eric Nelsonf201f232016-04-02 07:37:14 -0700214static inline void blkcache_invalidate(int iftype, int dev) {}
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700215
216#endif
217
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600218#if CONFIG_IS_ENABLED(BLK)
Simon Glasscceee552016-02-29 15:25:55 -0700219struct udevice;
220
221/* Operations on block devices */
222struct blk_ops {
223 /**
224 * read() - read from a block device
225 *
226 * @dev: Device to read from
227 * @start: Start block number to read (0=first)
228 * @blkcnt: Number of blocks to read
229 * @buffer: Destination buffer for data read
230 * @return number of blocks read, or -ve error number (see the
231 * IS_ERR_VALUE() macro
232 */
233 unsigned long (*read)(struct udevice *dev, lbaint_t start,
234 lbaint_t blkcnt, void *buffer);
235
236 /**
237 * write() - write to a block device
238 *
239 * @dev: Device to write to
240 * @start: Start block number to write (0=first)
241 * @blkcnt: Number of blocks to write
242 * @buffer: Source buffer for data to write
243 * @return number of blocks written, or -ve error number (see the
244 * IS_ERR_VALUE() macro
245 */
246 unsigned long (*write)(struct udevice *dev, lbaint_t start,
247 lbaint_t blkcnt, const void *buffer);
248
249 /**
250 * erase() - erase a section of a block device
251 *
252 * @dev: Device to (partially) erase
253 * @start: Start block number to erase (0=first)
254 * @blkcnt: Number of blocks to erase
255 * @return number of blocks erased, or -ve error number (see the
256 * IS_ERR_VALUE() macro
257 */
258 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
259 lbaint_t blkcnt);
Simon Glass13c2c292016-05-01 13:52:30 -0600260
261 /**
262 * select_hwpart() - select a particular hardware partition
263 *
264 * Some devices (e.g. MMC) can support partitioning at the hardware
265 * level. This is quite separate from the normal idea of
266 * software-based partitions. MMC hardware partitions must be
267 * explicitly selected. Once selected only the region of the device
268 * covered by that partition is accessible.
269 *
270 * The MMC standard provides for two boot partitions (numbered 1 and 2),
271 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
272 *
273 * @desc: Block device to update
274 * @hwpart: Hardware partition number to select. 0 means the raw
275 * device, 1 is the first partition, 2 is the second, etc.
276 * @return 0 if OK, -ve on error
277 */
278 int (*select_hwpart)(struct udevice *dev, int hwpart);
Simon Glasscceee552016-02-29 15:25:55 -0700279};
280
281#define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
282
283/*
284 * These functions should take struct udevice instead of struct blk_desc,
285 * but this is convenient for migration to driver model. Add a 'd' prefix
286 * to the function operations, so that blk_read(), etc. can be reserved for
287 * functions with the correct arguments.
288 */
289unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
290 lbaint_t blkcnt, void *buffer);
291unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
292 lbaint_t blkcnt, const void *buffer);
293unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
294 lbaint_t blkcnt);
295
296/**
Simon Glassd5d4c102017-04-23 20:02:05 -0600297 * blk_find_device() - Find a block device
298 *
299 * This function does not activate the device. The device will be returned
300 * whether or not it is activated.
301 *
302 * @if_type: Interface type (enum if_type_t)
303 * @devnum: Device number (specific to each interface type)
304 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100305 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glassd5d4c102017-04-23 20:02:05 -0600306 */
307int blk_find_device(int if_type, int devnum, struct udevice **devp);
308
309/**
Simon Glasscceee552016-02-29 15:25:55 -0700310 * blk_get_device() - Find and probe a block device ready for use
311 *
312 * @if_type: Interface type (enum if_type_t)
313 * @devnum: Device number (specific to each interface type)
314 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100315 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700316 */
317int blk_get_device(int if_type, int devnum, struct udevice **devp);
318
319/**
320 * blk_first_device() - Find the first device for a given interface
321 *
322 * The device is probed ready for use
323 *
324 * @devnum: Device number (specific to each interface type)
325 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100326 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700327 */
328int blk_first_device(int if_type, struct udevice **devp);
329
330/**
331 * blk_next_device() - Find the next device for a given interface
332 *
333 * This can be called repeatedly after blk_first_device() to iterate through
334 * all devices of the given interface type.
335 *
336 * The device is probed ready for use
337 *
338 * @devp: On entry, the previous device returned. On exit, the next
339 * device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100340 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700341 */
342int blk_next_device(struct udevice **devp);
343
344/**
345 * blk_create_device() - Create a new block device
346 *
347 * @parent: Parent of the new device
348 * @drv_name: Driver name to use for the block device
349 * @name: Name for the device
350 * @if_type: Interface type (enum if_type_t)
Simon Glassd089ba32016-05-01 11:36:28 -0600351 * @devnum: Device number, specific to the interface type, or -1 to
352 * allocate the next available number
Simon Glasscceee552016-02-29 15:25:55 -0700353 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200354 * @lba: Total number of blocks of the device
Simon Glasscceee552016-02-29 15:25:55 -0700355 * @devp: the new device (which has not been probed)
356 */
357int blk_create_device(struct udevice *parent, const char *drv_name,
358 const char *name, int if_type, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200359 lbaint_t lba, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700360
361/**
Simon Glass966b6952016-05-01 11:36:29 -0600362 * blk_create_devicef() - Create a new named block device
363 *
364 * @parent: Parent of the new device
365 * @drv_name: Driver name to use for the block device
366 * @name: Name for the device (parent name is prepended)
367 * @if_type: Interface type (enum if_type_t)
368 * @devnum: Device number, specific to the interface type, or -1 to
369 * allocate the next available number
370 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200371 * @lba: Total number of blocks of the device
Simon Glass966b6952016-05-01 11:36:29 -0600372 * @devp: the new device (which has not been probed)
373 */
374int blk_create_devicef(struct udevice *parent, const char *drv_name,
375 const char *name, int if_type, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200376 lbaint_t lba, struct udevice **devp);
Simon Glass966b6952016-05-01 11:36:29 -0600377
378/**
AKASHI Takahiro3e32dbe2021-12-10 15:49:29 +0900379 * blk_probe_or_unbind() - Try to probe
380 *
381 * Try to probe the device, primarily for enumerating partitions.
382 * If it fails, the device itself is unbound since it means that it won't
383 * work any more.
384 *
385 * @dev: The device to probe
386 * Return: 0 if OK, -ve on error
387 */
388int blk_probe_or_unbind(struct udevice *dev);
389
390/**
Simon Glasscceee552016-02-29 15:25:55 -0700391 * blk_unbind_all() - Unbind all device of the given interface type
392 *
393 * The devices are removed and then unbound.
394 *
395 * @if_type: Interface type to unbind
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100396 * Return: 0 if OK, -ve on error
Simon Glasscceee552016-02-29 15:25:55 -0700397 */
398int blk_unbind_all(int if_type);
399
Simon Glassd089ba32016-05-01 11:36:28 -0600400/**
401 * blk_find_max_devnum() - find the maximum device number for an interface type
402 *
403 * Finds the last allocated device number for an interface type @if_type. The
404 * next number is safe to use for a newly allocated device.
405 *
406 * @if_type: Interface type to scan
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100407 * Return: maximum device number found, or -ENODEV if none, or other -ve on
Simon Glassd089ba32016-05-01 11:36:28 -0600408 * error
409 */
410int blk_find_max_devnum(enum if_type if_type);
411
Simon Glass13c2c292016-05-01 13:52:30 -0600412/**
Bin Mengfd5eda72018-10-15 02:21:09 -0700413 * blk_next_free_devnum() - get the next device number for an interface type
414 *
415 * Finds the next number that is safe to use for a newly allocated device for
416 * an interface type @if_type.
417 *
418 * @if_type: Interface type to scan
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100419 * Return: next device number safe to use, or -ve on error
Bin Mengfd5eda72018-10-15 02:21:09 -0700420 */
421int blk_next_free_devnum(enum if_type if_type);
422
423/**
Simon Glass13c2c292016-05-01 13:52:30 -0600424 * blk_select_hwpart() - select a hardware partition
425 *
426 * Select a hardware partition if the device supports it (typically MMC does)
427 *
428 * @dev: Device to update
429 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100430 * Return: 0 if OK, -ve on error
Simon Glass13c2c292016-05-01 13:52:30 -0600431 */
432int blk_select_hwpart(struct udevice *dev, int hwpart);
433
Tom Rini7c41d142017-06-10 10:01:05 -0400434/**
435 * blk_get_from_parent() - obtain a block device by looking up its parent
436 *
437 * All devices with
438 */
439int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
440
Tien Fong Chee10378522018-07-06 16:26:36 +0800441/**
Simon Glassf3086cf2022-04-24 23:31:03 -0600442 * blk_get_devtype() - Get the device type of a block device
443 *
444 * @dev: Block device to check
445 * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc"
446 */
447const char *blk_get_devtype(struct udevice *dev);
448
449/**
Tien Fong Chee10378522018-07-06 16:26:36 +0800450 * blk_get_by_device() - Get the block device descriptor for the given device
451 * @dev: Instance of a storage device
452 *
453 * Return: With block device descriptor on success , NULL if there is no such
454 * block device.
455 */
456struct blk_desc *blk_get_by_device(struct udevice *dev);
457
Simon Glasscceee552016-02-29 15:25:55 -0700458#else
459#include <errno.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -0700460/*
461 * These functions should take struct udevice instead of struct blk_desc,
462 * but this is convenient for migration to driver model. Add a 'd' prefix
463 * to the function operations, so that blk_read(), etc. can be reserved for
464 * functions with the correct arguments.
465 */
466static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
467 lbaint_t blkcnt, void *buffer)
468{
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700469 ulong blks_read;
470 if (blkcache_read(block_dev->if_type, block_dev->devnum,
471 start, blkcnt, block_dev->blksz, buffer))
472 return blkcnt;
473
Simon Glass2ee8ada2016-02-29 15:25:52 -0700474 /*
475 * We could check if block_read is NULL and return -ENOSYS. But this
476 * bloats the code slightly (cause some board to fail to build), and
477 * it would be an error to try an operation that does not exist.
478 */
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700479 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
480 if (blks_read == blkcnt)
481 blkcache_fill(block_dev->if_type, block_dev->devnum,
482 start, blkcnt, block_dev->blksz, buffer);
483
484 return blks_read;
Simon Glass2ee8ada2016-02-29 15:25:52 -0700485}
486
487static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
488 lbaint_t blkcnt, const void *buffer)
489{
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700490 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700491 return block_dev->block_write(block_dev, start, blkcnt, buffer);
492}
493
494static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
495 lbaint_t blkcnt)
496{
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700497 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700498 return block_dev->block_erase(block_dev, start, blkcnt);
499}
Simon Glass3bf2ab92016-05-01 11:36:03 -0600500
501/**
502 * struct blk_driver - Driver for block interface types
503 *
504 * This provides access to the block devices for each interface type. One
505 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
506 * type that is to be supported.
507 *
508 * @if_typename: Interface type name
509 * @if_type: Interface type
510 * @max_devs: Maximum number of devices supported
511 * @desc: Pointer to list of devices for this interface type,
512 * or NULL to use @get_dev() instead
513 */
514struct blk_driver {
515 const char *if_typename;
516 enum if_type if_type;
517 int max_devs;
518 struct blk_desc *desc;
519 /**
520 * get_dev() - get a pointer to a block device given its number
521 *
522 * Each interface allocates its own devices and typically
523 * struct blk_desc is contained with the interface's data structure.
524 * There is no global numbering for block devices. This method allows
525 * the device for an interface type to be obtained when @desc is NULL.
526 *
527 * @devnum: Device number (0 for first device on that interface,
528 * 1 for second, etc.
529 * @descp: Returns pointer to the block device on success
530 * @return 0 if OK, -ve on error
531 */
532 int (*get_dev)(int devnum, struct blk_desc **descp);
533
534 /**
535 * select_hwpart() - Select a hardware partition
536 *
537 * Some devices (e.g. MMC) can support partitioning at the hardware
538 * level. This is quite separate from the normal idea of
539 * software-based partitions. MMC hardware partitions must be
540 * explicitly selected. Once selected only the region of the device
541 * covered by that partition is accessible.
542 *
543 * The MMC standard provides for two boot partitions (numbered 1 and 2),
544 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
545 * Partition 0 is the main user-data partition.
546 *
547 * @desc: Block device descriptor
548 * @hwpart: Hardware partition number to select. 0 means the main
549 * user-data partition, 1 is the first partition, 2 is
550 * the second, etc.
551 * @return 0 if OK, other value for an error
552 */
553 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
554};
555
556/*
557 * Declare a new U-Boot legacy block driver. New drivers should use driver
558 * model (UCLASS_BLK).
559 */
560#define U_BOOT_LEGACY_BLK(__name) \
561 ll_entry_declare(struct blk_driver, __name, blk_driver)
562
563struct blk_driver *blk_driver_lookup_type(int if_type);
564
Simon Glasscceee552016-02-29 15:25:55 -0700565#endif /* !CONFIG_BLK */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700566
Simon Glass3bf2ab92016-05-01 11:36:03 -0600567/**
568 * blk_get_devnum_by_typename() - Get a block device by type and number
569 *
570 * This looks through the available block devices of the given type, returning
571 * the one with the given @devnum.
572 *
573 * @if_type: Block device type
574 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100575 * Return: point to block device descriptor, or NULL if not found
Simon Glass3bf2ab92016-05-01 11:36:03 -0600576 */
577struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
578
579/**
580 * blk_get_devnum_by_type() - Get a block device by type name, and number
581 *
582 * This looks up the block device type based on @if_typename, then calls
583 * blk_get_devnum_by_type().
584 *
585 * @if_typename: Block device type name
586 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100587 * Return: point to block device descriptor, or NULL if not found
Simon Glass3bf2ab92016-05-01 11:36:03 -0600588 */
589struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
590 int devnum);
591
592/**
593 * blk_dselect_hwpart() - select a hardware partition
594 *
595 * This selects a hardware partition (such as is supported by MMC). The block
596 * device size may change as this effectively points the block device to a
597 * partition at the hardware level. See the select_hwpart() method above.
598 *
599 * @desc: Block device descriptor for the device to select
600 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100601 * Return: 0 if OK, -ve on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600602 */
603int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
604
605/**
606 * blk_list_part() - list the partitions for block devices of a given type
607 *
608 * This looks up the partition type for each block device of type @if_type,
609 * then displays a list of partitions.
610 *
611 * @if_type: Block device type
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100612 * Return: 0 if OK, -ENODEV if there is none of that type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600613 */
614int blk_list_part(enum if_type if_type);
615
616/**
617 * blk_list_devices() - list the block devices of a given type
618 *
619 * This lists each block device of the type @if_type, showing the capacity
620 * as well as type-specific information.
621 *
622 * @if_type: Block device type
623 */
624void blk_list_devices(enum if_type if_type);
625
626/**
627 * blk_show_device() - show information about a given block device
628 *
629 * This shows the block device capacity as well as type-specific information.
630 *
631 * @if_type: Block device type
632 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100633 * Return: 0 if OK, -ENODEV for invalid device number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600634 */
635int blk_show_device(enum if_type if_type, int devnum);
636
637/**
638 * blk_print_device_num() - show information about a given block device
639 *
640 * This is similar to blk_show_device() but returns an error if the block
641 * device type is unknown.
642 *
643 * @if_type: Block device type
644 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100645 * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
Simon Glass3bf2ab92016-05-01 11:36:03 -0600646 * device is not connected
647 */
648int blk_print_device_num(enum if_type if_type, int devnum);
649
650/**
651 * blk_print_part_devnum() - print the partition information for a device
652 *
653 * @if_type: Block device type
654 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100655 * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
Simon Glass3bf2ab92016-05-01 11:36:03 -0600656 * the interface type is not supported, other -ve on other error
657 */
658int blk_print_part_devnum(enum if_type if_type, int devnum);
659
660/**
661 * blk_read_devnum() - read blocks from a device
662 *
663 * @if_type: Block device type
664 * @devnum: Device number
665 * @blkcnt: Number of blocks to read
666 * @buffer: Address to write data to
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100667 * Return: number of blocks read, or -ve error number on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600668 */
669ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
670 lbaint_t blkcnt, void *buffer);
671
672/**
673 * blk_write_devnum() - write blocks to a device
674 *
675 * @if_type: Block device type
676 * @devnum: Device number
677 * @blkcnt: Number of blocks to write
678 * @buffer: Address to read data from
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100679 * Return: number of blocks written, or -ve error number on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600680 */
681ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
682 lbaint_t blkcnt, const void *buffer);
683
684/**
685 * blk_select_hwpart_devnum() - select a hardware partition
686 *
687 * This is similar to blk_dselect_hwpart() but it looks up the interface and
688 * device number.
689 *
690 * @if_type: Block device type
691 * @devnum: Device number
692 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100693 * Return: 0 if OK, -ve on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600694 */
695int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
696
Simon Glass85af5a42017-07-29 11:34:53 -0600697/**
698 * blk_get_if_type_name() - Get the name of an interface type
699 *
700 * @if_type: Interface type to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100701 * Return: name of interface, or NULL if none
Simon Glass85af5a42017-07-29 11:34:53 -0600702 */
703const char *blk_get_if_type_name(enum if_type if_type);
704
Simon Glassbf2e7952017-07-29 11:34:54 -0600705/**
706 * blk_common_cmd() - handle common commands with block devices
707 *
708 * @args: Number of arguments to the command (argv[0] is the command itself)
709 * @argv: Command arguments
710 * @if_type: Interface type
711 * @cur_devnump: Current device number for this interface type
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100712 * Return: 0 if OK, CMD_RET_ERROR on error
Simon Glassbf2e7952017-07-29 11:34:54 -0600713 */
Simon Glassed38aef2020-05-10 11:40:03 -0600714int blk_common_cmd(int argc, char *const argv[], enum if_type if_type,
Simon Glassbf2e7952017-07-29 11:34:54 -0600715 int *cur_devnump);
716
Simon Glassfc7a7442021-07-05 16:32:59 -0600717enum blk_flag_t {
718 BLKF_FIXED = 1 << 0,
719 BLKF_REMOVABLE = 1 << 1,
720 BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE,
721};
722
723/**
724 * blk_first_device_err() - Get the first block device
725 *
726 * The device returned is probed if necessary, and ready for use
727 *
728 * @flags: Indicates type of device to return
729 * @devp: Returns pointer to the first device in that uclass, or NULL if none
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100730 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glassfc7a7442021-07-05 16:32:59 -0600731 */
732int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
733
734/**
735 * blk_next_device_err() - Get the next block device
736 *
737 * The device returned is probed if necessary, and ready for use
738 *
739 * @flags: Indicates type of device to return
740 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
741 * to the next device in the uclass if no error occurred, or -ENODEV if
742 * there is no next device.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100743 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glassfc7a7442021-07-05 16:32:59 -0600744 */
745int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
746
747/**
Simon Glass8e61f932022-02-28 12:08:35 -0700748 * blk_find_first() - Return the first matching block device
749 * @flags: Indicates type of device to return
750 * @devp: Returns pointer to device, or NULL on error
751 *
752 * The device is not prepared for use - this is an internal function.
753 * The function uclass_get_device_tail() can be used to probe the device.
754 *
755 * Note that some devices are considered removable until they have been probed
756 *
757 * @return 0 if found, -ENODEV if not found
758 */
759int blk_find_first(enum blk_flag_t flags, struct udevice **devp);
760
761/**
762 * blk_find_next() - Return the next matching block device
763 * @flags: Indicates type of device to return
764 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
765 * to the next device in the same uclass, or NULL if none
766 *
767 * The device is not prepared for use - this is an internal function.
768 * The function uclass_get_device_tail() can be used to probe the device.
769 *
770 * Note that some devices are considered removable until they have been probed
771 *
772 * @return 0 if found, -ENODEV if not found
773 */
774int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
775
776/**
777 * blk_foreach() - iterate through block devices
778 *
779 * This creates a for() loop which works through the available block devices in
780 * order from start to end.
781 *
782 * If for some reason the uclass cannot be found, this does nothing.
783 *
784 * @_flags: Indicates type of device to return
785 * @_pos: struct udevice * to hold the current device. Set to NULL when there
786 * are no more devices.
787 */
788#define blk_foreach(_flags, _pos) \
789 for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \
790 _ret = blk_find_next(_flags, &_pos))
791
792/**
Simon Glassfc7a7442021-07-05 16:32:59 -0600793 * blk_foreach_probe() - Helper function to iteration through block devices
794 *
795 * This creates a for() loop which works through the available devices in
796 * a uclass in order from start to end. Devices are probed if necessary,
797 * and ready for use.
798 *
799 * @flags: Indicates type of device to return
800 * @dev: struct udevice * to hold the current device. Set to NULL when there
801 * are no more devices.
802 */
803#define blk_foreach_probe(flags, pos) \
804 for (int _ret = blk_first_device_err(flags, &(pos)); \
805 !_ret && pos; \
806 _ret = blk_next_device_err(flags, &(pos)))
807
808/**
809 * blk_count_devices() - count the number of devices of a particular type
810 *
811 * @flags: Indicates type of device to find
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100812 * Return: number of devices matching those flags
Simon Glassfc7a7442021-07-05 16:32:59 -0600813 */
814int blk_count_devices(enum blk_flag_t flag);
815
Simon Glassac8162f2016-02-29 15:25:39 -0700816#endif