blob: fd98e38f10bcf133172b4a87475134d489d809e1 [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>
Richard Purdie4b23aff2007-05-29 13:31:42 +010033#include <linux/mtd/mtd.h>
34
35#define OOPS_PAGE_SIZE 4096
36
Richard Purdie6ce0a8562008-01-29 11:27:11 +000037struct mtdoops_context {
Richard Purdie4b23aff2007-05-29 13:31:42 +010038 int mtd_index;
Richard Purdie6ce0a8562008-01-29 11:27:11 +000039 struct work_struct work_erase;
40 struct work_struct work_write;
Richard Purdie4b23aff2007-05-29 13:31:42 +010041 struct mtd_info *mtd;
42 int oops_pages;
43 int nextpage;
44 int nextcount;
45
46 void *oops_buf;
Richard Purdie47c152b2008-01-29 10:21:56 +000047
48 /* writecount and disabling ready are spin lock protected */
49 spinlock_t writecount_lock;
Richard Purdie4b23aff2007-05-29 13:31:42 +010050 int ready;
51 int writecount;
52} oops_cxt;
53
54static void mtdoops_erase_callback(struct erase_info *done)
55{
56 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
57 wake_up(wait_q);
58}
59
60static int mtdoops_erase_block(struct mtd_info *mtd, int offset)
61{
62 struct erase_info erase;
63 DECLARE_WAITQUEUE(wait, current);
64 wait_queue_head_t wait_q;
65 int ret;
66
67 init_waitqueue_head(&wait_q);
68 erase.mtd = mtd;
69 erase.callback = mtdoops_erase_callback;
70 erase.addr = offset;
Richard Purdie79dcd8e2008-01-29 10:25:55 +000071 erase.len = mtd->erasesize;
Richard Purdie4b23aff2007-05-29 13:31:42 +010072 erase.priv = (u_long)&wait_q;
73
74 set_current_state(TASK_INTERRUPTIBLE);
75 add_wait_queue(&wait_q, &wait);
76
77 ret = mtd->erase(mtd, &erase);
78 if (ret) {
79 set_current_state(TASK_RUNNING);
80 remove_wait_queue(&wait_q, &wait);
81 printk (KERN_WARNING "mtdoops: erase of region [0x%x, 0x%x] "
82 "on \"%s\" failed\n",
83 erase.addr, erase.len, mtd->name);
84 return ret;
85 }
86
87 schedule(); /* Wait for erase to finish. */
88 remove_wait_queue(&wait_q, &wait);
89
90 return 0;
91}
92
Richard Purdie6ce0a8562008-01-29 11:27:11 +000093static void mtdoops_inc_counter(struct mtdoops_context *cxt)
Richard Purdie4b23aff2007-05-29 13:31:42 +010094{
95 struct mtd_info *mtd = cxt->mtd;
96 size_t retlen;
97 u32 count;
98 int ret;
99
100 cxt->nextpage++;
101 if (cxt->nextpage > cxt->oops_pages)
102 cxt->nextpage = 0;
103 cxt->nextcount++;
104 if (cxt->nextcount == 0xffffffff)
105 cxt->nextcount = 0;
106
107 ret = mtd->read(mtd, cxt->nextpage * OOPS_PAGE_SIZE, 4,
108 &retlen, (u_char *) &count);
Richard Purdie2986bd22008-01-29 11:27:09 +0000109 if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) {
Andrew Morton68d09b12007-08-10 14:01:31 -0700110 printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)"
Richard Purdie4b23aff2007-05-29 13:31:42 +0100111 ", err %d.\n", cxt->nextpage * OOPS_PAGE_SIZE,
112 retlen, ret);
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000113 schedule_work(&cxt->work_erase);
114 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100115 }
116
117 /* See if we need to erase the next block */
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000118 if (count != 0xffffffff) {
119 schedule_work(&cxt->work_erase);
120 return;
121 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100122
123 printk(KERN_DEBUG "mtdoops: Ready %d, %d (no erase)\n",
124 cxt->nextpage, cxt->nextcount);
125 cxt->ready = 1;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100126}
127
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000128/* Scheduled work - when we can't proceed without erasing a block */
129static void mtdoops_workfunc_erase(struct work_struct *work)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100130{
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000131 struct mtdoops_context *cxt =
132 container_of(work, struct mtdoops_context, work_erase);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100133 struct mtd_info *mtd = cxt->mtd;
134 int i = 0, j, ret, mod;
135
136 /* We were unregistered */
137 if (!mtd)
138 return;
139
140 mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize;
141 if (mod != 0) {
142 cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE);
143 if (cxt->nextpage > cxt->oops_pages)
144 cxt->nextpage = 0;
145 }
146
Richard Purdie2986bd22008-01-29 11:27:09 +0000147 while (mtd->block_isbad) {
148 ret = mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
149 if (!ret)
150 break;
151 if (ret < 0) {
152 printk(KERN_ERR "mtdoops: block_isbad failed, aborting.\n");
153 return;
154 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100155badblock:
156 printk(KERN_WARNING "mtdoops: Bad block at %08x\n",
157 cxt->nextpage * OOPS_PAGE_SIZE);
158 i++;
159 cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE);
160 if (cxt->nextpage > cxt->oops_pages)
161 cxt->nextpage = 0;
162 if (i == (cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE))) {
163 printk(KERN_ERR "mtdoops: All blocks bad!\n");
164 return;
165 }
166 }
167
168 for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
169 ret = mtdoops_erase_block(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
170
Richard Purdie2986bd22008-01-29 11:27:09 +0000171 if (ret >= 0) {
172 printk(KERN_DEBUG "mtdoops: Ready %d, %d \n", cxt->nextpage, cxt->nextcount);
173 cxt->ready = 1;
174 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100175 }
176
Richard Purdie2986bd22008-01-29 11:27:09 +0000177 if (mtd->block_markbad && (ret == -EIO)) {
178 ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
179 if (ret < 0) {
180 printk(KERN_ERR "mtdoops: block_markbad failed, aborting.\n");
181 return;
182 }
183 }
184 goto badblock;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100185}
186
Richard Purdie621e4f82008-02-06 10:17:50 +0000187static void mtdoops_write(struct mtdoops_context *cxt, int panic)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100188{
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000189 struct mtd_info *mtd = cxt->mtd;
190 size_t retlen;
191 int ret;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100192
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000193 if (cxt->writecount < OOPS_PAGE_SIZE)
194 memset(cxt->oops_buf + cxt->writecount, 0xff,
195 OOPS_PAGE_SIZE - cxt->writecount);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100196
Richard Purdie621e4f82008-02-06 10:17:50 +0000197 if (panic)
198 ret = mtd->panic_write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
199 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
200 else
201 ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000202 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
203
204 cxt->writecount = 0;
205
206 if ((retlen != OOPS_PAGE_SIZE) || (ret < 0))
207 printk(KERN_ERR "mtdoops: Write failure at %d (%td of %d written), err %d.\n",
208 cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret);
209
210 mtdoops_inc_counter(cxt);
Richard Purdie621e4f82008-02-06 10:17:50 +0000211}
212
213
214static void mtdoops_workfunc_write(struct work_struct *work)
215{
216 struct mtdoops_context *cxt =
217 container_of(work, struct mtdoops_context, work_write);
218
219 mtdoops_write(cxt, 0);
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000220}
221
222static void find_next_position(struct mtdoops_context *cxt)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100223{
224 struct mtd_info *mtd = cxt->mtd;
Richard Purdie2986bd22008-01-29 11:27:09 +0000225 int ret, page, maxpos = 0;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100226 u32 count, maxcount = 0xffffffff;
227 size_t retlen;
228
229 for (page = 0; page < cxt->oops_pages; page++) {
Richard Purdie2986bd22008-01-29 11:27:09 +0000230 ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 4, &retlen, (u_char *) &count);
231 if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) {
232 printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)"
233 ", err %d.\n", page * OOPS_PAGE_SIZE, retlen, ret);
234 continue;
235 }
236
Richard Purdie4b23aff2007-05-29 13:31:42 +0100237 if (count == 0xffffffff)
238 continue;
239 if (maxcount == 0xffffffff) {
240 maxcount = count;
241 maxpos = page;
242 } else if ((count < 0x40000000) && (maxcount > 0xc0000000)) {
243 maxcount = count;
244 maxpos = page;
245 } else if ((count > maxcount) && (count < 0xc0000000)) {
246 maxcount = count;
247 maxpos = page;
248 } else if ((count > maxcount) && (count > 0xc0000000)
249 && (maxcount > 0x80000000)) {
250 maxcount = count;
251 maxpos = page;
252 }
253 }
254 if (maxcount == 0xffffffff) {
255 cxt->nextpage = 0;
256 cxt->nextcount = 1;
257 cxt->ready = 1;
258 printk(KERN_DEBUG "mtdoops: Ready %d, %d (first init)\n",
259 cxt->nextpage, cxt->nextcount);
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000260 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100261 }
262
263 cxt->nextpage = maxpos;
264 cxt->nextcount = maxcount;
265
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000266 mtdoops_inc_counter(cxt);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100267}
268
269
270static void mtdoops_notify_add(struct mtd_info *mtd)
271{
272 struct mtdoops_context *cxt = &oops_cxt;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100273
274 if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
275 return;
276
277 if (mtd->size < (mtd->erasesize * 2)) {
278 printk(KERN_ERR "MTD partition %d not big enough for mtdoops\n",
279 mtd->index);
280 return;
281 }
282
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000283 if (mtd->erasesize < OOPS_PAGE_SIZE) {
284 printk(KERN_ERR "Eraseblock size of MTD partition %d too small\n",
285 mtd->index);
286 return;
287 }
288
Richard Purdie4b23aff2007-05-29 13:31:42 +0100289 cxt->mtd = mtd;
290 cxt->oops_pages = mtd->size / OOPS_PAGE_SIZE;
291
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000292 find_next_position(cxt);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100293
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000294 printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100295}
296
297static void mtdoops_notify_remove(struct mtd_info *mtd)
298{
299 struct mtdoops_context *cxt = &oops_cxt;
300
301 if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
302 return;
303
304 cxt->mtd = NULL;
305 flush_scheduled_work();
306}
307
Richard Purdie8691a722007-07-10 20:33:54 +0100308static void mtdoops_console_sync(void)
309{
310 struct mtdoops_context *cxt = &oops_cxt;
311 struct mtd_info *mtd = cxt->mtd;
Richard Purdie47c152b2008-01-29 10:21:56 +0000312 unsigned long flags;
Richard Purdie8691a722007-07-10 20:33:54 +0100313
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000314 if (!cxt->ready || !mtd || cxt->writecount == 0)
Richard Purdie8691a722007-07-10 20:33:54 +0100315 return;
316
Richard Purdie47c152b2008-01-29 10:21:56 +0000317 /*
318 * Once ready is 0 and we've held the lock no further writes to the
319 * buffer will happen
320 */
321 spin_lock_irqsave(&cxt->writecount_lock, flags);
322 if (!cxt->ready) {
323 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
324 return;
325 }
Richard Purdie8691a722007-07-10 20:33:54 +0100326 cxt->ready = 0;
Richard Purdie47c152b2008-01-29 10:21:56 +0000327 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
Richard Purdie8691a722007-07-10 20:33:54 +0100328
Richard Purdie621e4f82008-02-06 10:17:50 +0000329 if (mtd->panic_write && in_interrupt())
330 /* Interrupt context, we're going to panic so try and log */
331 mtdoops_write(cxt, 1);
332 else
333 schedule_work(&cxt->work_write);
Richard Purdie8691a722007-07-10 20:33:54 +0100334}
Richard Purdie4b23aff2007-05-29 13:31:42 +0100335
336static void
337mtdoops_console_write(struct console *co, const char *s, unsigned int count)
338{
339 struct mtdoops_context *cxt = co->data;
340 struct mtd_info *mtd = cxt->mtd;
Richard Purdie47c152b2008-01-29 10:21:56 +0000341 unsigned long flags;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100342
Richard Purdie8691a722007-07-10 20:33:54 +0100343 if (!oops_in_progress) {
344 mtdoops_console_sync();
Richard Purdie4b23aff2007-05-29 13:31:42 +0100345 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100346 }
347
Richard Purdie8691a722007-07-10 20:33:54 +0100348 if (!cxt->ready || !mtd)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100349 return;
350
Richard Purdie47c152b2008-01-29 10:21:56 +0000351 /* Locking on writecount ensures sequential writes to the buffer */
352 spin_lock_irqsave(&cxt->writecount_lock, flags);
353
354 /* Check ready status didn't change whilst waiting for the lock */
355 if (!cxt->ready)
356 return;
357
Richard Purdie4b23aff2007-05-29 13:31:42 +0100358 if (cxt->writecount == 0) {
359 u32 *stamp = cxt->oops_buf;
360 *stamp = cxt->nextcount;
361 cxt->writecount = 4;
362 }
363
364 if ((count + cxt->writecount) > OOPS_PAGE_SIZE)
365 count = OOPS_PAGE_SIZE - cxt->writecount;
366
Peter Korsgaard235d6202007-11-06 11:56:02 +0100367 memcpy(cxt->oops_buf + cxt->writecount, s, count);
368 cxt->writecount += count;
Richard Purdie47c152b2008-01-29 10:21:56 +0000369
370 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
371
372 if (cxt->writecount == OOPS_PAGE_SIZE)
373 mtdoops_console_sync();
Richard Purdie4b23aff2007-05-29 13:31:42 +0100374}
375
376static int __init mtdoops_console_setup(struct console *co, char *options)
377{
378 struct mtdoops_context *cxt = co->data;
379
380 if (cxt->mtd_index != -1)
381 return -EBUSY;
382 if (co->index == -1)
383 return -EINVAL;
384
385 cxt->mtd_index = co->index;
386 return 0;
387}
388
389static struct mtd_notifier mtdoops_notifier = {
390 .add = mtdoops_notify_add,
391 .remove = mtdoops_notify_remove,
392};
393
394static struct console mtdoops_console = {
395 .name = "ttyMTD",
396 .write = mtdoops_console_write,
397 .setup = mtdoops_console_setup,
Richard Purdie8691a722007-07-10 20:33:54 +0100398 .unblank = mtdoops_console_sync,
Richard Purdie4b23aff2007-05-29 13:31:42 +0100399 .index = -1,
400 .data = &oops_cxt,
401};
402
403static int __init mtdoops_console_init(void)
404{
405 struct mtdoops_context *cxt = &oops_cxt;
406
407 cxt->mtd_index = -1;
408 cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE);
409
410 if (!cxt->oops_buf) {
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000411 printk(KERN_ERR "Failed to allocate mtdoops buffer workspace\n");
Richard Purdie4b23aff2007-05-29 13:31:42 +0100412 return -ENOMEM;
413 }
414
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000415 INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
416 INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100417
418 register_console(&mtdoops_console);
419 register_mtd_user(&mtdoops_notifier);
420 return 0;
421}
422
423static void __exit mtdoops_console_exit(void)
424{
425 struct mtdoops_context *cxt = &oops_cxt;
426
427 unregister_mtd_user(&mtdoops_notifier);
428 unregister_console(&mtdoops_console);
429 vfree(cxt->oops_buf);
430}
431
432
433subsys_initcall(mtdoops_console_init);
434module_exit(mtdoops_console_exit);
435
436MODULE_LICENSE("GPL");
437MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
438MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");