yaffs_mtdif2.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2010 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. /* mtd interface for YAFFS2 */
  14. #include "yportenv.h"
  15. #include "yaffs_trace.h"
  16. #include "yaffs_mtdif2.h"
  17. #include "linux/mtd/mtd.h"
  18. #include "linux/types.h"
  19. #include "linux/time.h"
  20. #include "yaffs_packedtags2.h"
  21. #include "yaffs_linux.h"
  22. /* NB For use with inband tags....
  23. * We assume that the data buffer is of size total_bytes_per_chunk so that we can also
  24. * use it to load the tags.
  25. */
  26. int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  27. const u8 *data,
  28. const struct yaffs_ext_tags *tags)
  29. {
  30. struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  31. struct mtd_oob_ops ops;
  32. int retval = 0;
  33. loff_t addr;
  34. struct yaffs_packed_tags2 pt;
  35. int packed_tags_size =
  36. dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
  37. void *packed_tags_ptr =
  38. dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
  39. yaffs_trace(YAFFS_TRACE_MTD,
  40. "nandmtd2_write_chunk_tags chunk %d data %p tags %p",
  41. nand_chunk, data, tags);
  42. addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
  43. /* For yaffs2 writing there must be both data and tags.
  44. * If we're using inband tags, then the tags are stuffed into
  45. * the end of the data buffer.
  46. */
  47. if (!data || !tags)
  48. BUG();
  49. else if (dev->param.inband_tags) {
  50. struct yaffs_packed_tags2_tags_only *pt2tp;
  51. pt2tp =
  52. (struct yaffs_packed_tags2_tags_only *)(data +
  53. dev->
  54. data_bytes_per_chunk);
  55. yaffs_pack_tags2_tags_only(pt2tp, tags);
  56. } else
  57. yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
  58. ops.mode = MTD_OPS_AUTO_OOB;
  59. ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
  60. ops.len = dev->param.total_bytes_per_chunk;
  61. ops.ooboffs = 0;
  62. ops.datbuf = (u8 *) data;
  63. ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
  64. retval = mtd_write_oob(mtd, addr, &ops);
  65. if (retval == 0)
  66. return YAFFS_OK;
  67. else
  68. return YAFFS_FAIL;
  69. }
  70. int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  71. u8 *data, struct yaffs_ext_tags *tags)
  72. {
  73. struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  74. struct mtd_oob_ops ops;
  75. size_t dummy;
  76. int retval = 0;
  77. int local_data = 0;
  78. loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
  79. struct yaffs_packed_tags2 pt;
  80. int packed_tags_size =
  81. dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
  82. void *packed_tags_ptr =
  83. dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
  84. yaffs_trace(YAFFS_TRACE_MTD,
  85. "nandmtd2_read_chunk_tags chunk %d data %p tags %p",
  86. nand_chunk, data, tags);
  87. if (dev->param.inband_tags) {
  88. if (!data) {
  89. local_data = 1;
  90. data = yaffs_get_temp_buffer(dev, __LINE__);
  91. }
  92. }
  93. if (dev->param.inband_tags || (data && !tags))
  94. retval = mtd_read(mtd, addr, dev->param.total_bytes_per_chunk,
  95. &dummy, data);
  96. else if (tags) {
  97. if (!data) {
  98. local_data = 1;
  99. data = yaffs_get_temp_buffer(dev, __LINE__);
  100. }
  101. ops.mode = MTD_OPS_AUTO_OOB;
  102. ops.ooblen = packed_tags_size;
  103. ops.ooboffs = 0;
  104. ops.len = dev->data_bytes_per_chunk;
  105. ops.datbuf = data;
  106. ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer;
  107. retval = mtd_read_oob(mtd, addr, &ops);
  108. if (retval) {
  109. pr_err("yaffsdebug mtdiferror %s retval:%d unfixed:%d fixed:%d chunk:%d addr:0x%llx\n",
  110. (retval == -EUCLEAN)?"fix ecc":"unfix ecc",
  111. retval, dev->n_ecc_unfixed, dev->n_ecc_fixed, nand_chunk, addr);
  112. #ifdef YAFFS_MVG_TEST_DEBUG_LOG
  113. if (retval != -EUCLEAN) {
  114. pr_err("dump checksum_BCH chunk:%d addr:0x%x\n", nand_chunk, addr);
  115. mtk_dump_byte(ops.datbuf, ops.len, 0);
  116. pr_err("dump BCH oob\n");
  117. mtk_dump_byte(ops.oobbuf, ops.ooblen, 0);
  118. }
  119. #endif
  120. }
  121. }
  122. if (dev->param.inband_tags) {
  123. if (tags) {
  124. struct yaffs_packed_tags2_tags_only *pt2tp;
  125. pt2tp =
  126. (struct yaffs_packed_tags2_tags_only *)&data[dev->
  127. data_bytes_per_chunk];
  128. yaffs_unpack_tags2_tags_only(tags, pt2tp);
  129. }
  130. } else {
  131. if (tags) {
  132. memcpy(packed_tags_ptr,
  133. yaffs_dev_to_lc(dev)->spare_buffer,
  134. packed_tags_size);
  135. yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
  136. }
  137. }
  138. if (local_data)
  139. yaffs_release_temp_buffer(dev, data, __LINE__);
  140. if (tags && retval == -EBADMSG
  141. && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
  142. tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  143. dev->n_ecc_unfixed++;
  144. }
  145. if (tags && retval == -EUCLEAN
  146. && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
  147. tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
  148. dev->n_ecc_fixed++;
  149. }
  150. if (retval == 0)
  151. return YAFFS_OK;
  152. else
  153. return YAFFS_FAIL;
  154. }
  155. int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no)
  156. {
  157. struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  158. int retval;
  159. yaffs_trace(YAFFS_TRACE_MTD,
  160. "nandmtd2_mark_block_bad %d", block_no);
  161. retval =
  162. mtd_block_markbad(mtd,
  163. block_no * dev->param.chunks_per_block *
  164. dev->param.total_bytes_per_chunk);
  165. if (retval == 0)
  166. return YAFFS_OK;
  167. else
  168. return YAFFS_FAIL;
  169. }
  170. int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
  171. enum yaffs_block_state *state, u32 *seq_number)
  172. {
  173. struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  174. int retval;
  175. yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_query_block %d", block_no);
  176. retval =
  177. mtd_block_isbad(mtd,
  178. block_no * dev->param.chunks_per_block *
  179. dev->param.total_bytes_per_chunk);
  180. if (retval) {
  181. yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
  182. *state = YAFFS_BLOCK_STATE_DEAD;
  183. *seq_number = 0;
  184. } else {
  185. struct yaffs_ext_tags t;
  186. nandmtd2_read_chunk_tags(dev, block_no *
  187. dev->param.chunks_per_block, NULL, &t);
  188. if (t.chunk_used) {
  189. *seq_number = t.seq_number;
  190. *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  191. } else {
  192. *seq_number = 0;
  193. *state = YAFFS_BLOCK_STATE_EMPTY;
  194. }
  195. /*if ecc unfixed, set the block empty. This block will be erased when allocate.*/
  196. if (t.ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
  197. pr_err("yaffsdebug : unfixed ecc error when scan block, seqnum:0x%x\n", t.seq_number);
  198. *seq_number = 0;
  199. *state = YAFFS_BLOCK_STATE_EMPTY;
  200. }
  201. }
  202. yaffs_trace(YAFFS_TRACE_MTD,
  203. "block is bad seq %d state %d", *seq_number, *state);
  204. if (retval == 0)
  205. return YAFFS_OK;
  206. else
  207. return YAFFS_FAIL;
  208. }