1/******************************************************************************
2 *
3 *  Copyright (C) 1999-2012 Broadcom Corporation
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18#ifndef GKI_COMMON_H
19#define GKI_COMMON_H
20
21#include "gki.h"
22#include "dyn_mem.h"
23
24#ifndef GKI_DEBUG
25#define GKI_DEBUG	FALSE
26#endif
27
28/* Task States: (For OSRdyTbl) */
29#define TASK_DEAD       0   /* b0000 */
30#define TASK_READY      1   /* b0001 */
31#define TASK_WAIT       2   /* b0010 */
32#define TASK_DELAY      4   /* b0100 */
33#define TASK_SUSPEND    8   /* b1000 */
34
35
36/********************************************************************
37**  Internal Error codes
38*********************************************************************/
39#define GKI_ERROR_BUF_CORRUPTED         0xFFFF
40#define GKI_ERROR_NOT_BUF_OWNER         0xFFFE
41#define GKI_ERROR_FREEBUF_BAD_QID       0xFFFD
42#define GKI_ERROR_FREEBUF_BUF_LINKED    0xFFFC
43#define GKI_ERROR_SEND_MSG_BAD_DEST     0xFFFB
44#define GKI_ERROR_SEND_MSG_BUF_LINKED   0xFFFA
45#define GKI_ERROR_ENQUEUE_BUF_LINKED    0xFFF9
46#define GKI_ERROR_DELETE_POOL_BAD_QID   0xFFF8
47#define GKI_ERROR_BUF_SIZE_TOOBIG       0xFFF7
48#define GKI_ERROR_BUF_SIZE_ZERO         0xFFF6
49#define GKI_ERROR_ADDR_NOT_IN_BUF       0xFFF5
50
51
52/********************************************************************
53**  Misc constants
54*********************************************************************/
55
56#define GKI_MAX_INT32           (0x7fffffffL)
57#define GKI_MAX_TIMESTAMP       (0xffffffffL)
58
59/********************************************************************
60**  Buffer Management Data Structures
61*********************************************************************/
62
63typedef struct _buffer_hdr
64{
65    struct _buffer_hdr *p_next;   /* next buffer in the queue */
66    UINT8   q_id;                 /* id of the queue */
67    UINT8   task_id;              /* task which allocated the buffer*/
68    UINT8   status;               /* FREE, UNLINKED or QUEUED */
69    UINT8   Type;
70
71#if GKI_BUFFER_DEBUG
72    /* for tracking who allocated the buffer */
73    #define _GKI_MAX_FUNCTION_NAME_LEN   (50)
74    char    _function[_GKI_MAX_FUNCTION_NAME_LEN+1];
75    int     _line;
76#endif
77
78} BUFFER_HDR_T;
79
80typedef struct _free_queue
81{
82    BUFFER_HDR_T *p_first;      /* first buffer in the queue */
83    BUFFER_HDR_T *p_last;       /* last buffer in the queue */
84    UINT16          size;          /* size of the buffers in the pool */
85    UINT16          total;         /* toatal number of buffers */
86    UINT16          cur_cnt;       /* number of  buffers currently allocated */
87    UINT16          max_cnt;       /* maximum number of buffers allocated at any time */
88} FREE_QUEUE_T;
89
90
91/* Buffer related defines
92*/
93#define ALIGN_POOL(pl_size)  ( (((pl_size) + 3) / sizeof(UINT32)) * sizeof(UINT32))
94#define BUFFER_HDR_SIZE     (sizeof(BUFFER_HDR_T))                  /* Offset past header */
95#define BUFFER_PADDING_SIZE (sizeof(BUFFER_HDR_T) + sizeof(UINT32)) /* Header + Magic Number */
96#define MAX_USER_BUF_SIZE   ((UINT16)0xffff - BUFFER_PADDING_SIZE)  /* pool size must allow for header */
97#define MAGIC_NO            0xDDBADDBA
98
99#define BUF_STATUS_FREE     0
100#define BUF_STATUS_UNLINKED 1
101#define BUF_STATUS_QUEUED   2
102
103#define GKI_USE_DEFERED_ALLOC_BUF_POOLS
104
105/* Exception related structures (Used in debug mode only)
106*/
107#if (GKI_DEBUG == TRUE)
108typedef struct
109{
110    UINT16  type;
111    UINT8   taskid;
112    UINT8   msg[GKI_MAX_EXCEPTION_MSGLEN];
113} EXCEPTION_T;
114#endif
115
116
117/* Put all GKI variables into one control block
118*/
119typedef struct
120{
121    /* Task management variables
122    */
123    /* The stack and stack size are not used on Windows
124    */
125#if (!defined GKI_USE_DEFERED_ALLOC_BUF_POOLS && (GKI_USE_DYNAMIC_BUFFERS == FALSE))
126
127#if (GKI_NUM_FIXED_BUF_POOLS > 0)
128    UINT8 bufpool0[(ALIGN_POOL(GKI_BUF0_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF0_MAX];
129#endif
130
131#if (GKI_NUM_FIXED_BUF_POOLS > 1)
132    UINT8 bufpool1[(ALIGN_POOL(GKI_BUF1_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF1_MAX];
133#endif
134
135#if (GKI_NUM_FIXED_BUF_POOLS > 2)
136    UINT8 bufpool2[(ALIGN_POOL(GKI_BUF2_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF2_MAX];
137#endif
138
139#if (GKI_NUM_FIXED_BUF_POOLS > 3)
140    UINT8 bufpool3[(ALIGN_POOL(GKI_BUF3_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF3_MAX];
141#endif
142
143#if (GKI_NUM_FIXED_BUF_POOLS > 4)
144    UINT8 bufpool4[(ALIGN_POOL(GKI_BUF4_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF4_MAX];
145#endif
146
147#if (GKI_NUM_FIXED_BUF_POOLS > 5)
148    UINT8 bufpool5[(ALIGN_POOL(GKI_BUF5_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF5_MAX];
149#endif
150
151#if (GKI_NUM_FIXED_BUF_POOLS > 6)
152    UINT8 bufpool6[(ALIGN_POOL(GKI_BUF6_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF6_MAX];
153#endif
154
155#if (GKI_NUM_FIXED_BUF_POOLS > 7)
156    UINT8 bufpool7[(ALIGN_POOL(GKI_BUF7_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF7_MAX];
157#endif
158
159#if (GKI_NUM_FIXED_BUF_POOLS > 8)
160    UINT8 bufpool8[(ALIGN_POOL(GKI_BUF8_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF8_MAX];
161#endif
162
163#if (GKI_NUM_FIXED_BUF_POOLS > 9)
164    UINT8 bufpool9[(ALIGN_POOL(GKI_BUF9_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF9_MAX];
165#endif
166
167#if (GKI_NUM_FIXED_BUF_POOLS > 10)
168    UINT8 bufpool10[(ALIGN_POOL(GKI_BUF10_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF10_MAX];
169#endif
170
171#if (GKI_NUM_FIXED_BUF_POOLS > 11)
172    UINT8 bufpool11[(ALIGN_POOL(GKI_BUF11_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF11_MAX];
173#endif
174
175#if (GKI_NUM_FIXED_BUF_POOLS > 12)
176    UINT8 bufpool12[(ALIGN_POOL(GKI_BUF12_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF12_MAX];
177#endif
178
179#if (GKI_NUM_FIXED_BUF_POOLS > 13)
180    UINT8 bufpool13[(ALIGN_POOL(GKI_BUF13_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF13_MAX];
181#endif
182
183#if (GKI_NUM_FIXED_BUF_POOLS > 14)
184    UINT8 bufpool14[(ALIGN_POOL(GKI_BUF14_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF14_MAX];
185#endif
186
187#if (GKI_NUM_FIXED_BUF_POOLS > 15)
188    UINT8 bufpool15[(ALIGN_POOL(GKI_BUF15_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF15_MAX];
189#endif
190
191#else
192/* Definitions for dynamic buffer use */
193#if (GKI_NUM_FIXED_BUF_POOLS > 0)
194    UINT8 *bufpool0;
195#endif
196
197#if (GKI_NUM_FIXED_BUF_POOLS > 1)
198    UINT8 *bufpool1;
199#endif
200
201#if (GKI_NUM_FIXED_BUF_POOLS > 2)
202    UINT8 *bufpool2;
203#endif
204
205#if (GKI_NUM_FIXED_BUF_POOLS > 3)
206    UINT8 *bufpool3;
207#endif
208
209#if (GKI_NUM_FIXED_BUF_POOLS > 4)
210    UINT8 *bufpool4;
211#endif
212
213#if (GKI_NUM_FIXED_BUF_POOLS > 5)
214    UINT8 *bufpool5;
215#endif
216
217#if (GKI_NUM_FIXED_BUF_POOLS > 6)
218    UINT8 *bufpool6;
219#endif
220
221#if (GKI_NUM_FIXED_BUF_POOLS > 7)
222    UINT8 *bufpool7;
223#endif
224
225#if (GKI_NUM_FIXED_BUF_POOLS > 8)
226    UINT8 *bufpool8;
227#endif
228
229#if (GKI_NUM_FIXED_BUF_POOLS > 9)
230    UINT8 *bufpool9;
231#endif
232
233#if (GKI_NUM_FIXED_BUF_POOLS > 10)
234    UINT8 *bufpool10;
235#endif
236
237#if (GKI_NUM_FIXED_BUF_POOLS > 11)
238    UINT8 *bufpool11;
239#endif
240
241#if (GKI_NUM_FIXED_BUF_POOLS > 12)
242    UINT8 *bufpool12;
243#endif
244
245#if (GKI_NUM_FIXED_BUF_POOLS > 13)
246    UINT8 *bufpool13;
247#endif
248
249#if (GKI_NUM_FIXED_BUF_POOLS > 14)
250    UINT8 *bufpool14;
251#endif
252
253#if (GKI_NUM_FIXED_BUF_POOLS > 15)
254    UINT8 *bufpool15;
255#endif
256
257#endif
258
259    UINT8  *OSStack[GKI_MAX_TASKS];         /* pointer to beginning of stack */
260    UINT16  OSStackSize[GKI_MAX_TASKS];     /* stack size available to each task */
261
262
263    INT8   *OSTName[GKI_MAX_TASKS];         /* name of the task */
264
265    UINT8   OSRdyTbl[GKI_MAX_TASKS];        /* current state of the task */
266    UINT16  OSWaitEvt[GKI_MAX_TASKS];       /* events that have to be processed by the task */
267    UINT16  OSWaitForEvt[GKI_MAX_TASKS];    /* events the task is waiting for*/
268
269    UINT32  OSTicks;                        /* system ticks from start */
270    UINT32  OSIdleCnt;                      /* idle counter */
271    INT16   OSDisableNesting;               /* counter to keep track of interrupt disable nesting */
272    INT16   OSLockNesting;                  /* counter to keep track of sched lock nesting */
273    INT16   OSIntNesting;                   /* counter to keep track of interrupt nesting */
274
275    /* Timer related variables
276    */
277    INT32   OSTicksTilExp;      /* Number of ticks till next timer expires */
278#if (defined(GKI_DELAY_STOP_SYS_TICK) && (GKI_DELAY_STOP_SYS_TICK > 0))
279    UINT32  OSTicksTilStop;     /* inactivity delay timer; OS Ticks till stopping system tick */
280#endif
281    INT32   OSNumOrigTicks;     /* Number of ticks between last timer expiration to the next one */
282
283    INT32   OSWaitTmr   [GKI_MAX_TASKS];  /* ticks the task has to wait, for specific events */
284
285    /* Only take up space timers used in the system (GKI_NUM_TIMERS defined in target.h) */
286#if (GKI_NUM_TIMERS > 0)
287    INT32   OSTaskTmr0  [GKI_MAX_TASKS];
288    INT32   OSTaskTmr0R [GKI_MAX_TASKS];
289#endif
290
291#if (GKI_NUM_TIMERS > 1)
292    INT32   OSTaskTmr1  [GKI_MAX_TASKS];
293    INT32   OSTaskTmr1R [GKI_MAX_TASKS];
294#endif
295
296#if (GKI_NUM_TIMERS > 2)
297    INT32   OSTaskTmr2  [GKI_MAX_TASKS];
298    INT32   OSTaskTmr2R [GKI_MAX_TASKS];
299#endif
300
301#if (GKI_NUM_TIMERS > 3)
302    INT32   OSTaskTmr3  [GKI_MAX_TASKS];
303    INT32   OSTaskTmr3R [GKI_MAX_TASKS];
304#endif
305
306
307
308    /* Buffer related variables
309    */
310    BUFFER_HDR_T    *OSTaskQFirst[GKI_MAX_TASKS][NUM_TASK_MBOX]; /* array of pointers to the first event in the task mailbox */
311    BUFFER_HDR_T    *OSTaskQLast [GKI_MAX_TASKS][NUM_TASK_MBOX]; /* array of pointers to the last event in the task mailbox */
312
313    /* Define the buffer pool management variables
314    */
315    FREE_QUEUE_T    freeq[GKI_NUM_TOTAL_BUF_POOLS];
316
317    UINT16   pool_buf_size[GKI_NUM_TOTAL_BUF_POOLS];
318    UINT16   pool_max_count[GKI_NUM_TOTAL_BUF_POOLS];
319    UINT16   pool_additions[GKI_NUM_TOTAL_BUF_POOLS];
320
321    /* Define the buffer pool start addresses
322    */
323    UINT8   *pool_start[GKI_NUM_TOTAL_BUF_POOLS];   /* array of pointers to the start of each buffer pool */
324    UINT8   *pool_end[GKI_NUM_TOTAL_BUF_POOLS];     /* array of pointers to the end of each buffer pool */
325    UINT16   pool_size[GKI_NUM_TOTAL_BUF_POOLS];    /* actual size of the buffers in a pool */
326
327    /* Define the buffer pool access control variables */
328    void        *p_user_mempool;                    /* User O/S memory pool */
329    UINT16      pool_access_mask;                   /* Bits are set if the corresponding buffer pool is a restricted pool */
330    UINT8       pool_list[GKI_NUM_TOTAL_BUF_POOLS]; /* buffer pools arranged in the order of size */
331    UINT8       curr_total_no_of_pools;             /* number of fixed buf pools + current number of dynamic pools */
332
333    BOOLEAN     timer_nesting;                      /* flag to prevent timer interrupt nesting */
334
335    /* Time queue arrays */
336    TIMER_LIST_Q *timer_queues[GKI_MAX_TIMER_QUEUES];
337    /* System tick callback */
338    SYSTEM_TICK_CBACK *p_tick_cb;
339    BOOLEAN     system_tick_running;                /* TRUE if system tick is running. Valid only if p_tick_cb is not NULL */
340
341#if (GKI_DEBUG == TRUE)
342    UINT16      ExceptionCnt;                       /* number of GKI exceptions that have happened */
343    EXCEPTION_T Exception[GKI_MAX_EXCEPTION];
344#endif
345
346} tGKI_COM_CB;
347
348
349#ifdef __cplusplus
350extern "C" {
351#endif
352
353/* Internal GKI function prototypes
354*/
355GKI_API extern BOOLEAN   gki_chk_buf_damage(void *);
356extern BOOLEAN   gki_chk_buf_owner(void *);
357extern void      gki_buffer_init (void);
358extern void      gki_timers_init(void);
359extern void      gki_adjust_timer_count (INT32);
360
361extern void    OSStartRdy(void);
362extern void	   OSCtxSw(void);
363extern void	   OSIntCtxSw(void);
364extern void    OSSched(void);
365extern void    OSIntEnter(void);
366extern void    OSIntExit(void);
367
368
369/* Debug aids
370*/
371typedef void  (*FP_PRINT)(char *, ...);
372
373#if (GKI_DEBUG == TRUE)
374
375typedef void  (*PKT_PRINT)(UINT8 *, UINT16);
376
377extern void gki_print_task(FP_PRINT);
378extern void gki_print_exception(FP_PRINT);
379extern void gki_print_timer(FP_PRINT);
380extern void gki_print_stack(FP_PRINT);
381extern void gki_print_buffer(FP_PRINT);
382extern void gki_print_buffer_statistics(FP_PRINT, INT16);
383GKI_API extern void gki_print_used_bufs (FP_PRINT, UINT8);
384extern void gki_dump(UINT8 *, UINT16, FP_PRINT);
385extern void gki_dump2(UINT16 *, UINT16, FP_PRINT);
386extern void gki_dump4(UINT32 *, UINT16, FP_PRINT);
387
388#endif
389#ifdef __cplusplus
390}
391#endif
392
393#endif
394