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 OOPS_PAGE_SIZE 4096
38 struct mtdoops_context
{
40 struct work_struct work_erase
;
41 struct work_struct work_write
;
49 /* writecount and disabling ready are spin lock protected */
50 spinlock_t writecount_lock
;
55 static void mtdoops_erase_callback(struct erase_info
*done
)
57 wait_queue_head_t
*wait_q
= (wait_queue_head_t
*)done
->priv
;
61 static int mtdoops_erase_block(struct mtd_info
*mtd
, int offset
)
63 struct erase_info erase
;
64 DECLARE_WAITQUEUE(wait
, current
);
65 wait_queue_head_t wait_q
;
68 init_waitqueue_head(&wait_q
);
70 erase
.callback
= mtdoops_erase_callback
;
72 erase
.len
= mtd
->erasesize
;
73 erase
.priv
= (u_long
)&wait_q
;
75 set_current_state(TASK_INTERRUPTIBLE
);
76 add_wait_queue(&wait_q
, &wait
);
78 ret
= mtd
->erase(mtd
, &erase
);
80 set_current_state(TASK_RUNNING
);
81 remove_wait_queue(&wait_q
, &wait
);
82 printk (KERN_WARNING
"mtdoops: erase of region [0x%x, 0x%x] "
84 erase
.addr
, erase
.len
, mtd
->name
);
88 schedule(); /* Wait for erase to finish. */
89 remove_wait_queue(&wait_q
, &wait
);
94 static void mtdoops_inc_counter(struct mtdoops_context
*cxt
)
96 struct mtd_info
*mtd
= cxt
->mtd
;
102 if (cxt
->nextpage
> cxt
->oops_pages
)
105 if (cxt
->nextcount
== 0xffffffff)
108 ret
= mtd
->read(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
, 4,
109 &retlen
, (u_char
*) &count
);
110 if ((retlen
!= 4) || ((ret
< 0) && (ret
!= -EUCLEAN
))) {
111 printk(KERN_ERR
"mtdoops: Read failure at %d (%td of 4 read)"
112 ", err %d.\n", cxt
->nextpage
* OOPS_PAGE_SIZE
,
114 schedule_work(&cxt
->work_erase
);
118 /* See if we need to erase the next block */
119 if (count
!= 0xffffffff) {
120 schedule_work(&cxt
->work_erase
);
124 printk(KERN_DEBUG
"mtdoops: Ready %d, %d (no erase)\n",
125 cxt
->nextpage
, cxt
->nextcount
);
129 /* Scheduled work - when we can't proceed without erasing a block */
130 static void mtdoops_workfunc_erase(struct work_struct
*work
)
132 struct mtdoops_context
*cxt
=
133 container_of(work
, struct mtdoops_context
, work_erase
);
134 struct mtd_info
*mtd
= cxt
->mtd
;
135 int i
= 0, j
, ret
, mod
;
137 /* We were unregistered */
141 mod
= (cxt
->nextpage
* OOPS_PAGE_SIZE
) % mtd
->erasesize
;
143 cxt
->nextpage
= cxt
->nextpage
+ ((mtd
->erasesize
- mod
) / OOPS_PAGE_SIZE
);
144 if (cxt
->nextpage
> cxt
->oops_pages
)
148 while (mtd
->block_isbad
) {
149 ret
= mtd
->block_isbad(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
);
153 printk(KERN_ERR
"mtdoops: block_isbad failed, aborting.\n");
157 printk(KERN_WARNING
"mtdoops: Bad block at %08x\n",
158 cxt
->nextpage
* OOPS_PAGE_SIZE
);
160 cxt
->nextpage
= cxt
->nextpage
+ (mtd
->erasesize
/ OOPS_PAGE_SIZE
);
161 if (cxt
->nextpage
> cxt
->oops_pages
)
163 if (i
== (cxt
->oops_pages
/ (mtd
->erasesize
/ OOPS_PAGE_SIZE
))) {
164 printk(KERN_ERR
"mtdoops: All blocks bad!\n");
169 for (j
= 0, ret
= -1; (j
< 3) && (ret
< 0); j
++)
170 ret
= mtdoops_erase_block(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
);
173 printk(KERN_DEBUG
"mtdoops: Ready %d, %d \n", cxt
->nextpage
, cxt
->nextcount
);
178 if (mtd
->block_markbad
&& (ret
== -EIO
)) {
179 ret
= mtd
->block_markbad(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
);
181 printk(KERN_ERR
"mtdoops: block_markbad failed, aborting.\n");
188 static void mtdoops_write(struct mtdoops_context
*cxt
, int panic
)
190 struct mtd_info
*mtd
= cxt
->mtd
;
194 if (cxt
->writecount
< OOPS_PAGE_SIZE
)
195 memset(cxt
->oops_buf
+ cxt
->writecount
, 0xff,
196 OOPS_PAGE_SIZE
- cxt
->writecount
);
199 ret
= mtd
->panic_write(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
,
200 OOPS_PAGE_SIZE
, &retlen
, cxt
->oops_buf
);
202 ret
= mtd
->write(mtd
, cxt
->nextpage
* OOPS_PAGE_SIZE
,
203 OOPS_PAGE_SIZE
, &retlen
, cxt
->oops_buf
);
207 if ((retlen
!= OOPS_PAGE_SIZE
) || (ret
< 0))
208 printk(KERN_ERR
"mtdoops: Write failure at %d (%td of %d written), err %d.\n",
209 cxt
->nextpage
* OOPS_PAGE_SIZE
, retlen
, OOPS_PAGE_SIZE
, ret
);
211 mtdoops_inc_counter(cxt
);
215 static void mtdoops_workfunc_write(struct work_struct
*work
)
217 struct mtdoops_context
*cxt
=
218 container_of(work
, struct mtdoops_context
, work_write
);
220 mtdoops_write(cxt
, 0);
223 static void find_next_position(struct mtdoops_context
*cxt
)
225 struct mtd_info
*mtd
= cxt
->mtd
;
226 int ret
, page
, maxpos
= 0;
227 u32 count
, maxcount
= 0xffffffff;
230 for (page
= 0; page
< cxt
->oops_pages
; page
++) {
231 ret
= mtd
->read(mtd
, page
* OOPS_PAGE_SIZE
, 4, &retlen
, (u_char
*) &count
);
232 if ((retlen
!= 4) || ((ret
< 0) && (ret
!= -EUCLEAN
))) {
233 printk(KERN_ERR
"mtdoops: Read failure at %d (%td of 4 read)"
234 ", err %d.\n", page
* OOPS_PAGE_SIZE
, retlen
, ret
);
238 if (count
== 0xffffffff)
240 if (maxcount
== 0xffffffff) {
243 } else if ((count
< 0x40000000) && (maxcount
> 0xc0000000)) {
246 } else if ((count
> maxcount
) && (count
< 0xc0000000)) {
249 } else if ((count
> maxcount
) && (count
> 0xc0000000)
250 && (maxcount
> 0x80000000)) {
255 if (maxcount
== 0xffffffff) {
259 printk(KERN_DEBUG
"mtdoops: Ready %d, %d (first init)\n",
260 cxt
->nextpage
, cxt
->nextcount
);
264 cxt
->nextpage
= maxpos
;
265 cxt
->nextcount
= maxcount
;
267 mtdoops_inc_counter(cxt
);
271 static void mtdoops_notify_add(struct mtd_info
*mtd
)
273 struct mtdoops_context
*cxt
= &oops_cxt
;
275 if ((mtd
->index
!= cxt
->mtd_index
) || cxt
->mtd_index
< 0)
278 if (mtd
->size
< (mtd
->erasesize
* 2)) {
279 printk(KERN_ERR
"MTD partition %d not big enough for mtdoops\n",
284 if (mtd
->erasesize
< OOPS_PAGE_SIZE
) {
285 printk(KERN_ERR
"Eraseblock size of MTD partition %d too small\n",
291 cxt
->oops_pages
= mtd
->size
/ OOPS_PAGE_SIZE
;
293 find_next_position(cxt
);
295 printk(KERN_INFO
"mtdoops: Attached to MTD device %d\n", mtd
->index
);
298 static void mtdoops_notify_remove(struct mtd_info
*mtd
)
300 struct mtdoops_context
*cxt
= &oops_cxt
;
302 if ((mtd
->index
!= cxt
->mtd_index
) || cxt
->mtd_index
< 0)
306 flush_scheduled_work();
309 static void mtdoops_console_sync(void)
311 struct mtdoops_context
*cxt
= &oops_cxt
;
312 struct mtd_info
*mtd
= cxt
->mtd
;
315 if (!cxt
->ready
|| !mtd
|| cxt
->writecount
== 0)
319 * Once ready is 0 and we've held the lock no further writes to the
322 spin_lock_irqsave(&cxt
->writecount_lock
, flags
);
324 spin_unlock_irqrestore(&cxt
->writecount_lock
, flags
);
328 spin_unlock_irqrestore(&cxt
->writecount_lock
, flags
);
330 if (mtd
->panic_write
&& in_interrupt())
331 /* Interrupt context, we're going to panic so try and log */
332 mtdoops_write(cxt
, 1);
334 schedule_work(&cxt
->work_write
);
338 mtdoops_console_write(struct console
*co
, const char *s
, unsigned int count
)
340 struct mtdoops_context
*cxt
= co
->data
;
341 struct mtd_info
*mtd
= cxt
->mtd
;
344 if (!oops_in_progress
) {
345 mtdoops_console_sync();
349 if (!cxt
->ready
|| !mtd
)
352 /* Locking on writecount ensures sequential writes to the buffer */
353 spin_lock_irqsave(&cxt
->writecount_lock
, flags
);
355 /* Check ready status didn't change whilst waiting for the lock */
359 if (cxt
->writecount
== 0) {
360 u32
*stamp
= cxt
->oops_buf
;
361 *stamp
= cxt
->nextcount
;
365 if ((count
+ cxt
->writecount
) > OOPS_PAGE_SIZE
)
366 count
= OOPS_PAGE_SIZE
- cxt
->writecount
;
368 memcpy(cxt
->oops_buf
+ cxt
->writecount
, s
, count
);
369 cxt
->writecount
+= count
;
371 spin_unlock_irqrestore(&cxt
->writecount_lock
, flags
);
373 if (cxt
->writecount
== OOPS_PAGE_SIZE
)
374 mtdoops_console_sync();
377 static int __init
mtdoops_console_setup(struct console
*co
, char *options
)
379 struct mtdoops_context
*cxt
= co
->data
;
381 if (cxt
->mtd_index
!= -1)
386 cxt
->mtd_index
= co
->index
;
390 static struct mtd_notifier mtdoops_notifier
= {
391 .add
= mtdoops_notify_add
,
392 .remove
= mtdoops_notify_remove
,
395 static struct console mtdoops_console
= {
397 .write
= mtdoops_console_write
,
398 .setup
= mtdoops_console_setup
,
399 .unblank
= mtdoops_console_sync
,
404 static int __init
mtdoops_console_init(void)
406 struct mtdoops_context
*cxt
= &oops_cxt
;
409 cxt
->oops_buf
= vmalloc(OOPS_PAGE_SIZE
);
411 if (!cxt
->oops_buf
) {
412 printk(KERN_ERR
"Failed to allocate mtdoops buffer workspace\n");
416 INIT_WORK(&cxt
->work_erase
, mtdoops_workfunc_erase
);
417 INIT_WORK(&cxt
->work_write
, mtdoops_workfunc_write
);
419 register_console(&mtdoops_console
);
420 register_mtd_user(&mtdoops_notifier
);
424 static void __exit
mtdoops_console_exit(void)
426 struct mtdoops_context
*cxt
= &oops_cxt
;
428 unregister_mtd_user(&mtdoops_notifier
);
429 unregister_console(&mtdoops_console
);
430 vfree(cxt
->oops_buf
);
434 subsys_initcall(mtdoops_console_init
);
435 module_exit(mtdoops_console_exit
);
437 MODULE_LICENSE("GPL");
438 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
439 MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");