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