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