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