yaffs_tagscompat.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2011 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include "yaffs_guts.h"
  14. #include "yaffs_tagscompat.h"
  15. #include "yaffs_ecc.h"
  16. #include "yaffs_getblockinfo.h"
  17. #include "yaffs_trace.h"
  18. static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk);
  19. /********** Tags ECC calculations *********/
  20. void yaffs_calc_ecc(const u8 *data, struct yaffs_spare *spare)
  21. {
  22. yaffs_ecc_calc(data, spare->ecc1);
  23. yaffs_ecc_calc(&data[256], spare->ecc2);
  24. }
  25. void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
  26. {
  27. /* Calculate an ecc */
  28. unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
  29. unsigned i, j;
  30. unsigned ecc = 0;
  31. unsigned bit = 0;
  32. tags->ecc = 0;
  33. for (i = 0; i < 8; i++) {
  34. for (j = 1; j & 0xff; j <<= 1) {
  35. bit++;
  36. if (b[i] & j)
  37. ecc ^= bit;
  38. }
  39. }
  40. tags->ecc = ecc;
  41. }
  42. int yaffs_check_tags_ecc(struct yaffs_tags *tags)
  43. {
  44. unsigned ecc = tags->ecc;
  45. yaffs_calc_tags_ecc(tags);
  46. ecc ^= tags->ecc;
  47. if (ecc && ecc <= 64) {
  48. /* TODO: Handle the failure better. Retire? */
  49. unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
  50. ecc--;
  51. b[ecc / 8] ^= (1 << (ecc & 7));
  52. /* Now recvalc the ecc */
  53. yaffs_calc_tags_ecc(tags);
  54. return 1; /* recovered error */
  55. } else if (ecc) {
  56. /* Wierd ecc failure value */
  57. /* TODO Need to do somethiong here */
  58. return -1; /* unrecovered error */
  59. }
  60. return 0;
  61. }
  62. /********** Tags **********/
  63. static void yaffs_load_tags_to_spare(struct yaffs_spare *spare_ptr,
  64. struct yaffs_tags *tags_ptr)
  65. {
  66. union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
  67. yaffs_calc_tags_ecc(tags_ptr);
  68. spare_ptr->tb0 = tu->as_bytes[0];
  69. spare_ptr->tb1 = tu->as_bytes[1];
  70. spare_ptr->tb2 = tu->as_bytes[2];
  71. spare_ptr->tb3 = tu->as_bytes[3];
  72. spare_ptr->tb4 = tu->as_bytes[4];
  73. spare_ptr->tb5 = tu->as_bytes[5];
  74. spare_ptr->tb6 = tu->as_bytes[6];
  75. spare_ptr->tb7 = tu->as_bytes[7];
  76. }
  77. static void yaffs_get_tags_from_spare(struct yaffs_dev *dev,
  78. struct yaffs_spare *spare_ptr,
  79. struct yaffs_tags *tags_ptr)
  80. {
  81. union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
  82. int result;
  83. tu->as_bytes[0] = spare_ptr->tb0;
  84. tu->as_bytes[1] = spare_ptr->tb1;
  85. tu->as_bytes[2] = spare_ptr->tb2;
  86. tu->as_bytes[3] = spare_ptr->tb3;
  87. tu->as_bytes[4] = spare_ptr->tb4;
  88. tu->as_bytes[5] = spare_ptr->tb5;
  89. tu->as_bytes[6] = spare_ptr->tb6;
  90. tu->as_bytes[7] = spare_ptr->tb7;
  91. result = yaffs_check_tags_ecc(tags_ptr);
  92. if (result > 0)
  93. dev->n_tags_ecc_fixed++;
  94. else if (result < 0)
  95. dev->n_tags_ecc_unfixed++;
  96. }
  97. static void yaffs_spare_init(struct yaffs_spare *spare)
  98. {
  99. memset(spare, 0xff, sizeof(struct yaffs_spare));
  100. }
  101. static int yaffs_wr_nand(struct yaffs_dev *dev,
  102. int nand_chunk, const u8 *data,
  103. struct yaffs_spare *spare)
  104. {
  105. if (nand_chunk < dev->param.start_block * dev->param.chunks_per_block) {
  106. yaffs_trace(YAFFS_TRACE_ERROR,
  107. "**>> yaffs chunk %d is not valid",
  108. nand_chunk);
  109. return YAFFS_FAIL;
  110. }
  111. return dev->param.write_chunk_fn(dev, nand_chunk, data, spare);
  112. }
  113. static int yaffs_rd_chunk_nand(struct yaffs_dev *dev,
  114. int nand_chunk,
  115. u8 *data,
  116. struct yaffs_spare *spare,
  117. enum yaffs_ecc_result *ecc_result,
  118. int correct_errors)
  119. {
  120. int ret_val;
  121. struct yaffs_spare local_spare;
  122. if (!spare) {
  123. /* If we don't have a real spare, then we use a local one. */
  124. /* Need this for the calculation of the ecc */
  125. spare = &local_spare;
  126. }
  127. if (!dev->param.use_nand_ecc) {
  128. ret_val =
  129. dev->param.read_chunk_fn(dev, nand_chunk, data, spare);
  130. if (data && correct_errors) {
  131. /* Do ECC correction */
  132. /* Todo handle any errors */
  133. int ecc_result1, ecc_result2;
  134. u8 calc_ecc[3];
  135. yaffs_ecc_calc(data, calc_ecc);
  136. ecc_result1 =
  137. yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
  138. yaffs_ecc_calc(&data[256], calc_ecc);
  139. ecc_result2 =
  140. yaffs_ecc_correct(&data[256], spare->ecc2,
  141. calc_ecc);
  142. if (ecc_result1 > 0) {
  143. yaffs_trace(YAFFS_TRACE_ERROR,
  144. "**>>yaffs ecc error fix performed on chunk %d:0",
  145. nand_chunk);
  146. dev->n_ecc_fixed++;
  147. } else if (ecc_result1 < 0) {
  148. yaffs_trace(YAFFS_TRACE_ERROR,
  149. "**>>yaffs ecc error unfixed on chunk %d:0",
  150. nand_chunk);
  151. dev->n_ecc_unfixed++;
  152. }
  153. if (ecc_result2 > 0) {
  154. yaffs_trace(YAFFS_TRACE_ERROR,
  155. "**>>yaffs ecc error fix performed on chunk %d:1",
  156. nand_chunk);
  157. dev->n_ecc_fixed++;
  158. } else if (ecc_result2 < 0) {
  159. yaffs_trace(YAFFS_TRACE_ERROR,
  160. "**>>yaffs ecc error unfixed on chunk %d:1",
  161. nand_chunk);
  162. dev->n_ecc_unfixed++;
  163. }
  164. if (ecc_result1 || ecc_result2) {
  165. /* We had a data problem on this page */
  166. yaffs_handle_rd_data_error(dev, nand_chunk);
  167. }
  168. if (ecc_result1 < 0 || ecc_result2 < 0)
  169. *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  170. else if (ecc_result1 > 0 || ecc_result2 > 0)
  171. *ecc_result = YAFFS_ECC_RESULT_FIXED;
  172. else
  173. *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  174. }
  175. } else {
  176. /* Must allocate enough memory for spare+2*sizeof(int) */
  177. /* for ecc results from device. */
  178. struct yaffs_nand_spare nspare;
  179. memset(&nspare, 0, sizeof(nspare));
  180. ret_val = dev->param.read_chunk_fn(dev, nand_chunk, data,
  181. (struct yaffs_spare *)
  182. &nspare);
  183. memcpy(spare, &nspare, sizeof(struct yaffs_spare));
  184. if (data && correct_errors) {
  185. if (nspare.eccres1 > 0) {
  186. yaffs_trace(YAFFS_TRACE_ERROR,
  187. "**>>mtd ecc error fix performed on chunk %d:0",
  188. nand_chunk);
  189. } else if (nspare.eccres1 < 0) {
  190. yaffs_trace(YAFFS_TRACE_ERROR,
  191. "**>>mtd ecc error unfixed on chunk %d:0",
  192. nand_chunk);
  193. }
  194. if (nspare.eccres2 > 0) {
  195. yaffs_trace(YAFFS_TRACE_ERROR,
  196. "**>>mtd ecc error fix performed on chunk %d:1",
  197. nand_chunk);
  198. } else if (nspare.eccres2 < 0) {
  199. yaffs_trace(YAFFS_TRACE_ERROR,
  200. "**>>mtd ecc error unfixed on chunk %d:1",
  201. nand_chunk);
  202. }
  203. if (nspare.eccres1 || nspare.eccres2) {
  204. /* We had a data problem on this page */
  205. yaffs_handle_rd_data_error(dev, nand_chunk);
  206. }
  207. if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
  208. *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  209. else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
  210. *ecc_result = YAFFS_ECC_RESULT_FIXED;
  211. else
  212. *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  213. }
  214. }
  215. return ret_val;
  216. }
  217. /*
  218. * Functions for robustisizing
  219. */
  220. static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk)
  221. {
  222. int flash_block = nand_chunk / dev->param.chunks_per_block;
  223. /* Mark the block for retirement */
  224. yaffs_get_block_info(dev, flash_block + dev->block_offset)->
  225. needs_retiring = 1;
  226. yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  227. "**>>Block %d marked for retirement",
  228. flash_block);
  229. /* TODO:
  230. * Just do a garbage collection on the affected block
  231. * then retire the block
  232. * NB recursion
  233. */
  234. }
  235. int yaffs_tags_compat_wr(struct yaffs_dev *dev,
  236. int nand_chunk,
  237. const u8 *data, const struct yaffs_ext_tags *ext_tags)
  238. {
  239. struct yaffs_spare spare;
  240. struct yaffs_tags tags;
  241. yaffs_spare_init(&spare);
  242. if (ext_tags->is_deleted)
  243. spare.page_status = 0;
  244. else {
  245. tags.obj_id = ext_tags->obj_id;
  246. tags.chunk_id = ext_tags->chunk_id;
  247. tags.n_bytes_lsb = ext_tags->n_bytes & (1024 - 1);
  248. if (dev->data_bytes_per_chunk >= 1024)
  249. tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
  250. else
  251. tags.n_bytes_msb = 3;
  252. tags.serial_number = ext_tags->serial_number;
  253. if (!dev->param.use_nand_ecc && data)
  254. yaffs_calc_ecc(data, &spare);
  255. yaffs_load_tags_to_spare(&spare, &tags);
  256. }
  257. return yaffs_wr_nand(dev, nand_chunk, data, &spare);
  258. }
  259. int yaffs_tags_compat_rd(struct yaffs_dev *dev,
  260. int nand_chunk,
  261. u8 *data, struct yaffs_ext_tags *ext_tags)
  262. {
  263. struct yaffs_spare spare;
  264. struct yaffs_tags tags;
  265. enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
  266. static struct yaffs_spare spare_ff;
  267. static int init;
  268. int deleted;
  269. if (!init) {
  270. memset(&spare_ff, 0xff, sizeof(spare_ff));
  271. init = 1;
  272. }
  273. if (!yaffs_rd_chunk_nand(dev, nand_chunk,
  274. data, &spare, &ecc_result, 1))
  275. return YAFFS_FAIL;
  276. /* ext_tags may be NULL */
  277. if (!ext_tags)
  278. return YAFFS_OK;
  279. deleted = (hweight8(spare.page_status) < 7) ? 1 : 0;
  280. ext_tags->is_deleted = deleted;
  281. ext_tags->ecc_result = ecc_result;
  282. ext_tags->block_bad = 0; /* We're reading it */
  283. /* therefore it is not a bad block */
  284. ext_tags->chunk_used =
  285. memcmp(&spare_ff, &spare, sizeof(spare_ff)) ? 1 : 0;
  286. if (ext_tags->chunk_used) {
  287. yaffs_get_tags_from_spare(dev, &spare, &tags);
  288. ext_tags->obj_id = tags.obj_id;
  289. ext_tags->chunk_id = tags.chunk_id;
  290. ext_tags->n_bytes = tags.n_bytes_lsb;
  291. if (dev->data_bytes_per_chunk >= 1024)
  292. ext_tags->n_bytes |=
  293. (((unsigned)tags.n_bytes_msb) << 10);
  294. ext_tags->serial_number = tags.serial_number;
  295. }
  296. return YAFFS_OK;
  297. }
  298. int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block)
  299. {
  300. struct yaffs_spare spare;
  301. memset(&spare, 0xff, sizeof(struct yaffs_spare));
  302. spare.block_status = 'Y';
  303. yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
  304. &spare);
  305. yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
  306. NULL, &spare);
  307. return YAFFS_OK;
  308. }
  309. int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
  310. int block_no,
  311. enum yaffs_block_state *state,
  312. u32 *seq_number)
  313. {
  314. struct yaffs_spare spare0, spare1;
  315. static struct yaffs_spare spare_ff;
  316. static int init;
  317. enum yaffs_ecc_result dummy;
  318. if (!init) {
  319. memset(&spare_ff, 0xff, sizeof(spare_ff));
  320. init = 1;
  321. }
  322. *seq_number = 0;
  323. yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block, NULL,
  324. &spare0, &dummy, 1);
  325. yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1,
  326. NULL, &spare1, &dummy, 1);
  327. if (hweight8(spare0.block_status & spare1.block_status) < 7)
  328. *state = YAFFS_BLOCK_STATE_DEAD;
  329. else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0)
  330. *state = YAFFS_BLOCK_STATE_EMPTY;
  331. else
  332. *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
  333. return YAFFS_OK;
  334. }