Blame view

drivers/mtd/mtdoops.c 11.8 KB
4b23aff08   Richard Purdie   [MTD] oops and pa...
1
2
3
  /*
   * MTD Oops/Panic logger
   *
a1452a377   David Woodhouse   mtd: Update copyr...
4
   * Copyright © 2007 Nokia Corporation. All rights reserved.
4b23aff08   Richard Purdie   [MTD] oops and pa...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   *
   * Author: Richard Purdie <rpurdie@openedhand.com>
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * version 2 as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA
   *
   */
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/console.h>
  #include <linux/vmalloc.h>
  #include <linux/workqueue.h>
  #include <linux/sched.h>
  #include <linux/wait.h>
621e4f8e9   Richard Purdie   [MTD] mtdoops: Us...
31
  #include <linux/delay.h>
f9f7dd222   David Woodhouse   [MTD] Fix mtdoops...
32
  #include <linux/interrupt.h>
4b23aff08   Richard Purdie   [MTD] oops and pa...
33
  #include <linux/mtd/mtd.h>
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
34
  #include <linux/kmsg_dump.h>
4b23aff08   Richard Purdie   [MTD] oops and pa...
35

1114e3d00   Simon Kagstrom   mtd: mtdoops: lim...
36
37
  /* Maximum MTD partition size */
  #define MTDOOPS_MAX_MTD_SIZE (8 * 1024 * 1024)
f0482ee36   Richard Purdie   [MTD] mtdoops: Ad...
38
  #define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
39
  #define MTDOOPS_HEADER_SIZE   8
9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
40
41
42
43
44
  
  static unsigned long record_size = 4096;
  module_param(record_size, ulong, 0400);
  MODULE_PARM_DESC(record_size,
  		"record size for MTD OOPS pages in bytes (default 4096)");
4b23aff08   Richard Purdie   [MTD] oops and pa...
45

2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
46
47
48
49
50
51
52
53
54
  static char mtddev[80];
  module_param_string(mtddev, mtddev, 80, 0400);
  MODULE_PARM_DESC(mtddev,
  		"name or index number of the MTD device to use");
  
  static int dump_oops = 1;
  module_param(dump_oops, int, 0600);
  MODULE_PARM_DESC(dump_oops,
  		"set to 1 to dump oopses, 0 to only dump panics (default 1)");
7903cbabc   Adrian Bunk   [MTD] mtdoops.c: ...
55
  static struct mtdoops_context {
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
56
  	struct kmsg_dumper dump;
4b23aff08   Richard Purdie   [MTD] oops and pa...
57
  	int mtd_index;
6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
58
59
  	struct work_struct work_erase;
  	struct work_struct work_write;
4b23aff08   Richard Purdie   [MTD] oops and pa...
60
61
62
63
  	struct mtd_info *mtd;
  	int oops_pages;
  	int nextpage;
  	int nextcount;
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
64
  	unsigned long *oops_page_used;
4b23aff08   Richard Purdie   [MTD] oops and pa...
65
66
  
  	void *oops_buf;
4b23aff08   Richard Purdie   [MTD] oops and pa...
67
  } oops_cxt;
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  static void mark_page_used(struct mtdoops_context *cxt, int page)
  {
  	set_bit(page, cxt->oops_page_used);
  }
  
  static void mark_page_unused(struct mtdoops_context *cxt, int page)
  {
  	clear_bit(page, cxt->oops_page_used);
  }
  
  static int page_is_used(struct mtdoops_context *cxt, int page)
  {
  	return test_bit(page, cxt->oops_page_used);
  }
4b23aff08   Richard Purdie   [MTD] oops and pa...
82
83
84
85
86
  static void mtdoops_erase_callback(struct erase_info *done)
  {
  	wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
  	wake_up(wait_q);
  }
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
87
  static int mtdoops_erase_block(struct mtdoops_context *cxt, int offset)
4b23aff08   Richard Purdie   [MTD] oops and pa...
88
  {
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
89
90
  	struct mtd_info *mtd = cxt->mtd;
  	u32 start_page_offset = mtd_div_by_eb(offset, mtd) * mtd->erasesize;
9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
91
92
  	u32 start_page = start_page_offset / record_size;
  	u32 erase_pages = mtd->erasesize / record_size;
4b23aff08   Richard Purdie   [MTD] oops and pa...
93
94
95
96
  	struct erase_info erase;
  	DECLARE_WAITQUEUE(wait, current);
  	wait_queue_head_t wait_q;
  	int ret;
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
97
  	int page;
4b23aff08   Richard Purdie   [MTD] oops and pa...
98
99
100
101
102
  
  	init_waitqueue_head(&wait_q);
  	erase.mtd = mtd;
  	erase.callback = mtdoops_erase_callback;
  	erase.addr = offset;
79dcd8e9e   Richard Purdie   [MTD] mtdoops: Va...
103
  	erase.len = mtd->erasesize;
4b23aff08   Richard Purdie   [MTD] oops and pa...
104
105
106
107
  	erase.priv = (u_long)&wait_q;
  
  	set_current_state(TASK_INTERRUPTIBLE);
  	add_wait_queue(&wait_q, &wait);
7e1f0dc05   Artem Bityutskiy   mtd: introduce mt...
108
  	ret = mtd_erase(mtd, &erase);
4b23aff08   Richard Purdie   [MTD] oops and pa...
109
110
111
  	if (ret) {
  		set_current_state(TASK_RUNNING);
  		remove_wait_queue(&wait_q, &wait);
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
112
113
114
  		printk(KERN_WARNING "mtdoops: erase of region [0x%llx, 0x%llx] on \"%s\" failed
  ",
  		       (unsigned long long)erase.addr,
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
115
  		       (unsigned long long)erase.len, mtddev);
4b23aff08   Richard Purdie   [MTD] oops and pa...
116
117
118
119
120
  		return ret;
  	}
  
  	schedule();  /* Wait for erase to finish. */
  	remove_wait_queue(&wait_q, &wait);
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
121
122
123
  	/* Mark pages as unused */
  	for (page = start_page; page < start_page + erase_pages; page++)
  		mark_page_unused(cxt, page);
4b23aff08   Richard Purdie   [MTD] oops and pa...
124
125
  	return 0;
  }
6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
126
  static void mtdoops_inc_counter(struct mtdoops_context *cxt)
4b23aff08   Richard Purdie   [MTD] oops and pa...
127
  {
4b23aff08   Richard Purdie   [MTD] oops and pa...
128
  	cxt->nextpage++;
ecd5b3102   Richard Purdie   [MTD] mtdoops: Fi...
129
  	if (cxt->nextpage >= cxt->oops_pages)
4b23aff08   Richard Purdie   [MTD] oops and pa...
130
131
132
133
  		cxt->nextpage = 0;
  	cxt->nextcount++;
  	if (cxt->nextcount == 0xffffffff)
  		cxt->nextcount = 0;
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
134
  	if (page_is_used(cxt, cxt->nextpage)) {
6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
135
136
137
  		schedule_work(&cxt->work_erase);
  		return;
  	}
4b23aff08   Richard Purdie   [MTD] oops and pa...
138

a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
139
140
141
  	printk(KERN_DEBUG "mtdoops: ready %d, %d (no erase)
  ",
  	       cxt->nextpage, cxt->nextcount);
4b23aff08   Richard Purdie   [MTD] oops and pa...
142
  }
6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
143
144
  /* Scheduled work - when we can't proceed without erasing a block */
  static void mtdoops_workfunc_erase(struct work_struct *work)
4b23aff08   Richard Purdie   [MTD] oops and pa...
145
  {
6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
146
147
  	struct mtdoops_context *cxt =
  			container_of(work, struct mtdoops_context, work_erase);
4b23aff08   Richard Purdie   [MTD] oops and pa...
148
149
150
151
152
153
  	struct mtd_info *mtd = cxt->mtd;
  	int i = 0, j, ret, mod;
  
  	/* We were unregistered */
  	if (!mtd)
  		return;
9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
154
  	mod = (cxt->nextpage * record_size) % mtd->erasesize;
4b23aff08   Richard Purdie   [MTD] oops and pa...
155
  	if (mod != 0) {
9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
156
  		cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / record_size);
ecd5b3102   Richard Purdie   [MTD] mtdoops: Fi...
157
  		if (cxt->nextpage >= cxt->oops_pages)
4b23aff08   Richard Purdie   [MTD] oops and pa...
158
159
  			cxt->nextpage = 0;
  	}
9cb93fbb5   Brian Norris   mtd: mtdoops: ref...
160
  	while ((ret = mtd_block_isbad(mtd, cxt->nextpage * record_size)) > 0) {
4b23aff08   Richard Purdie   [MTD] oops and pa...
161
  badblock:
9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
162
163
164
  		printk(KERN_WARNING "mtdoops: bad block at %08lx
  ",
  		       cxt->nextpage * record_size);
4b23aff08   Richard Purdie   [MTD] oops and pa...
165
  		i++;
9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
166
  		cxt->nextpage = cxt->nextpage + (mtd->erasesize / record_size);
ecd5b3102   Richard Purdie   [MTD] mtdoops: Fi...
167
  		if (cxt->nextpage >= cxt->oops_pages)
4b23aff08   Richard Purdie   [MTD] oops and pa...
168
  			cxt->nextpage = 0;
9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
169
  		if (i == cxt->oops_pages / (mtd->erasesize / record_size)) {
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
170
171
  			printk(KERN_ERR "mtdoops: all blocks bad!
  ");
4b23aff08   Richard Purdie   [MTD] oops and pa...
172
173
174
  			return;
  		}
  	}
9cb93fbb5   Brian Norris   mtd: mtdoops: ref...
175
176
177
178
179
  	if (ret < 0) {
  		printk(KERN_ERR "mtdoops: mtd_block_isbad failed, aborting
  ");
  		return;
  	}
4b23aff08   Richard Purdie   [MTD] oops and pa...
180
  	for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
181
  		ret = mtdoops_erase_block(cxt, cxt->nextpage * record_size);
4b23aff08   Richard Purdie   [MTD] oops and pa...
182

2986bd2a3   Richard Purdie   [MTD] mtdoops: Ad...
183
  	if (ret >= 0) {
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
184
185
186
  		printk(KERN_DEBUG "mtdoops: ready %d, %d
  ",
  		       cxt->nextpage, cxt->nextcount);
2986bd2a3   Richard Purdie   [MTD] mtdoops: Ad...
187
  		return;
4b23aff08   Richard Purdie   [MTD] oops and pa...
188
  	}
bb4a09866   Artem Bityutskiy   mtdoops: clean-up...
189
  	if (ret == -EIO) {
5942ddbc5   Artem Bityutskiy   mtd: introduce mt...
190
  		ret = mtd_block_markbad(mtd, cxt->nextpage * record_size);
bb4a09866   Artem Bityutskiy   mtdoops: clean-up...
191
  		if (ret < 0 && ret != -EOPNOTSUPP) {
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
192
193
  			printk(KERN_ERR "mtdoops: block_markbad failed, aborting
  ");
2986bd2a3   Richard Purdie   [MTD] mtdoops: Ad...
194
195
196
197
  			return;
  		}
  	}
  	goto badblock;
4b23aff08   Richard Purdie   [MTD] oops and pa...
198
  }
621e4f8e9   Richard Purdie   [MTD] mtdoops: Us...
199
  static void mtdoops_write(struct mtdoops_context *cxt, int panic)
4b23aff08   Richard Purdie   [MTD] oops and pa...
200
  {
6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
201
202
  	struct mtd_info *mtd = cxt->mtd;
  	size_t retlen;
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
203
  	u32 *hdr;
6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
204
  	int ret;
4b23aff08   Richard Purdie   [MTD] oops and pa...
205

2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
206
207
208
209
  	/* Add mtdoops header to the buffer */
  	hdr = cxt->oops_buf;
  	hdr[0] = cxt->nextcount;
  	hdr[1] = MTDOOPS_KERNMSG_MAGIC;
6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
210

016c1291c   Artem Bityutskiy   mtd: mtdoops: do ...
211
  	if (panic) {
7ae79d7ff   Artem Bityutskiy   mtd: introduce mt...
212
213
  		ret = mtd_panic_write(mtd, cxt->nextpage * record_size,
  				      record_size, &retlen, cxt->oops_buf);
016c1291c   Artem Bityutskiy   mtd: mtdoops: do ...
214
215
216
217
218
219
  		if (ret == -EOPNOTSUPP) {
  			printk(KERN_ERR "mtdoops: Cannot write from panic without panic_write
  ");
  			return;
  		}
  	} else
eda95cbf7   Artem Bityutskiy   mtd: introduce mt...
220
221
  		ret = mtd_write(mtd, cxt->nextpage * record_size,
  				record_size, &retlen, cxt->oops_buf);
6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
222

9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
223
224
225
226
  	if (retlen != record_size || ret < 0)
  		printk(KERN_ERR "mtdoops: write failure at %ld (%td of %ld written), error %d
  ",
  		       cxt->nextpage * record_size, retlen, record_size, ret);
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
227
  	mark_page_used(cxt, cxt->nextpage);
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
228
  	memset(cxt->oops_buf, 0xff, record_size);
6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
229
230
  
  	mtdoops_inc_counter(cxt);
621e4f8e9   Richard Purdie   [MTD] mtdoops: Us...
231
  }
621e4f8e9   Richard Purdie   [MTD] mtdoops: Us...
232
233
234
235
236
237
  static void mtdoops_workfunc_write(struct work_struct *work)
  {
  	struct mtdoops_context *cxt =
  			container_of(work, struct mtdoops_context, work_write);
  
  	mtdoops_write(cxt, 0);
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
238
  }
4b23aff08   Richard Purdie   [MTD] oops and pa...
239

6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
240
  static void find_next_position(struct mtdoops_context *cxt)
4b23aff08   Richard Purdie   [MTD] oops and pa...
241
242
  {
  	struct mtd_info *mtd = cxt->mtd;
2986bd2a3   Richard Purdie   [MTD] mtdoops: Ad...
243
  	int ret, page, maxpos = 0;
f0482ee36   Richard Purdie   [MTD] mtdoops: Ad...
244
  	u32 count[2], maxcount = 0xffffffff;
4b23aff08   Richard Purdie   [MTD] oops and pa...
245
246
247
  	size_t retlen;
  
  	for (page = 0; page < cxt->oops_pages; page++) {
bb4a09866   Artem Bityutskiy   mtdoops: clean-up...
248
  		if (mtd_block_isbad(mtd, page * record_size))
3538c5632   Roman Tereshonkov   mtd: mtdoops: ski...
249
  			continue;
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
250
251
  		/* Assume the page is used */
  		mark_page_used(cxt, page);
329ad399a   Artem Bityutskiy   mtd: introduce mt...
252
253
  		ret = mtd_read(mtd, page * record_size, MTDOOPS_HEADER_SIZE,
  			       &retlen, (u_char *)&count[0]);
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
254
  		if (retlen != MTDOOPS_HEADER_SIZE ||
d57f40544   Brian Norris   mtd: utilize `mtd...
255
  				(ret < 0 && !mtd_is_bitflip(ret))) {
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
256
257
258
259
  			printk(KERN_ERR "mtdoops: read failure at %ld (%td of %d read), err %d
  ",
  			       page * record_size, retlen,
  			       MTDOOPS_HEADER_SIZE, ret);
2986bd2a3   Richard Purdie   [MTD] mtdoops: Ad...
260
261
  			continue;
  		}
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
262
263
  		if (count[0] == 0xffffffff && count[1] == 0xffffffff)
  			mark_page_unused(cxt, page);
cd409c612   Matthieu CASTET   mtdoops: don't er...
264
  		if (count[0] == 0xffffffff || count[1] != MTDOOPS_KERNMSG_MAGIC)
4b23aff08   Richard Purdie   [MTD] oops and pa...
265
266
  			continue;
  		if (maxcount == 0xffffffff) {
f0482ee36   Richard Purdie   [MTD] mtdoops: Ad...
267
  			maxcount = count[0];
4b23aff08   Richard Purdie   [MTD] oops and pa...
268
  			maxpos = page;
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
269
  		} else if (count[0] < 0x40000000 && maxcount > 0xc0000000) {
f0482ee36   Richard Purdie   [MTD] mtdoops: Ad...
270
  			maxcount = count[0];
4b23aff08   Richard Purdie   [MTD] oops and pa...
271
  			maxpos = page;
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
272
  		} else if (count[0] > maxcount && count[0] < 0xc0000000) {
f0482ee36   Richard Purdie   [MTD] mtdoops: Ad...
273
  			maxcount = count[0];
4b23aff08   Richard Purdie   [MTD] oops and pa...
274
  			maxpos = page;
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
275
276
  		} else if (count[0] > maxcount && count[0] > 0xc0000000
  					&& maxcount > 0x80000000) {
f0482ee36   Richard Purdie   [MTD] mtdoops: Ad...
277
  			maxcount = count[0];
4b23aff08   Richard Purdie   [MTD] oops and pa...
278
279
280
281
  			maxpos = page;
  		}
  	}
  	if (maxcount == 0xffffffff) {
cd409c612   Matthieu CASTET   mtdoops: don't er...
282
283
284
285
286
287
  		cxt->nextpage = cxt->oops_pages - 1;
  		cxt->nextcount = 0;
  	}
  	else {
  		cxt->nextpage = maxpos;
  		cxt->nextcount = maxcount;
4b23aff08   Richard Purdie   [MTD] oops and pa...
288
  	}
4b23aff08   Richard Purdie   [MTD] oops and pa...
289

6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
290
  	mtdoops_inc_counter(cxt);
4b23aff08   Richard Purdie   [MTD] oops and pa...
291
  }
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
292
  static void mtdoops_do_dump(struct kmsg_dumper *dumper,
e2ae715d6   Kay Sievers   kmsg - kmsg_dump(...
293
  			    enum kmsg_dump_reason reason)
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
294
295
296
  {
  	struct mtdoops_context *cxt = container_of(dumper,
  			struct mtdoops_context, dump);
fc2d557c7   Seiji Aguchi   kmsg_dump: constr...
297

2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
298
299
300
  	/* Only dump oopses if dump_oops is set */
  	if (reason == KMSG_DUMP_OOPS && !dump_oops)
  		return;
e2ae715d6   Kay Sievers   kmsg - kmsg_dump(...
301
302
  	kmsg_dump_get_buffer(dumper, true, cxt->oops_buf + MTDOOPS_HEADER_SIZE,
  			     record_size - MTDOOPS_HEADER_SIZE, NULL);
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
303
304
  
  	/* Panics must be written immediately */
016c1291c   Artem Bityutskiy   mtd: mtdoops: do ...
305
306
  	if (reason != KMSG_DUMP_OOPS)
  		mtdoops_write(cxt, 1);
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
307
308
309
310
  
  	/* For other cases, schedule work to write it "nicely" */
  	schedule_work(&cxt->work_write);
  }
4b23aff08   Richard Purdie   [MTD] oops and pa...
311
312
313
314
  
  static void mtdoops_notify_add(struct mtd_info *mtd)
  {
  	struct mtdoops_context *cxt = &oops_cxt;
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
315
316
  	u64 mtdoops_pages = div_u64(mtd->size, record_size);
  	int err;
4b23aff08   Richard Purdie   [MTD] oops and pa...
317

2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
318
  	if (!strcmp(mtd->name, mtddev))
e2a0f25b4   Adrian Hunter   [MTD] mtdoops: al...
319
  		cxt->mtd_index = mtd->index;
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
320
  	if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
4b23aff08   Richard Purdie   [MTD] oops and pa...
321
  		return;
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
322
323
324
325
  	if (mtd->size < mtd->erasesize * 2) {
  		printk(KERN_ERR "mtdoops: MTD partition %d not big enough for mtdoops
  ",
  		       mtd->index);
4b23aff08   Richard Purdie   [MTD] oops and pa...
326
327
  		return;
  	}
9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
328
  	if (mtd->erasesize < record_size) {
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
329
330
331
  		printk(KERN_ERR "mtdoops: eraseblock size of MTD partition %d too small
  ",
  		       mtd->index);
79dcd8e9e   Richard Purdie   [MTD] mtdoops: Va...
332
333
  		return;
  	}
1114e3d00   Simon Kagstrom   mtd: mtdoops: lim...
334
335
336
337
338
339
  	if (mtd->size > MTDOOPS_MAX_MTD_SIZE) {
  		printk(KERN_ERR "mtdoops: mtd%d is too large (limit is %d MiB)
  ",
  		       mtd->index, MTDOOPS_MAX_MTD_SIZE / 1024 / 1024);
  		return;
  	}
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
340
341
  	/* oops_page_used is a bit field */
  	cxt->oops_page_used = vmalloc(DIV_ROUND_UP(mtdoops_pages,
556f06358   Roman Tereshonkov   mtdoops: fix the ...
342
  			BITS_PER_LONG) * sizeof(unsigned long));
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
343
  	if (!cxt->oops_page_used) {
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
344
345
346
347
  		printk(KERN_ERR "mtdoops: could not allocate page array
  ");
  		return;
  	}
e2ae715d6   Kay Sievers   kmsg - kmsg_dump(...
348
  	cxt->dump.max_reason = KMSG_DUMP_OOPS;
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
349
350
351
352
353
354
355
  	cxt->dump.dump = mtdoops_do_dump;
  	err = kmsg_dump_register(&cxt->dump);
  	if (err) {
  		printk(KERN_ERR "mtdoops: registering kmsg dumper failed, error %d
  ", err);
  		vfree(cxt->oops_page_used);
  		cxt->oops_page_used = NULL;
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
356
357
  		return;
  	}
4b23aff08   Richard Purdie   [MTD] oops and pa...
358

1114e3d00   Simon Kagstrom   mtd: mtdoops: lim...
359
  	cxt->mtd = mtd;
9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
360
  	cxt->oops_pages = (int)mtd->size / record_size;
6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
361
  	find_next_position(cxt);
79dcd8e9e   Richard Purdie   [MTD] mtdoops: Va...
362
363
  	printk(KERN_INFO "mtdoops: Attached to MTD device %d
  ", mtd->index);
4b23aff08   Richard Purdie   [MTD] oops and pa...
364
365
366
367
368
  }
  
  static void mtdoops_notify_remove(struct mtd_info *mtd)
  {
  	struct mtdoops_context *cxt = &oops_cxt;
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
369
  	if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
4b23aff08   Richard Purdie   [MTD] oops and pa...
370
  		return;
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
371
372
373
  	if (kmsg_dump_unregister(&cxt->dump) < 0)
  		printk(KERN_WARNING "mtdoops: could not unregister kmsg_dumper
  ");
4b23aff08   Richard Purdie   [MTD] oops and pa...
374
  	cxt->mtd = NULL;
43829731d   Tejun Heo   workqueue: deprec...
375
376
  	flush_work(&cxt->work_erase);
  	flush_work(&cxt->work_write);
4b23aff08   Richard Purdie   [MTD] oops and pa...
377
  }
4b23aff08   Richard Purdie   [MTD] oops and pa...
378
379
380
381
382
  
  static struct mtd_notifier mtdoops_notifier = {
  	.add	= mtdoops_notify_add,
  	.remove	= mtdoops_notify_remove,
  };
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
383
  static int __init mtdoops_init(void)
4b23aff08   Richard Purdie   [MTD] oops and pa...
384
385
  {
  	struct mtdoops_context *cxt = &oops_cxt;
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
386
387
  	int mtd_index;
  	char *endp;
4b23aff08   Richard Purdie   [MTD] oops and pa...
388

2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
389
390
391
392
393
  	if (strlen(mtddev) == 0) {
  		printk(KERN_ERR "mtdoops: mtd device (mtddev=name/number) must be supplied
  ");
  		return -EINVAL;
  	}
9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
394
395
396
397
398
399
400
401
402
403
  	if ((record_size & 4095) != 0) {
  		printk(KERN_ERR "mtdoops: record_size must be a multiple of 4096
  ");
  		return -EINVAL;
  	}
  	if (record_size < 4096) {
  		printk(KERN_ERR "mtdoops: record_size must be over 4096 bytes
  ");
  		return -EINVAL;
  	}
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
404
405
  
  	/* Setup the MTD device to use */
4b23aff08   Richard Purdie   [MTD] oops and pa...
406
  	cxt->mtd_index = -1;
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
407
408
409
  	mtd_index = simple_strtoul(mtddev, &endp, 0);
  	if (*endp == '\0')
  		cxt->mtd_index = mtd_index;
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
410

9507b0c83   Simon Kagstrom   mtd: mtdoops: mak...
411
  	cxt->oops_buf = vmalloc(record_size);
4b23aff08   Richard Purdie   [MTD] oops and pa...
412
  	if (!cxt->oops_buf) {
a15b124fc   Artem Bityutskiy   mtd: mtdoops: sev...
413
414
  		printk(KERN_ERR "mtdoops: failed to allocate buffer workspace
  ");
4b23aff08   Richard Purdie   [MTD] oops and pa...
415
416
  		return -ENOMEM;
  	}
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
417
  	memset(cxt->oops_buf, 0xff, record_size);
4b23aff08   Richard Purdie   [MTD] oops and pa...
418

6ce0a856c   Richard Purdie   [MTD] mtdoops: Pe...
419
420
  	INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
  	INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
4b23aff08   Richard Purdie   [MTD] oops and pa...
421

4b23aff08   Richard Purdie   [MTD] oops and pa...
422
423
424
  	register_mtd_user(&mtdoops_notifier);
  	return 0;
  }
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
425
  static void __exit mtdoops_exit(void)
4b23aff08   Richard Purdie   [MTD] oops and pa...
426
427
428
429
  {
  	struct mtdoops_context *cxt = &oops_cxt;
  
  	unregister_mtd_user(&mtdoops_notifier);
4b23aff08   Richard Purdie   [MTD] oops and pa...
430
  	vfree(cxt->oops_buf);
be95745f0   Simon Kagstrom   mtd: mtdoops: kee...
431
  	vfree(cxt->oops_page_used);
4b23aff08   Richard Purdie   [MTD] oops and pa...
432
  }
2e386e4ba   Simon Kagstrom   mtd: mtdoops: ref...
433
434
  module_init(mtdoops_init);
  module_exit(mtdoops_exit);
4b23aff08   Richard Purdie   [MTD] oops and pa...
435
436
437
438
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
  MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");