mtdoops.c 8.7 KB

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