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