blob: 263a791f4c8ebacfed46e01c58bd85cc88a48b35 [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 */
99int blkcache_read
100 (int iftype, int dev,
101 lbaint_t start, lbaint_t blkcnt,
102 unsigned long blksz, void *buffer);
103
104/**
105 * blkcache_fill() - make data read from a block device available
106 * to the block cache
107 *
108 * @param iftype - IF_TYPE_x for type of device
109 * @param dev - device index of particular type
110 * @param start - starting block number
111 * @param blkcnt - number of blocks available
112 * @param blksz - size in bytes of each block
113 * @param buf - buffer containing data to cache
114 *
115 */
116void blkcache_fill
117 (int iftype, int dev,
118 lbaint_t start, lbaint_t blkcnt,
119 unsigned long blksz, void const *buffer);
120
121/**
122 * blkcache_invalidate() - discard the cache for a set of blocks
123 * because of a write or device (re)initialization.
124 *
125 * @param iftype - IF_TYPE_x for type of device
126 * @param dev - device index of particular type
127 */
128void blkcache_invalidate
129 (int iftype, int dev);
130
131/**
132 * blkcache_configure() - configure block cache
133 *
134 * @param blocks - maximum blocks per entry
135 * @param entries - maximum entries in cache
136 */
137void blkcache_configure(unsigned blocks, unsigned entries);
138
139/*
140 * statistics of the block cache
141 */
142struct block_cache_stats {
143 unsigned hits;
144 unsigned misses;
145 unsigned entries; /* current entry count */
146 unsigned max_blocks_per_entry;
147 unsigned max_entries;
148};
149
150/**
151 * get_blkcache_stats() - return statistics and reset
152 *
153 * @param stats - statistics are copied here
154 */
155void blkcache_stats(struct block_cache_stats *stats);
156
157#else
158
159static inline int blkcache_read
160 (int iftype, int dev,
161 lbaint_t start, lbaint_t blkcnt,
162 unsigned long blksz, void *buffer)
163{
164 return 0;
165}
166
167static inline void blkcache_fill
168 (int iftype, int dev,
169 lbaint_t start, lbaint_t blkcnt,
170 unsigned long blksz, void const *buffer) {}
171
172static inline void blkcache_invalidate
173 (int iftype, int dev) {}
174
175#endif
176
Simon Glass09d71aa2016-02-29 15:25:55 -0700177#ifdef CONFIG_BLK
178struct udevice;
179
180/* Operations on block devices */
181struct blk_ops {
182 /**
183 * read() - read from a block device
184 *
185 * @dev: Device to read from
186 * @start: Start block number to read (0=first)
187 * @blkcnt: Number of blocks to read
188 * @buffer: Destination buffer for data read
189 * @return number of blocks read, or -ve error number (see the
190 * IS_ERR_VALUE() macro
191 */
192 unsigned long (*read)(struct udevice *dev, lbaint_t start,
193 lbaint_t blkcnt, void *buffer);
194
195 /**
196 * write() - write to a block device
197 *
198 * @dev: Device to write to
199 * @start: Start block number to write (0=first)
200 * @blkcnt: Number of blocks to write
201 * @buffer: Source buffer for data to write
202 * @return number of blocks written, or -ve error number (see the
203 * IS_ERR_VALUE() macro
204 */
205 unsigned long (*write)(struct udevice *dev, lbaint_t start,
206 lbaint_t blkcnt, const void *buffer);
207
208 /**
209 * erase() - erase a section of a block device
210 *
211 * @dev: Device to (partially) erase
212 * @start: Start block number to erase (0=first)
213 * @blkcnt: Number of blocks to erase
214 * @return number of blocks erased, or -ve error number (see the
215 * IS_ERR_VALUE() macro
216 */
217 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
218 lbaint_t blkcnt);
219};
220
221#define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
222
223/*
224 * These functions should take struct udevice instead of struct blk_desc,
225 * but this is convenient for migration to driver model. Add a 'd' prefix
226 * to the function operations, so that blk_read(), etc. can be reserved for
227 * functions with the correct arguments.
228 */
229unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
230 lbaint_t blkcnt, void *buffer);
231unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
232 lbaint_t blkcnt, const void *buffer);
233unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
234 lbaint_t blkcnt);
235
236/**
237 * blk_get_device() - Find and probe a block device ready for use
238 *
239 * @if_type: Interface type (enum if_type_t)
240 * @devnum: Device number (specific to each interface type)
241 * @devp: the device, if found
242 * @return - if found, -ENODEV if no device found, or other -ve error value
243 */
244int blk_get_device(int if_type, int devnum, struct udevice **devp);
245
246/**
247 * blk_first_device() - Find the first device for a given interface
248 *
249 * The device is probed ready for use
250 *
251 * @devnum: Device number (specific to each interface type)
252 * @devp: the device, if found
253 * @return 0 if found, -ENODEV if no device, or other -ve error value
254 */
255int blk_first_device(int if_type, struct udevice **devp);
256
257/**
258 * blk_next_device() - Find the next device for a given interface
259 *
260 * This can be called repeatedly after blk_first_device() to iterate through
261 * all devices of the given interface type.
262 *
263 * The device is probed ready for use
264 *
265 * @devp: On entry, the previous device returned. On exit, the next
266 * device, if found
267 * @return 0 if found, -ENODEV if no device, or other -ve error value
268 */
269int blk_next_device(struct udevice **devp);
270
271/**
272 * blk_create_device() - Create a new block device
273 *
274 * @parent: Parent of the new device
275 * @drv_name: Driver name to use for the block device
276 * @name: Name for the device
277 * @if_type: Interface type (enum if_type_t)
278 * @devnum: Device number, specific to the interface type
279 * @blksz: Block size of the device in bytes (typically 512)
280 * @size: Total size of the device in bytes
281 * @devp: the new device (which has not been probed)
282 */
283int blk_create_device(struct udevice *parent, const char *drv_name,
284 const char *name, int if_type, int devnum, int blksz,
285 lbaint_t size, struct udevice **devp);
286
287/**
288 * blk_prepare_device() - Prepare a block device for use
289 *
290 * This reads partition information from the device if supported.
291 *
292 * @dev: Device to prepare
293 * @return 0 if ok, -ve on error
294 */
295int blk_prepare_device(struct udevice *dev);
296
297/**
298 * blk_unbind_all() - Unbind all device of the given interface type
299 *
300 * The devices are removed and then unbound.
301 *
302 * @if_type: Interface type to unbind
303 * @return 0 if OK, -ve on error
304 */
305int blk_unbind_all(int if_type);
306
307#else
308#include <errno.h>
Simon Glass2a981dc2016-02-29 15:25:52 -0700309/*
310 * These functions should take struct udevice instead of struct blk_desc,
311 * but this is convenient for migration to driver model. Add a 'd' prefix
312 * to the function operations, so that blk_read(), etc. can be reserved for
313 * functions with the correct arguments.
314 */
315static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
316 lbaint_t blkcnt, void *buffer)
317{
Eric Nelsone40cf342016-03-28 10:05:44 -0700318 ulong blks_read;
319 if (blkcache_read(block_dev->if_type, block_dev->devnum,
320 start, blkcnt, block_dev->blksz, buffer))
321 return blkcnt;
322
Simon Glass2a981dc2016-02-29 15:25:52 -0700323 /*
324 * We could check if block_read is NULL and return -ENOSYS. But this
325 * bloats the code slightly (cause some board to fail to build), and
326 * it would be an error to try an operation that does not exist.
327 */
Eric Nelsone40cf342016-03-28 10:05:44 -0700328 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
329 if (blks_read == blkcnt)
330 blkcache_fill(block_dev->if_type, block_dev->devnum,
331 start, blkcnt, block_dev->blksz, buffer);
332
333 return blks_read;
Simon Glass2a981dc2016-02-29 15:25:52 -0700334}
335
336static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
337 lbaint_t blkcnt, const void *buffer)
338{
Eric Nelsone40cf342016-03-28 10:05:44 -0700339 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glass2a981dc2016-02-29 15:25:52 -0700340 return block_dev->block_write(block_dev, start, blkcnt, buffer);
341}
342
343static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
344 lbaint_t blkcnt)
345{
Eric Nelsone40cf342016-03-28 10:05:44 -0700346 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glass2a981dc2016-02-29 15:25:52 -0700347 return block_dev->block_erase(block_dev, start, blkcnt);
348}
Simon Glass09d71aa2016-02-29 15:25:55 -0700349#endif /* !CONFIG_BLK */
Simon Glass2a981dc2016-02-29 15:25:52 -0700350
Simon Glass1a736612016-02-29 15:25:39 -0700351#endif