mtdoops.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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/spinlock.h>
  31. #include <linux/mtd/mtd.h>
  32. #define OOPS_PAGE_SIZE 4096
  33. struct mtdoops_context {
  34. int mtd_index;
  35. struct work_struct work_erase;
  36. struct work_struct work_write;
  37. struct mtd_info *mtd;
  38. int oops_pages;
  39. int nextpage;
  40. int nextcount;
  41. void *oops_buf;
  42. /* writecount and disabling ready are spin lock protected */
  43. spinlock_t writecount_lock;
  44. int ready;
  45. int writecount;
  46. } oops_cxt;
  47. static void mtdoops_erase_callback(struct erase_info *done)
  48. {
  49. wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
  50. wake_up(wait_q);
  51. }
  52. static int mtdoops_erase_block(struct mtd_info *mtd, int offset)
  53. {
  54. struct erase_info erase;
  55. DECLARE_WAITQUEUE(wait, current);
  56. wait_queue_head_t wait_q;
  57. int ret;
  58. init_waitqueue_head(&wait_q);
  59. erase.mtd = mtd;
  60. erase.callback = mtdoops_erase_callback;
  61. erase.addr = offset;
  62. if (mtd->erasesize < OOPS_PAGE_SIZE)
  63. erase.len = OOPS_PAGE_SIZE;
  64. else
  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_workfunc_write(struct work_struct *work)
  165. {
  166. struct mtdoops_context *cxt =
  167. container_of(work, struct mtdoops_context, work_write);
  168. struct mtd_info *mtd = cxt->mtd;
  169. size_t retlen;
  170. int ret;
  171. if (cxt->writecount < OOPS_PAGE_SIZE)
  172. memset(cxt->oops_buf + cxt->writecount, 0xff,
  173. OOPS_PAGE_SIZE - cxt->writecount);
  174. ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
  175. OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
  176. cxt->writecount = 0;
  177. if ((retlen != OOPS_PAGE_SIZE) || (ret < 0))
  178. printk(KERN_ERR "mtdoops: Write failure at %d (%td of %d written), err %d.\n",
  179. cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret);
  180. mtdoops_inc_counter(cxt);
  181. }
  182. static void find_next_position(struct mtdoops_context *cxt)
  183. {
  184. struct mtd_info *mtd = cxt->mtd;
  185. int ret, page, maxpos = 0;
  186. u32 count, maxcount = 0xffffffff;
  187. size_t retlen;
  188. for (page = 0; page < cxt->oops_pages; page++) {
  189. ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 4, &retlen, (u_char *) &count);
  190. if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) {
  191. printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)"
  192. ", err %d.\n", page * OOPS_PAGE_SIZE, retlen, ret);
  193. continue;
  194. }
  195. if (count == 0xffffffff)
  196. continue;
  197. if (maxcount == 0xffffffff) {
  198. maxcount = count;
  199. maxpos = page;
  200. } else if ((count < 0x40000000) && (maxcount > 0xc0000000)) {
  201. maxcount = count;
  202. maxpos = page;
  203. } else if ((count > maxcount) && (count < 0xc0000000)) {
  204. maxcount = count;
  205. maxpos = page;
  206. } else if ((count > maxcount) && (count > 0xc0000000)
  207. && (maxcount > 0x80000000)) {
  208. maxcount = count;
  209. maxpos = page;
  210. }
  211. }
  212. if (maxcount == 0xffffffff) {
  213. cxt->nextpage = 0;
  214. cxt->nextcount = 1;
  215. cxt->ready = 1;
  216. printk(KERN_DEBUG "mtdoops: Ready %d, %d (first init)\n",
  217. cxt->nextpage, cxt->nextcount);
  218. return;
  219. }
  220. cxt->nextpage = maxpos;
  221. cxt->nextcount = maxcount;
  222. mtdoops_inc_counter(cxt);
  223. }
  224. static void mtdoops_notify_add(struct mtd_info *mtd)
  225. {
  226. struct mtdoops_context *cxt = &oops_cxt;
  227. if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
  228. return;
  229. if (mtd->size < (mtd->erasesize * 2)) {
  230. printk(KERN_ERR "MTD partition %d not big enough for mtdoops\n",
  231. mtd->index);
  232. return;
  233. }
  234. cxt->mtd = mtd;
  235. cxt->oops_pages = mtd->size / OOPS_PAGE_SIZE;
  236. find_next_position(cxt);
  237. printk(KERN_DEBUG "mtdoops: Attached to MTD device %d\n", mtd->index);
  238. }
  239. static void mtdoops_notify_remove(struct mtd_info *mtd)
  240. {
  241. struct mtdoops_context *cxt = &oops_cxt;
  242. if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
  243. return;
  244. cxt->mtd = NULL;
  245. flush_scheduled_work();
  246. }
  247. static void mtdoops_console_sync(void)
  248. {
  249. struct mtdoops_context *cxt = &oops_cxt;
  250. struct mtd_info *mtd = cxt->mtd;
  251. unsigned long flags;
  252. if (!cxt->ready || !mtd || cxt->writecount == 0)
  253. return;
  254. /*
  255. * Once ready is 0 and we've held the lock no further writes to the
  256. * buffer will happen
  257. */
  258. spin_lock_irqsave(&cxt->writecount_lock, flags);
  259. if (!cxt->ready) {
  260. spin_unlock_irqrestore(&cxt->writecount_lock, flags);
  261. return;
  262. }
  263. cxt->ready = 0;
  264. spin_unlock_irqrestore(&cxt->writecount_lock, flags);
  265. schedule_work(&cxt->work_write);
  266. }
  267. static void
  268. mtdoops_console_write(struct console *co, const char *s, unsigned int count)
  269. {
  270. struct mtdoops_context *cxt = co->data;
  271. struct mtd_info *mtd = cxt->mtd;
  272. unsigned long flags;
  273. if (!oops_in_progress) {
  274. mtdoops_console_sync();
  275. return;
  276. }
  277. if (!cxt->ready || !mtd)
  278. return;
  279. /* Locking on writecount ensures sequential writes to the buffer */
  280. spin_lock_irqsave(&cxt->writecount_lock, flags);
  281. /* Check ready status didn't change whilst waiting for the lock */
  282. if (!cxt->ready)
  283. return;
  284. if (cxt->writecount == 0) {
  285. u32 *stamp = cxt->oops_buf;
  286. *stamp = cxt->nextcount;
  287. cxt->writecount = 4;
  288. }
  289. if ((count + cxt->writecount) > OOPS_PAGE_SIZE)
  290. count = OOPS_PAGE_SIZE - cxt->writecount;
  291. memcpy(cxt->oops_buf + cxt->writecount, s, count);
  292. cxt->writecount += count;
  293. spin_unlock_irqrestore(&cxt->writecount_lock, flags);
  294. if (cxt->writecount == OOPS_PAGE_SIZE)
  295. mtdoops_console_sync();
  296. }
  297. static int __init mtdoops_console_setup(struct console *co, char *options)
  298. {
  299. struct mtdoops_context *cxt = co->data;
  300. if (cxt->mtd_index != -1)
  301. return -EBUSY;
  302. if (co->index == -1)
  303. return -EINVAL;
  304. cxt->mtd_index = co->index;
  305. return 0;
  306. }
  307. static struct mtd_notifier mtdoops_notifier = {
  308. .add = mtdoops_notify_add,
  309. .remove = mtdoops_notify_remove,
  310. };
  311. static struct console mtdoops_console = {
  312. .name = "ttyMTD",
  313. .write = mtdoops_console_write,
  314. .setup = mtdoops_console_setup,
  315. .unblank = mtdoops_console_sync,
  316. .flags = CON_PRINTBUFFER,
  317. .index = -1,
  318. .data = &oops_cxt,
  319. };
  320. static int __init mtdoops_console_init(void)
  321. {
  322. struct mtdoops_context *cxt = &oops_cxt;
  323. cxt->mtd_index = -1;
  324. cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE);
  325. if (!cxt->oops_buf) {
  326. printk(KERN_ERR "Failed to allocate oops buffer workspace\n");
  327. return -ENOMEM;
  328. }
  329. INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
  330. INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
  331. register_console(&mtdoops_console);
  332. register_mtd_user(&mtdoops_notifier);
  333. return 0;
  334. }
  335. static void __exit mtdoops_console_exit(void)
  336. {
  337. struct mtdoops_context *cxt = &oops_cxt;
  338. unregister_mtd_user(&mtdoops_notifier);
  339. unregister_console(&mtdoops_console);
  340. vfree(cxt->oops_buf);
  341. }
  342. subsys_initcall(mtdoops_console_init);
  343. module_exit(mtdoops_console_exit);
  344. MODULE_LICENSE("GPL");
  345. MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
  346. MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");