ecc-sw-bch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * This file provides ECC correction for more than 1 bit per block of data,
  4. * using binary BCH codes. It relies on the generic BCH library lib/bch.c.
  5. *
  6. * Copyright © 2011 Ivan Djelic <ivan.djelic@parrot.com>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/bitops.h>
  13. #include <linux/mtd/nand.h>
  14. #include <linux/mtd/nand-ecc-sw-bch.h>
  15. /**
  16. * nand_ecc_sw_bch_calculate - Calculate the ECC corresponding to a data block
  17. * @nand: NAND device
  18. * @buf: Input buffer with raw data
  19. * @code: Output buffer with ECC
  20. */
  21. int nand_ecc_sw_bch_calculate(struct nand_device *nand,
  22. const unsigned char *buf, unsigned char *code)
  23. {
  24. struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
  25. unsigned int i;
  26. memset(code, 0, engine_conf->code_size);
  27. bch_encode(engine_conf->bch, buf, nand->ecc.ctx.conf.step_size, code);
  28. /* apply mask so that an erased page is a valid codeword */
  29. for (i = 0; i < engine_conf->code_size; i++)
  30. code[i] ^= engine_conf->eccmask[i];
  31. return 0;
  32. }
  33. EXPORT_SYMBOL(nand_ecc_sw_bch_calculate);
  34. /**
  35. * nand_ecc_sw_bch_correct - Detect, correct and report bit error(s)
  36. * @nand: NAND device
  37. * @buf: Raw data read from the chip
  38. * @read_ecc: ECC bytes from the chip
  39. * @calc_ecc: ECC calculated from the raw data
  40. *
  41. * Detect and correct bit errors for a data block.
  42. */
  43. int nand_ecc_sw_bch_correct(struct nand_device *nand, unsigned char *buf,
  44. unsigned char *read_ecc, unsigned char *calc_ecc)
  45. {
  46. struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
  47. unsigned int step_size = nand->ecc.ctx.conf.step_size;
  48. unsigned int *errloc = engine_conf->errloc;
  49. int i, count;
  50. count = bch_decode(engine_conf->bch, NULL, step_size, read_ecc,
  51. calc_ecc, NULL, errloc);
  52. if (count > 0) {
  53. for (i = 0; i < count; i++) {
  54. if (errloc[i] < (step_size * 8))
  55. /* The error is in the data area: correct it */
  56. buf[errloc[i] >> 3] ^= (1 << (errloc[i] & 7));
  57. /* Otherwise the error is in the ECC area: nothing to do */
  58. pr_debug("%s: corrected bitflip %u\n", __func__,
  59. errloc[i]);
  60. }
  61. } else if (count < 0) {
  62. pr_err("ECC unrecoverable error\n");
  63. count = -EBADMSG;
  64. }
  65. return count;
  66. }
  67. EXPORT_SYMBOL(nand_ecc_sw_bch_correct);
  68. /**
  69. * nand_ecc_sw_bch_cleanup - Cleanup software BCH ECC resources
  70. * @nand: NAND device
  71. */
  72. static void nand_ecc_sw_bch_cleanup(struct nand_device *nand)
  73. {
  74. struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
  75. bch_free(engine_conf->bch);
  76. kfree(engine_conf->errloc);
  77. kfree(engine_conf->eccmask);
  78. }
  79. /**
  80. * nand_ecc_sw_bch_init - Initialize software BCH ECC engine
  81. * @nand: NAND device
  82. *
  83. * Returns: a pointer to a new NAND BCH control structure, or NULL upon failure
  84. *
  85. * Initialize NAND BCH error correction. @nand.ecc parameters 'step_size' and
  86. * 'bytes' are used to compute the following BCH parameters:
  87. * m, the Galois field order
  88. * t, the error correction capability
  89. * 'bytes' should be equal to the number of bytes required to store m * t
  90. * bits, where m is such that 2^m - 1 > step_size * 8.
  91. *
  92. * Example: to configure 4 bit correction per 512 bytes, you should pass
  93. * step_size = 512 (thus, m = 13 is the smallest integer such that 2^m - 1 > 512 * 8)
  94. * bytes = 7 (7 bytes are required to store m * t = 13 * 4 = 52 bits)
  95. */
  96. static int nand_ecc_sw_bch_init(struct nand_device *nand)
  97. {
  98. struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
  99. unsigned int eccsize = nand->ecc.ctx.conf.step_size;
  100. unsigned int eccbytes = engine_conf->code_size;
  101. unsigned int m, t, i;
  102. unsigned char *erased_page;
  103. int ret;
  104. m = fls(1 + (8 * eccsize));
  105. t = (eccbytes * 8) / m;
  106. engine_conf->bch = bch_init(m, t, 0, false);
  107. if (!engine_conf->bch)
  108. return -EINVAL;
  109. engine_conf->eccmask = kzalloc(eccbytes, GFP_KERNEL);
  110. engine_conf->errloc = kmalloc_array(t, sizeof(*engine_conf->errloc),
  111. GFP_KERNEL);
  112. if (!engine_conf->eccmask || !engine_conf->errloc) {
  113. ret = -ENOMEM;
  114. goto cleanup;
  115. }
  116. /* Compute and store the inverted ECC of an erased step */
  117. erased_page = kmalloc(eccsize, GFP_KERNEL);
  118. if (!erased_page) {
  119. ret = -ENOMEM;
  120. goto cleanup;
  121. }
  122. memset(erased_page, 0xff, eccsize);
  123. bch_encode(engine_conf->bch, erased_page, eccsize,
  124. engine_conf->eccmask);
  125. kfree(erased_page);
  126. for (i = 0; i < eccbytes; i++)
  127. engine_conf->eccmask[i] ^= 0xff;
  128. /* Verify that the number of code bytes has the expected value */
  129. if (engine_conf->bch->ecc_bytes != eccbytes) {
  130. pr_err("Invalid number of ECC bytes: %u, expected: %u\n",
  131. eccbytes, engine_conf->bch->ecc_bytes);
  132. ret = -EINVAL;
  133. goto cleanup;
  134. }
  135. /* Sanity checks */
  136. if (8 * (eccsize + eccbytes) >= (1 << m)) {
  137. pr_err("ECC step size is too large (%u)\n", eccsize);
  138. ret = -EINVAL;
  139. goto cleanup;
  140. }
  141. return 0;
  142. cleanup:
  143. nand_ecc_sw_bch_cleanup(nand);
  144. return ret;
  145. }
  146. int nand_ecc_sw_bch_init_ctx(struct nand_device *nand)
  147. {
  148. struct nand_ecc_props *conf = &nand->ecc.ctx.conf;
  149. struct mtd_info *mtd = nanddev_to_mtd(nand);
  150. struct nand_ecc_sw_bch_conf *engine_conf;
  151. unsigned int code_size = 0, nsteps;
  152. int ret;
  153. /* Only large page NAND chips may use BCH */
  154. if (mtd->oobsize < 64) {
  155. pr_err("BCH cannot be used with small page NAND chips\n");
  156. return -EINVAL;
  157. }
  158. if (!mtd->ooblayout)
  159. mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
  160. conf->engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
  161. conf->algo = NAND_ECC_ALGO_BCH;
  162. conf->step_size = nand->ecc.user_conf.step_size;
  163. conf->strength = nand->ecc.user_conf.strength;
  164. /*
  165. * Board driver should supply ECC size and ECC strength
  166. * values to select how many bits are correctable.
  167. * Otherwise, default to 512 bytes for large page devices and 256 for
  168. * small page devices.
  169. */
  170. if (!conf->step_size) {
  171. if (mtd->oobsize >= 64)
  172. conf->step_size = 512;
  173. else
  174. conf->step_size = 256;
  175. conf->strength = 4;
  176. }
  177. nsteps = mtd->writesize / conf->step_size;
  178. /* Maximize */
  179. if (nand->ecc.user_conf.flags & NAND_ECC_MAXIMIZE_STRENGTH) {
  180. conf->step_size = 1024;
  181. nsteps = mtd->writesize / conf->step_size;
  182. /* Reserve 2 bytes for the BBM */
  183. code_size = (mtd->oobsize - 2) / nsteps;
  184. conf->strength = code_size * 8 / fls(8 * conf->step_size);
  185. }
  186. if (!code_size)
  187. code_size = DIV_ROUND_UP(conf->strength *
  188. fls(8 * conf->step_size), 8);
  189. if (!conf->strength)
  190. conf->strength = (code_size * 8) / fls(8 * conf->step_size);
  191. if (!code_size && !conf->strength) {
  192. pr_err("Missing ECC parameters\n");
  193. return -EINVAL;
  194. }
  195. engine_conf = kzalloc(sizeof(*engine_conf), GFP_KERNEL);
  196. if (!engine_conf)
  197. return -ENOMEM;
  198. ret = nand_ecc_init_req_tweaking(&engine_conf->req_ctx, nand);
  199. if (ret)
  200. goto free_engine_conf;
  201. engine_conf->code_size = code_size;
  202. engine_conf->calc_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  203. engine_conf->code_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  204. if (!engine_conf->calc_buf || !engine_conf->code_buf) {
  205. ret = -ENOMEM;
  206. goto free_bufs;
  207. }
  208. nand->ecc.ctx.priv = engine_conf;
  209. nand->ecc.ctx.nsteps = nsteps;
  210. nand->ecc.ctx.total = nsteps * code_size;
  211. ret = nand_ecc_sw_bch_init(nand);
  212. if (ret)
  213. goto free_bufs;
  214. /* Verify the layout validity */
  215. if (mtd_ooblayout_count_eccbytes(mtd) !=
  216. nand->ecc.ctx.nsteps * engine_conf->code_size) {
  217. pr_err("Invalid ECC layout\n");
  218. ret = -EINVAL;
  219. goto cleanup_bch_ctx;
  220. }
  221. return 0;
  222. cleanup_bch_ctx:
  223. nand_ecc_sw_bch_cleanup(nand);
  224. free_bufs:
  225. nand_ecc_cleanup_req_tweaking(&engine_conf->req_ctx);
  226. kfree(engine_conf->calc_buf);
  227. kfree(engine_conf->code_buf);
  228. free_engine_conf:
  229. kfree(engine_conf);
  230. return ret;
  231. }
  232. EXPORT_SYMBOL(nand_ecc_sw_bch_init_ctx);
  233. void nand_ecc_sw_bch_cleanup_ctx(struct nand_device *nand)
  234. {
  235. struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
  236. if (engine_conf) {
  237. nand_ecc_sw_bch_cleanup(nand);
  238. nand_ecc_cleanup_req_tweaking(&engine_conf->req_ctx);
  239. kfree(engine_conf->calc_buf);
  240. kfree(engine_conf->code_buf);
  241. kfree(engine_conf);
  242. }
  243. }
  244. EXPORT_SYMBOL(nand_ecc_sw_bch_cleanup_ctx);
  245. static int nand_ecc_sw_bch_prepare_io_req(struct nand_device *nand,
  246. struct nand_page_io_req *req)
  247. {
  248. struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
  249. struct mtd_info *mtd = nanddev_to_mtd(nand);
  250. int eccsize = nand->ecc.ctx.conf.step_size;
  251. int eccbytes = engine_conf->code_size;
  252. int eccsteps = nand->ecc.ctx.nsteps;
  253. int total = nand->ecc.ctx.total;
  254. u8 *ecccalc = engine_conf->calc_buf;
  255. const u8 *data;
  256. int i;
  257. /* Nothing to do for a raw operation */
  258. if (req->mode == MTD_OPS_RAW)
  259. return 0;
  260. /* This engine does not provide BBM/free OOB bytes protection */
  261. if (!req->datalen)
  262. return 0;
  263. nand_ecc_tweak_req(&engine_conf->req_ctx, req);
  264. /* No more preparation for page read */
  265. if (req->type == NAND_PAGE_READ)
  266. return 0;
  267. /* Preparation for page write: derive the ECC bytes and place them */
  268. for (i = 0, data = req->databuf.out;
  269. eccsteps;
  270. eccsteps--, i += eccbytes, data += eccsize)
  271. nand_ecc_sw_bch_calculate(nand, data, &ecccalc[i]);
  272. return mtd_ooblayout_set_eccbytes(mtd, ecccalc, (void *)req->oobbuf.out,
  273. 0, total);
  274. }
  275. static int nand_ecc_sw_bch_finish_io_req(struct nand_device *nand,
  276. struct nand_page_io_req *req)
  277. {
  278. struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
  279. struct mtd_info *mtd = nanddev_to_mtd(nand);
  280. int eccsize = nand->ecc.ctx.conf.step_size;
  281. int total = nand->ecc.ctx.total;
  282. int eccbytes = engine_conf->code_size;
  283. int eccsteps = nand->ecc.ctx.nsteps;
  284. u8 *ecccalc = engine_conf->calc_buf;
  285. u8 *ecccode = engine_conf->code_buf;
  286. unsigned int max_bitflips = 0;
  287. u8 *data = req->databuf.in;
  288. int i, ret;
  289. /* Nothing to do for a raw operation */
  290. if (req->mode == MTD_OPS_RAW)
  291. return 0;
  292. /* This engine does not provide BBM/free OOB bytes protection */
  293. if (!req->datalen)
  294. return 0;
  295. /* No more preparation for page write */
  296. if (req->type == NAND_PAGE_WRITE) {
  297. nand_ecc_restore_req(&engine_conf->req_ctx, req);
  298. return 0;
  299. }
  300. /* Finish a page read: retrieve the (raw) ECC bytes*/
  301. ret = mtd_ooblayout_get_eccbytes(mtd, ecccode, req->oobbuf.in, 0,
  302. total);
  303. if (ret)
  304. return ret;
  305. /* Calculate the ECC bytes */
  306. for (i = 0; eccsteps; eccsteps--, i += eccbytes, data += eccsize)
  307. nand_ecc_sw_bch_calculate(nand, data, &ecccalc[i]);
  308. /* Finish a page read: compare and correct */
  309. for (eccsteps = nand->ecc.ctx.nsteps, i = 0, data = req->databuf.in;
  310. eccsteps;
  311. eccsteps--, i += eccbytes, data += eccsize) {
  312. int stat = nand_ecc_sw_bch_correct(nand, data,
  313. &ecccode[i],
  314. &ecccalc[i]);
  315. if (stat < 0) {
  316. mtd->ecc_stats.failed++;
  317. } else {
  318. mtd->ecc_stats.corrected += stat;
  319. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  320. }
  321. }
  322. nand_ecc_restore_req(&engine_conf->req_ctx, req);
  323. return max_bitflips;
  324. }
  325. static struct nand_ecc_engine_ops nand_ecc_sw_bch_engine_ops = {
  326. .init_ctx = nand_ecc_sw_bch_init_ctx,
  327. .cleanup_ctx = nand_ecc_sw_bch_cleanup_ctx,
  328. .prepare_io_req = nand_ecc_sw_bch_prepare_io_req,
  329. .finish_io_req = nand_ecc_sw_bch_finish_io_req,
  330. };
  331. static struct nand_ecc_engine nand_ecc_sw_bch_engine = {
  332. .ops = &nand_ecc_sw_bch_engine_ops,
  333. };
  334. struct nand_ecc_engine *nand_ecc_sw_bch_get_engine(void)
  335. {
  336. return &nand_ecc_sw_bch_engine;
  337. }
  338. EXPORT_SYMBOL(nand_ecc_sw_bch_get_engine);
  339. MODULE_LICENSE("GPL");
  340. MODULE_AUTHOR("Ivan Djelic <ivan.djelic@parrot.com>");
  341. MODULE_DESCRIPTION("NAND software BCH ECC support");