]> Git Repo - J-u-boot.git/blob - include/blk.h
Merge branch '2023-08-22-assorted-code-cleanups' into next
[J-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 <bouncebuf.h>
11 #include <dm/uclass-id.h>
12 #include <efi.h>
13
14 #ifdef CONFIG_SYS_64BIT_LBA
15 typedef uint64_t lbaint_t;
16 #define LBAFlength "ll"
17 #else
18 typedef ulong lbaint_t;
19 #define LBAFlength "l"
20 #endif
21 #define LBAF "%" LBAFlength "x"
22 #define LBAFU "%" LBAFlength "u"
23
24 struct udevice;
25
26 static inline bool blk_enabled(void)
27 {
28         return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_SPL_LEGACY_BLOCK);
29 }
30
31 #define BLK_VEN_SIZE            40
32 #define BLK_PRD_SIZE            20
33 #define BLK_REV_SIZE            8
34
35 #define PART_FORMAT_PCAT        0x1
36 #define PART_FORMAT_GPT         0x2
37
38 /*
39  * Identifies the partition table type (ie. MBR vs GPT GUID) signature
40  */
41 enum 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 /*
50  * With driver model (CONFIG_BLK) this is uclass platform data, accessible
51  * with dev_get_uclass_plat(dev)
52  */
53 struct blk_desc {
54         /*
55          * TODO: With driver model we should be able to use the parent
56          * device's uclass instead.
57          */
58         enum uclass_id  uclass_id;      /* type of the interface */
59         int             devnum;         /* device number */
60         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 */
66         /* device can use 48bit addr (ATA/ATAPI v7) */
67         bool    lba48;
68         unsigned char   atapi;          /* Use ATAPI protocol */
69         lbaint_t        lba;            /* number of blocks */
70         unsigned long   blksz;          /* block size */
71         int             log2blksz;      /* for convenience: log2(blksz) */
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 */
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         };
80 #if CONFIG_IS_ENABLED(BLK)
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          */
86         struct udevice *bdev;
87 #else
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 */
100 #endif
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
107 #if CONFIG_IS_ENABLED(BLOCK_CACHE)
108
109 /**
110  * blkcache_init() - initialize the block cache list pointers
111  */
112 int blkcache_init(void);
113
114 /**
115  * blkcache_read() - attempt to read a set of blocks from cache
116  *
117  * @param iftype - uclass_id_x for type of device
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
122  * @param buffer - buffer to contain cached data
123  *
124  * Return: - 1 if block returned from cache, 0 otherwise.
125  */
126 int blkcache_read(int iftype, int dev,
127                   lbaint_t start, lbaint_t blkcnt,
128                   unsigned long blksz, void *buffer);
129
130 /**
131  * blkcache_fill() - make data read from a block device available
132  * to the block cache
133  *
134  * @param iftype - uclass_id_x for type of device
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
139  * @param buffer - buffer containing data to cache
140  *
141  */
142 void blkcache_fill(int iftype, int dev,
143                    lbaint_t start, lbaint_t blkcnt,
144                    unsigned long blksz, void const *buffer);
145
146 /**
147  * blkcache_invalidate() - discard the cache for a set of blocks
148  * because of a write or device (re)initialization.
149  *
150  * @iftype - UCLASS_ID_ for type of device, or -1 for any
151  * @dev - device index of particular type, if @iftype is not -1
152  */
153 void blkcache_invalidate(int iftype, int dev);
154
155 /**
156  * blkcache_configure() - configure block cache
157  *
158  * @param blocks - maximum blocks per entry
159  * @param entries - maximum entries in cache
160  */
161 void blkcache_configure(unsigned blocks, unsigned entries);
162
163 /*
164  * statistics of the block cache
165  */
166 struct 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  */
179 void blkcache_stats(struct block_cache_stats *stats);
180
181 /** blkcache_free() - free all memory allocated to the block cache */
182 void blkcache_free(void);
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 static inline void blkcache_free(void) {}
200
201 #endif
202
203 #if CONFIG_IS_ENABLED(BLK)
204 struct udevice;
205
206 /* Operations on block devices */
207 struct blk_ops {
208         /**
209          * read() - read from a block device
210          *
211          * @dev:        Device to read from
212          * @start:      Start block number to read (0=first)
213          * @blkcnt:     Number of blocks to read
214          * @buffer:     Destination buffer for data read
215          * @return number of blocks read, or -ve error number (see the
216          * IS_ERR_VALUE() macro
217          */
218         unsigned long (*read)(struct udevice *dev, lbaint_t start,
219                               lbaint_t blkcnt, void *buffer);
220
221         /**
222          * write() - write to a block device
223          *
224          * @dev:        Device to write to
225          * @start:      Start block number to write (0=first)
226          * @blkcnt:     Number of blocks to write
227          * @buffer:     Source buffer for data to write
228          * @return number of blocks written, or -ve error number (see the
229          * IS_ERR_VALUE() macro
230          */
231         unsigned long (*write)(struct udevice *dev, lbaint_t start,
232                                lbaint_t blkcnt, const void *buffer);
233
234         /**
235          * erase() - erase a section of a block device
236          *
237          * @dev:        Device to (partially) erase
238          * @start:      Start block number to erase (0=first)
239          * @blkcnt:     Number of blocks to erase
240          * @return number of blocks erased, or -ve error number (see the
241          * IS_ERR_VALUE() macro
242          */
243         unsigned long (*erase)(struct udevice *dev, lbaint_t start,
244                                lbaint_t blkcnt);
245
246         /**
247          * select_hwpart() - select a particular hardware partition
248          *
249          * Some devices (e.g. MMC) can support partitioning at the hardware
250          * level. This is quite separate from the normal idea of
251          * software-based partitions. MMC hardware partitions must be
252          * explicitly selected. Once selected only the region of the device
253          * covered by that partition is accessible.
254          *
255          * The MMC standard provides for two boot partitions (numbered 1 and 2),
256          * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
257          *
258          * @dev:        Block device to update
259          * @hwpart:     Hardware partition number to select. 0 means the raw
260          *              device, 1 is the first partition, 2 is the second, etc.
261          * @return 0 if OK, -ve on error
262          */
263         int (*select_hwpart)(struct udevice *dev, int hwpart);
264
265 #if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
266         /**
267          * buffer_aligned() - test memory alignment of block operation buffer
268          *
269          * Some devices have limited DMA capabilities and require that the
270          * buffers passed to them fit specific properties. This optional
271          * callback can be used to indicate whether a buffer alignment is
272          * suitable for the device DMA or not, and trigger use of generic
273          * bounce buffer implementation to help use of unsuitable buffers
274          * at the expense of performance degradation.
275          *
276          * @dev:        Block device associated with the request
277          * @state:      Bounce buffer state
278          * @return 1 if OK, 0 if unaligned
279          */
280         int (*buffer_aligned)(struct udevice *dev, struct bounce_buffer *state);
281 #endif  /* CONFIG_BOUNCE_BUFFER */
282 };
283
284 /*
285  * These functions should take struct udevice instead of struct blk_desc,
286  * but this is convenient for migration to driver model. Add a 'd' prefix
287  * to the function operations, so that blk_read(), etc. can be reserved for
288  * functions with the correct arguments.
289  */
290 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
291                         lbaint_t blkcnt, void *buffer);
292 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
293                          lbaint_t blkcnt, const void *buffer);
294 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
295                          lbaint_t blkcnt);
296
297 /**
298  * blk_read() - Read from a block device
299  *
300  * @dev: Device to read from
301  * @start: Start block for the read
302  * @blkcnt: Number of blocks to read
303  * @buf: Place to put the data
304  * @return number of blocks read (which may be less than @blkcnt),
305  * or -ve on error. This never returns 0 unless @blkcnt is 0
306  */
307 long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
308               void *buffer);
309
310 /**
311  * blk_write() - Write to a block device
312  *
313  * @dev: Device to write to
314  * @start: Start block for the write
315  * @blkcnt: Number of blocks to write
316  * @buf: Data to write
317  * @return number of blocks written (which may be less than @blkcnt),
318  * or -ve on error. This never returns 0 unless @blkcnt is 0
319  */
320 long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
321                const void *buffer);
322
323 /**
324  * blk_erase() - Erase part of a block device
325  *
326  * @dev: Device to erase
327  * @start: Start block for the erase
328  * @blkcnt: Number of blocks to erase
329  * @return number of blocks erased (which may be less than @blkcnt),
330  * or -ve on error. This never returns 0 unless @blkcnt is 0
331  */
332 long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
333
334 /**
335  * blk_find_device() - Find a block device
336  *
337  * This function does not activate the device. The device will be returned
338  * whether or not it is activated.
339  *
340  * @uclass_id:  Interface type (enum uclass_id_t)
341  * @devnum:     Device number (specific to each interface type)
342  * @devp:       the device, if found
343  * Return: 0 if found, -ENODEV if no device found, or other -ve error value
344  */
345 int blk_find_device(int uclass_id, int devnum, struct udevice **devp);
346
347 /**
348  * blk_get_device() - Find and probe a block device ready for use
349  *
350  * @uclass_id:  Interface type (enum uclass_id_t)
351  * @devnum:     Device number (specific to each interface type)
352  * @devp:       the device, if found
353  * Return: 0 if found, -ENODEV if no device found, or other -ve error value
354  */
355 int blk_get_device(int uclass_id, int devnum, struct udevice **devp);
356
357 /**
358  * blk_first_device() - Find the first device for a given interface
359  *
360  * The device is probed ready for use
361  *
362  * @devnum:     Device number (specific to each interface type)
363  * @devp:       the device, if found
364  * Return: 0 if found, -ENODEV if no device, or other -ve error value
365  */
366 int blk_first_device(int uclass_id, struct udevice **devp);
367
368 /**
369  * blk_next_device() - Find the next device for a given interface
370  *
371  * This can be called repeatedly after blk_first_device() to iterate through
372  * all devices of the given interface type.
373  *
374  * The device is probed ready for use
375  *
376  * @devp:       On entry, the previous device returned. On exit, the next
377  *              device, if found
378  * Return: 0 if found, -ENODEV if no device, or other -ve error value
379  */
380 int blk_next_device(struct udevice **devp);
381
382 /**
383  * blk_create_device() - Create a new block device
384  *
385  * @parent:     Parent of the new device
386  * @drv_name:   Driver name to use for the block device
387  * @name:       Name for the device
388  * @uclass_id:  Interface type (enum uclass_id_t)
389  * @devnum:     Device number, specific to the interface type, or -1 to
390  *              allocate the next available number
391  * @blksz:      Block size of the device in bytes (typically 512)
392  * @lba:        Total number of blocks of the device
393  * @devp:       the new device (which has not been probed)
394  */
395 int blk_create_device(struct udevice *parent, const char *drv_name,
396                       const char *name, int uclass_id, int devnum, int blksz,
397                       lbaint_t lba, struct udevice **devp);
398
399 /**
400  * blk_create_devicef() - Create a new named block device
401  *
402  * @parent:     Parent of the new device
403  * @drv_name:   Driver name to use for the block device
404  * @name:       Name for the device (parent name is prepended)
405  * @uclass_id:  Interface type (enum uclass_id_t)
406  * @devnum:     Device number, specific to the interface type, or -1 to
407  *              allocate the next available number
408  * @blksz:      Block size of the device in bytes (typically 512)
409  * @lba:        Total number of blocks of the device
410  * @devp:       the new device (which has not been probed)
411  */
412 int blk_create_devicef(struct udevice *parent, const char *drv_name,
413                        const char *name, int uclass_id, int devnum, int blksz,
414                        lbaint_t lba, struct udevice **devp);
415
416 /**
417  * blk_probe_or_unbind() - Try to probe
418  *
419  * Try to probe the device, primarily for enumerating partitions.
420  * If it fails, the device itself is unbound since it means that it won't
421  * work any more.
422  *
423  * @dev:        The device to probe
424  * Return:      0 if OK, -ve on error
425  */
426 int blk_probe_or_unbind(struct udevice *dev);
427
428 /**
429  * blk_unbind_all() - Unbind all device of the given interface type
430  *
431  * The devices are removed and then unbound.
432  *
433  * @uclass_id:  Interface type to unbind
434  * Return: 0 if OK, -ve on error
435  */
436 int blk_unbind_all(int uclass_id);
437
438 /**
439  * blk_find_max_devnum() - find the maximum device number for an interface type
440  *
441  * Finds the last allocated device number for an interface type @uclass_id. The
442  * next number is safe to use for a newly allocated device.
443  *
444  * @uclass_id:  Interface type to scan
445  * Return: maximum device number found, or -ENODEV if none, or other -ve on
446  * error
447  */
448 int blk_find_max_devnum(enum uclass_id uclass_id);
449
450 /**
451  * blk_next_free_devnum() - get the next device number for an interface type
452  *
453  * Finds the next number that is safe to use for a newly allocated device for
454  * an interface type @uclass_id.
455  *
456  * @uclass_id:  Interface type to scan
457  * Return: next device number safe to use, or -ve on error
458  */
459 int blk_next_free_devnum(enum uclass_id uclass_id);
460
461 /**
462  * blk_select_hwpart() - select a hardware partition
463  *
464  * Select a hardware partition if the device supports it (typically MMC does)
465  *
466  * @dev:        Device to update
467  * @hwpart:     Partition number to select
468  * Return: 0 if OK, -ve on error
469  */
470 int blk_select_hwpart(struct udevice *dev, int hwpart);
471
472 /**
473  * blk_find_from_parent() - find a block device by looking up its parent
474  *
475  * All block devices have a parent 'media' device which provides the block
476  * driver for the block device, ensuring that access to the underlying medium
477  * is available.
478  *
479  * The block device is not activated by this function. See
480  * blk_get_from_parent() for that.
481  *
482  * @parent: Media device
483  * @devp: Returns the associated block device, if any
484  * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
485  * UCLASS_BLK child
486  */
487 int blk_find_from_parent(struct udevice *parent, struct udevice **devp);
488
489 /**
490  * blk_get_from_parent() - obtain a block device by looking up its parent
491  *
492  * All block devices have a parent 'media' device which provides the block
493  * driver for the block device, ensuring that access to the underlying medium
494  * is available.
495  *
496  * The block device is probed and ready for use.
497  *
498  * @parent: Media device
499  * @devp: Returns the associated block device, if any
500  * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
501  * UCLASS_BLK child
502  */
503 int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
504
505 /**
506  * blk_get_devtype() - Get the device type of a block device
507  *
508  * @dev:        Block device to check
509  * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc"
510  */
511 const char *blk_get_devtype(struct udevice *dev);
512
513 /**
514  * blk_get_by_device() - Get the block device descriptor for the given device
515  * @dev:        Instance of a storage device (the parent of the block device)
516  *
517  * Return: With block device descriptor on success , NULL if there is no such
518  *         block device.
519  */
520 struct blk_desc *blk_get_by_device(struct udevice *dev);
521
522 #else
523 #include <errno.h>
524 /*
525  * These functions should take struct udevice instead of struct blk_desc,
526  * but this is convenient for migration to driver model. Add a 'd' prefix
527  * to the function operations, so that blk_read(), etc. can be reserved for
528  * functions with the correct arguments.
529  */
530 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
531                               lbaint_t blkcnt, void *buffer)
532 {
533         ulong blks_read;
534         if (blkcache_read(block_dev->uclass_id, block_dev->devnum,
535                           start, blkcnt, block_dev->blksz, buffer))
536                 return blkcnt;
537
538         /*
539          * We could check if block_read is NULL and return -ENOSYS. But this
540          * bloats the code slightly (cause some board to fail to build), and
541          * it would be an error to try an operation that does not exist.
542          */
543         blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
544         if (blks_read == blkcnt)
545                 blkcache_fill(block_dev->uclass_id, block_dev->devnum,
546                               start, blkcnt, block_dev->blksz, buffer);
547
548         return blks_read;
549 }
550
551 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
552                                lbaint_t blkcnt, const void *buffer)
553 {
554         blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
555         return block_dev->block_write(block_dev, start, blkcnt, buffer);
556 }
557
558 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
559                                lbaint_t blkcnt)
560 {
561         blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
562         return block_dev->block_erase(block_dev, start, blkcnt);
563 }
564
565 /**
566  * struct blk_driver - Driver for block interface types
567  *
568  * This provides access to the block devices for each interface type. One
569  * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
570  * type that is to be supported.
571  *
572  * @uclass_idname:      Interface type name
573  * @uclass_id:          Interface type
574  * @max_devs:           Maximum number of devices supported
575  * @desc:               Pointer to list of devices for this interface type,
576  *                      or NULL to use @get_dev() instead
577  */
578 struct blk_driver {
579         const char *uclass_idname;
580         enum uclass_id uclass_id;
581         int max_devs;
582         struct blk_desc *desc;
583         /**
584          * get_dev() - get a pointer to a block device given its number
585          *
586          * Each interface allocates its own devices and typically
587          * struct blk_desc is contained with the interface's data structure.
588          * There is no global numbering for block devices. This method allows
589          * the device for an interface type to be obtained when @desc is NULL.
590          *
591          * @devnum:     Device number (0 for first device on that interface,
592          *              1 for second, etc.
593          * @descp:      Returns pointer to the block device on success
594          * @return 0 if OK, -ve on error
595          */
596         int (*get_dev)(int devnum, struct blk_desc **descp);
597
598         /**
599          * select_hwpart() - Select a hardware partition
600          *
601          * Some devices (e.g. MMC) can support partitioning at the hardware
602          * level. This is quite separate from the normal idea of
603          * software-based partitions. MMC hardware partitions must be
604          * explicitly selected. Once selected only the region of the device
605          * covered by that partition is accessible.
606          *
607          * The MMC standard provides for two boot partitions (numbered 1 and 2),
608          * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
609          * Partition 0 is the main user-data partition.
610          *
611          * @desc:       Block device descriptor
612          * @hwpart:     Hardware partition number to select. 0 means the main
613          *              user-data partition, 1 is the first partition, 2 is
614          *              the second, etc.
615          * @return 0 if OK, other value for an error
616          */
617         int (*select_hwpart)(struct blk_desc *desc, int hwpart);
618 };
619
620 /*
621  * Declare a new U-Boot legacy block driver. New drivers should use driver
622  * model (UCLASS_BLK).
623  */
624 #define U_BOOT_LEGACY_BLK(__name)                                       \
625         ll_entry_declare(struct blk_driver, __name, blk_driver)
626
627 struct blk_driver *blk_driver_lookup_type(int uclass_id);
628
629 #endif /* !CONFIG_BLK */
630
631 /**
632  * blk_get_devnum_by_uclass_idname() - Get a block device by type and number
633  *
634  * This looks through the available block devices of the given type, returning
635  * the one with the given @devnum.
636  *
637  * @uclass_id:  Block device type
638  * @devnum:     Device number
639  * Return: point to block device descriptor, or NULL if not found
640  */
641 struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum);
642
643 /**
644  * blk_get_devnum_by_uclass_id() - Get a block device by type name, and number
645  *
646  * This looks up the block device type based on @uclass_idname, then calls
647  * blk_get_devnum_by_uclass_id().
648  *
649  * @uclass_idname:      Block device type name
650  * @devnum:             Device number
651  * Return: point to block device descriptor, or NULL if not found
652  */
653 struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname,
654                                             int devnum);
655
656 /**
657  * blk_dselect_hwpart() - select a hardware partition
658  *
659  * This selects a hardware partition (such as is supported by MMC). The block
660  * device size may change as this effectively points the block device to a
661  * partition at the hardware level. See the select_hwpart() method above.
662  *
663  * @desc:       Block device descriptor for the device to select
664  * @hwpart:     Partition number to select
665  * Return: 0 if OK, -ve on error
666  */
667 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
668
669 /**
670  * blk_list_part() - list the partitions for block devices of a given type
671  *
672  * This looks up the partition type for each block device of type @uclass_id,
673  * then displays a list of partitions.
674  *
675  * @uclass_id:  Block device type
676  * Return: 0 if OK, -ENODEV if there is none of that type
677  */
678 int blk_list_part(enum uclass_id uclass_id);
679
680 /**
681  * blk_list_devices() - list the block devices of a given type
682  *
683  * This lists each block device of the type @uclass_id, showing the capacity
684  * as well as type-specific information.
685  *
686  * @uclass_id:  Block device type
687  */
688 void blk_list_devices(enum uclass_id uclass_id);
689
690 /**
691  * blk_show_device() - show information about a given block device
692  *
693  * This shows the block device capacity as well as type-specific information.
694  *
695  * @uclass_id:  Block device type
696  * @devnum:     Device number
697  * Return: 0 if OK, -ENODEV for invalid device number
698  */
699 int blk_show_device(enum uclass_id uclass_id, int devnum);
700
701 /**
702  * blk_print_device_num() - show information about a given block device
703  *
704  * This is similar to blk_show_device() but returns an error if the block
705  * device type is unknown.
706  *
707  * @uclass_id:  Block device type
708  * @devnum:     Device number
709  * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
710  * device is not connected
711  */
712 int blk_print_device_num(enum uclass_id uclass_id, int devnum);
713
714 /**
715  * blk_print_part_devnum() - print the partition information for a device
716  *
717  * @uclass_id:  Block device type
718  * @devnum:     Device number
719  * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
720  * the interface type is not supported, other -ve on other error
721  */
722 int blk_print_part_devnum(enum uclass_id uclass_id, int devnum);
723
724 /**
725  * blk_read_devnum() - read blocks from a device
726  *
727  * @uclass_id:  Block device type
728  * @devnum:     Device number
729  * @start:      Start block number to read (0=first)
730  * @blkcnt:     Number of blocks to read
731  * @buffer:     Address to write data to
732  * Return: number of blocks read, or -ve error number on error
733  */
734 ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
735                       lbaint_t blkcnt, void *buffer);
736
737 /**
738  * blk_write_devnum() - write blocks to a device
739  *
740  * @uclass_id:  Block device type
741  * @devnum:     Device number
742  * @start:      Start block number to write (0=first)
743  * @blkcnt:     Number of blocks to write
744  * @buffer:     Address to read data from
745  * Return: number of blocks written, or -ve error number on error
746  */
747 ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
748                        lbaint_t blkcnt, const void *buffer);
749
750 /**
751  * blk_select_hwpart_devnum() - select a hardware partition
752  *
753  * This is similar to blk_dselect_hwpart() but it looks up the interface and
754  * device number.
755  *
756  * @uclass_id:  Block device type
757  * @devnum:     Device number
758  * @hwpart:     Partition number to select
759  * Return: 0 if OK, -ve on error
760  */
761 int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart);
762
763 /**
764  * blk_get_uclass_name() - Get the name of an interface type
765  *
766  * @uclass_id: Interface type to check
767  * Return: name of interface, or NULL if none
768  */
769 const char *blk_get_uclass_name(enum uclass_id uclass_id);
770
771 /**
772  * blk_common_cmd() - handle common commands with block devices
773  *
774  * @args: Number of arguments to the command (argv[0] is the command itself)
775  * @argv: Command arguments
776  * @uclass_id: Interface type
777  * @cur_devnump: Current device number for this interface type
778  * Return: 0 if OK, CMD_RET_ERROR on error
779  */
780 int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
781                    int *cur_devnump);
782
783 enum blk_flag_t {
784         BLKF_FIXED      = 1 << 0,
785         BLKF_REMOVABLE  = 1 << 1,
786         BLKF_BOTH       = BLKF_FIXED | BLKF_REMOVABLE,
787 };
788
789 /**
790  * blk_first_device_err() - Get the first block device
791  *
792  * The device returned is probed if necessary, and ready for use
793  *
794  * @flags: Indicates type of device to return
795  * @devp: Returns pointer to the first device in that uclass, or NULL if none
796  * Return: 0 if found, -ENODEV if not found, other -ve on error
797  */
798 int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
799
800 /**
801  * blk_next_device_err() - Get the next block device
802  *
803  * The device returned is probed if necessary, and ready for use
804  *
805  * @flags: Indicates type of device to return
806  * @devp: On entry, pointer to device to lookup. On exit, returns pointer
807  * to the next device in the uclass if no error occurred, or -ENODEV if
808  * there is no next device.
809  * Return: 0 if found, -ENODEV if not found, other -ve on error
810  */
811 int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
812
813 /**
814  * blk_find_first() - Return the first matching block device
815  * @flags: Indicates type of device to return
816  * @devp:       Returns pointer to device, or NULL on error
817  *
818  * The device is not prepared for use - this is an internal function.
819  * The function uclass_get_device_tail() can be used to probe the device.
820  *
821  * Note that some devices are considered removable until they have been probed
822  *
823  * @return 0 if found, -ENODEV if not found
824  */
825 int blk_find_first(enum blk_flag_t flags, struct udevice **devp);
826
827 /**
828  * blk_find_next() - Return the next matching block device
829  * @flags: Indicates type of device to return
830  * @devp: On entry, pointer to device to lookup. On exit, returns pointer
831  * to the next device in the same uclass, or NULL if none
832  *
833  * The device is not prepared for use - this is an internal function.
834  * The function uclass_get_device_tail() can be used to probe the device.
835  *
836  * Note that some devices are considered removable until they have been probed
837  *
838  * @return 0 if found, -ENODEV if not found
839  */
840 int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
841
842 /**
843  * blk_foreach() - iterate through block devices
844  *
845  * This creates a for() loop which works through the available block devices in
846  * order from start to end.
847  *
848  * If for some reason the uclass cannot be found, this does nothing.
849  *
850  * @_flags: Indicates type of device to return
851  * @_pos: struct udevice * to hold the current device. Set to NULL when there
852  * are no more devices.
853  */
854 #define blk_foreach(_flags, _pos) \
855         for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \
856              _ret = blk_find_next(_flags, &_pos))
857
858 /**
859  * blk_foreach_probe() - Helper function to iteration through block devices
860  *
861  * This creates a for() loop which works through the available devices in
862  * a uclass in order from start to end. Devices are probed if necessary,
863  * and ready for use.
864  *
865  * @flags: Indicates type of device to return
866  * @dev: struct udevice * to hold the current device. Set to NULL when there
867  * are no more devices.
868  */
869 #define blk_foreach_probe(flags, pos)   \
870         for (int _ret = blk_first_device_err(flags, &(pos)); \
871              !_ret && pos; \
872              _ret = blk_next_device_err(flags, &(pos)))
873
874 /**
875  * blk_count_devices() - count the number of devices of a particular type
876  *
877  * @flags: Indicates type of device to find
878  * Return: number of devices matching those flags
879  */
880 int blk_count_devices(enum blk_flag_t flag);
881
882 #endif
This page took 0.088335 seconds and 4 git commands to generate.