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; |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 47 | unsigned long *oops_page_used; |
Adrian Hunter | e2a0f25 | 2009-02-16 18:21:35 +0200 | [diff] [blame] | 48 | char *name; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 49 | |
| 50 | void *oops_buf; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 51 | |
| 52 | /* writecount and disabling ready are spin lock protected */ |
| 53 | spinlock_t writecount_lock; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 54 | int ready; |
| 55 | int writecount; |
| 56 | } oops_cxt; |
| 57 | |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 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 | |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 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 | |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 79 | static int mtdoops_erase_block(struct mtdoops_context *cxt, int offset) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 80 | { |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 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; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 85 | struct erase_info erase; |
| 86 | DECLARE_WAITQUEUE(wait, current); |
| 87 | wait_queue_head_t wait_q; |
| 88 | int ret; |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 89 | int page; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 90 | |
| 91 | init_waitqueue_head(&wait_q); |
| 92 | erase.mtd = mtd; |
| 93 | erase.callback = mtdoops_erase_callback; |
| 94 | erase.addr = offset; |
Richard Purdie | 79dcd8e | 2008-01-29 10:25:55 +0000 | [diff] [blame] | 95 | erase.len = mtd->erasesize; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 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); |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 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); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | schedule(); /* Wait for erase to finish. */ |
| 112 | remove_wait_queue(&wait_q, &wait); |
| 113 | |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 114 | /* Mark pages as unused */ |
| 115 | for (page = start_page; page < start_page + erase_pages; page++) |
| 116 | mark_page_unused(cxt, page); |
| 117 | |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 121 | static void mtdoops_inc_counter(struct mtdoops_context *cxt) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 122 | { |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 123 | cxt->nextpage++; |
Richard Purdie | ecd5b31 | 2008-07-26 09:17:41 +0100 | [diff] [blame] | 124 | if (cxt->nextpage >= cxt->oops_pages) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 125 | cxt->nextpage = 0; |
| 126 | cxt->nextcount++; |
| 127 | if (cxt->nextcount == 0xffffffff) |
| 128 | cxt->nextcount = 0; |
| 129 | |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 130 | if (page_is_used(cxt, cxt->nextpage)) { |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 131 | schedule_work(&cxt->work_erase); |
| 132 | return; |
| 133 | } |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 134 | |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 135 | printk(KERN_DEBUG "mtdoops: ready %d, %d (no erase)\n", |
| 136 | cxt->nextpage, cxt->nextcount); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 137 | cxt->ready = 1; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 140 | /* Scheduled work - when we can't proceed without erasing a block */ |
| 141 | static void mtdoops_workfunc_erase(struct work_struct *work) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 142 | { |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 143 | struct mtdoops_context *cxt = |
| 144 | container_of(work, struct mtdoops_context, work_erase); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 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); |
Richard Purdie | ecd5b31 | 2008-07-26 09:17:41 +0100 | [diff] [blame] | 155 | if (cxt->nextpage >= cxt->oops_pages) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 156 | cxt->nextpage = 0; |
| 157 | } |
| 158 | |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 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) { |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 164 | printk(KERN_ERR "mtdoops: block_isbad failed, aborting\n"); |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 165 | return; |
| 166 | } |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 167 | badblock: |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 168 | printk(KERN_WARNING "mtdoops: bad block at %08x\n", |
| 169 | cxt->nextpage * OOPS_PAGE_SIZE); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 170 | i++; |
| 171 | cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE); |
Richard Purdie | ecd5b31 | 2008-07-26 09:17:41 +0100 | [diff] [blame] | 172 | if (cxt->nextpage >= cxt->oops_pages) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 173 | cxt->nextpage = 0; |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 174 | if (i == cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE)) { |
| 175 | printk(KERN_ERR "mtdoops: all blocks bad!\n"); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | for (j = 0, ret = -1; (j < 3) && (ret < 0); j++) |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 181 | ret = mtdoops_erase_block(cxt, cxt->nextpage * OOPS_PAGE_SIZE); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 182 | |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 183 | if (ret >= 0) { |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 184 | printk(KERN_DEBUG "mtdoops: ready %d, %d\n", |
| 185 | cxt->nextpage, cxt->nextcount); |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 186 | cxt->ready = 1; |
| 187 | return; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 190 | if (mtd->block_markbad && ret == -EIO) { |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 191 | ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE); |
| 192 | if (ret < 0) { |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 193 | printk(KERN_ERR "mtdoops: block_markbad failed, aborting\n"); |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 194 | return; |
| 195 | } |
| 196 | } |
| 197 | goto badblock; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 200 | static void mtdoops_write(struct mtdoops_context *cxt, int panic) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 201 | { |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 202 | struct mtd_info *mtd = cxt->mtd; |
| 203 | size_t retlen; |
| 204 | int ret; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 205 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 206 | if (cxt->writecount < OOPS_PAGE_SIZE) |
| 207 | memset(cxt->oops_buf + cxt->writecount, 0xff, |
| 208 | OOPS_PAGE_SIZE - cxt->writecount); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 209 | |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 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, |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 215 | OOPS_PAGE_SIZE, &retlen, cxt->oops_buf); |
| 216 | |
| 217 | cxt->writecount = 0; |
| 218 | |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 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); |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 222 | mark_page_used(cxt, cxt->nextpage); |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 223 | |
| 224 | mtdoops_inc_counter(cxt); |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 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); |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 234 | } |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 235 | |
| 236 | static void find_next_position(struct mtdoops_context *cxt) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 237 | { |
| 238 | struct mtd_info *mtd = cxt->mtd; |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 239 | int ret, page, maxpos = 0; |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 240 | u32 count[2], maxcount = 0xffffffff; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 241 | size_t retlen; |
| 242 | |
| 243 | for (page = 0; page < cxt->oops_pages; page++) { |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 244 | /* Assume the page is used */ |
| 245 | mark_page_used(cxt, page); |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 246 | ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 8, &retlen, (u_char *) &count[0]); |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 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); |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 250 | continue; |
| 251 | } |
| 252 | |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 253 | if (count[0] == 0xffffffff && count[1] == 0xffffffff) |
| 254 | mark_page_unused(cxt, page); |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 255 | if (count[1] != MTDOOPS_KERNMSG_MAGIC) |
| 256 | continue; |
| 257 | if (count[0] == 0xffffffff) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 258 | continue; |
| 259 | if (maxcount == 0xffffffff) { |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 260 | maxcount = count[0]; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 261 | maxpos = page; |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 262 | } else if (count[0] < 0x40000000 && maxcount > 0xc0000000) { |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 263 | maxcount = count[0]; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 264 | maxpos = page; |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 265 | } else if (count[0] > maxcount && count[0] < 0xc0000000) { |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 266 | maxcount = count[0]; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 267 | maxpos = page; |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 268 | } else if (count[0] > maxcount && count[0] > 0xc0000000 |
| 269 | && maxcount > 0x80000000) { |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 270 | maxcount = count[0]; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 271 | maxpos = page; |
| 272 | } |
| 273 | } |
| 274 | if (maxcount == 0xffffffff) { |
| 275 | cxt->nextpage = 0; |
| 276 | cxt->nextcount = 1; |
Richard Purdie | 43b5693 | 2008-07-26 09:25:18 +0100 | [diff] [blame] | 277 | schedule_work(&cxt->work_erase); |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 278 | return; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | cxt->nextpage = maxpos; |
| 282 | cxt->nextcount = maxcount; |
| 283 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 284 | mtdoops_inc_counter(cxt); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
| 288 | static void mtdoops_notify_add(struct mtd_info *mtd) |
| 289 | { |
| 290 | struct mtdoops_context *cxt = &oops_cxt; |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 291 | u64 mtdoops_pages = mtd->size; |
| 292 | |
| 293 | do_div(mtdoops_pages, OOPS_PAGE_SIZE); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 294 | |
Adrian Hunter | e2a0f25 | 2009-02-16 18:21:35 +0200 | [diff] [blame] | 295 | if (cxt->name && !strcmp(mtd->name, cxt->name)) |
| 296 | cxt->mtd_index = mtd->index; |
| 297 | |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 298 | if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 299 | return; |
| 300 | |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 301 | if (mtd->size < mtd->erasesize * 2) { |
| 302 | printk(KERN_ERR "mtdoops: MTD partition %d not big enough for mtdoops\n", |
| 303 | mtd->index); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | |
Richard Purdie | 79dcd8e | 2008-01-29 10:25:55 +0000 | [diff] [blame] | 307 | if (mtd->erasesize < OOPS_PAGE_SIZE) { |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 308 | printk(KERN_ERR "mtdoops: eraseblock size of MTD partition %d too small\n", |
| 309 | mtd->index); |
Richard Purdie | 79dcd8e | 2008-01-29 10:25:55 +0000 | [diff] [blame] | 310 | return; |
| 311 | } |
| 312 | |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 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 | } |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 320 | cxt->mtd = mtd; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 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; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 325 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 326 | find_next_position(cxt); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 327 | |
Richard Purdie | 79dcd8e | 2008-01-29 10:25:55 +0000 | [diff] [blame] | 328 | printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static void mtdoops_notify_remove(struct mtd_info *mtd) |
| 332 | { |
| 333 | struct mtdoops_context *cxt = &oops_cxt; |
| 334 | |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 335 | if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 336 | return; |
| 337 | |
| 338 | cxt->mtd = NULL; |
| 339 | flush_scheduled_work(); |
| 340 | } |
| 341 | |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 342 | static void mtdoops_console_sync(void) |
| 343 | { |
| 344 | struct mtdoops_context *cxt = &oops_cxt; |
| 345 | struct mtd_info *mtd = cxt->mtd; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 346 | unsigned long flags; |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 347 | |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 348 | if (!cxt->ready || !mtd || cxt->writecount == 0) |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 349 | return; |
| 350 | |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 351 | /* |
| 352 | * Once ready is 0 and we've held the lock no further writes to the |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 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 | } |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 360 | cxt->ready = 0; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 361 | spin_unlock_irqrestore(&cxt->writecount_lock, flags); |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 362 | |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 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); |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 368 | } |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 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; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 375 | unsigned long flags; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 376 | |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 377 | if (!oops_in_progress) { |
| 378 | mtdoops_console_sync(); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 379 | return; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 382 | if (!cxt->ready || !mtd) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 383 | return; |
| 384 | |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 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 */ |
Adrian Hunter | 48ec00a | 2009-03-04 09:53:40 +0200 | [diff] [blame] | 389 | if (!cxt->ready) { |
| 390 | spin_unlock_irqrestore(&cxt->writecount_lock, flags); |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 391 | return; |
Adrian Hunter | 48ec00a | 2009-03-04 09:53:40 +0200 | [diff] [blame] | 392 | } |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 393 | |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 394 | if (cxt->writecount == 0) { |
| 395 | u32 *stamp = cxt->oops_buf; |
Richard Purdie | f0482ee | 2008-07-26 09:22:45 +0100 | [diff] [blame] | 396 | *stamp++ = cxt->nextcount; |
| 397 | *stamp = MTDOOPS_KERNMSG_MAGIC; |
| 398 | cxt->writecount = 8; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 401 | if (count + cxt->writecount > OOPS_PAGE_SIZE) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 402 | count = OOPS_PAGE_SIZE - cxt->writecount; |
| 403 | |
Peter Korsgaard | 235d620 | 2007-11-06 11:56:02 +0100 | [diff] [blame] | 404 | memcpy(cxt->oops_buf + cxt->writecount, s, count); |
| 405 | cxt->writecount += count; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 406 | |
| 407 | spin_unlock_irqrestore(&cxt->writecount_lock, flags); |
| 408 | |
| 409 | if (cxt->writecount == OOPS_PAGE_SIZE) |
| 410 | mtdoops_console_sync(); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static int __init mtdoops_console_setup(struct console *co, char *options) |
| 414 | { |
| 415 | struct mtdoops_context *cxt = co->data; |
| 416 | |
Adrian Hunter | e2a0f25 | 2009-02-16 18:21:35 +0200 | [diff] [blame] | 417 | if (cxt->mtd_index != -1 || cxt->name) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 418 | return -EBUSY; |
Adrian Hunter | e2a0f25 | 2009-02-16 18:21:35 +0200 | [diff] [blame] | 419 | if (options) { |
| 420 | cxt->name = kstrdup(options, GFP_KERNEL); |
| 421 | return 0; |
| 422 | } |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 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, |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 439 | .unblank = mtdoops_console_sync, |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 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); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 450 | if (!cxt->oops_buf) { |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 451 | printk(KERN_ERR "mtdoops: failed to allocate buffer workspace\n"); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 452 | return -ENOMEM; |
| 453 | } |
| 454 | |
Artem Bityutskiy | a15b124 | 2009-10-11 13:40:40 +0300 | [diff] [blame] | 455 | spin_lock_init(&cxt->writecount_lock); |
Richard Purdie | 6ce0a85 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 456 | INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase); |
| 457 | INIT_WORK(&cxt->work_write, mtdoops_workfunc_write); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 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); |
Adrian Hunter | e2a0f25 | 2009-02-16 18:21:35 +0200 | [diff] [blame] | 470 | kfree(cxt->name); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 471 | vfree(cxt->oops_buf); |
Simon Kagstrom | be95745 | 2009-10-29 13:41:11 +0100 | [diff] [blame^] | 472 | vfree(cxt->oops_page_used); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 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"); |