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