blob: 64772dc0ea2bbe0f40d0cbff42b3cddadb52ceb2 [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
Simon Kagstrom1114e3d2009-11-03 08:08:41 +020036/* Maximum MTD partition size */
37#define MTDOOPS_MAX_MTD_SIZE (8 * 1024 * 1024)
38
Richard Purdief0482ee2008-07-26 09:22:45 +010039#define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
Simon Kagstrom9507b0c2009-10-29 13:41:19 +010040
41static unsigned long record_size = 4096;
42module_param(record_size, ulong, 0400);
43MODULE_PARM_DESC(record_size,
44 "record size for MTD OOPS pages in bytes (default 4096)");
Richard Purdie4b23aff2007-05-29 13:31:42 +010045
Adrian Bunk7903cba2008-04-18 13:44:11 -070046static struct mtdoops_context {
Richard Purdie4b23aff2007-05-29 13:31:42 +010047 int mtd_index;
Richard Purdie6ce0a8562008-01-29 11:27:11 +000048 struct work_struct work_erase;
49 struct work_struct work_write;
Richard Purdie4b23aff2007-05-29 13:31:42 +010050 struct mtd_info *mtd;
51 int oops_pages;
52 int nextpage;
53 int nextcount;
Simon Kagstrombe957452009-10-29 13:41:11 +010054 unsigned long *oops_page_used;
Adrian Huntere2a0f252009-02-16 18:21:35 +020055 char *name;
Richard Purdie4b23aff2007-05-29 13:31:42 +010056
57 void *oops_buf;
Richard Purdie47c152b2008-01-29 10:21:56 +000058
59 /* writecount and disabling ready are spin lock protected */
60 spinlock_t writecount_lock;
Richard Purdie4b23aff2007-05-29 13:31:42 +010061 int ready;
62 int writecount;
63} oops_cxt;
64
Simon Kagstrombe957452009-10-29 13:41:11 +010065static void mark_page_used(struct mtdoops_context *cxt, int page)
66{
67 set_bit(page, cxt->oops_page_used);
68}
69
70static void mark_page_unused(struct mtdoops_context *cxt, int page)
71{
72 clear_bit(page, cxt->oops_page_used);
73}
74
75static int page_is_used(struct mtdoops_context *cxt, int page)
76{
77 return test_bit(page, cxt->oops_page_used);
78}
79
Richard Purdie4b23aff2007-05-29 13:31:42 +010080static void mtdoops_erase_callback(struct erase_info *done)
81{
82 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
83 wake_up(wait_q);
84}
85
Simon Kagstrombe957452009-10-29 13:41:11 +010086static int mtdoops_erase_block(struct mtdoops_context *cxt, int offset)
Richard Purdie4b23aff2007-05-29 13:31:42 +010087{
Simon Kagstrombe957452009-10-29 13:41:11 +010088 struct mtd_info *mtd = cxt->mtd;
89 u32 start_page_offset = mtd_div_by_eb(offset, mtd) * mtd->erasesize;
Simon Kagstrom9507b0c2009-10-29 13:41:19 +010090 u32 start_page = start_page_offset / record_size;
91 u32 erase_pages = mtd->erasesize / record_size;
Richard Purdie4b23aff2007-05-29 13:31:42 +010092 struct erase_info erase;
93 DECLARE_WAITQUEUE(wait, current);
94 wait_queue_head_t wait_q;
95 int ret;
Simon Kagstrombe957452009-10-29 13:41:11 +010096 int page;
Richard Purdie4b23aff2007-05-29 13:31:42 +010097
98 init_waitqueue_head(&wait_q);
99 erase.mtd = mtd;
100 erase.callback = mtdoops_erase_callback;
101 erase.addr = offset;
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000102 erase.len = mtd->erasesize;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100103 erase.priv = (u_long)&wait_q;
104
105 set_current_state(TASK_INTERRUPTIBLE);
106 add_wait_queue(&wait_q, &wait);
107
108 ret = mtd->erase(mtd, &erase);
109 if (ret) {
110 set_current_state(TASK_RUNNING);
111 remove_wait_queue(&wait_q, &wait);
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300112 printk(KERN_WARNING "mtdoops: erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
113 (unsigned long long)erase.addr,
114 (unsigned long long)erase.len, mtd->name);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100115 return ret;
116 }
117
118 schedule(); /* Wait for erase to finish. */
119 remove_wait_queue(&wait_q, &wait);
120
Simon Kagstrombe957452009-10-29 13:41:11 +0100121 /* Mark pages as unused */
122 for (page = start_page; page < start_page + erase_pages; page++)
123 mark_page_unused(cxt, page);
124
Richard Purdie4b23aff2007-05-29 13:31:42 +0100125 return 0;
126}
127
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000128static void mtdoops_inc_counter(struct mtdoops_context *cxt)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100129{
Richard Purdie4b23aff2007-05-29 13:31:42 +0100130 cxt->nextpage++;
Richard Purdieecd5b312008-07-26 09:17:41 +0100131 if (cxt->nextpage >= cxt->oops_pages)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100132 cxt->nextpage = 0;
133 cxt->nextcount++;
134 if (cxt->nextcount == 0xffffffff)
135 cxt->nextcount = 0;
136
Simon Kagstrombe957452009-10-29 13:41:11 +0100137 if (page_is_used(cxt, cxt->nextpage)) {
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000138 schedule_work(&cxt->work_erase);
139 return;
140 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100141
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300142 printk(KERN_DEBUG "mtdoops: ready %d, %d (no erase)\n",
143 cxt->nextpage, cxt->nextcount);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100144 cxt->ready = 1;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100145}
146
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000147/* Scheduled work - when we can't proceed without erasing a block */
148static void mtdoops_workfunc_erase(struct work_struct *work)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100149{
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000150 struct mtdoops_context *cxt =
151 container_of(work, struct mtdoops_context, work_erase);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100152 struct mtd_info *mtd = cxt->mtd;
153 int i = 0, j, ret, mod;
154
155 /* We were unregistered */
156 if (!mtd)
157 return;
158
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100159 mod = (cxt->nextpage * record_size) % mtd->erasesize;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100160 if (mod != 0) {
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100161 cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / record_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 }
165
Richard Purdie2986bd22008-01-29 11:27:09 +0000166 while (mtd->block_isbad) {
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100167 ret = mtd->block_isbad(mtd, cxt->nextpage * record_size);
Richard Purdie2986bd22008-01-29 11:27:09 +0000168 if (!ret)
169 break;
170 if (ret < 0) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300171 printk(KERN_ERR "mtdoops: block_isbad failed, aborting\n");
Richard Purdie2986bd22008-01-29 11:27:09 +0000172 return;
173 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100174badblock:
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100175 printk(KERN_WARNING "mtdoops: bad block at %08lx\n",
176 cxt->nextpage * record_size);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100177 i++;
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100178 cxt->nextpage = cxt->nextpage + (mtd->erasesize / record_size);
Richard Purdieecd5b312008-07-26 09:17:41 +0100179 if (cxt->nextpage >= cxt->oops_pages)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100180 cxt->nextpage = 0;
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100181 if (i == cxt->oops_pages / (mtd->erasesize / record_size)) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300182 printk(KERN_ERR "mtdoops: all blocks bad!\n");
Richard Purdie4b23aff2007-05-29 13:31:42 +0100183 return;
184 }
185 }
186
187 for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100188 ret = mtdoops_erase_block(cxt, cxt->nextpage * record_size);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100189
Richard Purdie2986bd22008-01-29 11:27:09 +0000190 if (ret >= 0) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300191 printk(KERN_DEBUG "mtdoops: ready %d, %d\n",
192 cxt->nextpage, cxt->nextcount);
Richard Purdie2986bd22008-01-29 11:27:09 +0000193 cxt->ready = 1;
194 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100195 }
196
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300197 if (mtd->block_markbad && ret == -EIO) {
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100198 ret = mtd->block_markbad(mtd, cxt->nextpage * record_size);
Richard Purdie2986bd22008-01-29 11:27:09 +0000199 if (ret < 0) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300200 printk(KERN_ERR "mtdoops: block_markbad failed, aborting\n");
Richard Purdie2986bd22008-01-29 11:27:09 +0000201 return;
202 }
203 }
204 goto badblock;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100205}
206
Richard Purdie621e4f82008-02-06 10:17:50 +0000207static void mtdoops_write(struct mtdoops_context *cxt, int panic)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100208{
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000209 struct mtd_info *mtd = cxt->mtd;
210 size_t retlen;
211 int ret;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100212
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100213 if (cxt->writecount < record_size)
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000214 memset(cxt->oops_buf + cxt->writecount, 0xff,
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100215 record_size - cxt->writecount);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100216
Richard Purdie621e4f82008-02-06 10:17:50 +0000217 if (panic)
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100218 ret = mtd->panic_write(mtd, cxt->nextpage * record_size,
219 record_size, &retlen, cxt->oops_buf);
Richard Purdie621e4f82008-02-06 10:17:50 +0000220 else
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100221 ret = mtd->write(mtd, cxt->nextpage * record_size,
222 record_size, &retlen, cxt->oops_buf);
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000223
224 cxt->writecount = 0;
225
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100226 if (retlen != record_size || ret < 0)
227 printk(KERN_ERR "mtdoops: write failure at %ld (%td of %ld written), error %d\n",
228 cxt->nextpage * record_size, retlen, record_size, ret);
Simon Kagstrombe957452009-10-29 13:41:11 +0100229 mark_page_used(cxt, cxt->nextpage);
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000230
231 mtdoops_inc_counter(cxt);
Richard Purdie621e4f82008-02-06 10:17:50 +0000232}
233
234
235static void mtdoops_workfunc_write(struct work_struct *work)
236{
237 struct mtdoops_context *cxt =
238 container_of(work, struct mtdoops_context, work_write);
239
240 mtdoops_write(cxt, 0);
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300241}
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000242
243static void find_next_position(struct mtdoops_context *cxt)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100244{
245 struct mtd_info *mtd = cxt->mtd;
Richard Purdie2986bd22008-01-29 11:27:09 +0000246 int ret, page, maxpos = 0;
Richard Purdief0482ee2008-07-26 09:22:45 +0100247 u32 count[2], maxcount = 0xffffffff;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100248 size_t retlen;
249
250 for (page = 0; page < cxt->oops_pages; page++) {
Simon Kagstrombe957452009-10-29 13:41:11 +0100251 /* Assume the page is used */
252 mark_page_used(cxt, page);
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100253 ret = mtd->read(mtd, page * record_size, 8, &retlen, (u_char *) &count[0]);
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300254 if (retlen != 8 || (ret < 0 && ret != -EUCLEAN)) {
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100255 printk(KERN_ERR "mtdoops: read failure at %ld (%td of 8 read), err %d\n",
256 page * record_size, retlen, ret);
Richard Purdie2986bd22008-01-29 11:27:09 +0000257 continue;
258 }
259
Simon Kagstrombe957452009-10-29 13:41:11 +0100260 if (count[0] == 0xffffffff && count[1] == 0xffffffff)
261 mark_page_unused(cxt, page);
Richard Purdief0482ee2008-07-26 09:22:45 +0100262 if (count[1] != MTDOOPS_KERNMSG_MAGIC)
263 continue;
264 if (count[0] == 0xffffffff)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100265 continue;
266 if (maxcount == 0xffffffff) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100267 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100268 maxpos = page;
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300269 } else if (count[0] < 0x40000000 && maxcount > 0xc0000000) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100270 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100271 maxpos = page;
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300272 } else if (count[0] > maxcount && count[0] < 0xc0000000) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100273 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100274 maxpos = page;
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300275 } else if (count[0] > maxcount && count[0] > 0xc0000000
276 && maxcount > 0x80000000) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100277 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100278 maxpos = page;
279 }
280 }
281 if (maxcount == 0xffffffff) {
282 cxt->nextpage = 0;
283 cxt->nextcount = 1;
Richard Purdie43b56932008-07-26 09:25:18 +0100284 schedule_work(&cxt->work_erase);
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000285 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100286 }
287
288 cxt->nextpage = maxpos;
289 cxt->nextcount = maxcount;
290
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000291 mtdoops_inc_counter(cxt);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100292}
293
294
295static void mtdoops_notify_add(struct mtd_info *mtd)
296{
297 struct mtdoops_context *cxt = &oops_cxt;
Simon Kagstrombe957452009-10-29 13:41:11 +0100298 u64 mtdoops_pages = mtd->size;
299
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100300 do_div(mtdoops_pages, record_size);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100301
Adrian Huntere2a0f252009-02-16 18:21:35 +0200302 if (cxt->name && !strcmp(mtd->name, cxt->name))
303 cxt->mtd_index = mtd->index;
304
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300305 if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100306 return;
307
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300308 if (mtd->size < mtd->erasesize * 2) {
309 printk(KERN_ERR "mtdoops: MTD partition %d not big enough for mtdoops\n",
310 mtd->index);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100311 return;
312 }
313
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100314 if (mtd->erasesize < record_size) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300315 printk(KERN_ERR "mtdoops: eraseblock size of MTD partition %d too small\n",
316 mtd->index);
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000317 return;
318 }
319
Simon Kagstrom1114e3d2009-11-03 08:08:41 +0200320 if (mtd->size > MTDOOPS_MAX_MTD_SIZE) {
321 printk(KERN_ERR "mtdoops: mtd%d is too large (limit is %d MiB)\n",
322 mtd->index, MTDOOPS_MAX_MTD_SIZE / 1024 / 1024);
323 return;
324 }
325
Simon Kagstrombe957452009-10-29 13:41:11 +0100326 /* oops_page_used is a bit field */
327 cxt->oops_page_used = vmalloc(DIV_ROUND_UP(mtdoops_pages,
328 BITS_PER_LONG));
329 if (!cxt->oops_page_used) {
330 printk(KERN_ERR "Could not allocate page array\n");
331 return;
332 }
Simon Kagstrom1114e3d2009-11-03 08:08:41 +0200333
Richard Purdie4b23aff2007-05-29 13:31:42 +0100334 cxt->mtd = mtd;
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100335 cxt->oops_pages = (int)mtd->size / record_size;
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000336 find_next_position(cxt);
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000337 printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100338}
339
340static void mtdoops_notify_remove(struct mtd_info *mtd)
341{
342 struct mtdoops_context *cxt = &oops_cxt;
343
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300344 if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100345 return;
346
347 cxt->mtd = NULL;
348 flush_scheduled_work();
349}
350
Richard Purdie8691a722007-07-10 20:33:54 +0100351static void mtdoops_console_sync(void)
352{
353 struct mtdoops_context *cxt = &oops_cxt;
354 struct mtd_info *mtd = cxt->mtd;
Richard Purdie47c152b2008-01-29 10:21:56 +0000355 unsigned long flags;
Richard Purdie8691a722007-07-10 20:33:54 +0100356
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000357 if (!cxt->ready || !mtd || cxt->writecount == 0)
Richard Purdie8691a722007-07-10 20:33:54 +0100358 return;
359
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300360 /*
361 * Once ready is 0 and we've held the lock no further writes to the
Richard Purdie47c152b2008-01-29 10:21:56 +0000362 * buffer will happen
363 */
364 spin_lock_irqsave(&cxt->writecount_lock, flags);
365 if (!cxt->ready) {
366 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
367 return;
368 }
Richard Purdie8691a722007-07-10 20:33:54 +0100369 cxt->ready = 0;
Richard Purdie47c152b2008-01-29 10:21:56 +0000370 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
Richard Purdie8691a722007-07-10 20:33:54 +0100371
Richard Purdie621e4f82008-02-06 10:17:50 +0000372 if (mtd->panic_write && in_interrupt())
373 /* Interrupt context, we're going to panic so try and log */
374 mtdoops_write(cxt, 1);
375 else
376 schedule_work(&cxt->work_write);
Richard Purdie8691a722007-07-10 20:33:54 +0100377}
Richard Purdie4b23aff2007-05-29 13:31:42 +0100378
379static void
380mtdoops_console_write(struct console *co, const char *s, unsigned int count)
381{
382 struct mtdoops_context *cxt = co->data;
383 struct mtd_info *mtd = cxt->mtd;
Richard Purdie47c152b2008-01-29 10:21:56 +0000384 unsigned long flags;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100385
Richard Purdie8691a722007-07-10 20:33:54 +0100386 if (!oops_in_progress) {
387 mtdoops_console_sync();
Richard Purdie4b23aff2007-05-29 13:31:42 +0100388 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100389 }
390
Richard Purdie8691a722007-07-10 20:33:54 +0100391 if (!cxt->ready || !mtd)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100392 return;
393
Richard Purdie47c152b2008-01-29 10:21:56 +0000394 /* Locking on writecount ensures sequential writes to the buffer */
395 spin_lock_irqsave(&cxt->writecount_lock, flags);
396
397 /* Check ready status didn't change whilst waiting for the lock */
Adrian Hunter48ec00a2009-03-04 09:53:40 +0200398 if (!cxt->ready) {
399 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
Richard Purdie47c152b2008-01-29 10:21:56 +0000400 return;
Adrian Hunter48ec00a2009-03-04 09:53:40 +0200401 }
Richard Purdie47c152b2008-01-29 10:21:56 +0000402
Richard Purdie4b23aff2007-05-29 13:31:42 +0100403 if (cxt->writecount == 0) {
404 u32 *stamp = cxt->oops_buf;
Richard Purdief0482ee2008-07-26 09:22:45 +0100405 *stamp++ = cxt->nextcount;
406 *stamp = MTDOOPS_KERNMSG_MAGIC;
407 cxt->writecount = 8;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100408 }
409
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100410 if (count + cxt->writecount > record_size)
411 count = record_size - cxt->writecount;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100412
Peter Korsgaard235d6202007-11-06 11:56:02 +0100413 memcpy(cxt->oops_buf + cxt->writecount, s, count);
414 cxt->writecount += count;
Richard Purdie47c152b2008-01-29 10:21:56 +0000415
416 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
417
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100418 if (cxt->writecount == record_size)
Richard Purdie47c152b2008-01-29 10:21:56 +0000419 mtdoops_console_sync();
Richard Purdie4b23aff2007-05-29 13:31:42 +0100420}
421
422static int __init mtdoops_console_setup(struct console *co, char *options)
423{
424 struct mtdoops_context *cxt = co->data;
425
Adrian Huntere2a0f252009-02-16 18:21:35 +0200426 if (cxt->mtd_index != -1 || cxt->name)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100427 return -EBUSY;
Adrian Huntere2a0f252009-02-16 18:21:35 +0200428 if (options) {
429 cxt->name = kstrdup(options, GFP_KERNEL);
430 return 0;
431 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100432 if (co->index == -1)
433 return -EINVAL;
434
435 cxt->mtd_index = co->index;
436 return 0;
437}
438
439static struct mtd_notifier mtdoops_notifier = {
440 .add = mtdoops_notify_add,
441 .remove = mtdoops_notify_remove,
442};
443
444static struct console mtdoops_console = {
445 .name = "ttyMTD",
446 .write = mtdoops_console_write,
447 .setup = mtdoops_console_setup,
Richard Purdie8691a722007-07-10 20:33:54 +0100448 .unblank = mtdoops_console_sync,
Richard Purdie4b23aff2007-05-29 13:31:42 +0100449 .index = -1,
450 .data = &oops_cxt,
451};
452
453static int __init mtdoops_console_init(void)
454{
455 struct mtdoops_context *cxt = &oops_cxt;
456
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100457 if ((record_size & 4095) != 0) {
458 printk(KERN_ERR "mtdoops: record_size must be a multiple of 4096\n");
459 return -EINVAL;
460 }
461 if (record_size < 4096) {
462 printk(KERN_ERR "mtdoops: record_size must be over 4096 bytes\n");
463 return -EINVAL;
464 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100465 cxt->mtd_index = -1;
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100466 cxt->oops_buf = vmalloc(record_size);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100467 if (!cxt->oops_buf) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300468 printk(KERN_ERR "mtdoops: failed to allocate buffer workspace\n");
Richard Purdie4b23aff2007-05-29 13:31:42 +0100469 return -ENOMEM;
470 }
471
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300472 spin_lock_init(&cxt->writecount_lock);
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000473 INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
474 INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100475
476 register_console(&mtdoops_console);
477 register_mtd_user(&mtdoops_notifier);
478 return 0;
479}
480
481static void __exit mtdoops_console_exit(void)
482{
483 struct mtdoops_context *cxt = &oops_cxt;
484
485 unregister_mtd_user(&mtdoops_notifier);
486 unregister_console(&mtdoops_console);
Adrian Huntere2a0f252009-02-16 18:21:35 +0200487 kfree(cxt->name);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100488 vfree(cxt->oops_buf);
Simon Kagstrombe957452009-10-29 13:41:11 +0100489 vfree(cxt->oops_page_used);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100490}
491
492
493subsys_initcall(mtdoops_console_init);
494module_exit(mtdoops_console_exit);
495
496MODULE_LICENSE("GPL");
497MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
498MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");