mtdoops.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * MTD Oops/Panic logger
  3. *
  4. * Copyright © 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/interrupt.h>
  32. #include <linux/mtd/mtd.h>
  33. #include <linux/kmsg_dump.h>
  34. /* Maximum MTD partition size */
  35. #define MTDOOPS_MAX_MTD_SIZE (8 * 1024 * 1024)
  36. #define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
  37. #define MTDOOPS_HEADER_SIZE 8
  38. static unsigned long record_size = 4096;
  39. module_param(record_size, ulong, 0400);
  40. MODULE_PARM_DESC(record_size,
  41. "record size for MTD OOPS pages in bytes (default 4096)");
  42. static char mtddev[80];
  43. module_param_string(mtddev, mtddev, 80, 0400);
  44. MODULE_PARM_DESC(mtddev,
  45. "name or index number of the MTD device to use");
  46. static int dump_oops = 1;
  47. module_param(dump_oops, int, 0600);
  48. MODULE_PARM_DESC(dump_oops,
  49. "set to 1 to dump oopses, 0 to only dump panics (default 1)");
  50. static struct mtdoops_context {
  51. struct kmsg_dumper dump;
  52. int mtd_index;
  53. struct work_struct work_erase;
  54. struct work_struct work_write;
  55. struct mtd_info *mtd;
  56. int oops_pages;
  57. int nextpage;
  58. int nextcount;
  59. unsigned long *oops_page_used;
  60. void *oops_buf;
  61. } oops_cxt;
  62. static void mark_page_used(struct mtdoops_context *cxt, int page)
  63. {
  64. set_bit(page, cxt->oops_page_used);
  65. }
  66. static void mark_page_unused(struct mtdoops_context *cxt, int page)
  67. {
  68. clear_bit(page, cxt->oops_page_used);
  69. }
  70. static int page_is_used(struct mtdoops_context *cxt, int page)
  71. {
  72. return test_bit(page, cxt->oops_page_used);
  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. int ret;
  82. int page;
  83. erase.addr = offset;
  84. erase.len = mtd->erasesize;
  85. ret = mtd_erase(mtd, &erase);
  86. if (ret) {
  87. printk(KERN_WARNING "mtdoops: erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
  88. (unsigned long long)erase.addr,
  89. (unsigned long long)erase.len, mtddev);
  90. return ret;
  91. }
  92. /* Mark pages as unused */
  93. for (page = start_page; page < start_page + erase_pages; page++)
  94. mark_page_unused(cxt, page);
  95. return 0;
  96. }
  97. static void mtdoops_inc_counter(struct mtdoops_context *cxt)
  98. {
  99. cxt->nextpage++;
  100. if (cxt->nextpage >= cxt->oops_pages)
  101. cxt->nextpage = 0;
  102. cxt->nextcount++;
  103. if (cxt->nextcount == 0xffffffff)
  104. cxt->nextcount = 0;
  105. if (page_is_used(cxt, cxt->nextpage)) {
  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. }
  112. /* Scheduled work - when we can't proceed without erasing a block */
  113. static void mtdoops_workfunc_erase(struct work_struct *work)
  114. {
  115. struct mtdoops_context *cxt =
  116. container_of(work, struct mtdoops_context, work_erase);
  117. struct mtd_info *mtd = cxt->mtd;
  118. int i = 0, j, ret, mod;
  119. /* We were unregistered */
  120. if (!mtd)
  121. return;
  122. mod = (cxt->nextpage * record_size) % mtd->erasesize;
  123. if (mod != 0) {
  124. cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / record_size);
  125. if (cxt->nextpage >= cxt->oops_pages)
  126. cxt->nextpage = 0;
  127. }
  128. while ((ret = mtd_block_isbad(mtd, cxt->nextpage * record_size)) > 0) {
  129. badblock:
  130. printk(KERN_WARNING "mtdoops: bad block at %08lx\n",
  131. cxt->nextpage * record_size);
  132. i++;
  133. cxt->nextpage = cxt->nextpage + (mtd->erasesize / record_size);
  134. if (cxt->nextpage >= cxt->oops_pages)
  135. cxt->nextpage = 0;
  136. if (i == cxt->oops_pages / (mtd->erasesize / record_size)) {
  137. printk(KERN_ERR "mtdoops: all blocks bad!\n");
  138. return;
  139. }
  140. }
  141. if (ret < 0) {
  142. printk(KERN_ERR "mtdoops: mtd_block_isbad failed, aborting\n");
  143. return;
  144. }
  145. for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
  146. ret = mtdoops_erase_block(cxt, cxt->nextpage * record_size);
  147. if (ret >= 0) {
  148. printk(KERN_DEBUG "mtdoops: ready %d, %d\n",
  149. cxt->nextpage, cxt->nextcount);
  150. return;
  151. }
  152. if (ret == -EIO) {
  153. ret = mtd_block_markbad(mtd, cxt->nextpage * record_size);
  154. if (ret < 0 && ret != -EOPNOTSUPP) {
  155. printk(KERN_ERR "mtdoops: block_markbad failed, aborting\n");
  156. return;
  157. }
  158. }
  159. goto badblock;
  160. }
  161. static void mtdoops_write(struct mtdoops_context *cxt, int panic)
  162. {
  163. struct mtd_info *mtd = cxt->mtd;
  164. size_t retlen;
  165. u32 *hdr;
  166. int ret;
  167. /* Add mtdoops header to the buffer */
  168. hdr = cxt->oops_buf;
  169. hdr[0] = cxt->nextcount;
  170. hdr[1] = MTDOOPS_KERNMSG_MAGIC;
  171. if (panic) {
  172. ret = mtd_panic_write(mtd, cxt->nextpage * record_size,
  173. record_size, &retlen, cxt->oops_buf);
  174. if (ret == -EOPNOTSUPP) {
  175. printk(KERN_ERR "mtdoops: Cannot write from panic without panic_write\n");
  176. return;
  177. }
  178. } else
  179. ret = mtd_write(mtd, cxt->nextpage * record_size,
  180. record_size, &retlen, cxt->oops_buf);
  181. if (retlen != record_size || ret < 0)
  182. printk(KERN_ERR "mtdoops: write failure at %ld (%td of %ld written), error %d\n",
  183. cxt->nextpage * record_size, retlen, record_size, ret);
  184. mark_page_used(cxt, cxt->nextpage);
  185. memset(cxt->oops_buf, 0xff, record_size);
  186. mtdoops_inc_counter(cxt);
  187. }
  188. static void mtdoops_workfunc_write(struct work_struct *work)
  189. {
  190. struct mtdoops_context *cxt =
  191. container_of(work, struct mtdoops_context, work_write);
  192. mtdoops_write(cxt, 0);
  193. }
  194. static void find_next_position(struct mtdoops_context *cxt)
  195. {
  196. struct mtd_info *mtd = cxt->mtd;
  197. int ret, page, maxpos = 0;
  198. u32 count[2], maxcount = 0xffffffff;
  199. size_t retlen;
  200. for (page = 0; page < cxt->oops_pages; page++) {
  201. if (mtd_block_isbad(mtd, page * record_size))
  202. continue;
  203. /* Assume the page is used */
  204. mark_page_used(cxt, page);
  205. ret = mtd_read(mtd, page * record_size, MTDOOPS_HEADER_SIZE,
  206. &retlen, (u_char *)&count[0]);
  207. if (retlen != MTDOOPS_HEADER_SIZE ||
  208. (ret < 0 && !mtd_is_bitflip(ret))) {
  209. printk(KERN_ERR "mtdoops: read failure at %ld (%td of %d read), err %d\n",
  210. page * record_size, retlen,
  211. MTDOOPS_HEADER_SIZE, ret);
  212. continue;
  213. }
  214. if (count[0] == 0xffffffff && count[1] == 0xffffffff)
  215. mark_page_unused(cxt, page);
  216. if (count[0] == 0xffffffff || count[1] != MTDOOPS_KERNMSG_MAGIC)
  217. continue;
  218. if (maxcount == 0xffffffff) {
  219. maxcount = count[0];
  220. maxpos = page;
  221. } else if (count[0] < 0x40000000 && maxcount > 0xc0000000) {
  222. maxcount = count[0];
  223. maxpos = page;
  224. } else if (count[0] > maxcount && count[0] < 0xc0000000) {
  225. maxcount = count[0];
  226. maxpos = page;
  227. } else if (count[0] > maxcount && count[0] > 0xc0000000
  228. && maxcount > 0x80000000) {
  229. maxcount = count[0];
  230. maxpos = page;
  231. }
  232. }
  233. if (maxcount == 0xffffffff) {
  234. cxt->nextpage = cxt->oops_pages - 1;
  235. cxt->nextcount = 0;
  236. }
  237. else {
  238. cxt->nextpage = maxpos;
  239. cxt->nextcount = maxcount;
  240. }
  241. mtdoops_inc_counter(cxt);
  242. }
  243. static void mtdoops_do_dump(struct kmsg_dumper *dumper,
  244. enum kmsg_dump_reason reason)
  245. {
  246. struct mtdoops_context *cxt = container_of(dumper,
  247. struct mtdoops_context, dump);
  248. /* Only dump oopses if dump_oops is set */
  249. if (reason == KMSG_DUMP_OOPS && !dump_oops)
  250. return;
  251. kmsg_dump_get_buffer(dumper, true, cxt->oops_buf + MTDOOPS_HEADER_SIZE,
  252. record_size - MTDOOPS_HEADER_SIZE, NULL);
  253. /* Panics must be written immediately */
  254. if (reason != KMSG_DUMP_OOPS)
  255. mtdoops_write(cxt, 1);
  256. /* For other cases, schedule work to write it "nicely" */
  257. schedule_work(&cxt->work_write);
  258. }
  259. static void mtdoops_notify_add(struct mtd_info *mtd)
  260. {
  261. struct mtdoops_context *cxt = &oops_cxt;
  262. u64 mtdoops_pages = div_u64(mtd->size, record_size);
  263. int err;
  264. if (!strcmp(mtd->name, mtddev))
  265. cxt->mtd_index = mtd->index;
  266. if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
  267. return;
  268. if (mtd->size < mtd->erasesize * 2) {
  269. printk(KERN_ERR "mtdoops: MTD partition %d not big enough for mtdoops\n",
  270. mtd->index);
  271. return;
  272. }
  273. if (mtd->erasesize < record_size) {
  274. printk(KERN_ERR "mtdoops: eraseblock size of MTD partition %d too small\n",
  275. mtd->index);
  276. return;
  277. }
  278. if (mtd->size > MTDOOPS_MAX_MTD_SIZE) {
  279. printk(KERN_ERR "mtdoops: mtd%d is too large (limit is %d MiB)\n",
  280. mtd->index, MTDOOPS_MAX_MTD_SIZE / 1024 / 1024);
  281. return;
  282. }
  283. /* oops_page_used is a bit field */
  284. cxt->oops_page_used =
  285. vmalloc(array_size(sizeof(unsigned long),
  286. DIV_ROUND_UP(mtdoops_pages,
  287. BITS_PER_LONG)));
  288. if (!cxt->oops_page_used) {
  289. printk(KERN_ERR "mtdoops: could not allocate page array\n");
  290. return;
  291. }
  292. cxt->dump.max_reason = KMSG_DUMP_OOPS;
  293. cxt->dump.dump = mtdoops_do_dump;
  294. err = kmsg_dump_register(&cxt->dump);
  295. if (err) {
  296. printk(KERN_ERR "mtdoops: registering kmsg dumper failed, error %d\n", err);
  297. vfree(cxt->oops_page_used);
  298. cxt->oops_page_used = NULL;
  299. return;
  300. }
  301. cxt->mtd = mtd;
  302. cxt->oops_pages = (int)mtd->size / record_size;
  303. find_next_position(cxt);
  304. printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
  305. }
  306. static void mtdoops_notify_remove(struct mtd_info *mtd)
  307. {
  308. struct mtdoops_context *cxt = &oops_cxt;
  309. if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
  310. return;
  311. if (kmsg_dump_unregister(&cxt->dump) < 0)
  312. printk(KERN_WARNING "mtdoops: could not unregister kmsg_dumper\n");
  313. cxt->mtd = NULL;
  314. flush_work(&cxt->work_erase);
  315. flush_work(&cxt->work_write);
  316. }
  317. static struct mtd_notifier mtdoops_notifier = {
  318. .add = mtdoops_notify_add,
  319. .remove = mtdoops_notify_remove,
  320. };
  321. static int __init mtdoops_init(void)
  322. {
  323. struct mtdoops_context *cxt = &oops_cxt;
  324. int mtd_index;
  325. char *endp;
  326. if (strlen(mtddev) == 0) {
  327. printk(KERN_ERR "mtdoops: mtd device (mtddev=name/number) must be supplied\n");
  328. return -EINVAL;
  329. }
  330. if ((record_size & 4095) != 0) {
  331. printk(KERN_ERR "mtdoops: record_size must be a multiple of 4096\n");
  332. return -EINVAL;
  333. }
  334. if (record_size < 4096) {
  335. printk(KERN_ERR "mtdoops: record_size must be over 4096 bytes\n");
  336. return -EINVAL;
  337. }
  338. /* Setup the MTD device to use */
  339. cxt->mtd_index = -1;
  340. mtd_index = simple_strtoul(mtddev, &endp, 0);
  341. if (*endp == '\0')
  342. cxt->mtd_index = mtd_index;
  343. cxt->oops_buf = vmalloc(record_size);
  344. if (!cxt->oops_buf) {
  345. printk(KERN_ERR "mtdoops: failed to allocate buffer workspace\n");
  346. return -ENOMEM;
  347. }
  348. memset(cxt->oops_buf, 0xff, record_size);
  349. INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
  350. INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
  351. register_mtd_user(&mtdoops_notifier);
  352. return 0;
  353. }
  354. static void __exit mtdoops_exit(void)
  355. {
  356. struct mtdoops_context *cxt = &oops_cxt;
  357. unregister_mtd_user(&mtdoops_notifier);
  358. vfree(cxt->oops_buf);
  359. vfree(cxt->oops_page_used);
  360. }
  361. module_init(mtdoops_init);
  362. module_exit(mtdoops_exit);
  363. MODULE_LICENSE("GPL");
  364. MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
  365. MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");