ecc-sw-hamming.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * This file contains an ECC algorithm that detects and corrects 1 bit
  4. * errors in a 256 byte block of data.
  5. *
  6. * Copyright © 2008 Koninklijke Philips Electronics NV.
  7. * Author: Frans Meulenbroeks
  8. *
  9. * Completely replaces the previous ECC implementation which was written by:
  10. * Steven J. Hill ([email protected])
  11. * Thomas Gleixner ([email protected])
  12. *
  13. * Information on how this algorithm works and how it was developed
  14. * can be found in Documentation/driver-api/mtd/nand_ecc.rst
  15. */
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/mtd/nand.h>
  20. #include <linux/mtd/nand-ecc-sw-hamming.h>
  21. #include <linux/slab.h>
  22. #include <asm/byteorder.h>
  23. /*
  24. * invparity is a 256 byte table that contains the odd parity
  25. * for each byte. So if the number of bits in a byte is even,
  26. * the array element is 1, and when the number of bits is odd
  27. * the array eleemnt is 0.
  28. */
  29. static const char invparity[256] = {
  30. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  31. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  32. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  33. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  34. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  35. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  36. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  37. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  38. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  39. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  40. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  41. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  42. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  43. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  44. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  45. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
  46. };
  47. /*
  48. * bitsperbyte contains the number of bits per byte
  49. * this is only used for testing and repairing parity
  50. * (a precalculated value slightly improves performance)
  51. */
  52. static const char bitsperbyte[256] = {
  53. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  54. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  55. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  56. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  57. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  58. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  59. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  60. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  61. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  62. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  63. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  64. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  65. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  66. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  67. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  68. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
  69. };
  70. /*
  71. * addressbits is a lookup table to filter out the bits from the xor-ed
  72. * ECC data that identify the faulty location.
  73. * this is only used for repairing parity
  74. * see the comments in nand_ecc_sw_hamming_correct for more details
  75. */
  76. static const char addressbits[256] = {
  77. 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
  78. 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
  79. 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
  80. 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
  81. 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
  82. 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
  83. 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
  84. 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
  85. 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
  86. 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
  87. 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
  88. 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
  89. 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
  90. 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
  91. 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
  92. 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
  93. 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
  94. 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
  95. 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
  96. 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
  97. 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
  98. 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
  99. 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
  100. 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
  101. 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
  102. 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
  103. 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
  104. 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
  105. 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
  106. 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
  107. 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
  108. 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f
  109. };
  110. int ecc_sw_hamming_calculate(const unsigned char *buf, unsigned int step_size,
  111. unsigned char *code, bool sm_order)
  112. {
  113. const u32 *bp = (uint32_t *)buf;
  114. const u32 eccsize_mult = (step_size == 256) ? 1 : 2;
  115. /* current value in buffer */
  116. u32 cur;
  117. /* rp0..rp17 are the various accumulated parities (per byte) */
  118. u32 rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7, rp8, rp9, rp10, rp11, rp12,
  119. rp13, rp14, rp15, rp16, rp17;
  120. /* Cumulative parity for all data */
  121. u32 par;
  122. /* Cumulative parity at the end of the loop (rp12, rp14, rp16) */
  123. u32 tmppar;
  124. int i;
  125. par = 0;
  126. rp4 = 0;
  127. rp6 = 0;
  128. rp8 = 0;
  129. rp10 = 0;
  130. rp12 = 0;
  131. rp14 = 0;
  132. rp16 = 0;
  133. rp17 = 0;
  134. /*
  135. * The loop is unrolled a number of times;
  136. * This avoids if statements to decide on which rp value to update
  137. * Also we process the data by longwords.
  138. * Note: passing unaligned data might give a performance penalty.
  139. * It is assumed that the buffers are aligned.
  140. * tmppar is the cumulative sum of this iteration.
  141. * needed for calculating rp12, rp14, rp16 and par
  142. * also used as a performance improvement for rp6, rp8 and rp10
  143. */
  144. for (i = 0; i < eccsize_mult << 2; i++) {
  145. cur = *bp++;
  146. tmppar = cur;
  147. rp4 ^= cur;
  148. cur = *bp++;
  149. tmppar ^= cur;
  150. rp6 ^= tmppar;
  151. cur = *bp++;
  152. tmppar ^= cur;
  153. rp4 ^= cur;
  154. cur = *bp++;
  155. tmppar ^= cur;
  156. rp8 ^= tmppar;
  157. cur = *bp++;
  158. tmppar ^= cur;
  159. rp4 ^= cur;
  160. rp6 ^= cur;
  161. cur = *bp++;
  162. tmppar ^= cur;
  163. rp6 ^= cur;
  164. cur = *bp++;
  165. tmppar ^= cur;
  166. rp4 ^= cur;
  167. cur = *bp++;
  168. tmppar ^= cur;
  169. rp10 ^= tmppar;
  170. cur = *bp++;
  171. tmppar ^= cur;
  172. rp4 ^= cur;
  173. rp6 ^= cur;
  174. rp8 ^= cur;
  175. cur = *bp++;
  176. tmppar ^= cur;
  177. rp6 ^= cur;
  178. rp8 ^= cur;
  179. cur = *bp++;
  180. tmppar ^= cur;
  181. rp4 ^= cur;
  182. rp8 ^= cur;
  183. cur = *bp++;
  184. tmppar ^= cur;
  185. rp8 ^= cur;
  186. cur = *bp++;
  187. tmppar ^= cur;
  188. rp4 ^= cur;
  189. rp6 ^= cur;
  190. cur = *bp++;
  191. tmppar ^= cur;
  192. rp6 ^= cur;
  193. cur = *bp++;
  194. tmppar ^= cur;
  195. rp4 ^= cur;
  196. cur = *bp++;
  197. tmppar ^= cur;
  198. par ^= tmppar;
  199. if ((i & 0x1) == 0)
  200. rp12 ^= tmppar;
  201. if ((i & 0x2) == 0)
  202. rp14 ^= tmppar;
  203. if (eccsize_mult == 2 && (i & 0x4) == 0)
  204. rp16 ^= tmppar;
  205. }
  206. /*
  207. * handle the fact that we use longword operations
  208. * we'll bring rp4..rp14..rp16 back to single byte entities by
  209. * shifting and xoring first fold the upper and lower 16 bits,
  210. * then the upper and lower 8 bits.
  211. */
  212. rp4 ^= (rp4 >> 16);
  213. rp4 ^= (rp4 >> 8);
  214. rp4 &= 0xff;
  215. rp6 ^= (rp6 >> 16);
  216. rp6 ^= (rp6 >> 8);
  217. rp6 &= 0xff;
  218. rp8 ^= (rp8 >> 16);
  219. rp8 ^= (rp8 >> 8);
  220. rp8 &= 0xff;
  221. rp10 ^= (rp10 >> 16);
  222. rp10 ^= (rp10 >> 8);
  223. rp10 &= 0xff;
  224. rp12 ^= (rp12 >> 16);
  225. rp12 ^= (rp12 >> 8);
  226. rp12 &= 0xff;
  227. rp14 ^= (rp14 >> 16);
  228. rp14 ^= (rp14 >> 8);
  229. rp14 &= 0xff;
  230. if (eccsize_mult == 2) {
  231. rp16 ^= (rp16 >> 16);
  232. rp16 ^= (rp16 >> 8);
  233. rp16 &= 0xff;
  234. }
  235. /*
  236. * we also need to calculate the row parity for rp0..rp3
  237. * This is present in par, because par is now
  238. * rp3 rp3 rp2 rp2 in little endian and
  239. * rp2 rp2 rp3 rp3 in big endian
  240. * as well as
  241. * rp1 rp0 rp1 rp0 in little endian and
  242. * rp0 rp1 rp0 rp1 in big endian
  243. * First calculate rp2 and rp3
  244. */
  245. #ifdef __BIG_ENDIAN
  246. rp2 = (par >> 16);
  247. rp2 ^= (rp2 >> 8);
  248. rp2 &= 0xff;
  249. rp3 = par & 0xffff;
  250. rp3 ^= (rp3 >> 8);
  251. rp3 &= 0xff;
  252. #else
  253. rp3 = (par >> 16);
  254. rp3 ^= (rp3 >> 8);
  255. rp3 &= 0xff;
  256. rp2 = par & 0xffff;
  257. rp2 ^= (rp2 >> 8);
  258. rp2 &= 0xff;
  259. #endif
  260. /* reduce par to 16 bits then calculate rp1 and rp0 */
  261. par ^= (par >> 16);
  262. #ifdef __BIG_ENDIAN
  263. rp0 = (par >> 8) & 0xff;
  264. rp1 = (par & 0xff);
  265. #else
  266. rp1 = (par >> 8) & 0xff;
  267. rp0 = (par & 0xff);
  268. #endif
  269. /* finally reduce par to 8 bits */
  270. par ^= (par >> 8);
  271. par &= 0xff;
  272. /*
  273. * and calculate rp5..rp15..rp17
  274. * note that par = rp4 ^ rp5 and due to the commutative property
  275. * of the ^ operator we can say:
  276. * rp5 = (par ^ rp4);
  277. * The & 0xff seems superfluous, but benchmarking learned that
  278. * leaving it out gives slightly worse results. No idea why, probably
  279. * it has to do with the way the pipeline in pentium is organized.
  280. */
  281. rp5 = (par ^ rp4) & 0xff;
  282. rp7 = (par ^ rp6) & 0xff;
  283. rp9 = (par ^ rp8) & 0xff;
  284. rp11 = (par ^ rp10) & 0xff;
  285. rp13 = (par ^ rp12) & 0xff;
  286. rp15 = (par ^ rp14) & 0xff;
  287. if (eccsize_mult == 2)
  288. rp17 = (par ^ rp16) & 0xff;
  289. /*
  290. * Finally calculate the ECC bits.
  291. * Again here it might seem that there are performance optimisations
  292. * possible, but benchmarks showed that on the system this is developed
  293. * the code below is the fastest
  294. */
  295. if (sm_order) {
  296. code[0] = (invparity[rp7] << 7) | (invparity[rp6] << 6) |
  297. (invparity[rp5] << 5) | (invparity[rp4] << 4) |
  298. (invparity[rp3] << 3) | (invparity[rp2] << 2) |
  299. (invparity[rp1] << 1) | (invparity[rp0]);
  300. code[1] = (invparity[rp15] << 7) | (invparity[rp14] << 6) |
  301. (invparity[rp13] << 5) | (invparity[rp12] << 4) |
  302. (invparity[rp11] << 3) | (invparity[rp10] << 2) |
  303. (invparity[rp9] << 1) | (invparity[rp8]);
  304. } else {
  305. code[1] = (invparity[rp7] << 7) | (invparity[rp6] << 6) |
  306. (invparity[rp5] << 5) | (invparity[rp4] << 4) |
  307. (invparity[rp3] << 3) | (invparity[rp2] << 2) |
  308. (invparity[rp1] << 1) | (invparity[rp0]);
  309. code[0] = (invparity[rp15] << 7) | (invparity[rp14] << 6) |
  310. (invparity[rp13] << 5) | (invparity[rp12] << 4) |
  311. (invparity[rp11] << 3) | (invparity[rp10] << 2) |
  312. (invparity[rp9] << 1) | (invparity[rp8]);
  313. }
  314. if (eccsize_mult == 1)
  315. code[2] =
  316. (invparity[par & 0xf0] << 7) |
  317. (invparity[par & 0x0f] << 6) |
  318. (invparity[par & 0xcc] << 5) |
  319. (invparity[par & 0x33] << 4) |
  320. (invparity[par & 0xaa] << 3) |
  321. (invparity[par & 0x55] << 2) |
  322. 3;
  323. else
  324. code[2] =
  325. (invparity[par & 0xf0] << 7) |
  326. (invparity[par & 0x0f] << 6) |
  327. (invparity[par & 0xcc] << 5) |
  328. (invparity[par & 0x33] << 4) |
  329. (invparity[par & 0xaa] << 3) |
  330. (invparity[par & 0x55] << 2) |
  331. (invparity[rp17] << 1) |
  332. (invparity[rp16] << 0);
  333. return 0;
  334. }
  335. EXPORT_SYMBOL(ecc_sw_hamming_calculate);
  336. /**
  337. * nand_ecc_sw_hamming_calculate - Calculate 3-byte ECC for 256/512-byte block
  338. * @nand: NAND device
  339. * @buf: Input buffer with raw data
  340. * @code: Output buffer with ECC
  341. */
  342. int nand_ecc_sw_hamming_calculate(struct nand_device *nand,
  343. const unsigned char *buf, unsigned char *code)
  344. {
  345. struct nand_ecc_sw_hamming_conf *engine_conf = nand->ecc.ctx.priv;
  346. unsigned int step_size = nand->ecc.ctx.conf.step_size;
  347. bool sm_order = engine_conf ? engine_conf->sm_order : false;
  348. return ecc_sw_hamming_calculate(buf, step_size, code, sm_order);
  349. }
  350. EXPORT_SYMBOL(nand_ecc_sw_hamming_calculate);
  351. int ecc_sw_hamming_correct(unsigned char *buf, unsigned char *read_ecc,
  352. unsigned char *calc_ecc, unsigned int step_size,
  353. bool sm_order)
  354. {
  355. const u32 eccsize_mult = step_size >> 8;
  356. unsigned char b0, b1, b2, bit_addr;
  357. unsigned int byte_addr;
  358. /*
  359. * b0 to b2 indicate which bit is faulty (if any)
  360. * we might need the xor result more than once,
  361. * so keep them in a local var
  362. */
  363. if (sm_order) {
  364. b0 = read_ecc[0] ^ calc_ecc[0];
  365. b1 = read_ecc[1] ^ calc_ecc[1];
  366. } else {
  367. b0 = read_ecc[1] ^ calc_ecc[1];
  368. b1 = read_ecc[0] ^ calc_ecc[0];
  369. }
  370. b2 = read_ecc[2] ^ calc_ecc[2];
  371. /* check if there are any bitfaults */
  372. /* repeated if statements are slightly more efficient than switch ... */
  373. /* ordered in order of likelihood */
  374. if ((b0 | b1 | b2) == 0)
  375. return 0; /* no error */
  376. if ((((b0 ^ (b0 >> 1)) & 0x55) == 0x55) &&
  377. (((b1 ^ (b1 >> 1)) & 0x55) == 0x55) &&
  378. ((eccsize_mult == 1 && ((b2 ^ (b2 >> 1)) & 0x54) == 0x54) ||
  379. (eccsize_mult == 2 && ((b2 ^ (b2 >> 1)) & 0x55) == 0x55))) {
  380. /* single bit error */
  381. /*
  382. * rp17/rp15/13/11/9/7/5/3/1 indicate which byte is the faulty
  383. * byte, cp 5/3/1 indicate the faulty bit.
  384. * A lookup table (called addressbits) is used to filter
  385. * the bits from the byte they are in.
  386. * A marginal optimisation is possible by having three
  387. * different lookup tables.
  388. * One as we have now (for b0), one for b2
  389. * (that would avoid the >> 1), and one for b1 (with all values
  390. * << 4). However it was felt that introducing two more tables
  391. * hardly justify the gain.
  392. *
  393. * The b2 shift is there to get rid of the lowest two bits.
  394. * We could also do addressbits[b2] >> 1 but for the
  395. * performance it does not make any difference
  396. */
  397. if (eccsize_mult == 1)
  398. byte_addr = (addressbits[b1] << 4) + addressbits[b0];
  399. else
  400. byte_addr = (addressbits[b2 & 0x3] << 8) +
  401. (addressbits[b1] << 4) + addressbits[b0];
  402. bit_addr = addressbits[b2 >> 2];
  403. /* flip the bit */
  404. buf[byte_addr] ^= (1 << bit_addr);
  405. return 1;
  406. }
  407. /* count nr of bits; use table lookup, faster than calculating it */
  408. if ((bitsperbyte[b0] + bitsperbyte[b1] + bitsperbyte[b2]) == 1)
  409. return 1; /* error in ECC data; no action needed */
  410. pr_err("%s: uncorrectable ECC error\n", __func__);
  411. return -EBADMSG;
  412. }
  413. EXPORT_SYMBOL(ecc_sw_hamming_correct);
  414. /**
  415. * nand_ecc_sw_hamming_correct - Detect and correct bit error(s)
  416. * @nand: NAND device
  417. * @buf: Raw data read from the chip
  418. * @read_ecc: ECC bytes read from the chip
  419. * @calc_ecc: ECC calculated from the raw data
  420. *
  421. * Detect and correct up to 1 bit error per 256/512-byte block.
  422. */
  423. int nand_ecc_sw_hamming_correct(struct nand_device *nand, unsigned char *buf,
  424. unsigned char *read_ecc,
  425. unsigned char *calc_ecc)
  426. {
  427. struct nand_ecc_sw_hamming_conf *engine_conf = nand->ecc.ctx.priv;
  428. unsigned int step_size = nand->ecc.ctx.conf.step_size;
  429. bool sm_order = engine_conf ? engine_conf->sm_order : false;
  430. return ecc_sw_hamming_correct(buf, read_ecc, calc_ecc, step_size,
  431. sm_order);
  432. }
  433. EXPORT_SYMBOL(nand_ecc_sw_hamming_correct);
  434. int nand_ecc_sw_hamming_init_ctx(struct nand_device *nand)
  435. {
  436. struct nand_ecc_props *conf = &nand->ecc.ctx.conf;
  437. struct nand_ecc_sw_hamming_conf *engine_conf;
  438. struct mtd_info *mtd = nanddev_to_mtd(nand);
  439. int ret;
  440. if (!mtd->ooblayout) {
  441. switch (mtd->oobsize) {
  442. case 8:
  443. case 16:
  444. mtd_set_ooblayout(mtd, nand_get_small_page_ooblayout());
  445. break;
  446. case 64:
  447. case 128:
  448. mtd_set_ooblayout(mtd,
  449. nand_get_large_page_hamming_ooblayout());
  450. break;
  451. default:
  452. return -ENOTSUPP;
  453. }
  454. }
  455. conf->engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
  456. conf->algo = NAND_ECC_ALGO_HAMMING;
  457. conf->step_size = nand->ecc.user_conf.step_size;
  458. conf->strength = 1;
  459. /* Use the strongest configuration by default */
  460. if (conf->step_size != 256 && conf->step_size != 512)
  461. conf->step_size = 256;
  462. engine_conf = kzalloc(sizeof(*engine_conf), GFP_KERNEL);
  463. if (!engine_conf)
  464. return -ENOMEM;
  465. ret = nand_ecc_init_req_tweaking(&engine_conf->req_ctx, nand);
  466. if (ret)
  467. goto free_engine_conf;
  468. engine_conf->code_size = 3;
  469. engine_conf->calc_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  470. engine_conf->code_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  471. if (!engine_conf->calc_buf || !engine_conf->code_buf) {
  472. ret = -ENOMEM;
  473. goto free_bufs;
  474. }
  475. nand->ecc.ctx.priv = engine_conf;
  476. nand->ecc.ctx.nsteps = mtd->writesize / conf->step_size;
  477. nand->ecc.ctx.total = nand->ecc.ctx.nsteps * engine_conf->code_size;
  478. return 0;
  479. free_bufs:
  480. nand_ecc_cleanup_req_tweaking(&engine_conf->req_ctx);
  481. kfree(engine_conf->calc_buf);
  482. kfree(engine_conf->code_buf);
  483. free_engine_conf:
  484. kfree(engine_conf);
  485. return ret;
  486. }
  487. EXPORT_SYMBOL(nand_ecc_sw_hamming_init_ctx);
  488. void nand_ecc_sw_hamming_cleanup_ctx(struct nand_device *nand)
  489. {
  490. struct nand_ecc_sw_hamming_conf *engine_conf = nand->ecc.ctx.priv;
  491. if (engine_conf) {
  492. nand_ecc_cleanup_req_tweaking(&engine_conf->req_ctx);
  493. kfree(engine_conf->calc_buf);
  494. kfree(engine_conf->code_buf);
  495. kfree(engine_conf);
  496. }
  497. }
  498. EXPORT_SYMBOL(nand_ecc_sw_hamming_cleanup_ctx);
  499. static int nand_ecc_sw_hamming_prepare_io_req(struct nand_device *nand,
  500. struct nand_page_io_req *req)
  501. {
  502. struct nand_ecc_sw_hamming_conf *engine_conf = nand->ecc.ctx.priv;
  503. struct mtd_info *mtd = nanddev_to_mtd(nand);
  504. int eccsize = nand->ecc.ctx.conf.step_size;
  505. int eccbytes = engine_conf->code_size;
  506. int eccsteps = nand->ecc.ctx.nsteps;
  507. int total = nand->ecc.ctx.total;
  508. u8 *ecccalc = engine_conf->calc_buf;
  509. const u8 *data;
  510. int i;
  511. /* Nothing to do for a raw operation */
  512. if (req->mode == MTD_OPS_RAW)
  513. return 0;
  514. /* This engine does not provide BBM/free OOB bytes protection */
  515. if (!req->datalen)
  516. return 0;
  517. nand_ecc_tweak_req(&engine_conf->req_ctx, req);
  518. /* No more preparation for page read */
  519. if (req->type == NAND_PAGE_READ)
  520. return 0;
  521. /* Preparation for page write: derive the ECC bytes and place them */
  522. for (i = 0, data = req->databuf.out;
  523. eccsteps;
  524. eccsteps--, i += eccbytes, data += eccsize)
  525. nand_ecc_sw_hamming_calculate(nand, data, &ecccalc[i]);
  526. return mtd_ooblayout_set_eccbytes(mtd, ecccalc, (void *)req->oobbuf.out,
  527. 0, total);
  528. }
  529. static int nand_ecc_sw_hamming_finish_io_req(struct nand_device *nand,
  530. struct nand_page_io_req *req)
  531. {
  532. struct nand_ecc_sw_hamming_conf *engine_conf = nand->ecc.ctx.priv;
  533. struct mtd_info *mtd = nanddev_to_mtd(nand);
  534. int eccsize = nand->ecc.ctx.conf.step_size;
  535. int total = nand->ecc.ctx.total;
  536. int eccbytes = engine_conf->code_size;
  537. int eccsteps = nand->ecc.ctx.nsteps;
  538. u8 *ecccalc = engine_conf->calc_buf;
  539. u8 *ecccode = engine_conf->code_buf;
  540. unsigned int max_bitflips = 0;
  541. u8 *data = req->databuf.in;
  542. int i, ret;
  543. /* Nothing to do for a raw operation */
  544. if (req->mode == MTD_OPS_RAW)
  545. return 0;
  546. /* This engine does not provide BBM/free OOB bytes protection */
  547. if (!req->datalen)
  548. return 0;
  549. /* No more preparation for page write */
  550. if (req->type == NAND_PAGE_WRITE) {
  551. nand_ecc_restore_req(&engine_conf->req_ctx, req);
  552. return 0;
  553. }
  554. /* Finish a page read: retrieve the (raw) ECC bytes*/
  555. ret = mtd_ooblayout_get_eccbytes(mtd, ecccode, req->oobbuf.in, 0,
  556. total);
  557. if (ret)
  558. return ret;
  559. /* Calculate the ECC bytes */
  560. for (i = 0; eccsteps; eccsteps--, i += eccbytes, data += eccsize)
  561. nand_ecc_sw_hamming_calculate(nand, data, &ecccalc[i]);
  562. /* Finish a page read: compare and correct */
  563. for (eccsteps = nand->ecc.ctx.nsteps, i = 0, data = req->databuf.in;
  564. eccsteps;
  565. eccsteps--, i += eccbytes, data += eccsize) {
  566. int stat = nand_ecc_sw_hamming_correct(nand, data,
  567. &ecccode[i],
  568. &ecccalc[i]);
  569. if (stat < 0) {
  570. mtd->ecc_stats.failed++;
  571. } else {
  572. mtd->ecc_stats.corrected += stat;
  573. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  574. }
  575. }
  576. nand_ecc_restore_req(&engine_conf->req_ctx, req);
  577. return max_bitflips;
  578. }
  579. static struct nand_ecc_engine_ops nand_ecc_sw_hamming_engine_ops = {
  580. .init_ctx = nand_ecc_sw_hamming_init_ctx,
  581. .cleanup_ctx = nand_ecc_sw_hamming_cleanup_ctx,
  582. .prepare_io_req = nand_ecc_sw_hamming_prepare_io_req,
  583. .finish_io_req = nand_ecc_sw_hamming_finish_io_req,
  584. };
  585. static struct nand_ecc_engine nand_ecc_sw_hamming_engine = {
  586. .ops = &nand_ecc_sw_hamming_engine_ops,
  587. };
  588. struct nand_ecc_engine *nand_ecc_sw_hamming_get_engine(void)
  589. {
  590. return &nand_ecc_sw_hamming_engine;
  591. }
  592. EXPORT_SYMBOL(nand_ecc_sw_hamming_get_engine);
  593. MODULE_LICENSE("GPL");
  594. MODULE_AUTHOR("Frans Meulenbroeks <[email protected]>");
  595. MODULE_DESCRIPTION("NAND software Hamming ECC support");