Moving all common header file into common_def.h file
[o-du/l2.git] / src / 5gnrmac / mac_msg_hdl.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 /* header include files -- defines (.h)  */
20 #include "common_def.h"
21 #include "rgu.h"
22 #include "tfu.h"
23 #include "rg_sch_inf.h"
24 #include "rg_env.h"
25 #include "lrg.h"
26 #include "crg.h"
27 #include "rg.h"
28 #include "du_log.h"
29 #include "lwr_mac.h"
30
31 /* header/extern include files (.x) */
32 #include "rgu.x"
33 #include "tfu.x"
34 #include "rg_sch_inf.x"
35 #include "lrg.x"
36 #include "crg.x"
37 #include "rg_prg.x"
38 #include "du_app_mac_inf.h"
39 #include "mac_upr_inf_api.h"
40 #include "rg.x"
41 #include "lwr_mac_fsm.h"
42 #include "mac.h"
43
44 /* This file contains message handling functionality for MAC */
45
46 extern MacCb  macCb;
47
48 extern void sendToLowerMac(uint16_t msgType, uint32_t msgLen, void *msg);
49 uint16_t buildMacPdu(RlcMacData *dlData);
50
51 /* Function pointer for sending crc ind from MAC to SCH */
52 MacSchCrcIndFunc macSchCrcIndOpts[]=
53 {
54    packMacSchCrcInd,
55    macSchCrcInd,
56    packMacSchCrcInd
57 };
58
59 /* Function pointer for sending DL RLC BO Info from MAC to SCH */
60 MacSchDlRlcBoInfoFunc macSchDlRlcBoInfoOpts[]=
61 {
62    packMacSchDlRlcBoInfo,
63    macSchDlRlcBoInfo,
64    packMacSchDlRlcBoInfo
65 };
66
67 /*******************************************************************
68  *
69  * @brief Sends DL BO Info to SCH
70  *
71  * @details
72  *
73  *    Function : sendDlRlcBoInfoMacToSch
74  *
75  *    Functionality:
76  *       Sends DL BO Info to SCH
77  *
78  * @params[in] 
79  * @return ROK     - success
80  *         RFAILED - failure
81  *
82  ****************************************************************/
83 int sendDlRlcBoInfoMacToSch(DlRlcBOInfo *dlBoInfo)
84 {
85    Pst pst;
86  
87    fillMacToSchPst(&pst);
88    pst.event = EVENT_DL_RLC_BO_INFO_TO_SCH;
89  
90    return(*macSchDlRlcBoInfoOpts[pst.selector])(&pst, dlBoInfo);
91 }
92
93 /*******************************************************************
94  *
95  * @brief Sends CRC Indication to SCH
96  *
97  * @details
98  *
99  *    Function : sendCrcIndMacToSch 
100  *
101  *    Functionality:
102  *       Sends CRC Indication to SCH
103  *
104  * @params[in] 
105  * @return ROK     - success
106  *         RFAILED - failure
107  *
108   ****************************************************************/
109 int sendCrcIndMacToSch(CrcIndInfo *crcInd)
110 {
111    Pst pst;
112  
113    fillMacToSchPst(&pst);
114    pst.event = EVENT_CRC_IND_TO_SCH;
115  
116    return(*macSchCrcIndOpts[pst.selector])(&pst, crcInd);
117 }
118  
119 /*******************************************************************
120  *
121  * @brief Processes CRC Indication from PHY
122  *
123  * @details
124  *
125  *    Function : fapiMacCrcInd
126  *
127  *    Functionality:
128  *       Processes CRC Indication from PHY
129  *
130  * @params[in] Post Structure Pointer
131  *             Crc Indication Pointer
132  * @return ROK     - success
133  *         RFAILED - failure
134  *
135  * ****************************************************************/
136 uint16_t fapiMacCrcInd(Pst *pst, CrcInd *crcInd)
137 {
138    CrcIndInfo   crcIndInfo;
139  
140    DU_LOG("\nMAC : Received CRC indication");
141
142    /* Considering one pdu and one preamble */ 
143    crcIndInfo.cellId = macCb.macCell->cellId;;
144    crcIndInfo.crnti = crcInd->crcInfo[0].rnti;
145    crcIndInfo.timingInfo.sfn = crcInd->timingInfo.sfn;
146    crcIndInfo.timingInfo.slot = crcInd->timingInfo.slot;
147    crcIndInfo.numCrcInd = crcInd->crcInfo[0].numCb;
148    crcIndInfo.crcInd[0] = crcInd->crcInfo[0].cbCrcStatus[0];
149  
150    return(sendCrcIndMacToSch(&crcIndInfo));
151 }
152  
153 /*******************************************************************
154  *
155  * @brief Process Rx Data Ind at MAC
156  *
157  * @details
158  *
159  *    Function : fapiMacRxDataInd
160  *
161  *    Functionality:
162  *       Process Rx Data Ind at MAC
163  *
164  * @params[in] Post structure
165  *             Rx Data Indication
166  * @return ROK     - success
167  *         RFAILED - failure
168  *
169  * ****************************************************************/
170 uint16_t fapiMacRxDataInd(Pst *pst, RxDataInd *rxDataInd)
171 {
172    uint16_t pduIdx;
173  
174    DU_LOG("\nMAC : Received Rx Data indication");
175   
176    /* TODO : compare the handle received in RxDataInd with handle send in PUSCH
177          * PDU, which is stored in raCb */
178
179    for(pduIdx = 0; pduIdx < rxDataInd->numPdus; pduIdx++)
180    {
181       unpackRxData(&rxDataInd->pdus[pduIdx]);
182    }
183    return ROK;
184 }
185
186 /*******************************************************************
187  *
188  * @brief Processes DL data from RLC
189  *
190  * @details
191  *
192  *    Function : MacRlcProcDlData 
193  *
194  *    Functionality:
195  *      Processes DL data from RLC
196  *
197  * @params[in] Post structure
198  *             DL data
199  * @return ROK     - success
200  *         RFAILED - failure
201  *
202  * ****************************************************************/
203 uint16_t MacRlcProcDlData(Pst* pst, SpId spId, RlcMacData *dlData)
204 {
205    return ROK;
206 }
207
208 /*******************************************************************
209  *
210  * @brief Processes BO status from RLC
211  *
212  * @details
213  *
214  *    Function : MacRlcProcBOStatus
215  *
216  *    Functionality:
217  *      Processes BO status from RLC
218  *
219  * @params[in] Post structure
220  *             BO status
221  * @return ROK     - success
222  *         RFAILED - failure
223  *
224  * ****************************************************************/
225 uint16_t MacRlcProcBOStatus(Pst* pst, SpId spId, RlcMacBOStatus*      boStatus)
226 {
227    return ROK;
228 }
229
230
231 /*******************************************************************
232  *
233  * @brief Handles cell start reuqest from DU APP
234  *
235  * @details
236  *
237  *    Function : MacHdlCellStartReq
238  *
239  *    Functionality:
240  *      Handles cell start reuqest from DU APP
241  *
242  * @params[in] Post structure pointer
243  *             Cell Start Request info pointer 
244  * @return ROK     - success
245  *         RFAILED - failure
246  *
247  * ****************************************************************/
248 uint16_t MacHdlCellStartReq(Pst *pst, MacCellStartInfo  *cellStartInfo)
249 {
250    DU_LOG("\nMAC : Handling cell start request");
251    sendToLowerMac(START_REQUEST, 0, cellStartInfo);
252
253    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellStartInfo, \
254            sizeof(MacCellStartInfo));
255
256    return ROK;
257 }
258
259 /*******************************************************************
260  *
261  * @brief Handles cell stop request from DU APP
262  *
263  * @details
264  *
265  *    Function : MacHdlCellStartReq
266  *
267  *    Functionality:
268  *        Handles cell stop request from DU APP
269  *
270  * @params[in] Post structure pointer
271  *             Mac Cell stop information
272  * @return ROK     - success
273  *         RFAILED - failure
274  *
275  * ****************************************************************/
276 uint16_t MacHdlCellStopReq(Pst *pst, MacCellStopInfo  *cellStopInfo)
277 {
278 #ifdef INTEL_FAPI
279    DU_LOG("\nMAC : Sending cell stop request to Lower Mac");
280    sendToLowerMac(FAPI_STOP_REQUEST, 0, cellStopInfo);
281 #endif
282
283    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellStopInfo, \
284            sizeof(MacCellStopInfo));
285  
286    return ROK;
287 }
288
289 /*******************************************************************
290  *
291  * @brief Handles DL CCCH Ind from DU APP
292  *
293  * @details
294  *
295  *    Function : MacHdlDlCcchInd 
296  *
297  *    Functionality:
298  *      Handles DL CCCH Ind from DU APP
299  *
300  * @params[in] Post structure pointer
301  *             DL CCCH Ind pointer 
302  * @return ROK     - success
303  *         RFAILED - failure
304  *
305  * ****************************************************************/
306 uint16_t MacHdlDlCcchInd(Pst *pst, DlCcchIndInfo *dlCcchIndInfo)
307 {
308    uint16_t idx;
309    DlRlcBOInfo  dlBoInfo;
310         memset(&dlBoInfo, 0, sizeof(DlRlcBOInfo));
311
312    DU_LOG("\nMAC : Handling DL CCCH IND");
313    
314    /* TODO : Fill DL RLC Buffer status info */
315    dlBoInfo.cellId = dlCcchIndInfo->cellId;
316    dlBoInfo.crnti = dlCcchIndInfo->crnti;
317    dlBoInfo.numLc = 0;
318    
319    if(dlCcchIndInfo->msgType == RRC_SETUP)
320    {
321       dlBoInfo.boInfo[dlBoInfo.numLc].lcId = SRB_ID_0;    // SRB ID 0 for msg4
322       dlBoInfo.boInfo[SRB_ID_0].dataVolume = \
323         dlCcchIndInfo->dlCcchMsgLen;
324       dlBoInfo.numLc++;
325
326       /* storing Msg4 Pdu in raCb */
327       if(macCb.macCell->macRaCb[0].crnti == dlCcchIndInfo->crnti)
328       {
329          macCb.macCell->macRaCb[0].msg4PduLen = dlCcchIndInfo->dlCcchMsgLen;
330          MAC_ALLOC(macCb.macCell->macRaCb[0].msg4Pdu, macCb.macCell->macRaCb[0]\
331          .msg4PduLen);
332          if(macCb.macCell->macRaCb[0].msg4Pdu)
333          {
334                            for(idx = 0; idx < dlCcchIndInfo->dlCcchMsgLen; idx++)
335                                 {
336                macCb.macCell->macRaCb[0].msg4Pdu[idx] =\
337                                           dlCcchIndInfo->dlCcchMsg[idx];
338                            }
339          }
340       }
341    }
342    sendDlRlcBoInfoMacToSch(&dlBoInfo);
343    
344    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo->dlCcchMsg, \
345       dlCcchIndInfo->dlCcchMsgLen);
346    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo, sizeof(DlCcchIndInfo));
347    return ROK;
348
349 }
350
351 /*******************************************************************
352  *
353  * @brief Sends UL CCCH Ind to DU APP
354  *
355  * @details
356  *
357  *    Function : macSendUlCcchInd
358  *
359  *    Functionality:
360  *        MAC sends UL CCCH Ind to DU APP
361  *
362  * @params[in] Post structure pointer
363  *            
364  * @return ROK     - success
365  *         RFAILED - failure
366  *
367  * ****************************************************************/
368 uint16_t macSendUlCcchInd(uint8_t *rrcContainer, uint16_t cellId, uint16_t crnti)
369 {
370    Pst pst;
371         uint8_t ret = ROK;
372    UlCcchIndInfo *ulCcchIndInfo = NULLP;
373
374    MAC_ALLOC_SHRABL_BUF(ulCcchIndInfo, sizeof(UlCcchIndInfo));
375    if(!ulCcchIndInfo)
376    {
377                 DU_LOG("\nMAC: Memory failed in macSendUlCcchInd");
378                 return RFAILED;
379         }
380
381         ulCcchIndInfo->cellId = cellId;
382         ulCcchIndInfo->crnti  = crnti;
383         ulCcchIndInfo->ulCcchMsg = rrcContainer;
384
385         /* Fill Pst */
386         pst.selector  = ODU_SELECTOR_LWLC;
387         pst.srcEnt    = ENTRG;
388         pst.dstEnt    = ENTDUAPP;
389         pst.dstInst   = 0;
390         pst.srcInst   = macCb.macInst;
391         pst.dstProcId = rgCb[pst.srcInst].rgInit.procId;
392         pst.srcProcId = rgCb[pst.srcInst].rgInit.procId;
393         pst.region    = MAC_MEM_REGION;
394         pst.pool      = MAC_POOL;
395         pst.event     = EVENT_MAC_UL_CCCH_IND;
396         pst.route     = 0;
397         pst.prior     = 0;
398         pst.intfVer   = 0;
399
400         if(MacDuAppUlCcchInd(&pst, ulCcchIndInfo) != ROK)
401         {
402                 DU_LOG("\nMAC: Failed to send UL CCCH Ind to DU APP");
403                 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg,
404                                 strlen((const char*)ulCcchIndInfo->ulCcchMsg));
405                 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
406                 ret = RFAILED;
407         }
408         return ret;
409 }
410
411 /*******************************************************************
412  *
413  * @brief Handles Ue Create Request from DU APP
414  *
415  * @details
416  *
417  *    Function : MacHdlUeCreateReq
418  *
419  *    Functionality:
420  *      Handles Ue create Request from DU APP
421  *
422  * @params[in] Post structure pointer
423  *             MacUeCfg pointer 
424  * @return ROK     - success
425  *         RFAILED - failure
426  *
427  * ****************************************************************/
428 uint8_t MacHdlUeCreateReq(Pst *pst, MacUeCfg *ueCfg)
429 {
430    /* TODO : Create MAC UE Context.
431                   Copy Tx Pdu from raCb
432                                  Delete RaCb
433                                  Send MacUeContext to SCH */
434    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, ueCfg, sizeof(MacUeCfg));
435    return ROK;
436 }
437
438 /**********************************************************************
439          End of file
440 **********************************************************************/
441