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