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/mtd/mtd.h>
33 #define OOPS_PAGE_SIZE 4096
35 static struct mtdoops_context
{
37 struct work_struct work
;
48 static void mtdoops_erase_callback(struct erase_info
*done
)
50 wait_queue_head_t
*wait_q
= (wait_queue_head_t
*)done
->priv
;
54 static int mtdoops_erase_block(struct mtd_info
*mtd
, int offset
)
56 struct erase_info erase
;
57 DECLARE_WAITQUEUE(wait
, current
);
58 wait_queue_head_t wait_q
;
61 init_waitqueue_head(&wait_q
);
63 erase
.callback
= mtdoops_erase_callback
;
65 if (mtd
->erasesize
< OOPS_PAGE_SIZE
)
66 erase
.len
= OOPS_PAGE_SIZE
;
68 erase
.len
= mtd
->erasesize
;
69 erase
.priv
= (u_long
)&wait_q
;
71 set_current_state(TASK_INTERRUPTIBLE
);
72 add_wait_queue(&wait_q
, &wait
);
74 ret
= mtd
->erase(mtd
, &erase
);
76 set_current_state(TASK_RUNNING
);
77 remove_wait_queue(&wait_q
, &wait
);
78 printk (KERN_WARNING
"mtdoops: erase of region [0x%x, 0x%x] "
80 erase
.addr
, erase
.len
, mtd
->name
);
84 schedule(); /* Wait for erase to finish. */
85 remove_wait_queue(&wait_q
, &wait
);
90 static int mtdoops_inc_counter(struct mtdoops_context
*cxt
)
92 struct mtd_info
*mtd
= cxt
->mtd
;
98 if (cxt
->nextpage
> cxt
->oops_pages
)
101 if (cxt
->nextcount
== 0xffffffff)
104 ret
= mtd
->read(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
, 4,
105 &retlen
, (u_char
*) &count
);
106 if ((retlen
!= 4) || (ret
< 0)) {
107 printk(KERN_ERR
"mtdoops: Read failure at %d (%td of 4 read)"
108 ", err %d.\n", cxt
->nextpage
* OOPS_PAGE_SIZE
,
113 /* See if we need to erase the next block */
114 if (count
!= 0xffffffff)
117 printk(KERN_DEBUG
"mtdoops: Ready %d, %d (no erase)\n",
118 cxt
->nextpage
, cxt
->nextcount
);
123 static void mtdoops_prepare(struct mtdoops_context
*cxt
)
125 struct mtd_info
*mtd
= cxt
->mtd
;
126 int i
= 0, j
, ret
, mod
;
128 /* We were unregistered */
132 mod
= (cxt
->nextpage
* OOPS_PAGE_SIZE
) % mtd
->erasesize
;
134 cxt
->nextpage
= cxt
->nextpage
+ ((mtd
->erasesize
- mod
) / OOPS_PAGE_SIZE
);
135 if (cxt
->nextpage
> cxt
->oops_pages
)
139 while (mtd
->block_isbad
&&
140 mtd
->block_isbad(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
)) {
142 printk(KERN_WARNING
"mtdoops: Bad block at %08x\n",
143 cxt
->nextpage
* OOPS_PAGE_SIZE
);
145 cxt
->nextpage
= cxt
->nextpage
+ (mtd
->erasesize
/ OOPS_PAGE_SIZE
);
146 if (cxt
->nextpage
> cxt
->oops_pages
)
148 if (i
== (cxt
->oops_pages
/ (mtd
->erasesize
/ OOPS_PAGE_SIZE
))) {
149 printk(KERN_ERR
"mtdoops: All blocks bad!\n");
154 for (j
= 0, ret
= -1; (j
< 3) && (ret
< 0); j
++)
155 ret
= mtdoops_erase_block(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
);
158 if (mtd
->block_markbad
)
159 mtd
->block_markbad(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
);
163 printk(KERN_DEBUG
"mtdoops: Ready %d, %d \n", cxt
->nextpage
, cxt
->nextcount
);
168 static void mtdoops_workfunc(struct work_struct
*work
)
170 struct mtdoops_context
*cxt
=
171 container_of(work
, struct mtdoops_context
, work
);
173 mtdoops_prepare(cxt
);
176 static int find_next_position(struct mtdoops_context
*cxt
)
178 struct mtd_info
*mtd
= cxt
->mtd
;
179 int page
, maxpos
= 0;
180 u32 count
, maxcount
= 0xffffffff;
183 for (page
= 0; page
< cxt
->oops_pages
; page
++) {
184 mtd
->read(mtd
, page
* OOPS_PAGE_SIZE
, 4, &retlen
, (u_char
*) &count
);
185 if (count
== 0xffffffff)
187 if (maxcount
== 0xffffffff) {
190 } else if ((count
< 0x40000000) && (maxcount
> 0xc0000000)) {
193 } else if ((count
> maxcount
) && (count
< 0xc0000000)) {
196 } else if ((count
> maxcount
) && (count
> 0xc0000000)
197 && (maxcount
> 0x80000000)) {
202 if (maxcount
== 0xffffffff) {
206 printk(KERN_DEBUG
"mtdoops: Ready %d, %d (first init)\n",
207 cxt
->nextpage
, cxt
->nextcount
);
211 cxt
->nextpage
= maxpos
;
212 cxt
->nextcount
= maxcount
;
214 return mtdoops_inc_counter(cxt
);
218 static void mtdoops_notify_add(struct mtd_info
*mtd
)
220 struct mtdoops_context
*cxt
= &oops_cxt
;
223 if ((mtd
->index
!= cxt
->mtd_index
) || cxt
->mtd_index
< 0)
226 if (mtd
->size
< (mtd
->erasesize
* 2)) {
227 printk(KERN_ERR
"MTD partition %d not big enough for mtdoops\n",
233 cxt
->oops_pages
= mtd
->size
/ OOPS_PAGE_SIZE
;
235 ret
= find_next_position(cxt
);
237 mtdoops_prepare(cxt
);
239 printk(KERN_DEBUG
"mtdoops: Attached to MTD device %d\n", mtd
->index
);
242 static void mtdoops_notify_remove(struct mtd_info
*mtd
)
244 struct mtdoops_context
*cxt
= &oops_cxt
;
246 if ((mtd
->index
!= cxt
->mtd_index
) || cxt
->mtd_index
< 0)
250 flush_scheduled_work();
253 static void mtdoops_console_sync(void)
255 struct mtdoops_context
*cxt
= &oops_cxt
;
256 struct mtd_info
*mtd
= cxt
->mtd
;
260 if (!cxt
->ready
|| !mtd
)
263 if (cxt
->writecount
== 0)
266 if (cxt
->writecount
< OOPS_PAGE_SIZE
)
267 memset(cxt
->oops_buf
+ cxt
->writecount
, 0xff,
268 OOPS_PAGE_SIZE
- cxt
->writecount
);
270 ret
= mtd
->write(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
,
271 OOPS_PAGE_SIZE
, &retlen
, cxt
->oops_buf
);
275 if ((retlen
!= OOPS_PAGE_SIZE
) || (ret
< 0))
276 printk(KERN_ERR
"mtdoops: Write failure at %d (%td of %d written), err %d.\n",
277 cxt
->nextpage
* OOPS_PAGE_SIZE
, retlen
, OOPS_PAGE_SIZE
, ret
);
279 ret
= mtdoops_inc_counter(cxt
);
281 schedule_work(&cxt
->work
);
285 mtdoops_console_write(struct console
*co
, const char *s
, unsigned int count
)
287 struct mtdoops_context
*cxt
= co
->data
;
288 struct mtd_info
*mtd
= cxt
->mtd
;
291 if (!oops_in_progress
) {
292 mtdoops_console_sync();
296 if (!cxt
->ready
|| !mtd
)
299 if (cxt
->writecount
== 0) {
300 u32
*stamp
= cxt
->oops_buf
;
301 *stamp
= cxt
->nextcount
;
305 if ((count
+ cxt
->writecount
) > OOPS_PAGE_SIZE
)
306 count
= OOPS_PAGE_SIZE
- cxt
->writecount
;
308 for (i
= 0; i
< count
; i
++, s
++)
309 *((char *)(cxt
->oops_buf
) + cxt
->writecount
+ i
) = *s
;
311 cxt
->writecount
= cxt
->writecount
+ count
;
314 static int __init
mtdoops_console_setup(struct console
*co
, char *options
)
316 struct mtdoops_context
*cxt
= co
->data
;
318 if (cxt
->mtd_index
!= -1)
323 cxt
->mtd_index
= co
->index
;
327 static struct mtd_notifier mtdoops_notifier
= {
328 .add
= mtdoops_notify_add
,
329 .remove
= mtdoops_notify_remove
,
332 static struct console mtdoops_console
= {
334 .write
= mtdoops_console_write
,
335 .setup
= mtdoops_console_setup
,
336 .unblank
= mtdoops_console_sync
,
337 .flags
= CON_PRINTBUFFER
,
342 static int __init
mtdoops_console_init(void)
344 struct mtdoops_context
*cxt
= &oops_cxt
;
347 cxt
->oops_buf
= vmalloc(OOPS_PAGE_SIZE
);
349 if (!cxt
->oops_buf
) {
350 printk(KERN_ERR
"Failed to allocate oops buffer workspace\n");
354 INIT_WORK(&cxt
->work
, mtdoops_workfunc
);
356 register_console(&mtdoops_console
);
357 register_mtd_user(&mtdoops_notifier
);
361 static void __exit
mtdoops_console_exit(void)
363 struct mtdoops_context
*cxt
= &oops_cxt
;
365 unregister_mtd_user(&mtdoops_notifier
);
366 unregister_console(&mtdoops_console
);
367 vfree(cxt
->oops_buf
);
371 subsys_initcall(mtdoops_console_init
);
372 module_exit(mtdoops_console_exit
);
374 MODULE_LICENSE("GPL");
375 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
376 MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");