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