blob: 7c7cf7f2b102a239976c04ef3bdc7d991bdc8231 [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
Marek Vasut847e24f2023-08-14 01:49:59 +020010#include <bouncebuf.h>
Simon Glassdbfa32c2022-08-11 19:34:59 -060011#include <dm/uclass-id.h>
Peter Jonesc27e1c12017-09-13 18:05:25 -040012#include <efi.h>
13
Simon Glassac8162f2016-02-29 15:25:39 -070014#ifdef CONFIG_SYS_64BIT_LBA
15typedef uint64_t lbaint_t;
16#define LBAFlength "ll"
17#else
18typedef ulong lbaint_t;
19#define LBAFlength "l"
20#endif
21#define LBAF "%" LBAFlength "x"
22#define LBAFU "%" LBAFlength "u"
23
Bin Meng2294ecb2023-09-26 16:43:31 +080024#define DEFAULT_BLKSZ 512
25
Simon Glassfc7a7442021-07-05 16:32:59 -060026struct udevice;
27
Simon Glassf5ac3032022-08-11 19:34:45 -060028static inline bool blk_enabled(void)
29{
Simon Glass3bf7d7a2022-08-11 19:34:48 -060030 return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_SPL_LEGACY_BLOCK);
Simon Glassf5ac3032022-08-11 19:34:45 -060031}
32
Bin Mengcf406d22017-09-10 05:12:50 -070033#define BLK_VEN_SIZE 40
34#define BLK_PRD_SIZE 20
35#define BLK_REV_SIZE 8
36
Masahisa Kojima6460c3e2021-10-26 17:27:25 +090037#define PART_FORMAT_PCAT 0x1
38#define PART_FORMAT_GPT 0x2
39
Simon Glasscceee552016-02-29 15:25:55 -070040/*
Peter Jonesc27e1c12017-09-13 18:05:25 -040041 * Identifies the partition table type (ie. MBR vs GPT GUID) signature
42 */
43enum sig_type {
44 SIG_TYPE_NONE,
45 SIG_TYPE_MBR,
46 SIG_TYPE_GUID,
47
48 SIG_TYPE_COUNT /* Number of signature types */
49};
50
51/*
Simon Glasscceee552016-02-29 15:25:55 -070052 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
Simon Glass71fa5b42020-12-03 16:55:18 -070053 * with dev_get_uclass_plat(dev)
Simon Glasscceee552016-02-29 15:25:55 -070054 */
Simon Glassac8162f2016-02-29 15:25:39 -070055struct blk_desc {
Simon Glasscceee552016-02-29 15:25:55 -070056 /*
57 * TODO: With driver model we should be able to use the parent
58 * device's uclass instead.
59 */
Simon Glassfada3f92022-09-17 09:00:09 -060060 enum uclass_id uclass_id; /* type of the interface */
Simon Glass2f26fff2016-02-29 15:25:51 -070061 int devnum; /* device number */
Simon Glassac8162f2016-02-29 15:25:39 -070062 unsigned char part_type; /* partition type */
63 unsigned char target; /* target SCSI ID */
64 unsigned char lun; /* target LUN */
65 unsigned char hwpart; /* HW partition, e.g. for eMMC */
66 unsigned char type; /* device type */
67 unsigned char removable; /* removable device */
Simon Glassac8162f2016-02-29 15:25:39 -070068 /* device can use 48bit addr (ATA/ATAPI v7) */
Simon Glass61317282023-04-25 10:54:41 -060069 bool lba48;
Simon Glass631db662023-04-25 10:54:35 -060070 unsigned char atapi; /* Use ATAPI protocol */
Johan Jonker10a986c2023-10-18 16:01:10 +020071 unsigned char bb; /* Use bounce buffer */
Simon Glassac8162f2016-02-29 15:25:39 -070072 lbaint_t lba; /* number of blocks */
73 unsigned long blksz; /* block size */
74 int log2blksz; /* for convenience: log2(blksz) */
Bin Mengcf406d22017-09-10 05:12:50 -070075 char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
76 char product[BLK_PRD_SIZE + 1]; /* device product number */
77 char revision[BLK_REV_SIZE + 1]; /* firmware revision */
Peter Jonesc27e1c12017-09-13 18:05:25 -040078 enum sig_type sig_type; /* Partition table signature type */
79 union {
80 uint32_t mbr_sig; /* MBR integer signature */
81 efi_guid_t guid_sig; /* GPT GUID Signature */
82 };
Simon Glass5f4bd8c2017-07-04 13:31:19 -060083#if CONFIG_IS_ENABLED(BLK)
Simon Glass8f5f7222016-05-01 13:52:33 -060084 /*
85 * For now we have a few functions which take struct blk_desc as a
86 * parameter. This field allows them to look up the associated
87 * device. Once these functions are removed we can drop this field.
88 */
Simon Glasscceee552016-02-29 15:25:55 -070089 struct udevice *bdev;
90#else
Simon Glassac8162f2016-02-29 15:25:39 -070091 unsigned long (*block_read)(struct blk_desc *block_dev,
92 lbaint_t start,
93 lbaint_t blkcnt,
94 void *buffer);
95 unsigned long (*block_write)(struct blk_desc *block_dev,
96 lbaint_t start,
97 lbaint_t blkcnt,
98 const void *buffer);
99 unsigned long (*block_erase)(struct blk_desc *block_dev,
100 lbaint_t start,
101 lbaint_t blkcnt);
102 void *priv; /* driver private struct pointer */
Simon Glasscceee552016-02-29 15:25:55 -0700103#endif
Simon Glassac8162f2016-02-29 15:25:39 -0700104};
105
106#define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
107#define PAD_TO_BLOCKSIZE(size, blk_desc) \
108 (PAD_SIZE(size, blk_desc->blksz))
109
Adam Fordd693fb92018-06-11 17:17:48 -0500110#if CONFIG_IS_ENABLED(BLOCK_CACHE)
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700111/**
112 * blkcache_read() - attempt to read a set of blocks from cache
113 *
Simon Glassfada3f92022-09-17 09:00:09 -0600114 * @param iftype - uclass_id_x for type of device
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700115 * @param dev - device index of particular type
116 * @param start - starting block number
117 * @param blkcnt - number of blocks to read
118 * @param blksz - size in bytes of each block
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200119 * @param buffer - buffer to contain cached data
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700120 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100121 * Return: - 1 if block returned from cache, 0 otherwise.
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700122 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700123int blkcache_read(int iftype, int dev,
124 lbaint_t start, lbaint_t blkcnt,
125 unsigned long blksz, void *buffer);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700126
127/**
128 * blkcache_fill() - make data read from a block device available
129 * to the block cache
130 *
Simon Glassfada3f92022-09-17 09:00:09 -0600131 * @param iftype - uclass_id_x for type of device
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700132 * @param dev - device index of particular type
133 * @param start - starting block number
134 * @param blkcnt - number of blocks available
135 * @param blksz - size in bytes of each block
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200136 * @param buffer - buffer containing data to cache
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700137 *
138 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700139void blkcache_fill(int iftype, int dev,
140 lbaint_t start, lbaint_t blkcnt,
141 unsigned long blksz, void const *buffer);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700142
143/**
144 * blkcache_invalidate() - discard the cache for a set of blocks
145 * because of a write or device (re)initialization.
146 *
Simon Glass76c62692022-10-29 19:47:08 -0600147 * @iftype - UCLASS_ID_ for type of device, or -1 for any
148 * @dev - device index of particular type, if @iftype is not -1
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700149 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700150void blkcache_invalidate(int iftype, int dev);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700151
152/**
153 * blkcache_configure() - configure block cache
154 *
155 * @param blocks - maximum blocks per entry
156 * @param entries - maximum entries in cache
157 */
158void blkcache_configure(unsigned blocks, unsigned entries);
159
160/*
161 * statistics of the block cache
162 */
163struct block_cache_stats {
164 unsigned hits;
165 unsigned misses;
166 unsigned entries; /* current entry count */
167 unsigned max_blocks_per_entry;
168 unsigned max_entries;
169};
170
171/**
172 * get_blkcache_stats() - return statistics and reset
173 *
174 * @param stats - statistics are copied here
175 */
176void blkcache_stats(struct block_cache_stats *stats);
177
Simon Glass76c62692022-10-29 19:47:08 -0600178/** blkcache_free() - free all memory allocated to the block cache */
179void blkcache_free(void);
180
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700181#else
182
Eric Nelsonf201f232016-04-02 07:37:14 -0700183static inline int blkcache_read(int iftype, int dev,
184 lbaint_t start, lbaint_t blkcnt,
185 unsigned long blksz, void *buffer)
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700186{
187 return 0;
188}
189
Eric Nelsonf201f232016-04-02 07:37:14 -0700190static inline void blkcache_fill(int iftype, int dev,
191 lbaint_t start, lbaint_t blkcnt,
192 unsigned long blksz, void const *buffer) {}
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700193
Eric Nelsonf201f232016-04-02 07:37:14 -0700194static inline void blkcache_invalidate(int iftype, int dev) {}
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700195
Simon Glass76c62692022-10-29 19:47:08 -0600196static inline void blkcache_free(void) {}
197
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700198#endif
199
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600200#if CONFIG_IS_ENABLED(BLK)
Simon Glasscceee552016-02-29 15:25:55 -0700201struct udevice;
202
203/* Operations on block devices */
204struct blk_ops {
205 /**
206 * read() - read from a block device
207 *
208 * @dev: Device to read from
209 * @start: Start block number to read (0=first)
210 * @blkcnt: Number of blocks to read
211 * @buffer: Destination buffer for data read
212 * @return number of blocks read, or -ve error number (see the
213 * IS_ERR_VALUE() macro
214 */
215 unsigned long (*read)(struct udevice *dev, lbaint_t start,
216 lbaint_t blkcnt, void *buffer);
217
218 /**
219 * write() - write to a block device
220 *
221 * @dev: Device to write to
222 * @start: Start block number to write (0=first)
223 * @blkcnt: Number of blocks to write
224 * @buffer: Source buffer for data to write
225 * @return number of blocks written, or -ve error number (see the
226 * IS_ERR_VALUE() macro
227 */
228 unsigned long (*write)(struct udevice *dev, lbaint_t start,
229 lbaint_t blkcnt, const void *buffer);
230
231 /**
232 * erase() - erase a section of a block device
233 *
234 * @dev: Device to (partially) erase
235 * @start: Start block number to erase (0=first)
236 * @blkcnt: Number of blocks to erase
237 * @return number of blocks erased, or -ve error number (see the
238 * IS_ERR_VALUE() macro
239 */
240 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
241 lbaint_t blkcnt);
Simon Glass13c2c292016-05-01 13:52:30 -0600242
243 /**
244 * select_hwpart() - select a particular hardware partition
245 *
246 * Some devices (e.g. MMC) can support partitioning at the hardware
247 * level. This is quite separate from the normal idea of
248 * software-based partitions. MMC hardware partitions must be
249 * explicitly selected. Once selected only the region of the device
250 * covered by that partition is accessible.
251 *
252 * The MMC standard provides for two boot partitions (numbered 1 and 2),
253 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
254 *
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200255 * @dev: Block device to update
Simon Glass13c2c292016-05-01 13:52:30 -0600256 * @hwpart: Hardware partition number to select. 0 means the raw
257 * device, 1 is the first partition, 2 is the second, etc.
258 * @return 0 if OK, -ve on error
259 */
260 int (*select_hwpart)(struct udevice *dev, int hwpart);
Marek Vasut847e24f2023-08-14 01:49:59 +0200261
262#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
263 /**
264 * buffer_aligned() - test memory alignment of block operation buffer
265 *
266 * Some devices have limited DMA capabilities and require that the
267 * buffers passed to them fit specific properties. This optional
268 * callback can be used to indicate whether a buffer alignment is
269 * suitable for the device DMA or not, and trigger use of generic
270 * bounce buffer implementation to help use of unsuitable buffers
271 * at the expense of performance degradation.
272 *
273 * @dev: Block device associated with the request
274 * @state: Bounce buffer state
275 * @return 1 if OK, 0 if unaligned
276 */
277 int (*buffer_aligned)(struct udevice *dev, struct bounce_buffer *state);
278#endif /* CONFIG_BOUNCE_BUFFER */
Simon Glasscceee552016-02-29 15:25:55 -0700279};
280
Simon Glasscceee552016-02-29 15:25:55 -0700281/*
282 * These functions should take struct udevice instead of struct blk_desc,
283 * but this is convenient for migration to driver model. Add a 'd' prefix
284 * to the function operations, so that blk_read(), etc. can be reserved for
285 * functions with the correct arguments.
286 */
287unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
288 lbaint_t blkcnt, void *buffer);
289unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
290 lbaint_t blkcnt, const void *buffer);
291unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
292 lbaint_t blkcnt);
293
294/**
Simon Glass18861002022-10-20 18:22:54 -0600295 * blk_read() - Read from a block device
296 *
297 * @dev: Device to read from
298 * @start: Start block for the read
299 * @blkcnt: Number of blocks to read
300 * @buf: Place to put the data
301 * @return number of blocks read (which may be less than @blkcnt),
302 * or -ve on error. This never returns 0 unless @blkcnt is 0
303 */
304long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
305 void *buffer);
306
307/**
308 * blk_write() - Write to a block device
309 *
310 * @dev: Device to write to
311 * @start: Start block for the write
312 * @blkcnt: Number of blocks to write
313 * @buf: Data to write
314 * @return number of blocks written (which may be less than @blkcnt),
315 * or -ve on error. This never returns 0 unless @blkcnt is 0
316 */
317long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
318 const void *buffer);
319
320/**
321 * blk_erase() - Erase part of a block device
322 *
323 * @dev: Device to erase
324 * @start: Start block for the erase
325 * @blkcnt: Number of blocks to erase
326 * @return number of blocks erased (which may be less than @blkcnt),
327 * or -ve on error. This never returns 0 unless @blkcnt is 0
328 */
329long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
330
331/**
Simon Glassd5d4c102017-04-23 20:02:05 -0600332 * blk_find_device() - Find a block device
333 *
334 * This function does not activate the device. The device will be returned
335 * whether or not it is activated.
336 *
Simon Glassfada3f92022-09-17 09:00:09 -0600337 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glassd5d4c102017-04-23 20:02:05 -0600338 * @devnum: Device number (specific to each interface type)
339 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100340 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glassd5d4c102017-04-23 20:02:05 -0600341 */
Simon Glassfada3f92022-09-17 09:00:09 -0600342int blk_find_device(int uclass_id, int devnum, struct udevice **devp);
Simon Glassd5d4c102017-04-23 20:02:05 -0600343
344/**
Simon Glasscceee552016-02-29 15:25:55 -0700345 * blk_get_device() - Find and probe a block device ready for use
346 *
Simon Glassfada3f92022-09-17 09:00:09 -0600347 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glasscceee552016-02-29 15:25:55 -0700348 * @devnum: Device number (specific to each interface type)
349 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100350 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700351 */
Simon Glassfada3f92022-09-17 09:00:09 -0600352int blk_get_device(int uclass_id, int devnum, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700353
354/**
355 * blk_first_device() - Find the first device for a given interface
356 *
357 * The device is probed ready for use
358 *
359 * @devnum: Device number (specific to each interface type)
360 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100361 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700362 */
Simon Glassfada3f92022-09-17 09:00:09 -0600363int blk_first_device(int uclass_id, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700364
365/**
366 * blk_next_device() - Find the next device for a given interface
367 *
368 * This can be called repeatedly after blk_first_device() to iterate through
369 * all devices of the given interface type.
370 *
371 * The device is probed ready for use
372 *
373 * @devp: On entry, the previous device returned. On exit, the next
374 * device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100375 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700376 */
377int blk_next_device(struct udevice **devp);
378
379/**
380 * blk_create_device() - Create a new block device
381 *
382 * @parent: Parent of the new device
383 * @drv_name: Driver name to use for the block device
384 * @name: Name for the device
Simon Glassfada3f92022-09-17 09:00:09 -0600385 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glassd089ba32016-05-01 11:36:28 -0600386 * @devnum: Device number, specific to the interface type, or -1 to
387 * allocate the next available number
Simon Glasscceee552016-02-29 15:25:55 -0700388 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200389 * @lba: Total number of blocks of the device
Simon Glasscceee552016-02-29 15:25:55 -0700390 * @devp: the new device (which has not been probed)
391 */
392int blk_create_device(struct udevice *parent, const char *drv_name,
Simon Glassfada3f92022-09-17 09:00:09 -0600393 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200394 lbaint_t lba, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700395
396/**
Simon Glass966b6952016-05-01 11:36:29 -0600397 * blk_create_devicef() - Create a new named block device
398 *
399 * @parent: Parent of the new device
400 * @drv_name: Driver name to use for the block device
401 * @name: Name for the device (parent name is prepended)
Simon Glassfada3f92022-09-17 09:00:09 -0600402 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glass966b6952016-05-01 11:36:29 -0600403 * @devnum: Device number, specific to the interface type, or -1 to
404 * allocate the next available number
405 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200406 * @lba: Total number of blocks of the device
Simon Glass966b6952016-05-01 11:36:29 -0600407 * @devp: the new device (which has not been probed)
408 */
409int blk_create_devicef(struct udevice *parent, const char *drv_name,
Simon Glassfada3f92022-09-17 09:00:09 -0600410 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200411 lbaint_t lba, struct udevice **devp);
Simon Glass966b6952016-05-01 11:36:29 -0600412
413/**
AKASHI Takahiro3e32dbe2021-12-10 15:49:29 +0900414 * blk_probe_or_unbind() - Try to probe
415 *
416 * Try to probe the device, primarily for enumerating partitions.
417 * If it fails, the device itself is unbound since it means that it won't
418 * work any more.
419 *
420 * @dev: The device to probe
421 * Return: 0 if OK, -ve on error
422 */
423int blk_probe_or_unbind(struct udevice *dev);
424
425/**
Simon Glasscceee552016-02-29 15:25:55 -0700426 * blk_unbind_all() - Unbind all device of the given interface type
427 *
428 * The devices are removed and then unbound.
429 *
Simon Glassfada3f92022-09-17 09:00:09 -0600430 * @uclass_id: Interface type to unbind
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100431 * Return: 0 if OK, -ve on error
Simon Glasscceee552016-02-29 15:25:55 -0700432 */
Simon Glassfada3f92022-09-17 09:00:09 -0600433int blk_unbind_all(int uclass_id);
Simon Glasscceee552016-02-29 15:25:55 -0700434
Simon Glassd089ba32016-05-01 11:36:28 -0600435/**
436 * blk_find_max_devnum() - find the maximum device number for an interface type
437 *
Simon Glassfada3f92022-09-17 09:00:09 -0600438 * Finds the last allocated device number for an interface type @uclass_id. The
Simon Glassd089ba32016-05-01 11:36:28 -0600439 * next number is safe to use for a newly allocated device.
440 *
Simon Glassfada3f92022-09-17 09:00:09 -0600441 * @uclass_id: Interface type to scan
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100442 * Return: maximum device number found, or -ENODEV if none, or other -ve on
Simon Glassd089ba32016-05-01 11:36:28 -0600443 * error
444 */
Simon Glassfada3f92022-09-17 09:00:09 -0600445int blk_find_max_devnum(enum uclass_id uclass_id);
Simon Glassd089ba32016-05-01 11:36:28 -0600446
Simon Glass13c2c292016-05-01 13:52:30 -0600447/**
Bin Mengfd5eda72018-10-15 02:21:09 -0700448 * blk_next_free_devnum() - get the next device number for an interface type
449 *
450 * Finds the next number that is safe to use for a newly allocated device for
Simon Glassfada3f92022-09-17 09:00:09 -0600451 * an interface type @uclass_id.
Bin Mengfd5eda72018-10-15 02:21:09 -0700452 *
Simon Glassfada3f92022-09-17 09:00:09 -0600453 * @uclass_id: Interface type to scan
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100454 * Return: next device number safe to use, or -ve on error
Bin Mengfd5eda72018-10-15 02:21:09 -0700455 */
Simon Glassfada3f92022-09-17 09:00:09 -0600456int blk_next_free_devnum(enum uclass_id uclass_id);
Bin Mengfd5eda72018-10-15 02:21:09 -0700457
458/**
Simon Glass13c2c292016-05-01 13:52:30 -0600459 * blk_select_hwpart() - select a hardware partition
460 *
461 * Select a hardware partition if the device supports it (typically MMC does)
462 *
463 * @dev: Device to update
464 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100465 * Return: 0 if OK, -ve on error
Simon Glass13c2c292016-05-01 13:52:30 -0600466 */
467int blk_select_hwpart(struct udevice *dev, int hwpart);
468
Tom Rini7c41d142017-06-10 10:01:05 -0400469/**
Simon Glassc0bcaaf2022-10-29 19:47:14 -0600470 * blk_find_from_parent() - find a block device by looking up its parent
471 *
472 * All block devices have a parent 'media' device which provides the block
473 * driver for the block device, ensuring that access to the underlying medium
474 * is available.
475 *
476 * The block device is not activated by this function. See
477 * blk_get_from_parent() for that.
478 *
479 * @parent: Media device
480 * @devp: Returns the associated block device, if any
481 * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
482 * UCLASS_BLK child
483 */
484int blk_find_from_parent(struct udevice *parent, struct udevice **devp);
485
486/**
Tom Rini7c41d142017-06-10 10:01:05 -0400487 * blk_get_from_parent() - obtain a block device by looking up its parent
488 *
Simon Glassc0bcaaf2022-10-29 19:47:14 -0600489 * All block devices have a parent 'media' device which provides the block
490 * driver for the block device, ensuring that access to the underlying medium
491 * is available.
492 *
493 * The block device is probed and ready for use.
494 *
495 * @parent: Media device
496 * @devp: Returns the associated block device, if any
497 * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
498 * UCLASS_BLK child
Tom Rini7c41d142017-06-10 10:01:05 -0400499 */
500int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
501
Tien Fong Chee10378522018-07-06 16:26:36 +0800502/**
Simon Glassf3086cf2022-04-24 23:31:03 -0600503 * blk_get_devtype() - Get the device type of a block device
504 *
505 * @dev: Block device to check
506 * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc"
507 */
508const char *blk_get_devtype(struct udevice *dev);
509
510/**
Tien Fong Chee10378522018-07-06 16:26:36 +0800511 * blk_get_by_device() - Get the block device descriptor for the given device
Simon Glass18861002022-10-20 18:22:54 -0600512 * @dev: Instance of a storage device (the parent of the block device)
Tien Fong Chee10378522018-07-06 16:26:36 +0800513 *
514 * Return: With block device descriptor on success , NULL if there is no such
515 * block device.
516 */
517struct blk_desc *blk_get_by_device(struct udevice *dev);
518
Bin Meng9efc2452023-09-26 16:43:41 +0800519/**
520 * blk_get_desc() - Get the block device descriptor for the given device number
521 *
522 * @uclass_id: Interface type
523 * @devnum: Device number (0 = first)
524 * @descp: Returns block device descriptor on success
525 * Return: 0 on success, -ENODEV if there is no such device and no device
526 * with a higher device number, -ENOENT if there is no such device but there
527 * is one with a higher number, or other -ve on other error.
528 */
529int blk_get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp);
530
Simon Glasscceee552016-02-29 15:25:55 -0700531#else
532#include <errno.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -0700533/*
534 * These functions should take struct udevice instead of struct blk_desc,
535 * but this is convenient for migration to driver model. Add a 'd' prefix
536 * to the function operations, so that blk_read(), etc. can be reserved for
537 * functions with the correct arguments.
538 */
539static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
540 lbaint_t blkcnt, void *buffer)
541{
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700542 ulong blks_read;
Simon Glassfada3f92022-09-17 09:00:09 -0600543 if (blkcache_read(block_dev->uclass_id, block_dev->devnum,
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700544 start, blkcnt, block_dev->blksz, buffer))
545 return blkcnt;
546
Simon Glass2ee8ada2016-02-29 15:25:52 -0700547 /*
548 * We could check if block_read is NULL and return -ENOSYS. But this
549 * bloats the code slightly (cause some board to fail to build), and
550 * it would be an error to try an operation that does not exist.
551 */
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700552 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
553 if (blks_read == blkcnt)
Simon Glassfada3f92022-09-17 09:00:09 -0600554 blkcache_fill(block_dev->uclass_id, block_dev->devnum,
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700555 start, blkcnt, block_dev->blksz, buffer);
556
557 return blks_read;
Simon Glass2ee8ada2016-02-29 15:25:52 -0700558}
559
560static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
561 lbaint_t blkcnt, const void *buffer)
562{
Simon Glassfada3f92022-09-17 09:00:09 -0600563 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700564 return block_dev->block_write(block_dev, start, blkcnt, buffer);
565}
566
567static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
568 lbaint_t blkcnt)
569{
Simon Glassfada3f92022-09-17 09:00:09 -0600570 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700571 return block_dev->block_erase(block_dev, start, blkcnt);
572}
Simon Glass3bf2ab92016-05-01 11:36:03 -0600573
574/**
575 * struct blk_driver - Driver for block interface types
576 *
577 * This provides access to the block devices for each interface type. One
578 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
579 * type that is to be supported.
580 *
Simon Glassfada3f92022-09-17 09:00:09 -0600581 * @uclass_idname: Interface type name
582 * @uclass_id: Interface type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600583 * @max_devs: Maximum number of devices supported
584 * @desc: Pointer to list of devices for this interface type,
585 * or NULL to use @get_dev() instead
586 */
587struct blk_driver {
Simon Glassfada3f92022-09-17 09:00:09 -0600588 const char *uclass_idname;
589 enum uclass_id uclass_id;
Simon Glass3bf2ab92016-05-01 11:36:03 -0600590 int max_devs;
591 struct blk_desc *desc;
592 /**
593 * get_dev() - get a pointer to a block device given its number
594 *
595 * Each interface allocates its own devices and typically
596 * struct blk_desc is contained with the interface's data structure.
597 * There is no global numbering for block devices. This method allows
598 * the device for an interface type to be obtained when @desc is NULL.
599 *
600 * @devnum: Device number (0 for first device on that interface,
601 * 1 for second, etc.
602 * @descp: Returns pointer to the block device on success
603 * @return 0 if OK, -ve on error
604 */
605 int (*get_dev)(int devnum, struct blk_desc **descp);
606
607 /**
608 * select_hwpart() - Select a hardware partition
609 *
610 * Some devices (e.g. MMC) can support partitioning at the hardware
611 * level. This is quite separate from the normal idea of
612 * software-based partitions. MMC hardware partitions must be
613 * explicitly selected. Once selected only the region of the device
614 * covered by that partition is accessible.
615 *
616 * The MMC standard provides for two boot partitions (numbered 1 and 2),
617 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
618 * Partition 0 is the main user-data partition.
619 *
620 * @desc: Block device descriptor
621 * @hwpart: Hardware partition number to select. 0 means the main
622 * user-data partition, 1 is the first partition, 2 is
623 * the second, etc.
624 * @return 0 if OK, other value for an error
625 */
626 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
627};
628
629/*
630 * Declare a new U-Boot legacy block driver. New drivers should use driver
631 * model (UCLASS_BLK).
632 */
633#define U_BOOT_LEGACY_BLK(__name) \
634 ll_entry_declare(struct blk_driver, __name, blk_driver)
635
Simon Glassfada3f92022-09-17 09:00:09 -0600636struct blk_driver *blk_driver_lookup_type(int uclass_id);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600637
Simon Glasscceee552016-02-29 15:25:55 -0700638#endif /* !CONFIG_BLK */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700639
Simon Glass3bf2ab92016-05-01 11:36:03 -0600640/**
Simon Glassfada3f92022-09-17 09:00:09 -0600641 * blk_get_devnum_by_uclass_idname() - Get a block device by type and number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600642 *
643 * This looks through the available block devices of the given type, returning
644 * the one with the given @devnum.
645 *
Simon Glassfada3f92022-09-17 09:00:09 -0600646 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600647 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100648 * Return: point to block device descriptor, or NULL if not found
Simon Glass3bf2ab92016-05-01 11:36:03 -0600649 */
Simon Glassfada3f92022-09-17 09:00:09 -0600650struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600651
652/**
Simon Glassfada3f92022-09-17 09:00:09 -0600653 * blk_get_devnum_by_uclass_id() - Get a block device by type name, and number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600654 *
Simon Glassfada3f92022-09-17 09:00:09 -0600655 * This looks up the block device type based on @uclass_idname, then calls
656 * blk_get_devnum_by_uclass_id().
Simon Glass3bf2ab92016-05-01 11:36:03 -0600657 *
Simon Glassfada3f92022-09-17 09:00:09 -0600658 * @uclass_idname: Block device type name
Simon Glass3bf2ab92016-05-01 11:36:03 -0600659 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100660 * Return: point to block device descriptor, or NULL if not found
Simon Glass3bf2ab92016-05-01 11:36:03 -0600661 */
Simon Glassfada3f92022-09-17 09:00:09 -0600662struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600663 int devnum);
664
665/**
666 * blk_dselect_hwpart() - select a hardware partition
667 *
668 * This selects a hardware partition (such as is supported by MMC). The block
669 * device size may change as this effectively points the block device to a
670 * partition at the hardware level. See the select_hwpart() method above.
671 *
672 * @desc: Block device descriptor for the device to select
673 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100674 * Return: 0 if OK, -ve on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600675 */
676int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
677
678/**
679 * blk_list_part() - list the partitions for block devices of a given type
680 *
Simon Glassfada3f92022-09-17 09:00:09 -0600681 * This looks up the partition type for each block device of type @uclass_id,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600682 * then displays a list of partitions.
683 *
Simon Glassfada3f92022-09-17 09:00:09 -0600684 * @uclass_id: Block device type
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100685 * Return: 0 if OK, -ENODEV if there is none of that type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600686 */
Simon Glassfada3f92022-09-17 09:00:09 -0600687int blk_list_part(enum uclass_id uclass_id);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600688
689/**
690 * blk_list_devices() - list the block devices of a given type
691 *
Simon Glassfada3f92022-09-17 09:00:09 -0600692 * This lists each block device of the type @uclass_id, showing the capacity
Simon Glass3bf2ab92016-05-01 11:36:03 -0600693 * as well as type-specific information.
694 *
Simon Glassfada3f92022-09-17 09:00:09 -0600695 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600696 */
Simon Glassfada3f92022-09-17 09:00:09 -0600697void blk_list_devices(enum uclass_id uclass_id);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600698
699/**
700 * blk_show_device() - show information about a given block device
701 *
702 * This shows the block device capacity as well as type-specific information.
703 *
Simon Glassfada3f92022-09-17 09:00:09 -0600704 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600705 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100706 * Return: 0 if OK, -ENODEV for invalid device number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600707 */
Simon Glassfada3f92022-09-17 09:00:09 -0600708int blk_show_device(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600709
710/**
711 * blk_print_device_num() - show information about a given block device
712 *
713 * This is similar to blk_show_device() but returns an error if the block
714 * device type is unknown.
715 *
Simon Glassfada3f92022-09-17 09:00:09 -0600716 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600717 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100718 * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
Simon Glass3bf2ab92016-05-01 11:36:03 -0600719 * device is not connected
720 */
Simon Glassfada3f92022-09-17 09:00:09 -0600721int blk_print_device_num(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600722
723/**
724 * blk_print_part_devnum() - print the partition information for a device
725 *
Simon Glassfada3f92022-09-17 09:00:09 -0600726 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600727 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100728 * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
Simon Glass3bf2ab92016-05-01 11:36:03 -0600729 * the interface type is not supported, other -ve on other error
730 */
Simon Glassfada3f92022-09-17 09:00:09 -0600731int blk_print_part_devnum(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600732
733/**
Simon Glass3bf2ab92016-05-01 11:36:03 -0600734 * blk_select_hwpart_devnum() - select a hardware partition
735 *
736 * This is similar to blk_dselect_hwpart() but it looks up the interface and
737 * device number.
738 *
Simon Glassfada3f92022-09-17 09:00:09 -0600739 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600740 * @devnum: Device number
741 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100742 * Return: 0 if OK, -ve on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600743 */
Simon Glassfada3f92022-09-17 09:00:09 -0600744int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600745
Simon Glass85af5a42017-07-29 11:34:53 -0600746/**
Simon Glassfada3f92022-09-17 09:00:09 -0600747 * blk_get_uclass_name() - Get the name of an interface type
Simon Glass85af5a42017-07-29 11:34:53 -0600748 *
Simon Glassfada3f92022-09-17 09:00:09 -0600749 * @uclass_id: Interface type to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100750 * Return: name of interface, or NULL if none
Simon Glass85af5a42017-07-29 11:34:53 -0600751 */
Simon Glassfada3f92022-09-17 09:00:09 -0600752const char *blk_get_uclass_name(enum uclass_id uclass_id);
Simon Glass85af5a42017-07-29 11:34:53 -0600753
Simon Glassbf2e7952017-07-29 11:34:54 -0600754/**
755 * blk_common_cmd() - handle common commands with block devices
756 *
757 * @args: Number of arguments to the command (argv[0] is the command itself)
758 * @argv: Command arguments
Simon Glassfada3f92022-09-17 09:00:09 -0600759 * @uclass_id: Interface type
Simon Glassbf2e7952017-07-29 11:34:54 -0600760 * @cur_devnump: Current device number for this interface type
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100761 * Return: 0 if OK, CMD_RET_ERROR on error
Simon Glassbf2e7952017-07-29 11:34:54 -0600762 */
Simon Glassfada3f92022-09-17 09:00:09 -0600763int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
Simon Glassbf2e7952017-07-29 11:34:54 -0600764 int *cur_devnump);
765
Simon Glassfc7a7442021-07-05 16:32:59 -0600766enum blk_flag_t {
767 BLKF_FIXED = 1 << 0,
768 BLKF_REMOVABLE = 1 << 1,
769 BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE,
770};
771
772/**
773 * blk_first_device_err() - Get the first block device
774 *
775 * The device returned is probed if necessary, and ready for use
776 *
777 * @flags: Indicates type of device to return
778 * @devp: Returns pointer to the first device in that uclass, or NULL if none
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100779 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glassfc7a7442021-07-05 16:32:59 -0600780 */
781int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
782
783/**
784 * blk_next_device_err() - Get the next block device
785 *
786 * The device returned is probed if necessary, and ready for use
787 *
788 * @flags: Indicates type of device to return
789 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
790 * to the next device in the uclass if no error occurred, or -ENODEV if
791 * there is no next device.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100792 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glassfc7a7442021-07-05 16:32:59 -0600793 */
794int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
795
796/**
Simon Glass8e61f932022-02-28 12:08:35 -0700797 * blk_find_first() - Return the first matching block device
798 * @flags: Indicates type of device to return
799 * @devp: Returns pointer to device, or NULL on error
800 *
801 * The device is not prepared for use - this is an internal function.
802 * The function uclass_get_device_tail() can be used to probe the device.
803 *
804 * Note that some devices are considered removable until they have been probed
805 *
806 * @return 0 if found, -ENODEV if not found
807 */
808int blk_find_first(enum blk_flag_t flags, struct udevice **devp);
809
810/**
811 * blk_find_next() - Return the next matching block device
812 * @flags: Indicates type of device to return
813 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
814 * to the next device in the same uclass, or NULL if none
815 *
816 * The device is not prepared for use - this is an internal function.
817 * The function uclass_get_device_tail() can be used to probe the device.
818 *
819 * Note that some devices are considered removable until they have been probed
820 *
821 * @return 0 if found, -ENODEV if not found
822 */
823int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
824
825/**
826 * blk_foreach() - iterate through block devices
827 *
828 * This creates a for() loop which works through the available block devices in
829 * order from start to end.
830 *
831 * If for some reason the uclass cannot be found, this does nothing.
832 *
833 * @_flags: Indicates type of device to return
834 * @_pos: struct udevice * to hold the current device. Set to NULL when there
835 * are no more devices.
836 */
837#define blk_foreach(_flags, _pos) \
838 for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \
839 _ret = blk_find_next(_flags, &_pos))
840
841/**
Simon Glassfc7a7442021-07-05 16:32:59 -0600842 * blk_foreach_probe() - Helper function to iteration through block devices
843 *
844 * This creates a for() loop which works through the available devices in
845 * a uclass in order from start to end. Devices are probed if necessary,
846 * and ready for use.
847 *
848 * @flags: Indicates type of device to return
849 * @dev: struct udevice * to hold the current device. Set to NULL when there
850 * are no more devices.
851 */
852#define blk_foreach_probe(flags, pos) \
853 for (int _ret = blk_first_device_err(flags, &(pos)); \
854 !_ret && pos; \
855 _ret = blk_next_device_err(flags, &(pos)))
856
857/**
858 * blk_count_devices() - count the number of devices of a particular type
859 *
860 * @flags: Indicates type of device to find
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100861 * Return: number of devices matching those flags
Simon Glassfc7a7442021-07-05 16:32:59 -0600862 */
863int blk_count_devices(enum blk_flag_t flag);
864
Simon Glassac8162f2016-02-29 15:25:39 -0700865#endif