mtdoops.c 9.4 KB

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