Prevent recursive gc
[yaffs2.git] / yaffs_ramem2k.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  * yaffs_ramem.c  NAND emulation on top of a chunk of RAM
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15  //yaffs_ramem2k.c: RAM emulation in-kernel for 2K pages (YAFFS2)
16
17
18 const char *yaffs_ramem2k_c_version = "$Id: yaffs_ramem2k.c,v 1.2 2005-04-24 09:26:08 charles Exp $";
19
20 #ifndef __KERNEL__
21 #define CONFIG_YAFFS_RAM_ENABLED
22 #endif
23
24 #ifdef CONFIG_YAFFS_RAM_ENABLED
25
26 #include "yportenv.h"
27
28 #include "yaffs_nandemul2k.h"
29 #include "yaffs_guts.h"
30 #include "yaffsinterface.h"
31 #include "devextras.h"
32 #include "yaffs_packedtags2.h"
33
34
35
36 #define EM_SIZE_IN_MEG (32)
37 #define PAGE_DATA_SIZE  (2048)
38 #define PAGE_SPARE_SIZE (64)
39 #define PAGES_PER_BLOCK (64)
40
41
42
43 #define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
44
45 #define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
46
47 #define BLOCK_TOTAL_SIZE (PAGES_PER_BLOCK * PAGE_TOTAL_SIZE)
48
49 #define BLOCKS_PER_MEG ((1<<20)/(PAGES_PER_BLOCK * PAGE_DATA_SIZE))
50
51
52 typedef struct 
53 {
54         __u8 data[PAGE_TOTAL_SIZE]; // Data + spare
55         int empty;      // is this empty?
56 } nandemul_Page;
57
58
59 typedef struct
60 {
61         nandemul_Page *page[PAGES_PER_BLOCK];
62         int damaged;    
63 } nandemul_Block;
64
65
66
67 typedef struct
68 {
69         nandemul_Block**block;
70         int nBlocks;
71 } nandemul_Device;
72
73 static nandemul_Device ned;
74
75 static int sizeInMB = EM_SIZE_IN_MEG;
76
77
78 static void nandemul_yield(int n)
79 {
80 #ifdef __KERNEL__
81         if(n > 0) schedule_timeout(n);
82 #endif
83
84 }
85
86
87 static void nandemul_ReallyEraseBlock(int blockNumber)
88 {
89         int i;
90         
91         nandemul_Block *blk;
92         
93         if(blockNumber < 0 || blockNumber >= ned.nBlocks)
94         {
95                 return;
96         }
97         
98         blk = ned.block[blockNumber];
99         
100         for(i = 0; i < PAGES_PER_BLOCK; i++)
101         {
102                 memset(blk->page[i],0xff,sizeof(nandemul_Page));
103                 blk->page[i]->empty = 1;
104         }
105         nandemul_yield(2);
106 }
107
108
109 static int nandemul2k_CalcNBlocks(void)
110 {
111         return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
112 }
113
114
115
116 static int  CheckInit(void)
117 {
118         static int initialised = 0;
119         
120         int i,j;
121         
122         int fail = 0;
123         int nBlocks; 
124
125         int nAllocated = 0;
126         
127         if(initialised) 
128         {
129                 return YAFFS_OK;
130         }
131         
132         
133         ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
134
135         
136         ned.block = YMALLOC(sizeof(nandemul_Block*) * nBlocks );
137         
138         if(!ned.block) return YAFFS_FAIL;
139         
140         
141         
142
143                 
144         for(i=fail=0; i <nBlocks; i++)
145         {
146                 
147                 nandemul_Block *blk;
148                 
149                 if(!(blk = ned.block[i] = YMALLOC(sizeof(nandemul_Block))))
150                 {
151                  fail = 1;
152                 }  
153                 else
154                 {
155                         for(j = 0; j < PAGES_PER_BLOCK; j++)
156                         {
157                                 if((blk->page[j] = YMALLOC(sizeof(nandemul_Page))) == 0)
158                                 {
159                                         fail = 1;
160                                 }
161                         }
162                         nandemul_ReallyEraseBlock(i);
163                         ned.block[i]->damaged = 0;
164                         nAllocated++;
165                 }
166         }
167         
168         if(fail)
169         {
170                 //Todo thump pages
171                 
172                 for(i = 0; i < nAllocated; i++)
173                 {
174                         YFREE(ned.block[i]);
175                 }
176                 YFREE(ned.block);
177                 
178                 T(YAFFS_TRACE_ALWAYS,("Allocation failed, could only allocate %dMB of %dMB requested.\n",
179                    nAllocated/64,sizeInMB));
180                 return 0;
181         }
182         
183         ned.nBlocks = nBlocks;
184         
185         initialised = 1;
186         
187         return 1;
188 }
189
190 int nandemul2k_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
191 {
192         int blk;
193         int pg;
194         int i;
195         
196         __u8 *x;
197
198         
199         blk = chunkInNAND/PAGES_PER_BLOCK;
200         pg = chunkInNAND%PAGES_PER_BLOCK;
201         
202         
203         if(data)
204         {
205                 x = ned.block[blk]->page[pg]->data;
206                 
207                 for(i = 0; i < PAGE_DATA_SIZE; i++)
208                 {
209                         x[i] &=data[i];
210                 }
211
212                 ned.block[blk]->page[pg]->empty = 0;
213         }
214         
215         
216         if(tags)
217         {
218                 x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
219                 
220                 yaffs_PackTags2((yaffs_PackedTags2 *)x,tags);
221                         
222         }
223         
224         if(tags || data)
225         {
226                 nandemul_yield(1);
227         }
228
229         return YAFFS_OK;
230 }
231
232
233 int nandemul2k_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
234 {
235         int blk;
236         int pg;
237         
238         __u8 *x;
239
240         
241         
242         blk = chunkInNAND/PAGES_PER_BLOCK;
243         pg = chunkInNAND%PAGES_PER_BLOCK;
244         
245         
246         if(data)
247         {
248                 memcpy(data,ned.block[blk]->page[pg]->data,PAGE_DATA_SIZE);
249         }
250         
251         
252         if(tags)
253         {
254                 x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
255                 
256                 yaffs_UnpackTags2(tags,(yaffs_PackedTags2 *)x);
257         }
258
259         return YAFFS_OK;
260 }
261
262
263 int nandemul2k_CheckChunkErased(yaffs_Device *dev,int chunkInNAND)
264 {
265         int blk;
266         int pg;
267         int i;
268
269         
270         
271         blk = chunkInNAND/PAGES_PER_BLOCK;
272         pg = chunkInNAND%PAGES_PER_BLOCK;
273         
274         
275         for(i = 0; i < PAGE_TOTAL_SIZE; i++)
276         {
277                 if(ned.block[blk]->page[pg]->data[i] != 0xFF)
278                 {
279                         return YAFFS_FAIL;
280                 }
281         }
282
283         return YAFFS_OK;
284
285 }
286
287 int nandemul2k_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
288 {
289         
290         
291         if(blockNumber < 0 || blockNumber >= ned.nBlocks)
292         {
293                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
294         }
295         else if(ned.block[blockNumber]->damaged)
296         {
297                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase damaged block %d\n",blockNumber));
298         }
299         else
300         {
301                 nandemul_ReallyEraseBlock(blockNumber);
302         }
303         
304         return YAFFS_OK;
305 }
306
307 int nandemul2k_InitialiseNAND(yaffs_Device *dev)
308 {
309         CheckInit();
310         return YAFFS_OK;
311 }
312  
313 int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
314 {
315         
316         __u8 *x;
317         
318         x = &ned.block[blockNo]->page[0]->data[PAGE_DATA_SIZE];
319         
320         memset(x,0,sizeof(yaffs_PackedTags2));
321         
322         
323         return YAFFS_OK;
324         
325 }
326
327 int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
328 {
329         yaffs_ExtendedTags tags;
330         int chunkNo;
331
332         *sequenceNumber = 0;
333         
334         chunkNo = blockNo * dev->nChunksPerBlock;
335         
336         nandemul2k_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
337         if(tags.blockBad)
338         {
339                 *state = YAFFS_BLOCK_STATE_DEAD;
340         }
341         else if(!tags.chunkUsed)
342         {
343                 *state = YAFFS_BLOCK_STATE_EMPTY;
344         }
345         else if(tags.chunkUsed)
346         {
347                 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
348                 *sequenceNumber = tags.sequenceNumber;
349         }
350         return YAFFS_OK;
351 }
352
353 int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
354
355 int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
356 int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
357
358
359 #endif //YAFFS_RAM_ENABLED
360