blob: 871922dcde0769d23df2aff80d25a8de48e1d17a [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
Simon Glassdbfa32c2022-08-11 19:34:59 -060010#include <dm/uclass-id.h>
Peter Jonesc27e1c12017-09-13 18:05:25 -040011#include <efi.h>
12
Simon Glassac8162f2016-02-29 15:25:39 -070013#ifdef CONFIG_SYS_64BIT_LBA
14typedef uint64_t lbaint_t;
15#define LBAFlength "ll"
16#else
17typedef ulong lbaint_t;
18#define LBAFlength "l"
19#endif
20#define LBAF "%" LBAFlength "x"
21#define LBAFU "%" LBAFlength "u"
22
Simon Glassfc7a7442021-07-05 16:32:59 -060023struct udevice;
24
Simon Glassf5ac3032022-08-11 19:34:45 -060025static inline bool blk_enabled(void)
26{
Simon Glass3bf7d7a2022-08-11 19:34:48 -060027 return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_SPL_LEGACY_BLOCK);
Simon Glassf5ac3032022-08-11 19:34:45 -060028}
29
Bin Mengcf406d22017-09-10 05:12:50 -070030#define BLK_VEN_SIZE 40
31#define BLK_PRD_SIZE 20
32#define BLK_REV_SIZE 8
33
Masahisa Kojima6460c3e2021-10-26 17:27:25 +090034#define PART_FORMAT_PCAT 0x1
35#define PART_FORMAT_GPT 0x2
36
Simon Glasscceee552016-02-29 15:25:55 -070037/*
Peter Jonesc27e1c12017-09-13 18:05:25 -040038 * Identifies the partition table type (ie. MBR vs GPT GUID) signature
39 */
40enum sig_type {
41 SIG_TYPE_NONE,
42 SIG_TYPE_MBR,
43 SIG_TYPE_GUID,
44
45 SIG_TYPE_COUNT /* Number of signature types */
46};
47
48/*
Simon Glasscceee552016-02-29 15:25:55 -070049 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
Simon Glass71fa5b42020-12-03 16:55:18 -070050 * with dev_get_uclass_plat(dev)
Simon Glasscceee552016-02-29 15:25:55 -070051 */
Simon Glassac8162f2016-02-29 15:25:39 -070052struct blk_desc {
Simon Glasscceee552016-02-29 15:25:55 -070053 /*
54 * TODO: With driver model we should be able to use the parent
55 * device's uclass instead.
56 */
Simon Glassfada3f92022-09-17 09:00:09 -060057 enum uclass_id uclass_id; /* type of the interface */
Simon Glass2f26fff2016-02-29 15:25:51 -070058 int devnum; /* device number */
Simon Glassac8162f2016-02-29 15:25:39 -070059 unsigned char part_type; /* partition type */
60 unsigned char target; /* target SCSI ID */
61 unsigned char lun; /* target LUN */
62 unsigned char hwpart; /* HW partition, e.g. for eMMC */
63 unsigned char type; /* device type */
64 unsigned char removable; /* removable device */
65#ifdef CONFIG_LBA48
66 /* device can use 48bit addr (ATA/ATAPI v7) */
67 unsigned char lba48;
68#endif
Simon Glass631db662023-04-25 10:54:35 -060069 unsigned char atapi; /* Use ATAPI protocol */
Simon Glassac8162f2016-02-29 15:25:39 -070070 lbaint_t lba; /* number of blocks */
71 unsigned long blksz; /* block size */
72 int log2blksz; /* for convenience: log2(blksz) */
Bin Mengcf406d22017-09-10 05:12:50 -070073 char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
74 char product[BLK_PRD_SIZE + 1]; /* device product number */
75 char revision[BLK_REV_SIZE + 1]; /* firmware revision */
Peter Jonesc27e1c12017-09-13 18:05:25 -040076 enum sig_type sig_type; /* Partition table signature type */
77 union {
78 uint32_t mbr_sig; /* MBR integer signature */
79 efi_guid_t guid_sig; /* GPT GUID Signature */
80 };
Simon Glass5f4bd8c2017-07-04 13:31:19 -060081#if CONFIG_IS_ENABLED(BLK)
Simon Glass8f5f7222016-05-01 13:52:33 -060082 /*
83 * For now we have a few functions which take struct blk_desc as a
84 * parameter. This field allows them to look up the associated
85 * device. Once these functions are removed we can drop this field.
86 */
Simon Glasscceee552016-02-29 15:25:55 -070087 struct udevice *bdev;
88#else
Simon Glassac8162f2016-02-29 15:25:39 -070089 unsigned long (*block_read)(struct blk_desc *block_dev,
90 lbaint_t start,
91 lbaint_t blkcnt,
92 void *buffer);
93 unsigned long (*block_write)(struct blk_desc *block_dev,
94 lbaint_t start,
95 lbaint_t blkcnt,
96 const void *buffer);
97 unsigned long (*block_erase)(struct blk_desc *block_dev,
98 lbaint_t start,
99 lbaint_t blkcnt);
100 void *priv; /* driver private struct pointer */
Simon Glasscceee552016-02-29 15:25:55 -0700101#endif
Simon Glassac8162f2016-02-29 15:25:39 -0700102};
103
104#define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
105#define PAD_TO_BLOCKSIZE(size, blk_desc) \
106 (PAD_SIZE(size, blk_desc->blksz))
107
Adam Fordd693fb92018-06-11 17:17:48 -0500108#if CONFIG_IS_ENABLED(BLOCK_CACHE)
Angelo Durgehello3d8e4c12020-01-21 10:37:27 +0100109
110/**
111 * blkcache_init() - initialize the block cache list pointers
112 */
113int blkcache_init(void);
114
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700115/**
116 * blkcache_read() - attempt to read a set of blocks from cache
117 *
Simon Glassfada3f92022-09-17 09:00:09 -0600118 * @param iftype - uclass_id_x for type of device
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700119 * @param dev - device index of particular type
120 * @param start - starting block number
121 * @param blkcnt - number of blocks to read
122 * @param blksz - size in bytes of each block
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200123 * @param buffer - buffer to contain cached data
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700124 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100125 * Return: - 1 if block returned from cache, 0 otherwise.
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700126 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700127int blkcache_read(int iftype, int dev,
128 lbaint_t start, lbaint_t blkcnt,
129 unsigned long blksz, void *buffer);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700130
131/**
132 * blkcache_fill() - make data read from a block device available
133 * to the block cache
134 *
Simon Glassfada3f92022-09-17 09:00:09 -0600135 * @param iftype - uclass_id_x for type of device
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700136 * @param dev - device index of particular type
137 * @param start - starting block number
138 * @param blkcnt - number of blocks available
139 * @param blksz - size in bytes of each block
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200140 * @param buffer - buffer containing data to cache
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700141 *
142 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700143void blkcache_fill(int iftype, int dev,
144 lbaint_t start, lbaint_t blkcnt,
145 unsigned long blksz, void const *buffer);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700146
147/**
148 * blkcache_invalidate() - discard the cache for a set of blocks
149 * because of a write or device (re)initialization.
150 *
Simon Glass76c62692022-10-29 19:47:08 -0600151 * @iftype - UCLASS_ID_ for type of device, or -1 for any
152 * @dev - device index of particular type, if @iftype is not -1
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700153 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700154void blkcache_invalidate(int iftype, int dev);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700155
156/**
157 * blkcache_configure() - configure block cache
158 *
159 * @param blocks - maximum blocks per entry
160 * @param entries - maximum entries in cache
161 */
162void blkcache_configure(unsigned blocks, unsigned entries);
163
164/*
165 * statistics of the block cache
166 */
167struct block_cache_stats {
168 unsigned hits;
169 unsigned misses;
170 unsigned entries; /* current entry count */
171 unsigned max_blocks_per_entry;
172 unsigned max_entries;
173};
174
175/**
176 * get_blkcache_stats() - return statistics and reset
177 *
178 * @param stats - statistics are copied here
179 */
180void blkcache_stats(struct block_cache_stats *stats);
181
Simon Glass76c62692022-10-29 19:47:08 -0600182/** blkcache_free() - free all memory allocated to the block cache */
183void blkcache_free(void);
184
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700185#else
186
Eric Nelsonf201f232016-04-02 07:37:14 -0700187static inline int blkcache_read(int iftype, int dev,
188 lbaint_t start, lbaint_t blkcnt,
189 unsigned long blksz, void *buffer)
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700190{
191 return 0;
192}
193
Eric Nelsonf201f232016-04-02 07:37:14 -0700194static inline void blkcache_fill(int iftype, int dev,
195 lbaint_t start, lbaint_t blkcnt,
196 unsigned long blksz, void const *buffer) {}
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700197
Eric Nelsonf201f232016-04-02 07:37:14 -0700198static inline void blkcache_invalidate(int iftype, int dev) {}
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700199
Simon Glass76c62692022-10-29 19:47:08 -0600200static inline void blkcache_free(void) {}
201
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700202#endif
203
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600204#if CONFIG_IS_ENABLED(BLK)
Simon Glasscceee552016-02-29 15:25:55 -0700205struct udevice;
206
207/* Operations on block devices */
208struct blk_ops {
209 /**
210 * read() - read from a block device
211 *
212 * @dev: Device to read from
213 * @start: Start block number to read (0=first)
214 * @blkcnt: Number of blocks to read
215 * @buffer: Destination buffer for data read
216 * @return number of blocks read, or -ve error number (see the
217 * IS_ERR_VALUE() macro
218 */
219 unsigned long (*read)(struct udevice *dev, lbaint_t start,
220 lbaint_t blkcnt, void *buffer);
221
222 /**
223 * write() - write to a block device
224 *
225 * @dev: Device to write to
226 * @start: Start block number to write (0=first)
227 * @blkcnt: Number of blocks to write
228 * @buffer: Source buffer for data to write
229 * @return number of blocks written, or -ve error number (see the
230 * IS_ERR_VALUE() macro
231 */
232 unsigned long (*write)(struct udevice *dev, lbaint_t start,
233 lbaint_t blkcnt, const void *buffer);
234
235 /**
236 * erase() - erase a section of a block device
237 *
238 * @dev: Device to (partially) erase
239 * @start: Start block number to erase (0=first)
240 * @blkcnt: Number of blocks to erase
241 * @return number of blocks erased, or -ve error number (see the
242 * IS_ERR_VALUE() macro
243 */
244 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
245 lbaint_t blkcnt);
Simon Glass13c2c292016-05-01 13:52:30 -0600246
247 /**
248 * select_hwpart() - select a particular hardware partition
249 *
250 * Some devices (e.g. MMC) can support partitioning at the hardware
251 * level. This is quite separate from the normal idea of
252 * software-based partitions. MMC hardware partitions must be
253 * explicitly selected. Once selected only the region of the device
254 * covered by that partition is accessible.
255 *
256 * The MMC standard provides for two boot partitions (numbered 1 and 2),
257 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
258 *
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200259 * @dev: Block device to update
Simon Glass13c2c292016-05-01 13:52:30 -0600260 * @hwpart: Hardware partition number to select. 0 means the raw
261 * device, 1 is the first partition, 2 is the second, etc.
262 * @return 0 if OK, -ve on error
263 */
264 int (*select_hwpart)(struct udevice *dev, int hwpart);
Simon Glasscceee552016-02-29 15:25:55 -0700265};
266
267#define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
268
269/*
270 * These functions should take struct udevice instead of struct blk_desc,
271 * but this is convenient for migration to driver model. Add a 'd' prefix
272 * to the function operations, so that blk_read(), etc. can be reserved for
273 * functions with the correct arguments.
274 */
275unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
276 lbaint_t blkcnt, void *buffer);
277unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
278 lbaint_t blkcnt, const void *buffer);
279unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
280 lbaint_t blkcnt);
281
282/**
Simon Glass18861002022-10-20 18:22:54 -0600283 * blk_read() - Read from a block device
284 *
285 * @dev: Device to read from
286 * @start: Start block for the read
287 * @blkcnt: Number of blocks to read
288 * @buf: Place to put the data
289 * @return number of blocks read (which may be less than @blkcnt),
290 * or -ve on error. This never returns 0 unless @blkcnt is 0
291 */
292long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
293 void *buffer);
294
295/**
296 * blk_write() - Write to a block device
297 *
298 * @dev: Device to write to
299 * @start: Start block for the write
300 * @blkcnt: Number of blocks to write
301 * @buf: Data to write
302 * @return number of blocks written (which may be less than @blkcnt),
303 * or -ve on error. This never returns 0 unless @blkcnt is 0
304 */
305long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
306 const void *buffer);
307
308/**
309 * blk_erase() - Erase part of a block device
310 *
311 * @dev: Device to erase
312 * @start: Start block for the erase
313 * @blkcnt: Number of blocks to erase
314 * @return number of blocks erased (which may be less than @blkcnt),
315 * or -ve on error. This never returns 0 unless @blkcnt is 0
316 */
317long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
318
319/**
Simon Glassd5d4c102017-04-23 20:02:05 -0600320 * blk_find_device() - Find a block device
321 *
322 * This function does not activate the device. The device will be returned
323 * whether or not it is activated.
324 *
Simon Glassfada3f92022-09-17 09:00:09 -0600325 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glassd5d4c102017-04-23 20:02:05 -0600326 * @devnum: Device number (specific to each interface type)
327 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100328 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glassd5d4c102017-04-23 20:02:05 -0600329 */
Simon Glassfada3f92022-09-17 09:00:09 -0600330int blk_find_device(int uclass_id, int devnum, struct udevice **devp);
Simon Glassd5d4c102017-04-23 20:02:05 -0600331
332/**
Simon Glasscceee552016-02-29 15:25:55 -0700333 * blk_get_device() - Find and probe a block device ready for use
334 *
Simon Glassfada3f92022-09-17 09:00:09 -0600335 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glasscceee552016-02-29 15:25:55 -0700336 * @devnum: Device number (specific to each interface type)
337 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100338 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700339 */
Simon Glassfada3f92022-09-17 09:00:09 -0600340int blk_get_device(int uclass_id, int devnum, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700341
342/**
343 * blk_first_device() - Find the first device for a given interface
344 *
345 * The device is probed ready for use
346 *
347 * @devnum: Device number (specific to each interface type)
348 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100349 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700350 */
Simon Glassfada3f92022-09-17 09:00:09 -0600351int blk_first_device(int uclass_id, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700352
353/**
354 * blk_next_device() - Find the next device for a given interface
355 *
356 * This can be called repeatedly after blk_first_device() to iterate through
357 * all devices of the given interface type.
358 *
359 * The device is probed ready for use
360 *
361 * @devp: On entry, the previous device returned. On exit, the next
362 * device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100363 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700364 */
365int blk_next_device(struct udevice **devp);
366
367/**
368 * blk_create_device() - Create a new block device
369 *
370 * @parent: Parent of the new device
371 * @drv_name: Driver name to use for the block device
372 * @name: Name for the device
Simon Glassfada3f92022-09-17 09:00:09 -0600373 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glassd089ba32016-05-01 11:36:28 -0600374 * @devnum: Device number, specific to the interface type, or -1 to
375 * allocate the next available number
Simon Glasscceee552016-02-29 15:25:55 -0700376 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200377 * @lba: Total number of blocks of the device
Simon Glasscceee552016-02-29 15:25:55 -0700378 * @devp: the new device (which has not been probed)
379 */
380int blk_create_device(struct udevice *parent, const char *drv_name,
Simon Glassfada3f92022-09-17 09:00:09 -0600381 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200382 lbaint_t lba, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700383
384/**
Simon Glass966b6952016-05-01 11:36:29 -0600385 * blk_create_devicef() - Create a new named block device
386 *
387 * @parent: Parent of the new device
388 * @drv_name: Driver name to use for the block device
389 * @name: Name for the device (parent name is prepended)
Simon Glassfada3f92022-09-17 09:00:09 -0600390 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glass966b6952016-05-01 11:36:29 -0600391 * @devnum: Device number, specific to the interface type, or -1 to
392 * allocate the next available number
393 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200394 * @lba: Total number of blocks of the device
Simon Glass966b6952016-05-01 11:36:29 -0600395 * @devp: the new device (which has not been probed)
396 */
397int blk_create_devicef(struct udevice *parent, const char *drv_name,
Simon Glassfada3f92022-09-17 09:00:09 -0600398 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200399 lbaint_t lba, struct udevice **devp);
Simon Glass966b6952016-05-01 11:36:29 -0600400
401/**
AKASHI Takahiro3e32dbe2021-12-10 15:49:29 +0900402 * blk_probe_or_unbind() - Try to probe
403 *
404 * Try to probe the device, primarily for enumerating partitions.
405 * If it fails, the device itself is unbound since it means that it won't
406 * work any more.
407 *
408 * @dev: The device to probe
409 * Return: 0 if OK, -ve on error
410 */
411int blk_probe_or_unbind(struct udevice *dev);
412
413/**
Simon Glasscceee552016-02-29 15:25:55 -0700414 * blk_unbind_all() - Unbind all device of the given interface type
415 *
416 * The devices are removed and then unbound.
417 *
Simon Glassfada3f92022-09-17 09:00:09 -0600418 * @uclass_id: Interface type to unbind
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100419 * Return: 0 if OK, -ve on error
Simon Glasscceee552016-02-29 15:25:55 -0700420 */
Simon Glassfada3f92022-09-17 09:00:09 -0600421int blk_unbind_all(int uclass_id);
Simon Glasscceee552016-02-29 15:25:55 -0700422
Simon Glassd089ba32016-05-01 11:36:28 -0600423/**
424 * blk_find_max_devnum() - find the maximum device number for an interface type
425 *
Simon Glassfada3f92022-09-17 09:00:09 -0600426 * Finds the last allocated device number for an interface type @uclass_id. The
Simon Glassd089ba32016-05-01 11:36:28 -0600427 * next number is safe to use for a newly allocated device.
428 *
Simon Glassfada3f92022-09-17 09:00:09 -0600429 * @uclass_id: Interface type to scan
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100430 * Return: maximum device number found, or -ENODEV if none, or other -ve on
Simon Glassd089ba32016-05-01 11:36:28 -0600431 * error
432 */
Simon Glassfada3f92022-09-17 09:00:09 -0600433int blk_find_max_devnum(enum uclass_id uclass_id);
Simon Glassd089ba32016-05-01 11:36:28 -0600434
Simon Glass13c2c292016-05-01 13:52:30 -0600435/**
Bin Mengfd5eda72018-10-15 02:21:09 -0700436 * blk_next_free_devnum() - get the next device number for an interface type
437 *
438 * Finds the next number that is safe to use for a newly allocated device for
Simon Glassfada3f92022-09-17 09:00:09 -0600439 * an interface type @uclass_id.
Bin Mengfd5eda72018-10-15 02:21:09 -0700440 *
Simon Glassfada3f92022-09-17 09:00:09 -0600441 * @uclass_id: Interface type to scan
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100442 * Return: next device number safe to use, or -ve on error
Bin Mengfd5eda72018-10-15 02:21:09 -0700443 */
Simon Glassfada3f92022-09-17 09:00:09 -0600444int blk_next_free_devnum(enum uclass_id uclass_id);
Bin Mengfd5eda72018-10-15 02:21:09 -0700445
446/**
Simon Glass13c2c292016-05-01 13:52:30 -0600447 * blk_select_hwpart() - select a hardware partition
448 *
449 * Select a hardware partition if the device supports it (typically MMC does)
450 *
451 * @dev: Device to update
452 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100453 * Return: 0 if OK, -ve on error
Simon Glass13c2c292016-05-01 13:52:30 -0600454 */
455int blk_select_hwpart(struct udevice *dev, int hwpart);
456
Tom Rini7c41d142017-06-10 10:01:05 -0400457/**
Simon Glassc0bcaaf2022-10-29 19:47:14 -0600458 * blk_find_from_parent() - find a block device by looking up its parent
459 *
460 * All block devices have a parent 'media' device which provides the block
461 * driver for the block device, ensuring that access to the underlying medium
462 * is available.
463 *
464 * The block device is not activated by this function. See
465 * blk_get_from_parent() for that.
466 *
467 * @parent: Media device
468 * @devp: Returns the associated block device, if any
469 * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
470 * UCLASS_BLK child
471 */
472int blk_find_from_parent(struct udevice *parent, struct udevice **devp);
473
474/**
Tom Rini7c41d142017-06-10 10:01:05 -0400475 * blk_get_from_parent() - obtain a block device by looking up its parent
476 *
Simon Glassc0bcaaf2022-10-29 19:47:14 -0600477 * All block devices have a parent 'media' device which provides the block
478 * driver for the block device, ensuring that access to the underlying medium
479 * is available.
480 *
481 * The block device is probed and ready for use.
482 *
483 * @parent: Media device
484 * @devp: Returns the associated block device, if any
485 * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
486 * UCLASS_BLK child
Tom Rini7c41d142017-06-10 10:01:05 -0400487 */
488int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
489
Tien Fong Chee10378522018-07-06 16:26:36 +0800490/**
Simon Glassf3086cf2022-04-24 23:31:03 -0600491 * blk_get_devtype() - Get the device type of a block device
492 *
493 * @dev: Block device to check
494 * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc"
495 */
496const char *blk_get_devtype(struct udevice *dev);
497
498/**
Tien Fong Chee10378522018-07-06 16:26:36 +0800499 * blk_get_by_device() - Get the block device descriptor for the given device
Simon Glass18861002022-10-20 18:22:54 -0600500 * @dev: Instance of a storage device (the parent of the block device)
Tien Fong Chee10378522018-07-06 16:26:36 +0800501 *
502 * Return: With block device descriptor on success , NULL if there is no such
503 * block device.
504 */
505struct blk_desc *blk_get_by_device(struct udevice *dev);
506
Simon Glasscceee552016-02-29 15:25:55 -0700507#else
508#include <errno.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -0700509/*
510 * These functions should take struct udevice instead of struct blk_desc,
511 * but this is convenient for migration to driver model. Add a 'd' prefix
512 * to the function operations, so that blk_read(), etc. can be reserved for
513 * functions with the correct arguments.
514 */
515static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
516 lbaint_t blkcnt, void *buffer)
517{
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700518 ulong blks_read;
Simon Glassfada3f92022-09-17 09:00:09 -0600519 if (blkcache_read(block_dev->uclass_id, block_dev->devnum,
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700520 start, blkcnt, block_dev->blksz, buffer))
521 return blkcnt;
522
Simon Glass2ee8ada2016-02-29 15:25:52 -0700523 /*
524 * We could check if block_read is NULL and return -ENOSYS. But this
525 * bloats the code slightly (cause some board to fail to build), and
526 * it would be an error to try an operation that does not exist.
527 */
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700528 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
529 if (blks_read == blkcnt)
Simon Glassfada3f92022-09-17 09:00:09 -0600530 blkcache_fill(block_dev->uclass_id, block_dev->devnum,
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700531 start, blkcnt, block_dev->blksz, buffer);
532
533 return blks_read;
Simon Glass2ee8ada2016-02-29 15:25:52 -0700534}
535
536static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
537 lbaint_t blkcnt, const void *buffer)
538{
Simon Glassfada3f92022-09-17 09:00:09 -0600539 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700540 return block_dev->block_write(block_dev, start, blkcnt, buffer);
541}
542
543static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
544 lbaint_t blkcnt)
545{
Simon Glassfada3f92022-09-17 09:00:09 -0600546 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700547 return block_dev->block_erase(block_dev, start, blkcnt);
548}
Simon Glass3bf2ab92016-05-01 11:36:03 -0600549
550/**
551 * struct blk_driver - Driver for block interface types
552 *
553 * This provides access to the block devices for each interface type. One
554 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
555 * type that is to be supported.
556 *
Simon Glassfada3f92022-09-17 09:00:09 -0600557 * @uclass_idname: Interface type name
558 * @uclass_id: Interface type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600559 * @max_devs: Maximum number of devices supported
560 * @desc: Pointer to list of devices for this interface type,
561 * or NULL to use @get_dev() instead
562 */
563struct blk_driver {
Simon Glassfada3f92022-09-17 09:00:09 -0600564 const char *uclass_idname;
565 enum uclass_id uclass_id;
Simon Glass3bf2ab92016-05-01 11:36:03 -0600566 int max_devs;
567 struct blk_desc *desc;
568 /**
569 * get_dev() - get a pointer to a block device given its number
570 *
571 * Each interface allocates its own devices and typically
572 * struct blk_desc is contained with the interface's data structure.
573 * There is no global numbering for block devices. This method allows
574 * the device for an interface type to be obtained when @desc is NULL.
575 *
576 * @devnum: Device number (0 for first device on that interface,
577 * 1 for second, etc.
578 * @descp: Returns pointer to the block device on success
579 * @return 0 if OK, -ve on error
580 */
581 int (*get_dev)(int devnum, struct blk_desc **descp);
582
583 /**
584 * select_hwpart() - Select a hardware partition
585 *
586 * Some devices (e.g. MMC) can support partitioning at the hardware
587 * level. This is quite separate from the normal idea of
588 * software-based partitions. MMC hardware partitions must be
589 * explicitly selected. Once selected only the region of the device
590 * covered by that partition is accessible.
591 *
592 * The MMC standard provides for two boot partitions (numbered 1 and 2),
593 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
594 * Partition 0 is the main user-data partition.
595 *
596 * @desc: Block device descriptor
597 * @hwpart: Hardware partition number to select. 0 means the main
598 * user-data partition, 1 is the first partition, 2 is
599 * the second, etc.
600 * @return 0 if OK, other value for an error
601 */
602 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
603};
604
605/*
606 * Declare a new U-Boot legacy block driver. New drivers should use driver
607 * model (UCLASS_BLK).
608 */
609#define U_BOOT_LEGACY_BLK(__name) \
610 ll_entry_declare(struct blk_driver, __name, blk_driver)
611
Simon Glassfada3f92022-09-17 09:00:09 -0600612struct blk_driver *blk_driver_lookup_type(int uclass_id);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600613
Simon Glasscceee552016-02-29 15:25:55 -0700614#endif /* !CONFIG_BLK */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700615
Simon Glass3bf2ab92016-05-01 11:36:03 -0600616/**
Simon Glassfada3f92022-09-17 09:00:09 -0600617 * blk_get_devnum_by_uclass_idname() - Get a block device by type and number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600618 *
619 * This looks through the available block devices of the given type, returning
620 * the one with the given @devnum.
621 *
Simon Glassfada3f92022-09-17 09:00:09 -0600622 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600623 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100624 * Return: point to block device descriptor, or NULL if not found
Simon Glass3bf2ab92016-05-01 11:36:03 -0600625 */
Simon Glassfada3f92022-09-17 09:00:09 -0600626struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600627
628/**
Simon Glassfada3f92022-09-17 09:00:09 -0600629 * blk_get_devnum_by_uclass_id() - Get a block device by type name, and number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600630 *
Simon Glassfada3f92022-09-17 09:00:09 -0600631 * This looks up the block device type based on @uclass_idname, then calls
632 * blk_get_devnum_by_uclass_id().
Simon Glass3bf2ab92016-05-01 11:36:03 -0600633 *
Simon Glassfada3f92022-09-17 09:00:09 -0600634 * @uclass_idname: Block device type name
Simon Glass3bf2ab92016-05-01 11:36:03 -0600635 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100636 * Return: point to block device descriptor, or NULL if not found
Simon Glass3bf2ab92016-05-01 11:36:03 -0600637 */
Simon Glassfada3f92022-09-17 09:00:09 -0600638struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600639 int devnum);
640
641/**
642 * blk_dselect_hwpart() - select a hardware partition
643 *
644 * This selects a hardware partition (such as is supported by MMC). The block
645 * device size may change as this effectively points the block device to a
646 * partition at the hardware level. See the select_hwpart() method above.
647 *
648 * @desc: Block device descriptor for the device to select
649 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100650 * Return: 0 if OK, -ve on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600651 */
652int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
653
654/**
655 * blk_list_part() - list the partitions for block devices of a given type
656 *
Simon Glassfada3f92022-09-17 09:00:09 -0600657 * This looks up the partition type for each block device of type @uclass_id,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600658 * then displays a list of partitions.
659 *
Simon Glassfada3f92022-09-17 09:00:09 -0600660 * @uclass_id: Block device type
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100661 * Return: 0 if OK, -ENODEV if there is none of that type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600662 */
Simon Glassfada3f92022-09-17 09:00:09 -0600663int blk_list_part(enum uclass_id uclass_id);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600664
665/**
666 * blk_list_devices() - list the block devices of a given type
667 *
Simon Glassfada3f92022-09-17 09:00:09 -0600668 * This lists each block device of the type @uclass_id, showing the capacity
Simon Glass3bf2ab92016-05-01 11:36:03 -0600669 * as well as type-specific information.
670 *
Simon Glassfada3f92022-09-17 09:00:09 -0600671 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600672 */
Simon Glassfada3f92022-09-17 09:00:09 -0600673void blk_list_devices(enum uclass_id uclass_id);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600674
675/**
676 * blk_show_device() - show information about a given block device
677 *
678 * This shows the block device capacity as well as type-specific information.
679 *
Simon Glassfada3f92022-09-17 09:00:09 -0600680 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600681 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100682 * Return: 0 if OK, -ENODEV for invalid device number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600683 */
Simon Glassfada3f92022-09-17 09:00:09 -0600684int blk_show_device(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600685
686/**
687 * blk_print_device_num() - show information about a given block device
688 *
689 * This is similar to blk_show_device() but returns an error if the block
690 * device type is unknown.
691 *
Simon Glassfada3f92022-09-17 09:00:09 -0600692 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600693 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100694 * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
Simon Glass3bf2ab92016-05-01 11:36:03 -0600695 * device is not connected
696 */
Simon Glassfada3f92022-09-17 09:00:09 -0600697int blk_print_device_num(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600698
699/**
700 * blk_print_part_devnum() - print the partition information for a device
701 *
Simon Glassfada3f92022-09-17 09:00:09 -0600702 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600703 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100704 * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
Simon Glass3bf2ab92016-05-01 11:36:03 -0600705 * the interface type is not supported, other -ve on other error
706 */
Simon Glassfada3f92022-09-17 09:00:09 -0600707int blk_print_part_devnum(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600708
709/**
710 * blk_read_devnum() - read blocks from a device
711 *
Simon Glassfada3f92022-09-17 09:00:09 -0600712 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600713 * @devnum: Device number
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200714 * @start: Start block number to read (0=first)
Simon Glass3bf2ab92016-05-01 11:36:03 -0600715 * @blkcnt: Number of blocks to read
716 * @buffer: Address to write data to
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100717 * Return: number of blocks read, or -ve error number on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600718 */
Simon Glassfada3f92022-09-17 09:00:09 -0600719ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600720 lbaint_t blkcnt, void *buffer);
721
722/**
723 * blk_write_devnum() - write blocks to a device
724 *
Simon Glassfada3f92022-09-17 09:00:09 -0600725 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600726 * @devnum: Device number
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200727 * @start: Start block number to write (0=first)
Simon Glass3bf2ab92016-05-01 11:36:03 -0600728 * @blkcnt: Number of blocks to write
729 * @buffer: Address to read data from
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100730 * Return: number of blocks written, or -ve error number on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600731 */
Simon Glassfada3f92022-09-17 09:00:09 -0600732ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600733 lbaint_t blkcnt, const void *buffer);
734
735/**
736 * blk_select_hwpart_devnum() - select a hardware partition
737 *
738 * This is similar to blk_dselect_hwpart() but it looks up the interface and
739 * device number.
740 *
Simon Glassfada3f92022-09-17 09:00:09 -0600741 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600742 * @devnum: Device number
743 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100744 * Return: 0 if OK, -ve on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600745 */
Simon Glassfada3f92022-09-17 09:00:09 -0600746int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600747
Simon Glass85af5a42017-07-29 11:34:53 -0600748/**
Simon Glassfada3f92022-09-17 09:00:09 -0600749 * blk_get_uclass_name() - Get the name of an interface type
Simon Glass85af5a42017-07-29 11:34:53 -0600750 *
Simon Glassfada3f92022-09-17 09:00:09 -0600751 * @uclass_id: Interface type to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100752 * Return: name of interface, or NULL if none
Simon Glass85af5a42017-07-29 11:34:53 -0600753 */
Simon Glassfada3f92022-09-17 09:00:09 -0600754const char *blk_get_uclass_name(enum uclass_id uclass_id);
Simon Glass85af5a42017-07-29 11:34:53 -0600755
Simon Glassbf2e7952017-07-29 11:34:54 -0600756/**
757 * blk_common_cmd() - handle common commands with block devices
758 *
759 * @args: Number of arguments to the command (argv[0] is the command itself)
760 * @argv: Command arguments
Simon Glassfada3f92022-09-17 09:00:09 -0600761 * @uclass_id: Interface type
Simon Glassbf2e7952017-07-29 11:34:54 -0600762 * @cur_devnump: Current device number for this interface type
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100763 * Return: 0 if OK, CMD_RET_ERROR on error
Simon Glassbf2e7952017-07-29 11:34:54 -0600764 */
Simon Glassfada3f92022-09-17 09:00:09 -0600765int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
Simon Glassbf2e7952017-07-29 11:34:54 -0600766 int *cur_devnump);
767
Simon Glassfc7a7442021-07-05 16:32:59 -0600768enum blk_flag_t {
769 BLKF_FIXED = 1 << 0,
770 BLKF_REMOVABLE = 1 << 1,
771 BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE,
772};
773
774/**
775 * blk_first_device_err() - Get the first block device
776 *
777 * The device returned is probed if necessary, and ready for use
778 *
779 * @flags: Indicates type of device to return
780 * @devp: Returns pointer to the first device in that uclass, or NULL if none
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100781 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glassfc7a7442021-07-05 16:32:59 -0600782 */
783int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
784
785/**
786 * blk_next_device_err() - Get the next block device
787 *
788 * The device returned is probed if necessary, and ready for use
789 *
790 * @flags: Indicates type of device to return
791 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
792 * to the next device in the uclass if no error occurred, or -ENODEV if
793 * there is no next device.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100794 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glassfc7a7442021-07-05 16:32:59 -0600795 */
796int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
797
798/**
Simon Glass8e61f932022-02-28 12:08:35 -0700799 * blk_find_first() - Return the first matching block device
800 * @flags: Indicates type of device to return
801 * @devp: Returns pointer to device, or NULL on error
802 *
803 * The device is not prepared for use - this is an internal function.
804 * The function uclass_get_device_tail() can be used to probe the device.
805 *
806 * Note that some devices are considered removable until they have been probed
807 *
808 * @return 0 if found, -ENODEV if not found
809 */
810int blk_find_first(enum blk_flag_t flags, struct udevice **devp);
811
812/**
813 * blk_find_next() - Return the next matching block device
814 * @flags: Indicates type of device to return
815 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
816 * to the next device in the same uclass, or NULL if none
817 *
818 * The device is not prepared for use - this is an internal function.
819 * The function uclass_get_device_tail() can be used to probe the device.
820 *
821 * Note that some devices are considered removable until they have been probed
822 *
823 * @return 0 if found, -ENODEV if not found
824 */
825int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
826
827/**
828 * blk_foreach() - iterate through block devices
829 *
830 * This creates a for() loop which works through the available block devices in
831 * order from start to end.
832 *
833 * If for some reason the uclass cannot be found, this does nothing.
834 *
835 * @_flags: Indicates type of device to return
836 * @_pos: struct udevice * to hold the current device. Set to NULL when there
837 * are no more devices.
838 */
839#define blk_foreach(_flags, _pos) \
840 for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \
841 _ret = blk_find_next(_flags, &_pos))
842
843/**
Simon Glassfc7a7442021-07-05 16:32:59 -0600844 * blk_foreach_probe() - Helper function to iteration through block devices
845 *
846 * This creates a for() loop which works through the available devices in
847 * a uclass in order from start to end. Devices are probed if necessary,
848 * and ready for use.
849 *
850 * @flags: Indicates type of device to return
851 * @dev: struct udevice * to hold the current device. Set to NULL when there
852 * are no more devices.
853 */
854#define blk_foreach_probe(flags, pos) \
855 for (int _ret = blk_first_device_err(flags, &(pos)); \
856 !_ret && pos; \
857 _ret = blk_next_device_err(flags, &(pos)))
858
859/**
860 * blk_count_devices() - count the number of devices of a particular type
861 *
862 * @flags: Indicates type of device to find
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100863 * Return: number of devices matching those flags
Simon Glassfc7a7442021-07-05 16:32:59 -0600864 */
865int blk_count_devices(enum blk_flag_t flag);
866
Simon Glassac8162f2016-02-29 15:25:39 -0700867#endif