Merge master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / drivers / mtd / mtdblock.c
1 /* 
2  * Direct MTD block device access
3  *
4  * $Id: mtdblock.c,v 1.66 2004/11/25 13:52:52 joern Exp $
5  *
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>
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/sched.h>        /* TASK_* */
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/blktrans.h>
21
22 static struct mtdblk_dev {
23         struct mtd_info *mtd;
24         int count;
25         struct semaphore cache_sem;
26         unsigned char *cache_data;
27         unsigned long cache_offset;
28         unsigned int cache_size;
29         enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
30 } *mtdblks[MAX_MTD_DEVICES];
31
32 /*
33  * Cache stuff...
34  * 
35  * Since typical flash erasable sectors are much larger than what Linux's
36  * buffer cache can handle, we must implement read-modify-write on flash
37  * sectors for each block write requests.  To avoid over-erasing flash sectors
38  * and to speed things up, we locally cache a whole flash sector while it is
39  * being written to until a different sector is required.
40  */
41
42 static void erase_callback(struct erase_info *done)
43 {
44         wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
45         wake_up(wait_q);
46 }
47
48 static int erase_write (struct mtd_info *mtd, unsigned long pos, 
49                         int len, const char *buf)
50 {
51         struct erase_info erase;
52         DECLARE_WAITQUEUE(wait, current);
53         wait_queue_head_t wait_q;
54         size_t retlen;
55         int ret;
56
57         /*
58          * First, let's erase the flash block.
59          */
60
61         init_waitqueue_head(&wait_q);
62         erase.mtd = mtd;
63         erase.callback = erase_callback;
64         erase.addr = pos;
65         erase.len = len;
66         erase.priv = (u_long)&wait_q;
67
68         set_current_state(TASK_INTERRUPTIBLE);
69         add_wait_queue(&wait_q, &wait);
70
71         ret = MTD_ERASE(mtd, &erase);
72         if (ret) {
73                 set_current_state(TASK_RUNNING);
74                 remove_wait_queue(&wait_q, &wait);
75                 printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
76                                      "on \"%s\" failed\n",
77                         pos, len, mtd->name);
78                 return ret;
79         }
80
81         schedule();  /* Wait for erase to finish. */
82         remove_wait_queue(&wait_q, &wait);
83
84         /*
85          * Next, writhe data to flash.
86          */
87
88         ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
89         if (ret)
90                 return ret;
91         if (retlen != len)
92                 return -EIO;
93         return 0;
94 }
95
96
97 static int write_cached_data (struct mtdblk_dev *mtdblk)
98 {
99         struct mtd_info *mtd = mtdblk->mtd;
100         int ret;
101
102         if (mtdblk->cache_state != STATE_DIRTY)
103                 return 0;
104
105         DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: writing cached data for \"%s\" "
106                         "at 0x%lx, size 0x%x\n", mtd->name, 
107                         mtdblk->cache_offset, mtdblk->cache_size);
108         
109         ret = erase_write (mtd, mtdblk->cache_offset, 
110                            mtdblk->cache_size, mtdblk->cache_data);
111         if (ret)
112                 return ret;
113
114         /*
115          * Here we could argubly set the cache state to STATE_CLEAN.
116          * However this could lead to inconsistency since we will not 
117          * be notified if this content is altered on the flash by other 
118          * means.  Let's declare it empty and leave buffering tasks to
119          * the buffer cache instead.
120          */
121         mtdblk->cache_state = STATE_EMPTY;
122         return 0;
123 }
124
125
126 static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos, 
127                             int len, const char *buf)
128 {
129         struct mtd_info *mtd = mtdblk->mtd;
130         unsigned int sect_size = mtdblk->cache_size;
131         size_t retlen;
132         int ret;
133
134         DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
135                 mtd->name, pos, len);
136         
137         if (!sect_size)
138                 return MTD_WRITE (mtd, pos, len, &retlen, buf);
139
140         while (len > 0) {
141                 unsigned long sect_start = (pos/sect_size)*sect_size;
142                 unsigned int offset = pos - sect_start;
143                 unsigned int size = sect_size - offset;
144                 if( size > len ) 
145                         size = len;
146
147                 if (size == sect_size) {
148                         /* 
149                          * We are covering a whole sector.  Thus there is no
150                          * need to bother with the cache while it may still be
151                          * useful for other partial writes.
152                          */
153                         ret = erase_write (mtd, pos, size, buf);
154                         if (ret)
155                                 return ret;
156                 } else {
157                         /* Partial sector: need to use the cache */
158
159                         if (mtdblk->cache_state == STATE_DIRTY &&
160                             mtdblk->cache_offset != sect_start) {
161                                 ret = write_cached_data(mtdblk);
162                                 if (ret) 
163                                         return ret;
164                         }
165
166                         if (mtdblk->cache_state == STATE_EMPTY ||
167                             mtdblk->cache_offset != sect_start) {
168                                 /* fill the cache with the current sector */
169                                 mtdblk->cache_state = STATE_EMPTY;
170                                 ret = MTD_READ(mtd, sect_start, sect_size, &retlen, mtdblk->cache_data);
171                                 if (ret)
172                                         return ret;
173                                 if (retlen != sect_size)
174                                         return -EIO;
175
176                                 mtdblk->cache_offset = sect_start;
177                                 mtdblk->cache_size = sect_size;
178                                 mtdblk->cache_state = STATE_CLEAN;
179                         }
180
181                         /* write data to our local cache */
182                         memcpy (mtdblk->cache_data + offset, buf, size);
183                         mtdblk->cache_state = STATE_DIRTY;
184                 }
185
186                 buf += size;
187                 pos += size;
188                 len -= size;
189         }
190
191         return 0;
192 }
193
194
195 static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos, 
196                            int len, char *buf)
197 {
198         struct mtd_info *mtd = mtdblk->mtd;
199         unsigned int sect_size = mtdblk->cache_size;
200         size_t retlen;
201         int ret;
202
203         DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n", 
204                         mtd->name, pos, len);
205         
206         if (!sect_size)
207                 return MTD_READ (mtd, pos, len, &retlen, buf);
208
209         while (len > 0) {
210                 unsigned long sect_start = (pos/sect_size)*sect_size;
211                 unsigned int offset = pos - sect_start;
212                 unsigned int size = sect_size - offset;
213                 if (size > len) 
214                         size = len;
215
216                 /*
217                  * Check if the requested data is already cached
218                  * Read the requested amount of data from our internal cache if it
219                  * contains what we want, otherwise we read the data directly
220                  * from flash.
221                  */
222                 if (mtdblk->cache_state != STATE_EMPTY &&
223                     mtdblk->cache_offset == sect_start) {
224                         memcpy (buf, mtdblk->cache_data + offset, size);
225                 } else {
226                         ret = MTD_READ (mtd, pos, size, &retlen, buf);
227                         if (ret)
228                                 return ret;
229                         if (retlen != size)
230                                 return -EIO;
231                 }
232
233                 buf += size;
234                 pos += size;
235                 len -= size;
236         }
237
238         return 0;
239 }
240
241 static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
242                               unsigned long block, char *buf)
243 {
244         struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
245         return do_cached_read(mtdblk, block<<9, 512, buf);
246 }
247
248 static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
249                               unsigned long block, char *buf)
250 {
251         struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
252         if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
253                 mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize);
254                 if (!mtdblk->cache_data)
255                         return -EINTR;
256                 /* -EINTR is not really correct, but it is the best match
257                  * documented in man 2 write for all cases.  We could also
258                  * return -EAGAIN sometimes, but why bother?
259                  */
260         }
261         return do_cached_write(mtdblk, block<<9, 512, buf);
262 }
263
264 static int mtdblock_open(struct mtd_blktrans_dev *mbd)
265 {
266         struct mtdblk_dev *mtdblk;
267         struct mtd_info *mtd = mbd->mtd;
268         int dev = mbd->devnum;
269
270         DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
271         
272         if (mtdblks[dev]) {
273                 mtdblks[dev]->count++;
274                 return 0;
275         }
276         
277         /* OK, it's not open. Create cache info for it */
278         mtdblk = kmalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
279         if (!mtdblk)
280                 return -ENOMEM;
281
282         memset(mtdblk, 0, sizeof(*mtdblk));
283         mtdblk->count = 1;
284         mtdblk->mtd = mtd;
285
286         init_MUTEX (&mtdblk->cache_sem);
287         mtdblk->cache_state = STATE_EMPTY;
288         if ((mtdblk->mtd->flags & MTD_CAP_RAM) != MTD_CAP_RAM &&
289             mtdblk->mtd->erasesize) {
290                 mtdblk->cache_size = mtdblk->mtd->erasesize;
291                 mtdblk->cache_data = NULL;
292         }
293
294         mtdblks[dev] = mtdblk;
295         
296         DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
297
298         return 0;
299 }
300
301 static int mtdblock_release(struct mtd_blktrans_dev *mbd)
302 {
303         int dev = mbd->devnum;
304         struct mtdblk_dev *mtdblk = mtdblks[dev];
305
306         DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
307
308         down(&mtdblk->cache_sem);
309         write_cached_data(mtdblk);
310         up(&mtdblk->cache_sem);
311
312         if (!--mtdblk->count) {
313                 /* It was the last usage. Free the device */
314                 mtdblks[dev] = NULL;
315                 if (mtdblk->mtd->sync)
316                         mtdblk->mtd->sync(mtdblk->mtd);
317                 vfree(mtdblk->cache_data);
318                 kfree(mtdblk);
319         }
320         DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
321
322         return 0;
323 }  
324
325 static int mtdblock_flush(struct mtd_blktrans_dev *dev)
326 {
327         struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
328
329         down(&mtdblk->cache_sem);
330         write_cached_data(mtdblk);
331         up(&mtdblk->cache_sem);
332
333         if (mtdblk->mtd->sync)
334                 mtdblk->mtd->sync(mtdblk->mtd);
335         return 0;
336 }
337
338 static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
339 {
340         struct mtd_blktrans_dev *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
341
342         if (!dev)
343                 return;
344
345         memset(dev, 0, sizeof(*dev));
346
347         dev->mtd = mtd;
348         dev->devnum = mtd->index;
349         dev->blksize = 512;
350         dev->size = mtd->size >> 9;
351         dev->tr = tr;
352
353         if (!(mtd->flags & MTD_WRITEABLE))
354                 dev->readonly = 1;
355
356         add_mtd_blktrans_dev(dev);
357 }
358
359 static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
360 {
361         del_mtd_blktrans_dev(dev);
362         kfree(dev);
363 }
364
365 static struct mtd_blktrans_ops mtdblock_tr = {
366         .name           = "mtdblock",
367         .major          = 31,
368         .part_bits      = 0,
369         .open           = mtdblock_open,
370         .flush          = mtdblock_flush,
371         .release        = mtdblock_release,
372         .readsect       = mtdblock_readsect,
373         .writesect      = mtdblock_writesect,
374         .add_mtd        = mtdblock_add_mtd,
375         .remove_dev     = mtdblock_remove_dev,
376         .owner          = THIS_MODULE,
377 };
378
379 static int __init init_mtdblock(void)
380 {
381         return register_mtd_blktrans(&mtdblock_tr);
382 }
383
384 static void __exit cleanup_mtdblock(void)
385 {
386         deregister_mtd_blktrans(&mtdblock_tr);
387 }
388
389 module_init(init_mtdblock);
390 module_exit(cleanup_mtdblock);
391
392
393 MODULE_LICENSE("GPL");
394 MODULE_AUTHOR("Nicolas Pitre <nico@cam.org> et al.");
395 MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");