12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24b23aff0SRichard Purdie /*
34b23aff0SRichard Purdie * MTD Oops/Panic logger
44b23aff0SRichard Purdie *
5a1452a37SDavid Woodhouse * Copyright © 2007 Nokia Corporation. All rights reserved.
64b23aff0SRichard Purdie *
74b23aff0SRichard Purdie * Author: Richard Purdie <rpurdie@openedhand.com>
84b23aff0SRichard Purdie */
94b23aff0SRichard Purdie
1043cfba56SRay Zhang #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1143cfba56SRay Zhang
124b23aff0SRichard Purdie #include <linux/kernel.h>
134b23aff0SRichard Purdie #include <linux/module.h>
144b23aff0SRichard Purdie #include <linux/console.h>
154b23aff0SRichard Purdie #include <linux/vmalloc.h>
164b23aff0SRichard Purdie #include <linux/workqueue.h>
174b23aff0SRichard Purdie #include <linux/sched.h>
184b23aff0SRichard Purdie #include <linux/wait.h>
19621e4f8eSRichard Purdie #include <linux/delay.h>
20f9f7dd22SDavid Woodhouse #include <linux/interrupt.h>
21aa641a22SJean-Marc Eurin #include <linux/timekeeping.h>
224b23aff0SRichard Purdie #include <linux/mtd/mtd.h>
232e386e4bSSimon Kagstrom #include <linux/kmsg_dump.h>
244b23aff0SRichard Purdie
251114e3d0SSimon Kagstrom /* Maximum MTD partition size */
261114e3d0SSimon Kagstrom #define MTDOOPS_MAX_MTD_SIZE (8 * 1024 * 1024)
271114e3d0SSimon Kagstrom
289507b0c8SSimon Kagstrom static unsigned long record_size = 4096;
299507b0c8SSimon Kagstrom module_param(record_size, ulong, 0400);
309507b0c8SSimon Kagstrom MODULE_PARM_DESC(record_size,
319507b0c8SSimon Kagstrom "record size for MTD OOPS pages in bytes (default 4096)");
324b23aff0SRichard Purdie
332e386e4bSSimon Kagstrom static char mtddev[80];
342e386e4bSSimon Kagstrom module_param_string(mtddev, mtddev, 80, 0400);
352e386e4bSSimon Kagstrom MODULE_PARM_DESC(mtddev,
362e386e4bSSimon Kagstrom "name or index number of the MTD device to use");
372e386e4bSSimon Kagstrom
382e386e4bSSimon Kagstrom static int dump_oops = 1;
392e386e4bSSimon Kagstrom module_param(dump_oops, int, 0600);
402e386e4bSSimon Kagstrom MODULE_PARM_DESC(dump_oops,
412e386e4bSSimon Kagstrom "set to 1 to dump oopses, 0 to only dump panics (default 1)");
422e386e4bSSimon Kagstrom
43aa641a22SJean-Marc Eurin #define MTDOOPS_KERNMSG_MAGIC_v1 0x5d005d00 /* Original */
44aa641a22SJean-Marc Eurin #define MTDOOPS_KERNMSG_MAGIC_v2 0x5d005e00 /* Adds the timestamp */
450bd359eeSJean-Marc Eurin
460bd359eeSJean-Marc Eurin struct mtdoops_hdr {
470bd359eeSJean-Marc Eurin u32 seq;
480bd359eeSJean-Marc Eurin u32 magic;
49aa641a22SJean-Marc Eurin ktime_t timestamp;
500bd359eeSJean-Marc Eurin } __packed;
510bd359eeSJean-Marc Eurin
527903cbabSAdrian Bunk static struct mtdoops_context {
532e386e4bSSimon Kagstrom struct kmsg_dumper dump;
542e386e4bSSimon Kagstrom
554b23aff0SRichard Purdie int mtd_index;
566ce0a856SRichard Purdie struct work_struct work_erase;
576ce0a856SRichard Purdie struct work_struct work_write;
584b23aff0SRichard Purdie struct mtd_info *mtd;
594b23aff0SRichard Purdie int oops_pages;
604b23aff0SRichard Purdie int nextpage;
614b23aff0SRichard Purdie int nextcount;
62be95745fSSimon Kagstrom unsigned long *oops_page_used;
634b23aff0SRichard Purdie
6440ddbbacSJohn Ogness unsigned long oops_buf_busy;
654b23aff0SRichard Purdie void *oops_buf;
664b23aff0SRichard Purdie } oops_cxt;
674b23aff0SRichard Purdie
mark_page_used(struct mtdoops_context * cxt,int page)68be95745fSSimon Kagstrom static void mark_page_used(struct mtdoops_context *cxt, int page)
69be95745fSSimon Kagstrom {
70be95745fSSimon Kagstrom set_bit(page, cxt->oops_page_used);
71be95745fSSimon Kagstrom }
72be95745fSSimon Kagstrom
mark_page_unused(struct mtdoops_context * cxt,int page)73be95745fSSimon Kagstrom static void mark_page_unused(struct mtdoops_context *cxt, int page)
74be95745fSSimon Kagstrom {
75be95745fSSimon Kagstrom clear_bit(page, cxt->oops_page_used);
76be95745fSSimon Kagstrom }
77be95745fSSimon Kagstrom
page_is_used(struct mtdoops_context * cxt,int page)78be95745fSSimon Kagstrom static int page_is_used(struct mtdoops_context *cxt, int page)
79be95745fSSimon Kagstrom {
80be95745fSSimon Kagstrom return test_bit(page, cxt->oops_page_used);
81be95745fSSimon Kagstrom }
82be95745fSSimon Kagstrom
mtdoops_erase_block(struct mtdoops_context * cxt,int offset)83be95745fSSimon Kagstrom static int mtdoops_erase_block(struct mtdoops_context *cxt, int offset)
844b23aff0SRichard Purdie {
85be95745fSSimon Kagstrom struct mtd_info *mtd = cxt->mtd;
86be95745fSSimon Kagstrom u32 start_page_offset = mtd_div_by_eb(offset, mtd) * mtd->erasesize;
879507b0c8SSimon Kagstrom u32 start_page = start_page_offset / record_size;
889507b0c8SSimon Kagstrom u32 erase_pages = mtd->erasesize / record_size;
894b23aff0SRichard Purdie struct erase_info erase;
904b23aff0SRichard Purdie int ret;
91be95745fSSimon Kagstrom int page;
924b23aff0SRichard Purdie
934b23aff0SRichard Purdie erase.addr = offset;
944b23aff0SRichard Purdie erase.len = mtd->erasesize;
954b23aff0SRichard Purdie
967e1f0dc0SArtem Bityutskiy ret = mtd_erase(mtd, &erase);
974b23aff0SRichard Purdie if (ret) {
9843cfba56SRay Zhang pr_warn("erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
99a15b124fSArtem Bityutskiy (unsigned long long)erase.addr,
1002e386e4bSSimon Kagstrom (unsigned long long)erase.len, mtddev);
1014b23aff0SRichard Purdie return ret;
1024b23aff0SRichard Purdie }
1034b23aff0SRichard Purdie
104be95745fSSimon Kagstrom /* Mark pages as unused */
105be95745fSSimon Kagstrom for (page = start_page; page < start_page + erase_pages; page++)
106be95745fSSimon Kagstrom mark_page_unused(cxt, page);
107be95745fSSimon Kagstrom
1084b23aff0SRichard Purdie return 0;
1094b23aff0SRichard Purdie }
1104b23aff0SRichard Purdie
mtdoops_erase(struct mtdoops_context * cxt)111340193e0SRay Zhang static void mtdoops_erase(struct mtdoops_context *cxt)
1124b23aff0SRichard Purdie {
1134b23aff0SRichard Purdie struct mtd_info *mtd = cxt->mtd;
1144b23aff0SRichard Purdie int i = 0, j, ret, mod;
1154b23aff0SRichard Purdie
1164b23aff0SRichard Purdie /* We were unregistered */
1174b23aff0SRichard Purdie if (!mtd)
1184b23aff0SRichard Purdie return;
1194b23aff0SRichard Purdie
1209507b0c8SSimon Kagstrom mod = (cxt->nextpage * record_size) % mtd->erasesize;
1214b23aff0SRichard Purdie if (mod != 0) {
1229507b0c8SSimon Kagstrom cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / record_size);
123ecd5b310SRichard Purdie if (cxt->nextpage >= cxt->oops_pages)
1244b23aff0SRichard Purdie cxt->nextpage = 0;
1254b23aff0SRichard Purdie }
1264b23aff0SRichard Purdie
1279cb93fbbSBrian Norris while ((ret = mtd_block_isbad(mtd, cxt->nextpage * record_size)) > 0) {
1284b23aff0SRichard Purdie badblock:
12943cfba56SRay Zhang pr_warn("bad block at %08lx\n",
1309507b0c8SSimon Kagstrom cxt->nextpage * record_size);
1314b23aff0SRichard Purdie i++;
1329507b0c8SSimon Kagstrom cxt->nextpage = cxt->nextpage + (mtd->erasesize / record_size);
133ecd5b310SRichard Purdie if (cxt->nextpage >= cxt->oops_pages)
1344b23aff0SRichard Purdie cxt->nextpage = 0;
1359507b0c8SSimon Kagstrom if (i == cxt->oops_pages / (mtd->erasesize / record_size)) {
13643cfba56SRay Zhang pr_err("all blocks bad!\n");
1374b23aff0SRichard Purdie return;
1384b23aff0SRichard Purdie }
1394b23aff0SRichard Purdie }
1404b23aff0SRichard Purdie
1419cb93fbbSBrian Norris if (ret < 0) {
14243cfba56SRay Zhang pr_err("mtd_block_isbad failed, aborting\n");
1439cb93fbbSBrian Norris return;
1449cb93fbbSBrian Norris }
1459cb93fbbSBrian Norris
1464b23aff0SRichard Purdie for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
1479507b0c8SSimon Kagstrom ret = mtdoops_erase_block(cxt, cxt->nextpage * record_size);
1484b23aff0SRichard Purdie
1492986bd2aSRichard Purdie if (ret >= 0) {
15043cfba56SRay Zhang pr_debug("ready %d, %d\n",
151a15b124fSArtem Bityutskiy cxt->nextpage, cxt->nextcount);
1522986bd2aSRichard Purdie return;
1534b23aff0SRichard Purdie }
1544b23aff0SRichard Purdie
155bb4a0986SArtem Bityutskiy if (ret == -EIO) {
1565942ddbcSArtem Bityutskiy ret = mtd_block_markbad(mtd, cxt->nextpage * record_size);
157bb4a0986SArtem Bityutskiy if (ret < 0 && ret != -EOPNOTSUPP) {
15843cfba56SRay Zhang pr_err("block_markbad failed, aborting\n");
1592986bd2aSRichard Purdie return;
1602986bd2aSRichard Purdie }
1612986bd2aSRichard Purdie }
1622986bd2aSRichard Purdie goto badblock;
1634b23aff0SRichard Purdie }
1644b23aff0SRichard Purdie
165340193e0SRay Zhang /* Scheduled work - when we can't proceed without erasing a block */
mtdoops_workfunc_erase(struct work_struct * work)166340193e0SRay Zhang static void mtdoops_workfunc_erase(struct work_struct *work)
167340193e0SRay Zhang {
168340193e0SRay Zhang struct mtdoops_context *cxt =
169340193e0SRay Zhang container_of(work, struct mtdoops_context, work_erase);
170340193e0SRay Zhang mtdoops_erase(cxt);
171340193e0SRay Zhang }
172340193e0SRay Zhang
mtdoops_inc_counter(struct mtdoops_context * cxt,int panic)1737cc84e0eSRay Zhang static void mtdoops_inc_counter(struct mtdoops_context *cxt, int panic)
174340193e0SRay Zhang {
175340193e0SRay Zhang cxt->nextpage++;
176340193e0SRay Zhang if (cxt->nextpage >= cxt->oops_pages)
177340193e0SRay Zhang cxt->nextpage = 0;
178340193e0SRay Zhang cxt->nextcount++;
179340193e0SRay Zhang if (cxt->nextcount == 0xffffffff)
180340193e0SRay Zhang cxt->nextcount = 0;
181340193e0SRay Zhang
182340193e0SRay Zhang if (page_is_used(cxt, cxt->nextpage)) {
1837cc84e0eSRay Zhang pr_debug("not ready %d, %d (erase %s)\n",
1847cc84e0eSRay Zhang cxt->nextpage, cxt->nextcount,
1857cc84e0eSRay Zhang panic ? "immediately" : "scheduled");
1867cc84e0eSRay Zhang if (panic) {
1877cc84e0eSRay Zhang /* In case of panic, erase immediately */
1887cc84e0eSRay Zhang mtdoops_erase(cxt);
1897cc84e0eSRay Zhang } else {
1907cc84e0eSRay Zhang /* Otherwise, schedule work to erase it "nicely" */
191340193e0SRay Zhang schedule_work(&cxt->work_erase);
192340193e0SRay Zhang }
1937cc84e0eSRay Zhang } else {
194340193e0SRay Zhang pr_debug("ready %d, %d (no erase)\n",
195340193e0SRay Zhang cxt->nextpage, cxt->nextcount);
196340193e0SRay Zhang }
1977cc84e0eSRay Zhang }
198340193e0SRay Zhang
mtdoops_write(struct mtdoops_context * cxt,int panic)199621e4f8eSRichard Purdie static void mtdoops_write(struct mtdoops_context *cxt, int panic)
2004b23aff0SRichard Purdie {
2016ce0a856SRichard Purdie struct mtd_info *mtd = cxt->mtd;
2026ce0a856SRichard Purdie size_t retlen;
2030bd359eeSJean-Marc Eurin struct mtdoops_hdr *hdr;
2046ce0a856SRichard Purdie int ret;
2054b23aff0SRichard Purdie
20640ddbbacSJohn Ogness if (test_and_set_bit(0, &cxt->oops_buf_busy))
20740ddbbacSJohn Ogness return;
20840ddbbacSJohn Ogness
2092e386e4bSSimon Kagstrom /* Add mtdoops header to the buffer */
2100bd359eeSJean-Marc Eurin hdr = (struct mtdoops_hdr *)cxt->oops_buf;
2110bd359eeSJean-Marc Eurin hdr->seq = cxt->nextcount;
212aa641a22SJean-Marc Eurin hdr->magic = MTDOOPS_KERNMSG_MAGIC_v2;
213aa641a22SJean-Marc Eurin hdr->timestamp = ktime_get_real();
2146ce0a856SRichard Purdie
215016c1291SArtem Bityutskiy if (panic) {
2167ae79d7fSArtem Bityutskiy ret = mtd_panic_write(mtd, cxt->nextpage * record_size,
2179507b0c8SSimon Kagstrom record_size, &retlen, cxt->oops_buf);
218016c1291SArtem Bityutskiy if (ret == -EOPNOTSUPP) {
21943cfba56SRay Zhang pr_err("Cannot write from panic without panic_write\n");
22040ddbbacSJohn Ogness goto out;
221016c1291SArtem Bityutskiy }
222016c1291SArtem Bityutskiy } else
223eda95cbfSArtem Bityutskiy ret = mtd_write(mtd, cxt->nextpage * record_size,
2249507b0c8SSimon Kagstrom record_size, &retlen, cxt->oops_buf);
2256ce0a856SRichard Purdie
2269507b0c8SSimon Kagstrom if (retlen != record_size || ret < 0)
22743cfba56SRay Zhang pr_err("write failure at %ld (%td of %ld written), error %d\n",
2289507b0c8SSimon Kagstrom cxt->nextpage * record_size, retlen, record_size, ret);
229be95745fSSimon Kagstrom mark_page_used(cxt, cxt->nextpage);
2302e386e4bSSimon Kagstrom memset(cxt->oops_buf, 0xff, record_size);
2316ce0a856SRichard Purdie
2327cc84e0eSRay Zhang mtdoops_inc_counter(cxt, panic);
23340ddbbacSJohn Ogness out:
23440ddbbacSJohn Ogness clear_bit(0, &cxt->oops_buf_busy);
2354b23aff0SRichard Purdie }
2364b23aff0SRichard Purdie
mtdoops_workfunc_write(struct work_struct * work)237621e4f8eSRichard Purdie static void mtdoops_workfunc_write(struct work_struct *work)
238621e4f8eSRichard Purdie {
239621e4f8eSRichard Purdie struct mtdoops_context *cxt =
240621e4f8eSRichard Purdie container_of(work, struct mtdoops_context, work_write);
241621e4f8eSRichard Purdie
242621e4f8eSRichard Purdie mtdoops_write(cxt, 0);
243621e4f8eSRichard Purdie }
244621e4f8eSRichard Purdie
find_next_position(struct mtdoops_context * cxt)2456ce0a856SRichard Purdie static void find_next_position(struct mtdoops_context *cxt)
2464b23aff0SRichard Purdie {
2474b23aff0SRichard Purdie struct mtd_info *mtd = cxt->mtd;
2480bd359eeSJean-Marc Eurin struct mtdoops_hdr hdr;
2492986bd2aSRichard Purdie int ret, page, maxpos = 0;
2500bd359eeSJean-Marc Eurin u32 maxcount = 0xffffffff;
2514b23aff0SRichard Purdie size_t retlen;
2524b23aff0SRichard Purdie
2534b23aff0SRichard Purdie for (page = 0; page < cxt->oops_pages; page++) {
254bb4a0986SArtem Bityutskiy if (mtd_block_isbad(mtd, page * record_size))
2553538c563SRoman Tereshonkov continue;
256be95745fSSimon Kagstrom /* Assume the page is used */
257be95745fSSimon Kagstrom mark_page_used(cxt, page);
2580bd359eeSJean-Marc Eurin ret = mtd_read(mtd, page * record_size, sizeof(hdr),
2590bd359eeSJean-Marc Eurin &retlen, (u_char *)&hdr);
2600bd359eeSJean-Marc Eurin if (retlen != sizeof(hdr) ||
261d57f4054SBrian Norris (ret < 0 && !mtd_is_bitflip(ret))) {
26243cfba56SRay Zhang pr_err("read failure at %ld (%zu of %zu read), err %d\n",
2630bd359eeSJean-Marc Eurin page * record_size, retlen, sizeof(hdr), ret);
2642986bd2aSRichard Purdie continue;
2652986bd2aSRichard Purdie }
2662986bd2aSRichard Purdie
2670bd359eeSJean-Marc Eurin if (hdr.seq == 0xffffffff && hdr.magic == 0xffffffff)
268be95745fSSimon Kagstrom mark_page_unused(cxt, page);
269aa641a22SJean-Marc Eurin if (hdr.seq == 0xffffffff ||
270aa641a22SJean-Marc Eurin (hdr.magic != MTDOOPS_KERNMSG_MAGIC_v1 &&
271aa641a22SJean-Marc Eurin hdr.magic != MTDOOPS_KERNMSG_MAGIC_v2))
2724b23aff0SRichard Purdie continue;
2734b23aff0SRichard Purdie if (maxcount == 0xffffffff) {
2740bd359eeSJean-Marc Eurin maxcount = hdr.seq;
2754b23aff0SRichard Purdie maxpos = page;
2760bd359eeSJean-Marc Eurin } else if (hdr.seq < 0x40000000 && maxcount > 0xc0000000) {
2770bd359eeSJean-Marc Eurin maxcount = hdr.seq;
2784b23aff0SRichard Purdie maxpos = page;
2790bd359eeSJean-Marc Eurin } else if (hdr.seq > maxcount && hdr.seq < 0xc0000000) {
2800bd359eeSJean-Marc Eurin maxcount = hdr.seq;
2814b23aff0SRichard Purdie maxpos = page;
2820bd359eeSJean-Marc Eurin } else if (hdr.seq > maxcount && hdr.seq > 0xc0000000
283a15b124fSArtem Bityutskiy && maxcount > 0x80000000) {
2840bd359eeSJean-Marc Eurin maxcount = hdr.seq;
2854b23aff0SRichard Purdie maxpos = page;
2864b23aff0SRichard Purdie }
2874b23aff0SRichard Purdie }
2884b23aff0SRichard Purdie if (maxcount == 0xffffffff) {
289cd409c61SMatthieu CASTET cxt->nextpage = cxt->oops_pages - 1;
290cd409c61SMatthieu CASTET cxt->nextcount = 0;
2914b23aff0SRichard Purdie }
292cd409c61SMatthieu CASTET else {
2934b23aff0SRichard Purdie cxt->nextpage = maxpos;
2944b23aff0SRichard Purdie cxt->nextcount = maxcount;
295cd409c61SMatthieu CASTET }
2964b23aff0SRichard Purdie
2977cc84e0eSRay Zhang mtdoops_inc_counter(cxt, 0);
2984b23aff0SRichard Purdie }
2994b23aff0SRichard Purdie
mtdoops_do_dump(struct kmsg_dumper * dumper,struct kmsg_dump_detail * detail)3002e386e4bSSimon Kagstrom static void mtdoops_do_dump(struct kmsg_dumper *dumper,
301e1a261baSJocelyn Falempe struct kmsg_dump_detail *detail)
3022e386e4bSSimon Kagstrom {
3032e386e4bSSimon Kagstrom struct mtdoops_context *cxt = container_of(dumper,
3042e386e4bSSimon Kagstrom struct mtdoops_context, dump);
305f9f3f02dSJohn Ogness struct kmsg_dump_iter iter;
306fc2d557cSSeiji Aguchi
3072e386e4bSSimon Kagstrom /* Only dump oopses if dump_oops is set */
308*1f7ce431SJocelyn Falempe if (detail->reason == KMSG_DUMP_OOPS && !dump_oops)
3092e386e4bSSimon Kagstrom return;
3102e386e4bSSimon Kagstrom
311f9f3f02dSJohn Ogness kmsg_dump_rewind(&iter);
312f9f3f02dSJohn Ogness
31340ddbbacSJohn Ogness if (test_and_set_bit(0, &cxt->oops_buf_busy))
31440ddbbacSJohn Ogness return;
3150bd359eeSJean-Marc Eurin kmsg_dump_get_buffer(&iter, true,
3160bd359eeSJean-Marc Eurin cxt->oops_buf + sizeof(struct mtdoops_hdr),
3170bd359eeSJean-Marc Eurin record_size - sizeof(struct mtdoops_hdr), NULL);
31840ddbbacSJohn Ogness clear_bit(0, &cxt->oops_buf_busy);
3192e386e4bSSimon Kagstrom
320*1f7ce431SJocelyn Falempe if (detail->reason != KMSG_DUMP_OOPS) {
3212e386e4bSSimon Kagstrom /* Panics must be written immediately */
3222e386e4bSSimon Kagstrom mtdoops_write(cxt, 1);
323c1cf1d57SMark Tomlinson } else {
3242e386e4bSSimon Kagstrom /* For other cases, schedule work to write it "nicely" */
3252e386e4bSSimon Kagstrom schedule_work(&cxt->work_write);
3262e386e4bSSimon Kagstrom }
327c1cf1d57SMark Tomlinson }
3284b23aff0SRichard Purdie
mtdoops_notify_add(struct mtd_info * mtd)3294b23aff0SRichard Purdie static void mtdoops_notify_add(struct mtd_info *mtd)
3304b23aff0SRichard Purdie {
3314b23aff0SRichard Purdie struct mtdoops_context *cxt = &oops_cxt;
3322e386e4bSSimon Kagstrom u64 mtdoops_pages = div_u64(mtd->size, record_size);
3332e386e4bSSimon Kagstrom int err;
334be95745fSSimon Kagstrom
3352e386e4bSSimon Kagstrom if (!strcmp(mtd->name, mtddev))
336e2a0f25bSAdrian Hunter cxt->mtd_index = mtd->index;
337e2a0f25bSAdrian Hunter
338a15b124fSArtem Bityutskiy if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
3394b23aff0SRichard Purdie return;
3404b23aff0SRichard Purdie
341a15b124fSArtem Bityutskiy if (mtd->size < mtd->erasesize * 2) {
34243cfba56SRay Zhang pr_err("MTD partition %d not big enough for mtdoops\n",
3434b23aff0SRichard Purdie mtd->index);
3444b23aff0SRichard Purdie return;
3454b23aff0SRichard Purdie }
3469507b0c8SSimon Kagstrom if (mtd->erasesize < record_size) {
34743cfba56SRay Zhang pr_err("eraseblock size of MTD partition %d too small\n",
34879dcd8e9SRichard Purdie mtd->index);
34979dcd8e9SRichard Purdie return;
35079dcd8e9SRichard Purdie }
3511114e3d0SSimon Kagstrom if (mtd->size > MTDOOPS_MAX_MTD_SIZE) {
35243cfba56SRay Zhang pr_err("mtd%d is too large (limit is %d MiB)\n",
3531114e3d0SSimon Kagstrom mtd->index, MTDOOPS_MAX_MTD_SIZE / 1024 / 1024);
3541114e3d0SSimon Kagstrom return;
3551114e3d0SSimon Kagstrom }
3561114e3d0SSimon Kagstrom
357be95745fSSimon Kagstrom /* oops_page_used is a bit field */
35842bc47b3SKees Cook cxt->oops_page_used =
35942bc47b3SKees Cook vmalloc(array_size(sizeof(unsigned long),
36042bc47b3SKees Cook DIV_ROUND_UP(mtdoops_pages,
36142bc47b3SKees Cook BITS_PER_LONG)));
362be95745fSSimon Kagstrom if (!cxt->oops_page_used) {
36343cfba56SRay Zhang pr_err("could not allocate page array\n");
3642e386e4bSSimon Kagstrom return;
3652e386e4bSSimon Kagstrom }
3662e386e4bSSimon Kagstrom
367e2ae715dSKay Sievers cxt->dump.max_reason = KMSG_DUMP_OOPS;
3682e386e4bSSimon Kagstrom cxt->dump.dump = mtdoops_do_dump;
3692e386e4bSSimon Kagstrom err = kmsg_dump_register(&cxt->dump);
3702e386e4bSSimon Kagstrom if (err) {
37143cfba56SRay Zhang pr_err("registering kmsg dumper failed, error %d\n", err);
3722e386e4bSSimon Kagstrom vfree(cxt->oops_page_used);
3732e386e4bSSimon Kagstrom cxt->oops_page_used = NULL;
374be95745fSSimon Kagstrom return;
375be95745fSSimon Kagstrom }
3761114e3d0SSimon Kagstrom
3774b23aff0SRichard Purdie cxt->mtd = mtd;
3789507b0c8SSimon Kagstrom cxt->oops_pages = (int)mtd->size / record_size;
3796ce0a856SRichard Purdie find_next_position(cxt);
38043cfba56SRay Zhang pr_info("Attached to MTD device %d\n", mtd->index);
3814b23aff0SRichard Purdie }
3824b23aff0SRichard Purdie
mtdoops_notify_remove(struct mtd_info * mtd)3834b23aff0SRichard Purdie static void mtdoops_notify_remove(struct mtd_info *mtd)
3844b23aff0SRichard Purdie {
3854b23aff0SRichard Purdie struct mtdoops_context *cxt = &oops_cxt;
3864b23aff0SRichard Purdie
387a15b124fSArtem Bityutskiy if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
3884b23aff0SRichard Purdie return;
3894b23aff0SRichard Purdie
3902e386e4bSSimon Kagstrom if (kmsg_dump_unregister(&cxt->dump) < 0)
39143cfba56SRay Zhang pr_warn("could not unregister kmsg_dumper\n");
3922e386e4bSSimon Kagstrom
3934b23aff0SRichard Purdie cxt->mtd = NULL;
39443829731STejun Heo flush_work(&cxt->work_erase);
39543829731STejun Heo flush_work(&cxt->work_write);
3964b23aff0SRichard Purdie }
3974b23aff0SRichard Purdie
3984b23aff0SRichard Purdie
3994b23aff0SRichard Purdie static struct mtd_notifier mtdoops_notifier = {
4004b23aff0SRichard Purdie .add = mtdoops_notify_add,
4014b23aff0SRichard Purdie .remove = mtdoops_notify_remove,
4024b23aff0SRichard Purdie };
4034b23aff0SRichard Purdie
mtdoops_init(void)4042e386e4bSSimon Kagstrom static int __init mtdoops_init(void)
4054b23aff0SRichard Purdie {
4064b23aff0SRichard Purdie struct mtdoops_context *cxt = &oops_cxt;
4072e386e4bSSimon Kagstrom int mtd_index;
4082e386e4bSSimon Kagstrom char *endp;
4094b23aff0SRichard Purdie
4102e386e4bSSimon Kagstrom if (strlen(mtddev) == 0) {
41143cfba56SRay Zhang pr_err("mtd device (mtddev=name/number) must be supplied\n");
4122e386e4bSSimon Kagstrom return -EINVAL;
4132e386e4bSSimon Kagstrom }
4149507b0c8SSimon Kagstrom if ((record_size & 4095) != 0) {
41543cfba56SRay Zhang pr_err("record_size must be a multiple of 4096\n");
4169507b0c8SSimon Kagstrom return -EINVAL;
4179507b0c8SSimon Kagstrom }
4189507b0c8SSimon Kagstrom if (record_size < 4096) {
41943cfba56SRay Zhang pr_err("record_size must be over 4096 bytes\n");
4209507b0c8SSimon Kagstrom return -EINVAL;
4219507b0c8SSimon Kagstrom }
4222e386e4bSSimon Kagstrom
4232e386e4bSSimon Kagstrom /* Setup the MTD device to use */
4244b23aff0SRichard Purdie cxt->mtd_index = -1;
4252e386e4bSSimon Kagstrom mtd_index = simple_strtoul(mtddev, &endp, 0);
4262e386e4bSSimon Kagstrom if (*endp == '\0')
4272e386e4bSSimon Kagstrom cxt->mtd_index = mtd_index;
4282e386e4bSSimon Kagstrom
4299507b0c8SSimon Kagstrom cxt->oops_buf = vmalloc(record_size);
430313ea21aSZhen Lei if (!cxt->oops_buf)
4314b23aff0SRichard Purdie return -ENOMEM;
4322e386e4bSSimon Kagstrom memset(cxt->oops_buf, 0xff, record_size);
43340ddbbacSJohn Ogness cxt->oops_buf_busy = 0;
4344b23aff0SRichard Purdie
4356ce0a856SRichard Purdie INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
4366ce0a856SRichard Purdie INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
4374b23aff0SRichard Purdie
4384b23aff0SRichard Purdie register_mtd_user(&mtdoops_notifier);
4394b23aff0SRichard Purdie return 0;
4404b23aff0SRichard Purdie }
4414b23aff0SRichard Purdie
mtdoops_exit(void)4422e386e4bSSimon Kagstrom static void __exit mtdoops_exit(void)
4434b23aff0SRichard Purdie {
4444b23aff0SRichard Purdie struct mtdoops_context *cxt = &oops_cxt;
4454b23aff0SRichard Purdie
4464b23aff0SRichard Purdie unregister_mtd_user(&mtdoops_notifier);
4474b23aff0SRichard Purdie vfree(cxt->oops_buf);
448be95745fSSimon Kagstrom vfree(cxt->oops_page_used);
4494b23aff0SRichard Purdie }
4504b23aff0SRichard Purdie
4514b23aff0SRichard Purdie
4522e386e4bSSimon Kagstrom module_init(mtdoops_init);
4532e386e4bSSimon Kagstrom module_exit(mtdoops_exit);
4544b23aff0SRichard Purdie
4554b23aff0SRichard Purdie MODULE_LICENSE("GPL");
4564b23aff0SRichard Purdie MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
4574b23aff0SRichard Purdie MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");
458