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