blob: c8cbb64c7f81f51e267837a07f3ec7f5585b3167 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass1a736612016-02-29 15:25:39 -07002/*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass1a736612016-02-29 15:25:39 -07005 */
6
7#ifndef BLK_H
8#define BLK_H
9
Simon Glasse33a5c62022-08-11 19:34:59 -060010#include <dm/uclass-id.h>
Peter Jonesff98cb92017-09-13 18:05:25 -040011#include <efi.h>
12
Simon Glass1a736612016-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 Glass96f37b02021-07-05 16:32:59 -060023struct udevice;
24
Simon Glassa51eb8d2022-08-11 19:34:45 -060025static inline bool blk_enabled(void)
26{
Simon Glass7f8967c2022-08-11 19:34:48 -060027 return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_SPL_LEGACY_BLOCK);
Simon Glassa51eb8d2022-08-11 19:34:45 -060028}
29
Bin Mengeb81b1a2017-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 Kojimace3dbc52021-10-26 17:27:25 +090034#define PART_FORMAT_PCAT 0x1
35#define PART_FORMAT_GPT 0x2
36
Simon Glass09d71aa2016-02-29 15:25:55 -070037/*
Peter Jonesff98cb92017-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 Glass09d71aa2016-02-29 15:25:55 -070049 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
Simon Glasscaa4daa2020-12-03 16:55:18 -070050 * with dev_get_uclass_plat(dev)
Simon Glass09d71aa2016-02-29 15:25:55 -070051 */
Simon Glass1a736612016-02-29 15:25:39 -070052struct blk_desc {
Simon Glass09d71aa2016-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 Glass8149b152022-09-17 09:00:09 -060057 enum uclass_id uclass_id; /* type of the interface */
Simon Glassbcce53d2016-02-29 15:25:51 -070058 int devnum; /* device number */
Simon Glass1a736612016-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
69 lbaint_t lba; /* number of blocks */
70 unsigned long blksz; /* block size */
71 int log2blksz; /* for convenience: log2(blksz) */
Bin Mengeb81b1a2017-09-10 05:12:50 -070072 char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
73 char product[BLK_PRD_SIZE + 1]; /* device product number */
74 char revision[BLK_REV_SIZE + 1]; /* firmware revision */
Peter Jonesff98cb92017-09-13 18:05:25 -040075 enum sig_type sig_type; /* Partition table signature type */
76 union {
77 uint32_t mbr_sig; /* MBR integer signature */
78 efi_guid_t guid_sig; /* GPT GUID Signature */
79 };
Simon Glassc4d660d2017-07-04 13:31:19 -060080#if CONFIG_IS_ENABLED(BLK)
Simon Glassb6694a32016-05-01 13:52:33 -060081 /*
82 * For now we have a few functions which take struct blk_desc as a
83 * parameter. This field allows them to look up the associated
84 * device. Once these functions are removed we can drop this field.
85 */
Simon Glass09d71aa2016-02-29 15:25:55 -070086 struct udevice *bdev;
87#else
Simon Glass1a736612016-02-29 15:25:39 -070088 unsigned long (*block_read)(struct blk_desc *block_dev,
89 lbaint_t start,
90 lbaint_t blkcnt,
91 void *buffer);
92 unsigned long (*block_write)(struct blk_desc *block_dev,
93 lbaint_t start,
94 lbaint_t blkcnt,
95 const void *buffer);
96 unsigned long (*block_erase)(struct blk_desc *block_dev,
97 lbaint_t start,
98 lbaint_t blkcnt);
99 void *priv; /* driver private struct pointer */
Simon Glass09d71aa2016-02-29 15:25:55 -0700100#endif
Simon Glass1a736612016-02-29 15:25:39 -0700101};
102
103#define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
104#define PAD_TO_BLOCKSIZE(size, blk_desc) \
105 (PAD_SIZE(size, blk_desc->blksz))
106
Adam Ford6fef62c2018-06-11 17:17:48 -0500107#if CONFIG_IS_ENABLED(BLOCK_CACHE)
Angelo Durgehello1526bcc2020-01-21 10:37:27 +0100108
109/**
110 * blkcache_init() - initialize the block cache list pointers
111 */
112int blkcache_init(void);
113
Eric Nelsone40cf342016-03-28 10:05:44 -0700114/**
115 * blkcache_read() - attempt to read a set of blocks from cache
116 *
Simon Glass8149b152022-09-17 09:00:09 -0600117 * @param iftype - uclass_id_x for type of device
Eric Nelsone40cf342016-03-28 10:05:44 -0700118 * @param dev - device index of particular type
119 * @param start - starting block number
120 * @param blkcnt - number of blocks to read
121 * @param blksz - size in bytes of each block
Mattijs Korpershoek601d4e12022-10-17 09:35:04 +0200122 * @param buffer - buffer to contain cached data
Eric Nelsone40cf342016-03-28 10:05:44 -0700123 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100124 * Return: - 1 if block returned from cache, 0 otherwise.
Eric Nelsone40cf342016-03-28 10:05:44 -0700125 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700126int blkcache_read(int iftype, int dev,
127 lbaint_t start, lbaint_t blkcnt,
128 unsigned long blksz, void *buffer);
Eric Nelsone40cf342016-03-28 10:05:44 -0700129
130/**
131 * blkcache_fill() - make data read from a block device available
132 * to the block cache
133 *
Simon Glass8149b152022-09-17 09:00:09 -0600134 * @param iftype - uclass_id_x for type of device
Eric Nelsone40cf342016-03-28 10:05:44 -0700135 * @param dev - device index of particular type
136 * @param start - starting block number
137 * @param blkcnt - number of blocks available
138 * @param blksz - size in bytes of each block
Mattijs Korpershoek601d4e12022-10-17 09:35:04 +0200139 * @param buffer - buffer containing data to cache
Eric Nelsone40cf342016-03-28 10:05:44 -0700140 *
141 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700142void blkcache_fill(int iftype, int dev,
143 lbaint_t start, lbaint_t blkcnt,
144 unsigned long blksz, void const *buffer);
Eric Nelsone40cf342016-03-28 10:05:44 -0700145
146/**
147 * blkcache_invalidate() - discard the cache for a set of blocks
148 * because of a write or device (re)initialization.
149 *
Simon Glass5ea894a2022-10-29 19:47:08 -0600150 * @iftype - UCLASS_ID_ for type of device, or -1 for any
151 * @dev - device index of particular type, if @iftype is not -1
Eric Nelsone40cf342016-03-28 10:05:44 -0700152 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700153void blkcache_invalidate(int iftype, int dev);
Eric Nelsone40cf342016-03-28 10:05:44 -0700154
155/**
156 * blkcache_configure() - configure block cache
157 *
158 * @param blocks - maximum blocks per entry
159 * @param entries - maximum entries in cache
160 */
161void blkcache_configure(unsigned blocks, unsigned entries);
162
163/*
164 * statistics of the block cache
165 */
166struct block_cache_stats {
167 unsigned hits;
168 unsigned misses;
169 unsigned entries; /* current entry count */
170 unsigned max_blocks_per_entry;
171 unsigned max_entries;
172};
173
174/**
175 * get_blkcache_stats() - return statistics and reset
176 *
177 * @param stats - statistics are copied here
178 */
179void blkcache_stats(struct block_cache_stats *stats);
180
Simon Glass5ea894a2022-10-29 19:47:08 -0600181/** blkcache_free() - free all memory allocated to the block cache */
182void blkcache_free(void);
183
Eric Nelsone40cf342016-03-28 10:05:44 -0700184#else
185
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700186static inline int blkcache_read(int iftype, int dev,
187 lbaint_t start, lbaint_t blkcnt,
188 unsigned long blksz, void *buffer)
Eric Nelsone40cf342016-03-28 10:05:44 -0700189{
190 return 0;
191}
192
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700193static inline void blkcache_fill(int iftype, int dev,
194 lbaint_t start, lbaint_t blkcnt,
195 unsigned long blksz, void const *buffer) {}
Eric Nelsone40cf342016-03-28 10:05:44 -0700196
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700197static inline void blkcache_invalidate(int iftype, int dev) {}
Eric Nelsone40cf342016-03-28 10:05:44 -0700198
Simon Glass5ea894a2022-10-29 19:47:08 -0600199static inline void blkcache_free(void) {}
200
Eric Nelsone40cf342016-03-28 10:05:44 -0700201#endif
202
Simon Glassc4d660d2017-07-04 13:31:19 -0600203#if CONFIG_IS_ENABLED(BLK)
Simon Glass09d71aa2016-02-29 15:25:55 -0700204struct udevice;
205
206/* Operations on block devices */
207struct blk_ops {
208 /**
209 * read() - read from a block device
210 *
211 * @dev: Device to read from
212 * @start: Start block number to read (0=first)
213 * @blkcnt: Number of blocks to read
214 * @buffer: Destination buffer for data read
215 * @return number of blocks read, or -ve error number (see the
216 * IS_ERR_VALUE() macro
217 */
218 unsigned long (*read)(struct udevice *dev, lbaint_t start,
219 lbaint_t blkcnt, void *buffer);
220
221 /**
222 * write() - write to a block device
223 *
224 * @dev: Device to write to
225 * @start: Start block number to write (0=first)
226 * @blkcnt: Number of blocks to write
227 * @buffer: Source buffer for data to write
228 * @return number of blocks written, or -ve error number (see the
229 * IS_ERR_VALUE() macro
230 */
231 unsigned long (*write)(struct udevice *dev, lbaint_t start,
232 lbaint_t blkcnt, const void *buffer);
233
234 /**
235 * erase() - erase a section of a block device
236 *
237 * @dev: Device to (partially) erase
238 * @start: Start block number to erase (0=first)
239 * @blkcnt: Number of blocks to erase
240 * @return number of blocks erased, or -ve error number (see the
241 * IS_ERR_VALUE() macro
242 */
243 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
244 lbaint_t blkcnt);
Simon Glasscd0fb552016-05-01 13:52:30 -0600245
246 /**
247 * select_hwpart() - select a particular hardware partition
248 *
249 * Some devices (e.g. MMC) can support partitioning at the hardware
250 * level. This is quite separate from the normal idea of
251 * software-based partitions. MMC hardware partitions must be
252 * explicitly selected. Once selected only the region of the device
253 * covered by that partition is accessible.
254 *
255 * The MMC standard provides for two boot partitions (numbered 1 and 2),
256 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
257 *
Mattijs Korpershoek601d4e12022-10-17 09:35:04 +0200258 * @dev: Block device to update
Simon Glasscd0fb552016-05-01 13:52:30 -0600259 * @hwpart: Hardware partition number to select. 0 means the raw
260 * device, 1 is the first partition, 2 is the second, etc.
261 * @return 0 if OK, -ve on error
262 */
263 int (*select_hwpart)(struct udevice *dev, int hwpart);
Simon Glass09d71aa2016-02-29 15:25:55 -0700264};
265
266#define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
267
268/*
269 * These functions should take struct udevice instead of struct blk_desc,
270 * but this is convenient for migration to driver model. Add a 'd' prefix
271 * to the function operations, so that blk_read(), etc. can be reserved for
272 * functions with the correct arguments.
273 */
274unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
275 lbaint_t blkcnt, void *buffer);
276unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
277 lbaint_t blkcnt, const void *buffer);
278unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
279 lbaint_t blkcnt);
280
281/**
Simon Glass606b9262022-10-20 18:22:54 -0600282 * blk_read() - Read from a block device
283 *
284 * @dev: Device to read from
285 * @start: Start block for the read
286 * @blkcnt: Number of blocks to read
287 * @buf: Place to put the data
288 * @return number of blocks read (which may be less than @blkcnt),
289 * or -ve on error. This never returns 0 unless @blkcnt is 0
290 */
291long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
292 void *buffer);
293
294/**
295 * blk_write() - Write to a block device
296 *
297 * @dev: Device to write to
298 * @start: Start block for the write
299 * @blkcnt: Number of blocks to write
300 * @buf: Data to write
301 * @return number of blocks written (which may be less than @blkcnt),
302 * or -ve on error. This never returns 0 unless @blkcnt is 0
303 */
304long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
305 const void *buffer);
306
307/**
308 * blk_erase() - Erase part of a block device
309 *
310 * @dev: Device to erase
311 * @start: Start block for the erase
312 * @blkcnt: Number of blocks to erase
313 * @return number of blocks erased (which may be less than @blkcnt),
314 * or -ve on error. This never returns 0 unless @blkcnt is 0
315 */
316long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
317
318/**
Simon Glass61392812017-04-23 20:02:05 -0600319 * blk_find_device() - Find a block device
320 *
321 * This function does not activate the device. The device will be returned
322 * whether or not it is activated.
323 *
Simon Glass8149b152022-09-17 09:00:09 -0600324 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glass61392812017-04-23 20:02:05 -0600325 * @devnum: Device number (specific to each interface type)
326 * @devp: the device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100327 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glass61392812017-04-23 20:02:05 -0600328 */
Simon Glass8149b152022-09-17 09:00:09 -0600329int blk_find_device(int uclass_id, int devnum, struct udevice **devp);
Simon Glass61392812017-04-23 20:02:05 -0600330
331/**
Simon Glass09d71aa2016-02-29 15:25:55 -0700332 * blk_get_device() - Find and probe a block device ready for use
333 *
Simon Glass8149b152022-09-17 09:00:09 -0600334 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glass09d71aa2016-02-29 15:25:55 -0700335 * @devnum: Device number (specific to each interface type)
336 * @devp: the device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100337 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glass09d71aa2016-02-29 15:25:55 -0700338 */
Simon Glass8149b152022-09-17 09:00:09 -0600339int blk_get_device(int uclass_id, int devnum, struct udevice **devp);
Simon Glass09d71aa2016-02-29 15:25:55 -0700340
341/**
342 * blk_first_device() - Find the first device for a given interface
343 *
344 * The device is probed ready for use
345 *
346 * @devnum: Device number (specific to each interface type)
347 * @devp: the device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100348 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glass09d71aa2016-02-29 15:25:55 -0700349 */
Simon Glass8149b152022-09-17 09:00:09 -0600350int blk_first_device(int uclass_id, struct udevice **devp);
Simon Glass09d71aa2016-02-29 15:25:55 -0700351
352/**
353 * blk_next_device() - Find the next device for a given interface
354 *
355 * This can be called repeatedly after blk_first_device() to iterate through
356 * all devices of the given interface type.
357 *
358 * The device is probed ready for use
359 *
360 * @devp: On entry, the previous device returned. On exit, the next
361 * device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100362 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glass09d71aa2016-02-29 15:25:55 -0700363 */
364int blk_next_device(struct udevice **devp);
365
366/**
367 * blk_create_device() - Create a new block device
368 *
369 * @parent: Parent of the new device
370 * @drv_name: Driver name to use for the block device
371 * @name: Name for the device
Simon Glass8149b152022-09-17 09:00:09 -0600372 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glass52138fd2016-05-01 11:36:28 -0600373 * @devnum: Device number, specific to the interface type, or -1 to
374 * allocate the next available number
Simon Glass09d71aa2016-02-29 15:25:55 -0700375 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200376 * @lba: Total number of blocks of the device
Simon Glass09d71aa2016-02-29 15:25:55 -0700377 * @devp: the new device (which has not been probed)
378 */
379int blk_create_device(struct udevice *parent, const char *drv_name,
Simon Glass8149b152022-09-17 09:00:09 -0600380 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200381 lbaint_t lba, struct udevice **devp);
Simon Glass09d71aa2016-02-29 15:25:55 -0700382
383/**
Simon Glass9107c972016-05-01 11:36:29 -0600384 * blk_create_devicef() - Create a new named block device
385 *
386 * @parent: Parent of the new device
387 * @drv_name: Driver name to use for the block device
388 * @name: Name for the device (parent name is prepended)
Simon Glass8149b152022-09-17 09:00:09 -0600389 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glass9107c972016-05-01 11:36:29 -0600390 * @devnum: Device number, specific to the interface type, or -1 to
391 * allocate the next available number
392 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200393 * @lba: Total number of blocks of the device
Simon Glass9107c972016-05-01 11:36:29 -0600394 * @devp: the new device (which has not been probed)
395 */
396int blk_create_devicef(struct udevice *parent, const char *drv_name,
Simon Glass8149b152022-09-17 09:00:09 -0600397 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200398 lbaint_t lba, struct udevice **devp);
Simon Glass9107c972016-05-01 11:36:29 -0600399
400/**
AKASHI Takahiro19b241c2021-12-10 15:49:29 +0900401 * blk_probe_or_unbind() - Try to probe
402 *
403 * Try to probe the device, primarily for enumerating partitions.
404 * If it fails, the device itself is unbound since it means that it won't
405 * work any more.
406 *
407 * @dev: The device to probe
408 * Return: 0 if OK, -ve on error
409 */
410int blk_probe_or_unbind(struct udevice *dev);
411
412/**
Simon Glass09d71aa2016-02-29 15:25:55 -0700413 * blk_unbind_all() - Unbind all device of the given interface type
414 *
415 * The devices are removed and then unbound.
416 *
Simon Glass8149b152022-09-17 09:00:09 -0600417 * @uclass_id: Interface type to unbind
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100418 * Return: 0 if OK, -ve on error
Simon Glass09d71aa2016-02-29 15:25:55 -0700419 */
Simon Glass8149b152022-09-17 09:00:09 -0600420int blk_unbind_all(int uclass_id);
Simon Glass09d71aa2016-02-29 15:25:55 -0700421
Simon Glass52138fd2016-05-01 11:36:28 -0600422/**
423 * blk_find_max_devnum() - find the maximum device number for an interface type
424 *
Simon Glass8149b152022-09-17 09:00:09 -0600425 * Finds the last allocated device number for an interface type @uclass_id. The
Simon Glass52138fd2016-05-01 11:36:28 -0600426 * next number is safe to use for a newly allocated device.
427 *
Simon Glass8149b152022-09-17 09:00:09 -0600428 * @uclass_id: Interface type to scan
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100429 * Return: maximum device number found, or -ENODEV if none, or other -ve on
Simon Glass52138fd2016-05-01 11:36:28 -0600430 * error
431 */
Simon Glass8149b152022-09-17 09:00:09 -0600432int blk_find_max_devnum(enum uclass_id uclass_id);
Simon Glass52138fd2016-05-01 11:36:28 -0600433
Simon Glasscd0fb552016-05-01 13:52:30 -0600434/**
Bin Mengc879eeb2018-10-15 02:21:09 -0700435 * blk_next_free_devnum() - get the next device number for an interface type
436 *
437 * Finds the next number that is safe to use for a newly allocated device for
Simon Glass8149b152022-09-17 09:00:09 -0600438 * an interface type @uclass_id.
Bin Mengc879eeb2018-10-15 02:21:09 -0700439 *
Simon Glass8149b152022-09-17 09:00:09 -0600440 * @uclass_id: Interface type to scan
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100441 * Return: next device number safe to use, or -ve on error
Bin Mengc879eeb2018-10-15 02:21:09 -0700442 */
Simon Glass8149b152022-09-17 09:00:09 -0600443int blk_next_free_devnum(enum uclass_id uclass_id);
Bin Mengc879eeb2018-10-15 02:21:09 -0700444
445/**
Simon Glasscd0fb552016-05-01 13:52:30 -0600446 * blk_select_hwpart() - select a hardware partition
447 *
448 * Select a hardware partition if the device supports it (typically MMC does)
449 *
450 * @dev: Device to update
451 * @hwpart: Partition number to select
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100452 * Return: 0 if OK, -ve on error
Simon Glasscd0fb552016-05-01 13:52:30 -0600453 */
454int blk_select_hwpart(struct udevice *dev, int hwpart);
455
Tom Rini4bdb49a2017-06-10 10:01:05 -0400456/**
457 * blk_get_from_parent() - obtain a block device by looking up its parent
458 *
459 * All devices with
460 */
461int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
462
Tien Fong Cheebc53d262018-07-06 16:26:36 +0800463/**
Simon Glass87571b72022-04-24 23:31:03 -0600464 * blk_get_devtype() - Get the device type of a block device
465 *
466 * @dev: Block device to check
467 * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc"
468 */
469const char *blk_get_devtype(struct udevice *dev);
470
471/**
Tien Fong Cheebc53d262018-07-06 16:26:36 +0800472 * blk_get_by_device() - Get the block device descriptor for the given device
Simon Glass606b9262022-10-20 18:22:54 -0600473 * @dev: Instance of a storage device (the parent of the block device)
Tien Fong Cheebc53d262018-07-06 16:26:36 +0800474 *
475 * Return: With block device descriptor on success , NULL if there is no such
476 * block device.
477 */
478struct blk_desc *blk_get_by_device(struct udevice *dev);
479
Simon Glass09d71aa2016-02-29 15:25:55 -0700480#else
481#include <errno.h>
Simon Glass2a981dc2016-02-29 15:25:52 -0700482/*
483 * These functions should take struct udevice instead of struct blk_desc,
484 * but this is convenient for migration to driver model. Add a 'd' prefix
485 * to the function operations, so that blk_read(), etc. can be reserved for
486 * functions with the correct arguments.
487 */
488static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
489 lbaint_t blkcnt, void *buffer)
490{
Eric Nelsone40cf342016-03-28 10:05:44 -0700491 ulong blks_read;
Simon Glass8149b152022-09-17 09:00:09 -0600492 if (blkcache_read(block_dev->uclass_id, block_dev->devnum,
Eric Nelsone40cf342016-03-28 10:05:44 -0700493 start, blkcnt, block_dev->blksz, buffer))
494 return blkcnt;
495
Simon Glass2a981dc2016-02-29 15:25:52 -0700496 /*
497 * We could check if block_read is NULL and return -ENOSYS. But this
498 * bloats the code slightly (cause some board to fail to build), and
499 * it would be an error to try an operation that does not exist.
500 */
Eric Nelsone40cf342016-03-28 10:05:44 -0700501 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
502 if (blks_read == blkcnt)
Simon Glass8149b152022-09-17 09:00:09 -0600503 blkcache_fill(block_dev->uclass_id, block_dev->devnum,
Eric Nelsone40cf342016-03-28 10:05:44 -0700504 start, blkcnt, block_dev->blksz, buffer);
505
506 return blks_read;
Simon Glass2a981dc2016-02-29 15:25:52 -0700507}
508
509static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
510 lbaint_t blkcnt, const void *buffer)
511{
Simon Glass8149b152022-09-17 09:00:09 -0600512 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass2a981dc2016-02-29 15:25:52 -0700513 return block_dev->block_write(block_dev, start, blkcnt, buffer);
514}
515
516static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
517 lbaint_t blkcnt)
518{
Simon Glass8149b152022-09-17 09:00:09 -0600519 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass2a981dc2016-02-29 15:25:52 -0700520 return block_dev->block_erase(block_dev, start, blkcnt);
521}
Simon Glass6eef6ea2016-05-01 11:36:03 -0600522
523/**
524 * struct blk_driver - Driver for block interface types
525 *
526 * This provides access to the block devices for each interface type. One
527 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
528 * type that is to be supported.
529 *
Simon Glass8149b152022-09-17 09:00:09 -0600530 * @uclass_idname: Interface type name
531 * @uclass_id: Interface type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600532 * @max_devs: Maximum number of devices supported
533 * @desc: Pointer to list of devices for this interface type,
534 * or NULL to use @get_dev() instead
535 */
536struct blk_driver {
Simon Glass8149b152022-09-17 09:00:09 -0600537 const char *uclass_idname;
538 enum uclass_id uclass_id;
Simon Glass6eef6ea2016-05-01 11:36:03 -0600539 int max_devs;
540 struct blk_desc *desc;
541 /**
542 * get_dev() - get a pointer to a block device given its number
543 *
544 * Each interface allocates its own devices and typically
545 * struct blk_desc is contained with the interface's data structure.
546 * There is no global numbering for block devices. This method allows
547 * the device for an interface type to be obtained when @desc is NULL.
548 *
549 * @devnum: Device number (0 for first device on that interface,
550 * 1 for second, etc.
551 * @descp: Returns pointer to the block device on success
552 * @return 0 if OK, -ve on error
553 */
554 int (*get_dev)(int devnum, struct blk_desc **descp);
555
556 /**
557 * select_hwpart() - Select a hardware partition
558 *
559 * Some devices (e.g. MMC) can support partitioning at the hardware
560 * level. This is quite separate from the normal idea of
561 * software-based partitions. MMC hardware partitions must be
562 * explicitly selected. Once selected only the region of the device
563 * covered by that partition is accessible.
564 *
565 * The MMC standard provides for two boot partitions (numbered 1 and 2),
566 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
567 * Partition 0 is the main user-data partition.
568 *
569 * @desc: Block device descriptor
570 * @hwpart: Hardware partition number to select. 0 means the main
571 * user-data partition, 1 is the first partition, 2 is
572 * the second, etc.
573 * @return 0 if OK, other value for an error
574 */
575 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
576};
577
578/*
579 * Declare a new U-Boot legacy block driver. New drivers should use driver
580 * model (UCLASS_BLK).
581 */
582#define U_BOOT_LEGACY_BLK(__name) \
583 ll_entry_declare(struct blk_driver, __name, blk_driver)
584
Simon Glass8149b152022-09-17 09:00:09 -0600585struct blk_driver *blk_driver_lookup_type(int uclass_id);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600586
Simon Glass09d71aa2016-02-29 15:25:55 -0700587#endif /* !CONFIG_BLK */
Simon Glass2a981dc2016-02-29 15:25:52 -0700588
Simon Glass6eef6ea2016-05-01 11:36:03 -0600589/**
Simon Glass8149b152022-09-17 09:00:09 -0600590 * blk_get_devnum_by_uclass_idname() - Get a block device by type and number
Simon Glass6eef6ea2016-05-01 11:36:03 -0600591 *
592 * This looks through the available block devices of the given type, returning
593 * the one with the given @devnum.
594 *
Simon Glass8149b152022-09-17 09:00:09 -0600595 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600596 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100597 * Return: point to block device descriptor, or NULL if not found
Simon Glass6eef6ea2016-05-01 11:36:03 -0600598 */
Simon Glass8149b152022-09-17 09:00:09 -0600599struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600600
601/**
Simon Glass8149b152022-09-17 09:00:09 -0600602 * blk_get_devnum_by_uclass_id() - Get a block device by type name, and number
Simon Glass6eef6ea2016-05-01 11:36:03 -0600603 *
Simon Glass8149b152022-09-17 09:00:09 -0600604 * This looks up the block device type based on @uclass_idname, then calls
605 * blk_get_devnum_by_uclass_id().
Simon Glass6eef6ea2016-05-01 11:36:03 -0600606 *
Simon Glass8149b152022-09-17 09:00:09 -0600607 * @uclass_idname: Block device type name
Simon Glass6eef6ea2016-05-01 11:36:03 -0600608 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100609 * Return: point to block device descriptor, or NULL if not found
Simon Glass6eef6ea2016-05-01 11:36:03 -0600610 */
Simon Glass8149b152022-09-17 09:00:09 -0600611struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname,
Simon Glass6eef6ea2016-05-01 11:36:03 -0600612 int devnum);
613
614/**
615 * blk_dselect_hwpart() - select a hardware partition
616 *
617 * This selects a hardware partition (such as is supported by MMC). The block
618 * device size may change as this effectively points the block device to a
619 * partition at the hardware level. See the select_hwpart() method above.
620 *
621 * @desc: Block device descriptor for the device to select
622 * @hwpart: Partition number to select
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100623 * Return: 0 if OK, -ve on error
Simon Glass6eef6ea2016-05-01 11:36:03 -0600624 */
625int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
626
627/**
628 * blk_list_part() - list the partitions for block devices of a given type
629 *
Simon Glass8149b152022-09-17 09:00:09 -0600630 * This looks up the partition type for each block device of type @uclass_id,
Simon Glass6eef6ea2016-05-01 11:36:03 -0600631 * then displays a list of partitions.
632 *
Simon Glass8149b152022-09-17 09:00:09 -0600633 * @uclass_id: Block device type
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100634 * Return: 0 if OK, -ENODEV if there is none of that type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600635 */
Simon Glass8149b152022-09-17 09:00:09 -0600636int blk_list_part(enum uclass_id uclass_id);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600637
638/**
639 * blk_list_devices() - list the block devices of a given type
640 *
Simon Glass8149b152022-09-17 09:00:09 -0600641 * This lists each block device of the type @uclass_id, showing the capacity
Simon Glass6eef6ea2016-05-01 11:36:03 -0600642 * as well as type-specific information.
643 *
Simon Glass8149b152022-09-17 09:00:09 -0600644 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600645 */
Simon Glass8149b152022-09-17 09:00:09 -0600646void blk_list_devices(enum uclass_id uclass_id);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600647
648/**
649 * blk_show_device() - show information about a given block device
650 *
651 * This shows the block device capacity as well as type-specific information.
652 *
Simon Glass8149b152022-09-17 09:00:09 -0600653 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600654 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100655 * Return: 0 if OK, -ENODEV for invalid device number
Simon Glass6eef6ea2016-05-01 11:36:03 -0600656 */
Simon Glass8149b152022-09-17 09:00:09 -0600657int blk_show_device(enum uclass_id uclass_id, int devnum);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600658
659/**
660 * blk_print_device_num() - show information about a given block device
661 *
662 * This is similar to blk_show_device() but returns an error if the block
663 * device type is unknown.
664 *
Simon Glass8149b152022-09-17 09:00:09 -0600665 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600666 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100667 * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
Simon Glass6eef6ea2016-05-01 11:36:03 -0600668 * device is not connected
669 */
Simon Glass8149b152022-09-17 09:00:09 -0600670int blk_print_device_num(enum uclass_id uclass_id, int devnum);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600671
672/**
673 * blk_print_part_devnum() - print the partition information for a device
674 *
Simon Glass8149b152022-09-17 09:00:09 -0600675 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600676 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100677 * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
Simon Glass6eef6ea2016-05-01 11:36:03 -0600678 * the interface type is not supported, other -ve on other error
679 */
Simon Glass8149b152022-09-17 09:00:09 -0600680int blk_print_part_devnum(enum uclass_id uclass_id, int devnum);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600681
682/**
683 * blk_read_devnum() - read blocks from a device
684 *
Simon Glass8149b152022-09-17 09:00:09 -0600685 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600686 * @devnum: Device number
Mattijs Korpershoek601d4e12022-10-17 09:35:04 +0200687 * @start: Start block number to read (0=first)
Simon Glass6eef6ea2016-05-01 11:36:03 -0600688 * @blkcnt: Number of blocks to read
689 * @buffer: Address to write data to
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100690 * Return: number of blocks read, or -ve error number on error
Simon Glass6eef6ea2016-05-01 11:36:03 -0600691 */
Simon Glass8149b152022-09-17 09:00:09 -0600692ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glass6eef6ea2016-05-01 11:36:03 -0600693 lbaint_t blkcnt, void *buffer);
694
695/**
696 * blk_write_devnum() - write blocks to a device
697 *
Simon Glass8149b152022-09-17 09:00:09 -0600698 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600699 * @devnum: Device number
Mattijs Korpershoek601d4e12022-10-17 09:35:04 +0200700 * @start: Start block number to write (0=first)
Simon Glass6eef6ea2016-05-01 11:36:03 -0600701 * @blkcnt: Number of blocks to write
702 * @buffer: Address to read data from
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100703 * Return: number of blocks written, or -ve error number on error
Simon Glass6eef6ea2016-05-01 11:36:03 -0600704 */
Simon Glass8149b152022-09-17 09:00:09 -0600705ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glass6eef6ea2016-05-01 11:36:03 -0600706 lbaint_t blkcnt, const void *buffer);
707
708/**
709 * blk_select_hwpart_devnum() - select a hardware partition
710 *
711 * This is similar to blk_dselect_hwpart() but it looks up the interface and
712 * device number.
713 *
Simon Glass8149b152022-09-17 09:00:09 -0600714 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600715 * @devnum: Device number
716 * @hwpart: Partition number to select
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100717 * Return: 0 if OK, -ve on error
Simon Glass6eef6ea2016-05-01 11:36:03 -0600718 */
Simon Glass8149b152022-09-17 09:00:09 -0600719int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600720
Simon Glass6faa4ed2017-07-29 11:34:53 -0600721/**
Simon Glass8149b152022-09-17 09:00:09 -0600722 * blk_get_uclass_name() - Get the name of an interface type
Simon Glass6faa4ed2017-07-29 11:34:53 -0600723 *
Simon Glass8149b152022-09-17 09:00:09 -0600724 * @uclass_id: Interface type to check
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100725 * Return: name of interface, or NULL if none
Simon Glass6faa4ed2017-07-29 11:34:53 -0600726 */
Simon Glass8149b152022-09-17 09:00:09 -0600727const char *blk_get_uclass_name(enum uclass_id uclass_id);
Simon Glass6faa4ed2017-07-29 11:34:53 -0600728
Simon Glass4395f662017-07-29 11:34:54 -0600729/**
730 * blk_common_cmd() - handle common commands with block devices
731 *
732 * @args: Number of arguments to the command (argv[0] is the command itself)
733 * @argv: Command arguments
Simon Glass8149b152022-09-17 09:00:09 -0600734 * @uclass_id: Interface type
Simon Glass4395f662017-07-29 11:34:54 -0600735 * @cur_devnump: Current device number for this interface type
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100736 * Return: 0 if OK, CMD_RET_ERROR on error
Simon Glass4395f662017-07-29 11:34:54 -0600737 */
Simon Glass8149b152022-09-17 09:00:09 -0600738int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
Simon Glass4395f662017-07-29 11:34:54 -0600739 int *cur_devnump);
740
Simon Glass96f37b02021-07-05 16:32:59 -0600741enum blk_flag_t {
742 BLKF_FIXED = 1 << 0,
743 BLKF_REMOVABLE = 1 << 1,
744 BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE,
745};
746
747/**
748 * blk_first_device_err() - Get the first block device
749 *
750 * The device returned is probed if necessary, and ready for use
751 *
752 * @flags: Indicates type of device to return
753 * @devp: Returns pointer to the first device in that uclass, or NULL if none
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100754 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glass96f37b02021-07-05 16:32:59 -0600755 */
756int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
757
758/**
759 * blk_next_device_err() - Get the next block device
760 *
761 * The device returned is probed if necessary, and ready for use
762 *
763 * @flags: Indicates type of device to return
764 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
765 * to the next device in the uclass if no error occurred, or -ENODEV if
766 * there is no next device.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100767 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glass96f37b02021-07-05 16:32:59 -0600768 */
769int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
770
771/**
Simon Glass49e86682022-02-28 12:08:35 -0700772 * blk_find_first() - Return the first matching block device
773 * @flags: Indicates type of device to return
774 * @devp: Returns pointer to device, or NULL on error
775 *
776 * The device is not prepared for use - this is an internal function.
777 * The function uclass_get_device_tail() can be used to probe the device.
778 *
779 * Note that some devices are considered removable until they have been probed
780 *
781 * @return 0 if found, -ENODEV if not found
782 */
783int blk_find_first(enum blk_flag_t flags, struct udevice **devp);
784
785/**
786 * blk_find_next() - Return the next matching block device
787 * @flags: Indicates type of device to return
788 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
789 * to the next device in the same uclass, or NULL if none
790 *
791 * The device is not prepared for use - this is an internal function.
792 * The function uclass_get_device_tail() can be used to probe the device.
793 *
794 * Note that some devices are considered removable until they have been probed
795 *
796 * @return 0 if found, -ENODEV if not found
797 */
798int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
799
800/**
801 * blk_foreach() - iterate through block devices
802 *
803 * This creates a for() loop which works through the available block devices in
804 * order from start to end.
805 *
806 * If for some reason the uclass cannot be found, this does nothing.
807 *
808 * @_flags: Indicates type of device to return
809 * @_pos: struct udevice * to hold the current device. Set to NULL when there
810 * are no more devices.
811 */
812#define blk_foreach(_flags, _pos) \
813 for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \
814 _ret = blk_find_next(_flags, &_pos))
815
816/**
Simon Glass96f37b02021-07-05 16:32:59 -0600817 * blk_foreach_probe() - Helper function to iteration through block devices
818 *
819 * This creates a for() loop which works through the available devices in
820 * a uclass in order from start to end. Devices are probed if necessary,
821 * and ready for use.
822 *
823 * @flags: Indicates type of device to return
824 * @dev: struct udevice * to hold the current device. Set to NULL when there
825 * are no more devices.
826 */
827#define blk_foreach_probe(flags, pos) \
828 for (int _ret = blk_first_device_err(flags, &(pos)); \
829 !_ret && pos; \
830 _ret = blk_next_device_err(flags, &(pos)))
831
832/**
833 * blk_count_devices() - count the number of devices of a particular type
834 *
835 * @flags: Indicates type of device to find
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100836 * Return: number of devices matching those flags
Simon Glass96f37b02021-07-05 16:32:59 -0600837 */
838int blk_count_devices(enum blk_flag_t flag);
839
Simon Glass1a736612016-02-29 15:25:39 -0700840#endif