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