]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/mtd/mtdoops.c
[MTD] Add mtd panic_write function pointer
[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>
47c152b8 31#include <linux/spinlock.h>
4b23aff0
RP
32#include <linux/mtd/mtd.h>
33
34#define OOPS_PAGE_SIZE 4096
35
6ce0a856 36struct mtdoops_context {
4b23aff0 37 int mtd_index;
6ce0a856
RP
38 struct work_struct work_erase;
39 struct work_struct work_write;
4b23aff0
RP
40 struct mtd_info *mtd;
41 int oops_pages;
42 int nextpage;
43 int nextcount;
44
45 void *oops_buf;
47c152b8
RP
46
47 /* writecount and disabling ready are spin lock protected */
48 spinlock_t writecount_lock;
4b23aff0
RP
49 int ready;
50 int writecount;
51} oops_cxt;
52
53static 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
59static 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;
79dcd8e9 70 erase.len = mtd->erasesize;
4b23aff0
RP
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
6ce0a856 92static void mtdoops_inc_counter(struct mtdoops_context *cxt)
4b23aff0
RP
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);
2986bd2a 108 if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) {
68d09b1b 109 printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)"
4b23aff0
RP
110 ", err %d.\n", cxt->nextpage * OOPS_PAGE_SIZE,
111 retlen, ret);
6ce0a856
RP
112 schedule_work(&cxt->work_erase);
113 return;
4b23aff0
RP
114 }
115
116 /* See if we need to erase the next block */
6ce0a856
RP
117 if (count != 0xffffffff) {
118 schedule_work(&cxt->work_erase);
119 return;
120 }
4b23aff0
RP
121
122 printk(KERN_DEBUG "mtdoops: Ready %d, %d (no erase)\n",
123 cxt->nextpage, cxt->nextcount);
124 cxt->ready = 1;
4b23aff0
RP
125}
126
6ce0a856
RP
127/* Scheduled work - when we can't proceed without erasing a block */
128static void mtdoops_workfunc_erase(struct work_struct *work)
4b23aff0 129{
6ce0a856
RP
130 struct mtdoops_context *cxt =
131 container_of(work, struct mtdoops_context, work_erase);
4b23aff0
RP
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
2986bd2a
RP
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 }
4b23aff0
RP
154badblock:
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
2986bd2a
RP
170 if (ret >= 0) {
171 printk(KERN_DEBUG "mtdoops: Ready %d, %d \n", cxt->nextpage, cxt->nextcount);
172 cxt->ready = 1;
173 return;
4b23aff0
RP
174 }
175
2986bd2a
RP
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;
4b23aff0
RP
184}
185
6ce0a856 186static void mtdoops_workfunc_write(struct work_struct *work)
4b23aff0
RP
187{
188 struct mtdoops_context *cxt =
6ce0a856
RP
189 container_of(work, struct mtdoops_context, work_write);
190 struct mtd_info *mtd = cxt->mtd;
191 size_t retlen;
192 int ret;
4b23aff0 193
6ce0a856
RP
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}
4b23aff0 209
6ce0a856 210static void find_next_position(struct mtdoops_context *cxt)
4b23aff0
RP
211{
212 struct mtd_info *mtd = cxt->mtd;
2986bd2a 213 int ret, page, maxpos = 0;
4b23aff0
RP
214 u32 count, maxcount = 0xffffffff;
215 size_t retlen;
216
217 for (page = 0; page < cxt->oops_pages; page++) {
2986bd2a
RP
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
4b23aff0
RP
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);
6ce0a856 248 return;
4b23aff0
RP
249 }
250
251 cxt->nextpage = maxpos;
252 cxt->nextcount = maxcount;
253
6ce0a856 254 mtdoops_inc_counter(cxt);
4b23aff0
RP
255}
256
257
258static void mtdoops_notify_add(struct mtd_info *mtd)
259{
260 struct mtdoops_context *cxt = &oops_cxt;
4b23aff0
RP
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
79dcd8e9
RP
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
4b23aff0
RP
277 cxt->mtd = mtd;
278 cxt->oops_pages = mtd->size / OOPS_PAGE_SIZE;
279
6ce0a856 280 find_next_position(cxt);
4b23aff0 281
79dcd8e9 282 printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
4b23aff0
RP
283}
284
285static 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
8691a729 296static void mtdoops_console_sync(void)
4b23aff0 297{
8691a729 298 struct mtdoops_context *cxt = &oops_cxt;
4b23aff0 299 struct mtd_info *mtd = cxt->mtd;
47c152b8 300 unsigned long flags;
4b23aff0 301
6ce0a856 302 if (!cxt->ready || !mtd || cxt->writecount == 0)
4b23aff0
RP
303 return;
304
47c152b8
RP
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 }
8691a729 314 cxt->ready = 0;
47c152b8 315 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
8691a729 316
6ce0a856 317 schedule_work(&cxt->work_write);
8691a729
RP
318}
319
320static void
321mtdoops_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;
47c152b8 325 unsigned long flags;
8691a729
RP
326
327 if (!oops_in_progress) {
328 mtdoops_console_sync();
329 return;
4b23aff0
RP
330 }
331
8691a729 332 if (!cxt->ready || !mtd)
4b23aff0
RP
333 return;
334
47c152b8
RP
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
4b23aff0
RP
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
235d6200
PK
351 memcpy(cxt->oops_buf + cxt->writecount, s, count);
352 cxt->writecount += count;
47c152b8
RP
353
354 spin_unlock_irqrestore(&cxt->writecount_lock, flags);
355
356 if (cxt->writecount == OOPS_PAGE_SIZE)
357 mtdoops_console_sync();
4b23aff0
RP
358}
359
360static 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
373static struct mtd_notifier mtdoops_notifier = {
374 .add = mtdoops_notify_add,
375 .remove = mtdoops_notify_remove,
376};
377
378static struct console mtdoops_console = {
379 .name = "ttyMTD",
380 .write = mtdoops_console_write,
381 .setup = mtdoops_console_setup,
8691a729 382 .unblank = mtdoops_console_sync,
4b23aff0
RP
383 .index = -1,
384 .data = &oops_cxt,
385};
386
387static 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) {
79dcd8e9 395 printk(KERN_ERR "Failed to allocate mtdoops buffer workspace\n");
4b23aff0
RP
396 return -ENOMEM;
397 }
398
6ce0a856
RP
399 INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
400 INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
4b23aff0
RP
401
402 register_console(&mtdoops_console);
403 register_mtd_user(&mtdoops_notifier);
404 return 0;
405}
406
407static 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
417subsys_initcall(mtdoops_console_init);
418module_exit(mtdoops_console_exit);
419
420MODULE_LICENSE("GPL");
421MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
422MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");