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 /* Task States: (For OSRdyTbl) */
24 #define TASK_DEAD 0    /* b0000 */
25 #define TASK_READY 1   /* b0001 */
26 
27 /********************************************************************
28 **  Internal Error codes
29 *********************************************************************/
30 #define GKI_ERROR_BUF_CORRUPTED 0xFFFF
31 #define GKI_ERROR_NOT_BUF_OWNER 0xFFFE
32 #define GKI_ERROR_FREEBUF_BAD_QID 0xFFFD
33 #define GKI_ERROR_FREEBUF_BUF_LINKED 0xFFFC
34 #define GKI_ERROR_SEND_MSG_BAD_DEST 0xFFFB
35 #define GKI_ERROR_SEND_MSG_BUF_LINKED 0xFFFA
36 #define GKI_ERROR_ENQUEUE_BUF_LINKED 0xFFF9
37 #define GKI_ERROR_DELETE_POOL_BAD_QID 0xFFF8
38 #define GKI_ERROR_BUF_SIZE_TOOBIG 0xFFF7
39 #define GKI_ERROR_BUF_SIZE_ZERO 0xFFF6
40 
41 /********************************************************************
42 **  Misc constants
43 *********************************************************************/
44 
45 #define GKI_MAX_INT32 (0x7fffffffL)
46 
47 /********************************************************************
48 **  Buffer Management Data Structures
49 *********************************************************************/
50 
51 typedef struct _buffer_hdr {
52   struct _buffer_hdr* p_next; /* next buffer in the queue */
53   uint8_t q_id;               /* id of the queue */
54   uint8_t task_id;            /* task which allocated the buffer*/
55   uint8_t status;             /* FREE, UNLINKED or QUEUED */
56   uint8_t Type;
57 } BUFFER_HDR_T;
58 
59 typedef struct _free_queue {
60   BUFFER_HDR_T* p_first; /* first buffer in the queue */
61   BUFFER_HDR_T* p_last;  /* last buffer in the queue */
62   uint16_t size;         /* size of the buffers in the pool */
63   uint16_t total;        /* toatal number of buffers */
64   uint16_t cur_cnt;      /* number of  buffers currently allocated */
65   uint16_t max_cnt;      /* maximum number of buffers allocated at any time */
66 } FREE_QUEUE_T;
67 
68 /* Buffer related defines
69 */
70 #define ALIGN_POOL(pl_size) \
71   ((((pl_size) + 3) / sizeof(uint32_t)) * sizeof(uint32_t))
72 /* Offset past header */
73 #define BUFFER_HDR_SIZE (sizeof(BUFFER_HDR_T))
74 /* Header + Magic Number */
75 #define BUFFER_PADDING_SIZE (sizeof(BUFFER_HDR_T) + sizeof(uint32_t))
76 /* pool size must allow for header */
77 #define MAX_USER_BUF_SIZE ((uint16_t)0xffff - BUFFER_PADDING_SIZE)
78 #define MAGIC_NO 0xDDBADDBA
79 
80 #define BUF_STATUS_FREE 0
81 #define BUF_STATUS_UNLINKED 1
82 #define BUF_STATUS_QUEUED 2
83 
84 /* Put all GKI variables into one control block
85 */
86 typedef struct {
87 /* Task management variables
88 */
89 /* The stack and stack size are not used on Windows
90 */
91 
92 #if (GKI_NUM_FIXED_BUF_POOLS > 0)
93   uint8_t bufpool0[(ALIGN_POOL(GKI_BUF0_SIZE) + BUFFER_PADDING_SIZE) *
94                    GKI_BUF0_MAX];
95 #endif
96 
97 #if (GKI_NUM_FIXED_BUF_POOLS > 1)
98   uint8_t bufpool1[(ALIGN_POOL(GKI_BUF1_SIZE) + BUFFER_PADDING_SIZE) *
99                    GKI_BUF1_MAX];
100 #endif
101 
102 #if (GKI_NUM_FIXED_BUF_POOLS > 2)
103   uint8_t bufpool2[(ALIGN_POOL(GKI_BUF2_SIZE) + BUFFER_PADDING_SIZE) *
104                    GKI_BUF2_MAX];
105 #endif
106 
107 #if (GKI_NUM_FIXED_BUF_POOLS > 3)
108   uint8_t bufpool3[(ALIGN_POOL(GKI_BUF3_SIZE) + BUFFER_PADDING_SIZE) *
109                    GKI_BUF3_MAX];
110 #endif
111 
112 #if (GKI_NUM_FIXED_BUF_POOLS > 4)
113   uint8_t bufpool4[(ALIGN_POOL(GKI_BUF4_SIZE) + BUFFER_PADDING_SIZE) *
114                    GKI_BUF4_MAX];
115 #endif
116 
117 #if (GKI_NUM_FIXED_BUF_POOLS > 5)
118   uint8_t bufpool5[(ALIGN_POOL(GKI_BUF5_SIZE) + BUFFER_PADDING_SIZE) *
119                    GKI_BUF5_MAX];
120 #endif
121 
122 #if (GKI_NUM_FIXED_BUF_POOLS > 6)
123   uint8_t bufpool6[(ALIGN_POOL(GKI_BUF6_SIZE) + BUFFER_PADDING_SIZE) *
124                    GKI_BUF6_MAX];
125 #endif
126 
127 #if (GKI_NUM_FIXED_BUF_POOLS > 7)
128   uint8_t bufpool7[(ALIGN_POOL(GKI_BUF7_SIZE) + BUFFER_PADDING_SIZE) *
129                    GKI_BUF7_MAX];
130 #endif
131 
132 #if (GKI_NUM_FIXED_BUF_POOLS > 8)
133   uint8_t bufpool8[(ALIGN_POOL(GKI_BUF8_SIZE) + BUFFER_PADDING_SIZE) *
134                    GKI_BUF8_MAX];
135 #endif
136 
137 #if (GKI_NUM_FIXED_BUF_POOLS > 9)
138   uint8_t bufpool9[(ALIGN_POOL(GKI_BUF9_SIZE) + BUFFER_PADDING_SIZE) *
139                    GKI_BUF9_MAX];
140 #endif
141 
142 #if (GKI_NUM_FIXED_BUF_POOLS > 10)
143   uint8_t bufpool10[(ALIGN_POOL(GKI_BUF10_SIZE) + BUFFER_PADDING_SIZE) *
144                     GKI_BUF10_MAX];
145 #endif
146 
147 #if (GKI_NUM_FIXED_BUF_POOLS > 11)
148   uint8_t bufpool11[(ALIGN_POOL(GKI_BUF11_SIZE) + BUFFER_PADDING_SIZE) *
149                     GKI_BUF11_MAX];
150 #endif
151 
152 #if (GKI_NUM_FIXED_BUF_POOLS > 12)
153   uint8_t bufpool12[(ALIGN_POOL(GKI_BUF12_SIZE) + BUFFER_PADDING_SIZE) *
154                     GKI_BUF12_MAX];
155 #endif
156 
157 #if (GKI_NUM_FIXED_BUF_POOLS > 13)
158   uint8_t bufpool13[(ALIGN_POOL(GKI_BUF13_SIZE) + BUFFER_PADDING_SIZE) *
159                     GKI_BUF13_MAX];
160 #endif
161 
162 #if (GKI_NUM_FIXED_BUF_POOLS > 14)
163   uint8_t bufpool14[(ALIGN_POOL(GKI_BUF14_SIZE) + BUFFER_PADDING_SIZE) *
164                     GKI_BUF14_MAX];
165 #endif
166 
167 #if (GKI_NUM_FIXED_BUF_POOLS > 15)
168   uint8_t bufpool15[(ALIGN_POOL(GKI_BUF15_SIZE) + BUFFER_PADDING_SIZE) *
169                     GKI_BUF15_MAX];
170 #endif
171 
172   uint8_t* OSStack[GKI_MAX_TASKS];     /* pointer to beginning of stack */
173   uint16_t OSStackSize[GKI_MAX_TASKS]; /* stack size available to each task */
174 
175   int8_t* OSTName[GKI_MAX_TASKS]; /* name of the task */
176 
177   uint8_t OSRdyTbl[GKI_MAX_TASKS]; /* current state of the task */
178   uint16_t OSWaitEvt
179       [GKI_MAX_TASKS]; /* events that have to be processed by the task */
180   uint16_t OSWaitForEvt[GKI_MAX_TASKS]; /* events the task is waiting for*/
181 
182   uint32_t OSTicks;   /* system ticks from start */
183   uint32_t OSIdleCnt; /* idle counter */
184   int16_t
185       OSDisableNesting; /* counter to keep track of interrupt disable nesting */
186   int16_t OSLockNesting; /* counter to keep track of sched lock nesting */
187   int16_t OSIntNesting;  /* counter to keep track of interrupt nesting */
188 
189   /* Timer related variables
190   */
191   int32_t OSTicksTilExp; /* Number of ticks till next timer expires */
192 #if (GKI_DELAY_STOP_SYS_TICK > 0)
193   uint32_t OSTicksTilStop; /* inactivity delay timer; OS Ticks till stopping
194                               system tick */
195 #endif
196   int32_t OSNumOrigTicks; /* Number of ticks between last timer expiration to
197                              the next one */
198 
199   int32_t OSWaitTmr
200       [GKI_MAX_TASKS]; /* ticks the task has to wait, for specific events */
201 
202 /* Only take up space timers used in the system (GKI_NUM_TIMERS defined in
203  * target.h) */
204 #if (GKI_NUM_TIMERS > 0)
205   int32_t OSTaskTmr0[GKI_MAX_TASKS];
206   int32_t OSTaskTmr0R[GKI_MAX_TASKS];
207 #endif
208 
209 #if (GKI_NUM_TIMERS > 1)
210   int32_t OSTaskTmr1[GKI_MAX_TASKS];
211   int32_t OSTaskTmr1R[GKI_MAX_TASKS];
212 #endif
213 
214 #if (GKI_NUM_TIMERS > 2)
215   int32_t OSTaskTmr2[GKI_MAX_TASKS];
216   int32_t OSTaskTmr2R[GKI_MAX_TASKS];
217 #endif
218 
219 #if (GKI_NUM_TIMERS > 3)
220   int32_t OSTaskTmr3[GKI_MAX_TASKS];
221   int32_t OSTaskTmr3R[GKI_MAX_TASKS];
222 #endif
223 
224   /* Buffer related variables
225   */
226   BUFFER_HDR_T* OSTaskQFirst[GKI_MAX_TASKS]
227                             [NUM_TASK_MBOX]; /* array of pointers to the first
228                                                 event in the task mailbox */
229   BUFFER_HDR_T* OSTaskQLast[GKI_MAX_TASKS]
230                            [NUM_TASK_MBOX]; /* array of pointers to the last
231                                                event in the task mailbox */
232 
233   /* Define the buffer pool management variables
234   */
235   FREE_QUEUE_T freeq[GKI_NUM_TOTAL_BUF_POOLS];
236 
237   uint16_t pool_buf_size[GKI_NUM_TOTAL_BUF_POOLS];
238   uint16_t pool_max_count[GKI_NUM_TOTAL_BUF_POOLS];
239   uint16_t pool_additions[GKI_NUM_TOTAL_BUF_POOLS];
240 
241   /* Define the buffer pool start addresses
242   */
243   uint8_t* pool_start[GKI_NUM_TOTAL_BUF_POOLS]; /* array of pointers to the
244                                                    start of each buffer pool */
245   uint8_t*
246       pool_end[GKI_NUM_TOTAL_BUF_POOLS]; /* array of pointers to the end of each
247                                             buffer pool */
248   uint16_t pool_size
249       [GKI_NUM_TOTAL_BUF_POOLS]; /* actual size of the buffers in a pool */
250 
251   /* Define the buffer pool access control variables */
252   void* p_user_mempool; /* User O/S memory pool */
253   uint16_t
254       pool_access_mask; /* Bits are set if the corresponding buffer pool is a
255                            restricted pool */
256   uint8_t pool_list[GKI_NUM_TOTAL_BUF_POOLS]; /* buffer pools arranged in the
257                                                  order of size */
258   uint8_t
259       curr_total_no_of_pools; /* number of fixed buf pools + current number of
260                                  dynamic pools */
261 
262   bool timer_nesting; /* flag to prevent timer interrupt nesting */
263 
264   /* Time queue arrays */
265   TIMER_LIST_Q* timer_queues[GKI_MAX_TIMER_QUEUES];
266   /* System tick callback */
267   SYSTEM_TICK_CBACK* p_tick_cb;
268   bool system_tick_running; /* TRUE if system tick is running. Valid only if
269                                p_tick_cb is not NULL */
270 
271 } tGKI_COM_CB;
272 
273 /* Internal GKI function prototypes
274 */
275 extern bool gki_chk_buf_damage(void*);
276 extern bool gki_chk_buf_owner(void*);
277 extern void gki_buffer_init(void);
278 extern void gki_timers_init(void);
279 extern void gki_adjust_timer_count(int32_t);
280 
281 #endif
282