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