xref: /openbmc/u-boot/include/blk.h (revision 0093b3fc)
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 #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 /* Interface types: */
24 enum if_type {
25 	IF_TYPE_UNKNOWN = 0,
26 	IF_TYPE_IDE,
27 	IF_TYPE_SCSI,
28 	IF_TYPE_ATAPI,
29 	IF_TYPE_USB,
30 	IF_TYPE_DOC,
31 	IF_TYPE_MMC,
32 	IF_TYPE_SD,
33 	IF_TYPE_SATA,
34 	IF_TYPE_HOST,
35 	IF_TYPE_SYSTEMACE,
36 	IF_TYPE_NVME,
37 	IF_TYPE_EFI,
38 
39 	IF_TYPE_COUNT,			/* Number of interface types */
40 };
41 
42 #define BLK_VEN_SIZE		40
43 #define BLK_PRD_SIZE		20
44 #define BLK_REV_SIZE		8
45 
46 /*
47  * Identifies the partition table type (ie. MBR vs GPT GUID) signature
48  */
49 enum sig_type {
50 	SIG_TYPE_NONE,
51 	SIG_TYPE_MBR,
52 	SIG_TYPE_GUID,
53 
54 	SIG_TYPE_COUNT			/* Number of signature types */
55 };
56 
57 /*
58  * With driver model (CONFIG_BLK) this is uclass platform data, accessible
59  * with dev_get_uclass_platdata(dev)
60  */
61 struct blk_desc {
62 	/*
63 	 * TODO: With driver model we should be able to use the parent
64 	 * device's uclass instead.
65 	 */
66 	enum if_type	if_type;	/* type of the interface */
67 	int		devnum;		/* device number */
68 	unsigned char	part_type;	/* partition type */
69 	unsigned char	target;		/* target SCSI ID */
70 	unsigned char	lun;		/* target LUN */
71 	unsigned char	hwpart;		/* HW partition, e.g. for eMMC */
72 	unsigned char	type;		/* device type */
73 	unsigned char	removable;	/* removable device */
74 #ifdef CONFIG_LBA48
75 	/* device can use 48bit addr (ATA/ATAPI v7) */
76 	unsigned char	lba48;
77 #endif
78 	lbaint_t	lba;		/* number of blocks */
79 	unsigned long	blksz;		/* block size */
80 	int		log2blksz;	/* for convenience: log2(blksz) */
81 	char		vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
82 	char		product[BLK_PRD_SIZE + 1]; /* device product number */
83 	char		revision[BLK_REV_SIZE + 1]; /* firmware revision */
84 	enum sig_type	sig_type;	/* Partition table signature type */
85 	union {
86 		uint32_t mbr_sig;	/* MBR integer signature */
87 		efi_guid_t guid_sig;	/* GPT GUID Signature */
88 	};
89 #if CONFIG_IS_ENABLED(BLK)
90 	/*
91 	 * For now we have a few functions which take struct blk_desc as a
92 	 * parameter. This field allows them to look up the associated
93 	 * device. Once these functions are removed we can drop this field.
94 	 */
95 	struct udevice *bdev;
96 #else
97 	unsigned long	(*block_read)(struct blk_desc *block_dev,
98 				      lbaint_t start,
99 				      lbaint_t blkcnt,
100 				      void *buffer);
101 	unsigned long	(*block_write)(struct blk_desc *block_dev,
102 				       lbaint_t start,
103 				       lbaint_t blkcnt,
104 				       const void *buffer);
105 	unsigned long	(*block_erase)(struct blk_desc *block_dev,
106 				       lbaint_t start,
107 				       lbaint_t blkcnt);
108 	void		*priv;		/* driver private struct pointer */
109 #endif
110 };
111 
112 #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
113 #define PAD_TO_BLOCKSIZE(size, blk_desc) \
114 	(PAD_SIZE(size, blk_desc->blksz))
115 
116 #ifdef CONFIG_BLOCK_CACHE
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_prepare_device() - Prepare a block device for use
363  *
364  * This reads partition information from the device if supported.
365  *
366  * @dev:	Device to prepare
367  * @return 0 if ok, -ve on error
368  */
369 int blk_prepare_device(struct udevice *dev);
370 
371 /**
372  * blk_unbind_all() - Unbind all device of the given interface type
373  *
374  * The devices are removed and then unbound.
375  *
376  * @if_type:	Interface type to unbind
377  * @return 0 if OK, -ve on error
378  */
379 int blk_unbind_all(int if_type);
380 
381 /**
382  * blk_find_max_devnum() - find the maximum device number for an interface type
383  *
384  * Finds the last allocated device number for an interface type @if_type. The
385  * next number is safe to use for a newly allocated device.
386  *
387  * @if_type:	Interface type to scan
388  * @return maximum device number found, or -ENODEV if none, or other -ve on
389  * error
390  */
391 int blk_find_max_devnum(enum if_type if_type);
392 
393 /**
394  * blk_select_hwpart() - select a hardware partition
395  *
396  * Select a hardware partition if the device supports it (typically MMC does)
397  *
398  * @dev:	Device to update
399  * @hwpart:	Partition number to select
400  * @return 0 if OK, -ve on error
401  */
402 int blk_select_hwpart(struct udevice *dev, int hwpart);
403 
404 /**
405  * blk_get_from_parent() - obtain a block device by looking up its parent
406  *
407  * All devices with
408  */
409 int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
410 
411 #else
412 #include <errno.h>
413 /*
414  * These functions should take struct udevice instead of struct blk_desc,
415  * but this is convenient for migration to driver model. Add a 'd' prefix
416  * to the function operations, so that blk_read(), etc. can be reserved for
417  * functions with the correct arguments.
418  */
419 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
420 			      lbaint_t blkcnt, void *buffer)
421 {
422 	ulong blks_read;
423 	if (blkcache_read(block_dev->if_type, block_dev->devnum,
424 			  start, blkcnt, block_dev->blksz, buffer))
425 		return blkcnt;
426 
427 	/*
428 	 * We could check if block_read is NULL and return -ENOSYS. But this
429 	 * bloats the code slightly (cause some board to fail to build), and
430 	 * it would be an error to try an operation that does not exist.
431 	 */
432 	blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
433 	if (blks_read == blkcnt)
434 		blkcache_fill(block_dev->if_type, block_dev->devnum,
435 			      start, blkcnt, block_dev->blksz, buffer);
436 
437 	return blks_read;
438 }
439 
440 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
441 			       lbaint_t blkcnt, const void *buffer)
442 {
443 	blkcache_invalidate(block_dev->if_type, block_dev->devnum);
444 	return block_dev->block_write(block_dev, start, blkcnt, buffer);
445 }
446 
447 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
448 			       lbaint_t blkcnt)
449 {
450 	blkcache_invalidate(block_dev->if_type, block_dev->devnum);
451 	return block_dev->block_erase(block_dev, start, blkcnt);
452 }
453 
454 /**
455  * struct blk_driver - Driver for block interface types
456  *
457  * This provides access to the block devices for each interface type. One
458  * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
459  * type that is to be supported.
460  *
461  * @if_typename:	Interface type name
462  * @if_type:		Interface type
463  * @max_devs:		Maximum number of devices supported
464  * @desc:		Pointer to list of devices for this interface type,
465  *			or NULL to use @get_dev() instead
466  */
467 struct blk_driver {
468 	const char *if_typename;
469 	enum if_type if_type;
470 	int max_devs;
471 	struct blk_desc *desc;
472 	/**
473 	 * get_dev() - get a pointer to a block device given its number
474 	 *
475 	 * Each interface allocates its own devices and typically
476 	 * struct blk_desc is contained with the interface's data structure.
477 	 * There is no global numbering for block devices. This method allows
478 	 * the device for an interface type to be obtained when @desc is NULL.
479 	 *
480 	 * @devnum:	Device number (0 for first device on that interface,
481 	 *		1 for second, etc.
482 	 * @descp:	Returns pointer to the block device on success
483 	 * @return 0 if OK, -ve on error
484 	 */
485 	int (*get_dev)(int devnum, struct blk_desc **descp);
486 
487 	/**
488 	 * select_hwpart() - Select a hardware partition
489 	 *
490 	 * Some devices (e.g. MMC) can support partitioning at the hardware
491 	 * level. This is quite separate from the normal idea of
492 	 * software-based partitions. MMC hardware partitions must be
493 	 * explicitly selected. Once selected only the region of the device
494 	 * covered by that partition is accessible.
495 	 *
496 	 * The MMC standard provides for two boot partitions (numbered 1 and 2),
497 	 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
498 	 * Partition 0 is the main user-data partition.
499 	 *
500 	 * @desc:	Block device descriptor
501 	 * @hwpart:	Hardware partition number to select. 0 means the main
502 	 *		user-data partition, 1 is the first partition, 2 is
503 	 *		the second, etc.
504 	 * @return 0 if OK, other value for an error
505 	 */
506 	int (*select_hwpart)(struct blk_desc *desc, int hwpart);
507 };
508 
509 /*
510  * Declare a new U-Boot legacy block driver. New drivers should use driver
511  * model (UCLASS_BLK).
512  */
513 #define U_BOOT_LEGACY_BLK(__name)					\
514 	ll_entry_declare(struct blk_driver, __name, blk_driver)
515 
516 struct blk_driver *blk_driver_lookup_type(int if_type);
517 
518 #endif /* !CONFIG_BLK */
519 
520 /**
521  * blk_get_devnum_by_typename() - Get a block device by type and number
522  *
523  * This looks through the available block devices of the given type, returning
524  * the one with the given @devnum.
525  *
526  * @if_type:	Block device type
527  * @devnum:	Device number
528  * @return point to block device descriptor, or NULL if not found
529  */
530 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
531 
532 /**
533  * blk_get_devnum_by_type() - Get a block device by type name, and number
534  *
535  * This looks up the block device type based on @if_typename, then calls
536  * blk_get_devnum_by_type().
537  *
538  * @if_typename:	Block device type name
539  * @devnum:		Device number
540  * @return point to block device descriptor, or NULL if not found
541  */
542 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
543 					    int devnum);
544 
545 /**
546  * blk_dselect_hwpart() - select a hardware partition
547  *
548  * This selects a hardware partition (such as is supported by MMC). The block
549  * device size may change as this effectively points the block device to a
550  * partition at the hardware level. See the select_hwpart() method above.
551  *
552  * @desc:	Block device descriptor for the device to select
553  * @hwpart:	Partition number to select
554  * @return 0 if OK, -ve on error
555  */
556 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
557 
558 /**
559  * blk_list_part() - list the partitions for block devices of a given type
560  *
561  * This looks up the partition type for each block device of type @if_type,
562  * then displays a list of partitions.
563  *
564  * @if_type:	Block device type
565  * @return 0 if OK, -ENODEV if there is none of that type
566  */
567 int blk_list_part(enum if_type if_type);
568 
569 /**
570  * blk_list_devices() - list the block devices of a given type
571  *
572  * This lists each block device of the type @if_type, showing the capacity
573  * as well as type-specific information.
574  *
575  * @if_type:	Block device type
576  */
577 void blk_list_devices(enum if_type if_type);
578 
579 /**
580  * blk_show_device() - show information about a given block device
581  *
582  * This shows the block device capacity as well as type-specific information.
583  *
584  * @if_type:	Block device type
585  * @devnum:	Device number
586  * @return 0 if OK, -ENODEV for invalid device number
587  */
588 int blk_show_device(enum if_type if_type, int devnum);
589 
590 /**
591  * blk_print_device_num() - show information about a given block device
592  *
593  * This is similar to blk_show_device() but returns an error if the block
594  * device type is unknown.
595  *
596  * @if_type:	Block device type
597  * @devnum:	Device number
598  * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
599  * device is not connected
600  */
601 int blk_print_device_num(enum if_type if_type, int devnum);
602 
603 /**
604  * blk_print_part_devnum() - print the partition information for a device
605  *
606  * @if_type:	Block device type
607  * @devnum:	Device number
608  * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
609  * the interface type is not supported, other -ve on other error
610  */
611 int blk_print_part_devnum(enum if_type if_type, int devnum);
612 
613 /**
614  * blk_read_devnum() - read blocks from a device
615  *
616  * @if_type:	Block device type
617  * @devnum:	Device number
618  * @blkcnt:	Number of blocks to read
619  * @buffer:	Address to write data to
620  * @return number of blocks read, or -ve error number on error
621  */
622 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
623 		      lbaint_t blkcnt, void *buffer);
624 
625 /**
626  * blk_write_devnum() - write blocks to a device
627  *
628  * @if_type:	Block device type
629  * @devnum:	Device number
630  * @blkcnt:	Number of blocks to write
631  * @buffer:	Address to read data from
632  * @return number of blocks written, or -ve error number on error
633  */
634 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
635 		       lbaint_t blkcnt, const void *buffer);
636 
637 /**
638  * blk_select_hwpart_devnum() - select a hardware partition
639  *
640  * This is similar to blk_dselect_hwpart() but it looks up the interface and
641  * device number.
642  *
643  * @if_type:	Block device type
644  * @devnum:	Device number
645  * @hwpart:	Partition number to select
646  * @return 0 if OK, -ve on error
647  */
648 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
649 
650 /**
651  * blk_get_if_type_name() - Get the name of an interface type
652  *
653  * @if_type: Interface type to check
654  * @return name of interface, or NULL if none
655  */
656 const char *blk_get_if_type_name(enum if_type if_type);
657 
658 /**
659  * blk_common_cmd() - handle common commands with block devices
660  *
661  * @args: Number of arguments to the command (argv[0] is the command itself)
662  * @argv: Command arguments
663  * @if_type: Interface type
664  * @cur_devnump: Current device number for this interface type
665  * @return 0 if OK, CMD_RET_ERROR on error
666  */
667 int blk_common_cmd(int argc, char * const argv[], enum if_type if_type,
668 		   int *cur_devnump);
669 
670 #endif
671