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> |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 31 | #include <linux/delay.h> |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 32 | #include <linux/spinlock.h> |
David Woodhouse | f9f7dd2 | 2008-02-07 10:50:57 +0000 | [diff] [blame] | 33 | #include <linux/interrupt.h> |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 34 | #include <linux/mtd/mtd.h> |
| 35 | |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 36 | #define MTDOOPS_KERNMSG_MAGIC 0x5d005d00 |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 37 | #define OOPS_PAGE_SIZE 4096 |
| 38 | |
Adrian Bunk | 7903cba | 2008-04-18 13:44:11 -0700 | [diff] [blame] | 39 | static struct mtdoops_context { |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 40 | int mtd_index; |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 41 | struct work_struct work_erase; |
| 42 | struct work_struct work_write; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 43 | struct mtd_info *mtd; |
| 44 | int oops_pages; |
| 45 | int nextpage; |
| 46 | int nextcount; |
Adrian Hunter | e2a0f25 | 2009-02-16 18:21:35 +0200 | [diff] [blame] | 47 | char *name; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 48 | |
| 49 | void *oops_buf; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 50 | |
| 51 | /* writecount and disabling ready are spin lock protected */ |
| 52 | spinlock_t writecount_lock; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 53 | int ready; |
| 54 | int writecount; |
| 55 | } oops_cxt; |
| 56 | |
| 57 | static 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 | |
| 63 | static 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; |
Richard Purdie | 79dcd8e | 2008-01-29 10:25:55 +0000 | [diff] [blame] | 74 | erase.len = mtd->erasesize; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 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); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 84 | printk (KERN_WARNING "mtdoops: erase of region [0x%llx, 0x%llx] " |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 85 | "on \"%s\" failed\n", |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 86 | (unsigned long long)erase.addr, (unsigned long long)erase.len, mtd->name); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 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 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 96 | static void mtdoops_inc_counter(struct mtdoops_context *cxt) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 97 | { |
| 98 | struct mtd_info *mtd = cxt->mtd; |
| 99 | size_t retlen; |
| 100 | u32 count; |
| 101 | int ret; |
| 102 | |
| 103 | cxt->nextpage++; |
Richard Purdie | ecd5b31 | 2008-07-26 09:17:41 +0100 | [diff] [blame] | 104 | if (cxt->nextpage >= cxt->oops_pages) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 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); |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 112 | if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) { |
Andrew Morton | 68d09b1 | 2007-08-10 14:01:31 -0700 | [diff] [blame] | 113 | printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)" |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 114 | ", err %d.\n", cxt->nextpage * OOPS_PAGE_SIZE, |
| 115 | retlen, ret); |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 116 | schedule_work(&cxt->work_erase); |
| 117 | return; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* See if we need to erase the next block */ |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 121 | if (count != 0xffffffff) { |
| 122 | schedule_work(&cxt->work_erase); |
| 123 | return; |
| 124 | } |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 125 | |
| 126 | printk(KERN_DEBUG "mtdoops: Ready %d, %d (no erase)\n", |
| 127 | cxt->nextpage, cxt->nextcount); |
| 128 | cxt->ready = 1; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 131 | /* Scheduled work - when we can't proceed without erasing a block */ |
| 132 | static void mtdoops_workfunc_erase(struct work_struct *work) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 133 | { |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 134 | struct mtdoops_context *cxt = |
| 135 | container_of(work, struct mtdoops_context, work_erase); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 136 | struct mtd_info *mtd = cxt->mtd; |
| 137 | int i = 0, j, ret, mod; |
| 138 | |
| 139 | /* We were unregistered */ |
| 140 | if (!mtd) |
| 141 | return; |
| 142 | |
| 143 | mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize; |
| 144 | if (mod != 0) { |
| 145 | cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE); |
Richard Purdie | ecd5b31 | 2008-07-26 09:17:41 +0100 | [diff] [blame] | 146 | if (cxt->nextpage >= cxt->oops_pages) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 147 | cxt->nextpage = 0; |
| 148 | } |
| 149 | |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 150 | while (mtd->block_isbad) { |
| 151 | ret = mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE); |
| 152 | if (!ret) |
| 153 | break; |
| 154 | if (ret < 0) { |
| 155 | printk(KERN_ERR "mtdoops: block_isbad failed, aborting.\n"); |
| 156 | return; |
| 157 | } |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 158 | badblock: |
| 159 | printk(KERN_WARNING "mtdoops: Bad block at %08x\n", |
| 160 | cxt->nextpage * OOPS_PAGE_SIZE); |
| 161 | i++; |
| 162 | cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE); |
Richard Purdie | ecd5b31 | 2008-07-26 09:17:41 +0100 | [diff] [blame] | 163 | if (cxt->nextpage >= cxt->oops_pages) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 164 | cxt->nextpage = 0; |
| 165 | if (i == (cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE))) { |
| 166 | printk(KERN_ERR "mtdoops: All blocks bad!\n"); |
| 167 | return; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | for (j = 0, ret = -1; (j < 3) && (ret < 0); j++) |
| 172 | ret = mtdoops_erase_block(mtd, cxt->nextpage * OOPS_PAGE_SIZE); |
| 173 | |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 174 | if (ret >= 0) { |
| 175 | printk(KERN_DEBUG "mtdoops: Ready %d, %d \n", cxt->nextpage, cxt->nextcount); |
| 176 | cxt->ready = 1; |
| 177 | return; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 180 | if (mtd->block_markbad && (ret == -EIO)) { |
| 181 | ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE); |
| 182 | if (ret < 0) { |
| 183 | printk(KERN_ERR "mtdoops: block_markbad failed, aborting.\n"); |
| 184 | return; |
| 185 | } |
| 186 | } |
| 187 | goto badblock; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 190 | static void mtdoops_write(struct mtdoops_context *cxt, int panic) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 191 | { |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 192 | struct mtd_info *mtd = cxt->mtd; |
| 193 | size_t retlen; |
| 194 | int ret; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 195 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 196 | if (cxt->writecount < OOPS_PAGE_SIZE) |
| 197 | memset(cxt->oops_buf + cxt->writecount, 0xff, |
| 198 | OOPS_PAGE_SIZE - cxt->writecount); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 199 | |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 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, |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 205 | OOPS_PAGE_SIZE, &retlen, cxt->oops_buf); |
| 206 | |
| 207 | cxt->writecount = 0; |
| 208 | |
| 209 | if ((retlen != OOPS_PAGE_SIZE) || (ret < 0)) |
| 210 | printk(KERN_ERR "mtdoops: Write failure at %d (%td of %d written), err %d.\n", |
| 211 | cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret); |
| 212 | |
| 213 | mtdoops_inc_counter(cxt); |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | |
| 217 | static 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); |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static void find_next_position(struct mtdoops_context *cxt) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 226 | { |
| 227 | struct mtd_info *mtd = cxt->mtd; |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 228 | int ret, page, maxpos = 0; |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 229 | u32 count[2], maxcount = 0xffffffff; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 230 | size_t retlen; |
| 231 | |
| 232 | for (page = 0; page < cxt->oops_pages; page++) { |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 233 | ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 8, &retlen, (u_char *) &count[0]); |
| 234 | if ((retlen != 8) || ((ret < 0) && (ret != -EUCLEAN))) { |
| 235 | printk(KERN_ERR "mtdoops: Read failure at %d (%td of 8 read)" |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 236 | ", err %d.\n", page * OOPS_PAGE_SIZE, retlen, ret); |
| 237 | continue; |
| 238 | } |
| 239 | |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 240 | if (count[1] != MTDOOPS_KERNMSG_MAGIC) |
| 241 | continue; |
| 242 | if (count[0] == 0xffffffff) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 243 | continue; |
| 244 | if (maxcount == 0xffffffff) { |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 245 | maxcount = count[0]; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 246 | maxpos = page; |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 247 | } else if ((count[0] < 0x40000000) && (maxcount > 0xc0000000)) { |
| 248 | maxcount = count[0]; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 249 | maxpos = page; |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 250 | } else if ((count[0] > maxcount) && (count[0] < 0xc0000000)) { |
| 251 | maxcount = count[0]; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 252 | maxpos = page; |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 253 | } else if ((count[0] > maxcount) && (count[0] > 0xc0000000) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 254 | && (maxcount > 0x80000000)) { |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 255 | maxcount = count[0]; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 256 | maxpos = page; |
| 257 | } |
| 258 | } |
| 259 | if (maxcount == 0xffffffff) { |
| 260 | cxt->nextpage = 0; |
| 261 | cxt->nextcount = 1; |
Richard Purdie | 43b5693 | 2008-07-26 09:25:18 +0100 | [diff] [blame] | 262 | schedule_work(&cxt->work_erase); |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 263 | return; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | cxt->nextpage = maxpos; |
| 267 | cxt->nextcount = maxcount; |
| 268 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 269 | mtdoops_inc_counter(cxt); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | |
| 273 | static void mtdoops_notify_add(struct mtd_info *mtd) |
| 274 | { |
| 275 | struct mtdoops_context *cxt = &oops_cxt; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 276 | |
Adrian Hunter | e2a0f25 | 2009-02-16 18:21:35 +0200 | [diff] [blame] | 277 | if (cxt->name && !strcmp(mtd->name, cxt->name)) |
| 278 | cxt->mtd_index = mtd->index; |
| 279 | |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 280 | if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0) |
| 281 | return; |
| 282 | |
| 283 | if (mtd->size < (mtd->erasesize * 2)) { |
| 284 | printk(KERN_ERR "MTD partition %d not big enough for mtdoops\n", |
| 285 | mtd->index); |
| 286 | return; |
| 287 | } |
| 288 | |
Richard Purdie | 79dcd8e | 2008-01-29 10:25:55 +0000 | [diff] [blame] | 289 | if (mtd->erasesize < OOPS_PAGE_SIZE) { |
| 290 | printk(KERN_ERR "Eraseblock size of MTD partition %d too small\n", |
| 291 | mtd->index); |
| 292 | return; |
| 293 | } |
| 294 | |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 295 | cxt->mtd = mtd; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 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; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 300 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 301 | find_next_position(cxt); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 302 | |
Richard Purdie | 79dcd8e | 2008-01-29 10:25:55 +0000 | [diff] [blame] | 303 | printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static void mtdoops_notify_remove(struct mtd_info *mtd) |
| 307 | { |
| 308 | struct mtdoops_context *cxt = &oops_cxt; |
| 309 | |
| 310 | if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0) |
| 311 | return; |
| 312 | |
| 313 | cxt->mtd = NULL; |
| 314 | flush_scheduled_work(); |
| 315 | } |
| 316 | |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 317 | static void mtdoops_console_sync(void) |
| 318 | { |
| 319 | struct mtdoops_context *cxt = &oops_cxt; |
| 320 | struct mtd_info *mtd = cxt->mtd; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 321 | unsigned long flags; |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 322 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 323 | if (!cxt->ready || !mtd || cxt->writecount == 0) |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 324 | return; |
| 325 | |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 326 | /* |
| 327 | * Once ready is 0 and we've held the lock no further writes to the |
| 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 | } |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 335 | cxt->ready = 0; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 336 | spin_unlock_irqrestore(&cxt->writecount_lock, flags); |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 337 | |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 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); |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 343 | } |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 344 | |
| 345 | static void |
| 346 | mtdoops_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; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 350 | unsigned long flags; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 351 | |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 352 | if (!oops_in_progress) { |
| 353 | mtdoops_console_sync(); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 354 | return; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 357 | if (!cxt->ready || !mtd) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 358 | return; |
| 359 | |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 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 */ |
Adrian Hunter | 48ec00a | 2009-03-04 09:53:40 +0200 | [diff] [blame] | 364 | if (!cxt->ready) { |
| 365 | spin_unlock_irqrestore(&cxt->writecount_lock, flags); |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 366 | return; |
Adrian Hunter | 48ec00a | 2009-03-04 09:53:40 +0200 | [diff] [blame] | 367 | } |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 368 | |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 369 | if (cxt->writecount == 0) { |
| 370 | u32 *stamp = cxt->oops_buf; |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 371 | *stamp++ = cxt->nextcount; |
| 372 | *stamp = MTDOOPS_KERNMSG_MAGIC; |
| 373 | cxt->writecount = 8; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | if ((count + cxt->writecount) > OOPS_PAGE_SIZE) |
| 377 | count = OOPS_PAGE_SIZE - cxt->writecount; |
| 378 | |
Peter Korsgaard | 235d620 | 2007-11-06 11:56:02 +0100 | [diff] [blame] | 379 | memcpy(cxt->oops_buf + cxt->writecount, s, count); |
| 380 | cxt->writecount += count; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 381 | |
| 382 | spin_unlock_irqrestore(&cxt->writecount_lock, flags); |
| 383 | |
| 384 | if (cxt->writecount == OOPS_PAGE_SIZE) |
| 385 | mtdoops_console_sync(); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static int __init mtdoops_console_setup(struct console *co, char *options) |
| 389 | { |
| 390 | struct mtdoops_context *cxt = co->data; |
| 391 | |
Adrian Hunter | e2a0f25 | 2009-02-16 18:21:35 +0200 | [diff] [blame] | 392 | if (cxt->mtd_index != -1 || cxt->name) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 393 | return -EBUSY; |
Adrian Hunter | e2a0f25 | 2009-02-16 18:21:35 +0200 | [diff] [blame] | 394 | if (options) { |
| 395 | cxt->name = kstrdup(options, GFP_KERNEL); |
| 396 | return 0; |
| 397 | } |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 398 | if (co->index == -1) |
| 399 | return -EINVAL; |
| 400 | |
| 401 | cxt->mtd_index = co->index; |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static struct mtd_notifier mtdoops_notifier = { |
| 406 | .add = mtdoops_notify_add, |
| 407 | .remove = mtdoops_notify_remove, |
| 408 | }; |
| 409 | |
| 410 | static struct console mtdoops_console = { |
| 411 | .name = "ttyMTD", |
| 412 | .write = mtdoops_console_write, |
| 413 | .setup = mtdoops_console_setup, |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 414 | .unblank = mtdoops_console_sync, |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 415 | .index = -1, |
| 416 | .data = &oops_cxt, |
| 417 | }; |
| 418 | |
| 419 | static 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); |
Adrian Hunter | 48ec00a | 2009-03-04 09:53:40 +0200 | [diff] [blame] | 425 | spin_lock_init(&cxt->writecount_lock); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 426 | |
| 427 | if (!cxt->oops_buf) { |
Richard Purdie | 79dcd8e | 2008-01-29 10:25:55 +0000 | [diff] [blame] | 428 | printk(KERN_ERR "Failed to allocate mtdoops buffer workspace\n"); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 429 | return -ENOMEM; |
| 430 | } |
| 431 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 432 | INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase); |
| 433 | INIT_WORK(&cxt->work_write, mtdoops_workfunc_write); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 434 | |
| 435 | register_console(&mtdoops_console); |
| 436 | register_mtd_user(&mtdoops_notifier); |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static void __exit mtdoops_console_exit(void) |
| 441 | { |
| 442 | struct mtdoops_context *cxt = &oops_cxt; |
| 443 | |
| 444 | unregister_mtd_user(&mtdoops_notifier); |
| 445 | unregister_console(&mtdoops_console); |
Adrian Hunter | e2a0f25 | 2009-02-16 18:21:35 +0200 | [diff] [blame] | 446 | kfree(cxt->name); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 447 | vfree(cxt->oops_buf); |
| 448 | } |
| 449 | |
| 450 | |
| 451 | subsys_initcall(mtdoops_console_init); |
| 452 | module_exit(mtdoops_console_exit); |
| 453 | |
| 454 | MODULE_LICENSE("GPL"); |
| 455 | MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>"); |
| 456 | MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver"); |