mtdoops.c 12 KB

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