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;
50 /* writecount and disabling ready are spin lock protected */
51 spinlock_t writecount_lock;
56 static void mtdoops_erase_callback(struct erase_info *done)
58 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
62 static int mtdoops_erase_block(struct mtd_info *mtd, int offset)
64 struct erase_info erase;
65 DECLARE_WAITQUEUE(wait, current);
66 wait_queue_head_t wait_q;
69 init_waitqueue_head(&wait_q);
71 erase.callback = mtdoops_erase_callback;
73 erase.len = mtd->erasesize;
74 erase.priv = (u_long)&wait_q;
76 set_current_state(TASK_INTERRUPTIBLE);
77 add_wait_queue(&wait_q, &wait);
79 ret = mtd->erase(mtd, &erase);
81 set_current_state(TASK_RUNNING);
82 remove_wait_queue(&wait_q, &wait);
83 printk (KERN_WARNING "mtdoops: erase of region [0x%llx, 0x%llx] "
85 (unsigned long long)erase.addr, (unsigned long long)erase.len, mtd->name);
89 schedule(); /* Wait for erase to finish. */
90 remove_wait_queue(&wait_q, &wait);
95 static void mtdoops_inc_counter(struct mtdoops_context *cxt)
97 struct mtd_info *mtd = cxt->mtd;
103 if (cxt->nextpage >= cxt->oops_pages)
106 if (cxt->nextcount == 0xffffffff)
109 ret = mtd->read(mtd, cxt->nextpage * OOPS_PAGE_SIZE, 4,
110 &retlen, (u_char *) &count);
111 if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) {
112 printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)"
113 ", err %d.\n", cxt->nextpage * OOPS_PAGE_SIZE,
115 schedule_work(&cxt->work_erase);
119 /* See if we need to erase the next block */
120 if (count != 0xffffffff) {
121 schedule_work(&cxt->work_erase);
125 printk(KERN_DEBUG "mtdoops: Ready %d, %d (no erase)\n",
126 cxt->nextpage, cxt->nextcount);
130 /* Scheduled work - when we can't proceed without erasing a block */
131 static void mtdoops_workfunc_erase(struct work_struct *work)
133 struct mtdoops_context *cxt =
134 container_of(work, struct mtdoops_context, work_erase);
135 struct mtd_info *mtd = cxt->mtd;
136 int i = 0, j, ret, mod;
138 /* We were unregistered */
142 mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize;
144 cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE);
145 if (cxt->nextpage >= cxt->oops_pages)
149 while (mtd->block_isbad) {
150 ret = mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
154 printk(KERN_ERR "mtdoops: block_isbad failed, aborting.\n");
158 printk(KERN_WARNING "mtdoops: Bad block at %08x\n",
159 cxt->nextpage * OOPS_PAGE_SIZE);
161 cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE);
162 if (cxt->nextpage >= cxt->oops_pages)
164 if (i == (cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE))) {
165 printk(KERN_ERR "mtdoops: All blocks bad!\n");
170 for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
171 ret = mtdoops_erase_block(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
174 printk(KERN_DEBUG "mtdoops: Ready %d, %d \n", cxt->nextpage, cxt->nextcount);
179 if (mtd->block_markbad && (ret == -EIO)) {
180 ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
182 printk(KERN_ERR "mtdoops: block_markbad failed, aborting.\n");
189 static void mtdoops_write(struct mtdoops_context *cxt, int panic)
191 struct mtd_info *mtd = cxt->mtd;
195 if (cxt->writecount < OOPS_PAGE_SIZE)
196 memset(cxt->oops_buf + cxt->writecount, 0xff,
197 OOPS_PAGE_SIZE - cxt->writecount);
200 ret = mtd->panic_write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
201 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
203 ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
204 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
208 if ((retlen != OOPS_PAGE_SIZE) || (ret < 0))
209 printk(KERN_ERR "mtdoops: Write failure at %d (%td of %d written), err %d.\n",
210 cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret);
212 mtdoops_inc_counter(cxt);
216 static void mtdoops_workfunc_write(struct work_struct *work)
218 struct mtdoops_context *cxt =
219 container_of(work, struct mtdoops_context, work_write);
221 mtdoops_write(cxt, 0);
224 static void find_next_position(struct mtdoops_context *cxt)
226 struct mtd_info *mtd = cxt->mtd;
227 int ret, page, maxpos = 0;
228 u32 count[2], maxcount = 0xffffffff;
231 for (page = 0; page < cxt->oops_pages; page++) {
232 ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 8, &retlen, (u_char *) &count[0]);
233 if ((retlen != 8) || ((ret < 0) && (ret != -EUCLEAN))) {
234 printk(KERN_ERR "mtdoops: Read failure at %d (%td of 8 read)"
235 ", err %d.\n", page * OOPS_PAGE_SIZE, retlen, ret);
239 if (count[1] != MTDOOPS_KERNMSG_MAGIC)
241 if (count[0] == 0xffffffff)
243 if (maxcount == 0xffffffff) {
246 } else if ((count[0] < 0x40000000) && (maxcount > 0xc0000000)) {
249 } else if ((count[0] > maxcount) && (count[0] < 0xc0000000)) {
252 } else if ((count[0] > maxcount) && (count[0] > 0xc0000000)
253 && (maxcount > 0x80000000)) {
258 if (maxcount == 0xffffffff) {
261 schedule_work(&cxt->work_erase);
265 cxt->nextpage = maxpos;
266 cxt->nextcount = maxcount;
268 mtdoops_inc_counter(cxt);
272 static void mtdoops_notify_add(struct mtd_info *mtd)
274 struct mtdoops_context *cxt = &oops_cxt;
276 if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
279 if (mtd->size < (mtd->erasesize * 2)) {
280 printk(KERN_ERR "MTD partition %d not big enough for mtdoops\n",
285 if (mtd->erasesize < OOPS_PAGE_SIZE) {
286 printk(KERN_ERR "Eraseblock size of MTD partition %d too small\n",
292 if (mtd->size > INT_MAX)
293 cxt->oops_pages = INT_MAX / OOPS_PAGE_SIZE;
295 cxt->oops_pages = (int)mtd->size / OOPS_PAGE_SIZE;
297 find_next_position(cxt);
299 printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
302 static void mtdoops_notify_remove(struct mtd_info *mtd)
304 struct mtdoops_context *cxt = &oops_cxt;
306 if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
310 flush_scheduled_work();
313 static void mtdoops_console_sync(void)
315 struct mtdoops_context *cxt = &oops_cxt;
316 struct mtd_info *mtd = cxt->mtd;
319 if (!cxt->ready || !mtd || cxt->writecount == 0)
323 * Once ready is 0 and we've held the lock no further writes to the
326 spin_lock_irqsave(&cxt->writecount_lock, flags);
328 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
332 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
334 if (mtd->panic_write && in_interrupt())
335 /* Interrupt context, we're going to panic so try and log */
336 mtdoops_write(cxt, 1);
338 schedule_work(&cxt->work_write);
342 mtdoops_console_write(struct console *co, const char *s, unsigned int count)
344 struct mtdoops_context *cxt = co->data;
345 struct mtd_info *mtd = cxt->mtd;
348 if (!oops_in_progress) {
349 mtdoops_console_sync();
353 if (!cxt->ready || !mtd)
356 /* Locking on writecount ensures sequential writes to the buffer */
357 spin_lock_irqsave(&cxt->writecount_lock, flags);
359 /* Check ready status didn't change whilst waiting for the lock */
363 if (cxt->writecount == 0) {
364 u32 *stamp = cxt->oops_buf;
365 *stamp++ = cxt->nextcount;
366 *stamp = MTDOOPS_KERNMSG_MAGIC;
370 if ((count + cxt->writecount) > OOPS_PAGE_SIZE)
371 count = OOPS_PAGE_SIZE - cxt->writecount;
373 memcpy(cxt->oops_buf + cxt->writecount, s, count);
374 cxt->writecount += count;
376 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
378 if (cxt->writecount == OOPS_PAGE_SIZE)
379 mtdoops_console_sync();
382 static int __init mtdoops_console_setup(struct console *co, char *options)
384 struct mtdoops_context *cxt = co->data;
386 if (cxt->mtd_index != -1)
391 cxt->mtd_index = co->index;
395 static struct mtd_notifier mtdoops_notifier = {
396 .add = mtdoops_notify_add,
397 .remove = mtdoops_notify_remove,
400 static struct console mtdoops_console = {
402 .write = mtdoops_console_write,
403 .setup = mtdoops_console_setup,
404 .unblank = mtdoops_console_sync,
409 static int __init mtdoops_console_init(void)
411 struct mtdoops_context *cxt = &oops_cxt;
414 cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE);
416 if (!cxt->oops_buf) {
417 printk(KERN_ERR "Failed to allocate mtdoops buffer workspace\n");
421 INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
422 INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
424 register_console(&mtdoops_console);
425 register_mtd_user(&mtdoops_notifier);
429 static void __exit mtdoops_console_exit(void)
431 struct mtdoops_context *cxt = &oops_cxt;
433 unregister_mtd_user(&mtdoops_notifier);
434 unregister_console(&mtdoops_console);
435 vfree(cxt->oops_buf);
439 subsys_initcall(mtdoops_console_init);
440 module_exit(mtdoops_console_exit);
442 MODULE_LICENSE("GPL");
443 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
444 MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");