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