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 #define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
37 #define OOPS_PAGE_SIZE 4096
39 static struct mtdoops_context {
41 struct work_struct work_erase;
42 struct work_struct work_write;
47 unsigned long *oops_page_used;
52 /* writecount and disabling ready are spin lock protected */
53 spinlock_t writecount_lock;
58 static void mark_page_used(struct mtdoops_context *cxt, int page)
60 set_bit(page, cxt->oops_page_used);
63 static void mark_page_unused(struct mtdoops_context *cxt, int page)
65 clear_bit(page, cxt->oops_page_used);
68 static int page_is_used(struct mtdoops_context *cxt, int page)
70 return test_bit(page, cxt->oops_page_used);
73 static void mtdoops_erase_callback(struct erase_info *done)
75 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
79 static int mtdoops_erase_block(struct mtdoops_context *cxt, int offset)
81 struct mtd_info *mtd = cxt->mtd;
82 u32 start_page_offset = mtd_div_by_eb(offset, mtd) * mtd->erasesize;
83 u32 start_page = start_page_offset / OOPS_PAGE_SIZE;
84 u32 erase_pages = mtd->erasesize / OOPS_PAGE_SIZE;
85 struct erase_info erase;
86 DECLARE_WAITQUEUE(wait, current);
87 wait_queue_head_t wait_q;
91 init_waitqueue_head(&wait_q);
93 erase.callback = mtdoops_erase_callback;
95 erase.len = mtd->erasesize;
96 erase.priv = (u_long)&wait_q;
98 set_current_state(TASK_INTERRUPTIBLE);
99 add_wait_queue(&wait_q, &wait);
101 ret = mtd->erase(mtd, &erase);
103 set_current_state(TASK_RUNNING);
104 remove_wait_queue(&wait_q, &wait);
105 printk(KERN_WARNING "mtdoops: erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
106 (unsigned long long)erase.addr,
107 (unsigned long long)erase.len, mtd->name);
111 schedule(); /* Wait for erase to finish. */
112 remove_wait_queue(&wait_q, &wait);
114 /* Mark pages as unused */
115 for (page = start_page; page < start_page + erase_pages; page++)
116 mark_page_unused(cxt, page);
121 static void mtdoops_inc_counter(struct mtdoops_context *cxt)
124 if (cxt->nextpage >= cxt->oops_pages)
127 if (cxt->nextcount == 0xffffffff)
130 if (page_is_used(cxt, cxt->nextpage)) {
131 schedule_work(&cxt->work_erase);
135 printk(KERN_DEBUG "mtdoops: ready %d, %d (no erase)\n",
136 cxt->nextpage, cxt->nextcount);
140 /* Scheduled work - when we can't proceed without erasing a block */
141 static void mtdoops_workfunc_erase(struct work_struct *work)
143 struct mtdoops_context *cxt =
144 container_of(work, struct mtdoops_context, work_erase);
145 struct mtd_info *mtd = cxt->mtd;
146 int i = 0, j, ret, mod;
148 /* We were unregistered */
152 mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize;
154 cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE);
155 if (cxt->nextpage >= cxt->oops_pages)
159 while (mtd->block_isbad) {
160 ret = mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
164 printk(KERN_ERR "mtdoops: block_isbad failed, aborting\n");
168 printk(KERN_WARNING "mtdoops: bad block at %08x\n",
169 cxt->nextpage * OOPS_PAGE_SIZE);
171 cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE);
172 if (cxt->nextpage >= cxt->oops_pages)
174 if (i == cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE)) {
175 printk(KERN_ERR "mtdoops: all blocks bad!\n");
180 for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
181 ret = mtdoops_erase_block(cxt, cxt->nextpage * OOPS_PAGE_SIZE);
184 printk(KERN_DEBUG "mtdoops: ready %d, %d\n",
185 cxt->nextpage, cxt->nextcount);
190 if (mtd->block_markbad && ret == -EIO) {
191 ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
193 printk(KERN_ERR "mtdoops: block_markbad failed, aborting\n");
200 static void mtdoops_write(struct mtdoops_context *cxt, int panic)
202 struct mtd_info *mtd = cxt->mtd;
206 if (cxt->writecount < OOPS_PAGE_SIZE)
207 memset(cxt->oops_buf + cxt->writecount, 0xff,
208 OOPS_PAGE_SIZE - cxt->writecount);
211 ret = mtd->panic_write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
212 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
214 ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
215 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
219 if (retlen != OOPS_PAGE_SIZE || ret < 0)
220 printk(KERN_ERR "mtdoops: write failure at %d (%td of %d written), error %d\n",
221 cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret);
222 mark_page_used(cxt, cxt->nextpage);
224 mtdoops_inc_counter(cxt);
228 static void mtdoops_workfunc_write(struct work_struct *work)
230 struct mtdoops_context *cxt =
231 container_of(work, struct mtdoops_context, work_write);
233 mtdoops_write(cxt, 0);
236 static void find_next_position(struct mtdoops_context *cxt)
238 struct mtd_info *mtd = cxt->mtd;
239 int ret, page, maxpos = 0;
240 u32 count[2], maxcount = 0xffffffff;
243 for (page = 0; page < cxt->oops_pages; page++) {
244 /* Assume the page is used */
245 mark_page_used(cxt, page);
246 ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 8, &retlen, (u_char *) &count[0]);
247 if (retlen != 8 || (ret < 0 && ret != -EUCLEAN)) {
248 printk(KERN_ERR "mtdoops: read failure at %d (%td of 8 read), err %d\n",
249 page * OOPS_PAGE_SIZE, retlen, ret);
253 if (count[0] == 0xffffffff && count[1] == 0xffffffff)
254 mark_page_unused(cxt, page);
255 if (count[1] != MTDOOPS_KERNMSG_MAGIC)
257 if (count[0] == 0xffffffff)
259 if (maxcount == 0xffffffff) {
262 } else if (count[0] < 0x40000000 && maxcount > 0xc0000000) {
265 } else if (count[0] > maxcount && count[0] < 0xc0000000) {
268 } else if (count[0] > maxcount && count[0] > 0xc0000000
269 && maxcount > 0x80000000) {
274 if (maxcount == 0xffffffff) {
277 schedule_work(&cxt->work_erase);
281 cxt->nextpage = maxpos;
282 cxt->nextcount = maxcount;
284 mtdoops_inc_counter(cxt);
288 static void mtdoops_notify_add(struct mtd_info *mtd)
290 struct mtdoops_context *cxt = &oops_cxt;
291 u64 mtdoops_pages = mtd->size;
293 do_div(mtdoops_pages, OOPS_PAGE_SIZE);
295 if (cxt->name && !strcmp(mtd->name, cxt->name))
296 cxt->mtd_index = mtd->index;
298 if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
301 if (mtd->size < mtd->erasesize * 2) {
302 printk(KERN_ERR "mtdoops: MTD partition %d not big enough for mtdoops\n",
307 if (mtd->erasesize < OOPS_PAGE_SIZE) {
308 printk(KERN_ERR "mtdoops: eraseblock size of MTD partition %d too small\n",
313 /* oops_page_used is a bit field */
314 cxt->oops_page_used = vmalloc(DIV_ROUND_UP(mtdoops_pages,
316 if (!cxt->oops_page_used) {
317 printk(KERN_ERR "Could not allocate page array\n");
321 if (mtd->size > INT_MAX)
322 cxt->oops_pages = INT_MAX / OOPS_PAGE_SIZE;
324 cxt->oops_pages = (int)mtd->size / OOPS_PAGE_SIZE;
326 find_next_position(cxt);
328 printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
331 static void mtdoops_notify_remove(struct mtd_info *mtd)
333 struct mtdoops_context *cxt = &oops_cxt;
335 if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
339 flush_scheduled_work();
342 static void mtdoops_console_sync(void)
344 struct mtdoops_context *cxt = &oops_cxt;
345 struct mtd_info *mtd = cxt->mtd;
348 if (!cxt->ready || !mtd || cxt->writecount == 0)
352 * Once ready is 0 and we've held the lock no further writes to the
355 spin_lock_irqsave(&cxt->writecount_lock, flags);
357 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
361 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
363 if (mtd->panic_write && in_interrupt())
364 /* Interrupt context, we're going to panic so try and log */
365 mtdoops_write(cxt, 1);
367 schedule_work(&cxt->work_write);
371 mtdoops_console_write(struct console *co, const char *s, unsigned int count)
373 struct mtdoops_context *cxt = co->data;
374 struct mtd_info *mtd = cxt->mtd;
377 if (!oops_in_progress) {
378 mtdoops_console_sync();
382 if (!cxt->ready || !mtd)
385 /* Locking on writecount ensures sequential writes to the buffer */
386 spin_lock_irqsave(&cxt->writecount_lock, flags);
388 /* Check ready status didn't change whilst waiting for the lock */
390 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
394 if (cxt->writecount == 0) {
395 u32 *stamp = cxt->oops_buf;
396 *stamp++ = cxt->nextcount;
397 *stamp = MTDOOPS_KERNMSG_MAGIC;
401 if (count + cxt->writecount > OOPS_PAGE_SIZE)
402 count = OOPS_PAGE_SIZE - cxt->writecount;
404 memcpy(cxt->oops_buf + cxt->writecount, s, count);
405 cxt->writecount += count;
407 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
409 if (cxt->writecount == OOPS_PAGE_SIZE)
410 mtdoops_console_sync();
413 static int __init mtdoops_console_setup(struct console *co, char *options)
415 struct mtdoops_context *cxt = co->data;
417 if (cxt->mtd_index != -1 || cxt->name)
420 cxt->name = kstrdup(options, GFP_KERNEL);
426 cxt->mtd_index = co->index;
430 static struct mtd_notifier mtdoops_notifier = {
431 .add = mtdoops_notify_add,
432 .remove = mtdoops_notify_remove,
435 static struct console mtdoops_console = {
437 .write = mtdoops_console_write,
438 .setup = mtdoops_console_setup,
439 .unblank = mtdoops_console_sync,
444 static int __init mtdoops_console_init(void)
446 struct mtdoops_context *cxt = &oops_cxt;
449 cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE);
450 if (!cxt->oops_buf) {
451 printk(KERN_ERR "mtdoops: failed to allocate buffer workspace\n");
455 spin_lock_init(&cxt->writecount_lock);
456 INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
457 INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
459 register_console(&mtdoops_console);
460 register_mtd_user(&mtdoops_notifier);
464 static void __exit mtdoops_console_exit(void)
466 struct mtdoops_context *cxt = &oops_cxt;
468 unregister_mtd_user(&mtdoops_notifier);
469 unregister_console(&mtdoops_console);
471 vfree(cxt->oops_buf);
472 vfree(cxt->oops_page_used);
476 subsys_initcall(mtdoops_console_init);
477 module_exit(mtdoops_console_exit);
479 MODULE_LICENSE("GPL");
480 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
481 MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");