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 struct mtdoops_context
{
37 struct work_struct work_erase
;
38 struct work_struct work_write
;
49 static void mtdoops_erase_callback(struct erase_info
*done
)
51 wait_queue_head_t
*wait_q
= (wait_queue_head_t
*)done
->priv
;
55 static int mtdoops_erase_block(struct mtd_info
*mtd
, int offset
)
57 struct erase_info erase
;
58 DECLARE_WAITQUEUE(wait
, current
);
59 wait_queue_head_t wait_q
;
62 init_waitqueue_head(&wait_q
);
64 erase
.callback
= mtdoops_erase_callback
;
66 if (mtd
->erasesize
< OOPS_PAGE_SIZE
)
67 erase
.len
= OOPS_PAGE_SIZE
;
69 erase
.len
= mtd
->erasesize
;
70 erase
.priv
= (u_long
)&wait_q
;
72 set_current_state(TASK_INTERRUPTIBLE
);
73 add_wait_queue(&wait_q
, &wait
);
75 ret
= mtd
->erase(mtd
, &erase
);
77 set_current_state(TASK_RUNNING
);
78 remove_wait_queue(&wait_q
, &wait
);
79 printk (KERN_WARNING
"mtdoops: erase of region [0x%x, 0x%x] "
81 erase
.addr
, erase
.len
, mtd
->name
);
85 schedule(); /* Wait for erase to finish. */
86 remove_wait_queue(&wait_q
, &wait
);
91 static void mtdoops_inc_counter(struct mtdoops_context
*cxt
)
93 struct mtd_info
*mtd
= cxt
->mtd
;
99 if (cxt
->nextpage
> cxt
->oops_pages
)
102 if (cxt
->nextcount
== 0xffffffff)
105 ret
= mtd
->read(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
, 4,
106 &retlen
, (u_char
*) &count
);
107 if ((retlen
!= 4) || ((ret
< 0) && (ret
!= -EUCLEAN
))) {
108 printk(KERN_ERR
"mtdoops: Read failure at %d (%td of 4 read)"
109 ", err %d.\n", cxt
->nextpage
* OOPS_PAGE_SIZE
,
111 schedule_work(&cxt
->work_erase
);
115 /* See if we need to erase the next block */
116 if (count
!= 0xffffffff) {
117 schedule_work(&cxt
->work_erase
);
121 printk(KERN_DEBUG
"mtdoops: Ready %d, %d (no erase)\n",
122 cxt
->nextpage
, cxt
->nextcount
);
126 /* Scheduled work - when we can't proceed without erasing a block */
127 static void mtdoops_workfunc_erase(struct work_struct
*work
)
129 struct mtdoops_context
*cxt
=
130 container_of(work
, struct mtdoops_context
, work_erase
);
131 struct mtd_info
*mtd
= cxt
->mtd
;
132 int i
= 0, j
, ret
, mod
;
134 /* We were unregistered */
138 mod
= (cxt
->nextpage
* OOPS_PAGE_SIZE
) % mtd
->erasesize
;
140 cxt
->nextpage
= cxt
->nextpage
+ ((mtd
->erasesize
- mod
) / OOPS_PAGE_SIZE
);
141 if (cxt
->nextpage
> cxt
->oops_pages
)
145 while (mtd
->block_isbad
) {
146 ret
= mtd
->block_isbad(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
);
150 printk(KERN_ERR
"mtdoops: block_isbad failed, aborting.\n");
154 printk(KERN_WARNING
"mtdoops: Bad block at %08x\n",
155 cxt
->nextpage
* OOPS_PAGE_SIZE
);
157 cxt
->nextpage
= cxt
->nextpage
+ (mtd
->erasesize
/ OOPS_PAGE_SIZE
);
158 if (cxt
->nextpage
> cxt
->oops_pages
)
160 if (i
== (cxt
->oops_pages
/ (mtd
->erasesize
/ OOPS_PAGE_SIZE
))) {
161 printk(KERN_ERR
"mtdoops: All blocks bad!\n");
166 for (j
= 0, ret
= -1; (j
< 3) && (ret
< 0); j
++)
167 ret
= mtdoops_erase_block(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
);
170 printk(KERN_DEBUG
"mtdoops: Ready %d, %d \n", cxt
->nextpage
, cxt
->nextcount
);
175 if (mtd
->block_markbad
&& (ret
== -EIO
)) {
176 ret
= mtd
->block_markbad(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
);
178 printk(KERN_ERR
"mtdoops: block_markbad failed, aborting.\n");
185 static void mtdoops_workfunc_write(struct work_struct
*work
)
187 struct mtdoops_context
*cxt
=
188 container_of(work
, struct mtdoops_context
, work_write
);
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
);
197 ret
= mtd
->write(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
,
198 OOPS_PAGE_SIZE
, &retlen
, cxt
->oops_buf
);
202 if ((retlen
!= OOPS_PAGE_SIZE
) || (ret
< 0))
203 printk(KERN_ERR
"mtdoops: Write failure at %d (%td of %d written), err %d.\n",
204 cxt
->nextpage
* OOPS_PAGE_SIZE
, retlen
, OOPS_PAGE_SIZE
, ret
);
206 mtdoops_inc_counter(cxt
);
209 static void find_next_position(struct mtdoops_context
*cxt
)
211 struct mtd_info
*mtd
= cxt
->mtd
;
212 int ret
, page
, maxpos
= 0;
213 u32 count
, maxcount
= 0xffffffff;
216 for (page
= 0; page
< cxt
->oops_pages
; page
++) {
217 ret
= mtd
->read(mtd
, page
* OOPS_PAGE_SIZE
, 4, &retlen
, (u_char
*) &count
);
218 if ((retlen
!= 4) || ((ret
< 0) && (ret
!= -EUCLEAN
))) {
219 printk(KERN_ERR
"mtdoops: Read failure at %d (%td of 4 read)"
220 ", err %d.\n", page
* OOPS_PAGE_SIZE
, retlen
, ret
);
224 if (count
== 0xffffffff)
226 if (maxcount
== 0xffffffff) {
229 } else if ((count
< 0x40000000) && (maxcount
> 0xc0000000)) {
232 } else if ((count
> maxcount
) && (count
< 0xc0000000)) {
235 } else if ((count
> maxcount
) && (count
> 0xc0000000)
236 && (maxcount
> 0x80000000)) {
241 if (maxcount
== 0xffffffff) {
245 printk(KERN_DEBUG
"mtdoops: Ready %d, %d (first init)\n",
246 cxt
->nextpage
, cxt
->nextcount
);
250 cxt
->nextpage
= maxpos
;
251 cxt
->nextcount
= maxcount
;
253 mtdoops_inc_counter(cxt
);
257 static void mtdoops_notify_add(struct mtd_info
*mtd
)
259 struct mtdoops_context
*cxt
= &oops_cxt
;
261 if ((mtd
->index
!= cxt
->mtd_index
) || cxt
->mtd_index
< 0)
264 if (mtd
->size
< (mtd
->erasesize
* 2)) {
265 printk(KERN_ERR
"MTD partition %d not big enough for mtdoops\n",
271 cxt
->oops_pages
= mtd
->size
/ OOPS_PAGE_SIZE
;
273 find_next_position(cxt
);
275 printk(KERN_DEBUG
"mtdoops: Attached to MTD device %d\n", mtd
->index
);
278 static void mtdoops_notify_remove(struct mtd_info
*mtd
)
280 struct mtdoops_context
*cxt
= &oops_cxt
;
282 if ((mtd
->index
!= cxt
->mtd_index
) || cxt
->mtd_index
< 0)
286 flush_scheduled_work();
289 static void mtdoops_console_sync(void)
291 struct mtdoops_context
*cxt
= &oops_cxt
;
292 struct mtd_info
*mtd
= cxt
->mtd
;
294 if (!cxt
->ready
|| !mtd
|| cxt
->writecount
== 0)
299 schedule_work(&cxt
->work_write
);
303 mtdoops_console_write(struct console
*co
, const char *s
, unsigned int count
)
305 struct mtdoops_context
*cxt
= co
->data
;
306 struct mtd_info
*mtd
= cxt
->mtd
;
308 if (!oops_in_progress
) {
309 mtdoops_console_sync();
313 if (!cxt
->ready
|| !mtd
)
316 if (cxt
->writecount
== 0) {
317 u32
*stamp
= cxt
->oops_buf
;
318 *stamp
= cxt
->nextcount
;
322 if ((count
+ cxt
->writecount
) > OOPS_PAGE_SIZE
)
323 count
= OOPS_PAGE_SIZE
- cxt
->writecount
;
325 memcpy(cxt
->oops_buf
+ cxt
->writecount
, s
, count
);
326 cxt
->writecount
+= count
;
329 static int __init
mtdoops_console_setup(struct console
*co
, char *options
)
331 struct mtdoops_context
*cxt
= co
->data
;
333 if (cxt
->mtd_index
!= -1)
338 cxt
->mtd_index
= co
->index
;
342 static struct mtd_notifier mtdoops_notifier
= {
343 .add
= mtdoops_notify_add
,
344 .remove
= mtdoops_notify_remove
,
347 static struct console mtdoops_console
= {
349 .write
= mtdoops_console_write
,
350 .setup
= mtdoops_console_setup
,
351 .unblank
= mtdoops_console_sync
,
352 .flags
= CON_PRINTBUFFER
,
357 static int __init
mtdoops_console_init(void)
359 struct mtdoops_context
*cxt
= &oops_cxt
;
362 cxt
->oops_buf
= vmalloc(OOPS_PAGE_SIZE
);
364 if (!cxt
->oops_buf
) {
365 printk(KERN_ERR
"Failed to allocate oops buffer workspace\n");
369 INIT_WORK(&cxt
->work_erase
, mtdoops_workfunc_erase
);
370 INIT_WORK(&cxt
->work_write
, mtdoops_workfunc_write
);
372 register_console(&mtdoops_console
);
373 register_mtd_user(&mtdoops_notifier
);
377 static void __exit
mtdoops_console_exit(void)
379 struct mtdoops_context
*cxt
= &oops_cxt
;
381 unregister_mtd_user(&mtdoops_notifier
);
382 unregister_console(&mtdoops_console
);
383 vfree(cxt
->oops_buf
);
387 subsys_initcall(mtdoops_console_init
);
388 module_exit(mtdoops_console_exit
);
390 MODULE_LICENSE("GPL");
391 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
392 MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");