blob: 4ced68be7ed7ef267116895905826f524fb4e402 [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Richard Purdie4b23aff2007-05-29 13:31:42 +01002/*
3 * MTD Oops/Panic logger
4 *
David Woodhousea1452a32010-08-08 20:58:20 +01005 * Copyright © 2007 Nokia Corporation. All rights reserved.
Richard Purdie4b23aff2007-05-29 13:31:42 +01006 *
7 * Author: Richard Purdie <rpurdie@openedhand.com>
Richard Purdie4b23aff2007-05-29 13:31:42 +01008 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/console.h>
13#include <linux/vmalloc.h>
14#include <linux/workqueue.h>
15#include <linux/sched.h>
16#include <linux/wait.h>
Richard Purdie621e4f82008-02-06 10:17:50 +000017#include <linux/delay.h>
David Woodhousef9f7dd22008-02-07 10:50:57 +000018#include <linux/interrupt.h>
Richard Purdie4b23aff2007-05-29 13:31:42 +010019#include <linux/mtd/mtd.h>
Simon Kagstrom2e386e42009-11-03 14:19:03 +010020#include <linux/kmsg_dump.h>
Richard Purdie4b23aff2007-05-29 13:31:42 +010021
Simon Kagstrom1114e3d2009-11-03 08:08:41 +020022/* Maximum MTD partition size */
23#define MTDOOPS_MAX_MTD_SIZE (8 * 1024 * 1024)
24
Richard Purdief0482ee2008-07-26 09:22:45 +010025#define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
Simon Kagstrom2e386e42009-11-03 14:19:03 +010026#define MTDOOPS_HEADER_SIZE 8
Simon Kagstrom9507b0c2009-10-29 13:41:19 +010027
28static unsigned long record_size = 4096;
29module_param(record_size, ulong, 0400);
30MODULE_PARM_DESC(record_size,
31 "record size for MTD OOPS pages in bytes (default 4096)");
Richard Purdie4b23aff2007-05-29 13:31:42 +010032
Simon Kagstrom2e386e42009-11-03 14:19:03 +010033static char mtddev[80];
34module_param_string(mtddev, mtddev, 80, 0400);
35MODULE_PARM_DESC(mtddev,
36 "name or index number of the MTD device to use");
37
38static int dump_oops = 1;
39module_param(dump_oops, int, 0600);
40MODULE_PARM_DESC(dump_oops,
41 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
42
Adrian Bunk7903cba2008-04-18 13:44:11 -070043static struct mtdoops_context {
Simon Kagstrom2e386e42009-11-03 14:19:03 +010044 struct kmsg_dumper dump;
45
Richard Purdie4b23aff2007-05-29 13:31:42 +010046 int mtd_index;
Richard Purdie6ce0a8562008-01-29 11:27:11 +000047 struct work_struct work_erase;
48 struct work_struct work_write;
Richard Purdie4b23aff2007-05-29 13:31:42 +010049 struct mtd_info *mtd;
50 int oops_pages;
51 int nextpage;
52 int nextcount;
Simon Kagstrombe957452009-10-29 13:41:11 +010053 unsigned long *oops_page_used;
Richard Purdie4b23aff2007-05-29 13:31:42 +010054
55 void *oops_buf;
Richard Purdie4b23aff2007-05-29 13:31:42 +010056} 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
Simon Kagstrombe957452009-10-29 13:41:11 +010073static int mtdoops_erase_block(struct mtdoops_context *cxt, int offset)
Richard Purdie4b23aff2007-05-29 13:31:42 +010074{
Simon Kagstrombe957452009-10-29 13:41:11 +010075 struct mtd_info *mtd = cxt->mtd;
76 u32 start_page_offset = mtd_div_by_eb(offset, mtd) * mtd->erasesize;
Simon Kagstrom9507b0c2009-10-29 13:41:19 +010077 u32 start_page = start_page_offset / record_size;
78 u32 erase_pages = mtd->erasesize / record_size;
Richard Purdie4b23aff2007-05-29 13:31:42 +010079 struct erase_info erase;
Richard Purdie4b23aff2007-05-29 13:31:42 +010080 int ret;
Simon Kagstrombe957452009-10-29 13:41:11 +010081 int page;
Richard Purdie4b23aff2007-05-29 13:31:42 +010082
Richard Purdie4b23aff2007-05-29 13:31:42 +010083 erase.addr = offset;
Richard Purdie79dcd8e2008-01-29 10:25:55 +000084 erase.len = mtd->erasesize;
Richard Purdie4b23aff2007-05-29 13:31:42 +010085
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +020086 ret = mtd_erase(mtd, &erase);
Richard Purdie4b23aff2007-05-29 13:31:42 +010087 if (ret) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +030088 printk(KERN_WARNING "mtdoops: erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
89 (unsigned long long)erase.addr,
Simon Kagstrom2e386e42009-11-03 14:19:03 +010090 (unsigned long long)erase.len, mtddev);
Richard Purdie4b23aff2007-05-29 13:31:42 +010091 return ret;
92 }
93
Simon Kagstrombe957452009-10-29 13:41:11 +010094 /* Mark pages as unused */
95 for (page = start_page; page < start_page + erase_pages; page++)
96 mark_page_unused(cxt, page);
97
Richard Purdie4b23aff2007-05-29 13:31:42 +010098 return 0;
99}
100
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000101static void mtdoops_inc_counter(struct mtdoops_context *cxt)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100102{
Richard Purdie4b23aff2007-05-29 13:31:42 +0100103 cxt->nextpage++;
Richard Purdieecd5b312008-07-26 09:17:41 +0100104 if (cxt->nextpage >= cxt->oops_pages)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100105 cxt->nextpage = 0;
106 cxt->nextcount++;
107 if (cxt->nextcount == 0xffffffff)
108 cxt->nextcount = 0;
109
Simon Kagstrombe957452009-10-29 13:41:11 +0100110 if (page_is_used(cxt, cxt->nextpage)) {
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000111 schedule_work(&cxt->work_erase);
112 return;
113 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100114
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300115 printk(KERN_DEBUG "mtdoops: ready %d, %d (no erase)\n",
116 cxt->nextpage, cxt->nextcount);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100117}
118
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000119/* Scheduled work - when we can't proceed without erasing a block */
120static void mtdoops_workfunc_erase(struct work_struct *work)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100121{
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000122 struct mtdoops_context *cxt =
123 container_of(work, struct mtdoops_context, work_erase);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100124 struct mtd_info *mtd = cxt->mtd;
125 int i = 0, j, ret, mod;
126
127 /* We were unregistered */
128 if (!mtd)
129 return;
130
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100131 mod = (cxt->nextpage * record_size) % mtd->erasesize;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100132 if (mod != 0) {
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100133 cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / record_size);
Richard Purdieecd5b312008-07-26 09:17:41 +0100134 if (cxt->nextpage >= cxt->oops_pages)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100135 cxt->nextpage = 0;
136 }
137
Brian Norris9cb93fb2012-05-11 13:30:33 -0700138 while ((ret = mtd_block_isbad(mtd, cxt->nextpage * record_size)) > 0) {
Richard Purdie4b23aff2007-05-29 13:31:42 +0100139badblock:
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100140 printk(KERN_WARNING "mtdoops: bad block at %08lx\n",
141 cxt->nextpage * record_size);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100142 i++;
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100143 cxt->nextpage = cxt->nextpage + (mtd->erasesize / record_size);
Richard Purdieecd5b312008-07-26 09:17:41 +0100144 if (cxt->nextpage >= cxt->oops_pages)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100145 cxt->nextpage = 0;
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100146 if (i == cxt->oops_pages / (mtd->erasesize / record_size)) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300147 printk(KERN_ERR "mtdoops: all blocks bad!\n");
Richard Purdie4b23aff2007-05-29 13:31:42 +0100148 return;
149 }
150 }
151
Brian Norris9cb93fb2012-05-11 13:30:33 -0700152 if (ret < 0) {
153 printk(KERN_ERR "mtdoops: mtd_block_isbad failed, aborting\n");
154 return;
155 }
156
Richard Purdie4b23aff2007-05-29 13:31:42 +0100157 for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100158 ret = mtdoops_erase_block(cxt, cxt->nextpage * record_size);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100159
Richard Purdie2986bd22008-01-29 11:27:09 +0000160 if (ret >= 0) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300161 printk(KERN_DEBUG "mtdoops: ready %d, %d\n",
162 cxt->nextpage, cxt->nextcount);
Richard Purdie2986bd22008-01-29 11:27:09 +0000163 return;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100164 }
165
Artem Bityutskiybb4a0982012-02-03 11:23:57 +0200166 if (ret == -EIO) {
Artem Bityutskiy5942ddb2011-12-23 19:37:38 +0200167 ret = mtd_block_markbad(mtd, cxt->nextpage * record_size);
Artem Bityutskiybb4a0982012-02-03 11:23:57 +0200168 if (ret < 0 && ret != -EOPNOTSUPP) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300169 printk(KERN_ERR "mtdoops: block_markbad failed, aborting\n");
Richard Purdie2986bd22008-01-29 11:27:09 +0000170 return;
171 }
172 }
173 goto badblock;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100174}
175
Richard Purdie621e4f82008-02-06 10:17:50 +0000176static void mtdoops_write(struct mtdoops_context *cxt, int panic)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100177{
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000178 struct mtd_info *mtd = cxt->mtd;
179 size_t retlen;
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100180 u32 *hdr;
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000181 int ret;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100182
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100183 /* Add mtdoops header to the buffer */
184 hdr = cxt->oops_buf;
185 hdr[0] = cxt->nextcount;
186 hdr[1] = MTDOOPS_KERNMSG_MAGIC;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100187
Artem Bityutskiy016c1292011-12-28 17:27:18 +0200188 if (panic) {
Artem Bityutskiy7ae79d72011-12-23 18:03:17 +0200189 ret = mtd_panic_write(mtd, cxt->nextpage * record_size,
190 record_size, &retlen, cxt->oops_buf);
Artem Bityutskiy016c1292011-12-28 17:27:18 +0200191 if (ret == -EOPNOTSUPP) {
192 printk(KERN_ERR "mtdoops: Cannot write from panic without panic_write\n");
193 return;
194 }
195 } else
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200196 ret = mtd_write(mtd, cxt->nextpage * record_size,
197 record_size, &retlen, cxt->oops_buf);
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000198
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100199 if (retlen != record_size || ret < 0)
200 printk(KERN_ERR "mtdoops: write failure at %ld (%td of %ld written), error %d\n",
201 cxt->nextpage * record_size, retlen, record_size, ret);
Simon Kagstrombe957452009-10-29 13:41:11 +0100202 mark_page_used(cxt, cxt->nextpage);
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100203 memset(cxt->oops_buf, 0xff, record_size);
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000204
205 mtdoops_inc_counter(cxt);
Richard Purdie621e4f82008-02-06 10:17:50 +0000206}
207
Richard Purdie621e4f82008-02-06 10:17:50 +0000208static void mtdoops_workfunc_write(struct work_struct *work)
209{
210 struct mtdoops_context *cxt =
211 container_of(work, struct mtdoops_context, work_write);
212
213 mtdoops_write(cxt, 0);
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300214}
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000215
216static void find_next_position(struct mtdoops_context *cxt)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100217{
218 struct mtd_info *mtd = cxt->mtd;
Richard Purdie2986bd22008-01-29 11:27:09 +0000219 int ret, page, maxpos = 0;
Richard Purdief0482ee2008-07-26 09:22:45 +0100220 u32 count[2], maxcount = 0xffffffff;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100221 size_t retlen;
222
223 for (page = 0; page < cxt->oops_pages; page++) {
Artem Bityutskiybb4a0982012-02-03 11:23:57 +0200224 if (mtd_block_isbad(mtd, page * record_size))
Roman Tereshonkov3538c562011-12-02 15:07:17 +0200225 continue;
Simon Kagstrombe957452009-10-29 13:41:11 +0100226 /* Assume the page is used */
227 mark_page_used(cxt, page);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200228 ret = mtd_read(mtd, page * record_size, MTDOOPS_HEADER_SIZE,
229 &retlen, (u_char *)&count[0]);
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100230 if (retlen != MTDOOPS_HEADER_SIZE ||
Brian Norrisd57f40542011-09-20 18:34:25 -0700231 (ret < 0 && !mtd_is_bitflip(ret))) {
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100232 printk(KERN_ERR "mtdoops: read failure at %ld (%td of %d read), err %d\n",
233 page * record_size, retlen,
234 MTDOOPS_HEADER_SIZE, ret);
Richard Purdie2986bd22008-01-29 11:27:09 +0000235 continue;
236 }
237
Simon Kagstrombe957452009-10-29 13:41:11 +0100238 if (count[0] == 0xffffffff && count[1] == 0xffffffff)
239 mark_page_unused(cxt, page);
Matthieu CASTETcd409c62012-10-19 17:29:33 +0200240 if (count[0] == 0xffffffff || count[1] != MTDOOPS_KERNMSG_MAGIC)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100241 continue;
242 if (maxcount == 0xffffffff) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100243 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100244 maxpos = page;
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300245 } else if (count[0] < 0x40000000 && maxcount > 0xc0000000) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100246 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100247 maxpos = page;
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300248 } else if (count[0] > maxcount && count[0] < 0xc0000000) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100249 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100250 maxpos = page;
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300251 } else if (count[0] > maxcount && count[0] > 0xc0000000
252 && maxcount > 0x80000000) {
Richard Purdief0482ee2008-07-26 09:22:45 +0100253 maxcount = count[0];
Richard Purdie4b23aff2007-05-29 13:31:42 +0100254 maxpos = page;
255 }
256 }
257 if (maxcount == 0xffffffff) {
Matthieu CASTETcd409c62012-10-19 17:29:33 +0200258 cxt->nextpage = cxt->oops_pages - 1;
259 cxt->nextcount = 0;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100260 }
Matthieu CASTETcd409c62012-10-19 17:29:33 +0200261 else {
262 cxt->nextpage = maxpos;
263 cxt->nextcount = maxcount;
264 }
Richard Purdie4b23aff2007-05-29 13:31:42 +0100265
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000266 mtdoops_inc_counter(cxt);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100267}
268
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100269static void mtdoops_do_dump(struct kmsg_dumper *dumper,
Kay Sieverse2ae7152012-06-15 14:07:51 +0200270 enum kmsg_dump_reason reason)
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100271{
272 struct mtdoops_context *cxt = container_of(dumper,
273 struct mtdoops_context, dump);
Seiji Aguchifc2d5572011-01-12 16:59:29 -0800274
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100275 /* Only dump oopses if dump_oops is set */
276 if (reason == KMSG_DUMP_OOPS && !dump_oops)
277 return;
278
Kay Sieverse2ae7152012-06-15 14:07:51 +0200279 kmsg_dump_get_buffer(dumper, true, cxt->oops_buf + MTDOOPS_HEADER_SIZE,
280 record_size - MTDOOPS_HEADER_SIZE, NULL);
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100281
282 /* Panics must be written immediately */
Artem Bityutskiy016c1292011-12-28 17:27:18 +0200283 if (reason != KMSG_DUMP_OOPS)
284 mtdoops_write(cxt, 1);
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100285
286 /* For other cases, schedule work to write it "nicely" */
287 schedule_work(&cxt->work_write);
288}
Richard Purdie4b23aff2007-05-29 13:31:42 +0100289
290static void mtdoops_notify_add(struct mtd_info *mtd)
291{
292 struct mtdoops_context *cxt = &oops_cxt;
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100293 u64 mtdoops_pages = div_u64(mtd->size, record_size);
294 int err;
Simon Kagstrombe957452009-10-29 13:41:11 +0100295
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100296 if (!strcmp(mtd->name, mtddev))
Adrian Huntere2a0f252009-02-16 18:21:35 +0200297 cxt->mtd_index = mtd->index;
298
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300299 if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100300 return;
301
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300302 if (mtd->size < mtd->erasesize * 2) {
303 printk(KERN_ERR "mtdoops: MTD partition %d not big enough for mtdoops\n",
304 mtd->index);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100305 return;
306 }
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100307 if (mtd->erasesize < record_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 }
Simon Kagstrom1114e3d2009-11-03 08:08:41 +0200312 if (mtd->size > MTDOOPS_MAX_MTD_SIZE) {
313 printk(KERN_ERR "mtdoops: mtd%d is too large (limit is %d MiB)\n",
314 mtd->index, MTDOOPS_MAX_MTD_SIZE / 1024 / 1024);
315 return;
316 }
317
Simon Kagstrombe957452009-10-29 13:41:11 +0100318 /* oops_page_used is a bit field */
Kees Cook42bc47b2018-06-12 14:27:11 -0700319 cxt->oops_page_used =
320 vmalloc(array_size(sizeof(unsigned long),
321 DIV_ROUND_UP(mtdoops_pages,
322 BITS_PER_LONG)));
Simon Kagstrombe957452009-10-29 13:41:11 +0100323 if (!cxt->oops_page_used) {
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100324 printk(KERN_ERR "mtdoops: could not allocate page array\n");
325 return;
326 }
327
Kay Sieverse2ae7152012-06-15 14:07:51 +0200328 cxt->dump.max_reason = KMSG_DUMP_OOPS;
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100329 cxt->dump.dump = mtdoops_do_dump;
330 err = kmsg_dump_register(&cxt->dump);
331 if (err) {
332 printk(KERN_ERR "mtdoops: registering kmsg dumper failed, error %d\n", err);
333 vfree(cxt->oops_page_used);
334 cxt->oops_page_used = NULL;
Simon Kagstrombe957452009-10-29 13:41:11 +0100335 return;
336 }
Simon Kagstrom1114e3d2009-11-03 08:08:41 +0200337
Richard Purdie4b23aff2007-05-29 13:31:42 +0100338 cxt->mtd = mtd;
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100339 cxt->oops_pages = (int)mtd->size / record_size;
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000340 find_next_position(cxt);
Richard Purdie79dcd8e2008-01-29 10:25:55 +0000341 printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100342}
343
344static void mtdoops_notify_remove(struct mtd_info *mtd)
345{
346 struct mtdoops_context *cxt = &oops_cxt;
347
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300348 if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100349 return;
350
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100351 if (kmsg_dump_unregister(&cxt->dump) < 0)
352 printk(KERN_WARNING "mtdoops: could not unregister kmsg_dumper\n");
353
Richard Purdie4b23aff2007-05-29 13:31:42 +0100354 cxt->mtd = NULL;
Tejun Heo43829732012-08-20 14:51:24 -0700355 flush_work(&cxt->work_erase);
356 flush_work(&cxt->work_write);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100357}
358
Richard Purdie4b23aff2007-05-29 13:31:42 +0100359
360static struct mtd_notifier mtdoops_notifier = {
361 .add = mtdoops_notify_add,
362 .remove = mtdoops_notify_remove,
363};
364
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100365static int __init mtdoops_init(void)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100366{
367 struct mtdoops_context *cxt = &oops_cxt;
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100368 int mtd_index;
369 char *endp;
Richard Purdie4b23aff2007-05-29 13:31:42 +0100370
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100371 if (strlen(mtddev) == 0) {
372 printk(KERN_ERR "mtdoops: mtd device (mtddev=name/number) must be supplied\n");
373 return -EINVAL;
374 }
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100375 if ((record_size & 4095) != 0) {
376 printk(KERN_ERR "mtdoops: record_size must be a multiple of 4096\n");
377 return -EINVAL;
378 }
379 if (record_size < 4096) {
380 printk(KERN_ERR "mtdoops: record_size must be over 4096 bytes\n");
381 return -EINVAL;
382 }
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100383
384 /* Setup the MTD device to use */
Richard Purdie4b23aff2007-05-29 13:31:42 +0100385 cxt->mtd_index = -1;
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100386 mtd_index = simple_strtoul(mtddev, &endp, 0);
387 if (*endp == '\0')
388 cxt->mtd_index = mtd_index;
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100389
Simon Kagstrom9507b0c2009-10-29 13:41:19 +0100390 cxt->oops_buf = vmalloc(record_size);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100391 if (!cxt->oops_buf) {
Artem Bityutskiya15b1242009-10-11 13:40:40 +0300392 printk(KERN_ERR "mtdoops: failed to allocate buffer workspace\n");
Richard Purdie4b23aff2007-05-29 13:31:42 +0100393 return -ENOMEM;
394 }
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100395 memset(cxt->oops_buf, 0xff, record_size);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100396
Richard Purdie6ce0a8562008-01-29 11:27:11 +0000397 INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
398 INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100399
Richard Purdie4b23aff2007-05-29 13:31:42 +0100400 register_mtd_user(&mtdoops_notifier);
401 return 0;
402}
403
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100404static void __exit mtdoops_exit(void)
Richard Purdie4b23aff2007-05-29 13:31:42 +0100405{
406 struct mtdoops_context *cxt = &oops_cxt;
407
408 unregister_mtd_user(&mtdoops_notifier);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100409 vfree(cxt->oops_buf);
Simon Kagstrombe957452009-10-29 13:41:11 +0100410 vfree(cxt->oops_page_used);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100411}
412
413
Simon Kagstrom2e386e42009-11-03 14:19:03 +0100414module_init(mtdoops_init);
415module_exit(mtdoops_exit);
Richard Purdie4b23aff2007-05-29 13:31:42 +0100416
417MODULE_LICENSE("GPL");
418MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
419MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");