blob: cc30e3861fa084942230fd5f675b64c9052770ae [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass1a736612016-02-29 15:25:39 -07002/*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass1a736612016-02-29 15:25:39 -07005 */
6
7#ifndef BLK_H
8#define BLK_H
9
Peter Jonesff98cb92017-09-13 18:05:25 -040010#include <efi.h>
11
Simon Glass1a736612016-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 Glass96f37b02021-07-05 16:32:59 -060022struct udevice;
23
Simon Glassa51eb8d2022-08-11 19:34:45 -060024static inline bool blk_enabled(void)
25{
Simon Glass7f8967c2022-08-11 19:34:48 -060026 return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_SPL_LEGACY_BLOCK);
Simon Glassa51eb8d2022-08-11 19:34:45 -060027}
28
Simon Glass1a736612016-02-29 15:25:39 -070029/* Interface types: */
Simon Glass5ec4f1a2016-02-29 15:25:40 -070030enum if_type {
31 IF_TYPE_UNKNOWN = 0,
32 IF_TYPE_IDE,
33 IF_TYPE_SCSI,
Simon Glass5ec4f1a2016-02-29 15:25:40 -070034 IF_TYPE_USB,
Simon Glass5ec4f1a2016-02-29 15:25:40 -070035 IF_TYPE_MMC,
Simon Glass5ec4f1a2016-02-29 15:25:40 -070036 IF_TYPE_SATA,
37 IF_TYPE_HOST,
Zhikang Zhangffab6942017-08-03 02:30:56 -070038 IF_TYPE_NVME,
Simon Glass2abd8d12021-12-04 08:56:30 -070039 IF_TYPE_EFI_LOADER,
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +030040 IF_TYPE_PVBLOCK,
Tuomas Tynkkynen4ad54ec2018-10-15 02:21:10 -070041 IF_TYPE_VIRTIO,
Simon Glass42b7f422021-12-04 08:56:31 -070042 IF_TYPE_EFI_MEDIA,
Simon Glass5ec4f1a2016-02-29 15:25:40 -070043
44 IF_TYPE_COUNT, /* Number of interface types */
45};
Simon Glass1a736612016-02-29 15:25:39 -070046
Bin Mengeb81b1a2017-09-10 05:12:50 -070047#define BLK_VEN_SIZE 40
48#define BLK_PRD_SIZE 20
49#define BLK_REV_SIZE 8
50
Masahisa Kojimace3dbc52021-10-26 17:27:25 +090051#define PART_FORMAT_PCAT 0x1
52#define PART_FORMAT_GPT 0x2
53
Simon Glass09d71aa2016-02-29 15:25:55 -070054/*
Peter Jonesff98cb92017-09-13 18:05:25 -040055 * Identifies the partition table type (ie. MBR vs GPT GUID) signature
56 */
57enum sig_type {
58 SIG_TYPE_NONE,
59 SIG_TYPE_MBR,
60 SIG_TYPE_GUID,
61
62 SIG_TYPE_COUNT /* Number of signature types */
63};
64
65/*
Simon Glass09d71aa2016-02-29 15:25:55 -070066 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
Simon Glasscaa4daa2020-12-03 16:55:18 -070067 * with dev_get_uclass_plat(dev)
Simon Glass09d71aa2016-02-29 15:25:55 -070068 */
Simon Glass1a736612016-02-29 15:25:39 -070069struct blk_desc {
Simon Glass09d71aa2016-02-29 15:25:55 -070070 /*
71 * TODO: With driver model we should be able to use the parent
72 * device's uclass instead.
73 */
Simon Glass5ec4f1a2016-02-29 15:25:40 -070074 enum if_type if_type; /* type of the interface */
Simon Glassbcce53d2016-02-29 15:25:51 -070075 int devnum; /* device number */
Simon Glass1a736612016-02-29 15:25:39 -070076 unsigned char part_type; /* partition type */
77 unsigned char target; /* target SCSI ID */
78 unsigned char lun; /* target LUN */
79 unsigned char hwpart; /* HW partition, e.g. for eMMC */
80 unsigned char type; /* device type */
81 unsigned char removable; /* removable device */
82#ifdef CONFIG_LBA48
83 /* device can use 48bit addr (ATA/ATAPI v7) */
84 unsigned char lba48;
85#endif
86 lbaint_t lba; /* number of blocks */
87 unsigned long blksz; /* block size */
88 int log2blksz; /* for convenience: log2(blksz) */
Bin Mengeb81b1a2017-09-10 05:12:50 -070089 char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
90 char product[BLK_PRD_SIZE + 1]; /* device product number */
91 char revision[BLK_REV_SIZE + 1]; /* firmware revision */
Peter Jonesff98cb92017-09-13 18:05:25 -040092 enum sig_type sig_type; /* Partition table signature type */
93 union {
94 uint32_t mbr_sig; /* MBR integer signature */
95 efi_guid_t guid_sig; /* GPT GUID Signature */
96 };
Simon Glassc4d660d2017-07-04 13:31:19 -060097#if CONFIG_IS_ENABLED(BLK)
Simon Glassb6694a32016-05-01 13:52:33 -060098 /*
99 * For now we have a few functions which take struct blk_desc as a
100 * parameter. This field allows them to look up the associated
101 * device. Once these functions are removed we can drop this field.
102 */
Simon Glass09d71aa2016-02-29 15:25:55 -0700103 struct udevice *bdev;
104#else
Simon Glass1a736612016-02-29 15:25:39 -0700105 unsigned long (*block_read)(struct blk_desc *block_dev,
106 lbaint_t start,
107 lbaint_t blkcnt,
108 void *buffer);
109 unsigned long (*block_write)(struct blk_desc *block_dev,
110 lbaint_t start,
111 lbaint_t blkcnt,
112 const void *buffer);
113 unsigned long (*block_erase)(struct blk_desc *block_dev,
114 lbaint_t start,
115 lbaint_t blkcnt);
116 void *priv; /* driver private struct pointer */
Simon Glass09d71aa2016-02-29 15:25:55 -0700117#endif
Simon Glass1a736612016-02-29 15:25:39 -0700118};
119
120#define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
121#define PAD_TO_BLOCKSIZE(size, blk_desc) \
122 (PAD_SIZE(size, blk_desc->blksz))
123
Adam Ford6fef62c2018-06-11 17:17:48 -0500124#if CONFIG_IS_ENABLED(BLOCK_CACHE)
Angelo Durgehello1526bcc2020-01-21 10:37:27 +0100125
126/**
127 * blkcache_init() - initialize the block cache list pointers
128 */
129int blkcache_init(void);
130
Eric Nelsone40cf342016-03-28 10:05:44 -0700131/**
132 * blkcache_read() - attempt to read a set of blocks from cache
133 *
134 * @param iftype - IF_TYPE_x for type of device
135 * @param dev - device index of particular type
136 * @param start - starting block number
137 * @param blkcnt - number of blocks to read
138 * @param blksz - size in bytes of each block
139 * @param buf - buffer to contain cached data
140 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100141 * Return: - 1 if block returned from cache, 0 otherwise.
Eric Nelsone40cf342016-03-28 10:05:44 -0700142 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700143int blkcache_read(int iftype, int dev,
144 lbaint_t start, lbaint_t blkcnt,
145 unsigned long blksz, void *buffer);
Eric Nelsone40cf342016-03-28 10:05:44 -0700146
147/**
148 * blkcache_fill() - make data read from a block device available
149 * to the block cache
150 *
151 * @param iftype - IF_TYPE_x for type of device
152 * @param dev - device index of particular type
153 * @param start - starting block number
154 * @param blkcnt - number of blocks available
155 * @param blksz - size in bytes of each block
156 * @param buf - buffer containing data to cache
157 *
158 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700159void blkcache_fill(int iftype, int dev,
160 lbaint_t start, lbaint_t blkcnt,
161 unsigned long blksz, void const *buffer);
Eric Nelsone40cf342016-03-28 10:05:44 -0700162
163/**
164 * blkcache_invalidate() - discard the cache for a set of blocks
165 * because of a write or device (re)initialization.
166 *
167 * @param iftype - IF_TYPE_x for type of device
168 * @param dev - device index of particular type
169 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700170void blkcache_invalidate(int iftype, int dev);
Eric Nelsone40cf342016-03-28 10:05:44 -0700171
172/**
173 * blkcache_configure() - configure block cache
174 *
175 * @param blocks - maximum blocks per entry
176 * @param entries - maximum entries in cache
177 */
178void blkcache_configure(unsigned blocks, unsigned entries);
179
180/*
181 * statistics of the block cache
182 */
183struct block_cache_stats {
184 unsigned hits;
185 unsigned misses;
186 unsigned entries; /* current entry count */
187 unsigned max_blocks_per_entry;
188 unsigned max_entries;
189};
190
191/**
192 * get_blkcache_stats() - return statistics and reset
193 *
194 * @param stats - statistics are copied here
195 */
196void blkcache_stats(struct block_cache_stats *stats);
197
198#else
199
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700200static inline int blkcache_read(int iftype, int dev,
201 lbaint_t start, lbaint_t blkcnt,
202 unsigned long blksz, void *buffer)
Eric Nelsone40cf342016-03-28 10:05:44 -0700203{
204 return 0;
205}
206
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700207static inline void blkcache_fill(int iftype, int dev,
208 lbaint_t start, lbaint_t blkcnt,
209 unsigned long blksz, void const *buffer) {}
Eric Nelsone40cf342016-03-28 10:05:44 -0700210
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700211static inline void blkcache_invalidate(int iftype, int dev) {}
Eric Nelsone40cf342016-03-28 10:05:44 -0700212
213#endif
214
Simon Glassc4d660d2017-07-04 13:31:19 -0600215#if CONFIG_IS_ENABLED(BLK)
Simon Glass09d71aa2016-02-29 15:25:55 -0700216struct udevice;
217
218/* Operations on block devices */
219struct blk_ops {
220 /**
221 * read() - read from a block device
222 *
223 * @dev: Device to read from
224 * @start: Start block number to read (0=first)
225 * @blkcnt: Number of blocks to read
226 * @buffer: Destination buffer for data read
227 * @return number of blocks read, or -ve error number (see the
228 * IS_ERR_VALUE() macro
229 */
230 unsigned long (*read)(struct udevice *dev, lbaint_t start,
231 lbaint_t blkcnt, void *buffer);
232
233 /**
234 * write() - write to a block device
235 *
236 * @dev: Device to write to
237 * @start: Start block number to write (0=first)
238 * @blkcnt: Number of blocks to write
239 * @buffer: Source buffer for data to write
240 * @return number of blocks written, or -ve error number (see the
241 * IS_ERR_VALUE() macro
242 */
243 unsigned long (*write)(struct udevice *dev, lbaint_t start,
244 lbaint_t blkcnt, const void *buffer);
245
246 /**
247 * erase() - erase a section of a block device
248 *
249 * @dev: Device to (partially) erase
250 * @start: Start block number to erase (0=first)
251 * @blkcnt: Number of blocks to erase
252 * @return number of blocks erased, or -ve error number (see the
253 * IS_ERR_VALUE() macro
254 */
255 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
256 lbaint_t blkcnt);
Simon Glasscd0fb552016-05-01 13:52:30 -0600257
258 /**
259 * select_hwpart() - select a particular hardware partition
260 *
261 * Some devices (e.g. MMC) can support partitioning at the hardware
262 * level. This is quite separate from the normal idea of
263 * software-based partitions. MMC hardware partitions must be
264 * explicitly selected. Once selected only the region of the device
265 * covered by that partition is accessible.
266 *
267 * The MMC standard provides for two boot partitions (numbered 1 and 2),
268 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
269 *
270 * @desc: Block device to update
271 * @hwpart: Hardware partition number to select. 0 means the raw
272 * device, 1 is the first partition, 2 is the second, etc.
273 * @return 0 if OK, -ve on error
274 */
275 int (*select_hwpart)(struct udevice *dev, int hwpart);
Simon Glass09d71aa2016-02-29 15:25:55 -0700276};
277
278#define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
279
280/*
281 * These functions should take struct udevice instead of struct blk_desc,
282 * but this is convenient for migration to driver model. Add a 'd' prefix
283 * to the function operations, so that blk_read(), etc. can be reserved for
284 * functions with the correct arguments.
285 */
286unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
287 lbaint_t blkcnt, void *buffer);
288unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
289 lbaint_t blkcnt, const void *buffer);
290unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
291 lbaint_t blkcnt);
292
293/**
Simon Glass61392812017-04-23 20:02:05 -0600294 * blk_find_device() - Find a block device
295 *
296 * This function does not activate the device. The device will be returned
297 * whether or not it is activated.
298 *
299 * @if_type: Interface type (enum if_type_t)
300 * @devnum: Device number (specific to each interface type)
301 * @devp: the device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100302 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glass61392812017-04-23 20:02:05 -0600303 */
304int blk_find_device(int if_type, int devnum, struct udevice **devp);
305
306/**
Simon Glass09d71aa2016-02-29 15:25:55 -0700307 * blk_get_device() - Find and probe a block device ready for use
308 *
309 * @if_type: Interface type (enum if_type_t)
310 * @devnum: Device number (specific to each interface type)
311 * @devp: the device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100312 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glass09d71aa2016-02-29 15:25:55 -0700313 */
314int blk_get_device(int if_type, int devnum, struct udevice **devp);
315
316/**
317 * blk_first_device() - Find the first device for a given interface
318 *
319 * The device is probed ready for use
320 *
321 * @devnum: Device number (specific to each interface type)
322 * @devp: the device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100323 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glass09d71aa2016-02-29 15:25:55 -0700324 */
325int blk_first_device(int if_type, struct udevice **devp);
326
327/**
328 * blk_next_device() - Find the next device for a given interface
329 *
330 * This can be called repeatedly after blk_first_device() to iterate through
331 * all devices of the given interface type.
332 *
333 * The device is probed ready for use
334 *
335 * @devp: On entry, the previous device returned. On exit, the next
336 * device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100337 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glass09d71aa2016-02-29 15:25:55 -0700338 */
339int blk_next_device(struct udevice **devp);
340
341/**
342 * blk_create_device() - Create a new block device
343 *
344 * @parent: Parent of the new device
345 * @drv_name: Driver name to use for the block device
346 * @name: Name for the device
347 * @if_type: Interface type (enum if_type_t)
Simon Glass52138fd2016-05-01 11:36:28 -0600348 * @devnum: Device number, specific to the interface type, or -1 to
349 * allocate the next available number
Simon Glass09d71aa2016-02-29 15:25:55 -0700350 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200351 * @lba: Total number of blocks of the device
Simon Glass09d71aa2016-02-29 15:25:55 -0700352 * @devp: the new device (which has not been probed)
353 */
354int blk_create_device(struct udevice *parent, const char *drv_name,
355 const char *name, int if_type, int devnum, int blksz,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200356 lbaint_t lba, struct udevice **devp);
Simon Glass09d71aa2016-02-29 15:25:55 -0700357
358/**
Simon Glass9107c972016-05-01 11:36:29 -0600359 * blk_create_devicef() - Create a new named block device
360 *
361 * @parent: Parent of the new device
362 * @drv_name: Driver name to use for the block device
363 * @name: Name for the device (parent name is prepended)
364 * @if_type: Interface type (enum if_type_t)
365 * @devnum: Device number, specific to the interface type, or -1 to
366 * allocate the next available number
367 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200368 * @lba: Total number of blocks of the device
Simon Glass9107c972016-05-01 11:36:29 -0600369 * @devp: the new device (which has not been probed)
370 */
371int blk_create_devicef(struct udevice *parent, const char *drv_name,
372 const char *name, int if_type, int devnum, int blksz,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200373 lbaint_t lba, struct udevice **devp);
Simon Glass9107c972016-05-01 11:36:29 -0600374
375/**
AKASHI Takahiro19b241c2021-12-10 15:49:29 +0900376 * blk_probe_or_unbind() - Try to probe
377 *
378 * Try to probe the device, primarily for enumerating partitions.
379 * If it fails, the device itself is unbound since it means that it won't
380 * work any more.
381 *
382 * @dev: The device to probe
383 * Return: 0 if OK, -ve on error
384 */
385int blk_probe_or_unbind(struct udevice *dev);
386
387/**
Simon Glass09d71aa2016-02-29 15:25:55 -0700388 * blk_unbind_all() - Unbind all device of the given interface type
389 *
390 * The devices are removed and then unbound.
391 *
392 * @if_type: Interface type to unbind
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100393 * Return: 0 if OK, -ve on error
Simon Glass09d71aa2016-02-29 15:25:55 -0700394 */
395int blk_unbind_all(int if_type);
396
Simon Glass52138fd2016-05-01 11:36:28 -0600397/**
398 * blk_find_max_devnum() - find the maximum device number for an interface type
399 *
400 * Finds the last allocated device number for an interface type @if_type. The
401 * next number is safe to use for a newly allocated device.
402 *
403 * @if_type: Interface type to scan
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100404 * Return: maximum device number found, or -ENODEV if none, or other -ve on
Simon Glass52138fd2016-05-01 11:36:28 -0600405 * error
406 */
407int blk_find_max_devnum(enum if_type if_type);
408
Simon Glasscd0fb552016-05-01 13:52:30 -0600409/**
Bin Mengc879eeb2018-10-15 02:21:09 -0700410 * blk_next_free_devnum() - get the next device number for an interface type
411 *
412 * Finds the next number that is safe to use for a newly allocated device for
413 * an interface type @if_type.
414 *
415 * @if_type: Interface type to scan
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100416 * Return: next device number safe to use, or -ve on error
Bin Mengc879eeb2018-10-15 02:21:09 -0700417 */
418int blk_next_free_devnum(enum if_type if_type);
419
420/**
Simon Glasscd0fb552016-05-01 13:52:30 -0600421 * blk_select_hwpart() - select a hardware partition
422 *
423 * Select a hardware partition if the device supports it (typically MMC does)
424 *
425 * @dev: Device to update
426 * @hwpart: Partition number to select
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100427 * Return: 0 if OK, -ve on error
Simon Glasscd0fb552016-05-01 13:52:30 -0600428 */
429int blk_select_hwpart(struct udevice *dev, int hwpart);
430
Tom Rini4bdb49a2017-06-10 10:01:05 -0400431/**
432 * blk_get_from_parent() - obtain a block device by looking up its parent
433 *
434 * All devices with
435 */
436int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
437
Tien Fong Cheebc53d262018-07-06 16:26:36 +0800438/**
Simon Glass87571b72022-04-24 23:31:03 -0600439 * blk_get_devtype() - Get the device type of a block device
440 *
441 * @dev: Block device to check
442 * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc"
443 */
444const char *blk_get_devtype(struct udevice *dev);
445
446/**
Tien Fong Cheebc53d262018-07-06 16:26:36 +0800447 * blk_get_by_device() - Get the block device descriptor for the given device
448 * @dev: Instance of a storage device
449 *
450 * Return: With block device descriptor on success , NULL if there is no such
451 * block device.
452 */
453struct blk_desc *blk_get_by_device(struct udevice *dev);
454
Simon Glass09d71aa2016-02-29 15:25:55 -0700455#else
456#include <errno.h>
Simon Glass2a981dc2016-02-29 15:25:52 -0700457/*
458 * These functions should take struct udevice instead of struct blk_desc,
459 * but this is convenient for migration to driver model. Add a 'd' prefix
460 * to the function operations, so that blk_read(), etc. can be reserved for
461 * functions with the correct arguments.
462 */
463static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
464 lbaint_t blkcnt, void *buffer)
465{
Eric Nelsone40cf342016-03-28 10:05:44 -0700466 ulong blks_read;
467 if (blkcache_read(block_dev->if_type, block_dev->devnum,
468 start, blkcnt, block_dev->blksz, buffer))
469 return blkcnt;
470
Simon Glass2a981dc2016-02-29 15:25:52 -0700471 /*
472 * We could check if block_read is NULL and return -ENOSYS. But this
473 * bloats the code slightly (cause some board to fail to build), and
474 * it would be an error to try an operation that does not exist.
475 */
Eric Nelsone40cf342016-03-28 10:05:44 -0700476 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
477 if (blks_read == blkcnt)
478 blkcache_fill(block_dev->if_type, block_dev->devnum,
479 start, blkcnt, block_dev->blksz, buffer);
480
481 return blks_read;
Simon Glass2a981dc2016-02-29 15:25:52 -0700482}
483
484static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
485 lbaint_t blkcnt, const void *buffer)
486{
Eric Nelsone40cf342016-03-28 10:05:44 -0700487 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glass2a981dc2016-02-29 15:25:52 -0700488 return block_dev->block_write(block_dev, start, blkcnt, buffer);
489}
490
491static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
492 lbaint_t blkcnt)
493{
Eric Nelsone40cf342016-03-28 10:05:44 -0700494 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glass2a981dc2016-02-29 15:25:52 -0700495 return block_dev->block_erase(block_dev, start, blkcnt);
496}
Simon Glass6eef6ea2016-05-01 11:36:03 -0600497
498/**
499 * struct blk_driver - Driver for block interface types
500 *
501 * This provides access to the block devices for each interface type. One
502 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
503 * type that is to be supported.
504 *
505 * @if_typename: Interface type name
506 * @if_type: Interface type
507 * @max_devs: Maximum number of devices supported
508 * @desc: Pointer to list of devices for this interface type,
509 * or NULL to use @get_dev() instead
510 */
511struct blk_driver {
512 const char *if_typename;
513 enum if_type if_type;
514 int max_devs;
515 struct blk_desc *desc;
516 /**
517 * get_dev() - get a pointer to a block device given its number
518 *
519 * Each interface allocates its own devices and typically
520 * struct blk_desc is contained with the interface's data structure.
521 * There is no global numbering for block devices. This method allows
522 * the device for an interface type to be obtained when @desc is NULL.
523 *
524 * @devnum: Device number (0 for first device on that interface,
525 * 1 for second, etc.
526 * @descp: Returns pointer to the block device on success
527 * @return 0 if OK, -ve on error
528 */
529 int (*get_dev)(int devnum, struct blk_desc **descp);
530
531 /**
532 * select_hwpart() - Select a hardware partition
533 *
534 * Some devices (e.g. MMC) can support partitioning at the hardware
535 * level. This is quite separate from the normal idea of
536 * software-based partitions. MMC hardware partitions must be
537 * explicitly selected. Once selected only the region of the device
538 * covered by that partition is accessible.
539 *
540 * The MMC standard provides for two boot partitions (numbered 1 and 2),
541 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
542 * Partition 0 is the main user-data partition.
543 *
544 * @desc: Block device descriptor
545 * @hwpart: Hardware partition number to select. 0 means the main
546 * user-data partition, 1 is the first partition, 2 is
547 * the second, etc.
548 * @return 0 if OK, other value for an error
549 */
550 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
551};
552
553/*
554 * Declare a new U-Boot legacy block driver. New drivers should use driver
555 * model (UCLASS_BLK).
556 */
557#define U_BOOT_LEGACY_BLK(__name) \
558 ll_entry_declare(struct blk_driver, __name, blk_driver)
559
560struct blk_driver *blk_driver_lookup_type(int if_type);
561
Simon Glass09d71aa2016-02-29 15:25:55 -0700562#endif /* !CONFIG_BLK */
Simon Glass2a981dc2016-02-29 15:25:52 -0700563
Simon Glass6eef6ea2016-05-01 11:36:03 -0600564/**
565 * blk_get_devnum_by_typename() - Get a block device by type and number
566 *
567 * This looks through the available block devices of the given type, returning
568 * the one with the given @devnum.
569 *
570 * @if_type: Block device type
571 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100572 * Return: point to block device descriptor, or NULL if not found
Simon Glass6eef6ea2016-05-01 11:36:03 -0600573 */
574struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
575
576/**
577 * blk_get_devnum_by_type() - Get a block device by type name, and number
578 *
579 * This looks up the block device type based on @if_typename, then calls
580 * blk_get_devnum_by_type().
581 *
582 * @if_typename: Block device type name
583 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100584 * Return: point to block device descriptor, or NULL if not found
Simon Glass6eef6ea2016-05-01 11:36:03 -0600585 */
586struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
587 int devnum);
588
589/**
590 * blk_dselect_hwpart() - select a hardware partition
591 *
592 * This selects a hardware partition (such as is supported by MMC). The block
593 * device size may change as this effectively points the block device to a
594 * partition at the hardware level. See the select_hwpart() method above.
595 *
596 * @desc: Block device descriptor for the device to select
597 * @hwpart: Partition number to select
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100598 * Return: 0 if OK, -ve on error
Simon Glass6eef6ea2016-05-01 11:36:03 -0600599 */
600int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
601
602/**
603 * blk_list_part() - list the partitions for block devices of a given type
604 *
605 * This looks up the partition type for each block device of type @if_type,
606 * then displays a list of partitions.
607 *
608 * @if_type: Block device type
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100609 * Return: 0 if OK, -ENODEV if there is none of that type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600610 */
611int blk_list_part(enum if_type if_type);
612
613/**
614 * blk_list_devices() - list the block devices of a given type
615 *
616 * This lists each block device of the type @if_type, showing the capacity
617 * as well as type-specific information.
618 *
619 * @if_type: Block device type
620 */
621void blk_list_devices(enum if_type if_type);
622
623/**
624 * blk_show_device() - show information about a given block device
625 *
626 * This shows the block device capacity as well as type-specific information.
627 *
628 * @if_type: Block device type
629 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100630 * Return: 0 if OK, -ENODEV for invalid device number
Simon Glass6eef6ea2016-05-01 11:36:03 -0600631 */
632int blk_show_device(enum if_type if_type, int devnum);
633
634/**
635 * blk_print_device_num() - show information about a given block device
636 *
637 * This is similar to blk_show_device() but returns an error if the block
638 * device type is unknown.
639 *
640 * @if_type: Block device type
641 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100642 * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
Simon Glass6eef6ea2016-05-01 11:36:03 -0600643 * device is not connected
644 */
645int blk_print_device_num(enum if_type if_type, int devnum);
646
647/**
648 * blk_print_part_devnum() - print the partition information for a device
649 *
650 * @if_type: Block device type
651 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100652 * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
Simon Glass6eef6ea2016-05-01 11:36:03 -0600653 * the interface type is not supported, other -ve on other error
654 */
655int blk_print_part_devnum(enum if_type if_type, int devnum);
656
657/**
658 * blk_read_devnum() - read blocks from a device
659 *
660 * @if_type: Block device type
661 * @devnum: Device number
662 * @blkcnt: Number of blocks to read
663 * @buffer: Address to write data to
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100664 * Return: number of blocks read, or -ve error number on error
Simon Glass6eef6ea2016-05-01 11:36:03 -0600665 */
666ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
667 lbaint_t blkcnt, void *buffer);
668
669/**
670 * blk_write_devnum() - write blocks to a device
671 *
672 * @if_type: Block device type
673 * @devnum: Device number
674 * @blkcnt: Number of blocks to write
675 * @buffer: Address to read data from
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100676 * Return: number of blocks written, or -ve error number on error
Simon Glass6eef6ea2016-05-01 11:36:03 -0600677 */
678ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
679 lbaint_t blkcnt, const void *buffer);
680
681/**
682 * blk_select_hwpart_devnum() - select a hardware partition
683 *
684 * This is similar to blk_dselect_hwpart() but it looks up the interface and
685 * device number.
686 *
687 * @if_type: Block device type
688 * @devnum: Device number
689 * @hwpart: Partition number to select
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100690 * Return: 0 if OK, -ve on error
Simon Glass6eef6ea2016-05-01 11:36:03 -0600691 */
692int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
693
Simon Glass6faa4ed2017-07-29 11:34:53 -0600694/**
695 * blk_get_if_type_name() - Get the name of an interface type
696 *
697 * @if_type: Interface type to check
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100698 * Return: name of interface, or NULL if none
Simon Glass6faa4ed2017-07-29 11:34:53 -0600699 */
700const char *blk_get_if_type_name(enum if_type if_type);
701
Simon Glass4395f662017-07-29 11:34:54 -0600702/**
703 * blk_common_cmd() - handle common commands with block devices
704 *
705 * @args: Number of arguments to the command (argv[0] is the command itself)
706 * @argv: Command arguments
707 * @if_type: Interface type
708 * @cur_devnump: Current device number for this interface type
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100709 * Return: 0 if OK, CMD_RET_ERROR on error
Simon Glass4395f662017-07-29 11:34:54 -0600710 */
Simon Glass09140112020-05-10 11:40:03 -0600711int blk_common_cmd(int argc, char *const argv[], enum if_type if_type,
Simon Glass4395f662017-07-29 11:34:54 -0600712 int *cur_devnump);
713
Simon Glass96f37b02021-07-05 16:32:59 -0600714enum blk_flag_t {
715 BLKF_FIXED = 1 << 0,
716 BLKF_REMOVABLE = 1 << 1,
717 BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE,
718};
719
720/**
721 * blk_first_device_err() - Get the first block device
722 *
723 * The device returned is probed if necessary, and ready for use
724 *
725 * @flags: Indicates type of device to return
726 * @devp: Returns pointer to the first device in that uclass, or NULL if none
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100727 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glass96f37b02021-07-05 16:32:59 -0600728 */
729int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
730
731/**
732 * blk_next_device_err() - Get the next block device
733 *
734 * The device returned is probed if necessary, and ready for use
735 *
736 * @flags: Indicates type of device to return
737 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
738 * to the next device in the uclass if no error occurred, or -ENODEV if
739 * there is no next device.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100740 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glass96f37b02021-07-05 16:32:59 -0600741 */
742int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
743
744/**
Simon Glass49e86682022-02-28 12:08:35 -0700745 * blk_find_first() - Return the first matching block device
746 * @flags: Indicates type of device to return
747 * @devp: Returns pointer to device, or NULL on error
748 *
749 * The device is not prepared for use - this is an internal function.
750 * The function uclass_get_device_tail() can be used to probe the device.
751 *
752 * Note that some devices are considered removable until they have been probed
753 *
754 * @return 0 if found, -ENODEV if not found
755 */
756int blk_find_first(enum blk_flag_t flags, struct udevice **devp);
757
758/**
759 * blk_find_next() - Return the next matching block device
760 * @flags: Indicates type of device to return
761 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
762 * to the next device in the same uclass, or NULL if none
763 *
764 * The device is not prepared for use - this is an internal function.
765 * The function uclass_get_device_tail() can be used to probe the device.
766 *
767 * Note that some devices are considered removable until they have been probed
768 *
769 * @return 0 if found, -ENODEV if not found
770 */
771int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
772
773/**
774 * blk_foreach() - iterate through block devices
775 *
776 * This creates a for() loop which works through the available block devices in
777 * order from start to end.
778 *
779 * If for some reason the uclass cannot be found, this does nothing.
780 *
781 * @_flags: Indicates type of device to return
782 * @_pos: struct udevice * to hold the current device. Set to NULL when there
783 * are no more devices.
784 */
785#define blk_foreach(_flags, _pos) \
786 for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \
787 _ret = blk_find_next(_flags, &_pos))
788
789/**
Simon Glass96f37b02021-07-05 16:32:59 -0600790 * blk_foreach_probe() - Helper function to iteration through block devices
791 *
792 * This creates a for() loop which works through the available devices in
793 * a uclass in order from start to end. Devices are probed if necessary,
794 * and ready for use.
795 *
796 * @flags: Indicates type of device to return
797 * @dev: struct udevice * to hold the current device. Set to NULL when there
798 * are no more devices.
799 */
800#define blk_foreach_probe(flags, pos) \
801 for (int _ret = blk_first_device_err(flags, &(pos)); \
802 !_ret && pos; \
803 _ret = blk_next_device_err(flags, &(pos)))
804
805/**
806 * blk_count_devices() - count the number of devices of a particular type
807 *
808 * @flags: Indicates type of device to find
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100809 * Return: number of devices matching those flags
Simon Glass96f37b02021-07-05 16:32:59 -0600810 */
811int blk_count_devices(enum blk_flag_t flag);
812
Simon Glass1a736612016-02-29 15:25:39 -0700813#endif