comparison services/ffs/task.c @ 0:75a11d740a02

initial import of gsm-fw from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 09 Jun 2016 00:02:41 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:75a11d740a02
1 /******************************************************************************
2 * Flash File System (ffs)
3 * Idea, design and coding by Mads Meisner-Jensen, mmj@ti.com
4 *
5 * FFS task. ONLY for target!
6 *
7 * $Id: task.c 1.48.1.1.1.24 Thu, 18 Dec 2003 10:50:52 +0100 tsj $
8 *
9 ******************************************************************************/
10
11 #include "../../include/config.h"
12 #include "ffs.h"
13 #include "core.h"
14 #include "task.h"
15 #include "ffstrace.h"
16 #include "intctl.h"
17 #include "ramffs.h"
18 #include "../../riviera/rvm/rvm_use_id_list.h"
19 #include <string.h>
20
21 /******************************************************************************
22 * Globals and function prototypes
23 ******************************************************************************/
24
25 extern UINT8 pcm_init(void);
26 void ffs_task(void);
27
28 static effs_t ffs_init_status;
29 req_id_t request_id_last = 0;
30
31 T_OS_MB_ID ffs_mb_id;
32 UINT16 ffs_addr_id;
33
34 /******************************************************************************
35 * FFS Test Task
36 ******************************************************************************/
37
38 // For this to work, change:
39 // 1. MAX_RVF_TASKS define in rvf_target.h
40
41 // Note that the FFS_TEST_TASK_ID is set one too low! We assume we are lucky
42 // that no other task has the same ID...
43
44 #if (WITH_TFFS == 1)
45
46 #define FFS_TEST_STACK_SIZE 1024
47
48 #define FFS_TEST_TASK_ID (MAX_RVF_TASKS - 1 - 1)
49
50 void ffs_test_task(void);
51 void test_init(int keepgoing);
52 int test_run(char *testname);
53
54 static uint8 ffs_test_stack[FFS_TEST_STACK_SIZE];
55
56 // This is the string of test cases to run by calling test_run(). This
57 // string as written to from tmffs module.
58 char ffs_test_string[128];
59
60 // Delay for <delay> milliseconds
61 void tffs_delay(int delay)
62 {
63 delay = 14 * delay / 64; // approx. same as division by 60/13
64 OS_DELAY(delay);
65 }
66
67 UINT32 rvf_get_tick_count(void);
68 // Timer functions for benchmarking
69 UINT32 tffs_timer_begin(void)
70 {
71 return rvf_get_tick_count();
72 }
73
74 UINT32 tffs_timer_end(UINT32 time_begin)
75 {
76 // return current time minus time_begin
77 UINT32 ticks;
78
79 ticks = rvf_get_tick_count();
80
81 return (ticks - time_begin) * 60 / 13;
82 }
83
84 void ffs_test_task(void)
85 {
86 effs_t error;
87
88 OS_DELAY(217); // wait approx. 1000ms
89
90 ttw(str(TTrTest, "ffs_test_task()" NL));
91
92 while (1) {
93 OS_DELAY(217); // wait approx. 1000ms
94
95 // Poll to see if we have tests to run... We know that the writer of
96 // ffs_test_string has a higher priority than us, so it is properly
97 // written when we reach here.
98 if (*ffs_test_string) {
99 test_init(0);
100 error = test_run(ffs_test_string);
101 *ffs_test_string = 0;
102
103 if (error == 0)
104 ttw(str(TTrTest, "TEST succeeded" NL));
105 else
106 ttw(ttr(TTrTest, "TEST cases failed: %d" NL, error));
107 }
108 }
109 }
110
111 #endif // End of WITH_TFFS
112
113 /******************************************************************************
114 * Target Platform Abstraction Functions
115 ******************************************************************************/
116
117 req_id_t request_id_get(void)
118 {
119 uint32 cpsr;
120
121 // We disable interrupt to avoid any other tasks to get the same id.
122 cpsr = int_disable();
123 request_id_last++;
124
125 if (request_id_last < 0)
126 request_id_last = 0;
127
128 int_enable(cpsr);
129
130 return request_id_last;
131 }
132
133 void *target_malloc(unsigned int size)
134 {
135 char *buf;
136
137 #if (_RVF == 1)
138 if ((rvf_get_buf(ffs_mb_id, size, (T_RVF_BUFFER*) &buf)) == RVF_RED)
139 return 0;
140 else
141 return buf;
142 #else
143 return 0;
144 #endif
145 }
146
147 void target_free(void *buf)
148 {
149 int error;
150
151 #if (_RVF == 1)
152 if ((error = OS_FREE(buf)) != OS_OK)
153 ttw(ttr(TTrFatal, "target_free() %d (FAILED)" NL, error));
154 #endif
155 }
156
157
158 /******************************************************************************
159 * FFS Blocking Call Handling
160 ******************************************************************************/
161
162 effs_t ffs_b_begin(struct ffs_blocking_s *fb, T_RVF_MUTEX *mutex, int *result)
163 {
164 effs_t error;
165
166 if ((error = rvf_initialize_mutex(mutex)) < 0)
167 return error;
168
169 if ((error = rvf_lock_mutex(mutex)) < 0) // This will succeed
170 return error;
171
172 fb->result = result;
173 fb->mutex = mutex;
174
175 return EFFS_OK;
176 }
177
178 effs_t ffs_b_end(T_RVF_MUTEX *mutex, int result)
179 {
180 effs_t error;
181
182 // Do not lock the mutex if the message send operation failed
183 if (result >= 0)
184 // This will block the task until the mutex has been released
185 if ((error = rvf_lock_mutex(mutex)) < 0)
186 return error;
187
188 if ((error = rvf_unlock_mutex(mutex)) < 0)
189 return error;
190
191 if ((error = rvf_delete_mutex(mutex)) < 0)
192 return error;
193
194 return EFFS_OK;
195 }
196
197 /******************************************************************************
198 * FFS Task
199 ******************************************************************************/
200 /* The below access to the intenal Nucleus variable "TCD_Interrupt_Level" is
201 * a workaround for a known Nucleus BUG (see CQ SWI-FIX-17560) */
202
203 void ffs_main_init()
204 {
205 extern int TCD_Interrupt_Level;
206 int tmp_int_level;
207 ttr_init(TTrTask|TTrTest|TTrTestInfo);
208 //ttr_init(TTrTask|TTrTest|TTrTestInfo|TTrDrvErase|TTrDrvWrite|TTrTaskLow|TTrApi);
209
210 tmp_int_level = TCD_Interrupt_Level; // Backup Int level
211 TCD_Interrupt_Level = 0xC0; // The Interrups are not yet enabled..
212 #if FFS_IN_RAM
213 #if CONFIG_MOKOFFS_COPY
214 bcopy_32byte_chunks(0x380000, _RAMFFS_area, 0x70000);
215 #else
216 memset(_RAMFFS_area, 0xFF, RAMFFS_TOTAL_SIZE);
217 #endif
218 #endif
219 ffs_init_status = ffs_initialize();
220 TCD_Interrupt_Level = tmp_int_level; // Restore Int level
221
222 #if 0 // moved to Application_Initialize() - FreeCalypso change
223 pcm_init(); // We have to call pcm_init() before G23 starts.
224 #endif
225 }
226
227 void ffs_task_init(T_OS_MB_ID mbid, T_RVF_ADDR_ID addr_id)
228 {
229 int error;
230
231 ffs_mb_id = mbid;
232 ffs_addr_id = addr_id;
233
234 #if (WITH_TFFS == 1)
235 error =
236 OS_CREATE_TASK((TASKPTR) ffs_test_task, FFS_TEST_TASK_ID, "TFFS",
237 ffs_test_stack, FFS_TEST_STACK_SIZE,
238 249, // priority
239 4, // ET4_TASK type
240 0, // no time slicing
241 RUNNING); // start state
242
243 ttw(ttr(TTrTask, "os_create_task('TFFS') %d" NL, error));
244 #endif
245 }
246
247 void ffs_task()
248 {
249 extern int etm_ffs_init(void);
250 struct ffs_req_s *request;
251 T_FFS_FILE_CNF *confirm_file;
252 T_FFS_STREAM_CNF *confirm_stream;
253 T_RVF_MUTEX *mutex_p;
254 int error, free_mail, os_error = OS_OK;
255 uint16 revision, manufacturer_id, device_id;
256 uint32 base;
257 fd_t fdi;
258 char *temp_path;
259 req_id_t temp_id;
260
261 #if FFS_IN_RAM && !CONFIG_MOKOFFS_COPY
262 // Non formatted FFS should be formatted
263 // So we don't have to use PCTM to format it
264 if (fs.initerror == EFFS_NOFORMAT)
265 {
266 ffs_format_nb("/", 0x2BAD, 0);
267 }
268
269 /* ffs_InitRFCap(); */
270 #endif
271
272 ttr(TTrTask, "ffs_init() %d" NL, ffs_init_status);
273
274 ffs_query(Q_FFS_REVISION, &revision);
275 ttr(TTrTask,"FFS revision: 0x%x" NL, revision);
276
277 ffs_query(Q_DEV_MANUFACTURER, &manufacturer_id);
278 ffs_query(Q_DEV_DEVICE, &device_id);
279 ffs_query(Q_DEV_DRIVER, &revision);
280 ttr(TTrTask,"FFS device, driver: 0x%02x, 0x%04x, %d" NL,
281 manufacturer_id, device_id, revision);
282
283 ffs_query(Q_DEV_BASE, &base);
284 ffs_query(Q_DEV_BLOCKS, &revision);
285 ttr(TTrTask,"FFS base, blocks: 0x%x, %d" NL, base, revision);
286
287 ffs_query(Q_FFS_FORMAT_READ, &manufacturer_id);
288 ffs_query(Q_FFS_FORMAT_WRITE, &device_id);
289 ttr(TTrTask,"FFS format read, write: 0x%x, 0x%x" NL NL,
290 manufacturer_id, device_id);
291
292 // If some blocks has been marked for reclaim, reclaim them now...
293 blocks_reclaim();
294
295 // We can only mkdir("pcm") *after* our mailbox has been allocated
296 // otherwise mkdir("pcm") will fail.
297 error = ffs_mkdir_nb("/pcm", 0);
298
299 // Register FFS to ETM database
300 error = etm_ffs_init();
301
302 while (1)
303 {
304 OS_MAIL_WAIT(OS_MAIL_EVENT_MASK);
305 request = (struct ffs_req_s *) OS_MAIL_READ();
306
307 ttw(ttr(TTrTaskLow, "ffs_task(%d):" NL, request->cmd));
308 switch (request->cmd) {
309 case WRITE: case SEEK: case CLOSE: case FTRUNC: case FDATASYNC:
310 ttw(ttr(TTrTaskLow, " fdi = %d" NL, request->fdi));
311 // Save a local copy of fdi because the task we call later can
312 // modify it and we have to use the fdi if a callback is used
313 fdi = (fd_t)request->fdi;
314 break;
315 default:
316 ttw(ttr(TTrTaskLow, " name = %s" NL, request->path));
317 break;
318 }
319 ttw(ttr(TTrTaskLow, " src = 0x%x" NL, request->src));
320 ttw(ttr(TTrTaskLow, " size = %d" NL, request->size));
321
322 if (tr_query(TTrTaskDelays))
323 OS_DELAY(5);
324
325 switch (request->cmd) {
326 case NOP: error = EFFS_OK; break;
327 case FILE_WRITE: error = task_file_write(request); break;
328 case SYMLINK: error = task_symlink(request); break;
329 case MKDIR: error = task_mkdir(request); break;
330 case REMOVE: error = task_remove(request); break;
331 case RENAME: error = task_rename(request); break;
332 case FCONTROL: error = task_fcontrol(request); break;
333 case PREFORMAT: error = task_preformat(request); break;
334 case FORMAT: error = task_format(request); break;
335 case OPEN: error = task_open(request); break;
336 case WRITE: error = task_write(request); break;
337 case SEEK: error = task_seek(request); break;
338 case CLOSE: error = task_close(request); break;
339 case TRUNC: error = task_trunc(request); break;
340 case FTRUNC: error = task_ftrunc(request); break;
341 case FDATASYNC: error = task_fdatasync(request); break;
342 default:
343 ttr(TTrFatal, "FFS FATAL: bad request: %d" NL, request->cmd);
344 break;
345 }
346
347 ttw(ttr(TTrTaskLow, "ffs_task(%d) %d" NL, request->cmd, error));
348 if (tr_query(TTrTaskDelays))
349 OS_DELAY(5);
350
351 // Call-back to caller
352 mutex_p = 0;
353 if (request->cp) {
354 free_mail = 0; // don't free mail we will reuse it
355 // We reuse the mail we received for our call-back. Due to
356 // this reuse, we must be careful with the order of
357 // assignments. If this is a stream modify function use
358 // ffs_stream_cnf else use ffs_file_cnf
359
360 temp_id = request->request_id; // Save id before we reuse the mail mem.
361 switch (request->cmd) {
362 case WRITE: case SEEK: case CLOSE: case FTRUNC: case FDATASYNC:
363 confirm_stream = (T_FFS_STREAM_CNF *) request;
364 confirm_stream->error = error;
365 confirm_stream->request_id = temp_id;
366 confirm_stream->fdi = fdi;
367 confirm_stream->header.msg_id = FFS_MESSAGE_OFFSET;
368
369 if (request->cp->callback_func) {
370 request->cp->callback_func((T_FFS_STREAM_CNF *)
371 confirm_stream);
372 }
373 else if (request->cp->addr_id) {
374 os_error = OS_MAIL_SEND(request->cp->addr_id,
375 confirm_stream);
376 }
377 else {
378 ttr(TTrFatal, "FFS WARNING: empty return path" NL);
379 free_mail = 1; // free mail
380 }
381 break;
382 default:
383 temp_path = (char *) request->path;
384 confirm_file = (T_FFS_FILE_CNF *) request;
385 confirm_file->error = error;
386 confirm_file->request_id = temp_id;
387 confirm_file->path = temp_path;
388 confirm_file->header.msg_id = FFS_MESSAGE_OFFSET;
389
390 if (request->cp->callback_func) {
391 request->cp->callback_func((T_FFS_FILE_CNF *) confirm_file);
392 }
393
394 else if (request->cp->addr_id) {
395 os_error = OS_MAIL_SEND(request->cp->addr_id,
396 confirm_file);
397 }
398 else {
399 ttr(TTrFatal, "FFS WARNING: empty return path" NL);
400 free_mail = 1; // free mail
401 }
402 break;
403 }
404
405 if (os_error != OS_OK)
406 ttr(TTrFatal, "FFS FATAL: os_send_msg() %d" NL, os_error);
407 }
408
409 // Blocking handling / unlocking mutex
410 else if (request->fb) {
411 // Save the pointer to the mutex and the error value (allocated
412 // on the stack by the macro FFS_BLOCKING_BEGIN)
413 mutex_p = request->fb->mutex;
414 *request->fb->result = error;
415 free_mail = 1; // free mail
416 }
417
418 else {
419 // The task must have been a non blocking without any callback
420 free_mail = 1; // free mail
421 }
422
423 // Free the mail memory. Note that we always free it, except when we
424 // have successfully reused it for sending a mail call-back.
425 if (free_mail == 1) {
426 os_error = OS_FREE(request);
427 if (os_error != OS_OK)
428 ttr(TTrFatal, "FFS FATAL: os_free() %d" NL, os_error);
429 }
430
431 // Blocking handling / unlocking mutex
432 if (mutex_p) {
433 // Wake up the caller task
434 if ((os_error = rvf_unlock_mutex(mutex_p)) < 0)
435 ttr(TTrFatal, "FFS FATAL: rvf_unlock_mutex() %d" NL, os_error);
436 }
437
438 tr_bstat();
439 }
440 }