Commit | Line | Data |
---|---|---|
4b23aff0 RP |
1 | /* |
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> | |
31 | #include <linux/mtd/mtd.h> | |
32 | ||
33 | #define OOPS_PAGE_SIZE 4096 | |
34 | ||
6ce0a856 | 35 | struct mtdoops_context { |
4b23aff0 | 36 | int mtd_index; |
6ce0a856 RP |
37 | struct work_struct work_erase; |
38 | struct work_struct work_write; | |
4b23aff0 RP |
39 | struct mtd_info *mtd; |
40 | int oops_pages; | |
41 | int nextpage; | |
42 | int nextcount; | |
43 | ||
44 | void *oops_buf; | |
45 | int ready; | |
46 | int writecount; | |
47 | } oops_cxt; | |
48 | ||
49 | static void mtdoops_erase_callback(struct erase_info *done) | |
50 | { | |
51 | wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv; | |
52 | wake_up(wait_q); | |
53 | } | |
54 | ||
55 | static int mtdoops_erase_block(struct mtd_info *mtd, int offset) | |
56 | { | |
57 | struct erase_info erase; | |
58 | DECLARE_WAITQUEUE(wait, current); | |
59 | wait_queue_head_t wait_q; | |
60 | int ret; | |
61 | ||
62 | init_waitqueue_head(&wait_q); | |
63 | erase.mtd = mtd; | |
64 | erase.callback = mtdoops_erase_callback; | |
65 | erase.addr = offset; | |
66 | if (mtd->erasesize < OOPS_PAGE_SIZE) | |
67 | erase.len = OOPS_PAGE_SIZE; | |
68 | else | |
69 | erase.len = mtd->erasesize; | |
70 | erase.priv = (u_long)&wait_q; | |
71 | ||
72 | set_current_state(TASK_INTERRUPTIBLE); | |
73 | add_wait_queue(&wait_q, &wait); | |
74 | ||
75 | ret = mtd->erase(mtd, &erase); | |
76 | if (ret) { | |
77 | set_current_state(TASK_RUNNING); | |
78 | remove_wait_queue(&wait_q, &wait); | |
79 | printk (KERN_WARNING "mtdoops: erase of region [0x%x, 0x%x] " | |
80 | "on \"%s\" failed\n", | |
81 | erase.addr, erase.len, mtd->name); | |
82 | return ret; | |
83 | } | |
84 | ||
85 | schedule(); /* Wait for erase to finish. */ | |
86 | remove_wait_queue(&wait_q, &wait); | |
87 | ||
88 | return 0; | |
89 | } | |
90 | ||
6ce0a856 | 91 | static void mtdoops_inc_counter(struct mtdoops_context *cxt) |
4b23aff0 RP |
92 | { |
93 | struct mtd_info *mtd = cxt->mtd; | |
94 | size_t retlen; | |
95 | u32 count; | |
96 | int ret; | |
97 | ||
98 | cxt->nextpage++; | |
99 | if (cxt->nextpage > cxt->oops_pages) | |
100 | cxt->nextpage = 0; | |
101 | cxt->nextcount++; | |
102 | if (cxt->nextcount == 0xffffffff) | |
103 | cxt->nextcount = 0; | |
104 | ||
105 | ret = mtd->read(mtd, cxt->nextpage * OOPS_PAGE_SIZE, 4, | |
106 | &retlen, (u_char *) &count); | |
2986bd2a | 107 | if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) { |
68d09b1b | 108 | printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)" |
4b23aff0 RP |
109 | ", err %d.\n", cxt->nextpage * OOPS_PAGE_SIZE, |
110 | retlen, ret); | |
6ce0a856 RP |
111 | schedule_work(&cxt->work_erase); |
112 | return; | |
4b23aff0 RP |
113 | } |
114 | ||
115 | /* See if we need to erase the next block */ | |
6ce0a856 RP |
116 | if (count != 0xffffffff) { |
117 | schedule_work(&cxt->work_erase); | |
118 | return; | |
119 | } | |
4b23aff0 RP |
120 | |
121 | printk(KERN_DEBUG "mtdoops: Ready %d, %d (no erase)\n", | |
122 | cxt->nextpage, cxt->nextcount); | |
123 | cxt->ready = 1; | |
4b23aff0 RP |
124 | } |
125 | ||
6ce0a856 RP |
126 | /* Scheduled work - when we can't proceed without erasing a block */ |
127 | static void mtdoops_workfunc_erase(struct work_struct *work) | |
4b23aff0 | 128 | { |
6ce0a856 RP |
129 | struct mtdoops_context *cxt = |
130 | container_of(work, struct mtdoops_context, work_erase); | |
4b23aff0 RP |
131 | struct mtd_info *mtd = cxt->mtd; |
132 | int i = 0, j, ret, mod; | |
133 | ||
134 | /* We were unregistered */ | |
135 | if (!mtd) | |
136 | return; | |
137 | ||
138 | mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize; | |
139 | if (mod != 0) { | |
140 | cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE); | |
141 | if (cxt->nextpage > cxt->oops_pages) | |
142 | cxt->nextpage = 0; | |
143 | } | |
144 | ||
2986bd2a RP |
145 | while (mtd->block_isbad) { |
146 | ret = mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE); | |
147 | if (!ret) | |
148 | break; | |
149 | if (ret < 0) { | |
150 | printk(KERN_ERR "mtdoops: block_isbad failed, aborting.\n"); | |
151 | return; | |
152 | } | |
4b23aff0 RP |
153 | badblock: |
154 | printk(KERN_WARNING "mtdoops: Bad block at %08x\n", | |
155 | cxt->nextpage * OOPS_PAGE_SIZE); | |
156 | i++; | |
157 | cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE); | |
158 | if (cxt->nextpage > cxt->oops_pages) | |
159 | cxt->nextpage = 0; | |
160 | if (i == (cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE))) { | |
161 | printk(KERN_ERR "mtdoops: All blocks bad!\n"); | |
162 | return; | |
163 | } | |
164 | } | |
165 | ||
166 | for (j = 0, ret = -1; (j < 3) && (ret < 0); j++) | |
167 | ret = mtdoops_erase_block(mtd, cxt->nextpage * OOPS_PAGE_SIZE); | |
168 | ||
2986bd2a RP |
169 | if (ret >= 0) { |
170 | printk(KERN_DEBUG "mtdoops: Ready %d, %d \n", cxt->nextpage, cxt->nextcount); | |
171 | cxt->ready = 1; | |
172 | return; | |
4b23aff0 RP |
173 | } |
174 | ||
2986bd2a RP |
175 | if (mtd->block_markbad && (ret == -EIO)) { |
176 | ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE); | |
177 | if (ret < 0) { | |
178 | printk(KERN_ERR "mtdoops: block_markbad failed, aborting.\n"); | |
179 | return; | |
180 | } | |
181 | } | |
182 | goto badblock; | |
4b23aff0 RP |
183 | } |
184 | ||
6ce0a856 | 185 | static void mtdoops_workfunc_write(struct work_struct *work) |
4b23aff0 RP |
186 | { |
187 | struct mtdoops_context *cxt = | |
6ce0a856 RP |
188 | container_of(work, struct mtdoops_context, work_write); |
189 | struct mtd_info *mtd = cxt->mtd; | |
190 | size_t retlen; | |
191 | int ret; | |
4b23aff0 | 192 | |
6ce0a856 RP |
193 | if (cxt->writecount < OOPS_PAGE_SIZE) |
194 | memset(cxt->oops_buf + cxt->writecount, 0xff, | |
195 | OOPS_PAGE_SIZE - cxt->writecount); | |
196 | ||
197 | ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE, | |
198 | OOPS_PAGE_SIZE, &retlen, cxt->oops_buf); | |
199 | ||
200 | cxt->writecount = 0; | |
201 | ||
202 | if ((retlen != OOPS_PAGE_SIZE) || (ret < 0)) | |
203 | printk(KERN_ERR "mtdoops: Write failure at %d (%td of %d written), err %d.\n", | |
204 | cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret); | |
205 | ||
206 | mtdoops_inc_counter(cxt); | |
207 | } | |
4b23aff0 | 208 | |
6ce0a856 | 209 | static void find_next_position(struct mtdoops_context *cxt) |
4b23aff0 RP |
210 | { |
211 | struct mtd_info *mtd = cxt->mtd; | |
2986bd2a | 212 | int ret, page, maxpos = 0; |
4b23aff0 RP |
213 | u32 count, maxcount = 0xffffffff; |
214 | size_t retlen; | |
215 | ||
216 | for (page = 0; page < cxt->oops_pages; page++) { | |
2986bd2a RP |
217 | ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 4, &retlen, (u_char *) &count); |
218 | if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) { | |
219 | printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)" | |
220 | ", err %d.\n", page * OOPS_PAGE_SIZE, retlen, ret); | |
221 | continue; | |
222 | } | |
223 | ||
4b23aff0 RP |
224 | if (count == 0xffffffff) |
225 | continue; | |
226 | if (maxcount == 0xffffffff) { | |
227 | maxcount = count; | |
228 | maxpos = page; | |
229 | } else if ((count < 0x40000000) && (maxcount > 0xc0000000)) { | |
230 | maxcount = count; | |
231 | maxpos = page; | |
232 | } else if ((count > maxcount) && (count < 0xc0000000)) { | |
233 | maxcount = count; | |
234 | maxpos = page; | |
235 | } else if ((count > maxcount) && (count > 0xc0000000) | |
236 | && (maxcount > 0x80000000)) { | |
237 | maxcount = count; | |
238 | maxpos = page; | |
239 | } | |
240 | } | |
241 | if (maxcount == 0xffffffff) { | |
242 | cxt->nextpage = 0; | |
243 | cxt->nextcount = 1; | |
244 | cxt->ready = 1; | |
245 | printk(KERN_DEBUG "mtdoops: Ready %d, %d (first init)\n", | |
246 | cxt->nextpage, cxt->nextcount); | |
6ce0a856 | 247 | return; |
4b23aff0 RP |
248 | } |
249 | ||
250 | cxt->nextpage = maxpos; | |
251 | cxt->nextcount = maxcount; | |
252 | ||
6ce0a856 | 253 | mtdoops_inc_counter(cxt); |
4b23aff0 RP |
254 | } |
255 | ||
256 | ||
257 | static void mtdoops_notify_add(struct mtd_info *mtd) | |
258 | { | |
259 | struct mtdoops_context *cxt = &oops_cxt; | |
4b23aff0 RP |
260 | |
261 | if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0) | |
262 | return; | |
263 | ||
264 | if (mtd->size < (mtd->erasesize * 2)) { | |
265 | printk(KERN_ERR "MTD partition %d not big enough for mtdoops\n", | |
266 | mtd->index); | |
267 | return; | |
268 | } | |
269 | ||
270 | cxt->mtd = mtd; | |
271 | cxt->oops_pages = mtd->size / OOPS_PAGE_SIZE; | |
272 | ||
6ce0a856 | 273 | find_next_position(cxt); |
4b23aff0 RP |
274 | |
275 | printk(KERN_DEBUG "mtdoops: Attached to MTD device %d\n", mtd->index); | |
276 | } | |
277 | ||
278 | static void mtdoops_notify_remove(struct mtd_info *mtd) | |
279 | { | |
280 | struct mtdoops_context *cxt = &oops_cxt; | |
281 | ||
282 | if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0) | |
283 | return; | |
284 | ||
285 | cxt->mtd = NULL; | |
286 | flush_scheduled_work(); | |
287 | } | |
288 | ||
8691a729 | 289 | static void mtdoops_console_sync(void) |
4b23aff0 | 290 | { |
8691a729 | 291 | struct mtdoops_context *cxt = &oops_cxt; |
4b23aff0 | 292 | struct mtd_info *mtd = cxt->mtd; |
4b23aff0 | 293 | |
6ce0a856 | 294 | if (!cxt->ready || !mtd || cxt->writecount == 0) |
4b23aff0 RP |
295 | return; |
296 | ||
8691a729 | 297 | cxt->ready = 0; |
8691a729 | 298 | |
6ce0a856 | 299 | schedule_work(&cxt->work_write); |
8691a729 RP |
300 | } |
301 | ||
302 | static void | |
303 | mtdoops_console_write(struct console *co, const char *s, unsigned int count) | |
304 | { | |
305 | struct mtdoops_context *cxt = co->data; | |
306 | struct mtd_info *mtd = cxt->mtd; | |
8691a729 RP |
307 | |
308 | if (!oops_in_progress) { | |
309 | mtdoops_console_sync(); | |
310 | return; | |
4b23aff0 RP |
311 | } |
312 | ||
8691a729 | 313 | if (!cxt->ready || !mtd) |
4b23aff0 RP |
314 | return; |
315 | ||
316 | if (cxt->writecount == 0) { | |
317 | u32 *stamp = cxt->oops_buf; | |
318 | *stamp = cxt->nextcount; | |
319 | cxt->writecount = 4; | |
320 | } | |
321 | ||
322 | if ((count + cxt->writecount) > OOPS_PAGE_SIZE) | |
323 | count = OOPS_PAGE_SIZE - cxt->writecount; | |
324 | ||
235d6200 PK |
325 | memcpy(cxt->oops_buf + cxt->writecount, s, count); |
326 | cxt->writecount += count; | |
4b23aff0 RP |
327 | } |
328 | ||
329 | static int __init mtdoops_console_setup(struct console *co, char *options) | |
330 | { | |
331 | struct mtdoops_context *cxt = co->data; | |
332 | ||
333 | if (cxt->mtd_index != -1) | |
334 | return -EBUSY; | |
335 | if (co->index == -1) | |
336 | return -EINVAL; | |
337 | ||
338 | cxt->mtd_index = co->index; | |
339 | return 0; | |
340 | } | |
341 | ||
342 | static struct mtd_notifier mtdoops_notifier = { | |
343 | .add = mtdoops_notify_add, | |
344 | .remove = mtdoops_notify_remove, | |
345 | }; | |
346 | ||
347 | static struct console mtdoops_console = { | |
348 | .name = "ttyMTD", | |
349 | .write = mtdoops_console_write, | |
350 | .setup = mtdoops_console_setup, | |
8691a729 | 351 | .unblank = mtdoops_console_sync, |
4b23aff0 RP |
352 | .flags = CON_PRINTBUFFER, |
353 | .index = -1, | |
354 | .data = &oops_cxt, | |
355 | }; | |
356 | ||
357 | static int __init mtdoops_console_init(void) | |
358 | { | |
359 | struct mtdoops_context *cxt = &oops_cxt; | |
360 | ||
361 | cxt->mtd_index = -1; | |
362 | cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE); | |
363 | ||
364 | if (!cxt->oops_buf) { | |
365 | printk(KERN_ERR "Failed to allocate oops buffer workspace\n"); | |
366 | return -ENOMEM; | |
367 | } | |
368 | ||
6ce0a856 RP |
369 | INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase); |
370 | INIT_WORK(&cxt->work_write, mtdoops_workfunc_write); | |
4b23aff0 RP |
371 | |
372 | register_console(&mtdoops_console); | |
373 | register_mtd_user(&mtdoops_notifier); | |
374 | return 0; | |
375 | } | |
376 | ||
377 | static void __exit mtdoops_console_exit(void) | |
378 | { | |
379 | struct mtdoops_context *cxt = &oops_cxt; | |
380 | ||
381 | unregister_mtd_user(&mtdoops_notifier); | |
382 | unregister_console(&mtdoops_console); | |
383 | vfree(cxt->oops_buf); | |
384 | } | |
385 | ||
386 | ||
387 | subsys_initcall(mtdoops_console_init); | |
388 | module_exit(mtdoops_console_exit); | |
389 | ||
390 | MODULE_LICENSE("GPL"); | |
391 | MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>"); | |
392 | MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver"); |