blob: e5c78463ebfd5361c38429c171720b0f1cef281b [file] [log] [blame]
Thomas Gleixner97894cd2005-11-07 11:15:26 +00001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Direct MTD block device access
3 *
Thomas Gleixner97894cd2005-11-07 11:15:26 +00004 * $Id: mtdblock.c,v 1.68 2005/11/07 11:14:20 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * (C) 2000-2003 Nicolas Pitre <nico@cam.org>
7 * (C) 1999-2003 David Woodhouse <dwmw2@infradead.org>
8 */
9
10#include <linux/config.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/fs.h>
12#include <linux/init.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010013#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010017#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/vmalloc.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/mtd/mtd.h>
21#include <linux/mtd/blktrans.h>
Ingo Molnar48b19262006-03-31 02:29:41 -080022#include <linux/mutex.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25static struct mtdblk_dev {
26 struct mtd_info *mtd;
27 int count;
Ingo Molnar48b19262006-03-31 02:29:41 -080028 struct mutex cache_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 unsigned char *cache_data;
30 unsigned long cache_offset;
31 unsigned int cache_size;
32 enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
33} *mtdblks[MAX_MTD_DEVICES];
34
35/*
36 * Cache stuff...
Thomas Gleixner97894cd2005-11-07 11:15:26 +000037 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Since typical flash erasable sectors are much larger than what Linux's
39 * buffer cache can handle, we must implement read-modify-write on flash
40 * sectors for each block write requests. To avoid over-erasing flash sectors
41 * and to speed things up, we locally cache a whole flash sector while it is
42 * being written to until a different sector is required.
43 */
44
45static void erase_callback(struct erase_info *done)
46{
47 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
48 wake_up(wait_q);
49}
50
Thomas Gleixner97894cd2005-11-07 11:15:26 +000051static int erase_write (struct mtd_info *mtd, unsigned long pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int len, const char *buf)
53{
54 struct erase_info erase;
55 DECLARE_WAITQUEUE(wait, current);
56 wait_queue_head_t wait_q;
57 size_t retlen;
58 int ret;
59
60 /*
61 * First, let's erase the flash block.
62 */
63
64 init_waitqueue_head(&wait_q);
65 erase.mtd = mtd;
66 erase.callback = erase_callback;
67 erase.addr = pos;
68 erase.len = len;
69 erase.priv = (u_long)&wait_q;
70
71 set_current_state(TASK_INTERRUPTIBLE);
72 add_wait_queue(&wait_q, &wait);
73
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +020074 ret = mtd->erase(mtd, &erase);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (ret) {
76 set_current_state(TASK_RUNNING);
77 remove_wait_queue(&wait_q, &wait);
78 printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
79 "on \"%s\" failed\n",
80 pos, len, mtd->name);
81 return ret;
82 }
83
84 schedule(); /* Wait for erase to finish. */
85 remove_wait_queue(&wait_q, &wait);
86
87 /*
88 * Next, writhe data to flash.
89 */
90
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +020091 ret = mtd->write(mtd, pos, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (ret)
93 return ret;
94 if (retlen != len)
95 return -EIO;
96 return 0;
97}
98
99
100static int write_cached_data (struct mtdblk_dev *mtdblk)
101{
102 struct mtd_info *mtd = mtdblk->mtd;
103 int ret;
104
105 if (mtdblk->cache_state != STATE_DIRTY)
106 return 0;
107
108 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: writing cached data for \"%s\" "
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000109 "at 0x%lx, size 0x%x\n", mtd->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 mtdblk->cache_offset, mtdblk->cache_size);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000111
112 ret = erase_write (mtd, mtdblk->cache_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 mtdblk->cache_size, mtdblk->cache_data);
114 if (ret)
115 return ret;
116
117 /*
118 * Here we could argubly set the cache state to STATE_CLEAN.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000119 * However this could lead to inconsistency since we will not
120 * be notified if this content is altered on the flash by other
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * means. Let's declare it empty and leave buffering tasks to
122 * the buffer cache instead.
123 */
124 mtdblk->cache_state = STATE_EMPTY;
125 return 0;
126}
127
128
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000129static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 int len, const char *buf)
131{
132 struct mtd_info *mtd = mtdblk->mtd;
133 unsigned int sect_size = mtdblk->cache_size;
134 size_t retlen;
135 int ret;
136
137 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
138 mtd->name, pos, len);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (!sect_size)
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200141 return mtd->write(mtd, pos, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 while (len > 0) {
144 unsigned long sect_start = (pos/sect_size)*sect_size;
145 unsigned int offset = pos - sect_start;
146 unsigned int size = sect_size - offset;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000147 if( size > len )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 size = len;
149
150 if (size == sect_size) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000151 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * We are covering a whole sector. Thus there is no
153 * need to bother with the cache while it may still be
154 * useful for other partial writes.
155 */
156 ret = erase_write (mtd, pos, size, buf);
157 if (ret)
158 return ret;
159 } else {
160 /* Partial sector: need to use the cache */
161
162 if (mtdblk->cache_state == STATE_DIRTY &&
163 mtdblk->cache_offset != sect_start) {
164 ret = write_cached_data(mtdblk);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000165 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return ret;
167 }
168
169 if (mtdblk->cache_state == STATE_EMPTY ||
170 mtdblk->cache_offset != sect_start) {
171 /* fill the cache with the current sector */
172 mtdblk->cache_state = STATE_EMPTY;
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200173 ret = mtd->read(mtd, sect_start, sect_size,
174 &retlen, mtdblk->cache_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (ret)
176 return ret;
177 if (retlen != sect_size)
178 return -EIO;
179
180 mtdblk->cache_offset = sect_start;
181 mtdblk->cache_size = sect_size;
182 mtdblk->cache_state = STATE_CLEAN;
183 }
184
185 /* write data to our local cache */
186 memcpy (mtdblk->cache_data + offset, buf, size);
187 mtdblk->cache_state = STATE_DIRTY;
188 }
189
190 buf += size;
191 pos += size;
192 len -= size;
193 }
194
195 return 0;
196}
197
198
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000199static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int len, char *buf)
201{
202 struct mtd_info *mtd = mtdblk->mtd;
203 unsigned int sect_size = mtdblk->cache_size;
204 size_t retlen;
205 int ret;
206
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000207 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 mtd->name, pos, len);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (!sect_size)
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200211 return mtd->read(mtd, pos, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 while (len > 0) {
214 unsigned long sect_start = (pos/sect_size)*sect_size;
215 unsigned int offset = pos - sect_start;
216 unsigned int size = sect_size - offset;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000217 if (size > len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 size = len;
219
220 /*
221 * Check if the requested data is already cached
222 * Read the requested amount of data from our internal cache if it
223 * contains what we want, otherwise we read the data directly
224 * from flash.
225 */
226 if (mtdblk->cache_state != STATE_EMPTY &&
227 mtdblk->cache_offset == sect_start) {
228 memcpy (buf, mtdblk->cache_data + offset, size);
229 } else {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200230 ret = mtd->read(mtd, pos, size, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (ret)
232 return ret;
233 if (retlen != size)
234 return -EIO;
235 }
236
237 buf += size;
238 pos += size;
239 len -= size;
240 }
241
242 return 0;
243}
244
245static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
246 unsigned long block, char *buf)
247{
248 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
249 return do_cached_read(mtdblk, block<<9, 512, buf);
250}
251
252static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
253 unsigned long block, char *buf)
254{
255 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
256 if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
257 mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize);
258 if (!mtdblk->cache_data)
259 return -EINTR;
260 /* -EINTR is not really correct, but it is the best match
261 * documented in man 2 write for all cases. We could also
262 * return -EAGAIN sometimes, but why bother?
263 */
264 }
265 return do_cached_write(mtdblk, block<<9, 512, buf);
266}
267
268static int mtdblock_open(struct mtd_blktrans_dev *mbd)
269{
270 struct mtdblk_dev *mtdblk;
271 struct mtd_info *mtd = mbd->mtd;
272 int dev = mbd->devnum;
273
274 DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (mtdblks[dev]) {
277 mtdblks[dev]->count++;
278 return 0;
279 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* OK, it's not open. Create cache info for it */
282 mtdblk = kmalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
283 if (!mtdblk)
284 return -ENOMEM;
285
286 memset(mtdblk, 0, sizeof(*mtdblk));
287 mtdblk->count = 1;
288 mtdblk->mtd = mtd;
289
Ingo Molnar48b19262006-03-31 02:29:41 -0800290 mutex_init(&mtdblk->cache_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 mtdblk->cache_state = STATE_EMPTY;
Joern Engel92cbfdc2006-05-30 14:25:24 +0200292 if ( !(mtdblk->mtd->flags & MTD_NO_ERASE) && mtdblk->mtd->erasesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 mtdblk->cache_size = mtdblk->mtd->erasesize;
294 mtdblk->cache_data = NULL;
295 }
296
297 mtdblks[dev] = mtdblk;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
300
301 return 0;
302}
303
304static int mtdblock_release(struct mtd_blktrans_dev *mbd)
305{
306 int dev = mbd->devnum;
307 struct mtdblk_dev *mtdblk = mtdblks[dev];
308
309 DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
310
Ingo Molnar48b19262006-03-31 02:29:41 -0800311 mutex_lock(&mtdblk->cache_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 write_cached_data(mtdblk);
Ingo Molnar48b19262006-03-31 02:29:41 -0800313 mutex_unlock(&mtdblk->cache_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (!--mtdblk->count) {
316 /* It was the last usage. Free the device */
317 mtdblks[dev] = NULL;
318 if (mtdblk->mtd->sync)
319 mtdblk->mtd->sync(mtdblk->mtd);
320 vfree(mtdblk->cache_data);
321 kfree(mtdblk);
322 }
323 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
324
325 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000326}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328static int mtdblock_flush(struct mtd_blktrans_dev *dev)
329{
330 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
331
Ingo Molnar48b19262006-03-31 02:29:41 -0800332 mutex_lock(&mtdblk->cache_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 write_cached_data(mtdblk);
Ingo Molnar48b19262006-03-31 02:29:41 -0800334 mutex_unlock(&mtdblk->cache_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 if (mtdblk->mtd->sync)
337 mtdblk->mtd->sync(mtdblk->mtd);
338 return 0;
339}
340
341static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
342{
343 struct mtd_blktrans_dev *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
344
345 if (!dev)
346 return;
347
348 memset(dev, 0, sizeof(*dev));
349
350 dev->mtd = mtd;
351 dev->devnum = mtd->index;
352 dev->blksize = 512;
353 dev->size = mtd->size >> 9;
354 dev->tr = tr;
355
356 if (!(mtd->flags & MTD_WRITEABLE))
357 dev->readonly = 1;
358
359 add_mtd_blktrans_dev(dev);
360}
361
362static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
363{
364 del_mtd_blktrans_dev(dev);
365 kfree(dev);
366}
367
368static struct mtd_blktrans_ops mtdblock_tr = {
369 .name = "mtdblock",
370 .major = 31,
371 .part_bits = 0,
372 .open = mtdblock_open,
373 .flush = mtdblock_flush,
374 .release = mtdblock_release,
375 .readsect = mtdblock_readsect,
376 .writesect = mtdblock_writesect,
377 .add_mtd = mtdblock_add_mtd,
378 .remove_dev = mtdblock_remove_dev,
379 .owner = THIS_MODULE,
380};
381
382static int __init init_mtdblock(void)
383{
384 return register_mtd_blktrans(&mtdblock_tr);
385}
386
387static void __exit cleanup_mtdblock(void)
388{
389 deregister_mtd_blktrans(&mtdblock_tr);
390}
391
392module_init(init_mtdblock);
393module_exit(cleanup_mtdblock);
394
395
396MODULE_LICENSE("GPL");
397MODULE_AUTHOR("Nicolas Pitre <nico@cam.org> et al.");
398MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");