[MTD] [DOC200x] eccbuf is statically defined and always evaluate to true
[linux-2.6/verdex.git] / drivers / mtd / mtdoops.c
blobfd98e38f10bcf133172b4a87475134d489d809e1
1 /*
2 * MTD Oops/Panic logger
4 * Copyright (C) 2007 Nokia Corporation. All rights reserved.
6 * Author: Richard Purdie <rpurdie@openedhand.com>
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.
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.
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
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/delay.h>
32 #include <linux/spinlock.h>
33 #include <linux/mtd/mtd.h>
35 #define OOPS_PAGE_SIZE 4096
37 struct mtdoops_context {
38 int mtd_index;
39 struct work_struct work_erase;
40 struct work_struct work_write;
41 struct mtd_info *mtd;
42 int oops_pages;
43 int nextpage;
44 int nextcount;
46 void *oops_buf;
48 /* writecount and disabling ready are spin lock protected */
49 spinlock_t writecount_lock;
50 int ready;
51 int writecount;
52 } oops_cxt;
54 static void mtdoops_erase_callback(struct erase_info *done)
56 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
57 wake_up(wait_q);
60 static int mtdoops_erase_block(struct mtd_info *mtd, int offset)
62 struct erase_info erase;
63 DECLARE_WAITQUEUE(wait, current);
64 wait_queue_head_t wait_q;
65 int ret;
67 init_waitqueue_head(&wait_q);
68 erase.mtd = mtd;
69 erase.callback = mtdoops_erase_callback;
70 erase.addr = offset;
71 erase.len = mtd->erasesize;
72 erase.priv = (u_long)&wait_q;
74 set_current_state(TASK_INTERRUPTIBLE);
75 add_wait_queue(&wait_q, &wait);
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;
87 schedule(); /* Wait for erase to finish. */
88 remove_wait_queue(&wait_q, &wait);
90 return 0;
93 static void mtdoops_inc_counter(struct mtdoops_context *cxt)
95 struct mtd_info *mtd = cxt->mtd;
96 size_t retlen;
97 u32 count;
98 int ret;
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;
107 ret = mtd->read(mtd, cxt->nextpage * OOPS_PAGE_SIZE, 4,
108 &retlen, (u_char *) &count);
109 if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) {
110 printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)"
111 ", err %d.\n", cxt->nextpage * OOPS_PAGE_SIZE,
112 retlen, ret);
113 schedule_work(&cxt->work_erase);
114 return;
117 /* See if we need to erase the next block */
118 if (count != 0xffffffff) {
119 schedule_work(&cxt->work_erase);
120 return;
123 printk(KERN_DEBUG "mtdoops: Ready %d, %d (no erase)\n",
124 cxt->nextpage, cxt->nextcount);
125 cxt->ready = 1;
128 /* Scheduled work - when we can't proceed without erasing a block */
129 static void mtdoops_workfunc_erase(struct work_struct *work)
131 struct mtdoops_context *cxt =
132 container_of(work, struct mtdoops_context, work_erase);
133 struct mtd_info *mtd = cxt->mtd;
134 int i = 0, j, ret, mod;
136 /* We were unregistered */
137 if (!mtd)
138 return;
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;
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;
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;
168 for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
169 ret = mtdoops_erase_block(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
171 if (ret >= 0) {
172 printk(KERN_DEBUG "mtdoops: Ready %d, %d \n", cxt->nextpage, cxt->nextcount);
173 cxt->ready = 1;
174 return;
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;
184 goto badblock;
187 static void mtdoops_write(struct mtdoops_context *cxt, int panic)
189 struct mtd_info *mtd = cxt->mtd;
190 size_t retlen;
191 int ret;
193 if (cxt->writecount < OOPS_PAGE_SIZE)
194 memset(cxt->oops_buf + cxt->writecount, 0xff,
195 OOPS_PAGE_SIZE - cxt->writecount);
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,
202 OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
204 cxt->writecount = 0;
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);
210 mtdoops_inc_counter(cxt);
214 static void mtdoops_workfunc_write(struct work_struct *work)
216 struct mtdoops_context *cxt =
217 container_of(work, struct mtdoops_context, work_write);
219 mtdoops_write(cxt, 0);
222 static void find_next_position(struct mtdoops_context *cxt)
224 struct mtd_info *mtd = cxt->mtd;
225 int ret, page, maxpos = 0;
226 u32 count, maxcount = 0xffffffff;
227 size_t retlen;
229 for (page = 0; page < cxt->oops_pages; page++) {
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;
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;
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);
260 return;
263 cxt->nextpage = maxpos;
264 cxt->nextcount = maxcount;
266 mtdoops_inc_counter(cxt);
270 static void mtdoops_notify_add(struct mtd_info *mtd)
272 struct mtdoops_context *cxt = &oops_cxt;
274 if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
275 return;
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;
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;
289 cxt->mtd = mtd;
290 cxt->oops_pages = mtd->size / OOPS_PAGE_SIZE;
292 find_next_position(cxt);
294 printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
297 static void mtdoops_notify_remove(struct mtd_info *mtd)
299 struct mtdoops_context *cxt = &oops_cxt;
301 if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
302 return;
304 cxt->mtd = NULL;
305 flush_scheduled_work();
308 static void mtdoops_console_sync(void)
310 struct mtdoops_context *cxt = &oops_cxt;
311 struct mtd_info *mtd = cxt->mtd;
312 unsigned long flags;
314 if (!cxt->ready || !mtd || cxt->writecount == 0)
315 return;
318 * Once ready is 0 and we've held the lock no further writes to the
319 * buffer will happen
321 spin_lock_irqsave(&cxt->writecount_lock, flags);
322 if (!cxt->ready) {
323 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
324 return;
326 cxt->ready = 0;
327 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
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);
336 static void
337 mtdoops_console_write(struct console *co, const char *s, unsigned int count)
339 struct mtdoops_context *cxt = co->data;
340 struct mtd_info *mtd = cxt->mtd;
341 unsigned long flags;
343 if (!oops_in_progress) {
344 mtdoops_console_sync();
345 return;
348 if (!cxt->ready || !mtd)
349 return;
351 /* Locking on writecount ensures sequential writes to the buffer */
352 spin_lock_irqsave(&cxt->writecount_lock, flags);
354 /* Check ready status didn't change whilst waiting for the lock */
355 if (!cxt->ready)
356 return;
358 if (cxt->writecount == 0) {
359 u32 *stamp = cxt->oops_buf;
360 *stamp = cxt->nextcount;
361 cxt->writecount = 4;
364 if ((count + cxt->writecount) > OOPS_PAGE_SIZE)
365 count = OOPS_PAGE_SIZE - cxt->writecount;
367 memcpy(cxt->oops_buf + cxt->writecount, s, count);
368 cxt->writecount += count;
370 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
372 if (cxt->writecount == OOPS_PAGE_SIZE)
373 mtdoops_console_sync();
376 static int __init mtdoops_console_setup(struct console *co, char *options)
378 struct mtdoops_context *cxt = co->data;
380 if (cxt->mtd_index != -1)
381 return -EBUSY;
382 if (co->index == -1)
383 return -EINVAL;
385 cxt->mtd_index = co->index;
386 return 0;
389 static struct mtd_notifier mtdoops_notifier = {
390 .add = mtdoops_notify_add,
391 .remove = mtdoops_notify_remove,
394 static struct console mtdoops_console = {
395 .name = "ttyMTD",
396 .write = mtdoops_console_write,
397 .setup = mtdoops_console_setup,
398 .unblank = mtdoops_console_sync,
399 .index = -1,
400 .data = &oops_cxt,
403 static int __init mtdoops_console_init(void)
405 struct mtdoops_context *cxt = &oops_cxt;
407 cxt->mtd_index = -1;
408 cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE);
410 if (!cxt->oops_buf) {
411 printk(KERN_ERR "Failed to allocate mtdoops buffer workspace\n");
412 return -ENOMEM;
415 INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
416 INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
418 register_console(&mtdoops_console);
419 register_mtd_user(&mtdoops_notifier);
420 return 0;
423 static void __exit mtdoops_console_exit(void)
425 struct mtdoops_context *cxt = &oops_cxt;
427 unregister_mtd_user(&mtdoops_notifier);
428 unregister_console(&mtdoops_console);
429 vfree(cxt->oops_buf);
433 subsys_initcall(mtdoops_console_init);
434 module_exit(mtdoops_console_exit);
436 MODULE_LICENSE("GPL");
437 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
438 MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");