xref: /openbmc/u-boot/include/blk.h (revision 82e0646a)
1 /*
2  * (C) Copyright 2000-2004
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #ifndef BLK_H
9 #define BLK_H
10 
11 #ifdef CONFIG_SYS_64BIT_LBA
12 typedef uint64_t lbaint_t;
13 #define LBAFlength "ll"
14 #else
15 typedef ulong lbaint_t;
16 #define LBAFlength "l"
17 #endif
18 #define LBAF "%" LBAFlength "x"
19 #define LBAFU "%" LBAFlength "u"
20 
21 /* Interface types: */
22 enum if_type {
23 	IF_TYPE_UNKNOWN = 0,
24 	IF_TYPE_IDE,
25 	IF_TYPE_SCSI,
26 	IF_TYPE_ATAPI,
27 	IF_TYPE_USB,
28 	IF_TYPE_DOC,
29 	IF_TYPE_MMC,
30 	IF_TYPE_SD,
31 	IF_TYPE_SATA,
32 	IF_TYPE_HOST,
33 	IF_TYPE_SYSTEMACE,
34 	IF_TYPE_NVME,
35 
36 	IF_TYPE_COUNT,			/* Number of interface types */
37 };
38 
39 #define BLK_VEN_SIZE		40
40 #define BLK_PRD_SIZE		20
41 #define BLK_REV_SIZE		8
42 
43 /*
44  * With driver model (CONFIG_BLK) this is uclass platform data, accessible
45  * with dev_get_uclass_platdata(dev)
46  */
47 struct blk_desc {
48 	/*
49 	 * TODO: With driver model we should be able to use the parent
50 	 * device's uclass instead.
51 	 */
52 	enum if_type	if_type;	/* type of the interface */
53 	int		devnum;		/* device number */
54 	unsigned char	part_type;	/* partition type */
55 	unsigned char	target;		/* target SCSI ID */
56 	unsigned char	lun;		/* target LUN */
57 	unsigned char	hwpart;		/* HW partition, e.g. for eMMC */
58 	unsigned char	type;		/* device type */
59 	unsigned char	removable;	/* removable device */
60 #ifdef CONFIG_LBA48
61 	/* device can use 48bit addr (ATA/ATAPI v7) */
62 	unsigned char	lba48;
63 #endif
64 	lbaint_t	lba;		/* number of blocks */
65 	unsigned long	blksz;		/* block size */
66 	int		log2blksz;	/* for convenience: log2(blksz) */
67 	char		vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
68 	char		product[BLK_PRD_SIZE + 1]; /* device product number */
69 	char		revision[BLK_REV_SIZE + 1]; /* firmware revision */
70 #if CONFIG_IS_ENABLED(BLK)
71 	/*
72 	 * For now we have a few functions which take struct blk_desc as a
73 	 * parameter. This field allows them to look up the associated
74 	 * device. Once these functions are removed we can drop this field.
75 	 */
76 	struct udevice *bdev;
77 #else
78 	unsigned long	(*block_read)(struct blk_desc *block_dev,
79 				      lbaint_t start,
80 				      lbaint_t blkcnt,
81 				      void *buffer);
82 	unsigned long	(*block_write)(struct blk_desc *block_dev,
83 				       lbaint_t start,
84 				       lbaint_t blkcnt,
85 				       const void *buffer);
86 	unsigned long	(*block_erase)(struct blk_desc *block_dev,
87 				       lbaint_t start,
88 				       lbaint_t blkcnt);
89 	void		*priv;		/* driver private struct pointer */
90 #endif
91 };
92 
93 #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
94 #define PAD_TO_BLOCKSIZE(size, blk_desc) \
95 	(PAD_SIZE(size, blk_desc->blksz))
96 
97 #ifdef CONFIG_BLOCK_CACHE
98 /**
99  * blkcache_read() - attempt to read a set of blocks from cache
100  *
101  * @param iftype - IF_TYPE_x for type of device
102  * @param dev - device index of particular type
103  * @param start - starting block number
104  * @param blkcnt - number of blocks to read
105  * @param blksz - size in bytes of each block
106  * @param buf - buffer to contain cached data
107  *
108  * @return - '1' if block returned from cache, '0' otherwise.
109  */
110 int blkcache_read(int iftype, int dev,
111 		  lbaint_t start, lbaint_t blkcnt,
112 		  unsigned long blksz, void *buffer);
113 
114 /**
115  * blkcache_fill() - make data read from a block device available
116  * to the block cache
117  *
118  * @param iftype - IF_TYPE_x for type of device
119  * @param dev - device index of particular type
120  * @param start - starting block number
121  * @param blkcnt - number of blocks available
122  * @param blksz - size in bytes of each block
123  * @param buf - buffer containing data to cache
124  *
125  */
126 void blkcache_fill(int iftype, int dev,
127 		   lbaint_t start, lbaint_t blkcnt,
128 		   unsigned long blksz, void const *buffer);
129 
130 /**
131  * blkcache_invalidate() - discard the cache for a set of blocks
132  * because of a write or device (re)initialization.
133  *
134  * @param iftype - IF_TYPE_x for type of device
135  * @param dev - device index of particular type
136  */
137 void blkcache_invalidate(int iftype, int dev);
138 
139 /**
140  * blkcache_configure() - configure block cache
141  *
142  * @param blocks - maximum blocks per entry
143  * @param entries - maximum entries in cache
144  */
145 void blkcache_configure(unsigned blocks, unsigned entries);
146 
147 /*
148  * statistics of the block cache
149  */
150 struct block_cache_stats {
151 	unsigned hits;
152 	unsigned misses;
153 	unsigned entries; /* current entry count */
154 	unsigned max_blocks_per_entry;
155 	unsigned max_entries;
156 };
157 
158 /**
159  * get_blkcache_stats() - return statistics and reset
160  *
161  * @param stats - statistics are copied here
162  */
163 void blkcache_stats(struct block_cache_stats *stats);
164 
165 #else
166 
167 static inline int blkcache_read(int iftype, int dev,
168 				lbaint_t start, lbaint_t blkcnt,
169 				unsigned long blksz, void *buffer)
170 {
171 	return 0;
172 }
173 
174 static inline void blkcache_fill(int iftype, int dev,
175 				 lbaint_t start, lbaint_t blkcnt,
176 				 unsigned long blksz, void const *buffer) {}
177 
178 static inline void blkcache_invalidate(int iftype, int dev) {}
179 
180 #endif
181 
182 #if CONFIG_IS_ENABLED(BLK)
183 struct udevice;
184 
185 /* Operations on block devices */
186 struct blk_ops {
187 	/**
188 	 * read() - read from a block device
189 	 *
190 	 * @dev:	Device to read from
191 	 * @start:	Start block number to read (0=first)
192 	 * @blkcnt:	Number of blocks to read
193 	 * @buffer:	Destination buffer for data read
194 	 * @return number of blocks read, or -ve error number (see the
195 	 * IS_ERR_VALUE() macro
196 	 */
197 	unsigned long (*read)(struct udevice *dev, lbaint_t start,
198 			      lbaint_t blkcnt, void *buffer);
199 
200 	/**
201 	 * write() - write to a block device
202 	 *
203 	 * @dev:	Device to write to
204 	 * @start:	Start block number to write (0=first)
205 	 * @blkcnt:	Number of blocks to write
206 	 * @buffer:	Source buffer for data to write
207 	 * @return number of blocks written, or -ve error number (see the
208 	 * IS_ERR_VALUE() macro
209 	 */
210 	unsigned long (*write)(struct udevice *dev, lbaint_t start,
211 			       lbaint_t blkcnt, const void *buffer);
212 
213 	/**
214 	 * erase() - erase a section of a block device
215 	 *
216 	 * @dev:	Device to (partially) erase
217 	 * @start:	Start block number to erase (0=first)
218 	 * @blkcnt:	Number of blocks to erase
219 	 * @return number of blocks erased, or -ve error number (see the
220 	 * IS_ERR_VALUE() macro
221 	 */
222 	unsigned long (*erase)(struct udevice *dev, lbaint_t start,
223 			       lbaint_t blkcnt);
224 
225 	/**
226 	 * select_hwpart() - select a particular hardware partition
227 	 *
228 	 * Some devices (e.g. MMC) can support partitioning at the hardware
229 	 * level. This is quite separate from the normal idea of
230 	 * software-based partitions. MMC hardware partitions must be
231 	 * explicitly selected. Once selected only the region of the device
232 	 * covered by that partition is accessible.
233 	 *
234 	 * The MMC standard provides for two boot partitions (numbered 1 and 2),
235 	 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
236 	 *
237 	 * @desc:	Block device to update
238 	 * @hwpart:	Hardware partition number to select. 0 means the raw
239 	 *		device, 1 is the first partition, 2 is the second, etc.
240 	 * @return 0 if OK, -ve on error
241 	 */
242 	int (*select_hwpart)(struct udevice *dev, int hwpart);
243 };
244 
245 #define blk_get_ops(dev)	((struct blk_ops *)(dev)->driver->ops)
246 
247 /*
248  * These functions should take struct udevice instead of struct blk_desc,
249  * but this is convenient for migration to driver model. Add a 'd' prefix
250  * to the function operations, so that blk_read(), etc. can be reserved for
251  * functions with the correct arguments.
252  */
253 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
254 			lbaint_t blkcnt, void *buffer);
255 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
256 			 lbaint_t blkcnt, const void *buffer);
257 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
258 			 lbaint_t blkcnt);
259 
260 /**
261  * blk_find_device() - Find a block device
262  *
263  * This function does not activate the device. The device will be returned
264  * whether or not it is activated.
265  *
266  * @if_type:	Interface type (enum if_type_t)
267  * @devnum:	Device number (specific to each interface type)
268  * @devp:	the device, if found
269  * @return 0 if found, -ENODEV if no device found, or other -ve error value
270  */
271 int blk_find_device(int if_type, int devnum, struct udevice **devp);
272 
273 /**
274  * blk_get_device() - Find and probe a block device ready for use
275  *
276  * @if_type:	Interface type (enum if_type_t)
277  * @devnum:	Device number (specific to each interface type)
278  * @devp:	the device, if found
279  * @return 0 if found, -ENODEV if no device found, or other -ve error value
280  */
281 int blk_get_device(int if_type, int devnum, struct udevice **devp);
282 
283 /**
284  * blk_first_device() - Find the first device for a given interface
285  *
286  * The device is probed ready for use
287  *
288  * @devnum:	Device number (specific to each interface type)
289  * @devp:	the device, if found
290  * @return 0 if found, -ENODEV if no device, or other -ve error value
291  */
292 int blk_first_device(int if_type, struct udevice **devp);
293 
294 /**
295  * blk_next_device() - Find the next device for a given interface
296  *
297  * This can be called repeatedly after blk_first_device() to iterate through
298  * all devices of the given interface type.
299  *
300  * The device is probed ready for use
301  *
302  * @devp:	On entry, the previous device returned. On exit, the next
303  *		device, if found
304  * @return 0 if found, -ENODEV if no device, or other -ve error value
305  */
306 int blk_next_device(struct udevice **devp);
307 
308 /**
309  * blk_create_device() - Create a new block device
310  *
311  * @parent:	Parent of the new device
312  * @drv_name:	Driver name to use for the block device
313  * @name:	Name for the device
314  * @if_type:	Interface type (enum if_type_t)
315  * @devnum:	Device number, specific to the interface type, or -1 to
316  *		allocate the next available number
317  * @blksz:	Block size of the device in bytes (typically 512)
318  * @lba:	Total number of blocks of the device
319  * @devp:	the new device (which has not been probed)
320  */
321 int blk_create_device(struct udevice *parent, const char *drv_name,
322 		      const char *name, int if_type, int devnum, int blksz,
323 		      lbaint_t lba, struct udevice **devp);
324 
325 /**
326  * blk_create_devicef() - Create a new named block device
327  *
328  * @parent:	Parent of the new device
329  * @drv_name:	Driver name to use for the block device
330  * @name:	Name for the device (parent name is prepended)
331  * @if_type:	Interface type (enum if_type_t)
332  * @devnum:	Device number, specific to the interface type, or -1 to
333  *		allocate the next available number
334  * @blksz:	Block size of the device in bytes (typically 512)
335  * @lba:	Total number of blocks of the device
336  * @devp:	the new device (which has not been probed)
337  */
338 int blk_create_devicef(struct udevice *parent, const char *drv_name,
339 		       const char *name, int if_type, int devnum, int blksz,
340 		       lbaint_t lba, struct udevice **devp);
341 
342 /**
343  * blk_prepare_device() - Prepare a block device for use
344  *
345  * This reads partition information from the device if supported.
346  *
347  * @dev:	Device to prepare
348  * @return 0 if ok, -ve on error
349  */
350 int blk_prepare_device(struct udevice *dev);
351 
352 /**
353  * blk_unbind_all() - Unbind all device of the given interface type
354  *
355  * The devices are removed and then unbound.
356  *
357  * @if_type:	Interface type to unbind
358  * @return 0 if OK, -ve on error
359  */
360 int blk_unbind_all(int if_type);
361 
362 /**
363  * blk_find_max_devnum() - find the maximum device number for an interface type
364  *
365  * Finds the last allocated device number for an interface type @if_type. The
366  * next number is safe to use for a newly allocated device.
367  *
368  * @if_type:	Interface type to scan
369  * @return maximum device number found, or -ENODEV if none, or other -ve on
370  * error
371  */
372 int blk_find_max_devnum(enum if_type if_type);
373 
374 /**
375  * blk_select_hwpart() - select a hardware partition
376  *
377  * Select a hardware partition if the device supports it (typically MMC does)
378  *
379  * @dev:	Device to update
380  * @hwpart:	Partition number to select
381  * @return 0 if OK, -ve on error
382  */
383 int blk_select_hwpart(struct udevice *dev, int hwpart);
384 
385 /**
386  * blk_get_from_parent() - obtain a block device by looking up its parent
387  *
388  * All devices with
389  */
390 int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
391 
392 #else
393 #include <errno.h>
394 /*
395  * These functions should take struct udevice instead of struct blk_desc,
396  * but this is convenient for migration to driver model. Add a 'd' prefix
397  * to the function operations, so that blk_read(), etc. can be reserved for
398  * functions with the correct arguments.
399  */
400 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
401 			      lbaint_t blkcnt, void *buffer)
402 {
403 	ulong blks_read;
404 	if (blkcache_read(block_dev->if_type, block_dev->devnum,
405 			  start, blkcnt, block_dev->blksz, buffer))
406 		return blkcnt;
407 
408 	/*
409 	 * We could check if block_read is NULL and return -ENOSYS. But this
410 	 * bloats the code slightly (cause some board to fail to build), and
411 	 * it would be an error to try an operation that does not exist.
412 	 */
413 	blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
414 	if (blks_read == blkcnt)
415 		blkcache_fill(block_dev->if_type, block_dev->devnum,
416 			      start, blkcnt, block_dev->blksz, buffer);
417 
418 	return blks_read;
419 }
420 
421 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
422 			       lbaint_t blkcnt, const void *buffer)
423 {
424 	blkcache_invalidate(block_dev->if_type, block_dev->devnum);
425 	return block_dev->block_write(block_dev, start, blkcnt, buffer);
426 }
427 
428 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
429 			       lbaint_t blkcnt)
430 {
431 	blkcache_invalidate(block_dev->if_type, block_dev->devnum);
432 	return block_dev->block_erase(block_dev, start, blkcnt);
433 }
434 
435 /**
436  * struct blk_driver - Driver for block interface types
437  *
438  * This provides access to the block devices for each interface type. One
439  * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
440  * type that is to be supported.
441  *
442  * @if_typename:	Interface type name
443  * @if_type:		Interface type
444  * @max_devs:		Maximum number of devices supported
445  * @desc:		Pointer to list of devices for this interface type,
446  *			or NULL to use @get_dev() instead
447  */
448 struct blk_driver {
449 	const char *if_typename;
450 	enum if_type if_type;
451 	int max_devs;
452 	struct blk_desc *desc;
453 	/**
454 	 * get_dev() - get a pointer to a block device given its number
455 	 *
456 	 * Each interface allocates its own devices and typically
457 	 * struct blk_desc is contained with the interface's data structure.
458 	 * There is no global numbering for block devices. This method allows
459 	 * the device for an interface type to be obtained when @desc is NULL.
460 	 *
461 	 * @devnum:	Device number (0 for first device on that interface,
462 	 *		1 for second, etc.
463 	 * @descp:	Returns pointer to the block device on success
464 	 * @return 0 if OK, -ve on error
465 	 */
466 	int (*get_dev)(int devnum, struct blk_desc **descp);
467 
468 	/**
469 	 * select_hwpart() - Select a hardware partition
470 	 *
471 	 * Some devices (e.g. MMC) can support partitioning at the hardware
472 	 * level. This is quite separate from the normal idea of
473 	 * software-based partitions. MMC hardware partitions must be
474 	 * explicitly selected. Once selected only the region of the device
475 	 * covered by that partition is accessible.
476 	 *
477 	 * The MMC standard provides for two boot partitions (numbered 1 and 2),
478 	 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
479 	 * Partition 0 is the main user-data partition.
480 	 *
481 	 * @desc:	Block device descriptor
482 	 * @hwpart:	Hardware partition number to select. 0 means the main
483 	 *		user-data partition, 1 is the first partition, 2 is
484 	 *		the second, etc.
485 	 * @return 0 if OK, other value for an error
486 	 */
487 	int (*select_hwpart)(struct blk_desc *desc, int hwpart);
488 };
489 
490 /*
491  * Declare a new U-Boot legacy block driver. New drivers should use driver
492  * model (UCLASS_BLK).
493  */
494 #define U_BOOT_LEGACY_BLK(__name)					\
495 	ll_entry_declare(struct blk_driver, __name, blk_driver)
496 
497 struct blk_driver *blk_driver_lookup_type(int if_type);
498 
499 #endif /* !CONFIG_BLK */
500 
501 /**
502  * blk_get_devnum_by_typename() - Get a block device by type and number
503  *
504  * This looks through the available block devices of the given type, returning
505  * the one with the given @devnum.
506  *
507  * @if_type:	Block device type
508  * @devnum:	Device number
509  * @return point to block device descriptor, or NULL if not found
510  */
511 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
512 
513 /**
514  * blk_get_devnum_by_type() - Get a block device by type name, and number
515  *
516  * This looks up the block device type based on @if_typename, then calls
517  * blk_get_devnum_by_type().
518  *
519  * @if_typename:	Block device type name
520  * @devnum:		Device number
521  * @return point to block device descriptor, or NULL if not found
522  */
523 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
524 					    int devnum);
525 
526 /**
527  * blk_dselect_hwpart() - select a hardware partition
528  *
529  * This selects a hardware partition (such as is supported by MMC). The block
530  * device size may change as this effectively points the block device to a
531  * partition at the hardware level. See the select_hwpart() method above.
532  *
533  * @desc:	Block device descriptor for the device to select
534  * @hwpart:	Partition number to select
535  * @return 0 if OK, -ve on error
536  */
537 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
538 
539 /**
540  * blk_list_part() - list the partitions for block devices of a given type
541  *
542  * This looks up the partition type for each block device of type @if_type,
543  * then displays a list of partitions.
544  *
545  * @if_type:	Block device type
546  * @return 0 if OK, -ENODEV if there is none of that type
547  */
548 int blk_list_part(enum if_type if_type);
549 
550 /**
551  * blk_list_devices() - list the block devices of a given type
552  *
553  * This lists each block device of the type @if_type, showing the capacity
554  * as well as type-specific information.
555  *
556  * @if_type:	Block device type
557  */
558 void blk_list_devices(enum if_type if_type);
559 
560 /**
561  * blk_show_device() - show information about a given block device
562  *
563  * This shows the block device capacity as well as type-specific information.
564  *
565  * @if_type:	Block device type
566  * @devnum:	Device number
567  * @return 0 if OK, -ENODEV for invalid device number
568  */
569 int blk_show_device(enum if_type if_type, int devnum);
570 
571 /**
572  * blk_print_device_num() - show information about a given block device
573  *
574  * This is similar to blk_show_device() but returns an error if the block
575  * device type is unknown.
576  *
577  * @if_type:	Block device type
578  * @devnum:	Device number
579  * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
580  * device is not connected
581  */
582 int blk_print_device_num(enum if_type if_type, int devnum);
583 
584 /**
585  * blk_print_part_devnum() - print the partition information for a device
586  *
587  * @if_type:	Block device type
588  * @devnum:	Device number
589  * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
590  * the interface type is not supported, other -ve on other error
591  */
592 int blk_print_part_devnum(enum if_type if_type, int devnum);
593 
594 /**
595  * blk_read_devnum() - read blocks from a device
596  *
597  * @if_type:	Block device type
598  * @devnum:	Device number
599  * @blkcnt:	Number of blocks to read
600  * @buffer:	Address to write data to
601  * @return number of blocks read, or -ve error number on error
602  */
603 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
604 		      lbaint_t blkcnt, void *buffer);
605 
606 /**
607  * blk_write_devnum() - write blocks to a device
608  *
609  * @if_type:	Block device type
610  * @devnum:	Device number
611  * @blkcnt:	Number of blocks to write
612  * @buffer:	Address to read data from
613  * @return number of blocks written, or -ve error number on error
614  */
615 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
616 		       lbaint_t blkcnt, const void *buffer);
617 
618 /**
619  * blk_select_hwpart_devnum() - select a hardware partition
620  *
621  * This is similar to blk_dselect_hwpart() but it looks up the interface and
622  * device number.
623  *
624  * @if_type:	Block device type
625  * @devnum:	Device number
626  * @hwpart:	Partition number to select
627  * @return 0 if OK, -ve on error
628  */
629 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
630 
631 /**
632  * blk_get_if_type_name() - Get the name of an interface type
633  *
634  * @if_type: Interface type to check
635  * @return name of interface, or NULL if none
636  */
637 const char *blk_get_if_type_name(enum if_type if_type);
638 
639 /**
640  * blk_common_cmd() - handle common commands with block devices
641  *
642  * @args: Number of arguments to the command (argv[0] is the command itself)
643  * @argv: Command arguments
644  * @if_type: Interface type
645  * @cur_devnump: Current device number for this interface type
646  * @return 0 if OK, CMD_RET_ERROR on error
647  */
648 int blk_common_cmd(int argc, char * const argv[], enum if_type if_type,
649 		   int *cur_devnump);
650 
651 #endif
652