blob: 2c9c7985a885d42ddf71594a8041ba56dd419654 [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 */
Simon Glassac8162f2016-02-29 15:25:39 -070065 /* device can use 48bit addr (ATA/ATAPI v7) */
Simon Glass61317282023-04-25 10:54:41 -060066 bool lba48;
Simon Glass631db662023-04-25 10:54:35 -060067 unsigned char atapi; /* Use ATAPI protocol */
Simon Glassac8162f2016-02-29 15:25:39 -070068 lbaint_t lba; /* number of blocks */
69 unsigned long blksz; /* block size */
70 int log2blksz; /* for convenience: log2(blksz) */
Bin Mengcf406d22017-09-10 05:12:50 -070071 char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
72 char product[BLK_PRD_SIZE + 1]; /* device product number */
73 char revision[BLK_REV_SIZE + 1]; /* firmware revision */
Peter Jonesc27e1c12017-09-13 18:05:25 -040074 enum sig_type sig_type; /* Partition table signature type */
75 union {
76 uint32_t mbr_sig; /* MBR integer signature */
77 efi_guid_t guid_sig; /* GPT GUID Signature */
78 };
Simon Glass5f4bd8c2017-07-04 13:31:19 -060079#if CONFIG_IS_ENABLED(BLK)
Simon Glass8f5f7222016-05-01 13:52:33 -060080 /*
81 * For now we have a few functions which take struct blk_desc as a
82 * parameter. This field allows them to look up the associated
83 * device. Once these functions are removed we can drop this field.
84 */
Simon Glasscceee552016-02-29 15:25:55 -070085 struct udevice *bdev;
86#else
Simon Glassac8162f2016-02-29 15:25:39 -070087 unsigned long (*block_read)(struct blk_desc *block_dev,
88 lbaint_t start,
89 lbaint_t blkcnt,
90 void *buffer);
91 unsigned long (*block_write)(struct blk_desc *block_dev,
92 lbaint_t start,
93 lbaint_t blkcnt,
94 const void *buffer);
95 unsigned long (*block_erase)(struct blk_desc *block_dev,
96 lbaint_t start,
97 lbaint_t blkcnt);
98 void *priv; /* driver private struct pointer */
Simon Glasscceee552016-02-29 15:25:55 -070099#endif
Simon Glassac8162f2016-02-29 15:25:39 -0700100};
101
102#define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
103#define PAD_TO_BLOCKSIZE(size, blk_desc) \
104 (PAD_SIZE(size, blk_desc->blksz))
105
Adam Fordd693fb92018-06-11 17:17:48 -0500106#if CONFIG_IS_ENABLED(BLOCK_CACHE)
Angelo Durgehello3d8e4c12020-01-21 10:37:27 +0100107
108/**
109 * blkcache_init() - initialize the block cache list pointers
110 */
111int blkcache_init(void);
112
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700113/**
114 * blkcache_read() - attempt to read a set of blocks from cache
115 *
Simon Glassfada3f92022-09-17 09:00:09 -0600116 * @param iftype - uclass_id_x for type of device
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700117 * @param dev - device index of particular type
118 * @param start - starting block number
119 * @param blkcnt - number of blocks to read
120 * @param blksz - size in bytes of each block
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200121 * @param buffer - buffer to contain cached data
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700122 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100123 * Return: - 1 if block returned from cache, 0 otherwise.
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700124 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700125int blkcache_read(int iftype, int dev,
126 lbaint_t start, lbaint_t blkcnt,
127 unsigned long blksz, void *buffer);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700128
129/**
130 * blkcache_fill() - make data read from a block device available
131 * to the block cache
132 *
Simon Glassfada3f92022-09-17 09:00:09 -0600133 * @param iftype - uclass_id_x for type of device
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700134 * @param dev - device index of particular type
135 * @param start - starting block number
136 * @param blkcnt - number of blocks available
137 * @param blksz - size in bytes of each block
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200138 * @param buffer - buffer containing data to cache
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700139 *
140 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700141void blkcache_fill(int iftype, int dev,
142 lbaint_t start, lbaint_t blkcnt,
143 unsigned long blksz, void const *buffer);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700144
145/**
146 * blkcache_invalidate() - discard the cache for a set of blocks
147 * because of a write or device (re)initialization.
148 *
Simon Glass76c62692022-10-29 19:47:08 -0600149 * @iftype - UCLASS_ID_ for type of device, or -1 for any
150 * @dev - device index of particular type, if @iftype is not -1
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700151 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700152void blkcache_invalidate(int iftype, int dev);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700153
154/**
155 * blkcache_configure() - configure block cache
156 *
157 * @param blocks - maximum blocks per entry
158 * @param entries - maximum entries in cache
159 */
160void blkcache_configure(unsigned blocks, unsigned entries);
161
162/*
163 * statistics of the block cache
164 */
165struct block_cache_stats {
166 unsigned hits;
167 unsigned misses;
168 unsigned entries; /* current entry count */
169 unsigned max_blocks_per_entry;
170 unsigned max_entries;
171};
172
173/**
174 * get_blkcache_stats() - return statistics and reset
175 *
176 * @param stats - statistics are copied here
177 */
178void blkcache_stats(struct block_cache_stats *stats);
179
Simon Glass76c62692022-10-29 19:47:08 -0600180/** blkcache_free() - free all memory allocated to the block cache */
181void blkcache_free(void);
182
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700183#else
184
Eric Nelsonf201f232016-04-02 07:37:14 -0700185static inline int blkcache_read(int iftype, int dev,
186 lbaint_t start, lbaint_t blkcnt,
187 unsigned long blksz, void *buffer)
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700188{
189 return 0;
190}
191
Eric Nelsonf201f232016-04-02 07:37:14 -0700192static inline void blkcache_fill(int iftype, int dev,
193 lbaint_t start, lbaint_t blkcnt,
194 unsigned long blksz, void const *buffer) {}
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700195
Eric Nelsonf201f232016-04-02 07:37:14 -0700196static inline void blkcache_invalidate(int iftype, int dev) {}
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700197
Simon Glass76c62692022-10-29 19:47:08 -0600198static inline void blkcache_free(void) {}
199
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700200#endif
201
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600202#if CONFIG_IS_ENABLED(BLK)
Simon Glasscceee552016-02-29 15:25:55 -0700203struct udevice;
204
205/* Operations on block devices */
206struct blk_ops {
207 /**
208 * read() - read from a block device
209 *
210 * @dev: Device to read from
211 * @start: Start block number to read (0=first)
212 * @blkcnt: Number of blocks to read
213 * @buffer: Destination buffer for data read
214 * @return number of blocks read, or -ve error number (see the
215 * IS_ERR_VALUE() macro
216 */
217 unsigned long (*read)(struct udevice *dev, lbaint_t start,
218 lbaint_t blkcnt, void *buffer);
219
220 /**
221 * write() - write to a block device
222 *
223 * @dev: Device to write to
224 * @start: Start block number to write (0=first)
225 * @blkcnt: Number of blocks to write
226 * @buffer: Source buffer for data to write
227 * @return number of blocks written, or -ve error number (see the
228 * IS_ERR_VALUE() macro
229 */
230 unsigned long (*write)(struct udevice *dev, lbaint_t start,
231 lbaint_t blkcnt, const void *buffer);
232
233 /**
234 * erase() - erase a section of a block device
235 *
236 * @dev: Device to (partially) erase
237 * @start: Start block number to erase (0=first)
238 * @blkcnt: Number of blocks to erase
239 * @return number of blocks erased, or -ve error number (see the
240 * IS_ERR_VALUE() macro
241 */
242 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
243 lbaint_t blkcnt);
Simon Glass13c2c292016-05-01 13:52:30 -0600244
245 /**
246 * select_hwpart() - select a particular hardware partition
247 *
248 * Some devices (e.g. MMC) can support partitioning at the hardware
249 * level. This is quite separate from the normal idea of
250 * software-based partitions. MMC hardware partitions must be
251 * explicitly selected. Once selected only the region of the device
252 * covered by that partition is accessible.
253 *
254 * The MMC standard provides for two boot partitions (numbered 1 and 2),
255 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
256 *
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200257 * @dev: Block device to update
Simon Glass13c2c292016-05-01 13:52:30 -0600258 * @hwpart: Hardware partition number to select. 0 means the raw
259 * device, 1 is the first partition, 2 is the second, etc.
260 * @return 0 if OK, -ve on error
261 */
262 int (*select_hwpart)(struct udevice *dev, int hwpart);
Simon Glasscceee552016-02-29 15:25:55 -0700263};
264
265#define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
266
267/*
268 * These functions should take struct udevice instead of struct blk_desc,
269 * but this is convenient for migration to driver model. Add a 'd' prefix
270 * to the function operations, so that blk_read(), etc. can be reserved for
271 * functions with the correct arguments.
272 */
273unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
274 lbaint_t blkcnt, void *buffer);
275unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
276 lbaint_t blkcnt, const void *buffer);
277unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
278 lbaint_t blkcnt);
279
280/**
Simon Glass18861002022-10-20 18:22:54 -0600281 * blk_read() - Read from a block device
282 *
283 * @dev: Device to read from
284 * @start: Start block for the read
285 * @blkcnt: Number of blocks to read
286 * @buf: Place to put the data
287 * @return number of blocks read (which may be less than @blkcnt),
288 * or -ve on error. This never returns 0 unless @blkcnt is 0
289 */
290long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
291 void *buffer);
292
293/**
294 * blk_write() - Write to a block device
295 *
296 * @dev: Device to write to
297 * @start: Start block for the write
298 * @blkcnt: Number of blocks to write
299 * @buf: Data to write
300 * @return number of blocks written (which may be less than @blkcnt),
301 * or -ve on error. This never returns 0 unless @blkcnt is 0
302 */
303long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
304 const void *buffer);
305
306/**
307 * blk_erase() - Erase part of a block device
308 *
309 * @dev: Device to erase
310 * @start: Start block for the erase
311 * @blkcnt: Number of blocks to erase
312 * @return number of blocks erased (which may be less than @blkcnt),
313 * or -ve on error. This never returns 0 unless @blkcnt is 0
314 */
315long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
316
317/**
Simon Glassd5d4c102017-04-23 20:02:05 -0600318 * blk_find_device() - Find a block device
319 *
320 * This function does not activate the device. The device will be returned
321 * whether or not it is activated.
322 *
Simon Glassfada3f92022-09-17 09:00:09 -0600323 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glassd5d4c102017-04-23 20:02:05 -0600324 * @devnum: Device number (specific to each interface type)
325 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100326 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glassd5d4c102017-04-23 20:02:05 -0600327 */
Simon Glassfada3f92022-09-17 09:00:09 -0600328int blk_find_device(int uclass_id, int devnum, struct udevice **devp);
Simon Glassd5d4c102017-04-23 20:02:05 -0600329
330/**
Simon Glasscceee552016-02-29 15:25:55 -0700331 * blk_get_device() - Find and probe a block device ready for use
332 *
Simon Glassfada3f92022-09-17 09:00:09 -0600333 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glasscceee552016-02-29 15:25:55 -0700334 * @devnum: Device number (specific to each interface type)
335 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100336 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700337 */
Simon Glassfada3f92022-09-17 09:00:09 -0600338int blk_get_device(int uclass_id, int devnum, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700339
340/**
341 * blk_first_device() - Find the first device for a given interface
342 *
343 * The device is probed ready for use
344 *
345 * @devnum: Device number (specific to each interface type)
346 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100347 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700348 */
Simon Glassfada3f92022-09-17 09:00:09 -0600349int blk_first_device(int uclass_id, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700350
351/**
352 * blk_next_device() - Find the next device for a given interface
353 *
354 * This can be called repeatedly after blk_first_device() to iterate through
355 * all devices of the given interface type.
356 *
357 * The device is probed ready for use
358 *
359 * @devp: On entry, the previous device returned. On exit, the next
360 * 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 */
363int blk_next_device(struct udevice **devp);
364
365/**
366 * blk_create_device() - Create a new block device
367 *
368 * @parent: Parent of the new device
369 * @drv_name: Driver name to use for the block device
370 * @name: Name for the device
Simon Glassfada3f92022-09-17 09:00:09 -0600371 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glassd089ba32016-05-01 11:36:28 -0600372 * @devnum: Device number, specific to the interface type, or -1 to
373 * allocate the next available number
Simon Glasscceee552016-02-29 15:25:55 -0700374 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200375 * @lba: Total number of blocks of the device
Simon Glasscceee552016-02-29 15:25:55 -0700376 * @devp: the new device (which has not been probed)
377 */
378int blk_create_device(struct udevice *parent, const char *drv_name,
Simon Glassfada3f92022-09-17 09:00:09 -0600379 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200380 lbaint_t lba, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700381
382/**
Simon Glass966b6952016-05-01 11:36:29 -0600383 * blk_create_devicef() - Create a new named block device
384 *
385 * @parent: Parent of the new device
386 * @drv_name: Driver name to use for the block device
387 * @name: Name for the device (parent name is prepended)
Simon Glassfada3f92022-09-17 09:00:09 -0600388 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glass966b6952016-05-01 11:36:29 -0600389 * @devnum: Device number, specific to the interface type, or -1 to
390 * allocate the next available number
391 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200392 * @lba: Total number of blocks of the device
Simon Glass966b6952016-05-01 11:36:29 -0600393 * @devp: the new device (which has not been probed)
394 */
395int blk_create_devicef(struct udevice *parent, const char *drv_name,
Simon Glassfada3f92022-09-17 09:00:09 -0600396 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200397 lbaint_t lba, struct udevice **devp);
Simon Glass966b6952016-05-01 11:36:29 -0600398
399/**
AKASHI Takahiro3e32dbe2021-12-10 15:49:29 +0900400 * blk_probe_or_unbind() - Try to probe
401 *
402 * Try to probe the device, primarily for enumerating partitions.
403 * If it fails, the device itself is unbound since it means that it won't
404 * work any more.
405 *
406 * @dev: The device to probe
407 * Return: 0 if OK, -ve on error
408 */
409int blk_probe_or_unbind(struct udevice *dev);
410
411/**
Simon Glasscceee552016-02-29 15:25:55 -0700412 * blk_unbind_all() - Unbind all device of the given interface type
413 *
414 * The devices are removed and then unbound.
415 *
Simon Glassfada3f92022-09-17 09:00:09 -0600416 * @uclass_id: Interface type to unbind
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100417 * Return: 0 if OK, -ve on error
Simon Glasscceee552016-02-29 15:25:55 -0700418 */
Simon Glassfada3f92022-09-17 09:00:09 -0600419int blk_unbind_all(int uclass_id);
Simon Glasscceee552016-02-29 15:25:55 -0700420
Simon Glassd089ba32016-05-01 11:36:28 -0600421/**
422 * blk_find_max_devnum() - find the maximum device number for an interface type
423 *
Simon Glassfada3f92022-09-17 09:00:09 -0600424 * Finds the last allocated device number for an interface type @uclass_id. The
Simon Glassd089ba32016-05-01 11:36:28 -0600425 * next number is safe to use for a newly allocated device.
426 *
Simon Glassfada3f92022-09-17 09:00:09 -0600427 * @uclass_id: Interface type to scan
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100428 * Return: maximum device number found, or -ENODEV if none, or other -ve on
Simon Glassd089ba32016-05-01 11:36:28 -0600429 * error
430 */
Simon Glassfada3f92022-09-17 09:00:09 -0600431int blk_find_max_devnum(enum uclass_id uclass_id);
Simon Glassd089ba32016-05-01 11:36:28 -0600432
Simon Glass13c2c292016-05-01 13:52:30 -0600433/**
Bin Mengfd5eda72018-10-15 02:21:09 -0700434 * blk_next_free_devnum() - get the next device number for an interface type
435 *
436 * Finds the next number that is safe to use for a newly allocated device for
Simon Glassfada3f92022-09-17 09:00:09 -0600437 * an interface type @uclass_id.
Bin Mengfd5eda72018-10-15 02:21:09 -0700438 *
Simon Glassfada3f92022-09-17 09:00:09 -0600439 * @uclass_id: Interface type to scan
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100440 * Return: next device number safe to use, or -ve on error
Bin Mengfd5eda72018-10-15 02:21:09 -0700441 */
Simon Glassfada3f92022-09-17 09:00:09 -0600442int blk_next_free_devnum(enum uclass_id uclass_id);
Bin Mengfd5eda72018-10-15 02:21:09 -0700443
444/**
Simon Glass13c2c292016-05-01 13:52:30 -0600445 * blk_select_hwpart() - select a hardware partition
446 *
447 * Select a hardware partition if the device supports it (typically MMC does)
448 *
449 * @dev: Device to update
450 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100451 * Return: 0 if OK, -ve on error
Simon Glass13c2c292016-05-01 13:52:30 -0600452 */
453int blk_select_hwpart(struct udevice *dev, int hwpart);
454
Tom Rini7c41d142017-06-10 10:01:05 -0400455/**
Simon Glassc0bcaaf2022-10-29 19:47:14 -0600456 * blk_find_from_parent() - find a block device by looking up its parent
457 *
458 * All block devices have a parent 'media' device which provides the block
459 * driver for the block device, ensuring that access to the underlying medium
460 * is available.
461 *
462 * The block device is not activated by this function. See
463 * blk_get_from_parent() for that.
464 *
465 * @parent: Media device
466 * @devp: Returns the associated block device, if any
467 * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
468 * UCLASS_BLK child
469 */
470int blk_find_from_parent(struct udevice *parent, struct udevice **devp);
471
472/**
Tom Rini7c41d142017-06-10 10:01:05 -0400473 * blk_get_from_parent() - obtain a block device by looking up its parent
474 *
Simon Glassc0bcaaf2022-10-29 19:47:14 -0600475 * All block devices have a parent 'media' device which provides the block
476 * driver for the block device, ensuring that access to the underlying medium
477 * is available.
478 *
479 * The block device is probed and ready for use.
480 *
481 * @parent: Media device
482 * @devp: Returns the associated block device, if any
483 * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
484 * UCLASS_BLK child
Tom Rini7c41d142017-06-10 10:01:05 -0400485 */
486int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
487
Tien Fong Chee10378522018-07-06 16:26:36 +0800488/**
Simon Glassf3086cf2022-04-24 23:31:03 -0600489 * blk_get_devtype() - Get the device type of a block device
490 *
491 * @dev: Block device to check
492 * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc"
493 */
494const char *blk_get_devtype(struct udevice *dev);
495
496/**
Tien Fong Chee10378522018-07-06 16:26:36 +0800497 * blk_get_by_device() - Get the block device descriptor for the given device
Simon Glass18861002022-10-20 18:22:54 -0600498 * @dev: Instance of a storage device (the parent of the block device)
Tien Fong Chee10378522018-07-06 16:26:36 +0800499 *
500 * Return: With block device descriptor on success , NULL if there is no such
501 * block device.
502 */
503struct blk_desc *blk_get_by_device(struct udevice *dev);
504
Simon Glasscceee552016-02-29 15:25:55 -0700505#else
506#include <errno.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -0700507/*
508 * These functions should take struct udevice instead of struct blk_desc,
509 * but this is convenient for migration to driver model. Add a 'd' prefix
510 * to the function operations, so that blk_read(), etc. can be reserved for
511 * functions with the correct arguments.
512 */
513static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
514 lbaint_t blkcnt, void *buffer)
515{
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700516 ulong blks_read;
Simon Glassfada3f92022-09-17 09:00:09 -0600517 if (blkcache_read(block_dev->uclass_id, block_dev->devnum,
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700518 start, blkcnt, block_dev->blksz, buffer))
519 return blkcnt;
520
Simon Glass2ee8ada2016-02-29 15:25:52 -0700521 /*
522 * We could check if block_read is NULL and return -ENOSYS. But this
523 * bloats the code slightly (cause some board to fail to build), and
524 * it would be an error to try an operation that does not exist.
525 */
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700526 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
527 if (blks_read == blkcnt)
Simon Glassfada3f92022-09-17 09:00:09 -0600528 blkcache_fill(block_dev->uclass_id, block_dev->devnum,
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700529 start, blkcnt, block_dev->blksz, buffer);
530
531 return blks_read;
Simon Glass2ee8ada2016-02-29 15:25:52 -0700532}
533
534static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
535 lbaint_t blkcnt, const void *buffer)
536{
Simon Glassfada3f92022-09-17 09:00:09 -0600537 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700538 return block_dev->block_write(block_dev, start, blkcnt, buffer);
539}
540
541static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
542 lbaint_t blkcnt)
543{
Simon Glassfada3f92022-09-17 09:00:09 -0600544 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700545 return block_dev->block_erase(block_dev, start, blkcnt);
546}
Simon Glass3bf2ab92016-05-01 11:36:03 -0600547
548/**
549 * struct blk_driver - Driver for block interface types
550 *
551 * This provides access to the block devices for each interface type. One
552 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
553 * type that is to be supported.
554 *
Simon Glassfada3f92022-09-17 09:00:09 -0600555 * @uclass_idname: Interface type name
556 * @uclass_id: Interface type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600557 * @max_devs: Maximum number of devices supported
558 * @desc: Pointer to list of devices for this interface type,
559 * or NULL to use @get_dev() instead
560 */
561struct blk_driver {
Simon Glassfada3f92022-09-17 09:00:09 -0600562 const char *uclass_idname;
563 enum uclass_id uclass_id;
Simon Glass3bf2ab92016-05-01 11:36:03 -0600564 int max_devs;
565 struct blk_desc *desc;
566 /**
567 * get_dev() - get a pointer to a block device given its number
568 *
569 * Each interface allocates its own devices and typically
570 * struct blk_desc is contained with the interface's data structure.
571 * There is no global numbering for block devices. This method allows
572 * the device for an interface type to be obtained when @desc is NULL.
573 *
574 * @devnum: Device number (0 for first device on that interface,
575 * 1 for second, etc.
576 * @descp: Returns pointer to the block device on success
577 * @return 0 if OK, -ve on error
578 */
579 int (*get_dev)(int devnum, struct blk_desc **descp);
580
581 /**
582 * select_hwpart() - Select a hardware partition
583 *
584 * Some devices (e.g. MMC) can support partitioning at the hardware
585 * level. This is quite separate from the normal idea of
586 * software-based partitions. MMC hardware partitions must be
587 * explicitly selected. Once selected only the region of the device
588 * covered by that partition is accessible.
589 *
590 * The MMC standard provides for two boot partitions (numbered 1 and 2),
591 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
592 * Partition 0 is the main user-data partition.
593 *
594 * @desc: Block device descriptor
595 * @hwpart: Hardware partition number to select. 0 means the main
596 * user-data partition, 1 is the first partition, 2 is
597 * the second, etc.
598 * @return 0 if OK, other value for an error
599 */
600 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
601};
602
603/*
604 * Declare a new U-Boot legacy block driver. New drivers should use driver
605 * model (UCLASS_BLK).
606 */
607#define U_BOOT_LEGACY_BLK(__name) \
608 ll_entry_declare(struct blk_driver, __name, blk_driver)
609
Simon Glassfada3f92022-09-17 09:00:09 -0600610struct blk_driver *blk_driver_lookup_type(int uclass_id);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600611
Simon Glasscceee552016-02-29 15:25:55 -0700612#endif /* !CONFIG_BLK */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700613
Simon Glass3bf2ab92016-05-01 11:36:03 -0600614/**
Simon Glassfada3f92022-09-17 09:00:09 -0600615 * blk_get_devnum_by_uclass_idname() - Get a block device by type and number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600616 *
617 * This looks through the available block devices of the given type, returning
618 * the one with the given @devnum.
619 *
Simon Glassfada3f92022-09-17 09:00:09 -0600620 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600621 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100622 * Return: point to block device descriptor, or NULL if not found
Simon Glass3bf2ab92016-05-01 11:36:03 -0600623 */
Simon Glassfada3f92022-09-17 09:00:09 -0600624struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600625
626/**
Simon Glassfada3f92022-09-17 09:00:09 -0600627 * blk_get_devnum_by_uclass_id() - Get a block device by type name, and number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600628 *
Simon Glassfada3f92022-09-17 09:00:09 -0600629 * This looks up the block device type based on @uclass_idname, then calls
630 * blk_get_devnum_by_uclass_id().
Simon Glass3bf2ab92016-05-01 11:36:03 -0600631 *
Simon Glassfada3f92022-09-17 09:00:09 -0600632 * @uclass_idname: Block device type name
Simon Glass3bf2ab92016-05-01 11:36:03 -0600633 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100634 * Return: point to block device descriptor, or NULL if not found
Simon Glass3bf2ab92016-05-01 11:36:03 -0600635 */
Simon Glassfada3f92022-09-17 09:00:09 -0600636struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600637 int devnum);
638
639/**
640 * blk_dselect_hwpart() - select a hardware partition
641 *
642 * This selects a hardware partition (such as is supported by MMC). The block
643 * device size may change as this effectively points the block device to a
644 * partition at the hardware level. See the select_hwpart() method above.
645 *
646 * @desc: Block device descriptor for the device to select
647 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100648 * Return: 0 if OK, -ve on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600649 */
650int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
651
652/**
653 * blk_list_part() - list the partitions for block devices of a given type
654 *
Simon Glassfada3f92022-09-17 09:00:09 -0600655 * This looks up the partition type for each block device of type @uclass_id,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600656 * then displays a list of partitions.
657 *
Simon Glassfada3f92022-09-17 09:00:09 -0600658 * @uclass_id: Block device type
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100659 * Return: 0 if OK, -ENODEV if there is none of that type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600660 */
Simon Glassfada3f92022-09-17 09:00:09 -0600661int blk_list_part(enum uclass_id uclass_id);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600662
663/**
664 * blk_list_devices() - list the block devices of a given type
665 *
Simon Glassfada3f92022-09-17 09:00:09 -0600666 * This lists each block device of the type @uclass_id, showing the capacity
Simon Glass3bf2ab92016-05-01 11:36:03 -0600667 * as well as type-specific information.
668 *
Simon Glassfada3f92022-09-17 09:00:09 -0600669 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600670 */
Simon Glassfada3f92022-09-17 09:00:09 -0600671void blk_list_devices(enum uclass_id uclass_id);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600672
673/**
674 * blk_show_device() - show information about a given block device
675 *
676 * This shows the block device capacity as well as type-specific information.
677 *
Simon Glassfada3f92022-09-17 09:00:09 -0600678 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600679 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100680 * Return: 0 if OK, -ENODEV for invalid device number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600681 */
Simon Glassfada3f92022-09-17 09:00:09 -0600682int blk_show_device(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600683
684/**
685 * blk_print_device_num() - show information about a given block device
686 *
687 * This is similar to blk_show_device() but returns an error if the block
688 * device type is unknown.
689 *
Simon Glassfada3f92022-09-17 09:00:09 -0600690 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600691 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100692 * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
Simon Glass3bf2ab92016-05-01 11:36:03 -0600693 * device is not connected
694 */
Simon Glassfada3f92022-09-17 09:00:09 -0600695int blk_print_device_num(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600696
697/**
698 * blk_print_part_devnum() - print the partition information for a device
699 *
Simon Glassfada3f92022-09-17 09:00:09 -0600700 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600701 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100702 * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
Simon Glass3bf2ab92016-05-01 11:36:03 -0600703 * the interface type is not supported, other -ve on other error
704 */
Simon Glassfada3f92022-09-17 09:00:09 -0600705int blk_print_part_devnum(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600706
707/**
708 * blk_read_devnum() - read blocks from a device
709 *
Simon Glassfada3f92022-09-17 09:00:09 -0600710 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600711 * @devnum: Device number
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200712 * @start: Start block number to read (0=first)
Simon Glass3bf2ab92016-05-01 11:36:03 -0600713 * @blkcnt: Number of blocks to read
714 * @buffer: Address to write data to
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100715 * Return: number of blocks read, or -ve error number on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600716 */
Simon Glassfada3f92022-09-17 09:00:09 -0600717ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600718 lbaint_t blkcnt, void *buffer);
719
720/**
721 * blk_write_devnum() - write blocks to a device
722 *
Simon Glassfada3f92022-09-17 09:00:09 -0600723 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600724 * @devnum: Device number
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200725 * @start: Start block number to write (0=first)
Simon Glass3bf2ab92016-05-01 11:36:03 -0600726 * @blkcnt: Number of blocks to write
727 * @buffer: Address to read data from
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100728 * Return: number of blocks written, or -ve error number on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600729 */
Simon Glassfada3f92022-09-17 09:00:09 -0600730ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600731 lbaint_t blkcnt, const void *buffer);
732
733/**
734 * 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