Initial cut of the open ath9k htc firmware.
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / target / buf_pool / buf_pool_static.c
1 /*
2  * @File: 
3  * 
4  * @Abstract: Buf pool implementation: static version
5  * 
6  * @Notes: 
7  * 
8  * 
9  * Copyright (c) 2007 Atheros Communications Inc.
10  * All rights reserved.
11  *
12  */
13
14 #include <osapi.h>
15 #include <Magpie_api.h> 
16 #include <cmnos_api.h>
17 #include <buf_pool_api.h>
18 #include <vbuf_api.h>
19 #include <vdesc_api.h>
20 #include <adf_os_mem.h> 
21
22 #include "buf_pool_static.h"
23
24 LOCAL htc_handle_t _buf_pool_static_init(adf_net_handle_t handle);
25 LOCAL void _buf_pool_static_create_pool(pool_handle_t handle, BUF_POOL_ID poolId, int nItems, int nSize);
26 LOCAL adf_nbuf_t  _buf_pool_static_alloc_buf(pool_handle_t handle, BUF_POOL_ID poolId, int reserve);
27 LOCAL adf_nbuf_t  _buf_pool_static_alloc_buf_align(pool_handle_t handle, BUF_POOL_ID poolId, int reserve, int align);
28 LOCAL void _buf_pool_static_free_buf(pool_handle_t handle, BUF_POOL_ID poolId, adf_nbuf_t buf);
29 LOCAL void _buf_pool_static_shutdown(pool_handle_t handle);      
30
31 BUF_POOL_STATIC_CONTEXT g_poolCtx;
32
33 void buf_pool_module_install(struct buf_pool_api *pAPIs)
34 {   
35     pAPIs->_init = _buf_pool_static_init;
36     pAPIs->_create_pool = _buf_pool_static_create_pool;
37     pAPIs->_alloc_buf = _buf_pool_static_alloc_buf;
38     pAPIs->_alloc_buf_align = _buf_pool_static_alloc_buf_align;
39     pAPIs->_free_buf = _buf_pool_static_free_buf;
40     pAPIs->_shutdown = _buf_pool_static_shutdown;
41 }
42  
43 LOCAL pool_handle_t _buf_pool_static_init(adf_os_handle_t handle)
44 {
45 #if 1
46     int i;
47     
48     for(i=0; i < POOL_ID_MAX; i++) {
49         g_poolCtx.bufQ[i] = NULL;
50     }
51     
52     return &g_poolCtx;
53 #else    
54     BUF_POOL_STATIC_CONTEXT *ctx;
55     
56     //ctx = (BUF_POOL_static_CONTEXT *)A_ALLOCRAM(sizeof(BUF_POOL_static_CONTEXT));
57     ctx = (BUF_POOL_STATIC_CONTEXT *)adf_os_mem_alloc(sizeof(BUF_POOL_STATIC_CONTEXT));
58     ctx->NetHandle = handle;
59     
60     return ctx; 
61 #endif    
62 }      
63     
64 LOCAL void _buf_pool_static_shutdown(pool_handle_t handle) 
65 {
66     // SHALL NOT BE USED in FW
67 }
68
69 LOCAL void _buf_pool_static_create_pool(pool_handle_t handle, BUF_POOL_ID poolId, int nItems, int nSize)
70 {
71     int i;
72     VBUF *buf;
73     VDESC *desc;
74     
75     //BUF_POOL_STATIC_CONTEXT *ctx = (BUF_POOL_STATIC_CONTEXT *)handle;
76     
77     for ( i = 0; i < nItems; i++) {
78         buf = VBUF_alloc_vbuf();
79         desc = VDESC_alloc_vdesc();
80
81         desc->buf_addr = (A_UINT8 *)adf_os_mem_alloc(nSize);
82         desc->buf_size = nSize;
83         desc->data_offset = 0;
84         desc->data_size = 0;
85         
86         buf->buf_length = 0;        
87         buf->desc_list = desc;
88         
89         if ( g_poolCtx.bufQ[poolId] == NULL ) {
90             g_poolCtx.bufQ[poolId] = buf;
91         } else {
92             buf->next_buf = g_poolCtx.bufQ[poolId];
93             g_poolCtx.bufQ[poolId] = buf;
94         }
95     }
96 }
97             
98 LOCAL adf_nbuf_t  _buf_pool_static_alloc_buf(pool_handle_t handle, BUF_POOL_ID poolId, int reserve)
99 {
100     VBUF *buf;
101     
102     buf = g_poolCtx.bufQ[poolId];
103     if ( buf != NULL ) {
104         g_poolCtx.bufQ[poolId] = buf->next_buf;
105         
106         buf->next_buf = NULL;
107         buf->desc_list->data_offset = reserve;
108         buf->desc_list->data_size = 0;
109         buf->buf_length = 0;
110     }
111     
112     return buf;
113 }
114
115 LOCAL adf_nbuf_t  _buf_pool_static_alloc_buf_align(pool_handle_t handle, BUF_POOL_ID poolId, int reserve, int align)
116 {
117     return _buf_pool_static_alloc_buf(handle, poolId, reserve);
118 }
119     
120 LOCAL void _buf_pool_static_free_buf(pool_handle_t handle, BUF_POOL_ID poolId, adf_nbuf_t buf)
121 {
122     if ( g_poolCtx.bufQ[poolId] == NULL ) {
123         g_poolCtx.bufQ[poolId] = buf;
124     } else {
125         buf->next_buf = g_poolCtx.bufQ[poolId];
126         g_poolCtx.bufQ[poolId] = buf;
127     }
128 }