blob: 6f6b2f3c70dffbb277acad057447858d41ed7447 [file] [log] [blame]
Richard Purdie4b23aff2007-05-29 13:31:42 +01001/*
2 * MTD Oops/Panic logger
3 *
4 * Copyright (C) 2007 Nokia Corporation. All rights reserved.
5 *
6 * Author: Richard Purdie <rpurdie@openedhand.com>
7 *
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.
11 *
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.
16 *
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
20 * 02110-1301 USA
21 *
22 */
23
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>
Richard Purdie621e4f82008-02-06 10:17:50 +000031#include <linux/delay.h>
Richard Purdie47c152b2008-01-29 10:21:56 +000032#include <linux/spinlock.h>
David Woodhousef9f7dd22008-02-07 10:50:57 +000033#include <linux/interrupt.h>
Richard Purdie4b23aff2007-05-29 13:31:42 +010034#include <linux/mtd/mtd.h>
35
Richard Purdief0482ee2008-07-26 09:22:45 +010036#define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
Richard Purdie4b23aff2007-05-29 13:31:42 +010037#define OOPS_PAGE_SIZE 4096
38
Adrian Bunk7903cba2008-04-18 13:44:11 -070039static struct mtdoops_context {
Richard Purdie4b23aff2007-05-29 13:31:42 +010040 int mtd_index;
Richard Purdie6ce0a852008-01-29 11:27:11 +000041 struct work_struct work_erase;
42 struct work_struct work_write;
Richard Purdie4b23aff2007-05-29 13:31:42 +010043 struct mtd_info *mtd;
44 int oops_pages;
45 int nextpage;
46 int nextcount;
47
48 void *oops_buf;
Richard Purdie47c152b2008-01-29 10:21:56 +000049
50 /* writecount and disabling ready are spin lock protected */
51 spinlock_t writecount_lock;
Richard Purdie4b23aff2007-05-29 13:31:42 +010052 int ready;
53 int writecount;
54} oops_cxt;
55
56static void mtdoops_erase_callback(struct erase_info *done)
57{
58 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
59 wake_up(wait_q);
60}
61
62static int mtdoops_erase_block(struct mtd_info *mtd, int offset)
63{
64 struct erase_info erase;
65 DECLARE_WAITQUEUE(wait, current);
66 wait_queue_head_t wait_q;
67 int ret;
68
69 init_waitqueue_head(&wait_q);
70 erase.mtd = mtd;
71 erase.callback = mtdoops_erase_callback;
72 erase.addr = offset;
Richard Purdie79dcd8e2008-01-29 10:25:55 +000073 erase.len = mtd->erasesize;
Richard Purdie4b23aff2007-05-29 13:31:42 +010074 erase.priv = (u_long)&wait_q;
75
76 set_current_state(TASK_INTERRUPTIBLE);
77 add_wait_queue(&wait_q, &wait);
78
79 ret = mtd->erase(mtd, &erase);
80 if (ret) {
81 set_current_state(TASK_RUNNING);
82 remove_wait_queue(&wait_q, &wait);
83 printk (KERN_WARNING "mtdoops: erase of region [0x%x, 0x%x] "
84 "on \"%s\" failed\n",
85 erase.addr, erase.len, mtd->name);
86 return ret;
87 }
88
89 schedule(); /* Wait for erase to finish. */
90 remove_wait_queue(&wait_q, &wait);
91
92 return 0;
93}
94
Richard Purdie6ce0a852008-01-29 11:27:11 +000095static void mtdoops_inc_counter(struct mtdoops_context *cxt)
Richard Purdie4b23aff2007-05-29 13:31:42 +010096{
97 struct mtd_info *mtd = cxt->mtd;
98 size_t retlen;
99 u32 count;
100 int ret;
101
102 cxt->nextpage++;
Richard Purdieecd5b312008-07-26 09:17:41 +0100103 if (cxt->nextpage >= cxt->oops_pages)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100104 cxt->nextpage = 0;
105 cxt->nextcount++;
106 if (cxt->nextcount == 0xffffffff)
107 cxt->nextcount = 0;
108
109 ret = mtd->read(mtd, cxt->nextpage * OOPS_PAGE_SIZE, 4,
110 &retlen, (u_char *) &count);
Richard Purdie2986bd22008-01-29 11:27:09 +0000111 if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) {
Andrew Morton68d09b12007-08-10 14:01:31 -0700112 printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)"
Richard Purdie4b23aff2007-05-29 13:31:42 +0100113 ", err %d.\n", cxt->nextpage * OOPS_PAGE_SIZE,
114 retlen, ret);
Richard Purdie6ce0a852008-01-29 11:27:11 +0000115 schedule_work(&cxt->work_erase);
116 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100117 }
118
119 /* See if we need to erase the next block */
Richard Purdie6ce0a852008-01-29 11:27:11 +0000120 if (count != 0xffffffff) {
121 schedule_work(&cxt->work_erase);
122 return;
123 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100124
125 printk(KERN_DEBUG "mtdoops: Ready %d, %d (no erase)\n",
126 cxt->nextpage, cxt->nextcount);
127 cxt->ready = 1;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100128}
129
Richard Purdie6ce0a852008-01-29 11:27:11 +0000130/* Scheduled work - when we can't proceed without erasing a block */
131static void mtdoops_workfunc_erase(struct work_struct *work)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100132{
Richard Purdie6ce0a852008-01-29 11:27:11 +0000133 struct mtdoops_context *cxt =
134 container_of(work, struct mtdoops_context, work_erase);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100135 struct mtd_info *mtd = cxt->mtd;
136 int i = 0, j, ret, mod;
137
138 /* We were unregistered */
139 if (!mtd)
140 return;
141
142 mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize;
143 if (mod != 0) {
144 cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE);
Richard Purdieecd5b312008-07-26 09:17:41 +0100145 if (cxt->nextpage >= cxt->oops_pages)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100146 cxt->nextpage = 0;
147 }
148
Richard Purdie2986bd22008-01-29 11:27:09 +0000149 while (mtd->block_isbad) {
150 ret = mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
151 if (!ret)
152 break;
153 if (ret < 0) {
154 printk(KERN_ERR "mtdoops: block_isbad failed, aborting.\n");
155 return;
156 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100157badblock:
158 printk(KERN_WARNING "mtdoops: Bad block at %08x\n",
159 cxt->nextpage * OOPS_PAGE_SIZE);
160 i++;
161 cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE);
Richard Purdieecd5b312008-07-26 09:17:41 +0100162 if (cxt->nextpage >= cxt->oops_pages)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100163 cxt->nextpage = 0;
164 if (i == (cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE))) {
165 printk(KERN_ERR "mtdoops: All blocks bad!\n");
166 return;
167 }
168 }
169
170 for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
171 ret = mtdoops_erase_block(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
172
Richard Purdie2986bd22008-01-29 11:27:09 +0000173 if (ret >= 0) {
174 printk(KERN_DEBUG "mtdoops: Ready %d, %d \n", cxt->nextpage, cxt->nextcount);
175 cxt->ready = 1;
176 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100177 }
178
Richard Purdie2986bd22008-01-29 11:27:09 +0000179 if (mtd->block_markbad && (ret == -EIO)) {
180 ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
181 if (ret < 0) {
182 printk(KERN_ERR "mtdoops: block_markbad failed, aborting.\n");
183 return;
184 }
185 }
186 goto badblock;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100187}
188
Richard Purdie621e4f82008-02-06 10:17:50 +0000189static void mtdoops_write(struct mtdoops_context *cxt, int panic)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100190{
Richard Purdie6ce0a852008-01-29 11:27:11 +0000191 struct mtd_info *mtd = cxt->mtd;
192 size_t retlen;
193 int ret;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100194
Richard Purdie6ce0a852008-01-29 11:27:11 +0000195 if (cxt->writecount < OOPS_PAGE_SIZE)
196 memset(cxt->oops_buf + cxt->writecount, 0xff,
197 OOPS_PAGE_SIZE - cxt->writecount);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100198
Richard Purdie621e4f82008-02-06 10:17:50 +0000199 if (panic)
200 ret = mtd->panic_write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
201 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
202 else
203 ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
Richard Purdie6ce0a852008-01-29 11:27:11 +0000204 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
205
206 cxt->writecount = 0;
207
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);
211
212 mtdoops_inc_counter(cxt);
Richard Purdie621e4f82008-02-06 10:17:50 +0000213}
214
215
216static void mtdoops_workfunc_write(struct work_struct *work)
217{
218 struct mtdoops_context *cxt =
219 container_of(work, struct mtdoops_context, work_write);
220
221 mtdoops_write(cxt, 0);
Richard Purdie6ce0a852008-01-29 11:27:11 +0000222}
223
224static void find_next_position(struct mtdoops_context *cxt)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100225{
226 struct mtd_info *mtd = cxt->mtd;
Richard Purdie2986bd22008-01-29 11:27:09 +0000227 int ret, page, maxpos = 0;
Richard Purdief0482ee2008-07-26 09:22:45 +0100228 u32 count[2], maxcount = 0xffffffff;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100229 size_t retlen;
230
231 for (page = 0; page < cxt->oops_pages; page++) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100232 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)"
Richard Purdie2986bd22008-01-29 11:27:09 +0000235 ", err %d.\n", page * OOPS_PAGE_SIZE, retlen, ret);
236 continue;
237 }
238
Richard Purdief0482ee2008-07-26 09:22:45 +0100239 if (count[1] != MTDOOPS_KERNMSG_MAGIC)
240 continue;
241 if (count[0] == 0xffffffff)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100242 continue;
243 if (maxcount == 0xffffffff) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100244 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100245 maxpos = page;
Richard Purdief0482ee2008-07-26 09:22:45 +0100246 } else if ((count[0] < 0x40000000) && (maxcount > 0xc0000000)) {
247 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100248 maxpos = page;
Richard Purdief0482ee2008-07-26 09:22:45 +0100249 } else if ((count[0] > maxcount) && (count[0] < 0xc0000000)) {
250 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100251 maxpos = page;
Richard Purdief0482ee2008-07-26 09:22:45 +0100252 } else if ((count[0] > maxcount) && (count[0] > 0xc0000000)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100253 && (maxcount > 0x80000000)) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100254 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100255 maxpos = page;
256 }
257 }
258 if (maxcount == 0xffffffff) {
259 cxt->nextpage = 0;
260 cxt->nextcount = 1;
261 cxt->ready = 1;
262 printk(KERN_DEBUG "mtdoops: Ready %d, %d (first init)\n",
263 cxt->nextpage, cxt->nextcount);
Richard Purdie6ce0a852008-01-29 11:27:11 +0000264 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100265 }
266
267 cxt->nextpage = maxpos;
268 cxt->nextcount = maxcount;
269
Richard Purdie6ce0a852008-01-29 11:27:11 +0000270 mtdoops_inc_counter(cxt);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100271}
272
273
274static void mtdoops_notify_add(struct mtd_info *mtd)
275{
276 struct mtdoops_context *cxt = &oops_cxt;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100277
278 if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
279 return;
280
281 if (mtd->size < (mtd->erasesize * 2)) {
282 printk(KERN_ERR "MTD partition %d not big enough for mtdoops\n",
283 mtd->index);
284 return;
285 }
286
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000287 if (mtd->erasesize < OOPS_PAGE_SIZE) {
288 printk(KERN_ERR "Eraseblock size of MTD partition %d too small\n",
289 mtd->index);
290 return;
291 }
292
Richard Purdie4b23aff2007-05-29 13:31:42 +0100293 cxt->mtd = mtd;
294 cxt->oops_pages = mtd->size / OOPS_PAGE_SIZE;
295
Richard Purdie6ce0a852008-01-29 11:27:11 +0000296 find_next_position(cxt);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100297
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000298 printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100299}
300
301static void mtdoops_notify_remove(struct mtd_info *mtd)
302{
303 struct mtdoops_context *cxt = &oops_cxt;
304
305 if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
306 return;
307
308 cxt->mtd = NULL;
309 flush_scheduled_work();
310}
311
Richard Purdie8691a722007-07-10 20:33:54 +0100312static void mtdoops_console_sync(void)
313{
314 struct mtdoops_context *cxt = &oops_cxt;
315 struct mtd_info *mtd = cxt->mtd;
Richard Purdie47c152b2008-01-29 10:21:56 +0000316 unsigned long flags;
Richard Purdie8691a722007-07-10 20:33:54 +0100317
Richard Purdie6ce0a852008-01-29 11:27:11 +0000318 if (!cxt->ready || !mtd || cxt->writecount == 0)
Richard Purdie8691a722007-07-10 20:33:54 +0100319 return;
320
Richard Purdie47c152b2008-01-29 10:21:56 +0000321 /*
322 * Once ready is 0 and we've held the lock no further writes to the
323 * buffer will happen
324 */
325 spin_lock_irqsave(&cxt->writecount_lock, flags);
326 if (!cxt->ready) {
327 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
328 return;
329 }
Richard Purdie8691a722007-07-10 20:33:54 +0100330 cxt->ready = 0;
Richard Purdie47c152b2008-01-29 10:21:56 +0000331 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
Richard Purdie8691a722007-07-10 20:33:54 +0100332
Richard Purdie621e4f82008-02-06 10:17:50 +0000333 if (mtd->panic_write && in_interrupt())
334 /* Interrupt context, we're going to panic so try and log */
335 mtdoops_write(cxt, 1);
336 else
337 schedule_work(&cxt->work_write);
Richard Purdie8691a722007-07-10 20:33:54 +0100338}
Richard Purdie4b23aff2007-05-29 13:31:42 +0100339
340static void
341mtdoops_console_write(struct console *co, const char *s, unsigned int count)
342{
343 struct mtdoops_context *cxt = co->data;
344 struct mtd_info *mtd = cxt->mtd;
Richard Purdie47c152b2008-01-29 10:21:56 +0000345 unsigned long flags;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100346
Richard Purdie8691a722007-07-10 20:33:54 +0100347 if (!oops_in_progress) {
348 mtdoops_console_sync();
Richard Purdie4b23aff2007-05-29 13:31:42 +0100349 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100350 }
351
Richard Purdie8691a722007-07-10 20:33:54 +0100352 if (!cxt->ready || !mtd)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100353 return;
354
Richard Purdie47c152b2008-01-29 10:21:56 +0000355 /* Locking on writecount ensures sequential writes to the buffer */
356 spin_lock_irqsave(&cxt->writecount_lock, flags);
357
358 /* Check ready status didn't change whilst waiting for the lock */
359 if (!cxt->ready)
360 return;
361
Richard Purdie4b23aff2007-05-29 13:31:42 +0100362 if (cxt->writecount == 0) {
363 u32 *stamp = cxt->oops_buf;
Richard Purdief0482ee2008-07-26 09:22:45 +0100364 *stamp++ = cxt->nextcount;
365 *stamp = MTDOOPS_KERNMSG_MAGIC;
366 cxt->writecount = 8;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100367 }
368
369 if ((count + cxt->writecount) > OOPS_PAGE_SIZE)
370 count = OOPS_PAGE_SIZE - cxt->writecount;
371
Peter Korsgaard235d6202007-11-06 11:56:02 +0100372 memcpy(cxt->oops_buf + cxt->writecount, s, count);
373 cxt->writecount += count;
Richard Purdie47c152b2008-01-29 10:21:56 +0000374
375 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
376
377 if (cxt->writecount == OOPS_PAGE_SIZE)
378 mtdoops_console_sync();
Richard Purdie4b23aff2007-05-29 13:31:42 +0100379}
380
381static int __init mtdoops_console_setup(struct console *co, char *options)
382{
383 struct mtdoops_context *cxt = co->data;
384
385 if (cxt->mtd_index != -1)
386 return -EBUSY;
387 if (co->index == -1)
388 return -EINVAL;
389
390 cxt->mtd_index = co->index;
391 return 0;
392}
393
394static struct mtd_notifier mtdoops_notifier = {
395 .add = mtdoops_notify_add,
396 .remove = mtdoops_notify_remove,
397};
398
399static struct console mtdoops_console = {
400 .name = "ttyMTD",
401 .write = mtdoops_console_write,
402 .setup = mtdoops_console_setup,
Richard Purdie8691a722007-07-10 20:33:54 +0100403 .unblank = mtdoops_console_sync,
Richard Purdie4b23aff2007-05-29 13:31:42 +0100404 .index = -1,
405 .data = &oops_cxt,
406};
407
408static int __init mtdoops_console_init(void)
409{
410 struct mtdoops_context *cxt = &oops_cxt;
411
412 cxt->mtd_index = -1;
413 cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE);
414
415 if (!cxt->oops_buf) {
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000416 printk(KERN_ERR "Failed to allocate mtdoops buffer workspace\n");
Richard Purdie4b23aff2007-05-29 13:31:42 +0100417 return -ENOMEM;
418 }
419
Richard Purdie6ce0a852008-01-29 11:27:11 +0000420 INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
421 INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100422
423 register_console(&mtdoops_console);
424 register_mtd_user(&mtdoops_notifier);
425 return 0;
426}
427
428static void __exit mtdoops_console_exit(void)
429{
430 struct mtdoops_context *cxt = &oops_cxt;
431
432 unregister_mtd_user(&mtdoops_notifier);
433 unregister_console(&mtdoops_console);
434 vfree(cxt->oops_buf);
435}
436
437
438subsys_initcall(mtdoops_console_init);
439module_exit(mtdoops_console_exit);
440
441MODULE_LICENSE("GPL");
442MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
443MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");