[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / drivers / mtd / mtdblock.c
blobb7c32c242bc712ce6e0bd87e358dc33eb40a8a56
1 /*
2 * Direct MTD block device access
4 * $Id: mtdblock.c,v 1.66 2004/11/25 13:52:52 joern Exp $
6 * (C) 2000-2003 Nicolas Pitre <nico@cam.org>
7 * (C) 1999-2003 David Woodhouse <dwmw2@infradead.org>
8 */
10 #include <linux/config.h>
11 #include <linux/types.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/fs.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/blktrans.h>
21 static struct mtdblk_dev {
22 struct mtd_info *mtd;
23 int count;
24 struct semaphore cache_sem;
25 unsigned char *cache_data;
26 unsigned long cache_offset;
27 unsigned int cache_size;
28 enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
29 } *mtdblks[MAX_MTD_DEVICES];
32 * Cache stuff...
34 * Since typical flash erasable sectors are much larger than what Linux's
35 * buffer cache can handle, we must implement read-modify-write on flash
36 * sectors for each block write requests. To avoid over-erasing flash sectors
37 * and to speed things up, we locally cache a whole flash sector while it is
38 * being written to until a different sector is required.
41 static void erase_callback(struct erase_info *done)
43 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
44 wake_up(wait_q);
47 static int erase_write (struct mtd_info *mtd, unsigned long pos,
48 int len, const char *buf)
50 struct erase_info erase;
51 DECLARE_WAITQUEUE(wait, current);
52 wait_queue_head_t wait_q;
53 size_t retlen;
54 int ret;
57 * First, let's erase the flash block.
60 init_waitqueue_head(&wait_q);
61 erase.mtd = mtd;
62 erase.callback = erase_callback;
63 erase.addr = pos;
64 erase.len = len;
65 erase.priv = (u_long)&wait_q;
67 set_current_state(TASK_INTERRUPTIBLE);
68 add_wait_queue(&wait_q, &wait);
70 ret = MTD_ERASE(mtd, &erase);
71 if (ret) {
72 set_current_state(TASK_RUNNING);
73 remove_wait_queue(&wait_q, &wait);
74 printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
75 "on \"%s\" failed\n",
76 pos, len, mtd->name);
77 return ret;
80 schedule(); /* Wait for erase to finish. */
81 remove_wait_queue(&wait_q, &wait);
84 * Next, writhe data to flash.
87 ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
88 if (ret)
89 return ret;
90 if (retlen != len)
91 return -EIO;
92 return 0;
96 static int write_cached_data (struct mtdblk_dev *mtdblk)
98 struct mtd_info *mtd = mtdblk->mtd;
99 int ret;
101 if (mtdblk->cache_state != STATE_DIRTY)
102 return 0;
104 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: writing cached data for \"%s\" "
105 "at 0x%lx, size 0x%x\n", mtd->name,
106 mtdblk->cache_offset, mtdblk->cache_size);
108 ret = erase_write (mtd, mtdblk->cache_offset,
109 mtdblk->cache_size, mtdblk->cache_data);
110 if (ret)
111 return ret;
114 * Here we could argubly set the cache state to STATE_CLEAN.
115 * However this could lead to inconsistency since we will not
116 * be notified if this content is altered on the flash by other
117 * means. Let's declare it empty and leave buffering tasks to
118 * the buffer cache instead.
120 mtdblk->cache_state = STATE_EMPTY;
121 return 0;
125 static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
126 int len, const char *buf)
128 struct mtd_info *mtd = mtdblk->mtd;
129 unsigned int sect_size = mtdblk->cache_size;
130 size_t retlen;
131 int ret;
133 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
134 mtd->name, pos, len);
136 if (!sect_size)
137 return MTD_WRITE (mtd, pos, len, &retlen, buf);
139 while (len > 0) {
140 unsigned long sect_start = (pos/sect_size)*sect_size;
141 unsigned int offset = pos - sect_start;
142 unsigned int size = sect_size - offset;
143 if( size > len )
144 size = len;
146 if (size == sect_size) {
148 * We are covering a whole sector. Thus there is no
149 * need to bother with the cache while it may still be
150 * useful for other partial writes.
152 ret = erase_write (mtd, pos, size, buf);
153 if (ret)
154 return ret;
155 } else {
156 /* Partial sector: need to use the cache */
158 if (mtdblk->cache_state == STATE_DIRTY &&
159 mtdblk->cache_offset != sect_start) {
160 ret = write_cached_data(mtdblk);
161 if (ret)
162 return ret;
165 if (mtdblk->cache_state == STATE_EMPTY ||
166 mtdblk->cache_offset != sect_start) {
167 /* fill the cache with the current sector */
168 mtdblk->cache_state = STATE_EMPTY;
169 ret = MTD_READ(mtd, sect_start, sect_size, &retlen, mtdblk->cache_data);
170 if (ret)
171 return ret;
172 if (retlen != sect_size)
173 return -EIO;
175 mtdblk->cache_offset = sect_start;
176 mtdblk->cache_size = sect_size;
177 mtdblk->cache_state = STATE_CLEAN;
180 /* write data to our local cache */
181 memcpy (mtdblk->cache_data + offset, buf, size);
182 mtdblk->cache_state = STATE_DIRTY;
185 buf += size;
186 pos += size;
187 len -= size;
190 return 0;
194 static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
195 int len, char *buf)
197 struct mtd_info *mtd = mtdblk->mtd;
198 unsigned int sect_size = mtdblk->cache_size;
199 size_t retlen;
200 int ret;
202 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
203 mtd->name, pos, len);
205 if (!sect_size)
206 return MTD_READ (mtd, pos, len, &retlen, buf);
208 while (len > 0) {
209 unsigned long sect_start = (pos/sect_size)*sect_size;
210 unsigned int offset = pos - sect_start;
211 unsigned int size = sect_size - offset;
212 if (size > len)
213 size = len;
216 * Check if the requested data is already cached
217 * Read the requested amount of data from our internal cache if it
218 * contains what we want, otherwise we read the data directly
219 * from flash.
221 if (mtdblk->cache_state != STATE_EMPTY &&
222 mtdblk->cache_offset == sect_start) {
223 memcpy (buf, mtdblk->cache_data + offset, size);
224 } else {
225 ret = MTD_READ (mtd, pos, size, &retlen, buf);
226 if (ret)
227 return ret;
228 if (retlen != size)
229 return -EIO;
232 buf += size;
233 pos += size;
234 len -= size;
237 return 0;
240 static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
241 unsigned long block, char *buf)
243 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
244 return do_cached_read(mtdblk, block<<9, 512, buf);
247 static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
248 unsigned long block, char *buf)
250 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
251 if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
252 mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize);
253 if (!mtdblk->cache_data)
254 return -EINTR;
255 /* -EINTR is not really correct, but it is the best match
256 * documented in man 2 write for all cases. We could also
257 * return -EAGAIN sometimes, but why bother?
260 return do_cached_write(mtdblk, block<<9, 512, buf);
263 static int mtdblock_open(struct mtd_blktrans_dev *mbd)
265 struct mtdblk_dev *mtdblk;
266 struct mtd_info *mtd = mbd->mtd;
267 int dev = mbd->devnum;
269 DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
271 if (mtdblks[dev]) {
272 mtdblks[dev]->count++;
273 return 0;
276 /* OK, it's not open. Create cache info for it */
277 mtdblk = kmalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
278 if (!mtdblk)
279 return -ENOMEM;
281 memset(mtdblk, 0, sizeof(*mtdblk));
282 mtdblk->count = 1;
283 mtdblk->mtd = mtd;
285 init_MUTEX (&mtdblk->cache_sem);
286 mtdblk->cache_state = STATE_EMPTY;
287 if ((mtdblk->mtd->flags & MTD_CAP_RAM) != MTD_CAP_RAM &&
288 mtdblk->mtd->erasesize) {
289 mtdblk->cache_size = mtdblk->mtd->erasesize;
290 mtdblk->cache_data = NULL;
293 mtdblks[dev] = mtdblk;
295 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
297 return 0;
300 static int mtdblock_release(struct mtd_blktrans_dev *mbd)
302 int dev = mbd->devnum;
303 struct mtdblk_dev *mtdblk = mtdblks[dev];
305 DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
307 down(&mtdblk->cache_sem);
308 write_cached_data(mtdblk);
309 up(&mtdblk->cache_sem);
311 if (!--mtdblk->count) {
312 /* It was the last usage. Free the device */
313 mtdblks[dev] = NULL;
314 if (mtdblk->mtd->sync)
315 mtdblk->mtd->sync(mtdblk->mtd);
316 vfree(mtdblk->cache_data);
317 kfree(mtdblk);
319 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
321 return 0;
324 static int mtdblock_flush(struct mtd_blktrans_dev *dev)
326 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
328 down(&mtdblk->cache_sem);
329 write_cached_data(mtdblk);
330 up(&mtdblk->cache_sem);
332 if (mtdblk->mtd->sync)
333 mtdblk->mtd->sync(mtdblk->mtd);
334 return 0;
337 static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
339 struct mtd_blktrans_dev *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
341 if (!dev)
342 return;
344 memset(dev, 0, sizeof(*dev));
346 dev->mtd = mtd;
347 dev->devnum = mtd->index;
348 dev->blksize = 512;
349 dev->size = mtd->size >> 9;
350 dev->tr = tr;
352 if (!(mtd->flags & MTD_WRITEABLE))
353 dev->readonly = 1;
355 add_mtd_blktrans_dev(dev);
358 static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
360 del_mtd_blktrans_dev(dev);
361 kfree(dev);
364 static struct mtd_blktrans_ops mtdblock_tr = {
365 .name = "mtdblock",
366 .major = 31,
367 .part_bits = 0,
368 .open = mtdblock_open,
369 .flush = mtdblock_flush,
370 .release = mtdblock_release,
371 .readsect = mtdblock_readsect,
372 .writesect = mtdblock_writesect,
373 .add_mtd = mtdblock_add_mtd,
374 .remove_dev = mtdblock_remove_dev,
375 .owner = THIS_MODULE,
378 static int __init init_mtdblock(void)
380 return register_mtd_blktrans(&mtdblock_tr);
383 static void __exit cleanup_mtdblock(void)
385 deregister_mtd_blktrans(&mtdblock_tr);
388 module_init(init_mtdblock);
389 module_exit(cleanup_mtdblock);
392 MODULE_LICENSE("GPL");
393 MODULE_AUTHOR("Nicolas Pitre <nico@cam.org> et al.");
394 MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");