]> Git Repo - J-u-boot.git/blame - include/blk.h
Merge branch '2022-10-21-assorted-fixes-and-updates'
[J-u-boot.git] / include / blk.h
CommitLineData
83d290c5 1/* SPDX-License-Identifier: GPL-2.0+ */
1a73661b
SG
2/*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, [email protected].
1a73661b
SG
5 */
6
7#ifndef BLK_H
8#define BLK_H
9
e33a5c6b 10#include <dm/uclass-id.h>
ff98cb90
PJ
11#include <efi.h>
12
1a73661b
SG
13#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
96f37b09
SG
23struct udevice;
24
a51eb8de
SG
25static inline bool blk_enabled(void)
26{
7f8967c2 27 return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_SPL_LEGACY_BLOCK);
a51eb8de
SG
28}
29
eb81b1a4
BM
30#define BLK_VEN_SIZE 40
31#define BLK_PRD_SIZE 20
32#define BLK_REV_SIZE 8
33
ce3dbc5d
MK
34#define PART_FORMAT_PCAT 0x1
35#define PART_FORMAT_GPT 0x2
36
ff98cb90
PJ
37/*
38 * 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
09d71aac
SG
48/*
49 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
caa4daa2 50 * with dev_get_uclass_plat(dev)
09d71aac 51 */
1a73661b 52struct blk_desc {
09d71aac
SG
53 /*
54 * TODO: With driver model we should be able to use the parent
55 * device's uclass instead.
56 */
8149b150 57 enum uclass_id uclass_id; /* type of the interface */
bcce53d0 58 int devnum; /* device number */
1a73661b
SG
59 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) */
eb81b1a4
BM
72 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 */
ff98cb90
PJ
75 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 };
c4d660d4 80#if CONFIG_IS_ENABLED(BLK)
b6694a33
SG
81 /*
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 */
09d71aac
SG
86 struct udevice *bdev;
87#else
1a73661b
SG
88 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 */
09d71aac 100#endif
1a73661b
SG
101};
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
6fef62cc 107#if CONFIG_IS_ENABLED(BLOCK_CACHE)
1526bcce
AD
108
109/**
110 * blkcache_init() - initialize the block cache list pointers
111 */
112int blkcache_init(void);
113
e40cf34a
EN
114/**
115 * blkcache_read() - attempt to read a set of blocks from cache
116 *
8149b150 117 * @param iftype - uclass_id_x for type of device
e40cf34a
EN
118 * @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
601d4e1a 122 * @param buffer - buffer to contain cached data
e40cf34a 123 *
185f812c 124 * Return: - 1 if block returned from cache, 0 otherwise.
e40cf34a 125 */
c8e4d2a8
EN
126int blkcache_read(int iftype, int dev,
127 lbaint_t start, lbaint_t blkcnt,
128 unsigned long blksz, void *buffer);
e40cf34a
EN
129
130/**
131 * blkcache_fill() - make data read from a block device available
132 * to the block cache
133 *
8149b150 134 * @param iftype - uclass_id_x for type of device
e40cf34a
EN
135 * @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
601d4e1a 139 * @param buffer - buffer containing data to cache
e40cf34a
EN
140 *
141 */
c8e4d2a8
EN
142void blkcache_fill(int iftype, int dev,
143 lbaint_t start, lbaint_t blkcnt,
144 unsigned long blksz, void const *buffer);
e40cf34a
EN
145
146/**
147 * blkcache_invalidate() - discard the cache for a set of blocks
148 * because of a write or device (re)initialization.
149 *
8149b150 150 * @param iftype - uclass_id_x for type of device
e40cf34a
EN
151 * @param dev - device index of particular type
152 */
c8e4d2a8 153void blkcache_invalidate(int iftype, int dev);
e40cf34a
EN
154
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
181#else
182
c8e4d2a8
EN
183static inline int blkcache_read(int iftype, int dev,
184 lbaint_t start, lbaint_t blkcnt,
185 unsigned long blksz, void *buffer)
e40cf34a
EN
186{
187 return 0;
188}
189
c8e4d2a8
EN
190static inline void blkcache_fill(int iftype, int dev,
191 lbaint_t start, lbaint_t blkcnt,
192 unsigned long blksz, void const *buffer) {}
e40cf34a 193
c8e4d2a8 194static inline void blkcache_invalidate(int iftype, int dev) {}
e40cf34a
EN
195
196#endif
197
c4d660d4 198#if CONFIG_IS_ENABLED(BLK)
09d71aac
SG
199struct udevice;
200
201/* Operations on block devices */
202struct blk_ops {
203 /**
204 * read() - read from a block device
205 *
206 * @dev: Device to read from
207 * @start: Start block number to read (0=first)
208 * @blkcnt: Number of blocks to read
209 * @buffer: Destination buffer for data read
210 * @return number of blocks read, or -ve error number (see the
211 * IS_ERR_VALUE() macro
212 */
213 unsigned long (*read)(struct udevice *dev, lbaint_t start,
214 lbaint_t blkcnt, void *buffer);
215
216 /**
217 * write() - write to a block device
218 *
219 * @dev: Device to write to
220 * @start: Start block number to write (0=first)
221 * @blkcnt: Number of blocks to write
222 * @buffer: Source buffer for data to write
223 * @return number of blocks written, or -ve error number (see the
224 * IS_ERR_VALUE() macro
225 */
226 unsigned long (*write)(struct udevice *dev, lbaint_t start,
227 lbaint_t blkcnt, const void *buffer);
228
229 /**
230 * erase() - erase a section of a block device
231 *
232 * @dev: Device to (partially) erase
233 * @start: Start block number to erase (0=first)
234 * @blkcnt: Number of blocks to erase
235 * @return number of blocks erased, or -ve error number (see the
236 * IS_ERR_VALUE() macro
237 */
238 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
239 lbaint_t blkcnt);
cd0fb55b
SG
240
241 /**
242 * select_hwpart() - select a particular hardware partition
243 *
244 * Some devices (e.g. MMC) can support partitioning at the hardware
245 * level. This is quite separate from the normal idea of
246 * software-based partitions. MMC hardware partitions must be
247 * explicitly selected. Once selected only the region of the device
248 * covered by that partition is accessible.
249 *
250 * The MMC standard provides for two boot partitions (numbered 1 and 2),
251 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
252 *
601d4e1a 253 * @dev: Block device to update
cd0fb55b
SG
254 * @hwpart: Hardware partition number to select. 0 means the raw
255 * device, 1 is the first partition, 2 is the second, etc.
256 * @return 0 if OK, -ve on error
257 */
258 int (*select_hwpart)(struct udevice *dev, int hwpart);
09d71aac
SG
259};
260
261#define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
262
263/*
264 * These functions should take struct udevice instead of struct blk_desc,
265 * but this is convenient for migration to driver model. Add a 'd' prefix
266 * to the function operations, so that blk_read(), etc. can be reserved for
267 * functions with the correct arguments.
268 */
269unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
270 lbaint_t blkcnt, void *buffer);
271unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
272 lbaint_t blkcnt, const void *buffer);
273unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
274 lbaint_t blkcnt);
275
6139281a
SG
276/**
277 * blk_find_device() - Find a block device
278 *
279 * This function does not activate the device. The device will be returned
280 * whether or not it is activated.
281 *
8149b150 282 * @uclass_id: Interface type (enum uclass_id_t)
6139281a
SG
283 * @devnum: Device number (specific to each interface type)
284 * @devp: the device, if found
185f812c 285 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
6139281a 286 */
8149b150 287int blk_find_device(int uclass_id, int devnum, struct udevice **devp);
6139281a 288
09d71aac
SG
289/**
290 * blk_get_device() - Find and probe a block device ready for use
291 *
8149b150 292 * @uclass_id: Interface type (enum uclass_id_t)
09d71aac
SG
293 * @devnum: Device number (specific to each interface type)
294 * @devp: the device, if found
185f812c 295 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
09d71aac 296 */
8149b150 297int blk_get_device(int uclass_id, int devnum, struct udevice **devp);
09d71aac
SG
298
299/**
300 * blk_first_device() - Find the first device for a given interface
301 *
302 * The device is probed ready for use
303 *
304 * @devnum: Device number (specific to each interface type)
305 * @devp: the device, if found
185f812c 306 * Return: 0 if found, -ENODEV if no device, or other -ve error value
09d71aac 307 */
8149b150 308int blk_first_device(int uclass_id, struct udevice **devp);
09d71aac
SG
309
310/**
311 * blk_next_device() - Find the next device for a given interface
312 *
313 * This can be called repeatedly after blk_first_device() to iterate through
314 * all devices of the given interface type.
315 *
316 * The device is probed ready for use
317 *
318 * @devp: On entry, the previous device returned. On exit, the next
319 * device, if found
185f812c 320 * Return: 0 if found, -ENODEV if no device, or other -ve error value
09d71aac
SG
321 */
322int blk_next_device(struct udevice **devp);
323
324/**
325 * blk_create_device() - Create a new block device
326 *
327 * @parent: Parent of the new device
328 * @drv_name: Driver name to use for the block device
329 * @name: Name for the device
8149b150 330 * @uclass_id: Interface type (enum uclass_id_t)
52138fd4
SG
331 * @devnum: Device number, specific to the interface type, or -1 to
332 * allocate the next available number
09d71aac 333 * @blksz: Block size of the device in bytes (typically 512)
5fe7702e 334 * @lba: Total number of blocks of the device
09d71aac
SG
335 * @devp: the new device (which has not been probed)
336 */
337int blk_create_device(struct udevice *parent, const char *drv_name,
8149b150 338 const char *name, int uclass_id, int devnum, int blksz,
5fe7702e 339 lbaint_t lba, struct udevice **devp);
09d71aac 340
9107c973
SG
341/**
342 * blk_create_devicef() - Create a new named block device
343 *
344 * @parent: Parent of the new device
345 * @drv_name: Driver name to use for the block device
346 * @name: Name for the device (parent name is prepended)
8149b150 347 * @uclass_id: Interface type (enum uclass_id_t)
9107c973
SG
348 * @devnum: Device number, specific to the interface type, or -1 to
349 * allocate the next available number
350 * @blksz: Block size of the device in bytes (typically 512)
5fe7702e 351 * @lba: Total number of blocks of the device
9107c973
SG
352 * @devp: the new device (which has not been probed)
353 */
354int blk_create_devicef(struct udevice *parent, const char *drv_name,
8149b150 355 const char *name, int uclass_id, int devnum, int blksz,
5fe7702e 356 lbaint_t lba, struct udevice **devp);
9107c973 357
19b241c6
AT
358/**
359 * blk_probe_or_unbind() - Try to probe
360 *
361 * Try to probe the device, primarily for enumerating partitions.
362 * If it fails, the device itself is unbound since it means that it won't
363 * work any more.
364 *
365 * @dev: The device to probe
366 * Return: 0 if OK, -ve on error
367 */
368int blk_probe_or_unbind(struct udevice *dev);
369
09d71aac
SG
370/**
371 * blk_unbind_all() - Unbind all device of the given interface type
372 *
373 * The devices are removed and then unbound.
374 *
8149b150 375 * @uclass_id: Interface type to unbind
185f812c 376 * Return: 0 if OK, -ve on error
09d71aac 377 */
8149b150 378int blk_unbind_all(int uclass_id);
09d71aac 379
52138fd4
SG
380/**
381 * blk_find_max_devnum() - find the maximum device number for an interface type
382 *
8149b150 383 * Finds the last allocated device number for an interface type @uclass_id. The
52138fd4
SG
384 * next number is safe to use for a newly allocated device.
385 *
8149b150 386 * @uclass_id: Interface type to scan
185f812c 387 * Return: maximum device number found, or -ENODEV if none, or other -ve on
52138fd4
SG
388 * error
389 */
8149b150 390int blk_find_max_devnum(enum uclass_id uclass_id);
52138fd4 391
c879eeb7
BM
392/**
393 * blk_next_free_devnum() - get the next device number for an interface type
394 *
395 * Finds the next number that is safe to use for a newly allocated device for
8149b150 396 * an interface type @uclass_id.
c879eeb7 397 *
8149b150 398 * @uclass_id: Interface type to scan
185f812c 399 * Return: next device number safe to use, or -ve on error
c879eeb7 400 */
8149b150 401int blk_next_free_devnum(enum uclass_id uclass_id);
c879eeb7 402
cd0fb55b
SG
403/**
404 * blk_select_hwpart() - select a hardware partition
405 *
406 * Select a hardware partition if the device supports it (typically MMC does)
407 *
408 * @dev: Device to update
409 * @hwpart: Partition number to select
185f812c 410 * Return: 0 if OK, -ve on error
cd0fb55b
SG
411 */
412int blk_select_hwpart(struct udevice *dev, int hwpart);
413
4bdb49a7
TR
414/**
415 * blk_get_from_parent() - obtain a block device by looking up its parent
416 *
417 * All devices with
418 */
419int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
420
87571b7f
SG
421/**
422 * blk_get_devtype() - Get the device type of a block device
423 *
424 * @dev: Block device to check
425 * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc"
426 */
427const char *blk_get_devtype(struct udevice *dev);
428
bc53d263
TFC
429/**
430 * blk_get_by_device() - Get the block device descriptor for the given device
431 * @dev: Instance of a storage device
432 *
433 * Return: With block device descriptor on success , NULL if there is no such
434 * block device.
435 */
436struct blk_desc *blk_get_by_device(struct udevice *dev);
437
09d71aac
SG
438#else
439#include <errno.h>
2a981dc2
SG
440/*
441 * These functions should take struct udevice instead of struct blk_desc,
442 * but this is convenient for migration to driver model. Add a 'd' prefix
443 * to the function operations, so that blk_read(), etc. can be reserved for
444 * functions with the correct arguments.
445 */
446static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
447 lbaint_t blkcnt, void *buffer)
448{
e40cf34a 449 ulong blks_read;
8149b150 450 if (blkcache_read(block_dev->uclass_id, block_dev->devnum,
e40cf34a
EN
451 start, blkcnt, block_dev->blksz, buffer))
452 return blkcnt;
453
2a981dc2
SG
454 /*
455 * We could check if block_read is NULL and return -ENOSYS. But this
456 * bloats the code slightly (cause some board to fail to build), and
457 * it would be an error to try an operation that does not exist.
458 */
e40cf34a
EN
459 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
460 if (blks_read == blkcnt)
8149b150 461 blkcache_fill(block_dev->uclass_id, block_dev->devnum,
e40cf34a
EN
462 start, blkcnt, block_dev->blksz, buffer);
463
464 return blks_read;
2a981dc2
SG
465}
466
467static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
468 lbaint_t blkcnt, const void *buffer)
469{
8149b150 470 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
2a981dc2
SG
471 return block_dev->block_write(block_dev, start, blkcnt, buffer);
472}
473
474static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
475 lbaint_t blkcnt)
476{
8149b150 477 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
2a981dc2
SG
478 return block_dev->block_erase(block_dev, start, blkcnt);
479}
6eef6eac
SG
480
481/**
482 * struct blk_driver - Driver for block interface types
483 *
484 * This provides access to the block devices for each interface type. One
485 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
486 * type that is to be supported.
487 *
8149b150
SG
488 * @uclass_idname: Interface type name
489 * @uclass_id: Interface type
6eef6eac
SG
490 * @max_devs: Maximum number of devices supported
491 * @desc: Pointer to list of devices for this interface type,
492 * or NULL to use @get_dev() instead
493 */
494struct blk_driver {
8149b150
SG
495 const char *uclass_idname;
496 enum uclass_id uclass_id;
6eef6eac
SG
497 int max_devs;
498 struct blk_desc *desc;
499 /**
500 * get_dev() - get a pointer to a block device given its number
501 *
502 * Each interface allocates its own devices and typically
503 * struct blk_desc is contained with the interface's data structure.
504 * There is no global numbering for block devices. This method allows
505 * the device for an interface type to be obtained when @desc is NULL.
506 *
507 * @devnum: Device number (0 for first device on that interface,
508 * 1 for second, etc.
509 * @descp: Returns pointer to the block device on success
510 * @return 0 if OK, -ve on error
511 */
512 int (*get_dev)(int devnum, struct blk_desc **descp);
513
514 /**
515 * select_hwpart() - Select a hardware partition
516 *
517 * Some devices (e.g. MMC) can support partitioning at the hardware
518 * level. This is quite separate from the normal idea of
519 * software-based partitions. MMC hardware partitions must be
520 * explicitly selected. Once selected only the region of the device
521 * covered by that partition is accessible.
522 *
523 * The MMC standard provides for two boot partitions (numbered 1 and 2),
524 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
525 * Partition 0 is the main user-data partition.
526 *
527 * @desc: Block device descriptor
528 * @hwpart: Hardware partition number to select. 0 means the main
529 * user-data partition, 1 is the first partition, 2 is
530 * the second, etc.
531 * @return 0 if OK, other value for an error
532 */
533 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
534};
535
536/*
537 * Declare a new U-Boot legacy block driver. New drivers should use driver
538 * model (UCLASS_BLK).
539 */
540#define U_BOOT_LEGACY_BLK(__name) \
541 ll_entry_declare(struct blk_driver, __name, blk_driver)
542
8149b150 543struct blk_driver *blk_driver_lookup_type(int uclass_id);
6eef6eac 544
09d71aac 545#endif /* !CONFIG_BLK */
2a981dc2 546
6eef6eac 547/**
8149b150 548 * blk_get_devnum_by_uclass_idname() - Get a block device by type and number
6eef6eac
SG
549 *
550 * This looks through the available block devices of the given type, returning
551 * the one with the given @devnum.
552 *
8149b150 553 * @uclass_id: Block device type
6eef6eac 554 * @devnum: Device number
185f812c 555 * Return: point to block device descriptor, or NULL if not found
6eef6eac 556 */
8149b150 557struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum);
6eef6eac
SG
558
559/**
8149b150 560 * blk_get_devnum_by_uclass_id() - Get a block device by type name, and number
6eef6eac 561 *
8149b150
SG
562 * This looks up the block device type based on @uclass_idname, then calls
563 * blk_get_devnum_by_uclass_id().
6eef6eac 564 *
8149b150 565 * @uclass_idname: Block device type name
6eef6eac 566 * @devnum: Device number
185f812c 567 * Return: point to block device descriptor, or NULL if not found
6eef6eac 568 */
8149b150 569struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname,
6eef6eac
SG
570 int devnum);
571
572/**
573 * blk_dselect_hwpart() - select a hardware partition
574 *
575 * This selects a hardware partition (such as is supported by MMC). The block
576 * device size may change as this effectively points the block device to a
577 * partition at the hardware level. See the select_hwpart() method above.
578 *
579 * @desc: Block device descriptor for the device to select
580 * @hwpart: Partition number to select
185f812c 581 * Return: 0 if OK, -ve on error
6eef6eac
SG
582 */
583int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
584
585/**
586 * blk_list_part() - list the partitions for block devices of a given type
587 *
8149b150 588 * This looks up the partition type for each block device of type @uclass_id,
6eef6eac
SG
589 * then displays a list of partitions.
590 *
8149b150 591 * @uclass_id: Block device type
185f812c 592 * Return: 0 if OK, -ENODEV if there is none of that type
6eef6eac 593 */
8149b150 594int blk_list_part(enum uclass_id uclass_id);
6eef6eac
SG
595
596/**
597 * blk_list_devices() - list the block devices of a given type
598 *
8149b150 599 * This lists each block device of the type @uclass_id, showing the capacity
6eef6eac
SG
600 * as well as type-specific information.
601 *
8149b150 602 * @uclass_id: Block device type
6eef6eac 603 */
8149b150 604void blk_list_devices(enum uclass_id uclass_id);
6eef6eac
SG
605
606/**
607 * blk_show_device() - show information about a given block device
608 *
609 * This shows the block device capacity as well as type-specific information.
610 *
8149b150 611 * @uclass_id: Block device type
6eef6eac 612 * @devnum: Device number
185f812c 613 * Return: 0 if OK, -ENODEV for invalid device number
6eef6eac 614 */
8149b150 615int blk_show_device(enum uclass_id uclass_id, int devnum);
6eef6eac
SG
616
617/**
618 * blk_print_device_num() - show information about a given block device
619 *
620 * This is similar to blk_show_device() but returns an error if the block
621 * device type is unknown.
622 *
8149b150 623 * @uclass_id: Block device type
6eef6eac 624 * @devnum: Device number
185f812c 625 * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
6eef6eac
SG
626 * device is not connected
627 */
8149b150 628int blk_print_device_num(enum uclass_id uclass_id, int devnum);
6eef6eac
SG
629
630/**
631 * blk_print_part_devnum() - print the partition information for a device
632 *
8149b150 633 * @uclass_id: Block device type
6eef6eac 634 * @devnum: Device number
185f812c 635 * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
6eef6eac
SG
636 * the interface type is not supported, other -ve on other error
637 */
8149b150 638int blk_print_part_devnum(enum uclass_id uclass_id, int devnum);
6eef6eac
SG
639
640/**
641 * blk_read_devnum() - read blocks from a device
642 *
8149b150 643 * @uclass_id: Block device type
6eef6eac 644 * @devnum: Device number
601d4e1a 645 * @start: Start block number to read (0=first)
6eef6eac
SG
646 * @blkcnt: Number of blocks to read
647 * @buffer: Address to write data to
185f812c 648 * Return: number of blocks read, or -ve error number on error
6eef6eac 649 */
8149b150 650ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
6eef6eac
SG
651 lbaint_t blkcnt, void *buffer);
652
653/**
654 * blk_write_devnum() - write blocks to a device
655 *
8149b150 656 * @uclass_id: Block device type
6eef6eac 657 * @devnum: Device number
601d4e1a 658 * @start: Start block number to write (0=first)
6eef6eac
SG
659 * @blkcnt: Number of blocks to write
660 * @buffer: Address to read data from
185f812c 661 * Return: number of blocks written, or -ve error number on error
6eef6eac 662 */
8149b150 663ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
6eef6eac
SG
664 lbaint_t blkcnt, const void *buffer);
665
666/**
667 * blk_select_hwpart_devnum() - select a hardware partition
668 *
669 * This is similar to blk_dselect_hwpart() but it looks up the interface and
670 * device number.
671 *
8149b150 672 * @uclass_id: Block device type
6eef6eac
SG
673 * @devnum: Device number
674 * @hwpart: Partition number to select
185f812c 675 * Return: 0 if OK, -ve on error
6eef6eac 676 */
8149b150 677int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart);
6eef6eac 678
6faa4ed7 679/**
8149b150 680 * blk_get_uclass_name() - Get the name of an interface type
6faa4ed7 681 *
8149b150 682 * @uclass_id: Interface type to check
185f812c 683 * Return: name of interface, or NULL if none
6faa4ed7 684 */
8149b150 685const char *blk_get_uclass_name(enum uclass_id uclass_id);
6faa4ed7 686
4395f667
SG
687/**
688 * blk_common_cmd() - handle common commands with block devices
689 *
690 * @args: Number of arguments to the command (argv[0] is the command itself)
691 * @argv: Command arguments
8149b150 692 * @uclass_id: Interface type
4395f667 693 * @cur_devnump: Current device number for this interface type
185f812c 694 * Return: 0 if OK, CMD_RET_ERROR on error
4395f667 695 */
8149b150 696int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
4395f667
SG
697 int *cur_devnump);
698
96f37b09
SG
699enum blk_flag_t {
700 BLKF_FIXED = 1 << 0,
701 BLKF_REMOVABLE = 1 << 1,
702 BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE,
703};
704
705/**
706 * blk_first_device_err() - Get the first block device
707 *
708 * The device returned is probed if necessary, and ready for use
709 *
710 * @flags: Indicates type of device to return
711 * @devp: Returns pointer to the first device in that uclass, or NULL if none
185f812c 712 * Return: 0 if found, -ENODEV if not found, other -ve on error
96f37b09
SG
713 */
714int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
715
716/**
717 * blk_next_device_err() - Get the next block device
718 *
719 * The device returned is probed if necessary, and ready for use
720 *
721 * @flags: Indicates type of device to return
722 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
723 * to the next device in the uclass if no error occurred, or -ENODEV if
724 * there is no next device.
185f812c 725 * Return: 0 if found, -ENODEV if not found, other -ve on error
96f37b09
SG
726 */
727int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
728
49e86681
SG
729/**
730 * blk_find_first() - Return the first matching block device
731 * @flags: Indicates type of device to return
732 * @devp: Returns pointer to device, or NULL on error
733 *
734 * The device is not prepared for use - this is an internal function.
735 * The function uclass_get_device_tail() can be used to probe the device.
736 *
737 * Note that some devices are considered removable until they have been probed
738 *
739 * @return 0 if found, -ENODEV if not found
740 */
741int blk_find_first(enum blk_flag_t flags, struct udevice **devp);
742
743/**
744 * blk_find_next() - Return the next matching block device
745 * @flags: Indicates type of device to return
746 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
747 * to the next device in the same uclass, or NULL if none
748 *
749 * The device is not prepared for use - this is an internal function.
750 * The function uclass_get_device_tail() can be used to probe the device.
751 *
752 * Note that some devices are considered removable until they have been probed
753 *
754 * @return 0 if found, -ENODEV if not found
755 */
756int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
757
758/**
759 * blk_foreach() - iterate through block devices
760 *
761 * This creates a for() loop which works through the available block devices in
762 * order from start to end.
763 *
764 * If for some reason the uclass cannot be found, this does nothing.
765 *
766 * @_flags: Indicates type of device to return
767 * @_pos: struct udevice * to hold the current device. Set to NULL when there
768 * are no more devices.
769 */
770#define blk_foreach(_flags, _pos) \
771 for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \
772 _ret = blk_find_next(_flags, &_pos))
773
96f37b09
SG
774/**
775 * blk_foreach_probe() - Helper function to iteration through block devices
776 *
777 * This creates a for() loop which works through the available devices in
778 * a uclass in order from start to end. Devices are probed if necessary,
779 * and ready for use.
780 *
781 * @flags: Indicates type of device to return
782 * @dev: struct udevice * to hold the current device. Set to NULL when there
783 * are no more devices.
784 */
785#define blk_foreach_probe(flags, pos) \
786 for (int _ret = blk_first_device_err(flags, &(pos)); \
787 !_ret && pos; \
788 _ret = blk_next_device_err(flags, &(pos)))
789
790/**
791 * blk_count_devices() - count the number of devices of a particular type
792 *
793 * @flags: Indicates type of device to find
185f812c 794 * Return: number of devices matching those flags
96f37b09
SG
795 */
796int blk_count_devices(enum blk_flag_t flag);
797
1a73661b 798#endif
This page took 0.479886 seconds and 4 git commands to generate.