mtdoops.c 11 KB

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