mtdoops.c 12 KB

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