mtdoops.c 12 KB

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