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