]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/mtd/mtdoops.c
mtd: mtdoops: limit the maximum mtd partition size
[mv-sheeva.git] / drivers / mtd / mtdoops.c
1 /*
2  * MTD Oops/Panic logger
3  *
4  * Copyright (C) 2007 Nokia Corporation. All rights reserved.
5  *
6  * Author: Richard Purdie <rpurdie@openedhand.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/console.h>
27 #include <linux/vmalloc.h>
28 #include <linux/workqueue.h>
29 #include <linux/sched.h>
30 #include <linux/wait.h>
31 #include <linux/delay.h>
32 #include <linux/spinlock.h>
33 #include <linux/interrupt.h>
34 #include <linux/mtd/mtd.h>
35
36 /* Maximum MTD partition size */
37 #define MTDOOPS_MAX_MTD_SIZE (8 * 1024 * 1024)
38
39 #define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
40 #define OOPS_PAGE_SIZE 4096
41
42 static struct mtdoops_context {
43         int mtd_index;
44         struct work_struct work_erase;
45         struct work_struct work_write;
46         struct mtd_info *mtd;
47         int oops_pages;
48         int nextpage;
49         int nextcount;
50         unsigned long *oops_page_used;
51         char *name;
52
53         void *oops_buf;
54
55         /* writecount and disabling ready are spin lock protected */
56         spinlock_t writecount_lock;
57         int ready;
58         int writecount;
59 } oops_cxt;
60
61 static void mark_page_used(struct mtdoops_context *cxt, int page)
62 {
63         set_bit(page, cxt->oops_page_used);
64 }
65
66 static void mark_page_unused(struct mtdoops_context *cxt, int page)
67 {
68         clear_bit(page, cxt->oops_page_used);
69 }
70
71 static int page_is_used(struct mtdoops_context *cxt, int page)
72 {
73         return test_bit(page, cxt->oops_page_used);
74 }
75
76 static void mtdoops_erase_callback(struct erase_info *done)
77 {
78         wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
79         wake_up(wait_q);
80 }
81
82 static int mtdoops_erase_block(struct mtdoops_context *cxt, int offset)
83 {
84         struct mtd_info *mtd = cxt->mtd;
85         u32 start_page_offset = mtd_div_by_eb(offset, mtd) * mtd->erasesize;
86         u32 start_page = start_page_offset / OOPS_PAGE_SIZE;
87         u32 erase_pages = mtd->erasesize / OOPS_PAGE_SIZE;
88         struct erase_info erase;
89         DECLARE_WAITQUEUE(wait, current);
90         wait_queue_head_t wait_q;
91         int ret;
92         int page;
93
94         init_waitqueue_head(&wait_q);
95         erase.mtd = mtd;
96         erase.callback = mtdoops_erase_callback;
97         erase.addr = offset;
98         erase.len = mtd->erasesize;
99         erase.priv = (u_long)&wait_q;
100
101         set_current_state(TASK_INTERRUPTIBLE);
102         add_wait_queue(&wait_q, &wait);
103
104         ret = mtd->erase(mtd, &erase);
105         if (ret) {
106                 set_current_state(TASK_RUNNING);
107                 remove_wait_queue(&wait_q, &wait);
108                 printk(KERN_WARNING "mtdoops: erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
109                        (unsigned long long)erase.addr,
110                        (unsigned long long)erase.len, mtd->name);
111                 return ret;
112         }
113
114         schedule();  /* Wait for erase to finish. */
115         remove_wait_queue(&wait_q, &wait);
116
117         /* Mark pages as unused */
118         for (page = start_page; page < start_page + erase_pages; page++)
119                 mark_page_unused(cxt, page);
120
121         return 0;
122 }
123
124 static void mtdoops_inc_counter(struct mtdoops_context *cxt)
125 {
126         cxt->nextpage++;
127         if (cxt->nextpage >= cxt->oops_pages)
128                 cxt->nextpage = 0;
129         cxt->nextcount++;
130         if (cxt->nextcount == 0xffffffff)
131                 cxt->nextcount = 0;
132
133         if (page_is_used(cxt, cxt->nextpage)) {
134                 schedule_work(&cxt->work_erase);
135                 return;
136         }
137
138         printk(KERN_DEBUG "mtdoops: ready %d, %d (no erase)\n",
139                cxt->nextpage, cxt->nextcount);
140         cxt->ready = 1;
141 }
142
143 /* Scheduled work - when we can't proceed without erasing a block */
144 static void mtdoops_workfunc_erase(struct work_struct *work)
145 {
146         struct mtdoops_context *cxt =
147                         container_of(work, struct mtdoops_context, work_erase);
148         struct mtd_info *mtd = cxt->mtd;
149         int i = 0, j, ret, mod;
150
151         /* We were unregistered */
152         if (!mtd)
153                 return;
154
155         mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize;
156         if (mod != 0) {
157                 cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE);
158                 if (cxt->nextpage >= cxt->oops_pages)
159                         cxt->nextpage = 0;
160         }
161
162         while (mtd->block_isbad) {
163                 ret = mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
164                 if (!ret)
165                         break;
166                 if (ret < 0) {
167                         printk(KERN_ERR "mtdoops: block_isbad failed, aborting\n");
168                         return;
169                 }
170 badblock:
171                 printk(KERN_WARNING "mtdoops: bad block at %08x\n",
172                        cxt->nextpage * OOPS_PAGE_SIZE);
173                 i++;
174                 cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE);
175                 if (cxt->nextpage >= cxt->oops_pages)
176                         cxt->nextpage = 0;
177                 if (i == cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE)) {
178                         printk(KERN_ERR "mtdoops: all blocks bad!\n");
179                         return;
180                 }
181         }
182
183         for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
184                 ret = mtdoops_erase_block(cxt, cxt->nextpage * OOPS_PAGE_SIZE);
185
186         if (ret >= 0) {
187                 printk(KERN_DEBUG "mtdoops: ready %d, %d\n",
188                        cxt->nextpage, cxt->nextcount);
189                 cxt->ready = 1;
190                 return;
191         }
192
193         if (mtd->block_markbad && ret == -EIO) {
194                 ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
195                 if (ret < 0) {
196                         printk(KERN_ERR "mtdoops: block_markbad failed, aborting\n");
197                         return;
198                 }
199         }
200         goto badblock;
201 }
202
203 static void mtdoops_write(struct mtdoops_context *cxt, int panic)
204 {
205         struct mtd_info *mtd = cxt->mtd;
206         size_t retlen;
207         int ret;
208
209         if (cxt->writecount < OOPS_PAGE_SIZE)
210                 memset(cxt->oops_buf + cxt->writecount, 0xff,
211                                         OOPS_PAGE_SIZE - cxt->writecount);
212
213         if (panic)
214                 ret = mtd->panic_write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
215                                         OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
216         else
217                 ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
218                                         OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
219
220         cxt->writecount = 0;
221
222         if (retlen != OOPS_PAGE_SIZE || ret < 0)
223                 printk(KERN_ERR "mtdoops: write failure at %d (%td of %d written), error %d\n",
224                        cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret);
225         mark_page_used(cxt, cxt->nextpage);
226
227         mtdoops_inc_counter(cxt);
228 }
229
230
231 static void mtdoops_workfunc_write(struct work_struct *work)
232 {
233         struct mtdoops_context *cxt =
234                         container_of(work, struct mtdoops_context, work_write);
235
236         mtdoops_write(cxt, 0);
237 }
238
239 static void find_next_position(struct mtdoops_context *cxt)
240 {
241         struct mtd_info *mtd = cxt->mtd;
242         int ret, page, maxpos = 0;
243         u32 count[2], maxcount = 0xffffffff;
244         size_t retlen;
245
246         for (page = 0; page < cxt->oops_pages; page++) {
247                 /* Assume the page is used */
248                 mark_page_used(cxt, page);
249                 ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 8, &retlen, (u_char *) &count[0]);
250                 if (retlen != 8 || (ret < 0 && ret != -EUCLEAN)) {
251                         printk(KERN_ERR "mtdoops: read failure at %d (%td of 8 read), err %d\n",
252                                page * OOPS_PAGE_SIZE, retlen, ret);
253                         continue;
254                 }
255
256                 if (count[0] == 0xffffffff && count[1] == 0xffffffff)
257                         mark_page_unused(cxt, page);
258                 if (count[1] != MTDOOPS_KERNMSG_MAGIC)
259                         continue;
260                 if (count[0] == 0xffffffff)
261                         continue;
262                 if (maxcount == 0xffffffff) {
263                         maxcount = count[0];
264                         maxpos = page;
265                 } else if (count[0] < 0x40000000 && maxcount > 0xc0000000) {
266                         maxcount = count[0];
267                         maxpos = page;
268                 } else if (count[0] > maxcount && count[0] < 0xc0000000) {
269                         maxcount = count[0];
270                         maxpos = page;
271                 } else if (count[0] > maxcount && count[0] > 0xc0000000
272                                         && maxcount > 0x80000000) {
273                         maxcount = count[0];
274                         maxpos = page;
275                 }
276         }
277         if (maxcount == 0xffffffff) {
278                 cxt->nextpage = 0;
279                 cxt->nextcount = 1;
280                 schedule_work(&cxt->work_erase);
281                 return;
282         }
283
284         cxt->nextpage = maxpos;
285         cxt->nextcount = maxcount;
286
287         mtdoops_inc_counter(cxt);
288 }
289
290
291 static void mtdoops_notify_add(struct mtd_info *mtd)
292 {
293         struct mtdoops_context *cxt = &oops_cxt;
294         u64 mtdoops_pages = mtd->size;
295
296         do_div(mtdoops_pages, OOPS_PAGE_SIZE);
297
298         if (cxt->name && !strcmp(mtd->name, cxt->name))
299                 cxt->mtd_index = mtd->index;
300
301         if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
302                 return;
303
304         if (mtd->size < mtd->erasesize * 2) {
305                 printk(KERN_ERR "mtdoops: MTD partition %d not big enough for mtdoops\n",
306                        mtd->index);
307                 return;
308         }
309
310         if (mtd->erasesize < OOPS_PAGE_SIZE) {
311                 printk(KERN_ERR "mtdoops: eraseblock size of MTD partition %d too small\n",
312                        mtd->index);
313                 return;
314         }
315
316         if (mtd->size > MTDOOPS_MAX_MTD_SIZE) {
317                 printk(KERN_ERR "mtdoops: mtd%d is too large (limit is %d MiB)\n",
318                        mtd->index, MTDOOPS_MAX_MTD_SIZE / 1024 / 1024);
319                 return;
320         }
321
322         /* oops_page_used is a bit field */
323         cxt->oops_page_used = vmalloc(DIV_ROUND_UP(mtdoops_pages,
324                         BITS_PER_LONG));
325         if (!cxt->oops_page_used) {
326                 printk(KERN_ERR "Could not allocate page array\n");
327                 return;
328         }
329
330         cxt->mtd = mtd;
331         cxt->oops_pages = (int)mtd->size / OOPS_PAGE_SIZE;
332         find_next_position(cxt);
333         printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
334 }
335
336 static void mtdoops_notify_remove(struct mtd_info *mtd)
337 {
338         struct mtdoops_context *cxt = &oops_cxt;
339
340         if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
341                 return;
342
343         cxt->mtd = NULL;
344         flush_scheduled_work();
345 }
346
347 static void mtdoops_console_sync(void)
348 {
349         struct mtdoops_context *cxt = &oops_cxt;
350         struct mtd_info *mtd = cxt->mtd;
351         unsigned long flags;
352
353         if (!cxt->ready || !mtd || cxt->writecount == 0)
354                 return;
355
356         /*
357          *  Once ready is 0 and we've held the lock no further writes to the
358          *  buffer will happen
359          */
360         spin_lock_irqsave(&cxt->writecount_lock, flags);
361         if (!cxt->ready) {
362                 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
363                 return;
364         }
365         cxt->ready = 0;
366         spin_unlock_irqrestore(&cxt->writecount_lock, flags);
367
368         if (mtd->panic_write && in_interrupt())
369                 /* Interrupt context, we're going to panic so try and log */
370                 mtdoops_write(cxt, 1);
371         else
372                 schedule_work(&cxt->work_write);
373 }
374
375 static void
376 mtdoops_console_write(struct console *co, const char *s, unsigned int count)
377 {
378         struct mtdoops_context *cxt = co->data;
379         struct mtd_info *mtd = cxt->mtd;
380         unsigned long flags;
381
382         if (!oops_in_progress) {
383                 mtdoops_console_sync();
384                 return;
385         }
386
387         if (!cxt->ready || !mtd)
388                 return;
389
390         /* Locking on writecount ensures sequential writes to the buffer */
391         spin_lock_irqsave(&cxt->writecount_lock, flags);
392
393         /* Check ready status didn't change whilst waiting for the lock */
394         if (!cxt->ready) {
395                 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
396                 return;
397         }
398
399         if (cxt->writecount == 0) {
400                 u32 *stamp = cxt->oops_buf;
401                 *stamp++ = cxt->nextcount;
402                 *stamp = MTDOOPS_KERNMSG_MAGIC;
403                 cxt->writecount = 8;
404         }
405
406         if (count + cxt->writecount > OOPS_PAGE_SIZE)
407                 count = OOPS_PAGE_SIZE - cxt->writecount;
408
409         memcpy(cxt->oops_buf + cxt->writecount, s, count);
410         cxt->writecount += count;
411
412         spin_unlock_irqrestore(&cxt->writecount_lock, flags);
413
414         if (cxt->writecount == OOPS_PAGE_SIZE)
415                 mtdoops_console_sync();
416 }
417
418 static int __init mtdoops_console_setup(struct console *co, char *options)
419 {
420         struct mtdoops_context *cxt = co->data;
421
422         if (cxt->mtd_index != -1 || cxt->name)
423                 return -EBUSY;
424         if (options) {
425                 cxt->name = kstrdup(options, GFP_KERNEL);
426                 return 0;
427         }
428         if (co->index == -1)
429                 return -EINVAL;
430
431         cxt->mtd_index = co->index;
432         return 0;
433 }
434
435 static struct mtd_notifier mtdoops_notifier = {
436         .add    = mtdoops_notify_add,
437         .remove = mtdoops_notify_remove,
438 };
439
440 static struct console mtdoops_console = {
441         .name           = "ttyMTD",
442         .write          = mtdoops_console_write,
443         .setup          = mtdoops_console_setup,
444         .unblank        = mtdoops_console_sync,
445         .index          = -1,
446         .data           = &oops_cxt,
447 };
448
449 static int __init mtdoops_console_init(void)
450 {
451         struct mtdoops_context *cxt = &oops_cxt;
452
453         cxt->mtd_index = -1;
454         cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE);
455         if (!cxt->oops_buf) {
456                 printk(KERN_ERR "mtdoops: failed to allocate buffer workspace\n");
457                 return -ENOMEM;
458         }
459
460         spin_lock_init(&cxt->writecount_lock);
461         INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
462         INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
463
464         register_console(&mtdoops_console);
465         register_mtd_user(&mtdoops_notifier);
466         return 0;
467 }
468
469 static void __exit mtdoops_console_exit(void)
470 {
471         struct mtdoops_context *cxt = &oops_cxt;
472
473         unregister_mtd_user(&mtdoops_notifier);
474         unregister_console(&mtdoops_console);
475         kfree(cxt->name);
476         vfree(cxt->oops_buf);
477         vfree(cxt->oops_page_used);
478 }
479
480
481 subsys_initcall(mtdoops_console_init);
482 module_exit(mtdoops_console_exit);
483
484 MODULE_LICENSE("GPL");
485 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
486 MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");