blob: f62467105a203f51bc0a2758f49de1cc293f5fd3 [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 Glass09d71aa2016-02-29 15:25:55 -0700343#endif /* !CONFIG_BLK */
Simon Glass2a981dc2016-02-29 15:25:52 -0700344
Simon Glass1a736612016-02-29 15:25:39 -0700345#endif