mtdoops.c 11 KB

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