mtdoops.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/console.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/sched.h>
  29. #include <linux/wait.h>
  30. #include <linux/delay.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/mtd/mtd.h>
  34. #define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
  35. #define OOPS_PAGE_SIZE 4096
  36. static struct mtdoops_context {
  37. int mtd_index;
  38. struct work_struct work_erase;
  39. struct work_struct work_write;
  40. struct mtd_info *mtd;
  41. int oops_pages;
  42. int nextpage;
  43. int nextcount;
  44. void *oops_buf;
  45. /* writecount and disabling ready are spin lock protected */
  46. spinlock_t writecount_lock;
  47. int ready;
  48. int writecount;
  49. } oops_cxt;
  50. static void mtdoops_erase_callback(struct erase_info *done)
  51. {
  52. wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
  53. wake_up(wait_q);
  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. init_waitqueue_head(&wait_q);
  62. erase.mtd = mtd;
  63. erase.callback = mtdoops_erase_callback;
  64. erase.addr = offset;
  65. erase.len = mtd->erasesize;
  66. erase.priv = (u_long)&wait_q;
  67. set_current_state(TASK_INTERRUPTIBLE);
  68. add_wait_queue(&wait_q, &wait);
  69. ret = mtd->erase(mtd, &erase);
  70. if (ret) {
  71. set_current_state(TASK_RUNNING);
  72. remove_wait_queue(&wait_q, &wait);
  73. printk (KERN_WARNING "mtdoops: erase of region [0x%x, 0x%x] "
  74. "on \"%s\" failed\n",
  75. erase.addr, erase.len, mtd->name);
  76. return ret;
  77. }
  78. schedule(); /* Wait for erase to finish. */
  79. remove_wait_queue(&wait_q, &wait);
  80. return 0;
  81. }
  82. static void mtdoops_inc_counter(struct mtdoops_context *cxt)
  83. {
  84. struct mtd_info *mtd = cxt->mtd;
  85. size_t retlen;
  86. u32 count;
  87. int ret;
  88. cxt->nextpage++;
  89. if (cxt->nextpage >= cxt->oops_pages)
  90. cxt->nextpage = 0;
  91. cxt->nextcount++;
  92. if (cxt->nextcount == 0xffffffff)
  93. cxt->nextcount = 0;
  94. ret = mtd->read(mtd, cxt->nextpage * OOPS_PAGE_SIZE, 4,
  95. &retlen, (u_char *) &count);
  96. if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) {
  97. printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)"
  98. ", err %d.\n", cxt->nextpage * OOPS_PAGE_SIZE,
  99. retlen, ret);
  100. schedule_work(&cxt->work_erase);
  101. return;
  102. }
  103. /* See if we need to erase the next block */
  104. if (count != 0xffffffff) {
  105. schedule_work(&cxt->work_erase);
  106. return;
  107. }
  108. printk(KERN_DEBUG "mtdoops: Ready %d, %d (no erase)\n",
  109. cxt->nextpage, cxt->nextcount);
  110. cxt->ready = 1;
  111. }
  112. /* Scheduled work - when we can't proceed without erasing a block */
  113. static void mtdoops_workfunc_erase(struct work_struct *work)
  114. {
  115. struct mtdoops_context *cxt =
  116. container_of(work, struct mtdoops_context, work_erase);
  117. struct mtd_info *mtd = cxt->mtd;
  118. int i = 0, j, ret, mod;
  119. /* We were unregistered */
  120. if (!mtd)
  121. return;
  122. mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize;
  123. if (mod != 0) {
  124. cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE);
  125. if (cxt->nextpage >= cxt->oops_pages)
  126. cxt->nextpage = 0;
  127. }
  128. while (mtd->block_isbad) {
  129. ret = mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
  130. if (!ret)
  131. break;
  132. if (ret < 0) {
  133. printk(KERN_ERR "mtdoops: block_isbad failed, aborting.\n");
  134. return;
  135. }
  136. badblock:
  137. printk(KERN_WARNING "mtdoops: Bad block at %08x\n",
  138. cxt->nextpage * OOPS_PAGE_SIZE);
  139. i++;
  140. cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE);
  141. if (cxt->nextpage >= cxt->oops_pages)
  142. cxt->nextpage = 0;
  143. if (i == (cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE))) {
  144. printk(KERN_ERR "mtdoops: All blocks bad!\n");
  145. return;
  146. }
  147. }
  148. for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
  149. ret = mtdoops_erase_block(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
  150. if (ret >= 0) {
  151. printk(KERN_DEBUG "mtdoops: Ready %d, %d \n", cxt->nextpage, cxt->nextcount);
  152. cxt->ready = 1;
  153. return;
  154. }
  155. if (mtd->block_markbad && (ret == -EIO)) {
  156. ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
  157. if (ret < 0) {
  158. printk(KERN_ERR "mtdoops: block_markbad failed, aborting.\n");
  159. return;
  160. }
  161. }
  162. goto badblock;
  163. }
  164. static void mtdoops_write(struct mtdoops_context *cxt, int panic)
  165. {
  166. struct mtd_info *mtd = cxt->mtd;
  167. size_t retlen;
  168. int ret;
  169. if (cxt->writecount < OOPS_PAGE_SIZE)
  170. memset(cxt->oops_buf + cxt->writecount, 0xff,
  171. OOPS_PAGE_SIZE - cxt->writecount);
  172. if (panic)
  173. ret = mtd->panic_write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
  174. OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
  175. else
  176. ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
  177. OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
  178. cxt->writecount = 0;
  179. if ((retlen != OOPS_PAGE_SIZE) || (ret < 0))
  180. printk(KERN_ERR "mtdoops: Write failure at %d (%td of %d written), err %d.\n",
  181. cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret);
  182. mtdoops_inc_counter(cxt);
  183. }
  184. static void mtdoops_workfunc_write(struct work_struct *work)
  185. {
  186. struct mtdoops_context *cxt =
  187. container_of(work, struct mtdoops_context, work_write);
  188. mtdoops_write(cxt, 0);
  189. }
  190. static void find_next_position(struct mtdoops_context *cxt)
  191. {
  192. struct mtd_info *mtd = cxt->mtd;
  193. int ret, page, maxpos = 0;
  194. u32 count[2], maxcount = 0xffffffff;
  195. size_t retlen;
  196. for (page = 0; page < cxt->oops_pages; page++) {
  197. ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 8, &retlen, (u_char *) &count[0]);
  198. if ((retlen != 8) || ((ret < 0) && (ret != -EUCLEAN))) {
  199. printk(KERN_ERR "mtdoops: Read failure at %d (%td of 8 read)"
  200. ", err %d.\n", page * OOPS_PAGE_SIZE, retlen, ret);
  201. continue;
  202. }
  203. if (count[1] != MTDOOPS_KERNMSG_MAGIC)
  204. continue;
  205. if (count[0] == 0xffffffff)
  206. continue;
  207. if (maxcount == 0xffffffff) {
  208. maxcount = count[0];
  209. maxpos = page;
  210. } else if ((count[0] < 0x40000000) && (maxcount > 0xc0000000)) {
  211. maxcount = count[0];
  212. maxpos = page;
  213. } else if ((count[0] > maxcount) && (count[0] < 0xc0000000)) {
  214. maxcount = count[0];
  215. maxpos = page;
  216. } else if ((count[0] > maxcount) && (count[0] > 0xc0000000)
  217. && (maxcount > 0x80000000)) {
  218. maxcount = count[0];
  219. maxpos = page;
  220. }
  221. }
  222. if (maxcount == 0xffffffff) {
  223. cxt->nextpage = 0;
  224. cxt->nextcount = 1;
  225. schedule_work(&cxt->work_erase);
  226. return;
  227. }
  228. cxt->nextpage = maxpos;
  229. cxt->nextcount = maxcount;
  230. mtdoops_inc_counter(cxt);
  231. }
  232. static void mtdoops_notify_add(struct mtd_info *mtd)
  233. {
  234. struct mtdoops_context *cxt = &oops_cxt;
  235. if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
  236. return;
  237. if (mtd->size < (mtd->erasesize * 2)) {
  238. printk(KERN_ERR "MTD partition %d not big enough for mtdoops\n",
  239. mtd->index);
  240. return;
  241. }
  242. if (mtd->erasesize < OOPS_PAGE_SIZE) {
  243. printk(KERN_ERR "Eraseblock size of MTD partition %d too small\n",
  244. mtd->index);
  245. return;
  246. }
  247. cxt->mtd = mtd;
  248. cxt->oops_pages = mtd->size / OOPS_PAGE_SIZE;
  249. find_next_position(cxt);
  250. printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
  251. }
  252. static void mtdoops_notify_remove(struct mtd_info *mtd)
  253. {
  254. struct mtdoops_context *cxt = &oops_cxt;
  255. if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
  256. return;
  257. cxt->mtd = NULL;
  258. flush_scheduled_work();
  259. }
  260. static void mtdoops_console_sync(void)
  261. {
  262. struct mtdoops_context *cxt = &oops_cxt;
  263. struct mtd_info *mtd = cxt->mtd;
  264. unsigned long flags;
  265. if (!cxt->ready || !mtd || cxt->writecount == 0)
  266. return;
  267. /*
  268. * Once ready is 0 and we've held the lock no further writes to the
  269. * buffer will happen
  270. */
  271. spin_lock_irqsave(&cxt->writecount_lock, flags);
  272. if (!cxt->ready) {
  273. spin_unlock_irqrestore(&cxt->writecount_lock, flags);
  274. return;
  275. }
  276. cxt->ready = 0;
  277. spin_unlock_irqrestore(&cxt->writecount_lock, flags);
  278. if (mtd->panic_write && in_interrupt())
  279. /* Interrupt context, we're going to panic so try and log */
  280. mtdoops_write(cxt, 1);
  281. else
  282. schedule_work(&cxt->work_write);
  283. }
  284. static void
  285. mtdoops_console_write(struct console *co, const char *s, unsigned int count)
  286. {
  287. struct mtdoops_context *cxt = co->data;
  288. struct mtd_info *mtd = cxt->mtd;
  289. unsigned long flags;
  290. if (!oops_in_progress) {
  291. mtdoops_console_sync();
  292. return;
  293. }
  294. if (!cxt->ready || !mtd)
  295. return;
  296. /* Locking on writecount ensures sequential writes to the buffer */
  297. spin_lock_irqsave(&cxt->writecount_lock, flags);
  298. /* Check ready status didn't change whilst waiting for the lock */
  299. if (!cxt->ready)
  300. return;
  301. if (cxt->writecount == 0) {
  302. u32 *stamp = cxt->oops_buf;
  303. *stamp++ = cxt->nextcount;
  304. *stamp = MTDOOPS_KERNMSG_MAGIC;
  305. cxt->writecount = 8;
  306. }
  307. if ((count + cxt->writecount) > OOPS_PAGE_SIZE)
  308. count = OOPS_PAGE_SIZE - cxt->writecount;
  309. memcpy(cxt->oops_buf + cxt->writecount, s, count);
  310. cxt->writecount += count;
  311. spin_unlock_irqrestore(&cxt->writecount_lock, flags);
  312. if (cxt->writecount == OOPS_PAGE_SIZE)
  313. mtdoops_console_sync();
  314. }
  315. static int __init mtdoops_console_setup(struct console *co, char *options)
  316. {
  317. struct mtdoops_context *cxt = co->data;
  318. if (cxt->mtd_index != -1)
  319. return -EBUSY;
  320. if (co->index == -1)
  321. return -EINVAL;
  322. cxt->mtd_index = co->index;
  323. return 0;
  324. }
  325. static struct mtd_notifier mtdoops_notifier = {
  326. .add = mtdoops_notify_add,
  327. .remove = mtdoops_notify_remove,
  328. };
  329. static struct console mtdoops_console = {
  330. .name = "ttyMTD",
  331. .write = mtdoops_console_write,
  332. .setup = mtdoops_console_setup,
  333. .unblank = mtdoops_console_sync,
  334. .index = -1,
  335. .data = &oops_cxt,
  336. };
  337. static int __init mtdoops_console_init(void)
  338. {
  339. struct mtdoops_context *cxt = &oops_cxt;
  340. cxt->mtd_index = -1;
  341. cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE);
  342. if (!cxt->oops_buf) {
  343. printk(KERN_ERR "Failed to allocate mtdoops buffer workspace\n");
  344. return -ENOMEM;
  345. }
  346. INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
  347. INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
  348. register_console(&mtdoops_console);
  349. register_mtd_user(&mtdoops_notifier);
  350. return 0;
  351. }
  352. static void __exit mtdoops_console_exit(void)
  353. {
  354. struct mtdoops_context *cxt = &oops_cxt;
  355. unregister_mtd_user(&mtdoops_notifier);
  356. unregister_console(&mtdoops_console);
  357. vfree(cxt->oops_buf);
  358. }
  359. subsys_initcall(mtdoops_console_init);
  360. module_exit(mtdoops_console_exit);
  361. MODULE_LICENSE("GPL");
  362. MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
  363. MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");