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