2 * MTD Oops/Panic logger
4 * Copyright (C) 2007 Nokia Corporation. All rights reserved.
6 * Author: Richard Purdie <rpurdie@openedhand.com>
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.
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.
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
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>
36 /* Maximum MTD partition size */
37 #define MTDOOPS_MAX_MTD_SIZE (8 * 1024 * 1024)
39 #define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
40 #define OOPS_PAGE_SIZE 4096
42 static struct mtdoops_context {
44 struct work_struct work_erase;
45 struct work_struct work_write;
50 unsigned long *oops_page_used;
55 /* writecount and disabling ready are spin lock protected */
56 spinlock_t writecount_lock;
61 static void mark_page_used(struct mtdoops_context *cxt, int page)
63 set_bit(page, cxt->oops_page_used);
66 static void mark_page_unused(struct mtdoops_context *cxt, int page)
68 clear_bit(page, cxt->oops_page_used);
71 static int page_is_used(struct mtdoops_context *cxt, int page)
73 return test_bit(page, cxt->oops_page_used);
76 static void mtdoops_erase_callback(struct erase_info *done)
78 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
82 static int mtdoops_erase_block(struct mtdoops_context *cxt, int offset)
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;
94 init_waitqueue_head(&wait_q);
96 erase.callback = mtdoops_erase_callback;
98 erase.len = mtd->erasesize;
99 erase.priv = (u_long)&wait_q;
101 set_current_state(TASK_INTERRUPTIBLE);
102 add_wait_queue(&wait_q, &wait);
104 ret = mtd->erase(mtd, &erase);
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);
114 schedule(); /* Wait for erase to finish. */
115 remove_wait_queue(&wait_q, &wait);
117 /* Mark pages as unused */
118 for (page = start_page; page < start_page + erase_pages; page++)
119 mark_page_unused(cxt, page);
124 static void mtdoops_inc_counter(struct mtdoops_context *cxt)
127 if (cxt->nextpage >= cxt->oops_pages)
130 if (cxt->nextcount == 0xffffffff)
133 if (page_is_used(cxt, cxt->nextpage)) {
134 schedule_work(&cxt->work_erase);
138 printk(KERN_DEBUG "mtdoops: ready %d, %d (no erase)\n",
139 cxt->nextpage, cxt->nextcount);
143 /* Scheduled work - when we can't proceed without erasing a block */
144 static void mtdoops_workfunc_erase(struct work_struct *work)
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;
151 /* We were unregistered */
155 mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize;
157 cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE);
158 if (cxt->nextpage >= cxt->oops_pages)
162 while (mtd->block_isbad) {
163 ret = mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
167 printk(KERN_ERR "mtdoops: block_isbad failed, aborting\n");
171 printk(KERN_WARNING "mtdoops: bad block at %08x\n",
172 cxt->nextpage * OOPS_PAGE_SIZE);
174 cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE);
175 if (cxt->nextpage >= cxt->oops_pages)
177 if (i == cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE)) {
178 printk(KERN_ERR "mtdoops: all blocks bad!\n");
183 for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
184 ret = mtdoops_erase_block(cxt, cxt->nextpage * OOPS_PAGE_SIZE);
187 printk(KERN_DEBUG "mtdoops: ready %d, %d\n",
188 cxt->nextpage, cxt->nextcount);
193 if (mtd->block_markbad && ret == -EIO) {
194 ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
196 printk(KERN_ERR "mtdoops: block_markbad failed, aborting\n");
203 static void mtdoops_write(struct mtdoops_context *cxt, int panic)
205 struct mtd_info *mtd = cxt->mtd;
209 if (cxt->writecount < OOPS_PAGE_SIZE)
210 memset(cxt->oops_buf + cxt->writecount, 0xff,
211 OOPS_PAGE_SIZE - cxt->writecount);
214 ret = mtd->panic_write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
215 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
217 ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
218 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
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);
227 mtdoops_inc_counter(cxt);
231 static void mtdoops_workfunc_write(struct work_struct *work)
233 struct mtdoops_context *cxt =
234 container_of(work, struct mtdoops_context, work_write);
236 mtdoops_write(cxt, 0);
239 static void find_next_position(struct mtdoops_context *cxt)
241 struct mtd_info *mtd = cxt->mtd;
242 int ret, page, maxpos = 0;
243 u32 count[2], maxcount = 0xffffffff;
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);
256 if (count[0] == 0xffffffff && count[1] == 0xffffffff)
257 mark_page_unused(cxt, page);
258 if (count[1] != MTDOOPS_KERNMSG_MAGIC)
260 if (count[0] == 0xffffffff)
262 if (maxcount == 0xffffffff) {
265 } else if (count[0] < 0x40000000 && maxcount > 0xc0000000) {
268 } else if (count[0] > maxcount && count[0] < 0xc0000000) {
271 } else if (count[0] > maxcount && count[0] > 0xc0000000
272 && maxcount > 0x80000000) {
277 if (maxcount == 0xffffffff) {
280 schedule_work(&cxt->work_erase);
284 cxt->nextpage = maxpos;
285 cxt->nextcount = maxcount;
287 mtdoops_inc_counter(cxt);
291 static void mtdoops_notify_add(struct mtd_info *mtd)
293 struct mtdoops_context *cxt = &oops_cxt;
294 u64 mtdoops_pages = mtd->size;
296 do_div(mtdoops_pages, OOPS_PAGE_SIZE);
298 if (cxt->name && !strcmp(mtd->name, cxt->name))
299 cxt->mtd_index = mtd->index;
301 if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
304 if (mtd->size < mtd->erasesize * 2) {
305 printk(KERN_ERR "mtdoops: MTD partition %d not big enough for mtdoops\n",
310 if (mtd->erasesize < OOPS_PAGE_SIZE) {
311 printk(KERN_ERR "mtdoops: eraseblock size of MTD partition %d too small\n",
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);
322 /* oops_page_used is a bit field */
323 cxt->oops_page_used = vmalloc(DIV_ROUND_UP(mtdoops_pages,
325 if (!cxt->oops_page_used) {
326 printk(KERN_ERR "Could not allocate page array\n");
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);
336 static void mtdoops_notify_remove(struct mtd_info *mtd)
338 struct mtdoops_context *cxt = &oops_cxt;
340 if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
344 flush_scheduled_work();
347 static void mtdoops_console_sync(void)
349 struct mtdoops_context *cxt = &oops_cxt;
350 struct mtd_info *mtd = cxt->mtd;
353 if (!cxt->ready || !mtd || cxt->writecount == 0)
357 * Once ready is 0 and we've held the lock no further writes to the
360 spin_lock_irqsave(&cxt->writecount_lock, flags);
362 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
366 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
368 if (mtd->panic_write && in_interrupt())
369 /* Interrupt context, we're going to panic so try and log */
370 mtdoops_write(cxt, 1);
372 schedule_work(&cxt->work_write);
376 mtdoops_console_write(struct console *co, const char *s, unsigned int count)
378 struct mtdoops_context *cxt = co->data;
379 struct mtd_info *mtd = cxt->mtd;
382 if (!oops_in_progress) {
383 mtdoops_console_sync();
387 if (!cxt->ready || !mtd)
390 /* Locking on writecount ensures sequential writes to the buffer */
391 spin_lock_irqsave(&cxt->writecount_lock, flags);
393 /* Check ready status didn't change whilst waiting for the lock */
395 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
399 if (cxt->writecount == 0) {
400 u32 *stamp = cxt->oops_buf;
401 *stamp++ = cxt->nextcount;
402 *stamp = MTDOOPS_KERNMSG_MAGIC;
406 if (count + cxt->writecount > OOPS_PAGE_SIZE)
407 count = OOPS_PAGE_SIZE - cxt->writecount;
409 memcpy(cxt->oops_buf + cxt->writecount, s, count);
410 cxt->writecount += count;
412 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
414 if (cxt->writecount == OOPS_PAGE_SIZE)
415 mtdoops_console_sync();
418 static int __init mtdoops_console_setup(struct console *co, char *options)
420 struct mtdoops_context *cxt = co->data;
422 if (cxt->mtd_index != -1 || cxt->name)
425 cxt->name = kstrdup(options, GFP_KERNEL);
431 cxt->mtd_index = co->index;
435 static struct mtd_notifier mtdoops_notifier = {
436 .add = mtdoops_notify_add,
437 .remove = mtdoops_notify_remove,
440 static struct console mtdoops_console = {
442 .write = mtdoops_console_write,
443 .setup = mtdoops_console_setup,
444 .unblank = mtdoops_console_sync,
449 static int __init mtdoops_console_init(void)
451 struct mtdoops_context *cxt = &oops_cxt;
454 cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE);
455 if (!cxt->oops_buf) {
456 printk(KERN_ERR "mtdoops: failed to allocate buffer workspace\n");
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);
464 register_console(&mtdoops_console);
465 register_mtd_user(&mtdoops_notifier);
469 static void __exit mtdoops_console_exit(void)
471 struct mtdoops_context *cxt = &oops_cxt;
473 unregister_mtd_user(&mtdoops_notifier);
474 unregister_console(&mtdoops_console);
476 vfree(cxt->oops_buf);
477 vfree(cxt->oops_page_used);
481 subsys_initcall(mtdoops_console_init);
482 module_exit(mtdoops_console_exit);
484 MODULE_LICENSE("GPL");
485 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
486 MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");