yaffs2: Yaffs endian support
[yaffs2.git] / yaffs_tagsmarshall.c
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  * This file handles the marshalling (ie internal<-->external structure
14  * translation between the internal tags and the stored tags in Yaffs2-style
15  * tags storage.
16  */
17
18 #include "yaffs_guts.h"
19 #include "yaffs_trace.h"
20 #include "yaffs_packedtags2.h"
21
22 static int yaffs_tags_marshall_write(struct yaffs_dev *dev,
23                                     int nand_chunk, const u8 *data,
24                                     const struct yaffs_ext_tags *tags)
25 {
26         struct yaffs_packed_tags2 pt;
27         int retval;
28
29         int packed_tags_size =
30             dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
31         void *packed_tags_ptr =
32             dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
33
34         yaffs_trace(YAFFS_TRACE_MTD,
35                 "yaffs_tags_marshall_write chunk %d data %p tags %p",
36                 nand_chunk, data, tags);
37
38         /* For yaffs2 writing there must be both data and tags.
39          * If we're using inband tags, then the tags are stuffed into
40          * the end of the data buffer.
41          */
42         if (!data || !tags)
43                 BUG();
44         else if (dev->param.inband_tags) {
45                 struct yaffs_packed_tags2_tags_only *pt2tp;
46                 pt2tp =
47                     (struct yaffs_packed_tags2_tags_only *)(data +
48                                                         dev->
49                                                         data_bytes_per_chunk);
50                 yaffs_pack_tags2_tags_only(dev, pt2tp, tags);
51         } else {
52                 yaffs_pack_tags2(dev, &pt, tags, !dev->param.no_tags_ecc);
53         }
54
55         retval = dev->drv.drv_write_chunk_fn(dev, nand_chunk,
56                         data, dev->param.total_bytes_per_chunk,
57                         (dev->param.inband_tags) ? NULL : packed_tags_ptr,
58                         (dev->param.inband_tags) ? 0 : packed_tags_size);
59
60         return retval;
61 }
62
63 static int yaffs_tags_marshall_read(struct yaffs_dev *dev,
64                                    int nand_chunk, u8 *data,
65                                    struct yaffs_ext_tags *tags)
66 {
67         int retval = 0;
68         int local_data = 0;
69         u8 spare_buffer[100];
70         enum yaffs_ecc_result ecc_result;
71
72         struct yaffs_packed_tags2 pt;
73
74         int packed_tags_size =
75             dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
76         void *packed_tags_ptr =
77             dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
78
79         yaffs_trace(YAFFS_TRACE_MTD,
80                 "yaffs_tags_marshall_read chunk %d data %p tags %p",
81                 nand_chunk, data, tags);
82
83         if (dev->param.inband_tags) {
84                 if (!data) {
85                         local_data = 1;
86                         data = yaffs_get_temp_buffer(dev);
87                 }
88         }
89
90         if (dev->param.inband_tags || (data && !tags))
91                 retval = dev->drv.drv_read_chunk_fn(dev, nand_chunk,
92                                         data, dev->param.total_bytes_per_chunk,
93                                         NULL, 0,
94                                         &ecc_result);
95         else if (tags)
96                 retval = dev->drv.drv_read_chunk_fn(dev, nand_chunk,
97                                         data, dev->param.total_bytes_per_chunk,
98                                         spare_buffer, packed_tags_size,
99                                         &ecc_result);
100         else
101                 BUG();
102
103
104         if (dev->param.inband_tags) {
105                 if (tags) {
106                         struct yaffs_packed_tags2_tags_only *pt2tp;
107                         pt2tp =
108                                 (struct yaffs_packed_tags2_tags_only *)
109                                 &data[dev->data_bytes_per_chunk];
110                         yaffs_unpack_tags2_tags_only(dev, tags, pt2tp);
111                 }
112         } else if (tags) {
113                 memcpy(packed_tags_ptr, spare_buffer, packed_tags_size);
114                 yaffs_unpack_tags2(dev, tags, &pt, !dev->param.no_tags_ecc);
115         }
116
117         if (local_data)
118                 yaffs_release_temp_buffer(dev, data);
119
120         if (tags && ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
121                 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
122                 dev->n_ecc_unfixed++;
123         }
124
125         if (tags && ecc_result == -YAFFS_ECC_RESULT_FIXED) {
126                 if (tags->ecc_result <= YAFFS_ECC_RESULT_NO_ERROR)
127                         tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
128                 dev->n_ecc_fixed++;
129         }
130
131         if (ecc_result < YAFFS_ECC_RESULT_UNFIXED)
132                 return YAFFS_OK;
133         else
134                 return YAFFS_FAIL;
135 }
136
137 static int yaffs_tags_marshall_query_block(struct yaffs_dev *dev, int block_no,
138                                enum yaffs_block_state *state,
139                                u32 *seq_number)
140 {
141         int retval;
142
143         yaffs_trace(YAFFS_TRACE_MTD, "yaffs_tags_marshall_query_block %d",
144                         block_no);
145
146         retval = dev->drv.drv_check_bad_fn(dev, block_no);
147
148         if (retval== YAFFS_FAIL) {
149                 yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
150
151                 *state = YAFFS_BLOCK_STATE_DEAD;
152                 *seq_number = 0;
153         } else {
154                 struct yaffs_ext_tags t;
155
156                 yaffs_tags_marshall_read(dev,
157                                     block_no * dev->param.chunks_per_block,
158                                     NULL, &t);
159
160                 if (t.chunk_used) {
161                         *seq_number = t.seq_number;
162                         *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
163                 } else {
164                         *seq_number = 0;
165                         *state = YAFFS_BLOCK_STATE_EMPTY;
166                 }
167         }
168
169         yaffs_trace(YAFFS_TRACE_MTD,
170                 "block query returns  seq %d state %d",
171                 *seq_number, *state);
172
173         if (retval == 0)
174                 return YAFFS_OK;
175         else
176                 return YAFFS_FAIL;
177 }
178
179 static int yaffs_tags_marshall_mark_bad(struct yaffs_dev *dev, int block_no)
180 {
181         return dev->drv.drv_mark_bad_fn(dev, block_no);
182
183 }
184
185
186 void yaffs_tags_marshall_install(struct yaffs_dev *dev)
187 {
188         if (!dev->param.is_yaffs2)
189                 return;
190
191         if (!dev->tagger.write_chunk_tags_fn)
192                 dev->tagger.write_chunk_tags_fn = yaffs_tags_marshall_write;
193
194         if (!dev->tagger.read_chunk_tags_fn)
195                 dev->tagger.read_chunk_tags_fn = yaffs_tags_marshall_read;
196
197         if (!dev->tagger.query_block_fn)
198                 dev->tagger.query_block_fn = yaffs_tags_marshall_query_block;
199
200         if (!dev->tagger.mark_bad_fn)
201                 dev->tagger.mark_bad_fn = yaffs_tags_marshall_mark_bad;
202
203 }