blob: 95e86e2d5d16cba0be33fc7557b7d99b8de999e6 [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
Simon Glassfc7a7442021-07-05 16:32:59 -060024struct udevice;
25
Simon Glassf5ac3032022-08-11 19:34:45 -060026static inline bool blk_enabled(void)
27{
Simon Glass3bf7d7a2022-08-11 19:34:48 -060028 return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_SPL_LEGACY_BLOCK);
Simon Glassf5ac3032022-08-11 19:34:45 -060029}
30
Bin Mengcf406d22017-09-10 05:12:50 -070031#define BLK_VEN_SIZE 40
32#define BLK_PRD_SIZE 20
33#define BLK_REV_SIZE 8
34
Masahisa Kojima6460c3e2021-10-26 17:27:25 +090035#define PART_FORMAT_PCAT 0x1
36#define PART_FORMAT_GPT 0x2
37
Simon Glasscceee552016-02-29 15:25:55 -070038/*
Peter Jonesc27e1c12017-09-13 18:05:25 -040039 * Identifies the partition table type (ie. MBR vs GPT GUID) signature
40 */
41enum sig_type {
42 SIG_TYPE_NONE,
43 SIG_TYPE_MBR,
44 SIG_TYPE_GUID,
45
46 SIG_TYPE_COUNT /* Number of signature types */
47};
48
49/*
Simon Glasscceee552016-02-29 15:25:55 -070050 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
Simon Glass71fa5b42020-12-03 16:55:18 -070051 * with dev_get_uclass_plat(dev)
Simon Glasscceee552016-02-29 15:25:55 -070052 */
Simon Glassac8162f2016-02-29 15:25:39 -070053struct blk_desc {
Simon Glasscceee552016-02-29 15:25:55 -070054 /*
55 * TODO: With driver model we should be able to use the parent
56 * device's uclass instead.
57 */
Simon Glassfada3f92022-09-17 09:00:09 -060058 enum uclass_id uclass_id; /* type of the interface */
Simon Glass2f26fff2016-02-29 15:25:51 -070059 int devnum; /* device number */
Simon Glassac8162f2016-02-29 15:25:39 -070060 unsigned char part_type; /* partition type */
61 unsigned char target; /* target SCSI ID */
62 unsigned char lun; /* target LUN */
63 unsigned char hwpart; /* HW partition, e.g. for eMMC */
64 unsigned char type; /* device type */
65 unsigned char removable; /* removable device */
Simon Glassac8162f2016-02-29 15:25:39 -070066 /* device can use 48bit addr (ATA/ATAPI v7) */
Simon Glass61317282023-04-25 10:54:41 -060067 bool lba48;
Simon Glass631db662023-04-25 10:54:35 -060068 unsigned char atapi; /* Use ATAPI protocol */
Simon Glassac8162f2016-02-29 15:25:39 -070069 lbaint_t lba; /* number of blocks */
70 unsigned long blksz; /* block size */
71 int log2blksz; /* for convenience: log2(blksz) */
Bin Mengcf406d22017-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 Jonesc27e1c12017-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 Glass5f4bd8c2017-07-04 13:31:19 -060080#if CONFIG_IS_ENABLED(BLK)
Simon Glass8f5f7222016-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 Glasscceee552016-02-29 15:25:55 -070086 struct udevice *bdev;
87#else
Simon Glassac8162f2016-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 Glasscceee552016-02-29 15:25:55 -0700100#endif
Simon Glassac8162f2016-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 Fordd693fb92018-06-11 17:17:48 -0500107#if CONFIG_IS_ENABLED(BLOCK_CACHE)
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700108/**
109 * blkcache_read() - attempt to read a set of blocks from cache
110 *
Simon Glassfada3f92022-09-17 09:00:09 -0600111 * @param iftype - uclass_id_x for type of device
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700112 * @param dev - device index of particular type
113 * @param start - starting block number
114 * @param blkcnt - number of blocks to read
115 * @param blksz - size in bytes of each block
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200116 * @param buffer - buffer to contain cached data
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700117 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100118 * Return: - 1 if block returned from cache, 0 otherwise.
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700119 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700120int blkcache_read(int iftype, int dev,
121 lbaint_t start, lbaint_t blkcnt,
122 unsigned long blksz, void *buffer);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700123
124/**
125 * blkcache_fill() - make data read from a block device available
126 * to the block cache
127 *
Simon Glassfada3f92022-09-17 09:00:09 -0600128 * @param iftype - uclass_id_x for type of device
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700129 * @param dev - device index of particular type
130 * @param start - starting block number
131 * @param blkcnt - number of blocks available
132 * @param blksz - size in bytes of each block
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200133 * @param buffer - buffer containing data to cache
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700134 *
135 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700136void blkcache_fill(int iftype, int dev,
137 lbaint_t start, lbaint_t blkcnt,
138 unsigned long blksz, void const *buffer);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700139
140/**
141 * blkcache_invalidate() - discard the cache for a set of blocks
142 * because of a write or device (re)initialization.
143 *
Simon Glass76c62692022-10-29 19:47:08 -0600144 * @iftype - UCLASS_ID_ for type of device, or -1 for any
145 * @dev - device index of particular type, if @iftype is not -1
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700146 */
Eric Nelsonf201f232016-04-02 07:37:14 -0700147void blkcache_invalidate(int iftype, int dev);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700148
149/**
150 * blkcache_configure() - configure block cache
151 *
152 * @param blocks - maximum blocks per entry
153 * @param entries - maximum entries in cache
154 */
155void blkcache_configure(unsigned blocks, unsigned entries);
156
157/*
158 * statistics of the block cache
159 */
160struct block_cache_stats {
161 unsigned hits;
162 unsigned misses;
163 unsigned entries; /* current entry count */
164 unsigned max_blocks_per_entry;
165 unsigned max_entries;
166};
167
168/**
169 * get_blkcache_stats() - return statistics and reset
170 *
171 * @param stats - statistics are copied here
172 */
173void blkcache_stats(struct block_cache_stats *stats);
174
Simon Glass76c62692022-10-29 19:47:08 -0600175/** blkcache_free() - free all memory allocated to the block cache */
176void blkcache_free(void);
177
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700178#else
179
Eric Nelsonf201f232016-04-02 07:37:14 -0700180static inline int blkcache_read(int iftype, int dev,
181 lbaint_t start, lbaint_t blkcnt,
182 unsigned long blksz, void *buffer)
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700183{
184 return 0;
185}
186
Eric Nelsonf201f232016-04-02 07:37:14 -0700187static inline void blkcache_fill(int iftype, int dev,
188 lbaint_t start, lbaint_t blkcnt,
189 unsigned long blksz, void const *buffer) {}
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700190
Eric Nelsonf201f232016-04-02 07:37:14 -0700191static inline void blkcache_invalidate(int iftype, int dev) {}
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700192
Simon Glass76c62692022-10-29 19:47:08 -0600193static inline void blkcache_free(void) {}
194
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700195#endif
196
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600197#if CONFIG_IS_ENABLED(BLK)
Simon Glasscceee552016-02-29 15:25:55 -0700198struct udevice;
199
200/* Operations on block devices */
201struct blk_ops {
202 /**
203 * read() - read from a block device
204 *
205 * @dev: Device to read from
206 * @start: Start block number to read (0=first)
207 * @blkcnt: Number of blocks to read
208 * @buffer: Destination buffer for data read
209 * @return number of blocks read, or -ve error number (see the
210 * IS_ERR_VALUE() macro
211 */
212 unsigned long (*read)(struct udevice *dev, lbaint_t start,
213 lbaint_t blkcnt, void *buffer);
214
215 /**
216 * write() - write to a block device
217 *
218 * @dev: Device to write to
219 * @start: Start block number to write (0=first)
220 * @blkcnt: Number of blocks to write
221 * @buffer: Source buffer for data to write
222 * @return number of blocks written, or -ve error number (see the
223 * IS_ERR_VALUE() macro
224 */
225 unsigned long (*write)(struct udevice *dev, lbaint_t start,
226 lbaint_t blkcnt, const void *buffer);
227
228 /**
229 * erase() - erase a section of a block device
230 *
231 * @dev: Device to (partially) erase
232 * @start: Start block number to erase (0=first)
233 * @blkcnt: Number of blocks to erase
234 * @return number of blocks erased, or -ve error number (see the
235 * IS_ERR_VALUE() macro
236 */
237 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
238 lbaint_t blkcnt);
Simon Glass13c2c292016-05-01 13:52:30 -0600239
240 /**
241 * select_hwpart() - select a particular hardware partition
242 *
243 * Some devices (e.g. MMC) can support partitioning at the hardware
244 * level. This is quite separate from the normal idea of
245 * software-based partitions. MMC hardware partitions must be
246 * explicitly selected. Once selected only the region of the device
247 * covered by that partition is accessible.
248 *
249 * The MMC standard provides for two boot partitions (numbered 1 and 2),
250 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
251 *
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200252 * @dev: Block device to update
Simon Glass13c2c292016-05-01 13:52:30 -0600253 * @hwpart: Hardware partition number to select. 0 means the raw
254 * device, 1 is the first partition, 2 is the second, etc.
255 * @return 0 if OK, -ve on error
256 */
257 int (*select_hwpart)(struct udevice *dev, int hwpart);
Marek Vasut847e24f2023-08-14 01:49:59 +0200258
259#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
260 /**
261 * buffer_aligned() - test memory alignment of block operation buffer
262 *
263 * Some devices have limited DMA capabilities and require that the
264 * buffers passed to them fit specific properties. This optional
265 * callback can be used to indicate whether a buffer alignment is
266 * suitable for the device DMA or not, and trigger use of generic
267 * bounce buffer implementation to help use of unsuitable buffers
268 * at the expense of performance degradation.
269 *
270 * @dev: Block device associated with the request
271 * @state: Bounce buffer state
272 * @return 1 if OK, 0 if unaligned
273 */
274 int (*buffer_aligned)(struct udevice *dev, struct bounce_buffer *state);
275#endif /* CONFIG_BOUNCE_BUFFER */
Simon Glasscceee552016-02-29 15:25:55 -0700276};
277
Simon Glasscceee552016-02-29 15:25:55 -0700278/*
279 * These functions should take struct udevice instead of struct blk_desc,
280 * but this is convenient for migration to driver model. Add a 'd' prefix
281 * to the function operations, so that blk_read(), etc. can be reserved for
282 * functions with the correct arguments.
283 */
284unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
285 lbaint_t blkcnt, void *buffer);
286unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
287 lbaint_t blkcnt, const void *buffer);
288unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
289 lbaint_t blkcnt);
290
291/**
Simon Glass18861002022-10-20 18:22:54 -0600292 * blk_read() - Read from a block device
293 *
294 * @dev: Device to read from
295 * @start: Start block for the read
296 * @blkcnt: Number of blocks to read
297 * @buf: Place to put the data
298 * @return number of blocks read (which may be less than @blkcnt),
299 * or -ve on error. This never returns 0 unless @blkcnt is 0
300 */
301long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
302 void *buffer);
303
304/**
305 * blk_write() - Write to a block device
306 *
307 * @dev: Device to write to
308 * @start: Start block for the write
309 * @blkcnt: Number of blocks to write
310 * @buf: Data to write
311 * @return number of blocks written (which may be less than @blkcnt),
312 * or -ve on error. This never returns 0 unless @blkcnt is 0
313 */
314long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
315 const void *buffer);
316
317/**
318 * blk_erase() - Erase part of a block device
319 *
320 * @dev: Device to erase
321 * @start: Start block for the erase
322 * @blkcnt: Number of blocks to erase
323 * @return number of blocks erased (which may be less than @blkcnt),
324 * or -ve on error. This never returns 0 unless @blkcnt is 0
325 */
326long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
327
328/**
Simon Glassd5d4c102017-04-23 20:02:05 -0600329 * blk_find_device() - Find a block device
330 *
331 * This function does not activate the device. The device will be returned
332 * whether or not it is activated.
333 *
Simon Glassfada3f92022-09-17 09:00:09 -0600334 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glassd5d4c102017-04-23 20:02:05 -0600335 * @devnum: Device number (specific to each interface type)
336 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100337 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glassd5d4c102017-04-23 20:02:05 -0600338 */
Simon Glassfada3f92022-09-17 09:00:09 -0600339int blk_find_device(int uclass_id, int devnum, struct udevice **devp);
Simon Glassd5d4c102017-04-23 20:02:05 -0600340
341/**
Simon Glasscceee552016-02-29 15:25:55 -0700342 * blk_get_device() - Find and probe a block device ready for use
343 *
Simon Glassfada3f92022-09-17 09:00:09 -0600344 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glasscceee552016-02-29 15:25:55 -0700345 * @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 found, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700348 */
Simon Glassfada3f92022-09-17 09:00:09 -0600349int blk_get_device(int uclass_id, int devnum, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700350
351/**
352 * blk_first_device() - Find the first device for a given interface
353 *
354 * The device is probed ready for use
355 *
356 * @devnum: Device number (specific to each interface type)
357 * @devp: the device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100358 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700359 */
Simon Glassfada3f92022-09-17 09:00:09 -0600360int blk_first_device(int uclass_id, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700361
362/**
363 * blk_next_device() - Find the next device for a given interface
364 *
365 * This can be called repeatedly after blk_first_device() to iterate through
366 * all devices of the given interface type.
367 *
368 * The device is probed ready for use
369 *
370 * @devp: On entry, the previous device returned. On exit, the next
371 * device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100372 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glasscceee552016-02-29 15:25:55 -0700373 */
374int blk_next_device(struct udevice **devp);
375
376/**
377 * blk_create_device() - Create a new block device
378 *
379 * @parent: Parent of the new device
380 * @drv_name: Driver name to use for the block device
381 * @name: Name for the device
Simon Glassfada3f92022-09-17 09:00:09 -0600382 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glassd089ba32016-05-01 11:36:28 -0600383 * @devnum: Device number, specific to the interface type, or -1 to
384 * allocate the next available number
Simon Glasscceee552016-02-29 15:25:55 -0700385 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200386 * @lba: Total number of blocks of the device
Simon Glasscceee552016-02-29 15:25:55 -0700387 * @devp: the new device (which has not been probed)
388 */
389int blk_create_device(struct udevice *parent, const char *drv_name,
Simon Glassfada3f92022-09-17 09:00:09 -0600390 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200391 lbaint_t lba, struct udevice **devp);
Simon Glasscceee552016-02-29 15:25:55 -0700392
393/**
Simon Glass966b6952016-05-01 11:36:29 -0600394 * blk_create_devicef() - Create a new named block device
395 *
396 * @parent: Parent of the new device
397 * @drv_name: Driver name to use for the block device
398 * @name: Name for the device (parent name is prepended)
Simon Glassfada3f92022-09-17 09:00:09 -0600399 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glass966b6952016-05-01 11:36:29 -0600400 * @devnum: Device number, specific to the interface type, or -1 to
401 * allocate the next available number
402 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200403 * @lba: Total number of blocks of the device
Simon Glass966b6952016-05-01 11:36:29 -0600404 * @devp: the new device (which has not been probed)
405 */
406int blk_create_devicef(struct udevice *parent, const char *drv_name,
Simon Glassfada3f92022-09-17 09:00:09 -0600407 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200408 lbaint_t lba, struct udevice **devp);
Simon Glass966b6952016-05-01 11:36:29 -0600409
410/**
AKASHI Takahiro3e32dbe2021-12-10 15:49:29 +0900411 * blk_probe_or_unbind() - Try to probe
412 *
413 * Try to probe the device, primarily for enumerating partitions.
414 * If it fails, the device itself is unbound since it means that it won't
415 * work any more.
416 *
417 * @dev: The device to probe
418 * Return: 0 if OK, -ve on error
419 */
420int blk_probe_or_unbind(struct udevice *dev);
421
422/**
Simon Glasscceee552016-02-29 15:25:55 -0700423 * blk_unbind_all() - Unbind all device of the given interface type
424 *
425 * The devices are removed and then unbound.
426 *
Simon Glassfada3f92022-09-17 09:00:09 -0600427 * @uclass_id: Interface type to unbind
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100428 * Return: 0 if OK, -ve on error
Simon Glasscceee552016-02-29 15:25:55 -0700429 */
Simon Glassfada3f92022-09-17 09:00:09 -0600430int blk_unbind_all(int uclass_id);
Simon Glasscceee552016-02-29 15:25:55 -0700431
Simon Glassd089ba32016-05-01 11:36:28 -0600432/**
433 * blk_find_max_devnum() - find the maximum device number for an interface type
434 *
Simon Glassfada3f92022-09-17 09:00:09 -0600435 * Finds the last allocated device number for an interface type @uclass_id. The
Simon Glassd089ba32016-05-01 11:36:28 -0600436 * next number is safe to use for a newly allocated device.
437 *
Simon Glassfada3f92022-09-17 09:00:09 -0600438 * @uclass_id: Interface type to scan
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100439 * Return: maximum device number found, or -ENODEV if none, or other -ve on
Simon Glassd089ba32016-05-01 11:36:28 -0600440 * error
441 */
Simon Glassfada3f92022-09-17 09:00:09 -0600442int blk_find_max_devnum(enum uclass_id uclass_id);
Simon Glassd089ba32016-05-01 11:36:28 -0600443
Simon Glass13c2c292016-05-01 13:52:30 -0600444/**
Bin Mengfd5eda72018-10-15 02:21:09 -0700445 * blk_next_free_devnum() - get the next device number for an interface type
446 *
447 * Finds the next number that is safe to use for a newly allocated device for
Simon Glassfada3f92022-09-17 09:00:09 -0600448 * an interface type @uclass_id.
Bin Mengfd5eda72018-10-15 02:21:09 -0700449 *
Simon Glassfada3f92022-09-17 09:00:09 -0600450 * @uclass_id: Interface type to scan
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100451 * Return: next device number safe to use, or -ve on error
Bin Mengfd5eda72018-10-15 02:21:09 -0700452 */
Simon Glassfada3f92022-09-17 09:00:09 -0600453int blk_next_free_devnum(enum uclass_id uclass_id);
Bin Mengfd5eda72018-10-15 02:21:09 -0700454
455/**
Simon Glass13c2c292016-05-01 13:52:30 -0600456 * blk_select_hwpart() - select a hardware partition
457 *
458 * Select a hardware partition if the device supports it (typically MMC does)
459 *
460 * @dev: Device to update
461 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100462 * Return: 0 if OK, -ve on error
Simon Glass13c2c292016-05-01 13:52:30 -0600463 */
464int blk_select_hwpart(struct udevice *dev, int hwpart);
465
Tom Rini7c41d142017-06-10 10:01:05 -0400466/**
Simon Glassc0bcaaf2022-10-29 19:47:14 -0600467 * blk_find_from_parent() - find a block device by looking up its parent
468 *
469 * All block devices have a parent 'media' device which provides the block
470 * driver for the block device, ensuring that access to the underlying medium
471 * is available.
472 *
473 * The block device is not activated by this function. See
474 * blk_get_from_parent() for that.
475 *
476 * @parent: Media device
477 * @devp: Returns the associated block device, if any
478 * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
479 * UCLASS_BLK child
480 */
481int blk_find_from_parent(struct udevice *parent, struct udevice **devp);
482
483/**
Tom Rini7c41d142017-06-10 10:01:05 -0400484 * blk_get_from_parent() - obtain a block device by looking up its parent
485 *
Simon Glassc0bcaaf2022-10-29 19:47:14 -0600486 * All block devices have a parent 'media' device which provides the block
487 * driver for the block device, ensuring that access to the underlying medium
488 * is available.
489 *
490 * The block device is probed and ready for use.
491 *
492 * @parent: Media device
493 * @devp: Returns the associated block device, if any
494 * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
495 * UCLASS_BLK child
Tom Rini7c41d142017-06-10 10:01:05 -0400496 */
497int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
498
Tien Fong Chee10378522018-07-06 16:26:36 +0800499/**
Simon Glassf3086cf2022-04-24 23:31:03 -0600500 * blk_get_devtype() - Get the device type of a block device
501 *
502 * @dev: Block device to check
503 * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc"
504 */
505const char *blk_get_devtype(struct udevice *dev);
506
507/**
Tien Fong Chee10378522018-07-06 16:26:36 +0800508 * blk_get_by_device() - Get the block device descriptor for the given device
Simon Glass18861002022-10-20 18:22:54 -0600509 * @dev: Instance of a storage device (the parent of the block device)
Tien Fong Chee10378522018-07-06 16:26:36 +0800510 *
511 * Return: With block device descriptor on success , NULL if there is no such
512 * block device.
513 */
514struct blk_desc *blk_get_by_device(struct udevice *dev);
515
Simon Glasscceee552016-02-29 15:25:55 -0700516#else
517#include <errno.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -0700518/*
519 * These functions should take struct udevice instead of struct blk_desc,
520 * but this is convenient for migration to driver model. Add a 'd' prefix
521 * to the function operations, so that blk_read(), etc. can be reserved for
522 * functions with the correct arguments.
523 */
524static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
525 lbaint_t blkcnt, void *buffer)
526{
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700527 ulong blks_read;
Simon Glassfada3f92022-09-17 09:00:09 -0600528 if (blkcache_read(block_dev->uclass_id, block_dev->devnum,
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700529 start, blkcnt, block_dev->blksz, buffer))
530 return blkcnt;
531
Simon Glass2ee8ada2016-02-29 15:25:52 -0700532 /*
533 * We could check if block_read is NULL and return -ENOSYS. But this
534 * bloats the code slightly (cause some board to fail to build), and
535 * it would be an error to try an operation that does not exist.
536 */
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700537 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
538 if (blks_read == blkcnt)
Simon Glassfada3f92022-09-17 09:00:09 -0600539 blkcache_fill(block_dev->uclass_id, block_dev->devnum,
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700540 start, blkcnt, block_dev->blksz, buffer);
541
542 return blks_read;
Simon Glass2ee8ada2016-02-29 15:25:52 -0700543}
544
545static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
546 lbaint_t blkcnt, const void *buffer)
547{
Simon Glassfada3f92022-09-17 09:00:09 -0600548 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700549 return block_dev->block_write(block_dev, start, blkcnt, buffer);
550}
551
552static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
553 lbaint_t blkcnt)
554{
Simon Glassfada3f92022-09-17 09:00:09 -0600555 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700556 return block_dev->block_erase(block_dev, start, blkcnt);
557}
Simon Glass3bf2ab92016-05-01 11:36:03 -0600558
559/**
560 * struct blk_driver - Driver for block interface types
561 *
562 * This provides access to the block devices for each interface type. One
563 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
564 * type that is to be supported.
565 *
Simon Glassfada3f92022-09-17 09:00:09 -0600566 * @uclass_idname: Interface type name
567 * @uclass_id: Interface type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600568 * @max_devs: Maximum number of devices supported
569 * @desc: Pointer to list of devices for this interface type,
570 * or NULL to use @get_dev() instead
571 */
572struct blk_driver {
Simon Glassfada3f92022-09-17 09:00:09 -0600573 const char *uclass_idname;
574 enum uclass_id uclass_id;
Simon Glass3bf2ab92016-05-01 11:36:03 -0600575 int max_devs;
576 struct blk_desc *desc;
577 /**
578 * get_dev() - get a pointer to a block device given its number
579 *
580 * Each interface allocates its own devices and typically
581 * struct blk_desc is contained with the interface's data structure.
582 * There is no global numbering for block devices. This method allows
583 * the device for an interface type to be obtained when @desc is NULL.
584 *
585 * @devnum: Device number (0 for first device on that interface,
586 * 1 for second, etc.
587 * @descp: Returns pointer to the block device on success
588 * @return 0 if OK, -ve on error
589 */
590 int (*get_dev)(int devnum, struct blk_desc **descp);
591
592 /**
593 * select_hwpart() - Select a hardware partition
594 *
595 * Some devices (e.g. MMC) can support partitioning at the hardware
596 * level. This is quite separate from the normal idea of
597 * software-based partitions. MMC hardware partitions must be
598 * explicitly selected. Once selected only the region of the device
599 * covered by that partition is accessible.
600 *
601 * The MMC standard provides for two boot partitions (numbered 1 and 2),
602 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
603 * Partition 0 is the main user-data partition.
604 *
605 * @desc: Block device descriptor
606 * @hwpart: Hardware partition number to select. 0 means the main
607 * user-data partition, 1 is the first partition, 2 is
608 * the second, etc.
609 * @return 0 if OK, other value for an error
610 */
611 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
612};
613
614/*
615 * Declare a new U-Boot legacy block driver. New drivers should use driver
616 * model (UCLASS_BLK).
617 */
618#define U_BOOT_LEGACY_BLK(__name) \
619 ll_entry_declare(struct blk_driver, __name, blk_driver)
620
Simon Glassfada3f92022-09-17 09:00:09 -0600621struct blk_driver *blk_driver_lookup_type(int uclass_id);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600622
Simon Glasscceee552016-02-29 15:25:55 -0700623#endif /* !CONFIG_BLK */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700624
Simon Glass3bf2ab92016-05-01 11:36:03 -0600625/**
Simon Glassfada3f92022-09-17 09:00:09 -0600626 * blk_get_devnum_by_uclass_idname() - Get a block device by type and number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600627 *
628 * This looks through the available block devices of the given type, returning
629 * the one with the given @devnum.
630 *
Simon Glassfada3f92022-09-17 09:00:09 -0600631 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600632 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100633 * Return: point to block device descriptor, or NULL if not found
Simon Glass3bf2ab92016-05-01 11:36:03 -0600634 */
Simon Glassfada3f92022-09-17 09:00:09 -0600635struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600636
637/**
Simon Glassfada3f92022-09-17 09:00:09 -0600638 * blk_get_devnum_by_uclass_id() - Get a block device by type name, and number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600639 *
Simon Glassfada3f92022-09-17 09:00:09 -0600640 * This looks up the block device type based on @uclass_idname, then calls
641 * blk_get_devnum_by_uclass_id().
Simon Glass3bf2ab92016-05-01 11:36:03 -0600642 *
Simon Glassfada3f92022-09-17 09:00:09 -0600643 * @uclass_idname: Block device type name
Simon Glass3bf2ab92016-05-01 11:36:03 -0600644 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100645 * Return: point to block device descriptor, or NULL if not found
Simon Glass3bf2ab92016-05-01 11:36:03 -0600646 */
Simon Glassfada3f92022-09-17 09:00:09 -0600647struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600648 int devnum);
649
650/**
651 * blk_dselect_hwpart() - select a hardware partition
652 *
653 * This selects a hardware partition (such as is supported by MMC). The block
654 * device size may change as this effectively points the block device to a
655 * partition at the hardware level. See the select_hwpart() method above.
656 *
657 * @desc: Block device descriptor for the device to select
658 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100659 * Return: 0 if OK, -ve on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600660 */
661int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
662
663/**
664 * blk_list_part() - list the partitions for block devices of a given type
665 *
Simon Glassfada3f92022-09-17 09:00:09 -0600666 * This looks up the partition type for each block device of type @uclass_id,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600667 * then displays a list of partitions.
668 *
Simon Glassfada3f92022-09-17 09:00:09 -0600669 * @uclass_id: Block device type
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100670 * Return: 0 if OK, -ENODEV if there is none of that type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600671 */
Simon Glassfada3f92022-09-17 09:00:09 -0600672int blk_list_part(enum uclass_id uclass_id);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600673
674/**
675 * blk_list_devices() - list the block devices of a given type
676 *
Simon Glassfada3f92022-09-17 09:00:09 -0600677 * This lists each block device of the type @uclass_id, showing the capacity
Simon Glass3bf2ab92016-05-01 11:36:03 -0600678 * as well as type-specific information.
679 *
Simon Glassfada3f92022-09-17 09:00:09 -0600680 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600681 */
Simon Glassfada3f92022-09-17 09:00:09 -0600682void blk_list_devices(enum uclass_id uclass_id);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600683
684/**
685 * blk_show_device() - show information about a given block device
686 *
687 * This shows the block device capacity as well as type-specific information.
688 *
Simon Glassfada3f92022-09-17 09:00:09 -0600689 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600690 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100691 * Return: 0 if OK, -ENODEV for invalid device number
Simon Glass3bf2ab92016-05-01 11:36:03 -0600692 */
Simon Glassfada3f92022-09-17 09:00:09 -0600693int blk_show_device(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600694
695/**
696 * blk_print_device_num() - show information about a given block device
697 *
698 * This is similar to blk_show_device() but returns an error if the block
699 * device type is unknown.
700 *
Simon Glassfada3f92022-09-17 09:00:09 -0600701 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600702 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100703 * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
Simon Glass3bf2ab92016-05-01 11:36:03 -0600704 * device is not connected
705 */
Simon Glassfada3f92022-09-17 09:00:09 -0600706int blk_print_device_num(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600707
708/**
709 * blk_print_part_devnum() - print the partition information for a device
710 *
Simon Glassfada3f92022-09-17 09:00:09 -0600711 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600712 * @devnum: Device number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100713 * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
Simon Glass3bf2ab92016-05-01 11:36:03 -0600714 * the interface type is not supported, other -ve on other error
715 */
Simon Glassfada3f92022-09-17 09:00:09 -0600716int blk_print_part_devnum(enum uclass_id uclass_id, int devnum);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600717
718/**
719 * blk_read_devnum() - read blocks from a device
720 *
Simon Glassfada3f92022-09-17 09:00:09 -0600721 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600722 * @devnum: Device number
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200723 * @start: Start block number to read (0=first)
Simon Glass3bf2ab92016-05-01 11:36:03 -0600724 * @blkcnt: Number of blocks to read
725 * @buffer: Address to write data to
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100726 * Return: number of blocks read, or -ve error number on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600727 */
Simon Glassfada3f92022-09-17 09:00:09 -0600728ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600729 lbaint_t blkcnt, void *buffer);
730
731/**
732 * blk_write_devnum() - write blocks to a device
733 *
Simon Glassfada3f92022-09-17 09:00:09 -0600734 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600735 * @devnum: Device number
Mattijs Korpershoek3cc71a02022-10-17 09:35:04 +0200736 * @start: Start block number to write (0=first)
Simon Glass3bf2ab92016-05-01 11:36:03 -0600737 * @blkcnt: Number of blocks to write
738 * @buffer: Address to read data from
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100739 * Return: number of blocks written, or -ve error number on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600740 */
Simon Glassfada3f92022-09-17 09:00:09 -0600741ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glass3bf2ab92016-05-01 11:36:03 -0600742 lbaint_t blkcnt, const void *buffer);
743
744/**
745 * blk_select_hwpart_devnum() - select a hardware partition
746 *
747 * This is similar to blk_dselect_hwpart() but it looks up the interface and
748 * device number.
749 *
Simon Glassfada3f92022-09-17 09:00:09 -0600750 * @uclass_id: Block device type
Simon Glass3bf2ab92016-05-01 11:36:03 -0600751 * @devnum: Device number
752 * @hwpart: Partition number to select
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100753 * Return: 0 if OK, -ve on error
Simon Glass3bf2ab92016-05-01 11:36:03 -0600754 */
Simon Glassfada3f92022-09-17 09:00:09 -0600755int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart);
Simon Glass3bf2ab92016-05-01 11:36:03 -0600756
Simon Glass85af5a42017-07-29 11:34:53 -0600757/**
Simon Glassfada3f92022-09-17 09:00:09 -0600758 * blk_get_uclass_name() - Get the name of an interface type
Simon Glass85af5a42017-07-29 11:34:53 -0600759 *
Simon Glassfada3f92022-09-17 09:00:09 -0600760 * @uclass_id: Interface type to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100761 * Return: name of interface, or NULL if none
Simon Glass85af5a42017-07-29 11:34:53 -0600762 */
Simon Glassfada3f92022-09-17 09:00:09 -0600763const char *blk_get_uclass_name(enum uclass_id uclass_id);
Simon Glass85af5a42017-07-29 11:34:53 -0600764
Simon Glassbf2e7952017-07-29 11:34:54 -0600765/**
766 * blk_common_cmd() - handle common commands with block devices
767 *
768 * @args: Number of arguments to the command (argv[0] is the command itself)
769 * @argv: Command arguments
Simon Glassfada3f92022-09-17 09:00:09 -0600770 * @uclass_id: Interface type
Simon Glassbf2e7952017-07-29 11:34:54 -0600771 * @cur_devnump: Current device number for this interface type
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100772 * Return: 0 if OK, CMD_RET_ERROR on error
Simon Glassbf2e7952017-07-29 11:34:54 -0600773 */
Simon Glassfada3f92022-09-17 09:00:09 -0600774int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
Simon Glassbf2e7952017-07-29 11:34:54 -0600775 int *cur_devnump);
776
Simon Glassfc7a7442021-07-05 16:32:59 -0600777enum blk_flag_t {
778 BLKF_FIXED = 1 << 0,
779 BLKF_REMOVABLE = 1 << 1,
780 BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE,
781};
782
783/**
784 * blk_first_device_err() - Get the first 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: Returns pointer to the first device in that uclass, or NULL if none
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100790 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glassfc7a7442021-07-05 16:32:59 -0600791 */
792int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
793
794/**
795 * blk_next_device_err() - Get the next block device
796 *
797 * The device returned is probed if necessary, and ready for use
798 *
799 * @flags: Indicates type of device to return
800 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
801 * to the next device in the uclass if no error occurred, or -ENODEV if
802 * there is no next device.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100803 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glassfc7a7442021-07-05 16:32:59 -0600804 */
805int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
806
807/**
Simon Glass8e61f932022-02-28 12:08:35 -0700808 * blk_find_first() - Return the first matching block device
809 * @flags: Indicates type of device to return
810 * @devp: Returns pointer to device, or NULL on error
811 *
812 * The device is not prepared for use - this is an internal function.
813 * The function uclass_get_device_tail() can be used to probe the device.
814 *
815 * Note that some devices are considered removable until they have been probed
816 *
817 * @return 0 if found, -ENODEV if not found
818 */
819int blk_find_first(enum blk_flag_t flags, struct udevice **devp);
820
821/**
822 * blk_find_next() - Return the next matching block device
823 * @flags: Indicates type of device to return
824 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
825 * to the next device in the same uclass, or NULL if none
826 *
827 * The device is not prepared for use - this is an internal function.
828 * The function uclass_get_device_tail() can be used to probe the device.
829 *
830 * Note that some devices are considered removable until they have been probed
831 *
832 * @return 0 if found, -ENODEV if not found
833 */
834int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
835
836/**
837 * blk_foreach() - iterate through block devices
838 *
839 * This creates a for() loop which works through the available block devices in
840 * order from start to end.
841 *
842 * If for some reason the uclass cannot be found, this does nothing.
843 *
844 * @_flags: Indicates type of device to return
845 * @_pos: struct udevice * to hold the current device. Set to NULL when there
846 * are no more devices.
847 */
848#define blk_foreach(_flags, _pos) \
849 for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \
850 _ret = blk_find_next(_flags, &_pos))
851
852/**
Simon Glassfc7a7442021-07-05 16:32:59 -0600853 * blk_foreach_probe() - Helper function to iteration through block devices
854 *
855 * This creates a for() loop which works through the available devices in
856 * a uclass in order from start to end. Devices are probed if necessary,
857 * and ready for use.
858 *
859 * @flags: Indicates type of device to return
860 * @dev: struct udevice * to hold the current device. Set to NULL when there
861 * are no more devices.
862 */
863#define blk_foreach_probe(flags, pos) \
864 for (int _ret = blk_first_device_err(flags, &(pos)); \
865 !_ret && pos; \
866 _ret = blk_next_device_err(flags, &(pos)))
867
868/**
869 * blk_count_devices() - count the number of devices of a particular type
870 *
871 * @flags: Indicates type of device to find
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100872 * Return: number of devices matching those flags
Simon Glassfc7a7442021-07-05 16:32:59 -0600873 */
874int blk_count_devices(enum blk_flag_t flag);
875
Simon Glassac8162f2016-02-29 15:25:39 -0700876#endif