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/mtd/mtd.h>
35 #define OOPS_PAGE_SIZE 4096
37 struct mtdoops_context {
39 struct work_struct work_erase;
40 struct work_struct work_write;
48 /* writecount and disabling ready are spin lock protected */
49 spinlock_t writecount_lock;
54 static void mtdoops_erase_callback(struct erase_info *done)
56 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
60 static int mtdoops_erase_block(struct mtd_info *mtd, int offset)
62 struct erase_info erase;
63 DECLARE_WAITQUEUE(wait, current);
64 wait_queue_head_t wait_q;
67 init_waitqueue_head(&wait_q);
69 erase.callback = mtdoops_erase_callback;
71 erase.len = mtd->erasesize;
72 erase.priv = (u_long)&wait_q;
74 set_current_state(TASK_INTERRUPTIBLE);
75 add_wait_queue(&wait_q, &wait);
77 ret = mtd->erase(mtd, &erase);
79 set_current_state(TASK_RUNNING);
80 remove_wait_queue(&wait_q, &wait);
81 printk (KERN_WARNING "mtdoops: erase of region [0x%x, 0x%x] "
83 erase.addr, erase.len, mtd->name);
87 schedule(); /* Wait for erase to finish. */
88 remove_wait_queue(&wait_q, &wait);
93 static void mtdoops_inc_counter(struct mtdoops_context *cxt)
95 struct mtd_info *mtd = cxt->mtd;
101 if (cxt->nextpage > cxt->oops_pages)
104 if (cxt->nextcount == 0xffffffff)
107 ret = mtd->read(mtd, cxt->nextpage * OOPS_PAGE_SIZE, 4,
108 &retlen, (u_char *) &count);
109 if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) {
110 printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)"
111 ", err %d.\n", cxt->nextpage * OOPS_PAGE_SIZE,
113 schedule_work(&cxt->work_erase);
117 /* See if we need to erase the next block */
118 if (count != 0xffffffff) {
119 schedule_work(&cxt->work_erase);
123 printk(KERN_DEBUG "mtdoops: Ready %d, %d (no erase)\n",
124 cxt->nextpage, cxt->nextcount);
128 /* Scheduled work - when we can't proceed without erasing a block */
129 static void mtdoops_workfunc_erase(struct work_struct *work)
131 struct mtdoops_context *cxt =
132 container_of(work, struct mtdoops_context, work_erase);
133 struct mtd_info *mtd = cxt->mtd;
134 int i = 0, j, ret, mod;
136 /* We were unregistered */
140 mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize;
142 cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE);
143 if (cxt->nextpage > cxt->oops_pages)
147 while (mtd->block_isbad) {
148 ret = mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
152 printk(KERN_ERR "mtdoops: block_isbad failed, aborting.\n");
156 printk(KERN_WARNING "mtdoops: Bad block at %08x\n",
157 cxt->nextpage * OOPS_PAGE_SIZE);
159 cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE);
160 if (cxt->nextpage > cxt->oops_pages)
162 if (i == (cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE))) {
163 printk(KERN_ERR "mtdoops: All blocks bad!\n");
168 for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
169 ret = mtdoops_erase_block(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
172 printk(KERN_DEBUG "mtdoops: Ready %d, %d \n", cxt->nextpage, cxt->nextcount);
177 if (mtd->block_markbad && (ret == -EIO)) {
178 ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
180 printk(KERN_ERR "mtdoops: block_markbad failed, aborting.\n");
187 static void mtdoops_write(struct mtdoops_context *cxt, int panic)
189 struct mtd_info *mtd = cxt->mtd;
193 if (cxt->writecount < OOPS_PAGE_SIZE)
194 memset(cxt->oops_buf + cxt->writecount, 0xff,
195 OOPS_PAGE_SIZE - cxt->writecount);
198 ret = mtd->panic_write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
199 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
201 ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
202 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
206 if ((retlen != OOPS_PAGE_SIZE) || (ret < 0))
207 printk(KERN_ERR "mtdoops: Write failure at %d (%td of %d written), err %d.\n",
208 cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret);
210 mtdoops_inc_counter(cxt);
214 static void mtdoops_workfunc_write(struct work_struct *work)
216 struct mtdoops_context *cxt =
217 container_of(work, struct mtdoops_context, work_write);
219 mtdoops_write(cxt, 0);
222 static void find_next_position(struct mtdoops_context *cxt)
224 struct mtd_info *mtd = cxt->mtd;
225 int ret, page, maxpos = 0;
226 u32 count, maxcount = 0xffffffff;
229 for (page = 0; page < cxt->oops_pages; page++) {
230 ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 4, &retlen, (u_char *) &count);
231 if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) {
232 printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)"
233 ", err %d.\n", page * OOPS_PAGE_SIZE, retlen, ret);
237 if (count == 0xffffffff)
239 if (maxcount == 0xffffffff) {
242 } else if ((count < 0x40000000) && (maxcount > 0xc0000000)) {
245 } else if ((count > maxcount) && (count < 0xc0000000)) {
248 } else if ((count > maxcount) && (count > 0xc0000000)
249 && (maxcount > 0x80000000)) {
254 if (maxcount == 0xffffffff) {
258 printk(KERN_DEBUG "mtdoops: Ready %d, %d (first init)\n",
259 cxt->nextpage, cxt->nextcount);
263 cxt->nextpage = maxpos;
264 cxt->nextcount = maxcount;
266 mtdoops_inc_counter(cxt);
270 static void mtdoops_notify_add(struct mtd_info *mtd)
272 struct mtdoops_context *cxt = &oops_cxt;
274 if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
277 if (mtd->size < (mtd->erasesize * 2)) {
278 printk(KERN_ERR "MTD partition %d not big enough for mtdoops\n",
283 if (mtd->erasesize < OOPS_PAGE_SIZE) {
284 printk(KERN_ERR "Eraseblock size of MTD partition %d too small\n",
290 cxt->oops_pages = mtd->size / OOPS_PAGE_SIZE;
292 find_next_position(cxt);
294 printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
297 static void mtdoops_notify_remove(struct mtd_info *mtd)
299 struct mtdoops_context *cxt = &oops_cxt;
301 if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
305 flush_scheduled_work();
308 static void mtdoops_console_sync(void)
310 struct mtdoops_context *cxt = &oops_cxt;
311 struct mtd_info *mtd = cxt->mtd;
314 if (!cxt->ready || !mtd || cxt->writecount == 0)
318 * Once ready is 0 and we've held the lock no further writes to the
321 spin_lock_irqsave(&cxt->writecount_lock, flags);
323 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
327 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
329 if (mtd->panic_write && in_interrupt())
330 /* Interrupt context, we're going to panic so try and log */
331 mtdoops_write(cxt, 1);
333 schedule_work(&cxt->work_write);
337 mtdoops_console_write(struct console *co, const char *s, unsigned int count)
339 struct mtdoops_context *cxt = co->data;
340 struct mtd_info *mtd = cxt->mtd;
343 if (!oops_in_progress) {
344 mtdoops_console_sync();
348 if (!cxt->ready || !mtd)
351 /* Locking on writecount ensures sequential writes to the buffer */
352 spin_lock_irqsave(&cxt->writecount_lock, flags);
354 /* Check ready status didn't change whilst waiting for the lock */
358 if (cxt->writecount == 0) {
359 u32 *stamp = cxt->oops_buf;
360 *stamp = cxt->nextcount;
364 if ((count + cxt->writecount) > OOPS_PAGE_SIZE)
365 count = OOPS_PAGE_SIZE - cxt->writecount;
367 memcpy(cxt->oops_buf + cxt->writecount, s, count);
368 cxt->writecount += count;
370 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
372 if (cxt->writecount == OOPS_PAGE_SIZE)
373 mtdoops_console_sync();
376 static int __init mtdoops_console_setup(struct console *co, char *options)
378 struct mtdoops_context *cxt = co->data;
380 if (cxt->mtd_index != -1)
385 cxt->mtd_index = co->index;
389 static struct mtd_notifier mtdoops_notifier = {
390 .add = mtdoops_notify_add,
391 .remove = mtdoops_notify_remove,
394 static struct console mtdoops_console = {
396 .write = mtdoops_console_write,
397 .setup = mtdoops_console_setup,
398 .unblank = mtdoops_console_sync,
403 static int __init mtdoops_console_init(void)
405 struct mtdoops_context *cxt = &oops_cxt;
408 cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE);
410 if (!cxt->oops_buf) {
411 printk(KERN_ERR "Failed to allocate mtdoops buffer workspace\n");
415 INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
416 INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
418 register_console(&mtdoops_console);
419 register_mtd_user(&mtdoops_notifier);
423 static void __exit mtdoops_console_exit(void)
425 struct mtdoops_context *cxt = &oops_cxt;
427 unregister_mtd_user(&mtdoops_notifier);
428 unregister_console(&mtdoops_console);
429 vfree(cxt->oops_buf);
433 subsys_initcall(mtdoops_console_init);
434 module_exit(mtdoops_console_exit);
436 MODULE_LICENSE("GPL");
437 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
438 MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");