yaffs_mtdif2.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. }
  59. ops.mode = MTD_OPS_AUTO_OOB;
  60. ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
  61. ops.len = dev->param.total_bytes_per_chunk;
  62. ops.ooboffs = 0;
  63. ops.datbuf = (u8 *) data;
  64. ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
  65. retval = mtd_write_oob(mtd, addr, &ops);
  66. if (retval == 0)
  67. return YAFFS_OK;
  68. else
  69. return YAFFS_FAIL;
  70. }
  71. int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  72. u8 * data, struct yaffs_ext_tags *tags)
  73. {
  74. struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  75. struct mtd_oob_ops ops;
  76. size_t dummy;
  77. int retval = 0;
  78. int local_data = 0;
  79. loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
  80. struct yaffs_packed_tags2 pt;
  81. int packed_tags_size =
  82. dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
  83. void *packed_tags_ptr =
  84. dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
  85. yaffs_trace(YAFFS_TRACE_MTD,
  86. "nandmtd2_read_chunk_tags chunk %d data %p tags %p",
  87. nand_chunk, data, tags);
  88. if (dev->param.inband_tags) {
  89. if (!data) {
  90. local_data = 1;
  91. data = yaffs_get_temp_buffer(dev, __LINE__);
  92. }
  93. }
  94. if (dev->param.inband_tags || (data && !tags))
  95. retval = mtd_read(mtd, addr, dev->param.total_bytes_per_chunk,
  96. &dummy, data);
  97. else if (tags) {
  98. ops.mode = MTD_OPS_AUTO_OOB;
  99. ops.ooblen = packed_tags_size;
  100. ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
  101. ops.ooboffs = 0;
  102. ops.datbuf = data;
  103. ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer;
  104. retval = mtd_read_oob(mtd, addr, &ops);
  105. }
  106. if (dev->param.inband_tags) {
  107. if (tags) {
  108. struct yaffs_packed_tags2_tags_only *pt2tp;
  109. pt2tp =
  110. (struct yaffs_packed_tags2_tags_only *)&data[dev->
  111. data_bytes_per_chunk];
  112. yaffs_unpack_tags2_tags_only(tags, pt2tp);
  113. }
  114. } else {
  115. if (tags) {
  116. memcpy(packed_tags_ptr,
  117. yaffs_dev_to_lc(dev)->spare_buffer,
  118. packed_tags_size);
  119. yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
  120. }
  121. }
  122. if (local_data)
  123. yaffs_release_temp_buffer(dev, data, __LINE__);
  124. if (tags && retval == -EBADMSG
  125. && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
  126. tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  127. dev->n_ecc_unfixed++;
  128. }
  129. if (tags && retval == -EUCLEAN
  130. && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
  131. tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
  132. dev->n_ecc_fixed++;
  133. }
  134. if (retval == 0)
  135. return YAFFS_OK;
  136. else
  137. return YAFFS_FAIL;
  138. }
  139. int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no)
  140. {
  141. struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  142. int retval;
  143. yaffs_trace(YAFFS_TRACE_MTD,
  144. "nandmtd2_mark_block_bad %d", block_no);
  145. retval =
  146. mtd_block_markbad(mtd,
  147. block_no * dev->param.chunks_per_block *
  148. dev->param.total_bytes_per_chunk);
  149. if (retval == 0)
  150. return YAFFS_OK;
  151. else
  152. return YAFFS_FAIL;
  153. }
  154. int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
  155. enum yaffs_block_state *state, u32 * seq_number)
  156. {
  157. struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  158. int retval;
  159. yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_query_block %d", block_no);
  160. retval =
  161. mtd_block_isbad(mtd,
  162. block_no * dev->param.chunks_per_block *
  163. dev->param.total_bytes_per_chunk);
  164. if (retval) {
  165. yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
  166. *state = YAFFS_BLOCK_STATE_DEAD;
  167. *seq_number = 0;
  168. } else {
  169. struct yaffs_ext_tags t;
  170. nandmtd2_read_chunk_tags(dev, block_no *
  171. dev->param.chunks_per_block, NULL, &t);
  172. if (t.chunk_used) {
  173. *seq_number = t.seq_number;
  174. *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  175. } else {
  176. *seq_number = 0;
  177. *state = YAFFS_BLOCK_STATE_EMPTY;
  178. }
  179. }
  180. yaffs_trace(YAFFS_TRACE_MTD,
  181. "block is bad seq %d state %d", *seq_number, *state);
  182. if (retval == 0)
  183. return YAFFS_OK;
  184. else
  185. return YAFFS_FAIL;
  186. }