Moving all common header file into common_def.h file
[o-du/l2.git] / src / 5gnrmac / lwr_mac_phy.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
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
19 /* This file contains APIs to send/receive messages from PHY */
20
21 #include "common_def.h"
22 #include "rg.h"
23 #include "lwr_mac_phy.h"
24 #include "lwr_mac_fsm.h"
25
26 #ifdef INTEL_WLS
27 #include "wls_lib.h"
28 #endif
29
30 EXTERN S16 rgClHndlCfgReq ARGS((void *msg));
31 EXTERN void l1ProcessFapiRequest ARGS((uint8_t msgType, uint32_t msgLen, void *msg));
32
33 #ifdef INTEL_WLS
34
35 /*******************************************************************
36  *
37  * @brief Enqueues memory blocks for use by L1
38  *
39  * @details
40  *
41  *    Function : LwrMacEnqueueWlsBlock
42  *
43  *    Functionality:
44  *      Enqueues memory blocks for use by L1
45  *
46  * @params[in] 
47  * @return void
48  *
49  * ****************************************************************/
50 void LwrMacEnqueueWlsBlock()
51 {
52    void *memPtr;
53    void *wlsHdlr;
54
55    WLS_MEM_ALLOC(memPtr, LWR_MAC_WLS_BUF_SIZE);
56  
57    if(memPtr) 
58    {
59       wlsHdlr = mtGetWlsHdl();
60       
61       /* allocate blocks for UL transmittion */
62       while(WLS_EnqueueBlock(wlsHdlr, WLS_VA2PA(wlsHdlr, memPtr)))
63       {
64          WLS_MEM_ALLOC(memPtr, LWR_MAC_WLS_BUF_SIZE);
65          if(!memPtr)
66            break;
67       }
68  
69       // free not enqueued block
70       if(memPtr)
71       {
72          WLS_MEM_FREE(memPtr, LWR_MAC_WLS_BUF_SIZE);
73       }
74    }
75 }/* LwrMacEnqueueWlsBlock */
76
77 /*******************************************************************
78  *
79  * @brief Enqueue N number of blocks
80  *
81  * @details
82  *
83  *    Function : enqueueNBlocks
84  *
85  *    Functionality:
86  *      Enqueue N number of memory blocks
87  *
88  * @params[in] Number of blocks
89  * @return ROK     - success
90  *         RFAILED - failure
91  *
92  * ****************************************************************/
93 uint16_t enqueueNBlocks(uint32_t numBlocks)
94 {
95    void    *memPtr;
96    void    *wlsHdlr;       /* WLS handler */
97  
98    wlsHdlr = mtGetWlsHdl();   
99    while(numBlocks)
100    {
101       numBlocks--;
102
103       memPtr = (void *)NULL;
104       WLS_MEM_ALLOC(memPtr, LWR_MAC_WLS_BUF_SIZE);
105       if(memPtr)
106       {
107          WLS_EnqueueBlock(wlsHdlr, WLS_VA2PA(wlsHdlr, memPtr));
108       }
109    }
110    RETVALUE(ROK);
111 }/* enqueueNBlocks */
112
113 /*******************************************************************
114  *
115  * @brief Add memory block (to be freed later) to list
116  *
117  * @details
118  *
119  *    Function : addWlsBlockToFree 
120  *
121  *    Functionality:
122  *       Add memory block (to be freed later) to list
123  *
124  * @params[in] 
125  * @return ROK     - success
126  *         RFAILED - failure
127  *
128  * ****************************************************************/
129 void addWlsBlockToFree(void *msg, uint32_t msgLen, uint8_t idx)
130 {
131    CmLList         *node;
132    WlsBlockToFree  *block;
133    MAC_ALLOC(block, sizeof(WlsBlockToFree));
134    if(block)
135    {
136       MAC_ALLOC(node, sizeof(CmLList));
137       if(node)
138       {
139          block->ptr = msg;
140          block->size = msgLen;
141
142          node->node = (PTR)block;
143          cmLListAdd2Tail(&wlsBlockToFreeList[idx], node);
144       }
145    }
146 }/* addWlsBlockToFree */
147
148 void freeWlsBlockList(uint8_t idx)
149 {
150    CmLList         *node;
151    WlsBlockToFree  *block;
152    if(wlsBlockToFreeList[idx].count)
153    {
154       CM_LLIST_FIRST_NODE(&wlsBlockToFreeList[idx], node);
155       while(node)
156       {
157          block = (WlsBlockToFree *)node->node;
158          cmLListDelFrm(&wlsBlockToFreeList[idx], node);
159          WLS_MEM_FREE(block->ptr, block->size);
160          MAC_FREE(block, sizeof(WlsBlockToFree));
161          MAC_FREE(node, sizeof(CmLList));
162          node = NULL;
163          CM_LLIST_FIRST_NODE(&wlsBlockToFreeList[idx], node);
164       }
165    }
166 }
167
168 /*******************************************************************
169  *
170  * @brief Receives msg from L1 
171  *
172  * @details
173  *
174  *    Function :LwrMacRecvPhyMsg 
175  *
176  *    Functionality:
177  *      Receives L1 Msg and enqueues memort for UL msg
178  *
179  * @params[in] 
180  * @return 
181  * ****************************************************************/
182 void LwrMacRecvPhyMsg()
183 {
184    uint32_t numL1Msg;   /* Number of L1 messaes received */
185    uint32_t numToGet;   /* Number of Memory blocks to get */
186    void     *wlsHdlr;       /* WLS handler */
187    uint64_t l1Msg;         /* Message received */
188    void     *l1MsgPtr;
189    uint32_t msgSize;
190    uint16_t msgType;
191    uint16_t flag;
192
193    wlsHdlr = mtGetWlsHdl();
194    if(WLS_Ready(wlsHdlr))
195    {
196       numToGet = WLS_Wait(wlsHdlr);
197       
198       numL1Msg = numToGet;
199
200       while(numToGet)
201       {
202          l1Msg = (uint64_t) NULL;
203          l1Msg = WLS_Get(wlsHdlr, &msgSize, &msgType, &flag);
204          if(l1Msg)
205          {
206             l1MsgPtr = WLS_PA2VA(wlsHdlr, l1Msg); 
207             handlePhyMessages(msgType, msgSize, l1MsgPtr);
208          }
209          numToGet--;
210       }
211       
212       if(numL1Msg)
213       {
214          enqueueNBlocks(numL1Msg);
215       }
216
217    }
218 } /* LwrMacRecvPhyMsg */
219
220 #endif /* INTEL_WLS */
221
222 /*******************************************************************
223  * 
224  *  @brief Sends message to PHY
225  * 
226  *  @details
227  * 
228  *    Function : LwrMacSendToPhy
229  *    Functionality:
230  *         -Sends message to PHY
231  * 
232  *  @params[in] Message Type
233  *              Message Length
234  *              Messaga Pointer
235  * 
236  *  @return void
237  * 
238  * *****************************************************************/
239
240 PUBLIC uint16_t LwrMacSendToPhy(uint8_t msgType, uint32_t msgLen, void *msg)
241 {
242 #ifdef INTEL_WLS
243    int ret;
244    unsigned long long pMsg;
245  
246    pMsg = WLS_VA2PA(mtGetWlsHdl(), msg);
247    ret = WLS_Put(mtGetWlsHdl(), pMsg, msgLen, msgType, 0);
248
249    if(ret != 0)
250    {
251       printf("\nFailure in sending message to PHY");
252            WLS_MEM_FREE(msg, msgLen);   
253       return RFAILED;
254    }
255    else
256    {
257       addWlsBlockToFree(msg, msgLen, (slotIndIdx-1));
258    }
259 #else
260    l1ProcessFapiRequest(msgType, msgLen, msg);
261 #endif
262    return ROK;
263 } /* LwrMacSendToPhy */
264
265 /**********************************************************************
266          End of file
267 **********************************************************************/