blob: a562c10a29aa15d1c8bc6fb5fa60c6c8a5ebd705 [file] [log] [blame]
Simon Glass1a736612016-02-29 15:25:39 -07001/*
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
12typedef uint64_t lbaint_t;
13#define LBAFlength "ll"
14#else
15typedef ulong lbaint_t;
16#define LBAFlength "l"
17#endif
18#define LBAF "%" LBAFlength "x"
19#define LBAFU "%" LBAFlength "u"
20
21/* Interface types: */
Simon Glass5ec4f1a2016-02-29 15:25:40 -070022enum 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
34 IF_TYPE_COUNT, /* Number of interface types */
35};
Simon Glass1a736612016-02-29 15:25:39 -070036
Simon Glass09d71aa2016-02-29 15:25:55 -070037/*
38 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
39 * with dev_get_uclass_platdata(dev)
40 */
Simon Glass1a736612016-02-29 15:25:39 -070041struct blk_desc {
Simon Glass09d71aa2016-02-29 15:25:55 -070042 /*
43 * TODO: With driver model we should be able to use the parent
44 * device's uclass instead.
45 */
Simon Glass5ec4f1a2016-02-29 15:25:40 -070046 enum if_type if_type; /* type of the interface */
Simon Glassbcce53d2016-02-29 15:25:51 -070047 int devnum; /* device number */
Simon Glass1a736612016-02-29 15:25:39 -070048 unsigned char part_type; /* partition type */
49 unsigned char target; /* target SCSI ID */
50 unsigned char lun; /* target LUN */
51 unsigned char hwpart; /* HW partition, e.g. for eMMC */
52 unsigned char type; /* device type */
53 unsigned char removable; /* removable device */
54#ifdef CONFIG_LBA48
55 /* device can use 48bit addr (ATA/ATAPI v7) */
56 unsigned char lba48;
57#endif
58 lbaint_t lba; /* number of blocks */
59 unsigned long blksz; /* block size */
60 int log2blksz; /* for convenience: log2(blksz) */
61 char vendor[40+1]; /* IDE model, SCSI Vendor */
62 char product[20+1]; /* IDE Serial no, SCSI product */
63 char revision[8+1]; /* firmware revision */
Simon Glass09d71aa2016-02-29 15:25:55 -070064#ifdef CONFIG_BLK
65 struct udevice *bdev;
66#else
Simon Glass1a736612016-02-29 15:25:39 -070067 unsigned long (*block_read)(struct blk_desc *block_dev,
68 lbaint_t start,
69 lbaint_t blkcnt,
70 void *buffer);
71 unsigned long (*block_write)(struct blk_desc *block_dev,
72 lbaint_t start,
73 lbaint_t blkcnt,
74 const void *buffer);
75 unsigned long (*block_erase)(struct blk_desc *block_dev,
76 lbaint_t start,
77 lbaint_t blkcnt);
78 void *priv; /* driver private struct pointer */
Simon Glass09d71aa2016-02-29 15:25:55 -070079#endif
Simon Glass1a736612016-02-29 15:25:39 -070080};
81
82#define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
83#define PAD_TO_BLOCKSIZE(size, blk_desc) \
84 (PAD_SIZE(size, blk_desc->blksz))
85
Eric Nelsone40cf342016-03-28 10:05:44 -070086#ifdef CONFIG_BLOCK_CACHE
87/**
88 * blkcache_read() - attempt to read a set of blocks from cache
89 *
90 * @param iftype - IF_TYPE_x for type of device
91 * @param dev - device index of particular type
92 * @param start - starting block number
93 * @param blkcnt - number of blocks to read
94 * @param blksz - size in bytes of each block
95 * @param buf - buffer to contain cached data
96 *
97 * @return - '1' if block returned from cache, '0' otherwise.
98 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -070099int blkcache_read(int iftype, int dev,
100 lbaint_t start, lbaint_t blkcnt,
101 unsigned long blksz, void *buffer);
Eric Nelsone40cf342016-03-28 10:05:44 -0700102
103/**
104 * blkcache_fill() - make data read from a block device available
105 * to the block cache
106 *
107 * @param iftype - IF_TYPE_x for type of device
108 * @param dev - device index of particular type
109 * @param start - starting block number
110 * @param blkcnt - number of blocks available
111 * @param blksz - size in bytes of each block
112 * @param buf - buffer containing data to cache
113 *
114 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700115void blkcache_fill(int iftype, int dev,
116 lbaint_t start, lbaint_t blkcnt,
117 unsigned long blksz, void const *buffer);
Eric Nelsone40cf342016-03-28 10:05:44 -0700118
119/**
120 * blkcache_invalidate() - discard the cache for a set of blocks
121 * because of a write or device (re)initialization.
122 *
123 * @param iftype - IF_TYPE_x for type of device
124 * @param dev - device index of particular type
125 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700126void blkcache_invalidate(int iftype, int dev);
Eric Nelsone40cf342016-03-28 10:05:44 -0700127
128/**
129 * blkcache_configure() - configure block cache
130 *
131 * @param blocks - maximum blocks per entry
132 * @param entries - maximum entries in cache
133 */
134void blkcache_configure(unsigned blocks, unsigned entries);
135
136/*
137 * statistics of the block cache
138 */
139struct block_cache_stats {
140 unsigned hits;
141 unsigned misses;
142 unsigned entries; /* current entry count */
143 unsigned max_blocks_per_entry;
144 unsigned max_entries;
145};
146
147/**
148 * get_blkcache_stats() - return statistics and reset
149 *
150 * @param stats - statistics are copied here
151 */
152void blkcache_stats(struct block_cache_stats *stats);
153
154#else
155
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700156static inline int blkcache_read(int iftype, int dev,
157 lbaint_t start, lbaint_t blkcnt,
158 unsigned long blksz, void *buffer)
Eric Nelsone40cf342016-03-28 10:05:44 -0700159{
160 return 0;
161}
162
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700163static inline void blkcache_fill(int iftype, int dev,
164 lbaint_t start, lbaint_t blkcnt,
165 unsigned long blksz, void const *buffer) {}
Eric Nelsone40cf342016-03-28 10:05:44 -0700166
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700167static inline void blkcache_invalidate(int iftype, int dev) {}
Eric Nelsone40cf342016-03-28 10:05:44 -0700168
169#endif
170
Simon Glass09d71aa2016-02-29 15:25:55 -0700171#ifdef CONFIG_BLK
172struct udevice;
173
174/* Operations on block devices */
175struct blk_ops {
176 /**
177 * read() - read from a block device
178 *
179 * @dev: Device to read from
180 * @start: Start block number to read (0=first)
181 * @blkcnt: Number of blocks to read
182 * @buffer: Destination buffer for data read
183 * @return number of blocks read, or -ve error number (see the
184 * IS_ERR_VALUE() macro
185 */
186 unsigned long (*read)(struct udevice *dev, lbaint_t start,
187 lbaint_t blkcnt, void *buffer);
188
189 /**
190 * write() - write to a block device
191 *
192 * @dev: Device to write to
193 * @start: Start block number to write (0=first)
194 * @blkcnt: Number of blocks to write
195 * @buffer: Source buffer for data to write
196 * @return number of blocks written, or -ve error number (see the
197 * IS_ERR_VALUE() macro
198 */
199 unsigned long (*write)(struct udevice *dev, lbaint_t start,
200 lbaint_t blkcnt, const void *buffer);
201
202 /**
203 * erase() - erase a section of a block device
204 *
205 * @dev: Device to (partially) erase
206 * @start: Start block number to erase (0=first)
207 * @blkcnt: Number of blocks to erase
208 * @return number of blocks erased, or -ve error number (see the
209 * IS_ERR_VALUE() macro
210 */
211 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
212 lbaint_t blkcnt);
213};
214
215#define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
216
217/*
218 * These functions should take struct udevice instead of struct blk_desc,
219 * but this is convenient for migration to driver model. Add a 'd' prefix
220 * to the function operations, so that blk_read(), etc. can be reserved for
221 * functions with the correct arguments.
222 */
223unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
224 lbaint_t blkcnt, void *buffer);
225unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
226 lbaint_t blkcnt, const void *buffer);
227unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
228 lbaint_t blkcnt);
229
230/**
231 * blk_get_device() - Find and probe a block device ready for use
232 *
233 * @if_type: Interface type (enum if_type_t)
234 * @devnum: Device number (specific to each interface type)
235 * @devp: the device, if found
236 * @return - if found, -ENODEV if no device found, or other -ve error value
237 */
238int blk_get_device(int if_type, int devnum, struct udevice **devp);
239
240/**
241 * blk_first_device() - Find the first device for a given interface
242 *
243 * The device is probed ready for use
244 *
245 * @devnum: Device number (specific to each interface type)
246 * @devp: the device, if found
247 * @return 0 if found, -ENODEV if no device, or other -ve error value
248 */
249int blk_first_device(int if_type, struct udevice **devp);
250
251/**
252 * blk_next_device() - Find the next device for a given interface
253 *
254 * This can be called repeatedly after blk_first_device() to iterate through
255 * all devices of the given interface type.
256 *
257 * The device is probed ready for use
258 *
259 * @devp: On entry, the previous device returned. On exit, the next
260 * device, if found
261 * @return 0 if found, -ENODEV if no device, or other -ve error value
262 */
263int blk_next_device(struct udevice **devp);
264
265/**
266 * blk_create_device() - Create a new block device
267 *
268 * @parent: Parent of the new device
269 * @drv_name: Driver name to use for the block device
270 * @name: Name for the device
271 * @if_type: Interface type (enum if_type_t)
272 * @devnum: Device number, specific to the interface type
273 * @blksz: Block size of the device in bytes (typically 512)
274 * @size: Total size of the device in bytes
275 * @devp: the new device (which has not been probed)
276 */
277int blk_create_device(struct udevice *parent, const char *drv_name,
278 const char *name, int if_type, int devnum, int blksz,
279 lbaint_t size, struct udevice **devp);
280
281/**
282 * blk_prepare_device() - Prepare a block device for use
283 *
284 * This reads partition information from the device if supported.
285 *
286 * @dev: Device to prepare
287 * @return 0 if ok, -ve on error
288 */
289int blk_prepare_device(struct udevice *dev);
290
291/**
292 * blk_unbind_all() - Unbind all device of the given interface type
293 *
294 * The devices are removed and then unbound.
295 *
296 * @if_type: Interface type to unbind
297 * @return 0 if OK, -ve on error
298 */
299int blk_unbind_all(int if_type);
300
301#else
302#include <errno.h>
Simon Glass2a981dc2016-02-29 15:25:52 -0700303/*
304 * These functions should take struct udevice instead of struct blk_desc,
305 * but this is convenient for migration to driver model. Add a 'd' prefix
306 * to the function operations, so that blk_read(), etc. can be reserved for
307 * functions with the correct arguments.
308 */
309static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
310 lbaint_t blkcnt, void *buffer)
311{
Eric Nelsone40cf342016-03-28 10:05:44 -0700312 ulong blks_read;
313 if (blkcache_read(block_dev->if_type, block_dev->devnum,
314 start, blkcnt, block_dev->blksz, buffer))
315 return blkcnt;
316
Simon Glass2a981dc2016-02-29 15:25:52 -0700317 /*
318 * We could check if block_read is NULL and return -ENOSYS. But this
319 * bloats the code slightly (cause some board to fail to build), and
320 * it would be an error to try an operation that does not exist.
321 */
Eric Nelsone40cf342016-03-28 10:05:44 -0700322 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
323 if (blks_read == blkcnt)
324 blkcache_fill(block_dev->if_type, block_dev->devnum,
325 start, blkcnt, block_dev->blksz, buffer);
326
327 return blks_read;
Simon Glass2a981dc2016-02-29 15:25:52 -0700328}
329
330static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
331 lbaint_t blkcnt, const void *buffer)
332{
Eric Nelsone40cf342016-03-28 10:05:44 -0700333 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glass2a981dc2016-02-29 15:25:52 -0700334 return block_dev->block_write(block_dev, start, blkcnt, buffer);
335}
336
337static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
338 lbaint_t blkcnt)
339{
Eric Nelsone40cf342016-03-28 10:05:44 -0700340 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glass2a981dc2016-02-29 15:25:52 -0700341 return block_dev->block_erase(block_dev, start, blkcnt);
342}
Simon Glass6eef6ea2016-05-01 11:36:03 -0600343
344/**
345 * struct blk_driver - Driver for block interface types
346 *
347 * This provides access to the block devices for each interface type. One
348 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
349 * type that is to be supported.
350 *
351 * @if_typename: Interface type name
352 * @if_type: Interface type
353 * @max_devs: Maximum number of devices supported
354 * @desc: Pointer to list of devices for this interface type,
355 * or NULL to use @get_dev() instead
356 */
357struct blk_driver {
358 const char *if_typename;
359 enum if_type if_type;
360 int max_devs;
361 struct blk_desc *desc;
362 /**
363 * get_dev() - get a pointer to a block device given its number
364 *
365 * Each interface allocates its own devices and typically
366 * struct blk_desc is contained with the interface's data structure.
367 * There is no global numbering for block devices. This method allows
368 * the device for an interface type to be obtained when @desc is NULL.
369 *
370 * @devnum: Device number (0 for first device on that interface,
371 * 1 for second, etc.
372 * @descp: Returns pointer to the block device on success
373 * @return 0 if OK, -ve on error
374 */
375 int (*get_dev)(int devnum, struct blk_desc **descp);
376
377 /**
378 * select_hwpart() - Select a hardware partition
379 *
380 * Some devices (e.g. MMC) can support partitioning at the hardware
381 * level. This is quite separate from the normal idea of
382 * software-based partitions. MMC hardware partitions must be
383 * explicitly selected. Once selected only the region of the device
384 * covered by that partition is accessible.
385 *
386 * The MMC standard provides for two boot partitions (numbered 1 and 2),
387 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
388 * Partition 0 is the main user-data partition.
389 *
390 * @desc: Block device descriptor
391 * @hwpart: Hardware partition number to select. 0 means the main
392 * user-data partition, 1 is the first partition, 2 is
393 * the second, etc.
394 * @return 0 if OK, other value for an error
395 */
396 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
397};
398
399/*
400 * Declare a new U-Boot legacy block driver. New drivers should use driver
401 * model (UCLASS_BLK).
402 */
403#define U_BOOT_LEGACY_BLK(__name) \
404 ll_entry_declare(struct blk_driver, __name, blk_driver)
405
406struct blk_driver *blk_driver_lookup_type(int if_type);
407
Simon Glass09d71aa2016-02-29 15:25:55 -0700408#endif /* !CONFIG_BLK */
Simon Glass2a981dc2016-02-29 15:25:52 -0700409
Simon Glass6eef6ea2016-05-01 11:36:03 -0600410/**
411 * blk_get_devnum_by_typename() - Get a block device by type and number
412 *
413 * This looks through the available block devices of the given type, returning
414 * the one with the given @devnum.
415 *
416 * @if_type: Block device type
417 * @devnum: Device number
418 * @return point to block device descriptor, or NULL if not found
419 */
420struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
421
422/**
423 * blk_get_devnum_by_type() - Get a block device by type name, and number
424 *
425 * This looks up the block device type based on @if_typename, then calls
426 * blk_get_devnum_by_type().
427 *
428 * @if_typename: Block device type name
429 * @devnum: Device number
430 * @return point to block device descriptor, or NULL if not found
431 */
432struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
433 int devnum);
434
435/**
436 * blk_dselect_hwpart() - select a hardware partition
437 *
438 * This selects a hardware partition (such as is supported by MMC). The block
439 * device size may change as this effectively points the block device to a
440 * partition at the hardware level. See the select_hwpart() method above.
441 *
442 * @desc: Block device descriptor for the device to select
443 * @hwpart: Partition number to select
444 * @return 0 if OK, -ve on error
445 */
446int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
447
448/**
449 * blk_list_part() - list the partitions for block devices of a given type
450 *
451 * This looks up the partition type for each block device of type @if_type,
452 * then displays a list of partitions.
453 *
454 * @if_type: Block device type
455 * @return 0 if OK, -ENODEV if there is none of that type
456 */
457int blk_list_part(enum if_type if_type);
458
459/**
460 * blk_list_devices() - list the block devices of a given type
461 *
462 * This lists each block device of the type @if_type, showing the capacity
463 * as well as type-specific information.
464 *
465 * @if_type: Block device type
466 */
467void blk_list_devices(enum if_type if_type);
468
469/**
470 * blk_show_device() - show information about a given block device
471 *
472 * This shows the block device capacity as well as type-specific information.
473 *
474 * @if_type: Block device type
475 * @devnum: Device number
476 * @return 0 if OK, -ENODEV for invalid device number
477 */
478int blk_show_device(enum if_type if_type, int devnum);
479
480/**
481 * blk_print_device_num() - show information about a given block device
482 *
483 * This is similar to blk_show_device() but returns an error if the block
484 * device type is unknown.
485 *
486 * @if_type: Block device type
487 * @devnum: Device number
488 * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
489 * device is not connected
490 */
491int blk_print_device_num(enum if_type if_type, int devnum);
492
493/**
494 * blk_print_part_devnum() - print the partition information for a device
495 *
496 * @if_type: Block device type
497 * @devnum: Device number
498 * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
499 * the interface type is not supported, other -ve on other error
500 */
501int blk_print_part_devnum(enum if_type if_type, int devnum);
502
503/**
504 * blk_read_devnum() - read blocks from a device
505 *
506 * @if_type: Block device type
507 * @devnum: Device number
508 * @blkcnt: Number of blocks to read
509 * @buffer: Address to write data to
510 * @return number of blocks read, or -ve error number on error
511 */
512ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
513 lbaint_t blkcnt, void *buffer);
514
515/**
516 * blk_write_devnum() - write blocks to a device
517 *
518 * @if_type: Block device type
519 * @devnum: Device number
520 * @blkcnt: Number of blocks to write
521 * @buffer: Address to read data from
522 * @return number of blocks written, or -ve error number on error
523 */
524ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
525 lbaint_t blkcnt, const void *buffer);
526
527/**
528 * blk_select_hwpart_devnum() - select a hardware partition
529 *
530 * This is similar to blk_dselect_hwpart() but it looks up the interface and
531 * device number.
532 *
533 * @if_type: Block device type
534 * @devnum: Device number
535 * @hwpart: Partition number to select
536 * @return 0 if OK, -ve on error
537 */
538int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
539
Simon Glass1a736612016-02-29 15:25:39 -0700540#endif