blob: 06c53824945535babb30edbc0bc3ad0605bbcf26 [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;
Simon Kagstrombe957452009-10-29 13:41:11 +010047 unsigned long *oops_page_used;
Adrian Huntere2a0f252009-02-16 18:21:35 +020048 char *name;
Richard Purdie4b23aff2007-05-29 13:31:42 +010049
50 void *oops_buf;
Richard Purdie47c152b2008-01-29 10:21:56 +000051
52 /* writecount and disabling ready are spin lock protected */
53 spinlock_t writecount_lock;
Richard Purdie4b23aff2007-05-29 13:31:42 +010054 int ready;
55 int writecount;
56} oops_cxt;
57
Simon Kagstrombe957452009-10-29 13:41:11 +010058static void mark_page_used(struct mtdoops_context *cxt, int page)
59{
60 set_bit(page, cxt->oops_page_used);
61}
62
63static void mark_page_unused(struct mtdoops_context *cxt, int page)
64{
65 clear_bit(page, cxt->oops_page_used);
66}
67
68static int page_is_used(struct mtdoops_context *cxt, int page)
69{
70 return test_bit(page, cxt->oops_page_used);
71}
72
Richard Purdie4b23aff2007-05-29 13:31:42 +010073static void mtdoops_erase_callback(struct erase_info *done)
74{
75 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
76 wake_up(wait_q);
77}
78
Simon Kagstrombe957452009-10-29 13:41:11 +010079static int mtdoops_erase_block(struct mtdoops_context *cxt, int offset)
Richard Purdie4b23aff2007-05-29 13:31:42 +010080{
Simon Kagstrombe957452009-10-29 13:41:11 +010081 struct mtd_info *mtd = cxt->mtd;
82 u32 start_page_offset = mtd_div_by_eb(offset, mtd) * mtd->erasesize;
83 u32 start_page = start_page_offset / OOPS_PAGE_SIZE;
84 u32 erase_pages = mtd->erasesize / OOPS_PAGE_SIZE;
Richard Purdie4b23aff2007-05-29 13:31:42 +010085 struct erase_info erase;
86 DECLARE_WAITQUEUE(wait, current);
87 wait_queue_head_t wait_q;
88 int ret;
Simon Kagstrombe957452009-10-29 13:41:11 +010089 int page;
Richard Purdie4b23aff2007-05-29 13:31:42 +010090
91 init_waitqueue_head(&wait_q);
92 erase.mtd = mtd;
93 erase.callback = mtdoops_erase_callback;
94 erase.addr = offset;
Richard Purdie79dcd8e2008-01-29 10:25:55 +000095 erase.len = mtd->erasesize;
Richard Purdie4b23aff2007-05-29 13:31:42 +010096 erase.priv = (u_long)&wait_q;
97
98 set_current_state(TASK_INTERRUPTIBLE);
99 add_wait_queue(&wait_q, &wait);
100
101 ret = mtd->erase(mtd, &erase);
102 if (ret) {
103 set_current_state(TASK_RUNNING);
104 remove_wait_queue(&wait_q, &wait);
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300105 printk(KERN_WARNING "mtdoops: erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
106 (unsigned long long)erase.addr,
107 (unsigned long long)erase.len, mtd->name);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100108 return ret;
109 }
110
111 schedule(); /* Wait for erase to finish. */
112 remove_wait_queue(&wait_q, &wait);
113
Simon Kagstrombe957452009-10-29 13:41:11 +0100114 /* Mark pages as unused */
115 for (page = start_page; page < start_page + erase_pages; page++)
116 mark_page_unused(cxt, page);
117
Richard Purdie4b23aff2007-05-29 13:31:42 +0100118 return 0;
119}
120
Richard Purdie6ce0a852008-01-29 11:27:11 +0000121static void mtdoops_inc_counter(struct mtdoops_context *cxt)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100122{
Richard Purdie4b23aff2007-05-29 13:31:42 +0100123 cxt->nextpage++;
Richard Purdieecd5b312008-07-26 09:17:41 +0100124 if (cxt->nextpage >= cxt->oops_pages)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100125 cxt->nextpage = 0;
126 cxt->nextcount++;
127 if (cxt->nextcount == 0xffffffff)
128 cxt->nextcount = 0;
129
Simon Kagstrombe957452009-10-29 13:41:11 +0100130 if (page_is_used(cxt, cxt->nextpage)) {
Richard Purdie6ce0a852008-01-29 11:27:11 +0000131 schedule_work(&cxt->work_erase);
132 return;
133 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100134
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300135 printk(KERN_DEBUG "mtdoops: ready %d, %d (no erase)\n",
136 cxt->nextpage, cxt->nextcount);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100137 cxt->ready = 1;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100138}
139
Richard Purdie6ce0a852008-01-29 11:27:11 +0000140/* Scheduled work - when we can't proceed without erasing a block */
141static void mtdoops_workfunc_erase(struct work_struct *work)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100142{
Richard Purdie6ce0a852008-01-29 11:27:11 +0000143 struct mtdoops_context *cxt =
144 container_of(work, struct mtdoops_context, work_erase);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100145 struct mtd_info *mtd = cxt->mtd;
146 int i = 0, j, ret, mod;
147
148 /* We were unregistered */
149 if (!mtd)
150 return;
151
152 mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize;
153 if (mod != 0) {
154 cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE);
Richard Purdieecd5b312008-07-26 09:17:41 +0100155 if (cxt->nextpage >= cxt->oops_pages)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100156 cxt->nextpage = 0;
157 }
158
Richard Purdie2986bd22008-01-29 11:27:09 +0000159 while (mtd->block_isbad) {
160 ret = mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
161 if (!ret)
162 break;
163 if (ret < 0) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300164 printk(KERN_ERR "mtdoops: block_isbad failed, aborting\n");
Richard Purdie2986bd22008-01-29 11:27:09 +0000165 return;
166 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100167badblock:
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300168 printk(KERN_WARNING "mtdoops: bad block at %08x\n",
169 cxt->nextpage * OOPS_PAGE_SIZE);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100170 i++;
171 cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE);
Richard Purdieecd5b312008-07-26 09:17:41 +0100172 if (cxt->nextpage >= cxt->oops_pages)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100173 cxt->nextpage = 0;
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300174 if (i == cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE)) {
175 printk(KERN_ERR "mtdoops: all blocks bad!\n");
Richard Purdie4b23aff2007-05-29 13:31:42 +0100176 return;
177 }
178 }
179
180 for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
Simon Kagstrombe957452009-10-29 13:41:11 +0100181 ret = mtdoops_erase_block(cxt, cxt->nextpage * OOPS_PAGE_SIZE);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100182
Richard Purdie2986bd22008-01-29 11:27:09 +0000183 if (ret >= 0) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300184 printk(KERN_DEBUG "mtdoops: ready %d, %d\n",
185 cxt->nextpage, cxt->nextcount);
Richard Purdie2986bd22008-01-29 11:27:09 +0000186 cxt->ready = 1;
187 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100188 }
189
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300190 if (mtd->block_markbad && ret == -EIO) {
Richard Purdie2986bd22008-01-29 11:27:09 +0000191 ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
192 if (ret < 0) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300193 printk(KERN_ERR "mtdoops: block_markbad failed, aborting\n");
Richard Purdie2986bd22008-01-29 11:27:09 +0000194 return;
195 }
196 }
197 goto badblock;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100198}
199
Richard Purdie621e4f82008-02-06 10:17:50 +0000200static void mtdoops_write(struct mtdoops_context *cxt, int panic)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100201{
Richard Purdie6ce0a852008-01-29 11:27:11 +0000202 struct mtd_info *mtd = cxt->mtd;
203 size_t retlen;
204 int ret;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100205
Richard Purdie6ce0a852008-01-29 11:27:11 +0000206 if (cxt->writecount < OOPS_PAGE_SIZE)
207 memset(cxt->oops_buf + cxt->writecount, 0xff,
208 OOPS_PAGE_SIZE - cxt->writecount);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100209
Richard Purdie621e4f82008-02-06 10:17:50 +0000210 if (panic)
211 ret = mtd->panic_write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
212 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
213 else
214 ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
Richard Purdie6ce0a852008-01-29 11:27:11 +0000215 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
216
217 cxt->writecount = 0;
218
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300219 if (retlen != OOPS_PAGE_SIZE || ret < 0)
220 printk(KERN_ERR "mtdoops: write failure at %d (%td of %d written), error %d\n",
221 cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret);
Simon Kagstrombe957452009-10-29 13:41:11 +0100222 mark_page_used(cxt, cxt->nextpage);
Richard Purdie6ce0a852008-01-29 11:27:11 +0000223
224 mtdoops_inc_counter(cxt);
Richard Purdie621e4f82008-02-06 10:17:50 +0000225}
226
227
228static void mtdoops_workfunc_write(struct work_struct *work)
229{
230 struct mtdoops_context *cxt =
231 container_of(work, struct mtdoops_context, work_write);
232
233 mtdoops_write(cxt, 0);
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300234}
Richard Purdie6ce0a852008-01-29 11:27:11 +0000235
236static void find_next_position(struct mtdoops_context *cxt)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100237{
238 struct mtd_info *mtd = cxt->mtd;
Richard Purdie2986bd22008-01-29 11:27:09 +0000239 int ret, page, maxpos = 0;
Richard Purdief0482ee2008-07-26 09:22:45 +0100240 u32 count[2], maxcount = 0xffffffff;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100241 size_t retlen;
242
243 for (page = 0; page < cxt->oops_pages; page++) {
Simon Kagstrombe957452009-10-29 13:41:11 +0100244 /* Assume the page is used */
245 mark_page_used(cxt, page);
Richard Purdief0482ee2008-07-26 09:22:45 +0100246 ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 8, &retlen, (u_char *) &count[0]);
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300247 if (retlen != 8 || (ret < 0 && ret != -EUCLEAN)) {
248 printk(KERN_ERR "mtdoops: read failure at %d (%td of 8 read), err %d\n",
249 page * OOPS_PAGE_SIZE, retlen, ret);
Richard Purdie2986bd22008-01-29 11:27:09 +0000250 continue;
251 }
252
Simon Kagstrombe957452009-10-29 13:41:11 +0100253 if (count[0] == 0xffffffff && count[1] == 0xffffffff)
254 mark_page_unused(cxt, page);
Richard Purdief0482ee2008-07-26 09:22:45 +0100255 if (count[1] != MTDOOPS_KERNMSG_MAGIC)
256 continue;
257 if (count[0] == 0xffffffff)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100258 continue;
259 if (maxcount == 0xffffffff) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100260 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100261 maxpos = page;
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300262 } else if (count[0] < 0x40000000 && maxcount > 0xc0000000) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100263 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100264 maxpos = page;
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300265 } else if (count[0] > maxcount && count[0] < 0xc0000000) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100266 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100267 maxpos = page;
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300268 } else if (count[0] > maxcount && count[0] > 0xc0000000
269 && maxcount > 0x80000000) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100270 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100271 maxpos = page;
272 }
273 }
274 if (maxcount == 0xffffffff) {
275 cxt->nextpage = 0;
276 cxt->nextcount = 1;
Richard Purdie43b56932008-07-26 09:25:18 +0100277 schedule_work(&cxt->work_erase);
Richard Purdie6ce0a852008-01-29 11:27:11 +0000278 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100279 }
280
281 cxt->nextpage = maxpos;
282 cxt->nextcount = maxcount;
283
Richard Purdie6ce0a852008-01-29 11:27:11 +0000284 mtdoops_inc_counter(cxt);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100285}
286
287
288static void mtdoops_notify_add(struct mtd_info *mtd)
289{
290 struct mtdoops_context *cxt = &oops_cxt;
Simon Kagstrombe957452009-10-29 13:41:11 +0100291 u64 mtdoops_pages = mtd->size;
292
293 do_div(mtdoops_pages, OOPS_PAGE_SIZE);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100294
Adrian Huntere2a0f252009-02-16 18:21:35 +0200295 if (cxt->name && !strcmp(mtd->name, cxt->name))
296 cxt->mtd_index = mtd->index;
297
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300298 if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100299 return;
300
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300301 if (mtd->size < mtd->erasesize * 2) {
302 printk(KERN_ERR "mtdoops: MTD partition %d not big enough for mtdoops\n",
303 mtd->index);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100304 return;
305 }
306
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000307 if (mtd->erasesize < OOPS_PAGE_SIZE) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300308 printk(KERN_ERR "mtdoops: eraseblock size of MTD partition %d too small\n",
309 mtd->index);
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000310 return;
311 }
312
Simon Kagstrombe957452009-10-29 13:41:11 +0100313 /* oops_page_used is a bit field */
314 cxt->oops_page_used = vmalloc(DIV_ROUND_UP(mtdoops_pages,
315 BITS_PER_LONG));
316 if (!cxt->oops_page_used) {
317 printk(KERN_ERR "Could not allocate page array\n");
318 return;
319 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100320 cxt->mtd = mtd;
Adrian Hunter69423d92008-12-10 13:37:21 +0000321 if (mtd->size > INT_MAX)
322 cxt->oops_pages = INT_MAX / OOPS_PAGE_SIZE;
323 else
324 cxt->oops_pages = (int)mtd->size / OOPS_PAGE_SIZE;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100325
Richard Purdie6ce0a852008-01-29 11:27:11 +0000326 find_next_position(cxt);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100327
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000328 printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100329}
330
331static void mtdoops_notify_remove(struct mtd_info *mtd)
332{
333 struct mtdoops_context *cxt = &oops_cxt;
334
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300335 if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100336 return;
337
338 cxt->mtd = NULL;
339 flush_scheduled_work();
340}
341
Richard Purdie8691a722007-07-10 20:33:54 +0100342static void mtdoops_console_sync(void)
343{
344 struct mtdoops_context *cxt = &oops_cxt;
345 struct mtd_info *mtd = cxt->mtd;
Richard Purdie47c152b2008-01-29 10:21:56 +0000346 unsigned long flags;
Richard Purdie8691a722007-07-10 20:33:54 +0100347
Richard Purdie6ce0a852008-01-29 11:27:11 +0000348 if (!cxt->ready || !mtd || cxt->writecount == 0)
Richard Purdie8691a722007-07-10 20:33:54 +0100349 return;
350
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300351 /*
352 * Once ready is 0 and we've held the lock no further writes to the
Richard Purdie47c152b2008-01-29 10:21:56 +0000353 * buffer will happen
354 */
355 spin_lock_irqsave(&cxt->writecount_lock, flags);
356 if (!cxt->ready) {
357 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
358 return;
359 }
Richard Purdie8691a722007-07-10 20:33:54 +0100360 cxt->ready = 0;
Richard Purdie47c152b2008-01-29 10:21:56 +0000361 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
Richard Purdie8691a722007-07-10 20:33:54 +0100362
Richard Purdie621e4f82008-02-06 10:17:50 +0000363 if (mtd->panic_write && in_interrupt())
364 /* Interrupt context, we're going to panic so try and log */
365 mtdoops_write(cxt, 1);
366 else
367 schedule_work(&cxt->work_write);
Richard Purdie8691a722007-07-10 20:33:54 +0100368}
Richard Purdie4b23aff2007-05-29 13:31:42 +0100369
370static void
371mtdoops_console_write(struct console *co, const char *s, unsigned int count)
372{
373 struct mtdoops_context *cxt = co->data;
374 struct mtd_info *mtd = cxt->mtd;
Richard Purdie47c152b2008-01-29 10:21:56 +0000375 unsigned long flags;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100376
Richard Purdie8691a722007-07-10 20:33:54 +0100377 if (!oops_in_progress) {
378 mtdoops_console_sync();
Richard Purdie4b23aff2007-05-29 13:31:42 +0100379 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100380 }
381
Richard Purdie8691a722007-07-10 20:33:54 +0100382 if (!cxt->ready || !mtd)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100383 return;
384
Richard Purdie47c152b2008-01-29 10:21:56 +0000385 /* Locking on writecount ensures sequential writes to the buffer */
386 spin_lock_irqsave(&cxt->writecount_lock, flags);
387
388 /* Check ready status didn't change whilst waiting for the lock */
Adrian Hunter48ec00a2009-03-04 09:53:40 +0200389 if (!cxt->ready) {
390 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
Richard Purdie47c152b2008-01-29 10:21:56 +0000391 return;
Adrian Hunter48ec00a2009-03-04 09:53:40 +0200392 }
Richard Purdie47c152b2008-01-29 10:21:56 +0000393
Richard Purdie4b23aff2007-05-29 13:31:42 +0100394 if (cxt->writecount == 0) {
395 u32 *stamp = cxt->oops_buf;
Richard Purdief0482ee2008-07-26 09:22:45 +0100396 *stamp++ = cxt->nextcount;
397 *stamp = MTDOOPS_KERNMSG_MAGIC;
398 cxt->writecount = 8;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100399 }
400
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300401 if (count + cxt->writecount > OOPS_PAGE_SIZE)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100402 count = OOPS_PAGE_SIZE - cxt->writecount;
403
Peter Korsgaard235d6202007-11-06 11:56:02 +0100404 memcpy(cxt->oops_buf + cxt->writecount, s, count);
405 cxt->writecount += count;
Richard Purdie47c152b2008-01-29 10:21:56 +0000406
407 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
408
409 if (cxt->writecount == OOPS_PAGE_SIZE)
410 mtdoops_console_sync();
Richard Purdie4b23aff2007-05-29 13:31:42 +0100411}
412
413static int __init mtdoops_console_setup(struct console *co, char *options)
414{
415 struct mtdoops_context *cxt = co->data;
416
Adrian Huntere2a0f252009-02-16 18:21:35 +0200417 if (cxt->mtd_index != -1 || cxt->name)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100418 return -EBUSY;
Adrian Huntere2a0f252009-02-16 18:21:35 +0200419 if (options) {
420 cxt->name = kstrdup(options, GFP_KERNEL);
421 return 0;
422 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100423 if (co->index == -1)
424 return -EINVAL;
425
426 cxt->mtd_index = co->index;
427 return 0;
428}
429
430static struct mtd_notifier mtdoops_notifier = {
431 .add = mtdoops_notify_add,
432 .remove = mtdoops_notify_remove,
433};
434
435static struct console mtdoops_console = {
436 .name = "ttyMTD",
437 .write = mtdoops_console_write,
438 .setup = mtdoops_console_setup,
Richard Purdie8691a722007-07-10 20:33:54 +0100439 .unblank = mtdoops_console_sync,
Richard Purdie4b23aff2007-05-29 13:31:42 +0100440 .index = -1,
441 .data = &oops_cxt,
442};
443
444static int __init mtdoops_console_init(void)
445{
446 struct mtdoops_context *cxt = &oops_cxt;
447
448 cxt->mtd_index = -1;
449 cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100450 if (!cxt->oops_buf) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300451 printk(KERN_ERR "mtdoops: failed to allocate buffer workspace\n");
Richard Purdie4b23aff2007-05-29 13:31:42 +0100452 return -ENOMEM;
453 }
454
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300455 spin_lock_init(&cxt->writecount_lock);
Richard Purdie6ce0a852008-01-29 11:27:11 +0000456 INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
457 INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100458
459 register_console(&mtdoops_console);
460 register_mtd_user(&mtdoops_notifier);
461 return 0;
462}
463
464static void __exit mtdoops_console_exit(void)
465{
466 struct mtdoops_context *cxt = &oops_cxt;
467
468 unregister_mtd_user(&mtdoops_notifier);
469 unregister_console(&mtdoops_console);
Adrian Huntere2a0f252009-02-16 18:21:35 +0200470 kfree(cxt->name);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100471 vfree(cxt->oops_buf);
Simon Kagstrombe957452009-10-29 13:41:11 +0100472 vfree(cxt->oops_page_used);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100473}
474
475
476subsys_initcall(mtdoops_console_init);
477module_exit(mtdoops_console_exit);
478
479MODULE_LICENSE("GPL");
480MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
481MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");