Changes for SR, BSR and MSG5 Handling
[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 /* Function pointer for sending short BSR from MAC to SCH */
61 MacSchBsrFunc macSchBsrOpts[]=
62 {
63    packMacSchBsr,
64    MacSchBsr,
65    packMacSchBsr
66 };
67
68 /* Function pointer for sending SR Uci ind from MAC to SCH */
69 MacSchSrUciIndFunc macSchSrUciIndOpts[]=
70 {
71    packMacSchSrUciInd,
72    MacSchSrUciInd,
73    packMacSchSrUciInd
74 };
75
76 /*******************************************************************
77  *
78  * @brief Sends DL BO Info to SCH
79  *
80  * @details
81  *
82  *    Function : sendDlRlcBoInfoMacToSch
83  *
84  *    Functionality:
85  *       Sends DL BO Info to SCH
86  *
87  * @params[in] 
88  * @return ROK     - success
89  *         RFAILED - failure
90  *
91  ****************************************************************/
92 uint8_t sendDlRlcBoInfoMacToSch(DlRlcBOInfo *dlBoInfo)
93 {
94    Pst pst;
95
96    FILL_PST_MAC_TO_SCH(pst, EVENT_DL_RLC_BO_INFO_TO_SCH);
97    return(*macSchDlRlcBoInfoOpts[pst.selector])(&pst, dlBoInfo);
98 }
99
100 /*******************************************************************
101  *
102  * @brief Sends CRC Indication to SCH
103  *
104  * @details
105  *
106  *    Function : sendCrcIndMacToSch 
107  *
108  *    Functionality:
109  *       Sends CRC Indication to SCH
110  *
111  * @params[in] 
112  * @return ROK     - success
113  *         RFAILED - failure
114  *
115  ****************************************************************/
116 uint8_t sendCrcIndMacToSch(CrcIndInfo *crcInd)
117 {
118    Pst pst;
119
120    FILL_PST_MAC_TO_SCH(pst, EVENT_CRC_IND_TO_SCH);
121    return(*macSchCrcIndOpts[pst.selector])(&pst, crcInd);
122 }
123
124 /*******************************************************************
125  *
126  * @brief Processes CRC Indication from PHY
127  *
128  * @details
129  *
130  *    Function : fapiMacCrcInd
131  *
132  *    Functionality:
133  *       Processes CRC Indication from PHY
134  *
135  * @params[in] Post Structure Pointer
136  *             Crc Indication Pointer
137  * @return ROK     - success
138  *         RFAILED - failure
139  *
140  * ****************************************************************/
141 uint8_t fapiMacCrcInd(Pst *pst, CrcInd *crcInd)
142 {
143    uint16_t     cellIdx;
144    CrcIndInfo   crcIndInfo;
145
146    DU_LOG("\nMAC : Received CRC indication");
147
148    GET_CELL_IDX(crcInd->cellId, cellIdx);
149    /* Considering one pdu and one preamble */ 
150    crcIndInfo.cellId = macCb.macCell[cellIdx]->cellId;;
151    crcIndInfo.crnti = crcInd->crcInfo[0].rnti;
152    crcIndInfo.timingInfo.sfn = crcInd->timingInfo.sfn;
153    crcIndInfo.timingInfo.slot = crcInd->timingInfo.slot;
154    crcIndInfo.numCrcInd = crcInd->crcInfo[0].numCb;
155    crcIndInfo.crcInd[0] = crcInd->crcInfo[0].cbCrcStatus[0];
156
157    return(sendCrcIndMacToSch(&crcIndInfo));
158 }
159
160 /*******************************************************************
161  *
162  * @brief Process Rx Data Ind at MAC
163  *
164  * @details
165  *
166  *    Function : fapiMacRxDataInd
167  *
168  *    Functionality:
169  *       Process Rx Data Ind at MAC
170  *
171  * @params[in] Post structure
172  *             Rx Data Indication
173  * @return ROK     - success
174  *         RFAILED - failure
175  *
176  * ****************************************************************/
177 uint8_t fapiMacRxDataInd(Pst *pst, RxDataInd *rxDataInd)
178 {
179    uint16_t pduIdx;
180
181    DU_LOG("\nMAC : Received Rx Data indication");
182
183    /* TODO : compare the handle received in RxDataInd with handle send in PUSCH
184     * PDU, which is stored in raCb */
185
186    for(pduIdx = 0; pduIdx < rxDataInd->numPdus; pduIdx++)
187    {
188       unpackRxData(rxDataInd->cellId, rxDataInd->timingInfo, &rxDataInd->pdus[pduIdx]);
189    }
190    return ROK;
191 }
192
193 /*******************************************************************
194  *
195  * @brief Processes DL data from RLC
196  *
197  * @details
198  *
199  *    Function : MacRlcProcDlData 
200  *
201  *    Functionality:
202  *      Processes DL data from RLC
203  *
204  * @params[in] Post structure
205  *             DL data
206  * @return ROK     - success
207  *         RFAILED - failure
208  *
209  * ****************************************************************/
210 uint16_t MacRlcProcDlData(Pst* pst, SpId spId, RlcMacData *dlData)
211 {
212    return ROK;
213 }
214
215 /*******************************************************************
216  *
217  * @brief Builds and Sends UL Data to RLC
218  *
219  * @details
220  *
221  *    Function : macProcUlData
222  *
223  *    Functionality: Builds and Sends UL Data to RLC
224  *
225  * @params[in] CellId
226  *             CRNTI
227  *             Slot information
228  *             LC Id on which payload was received
229  *             Pointer to the payload
230  *             Length of payload
231  * @return ROK     - success
232  *         RFAILED - failure
233  *
234  * ****************************************************************/
235 uint8_t macProcUlData(uint16_t cellId, uint16_t rnti, SlotIndInfo slotInfo, \
236 uint8_t lcId, uint16_t pduLen, uint8_t *pdu)
237 {
238    Pst         pst;
239    RlcMacData  *ulData;
240
241    /* Filling RLC Ul Data*/
242    MAC_ALLOC_SHRABL_BUF(ulData, sizeof(RlcMacData));
243    if(!ulData)
244    {
245       DU_LOG("\nMAC : Memory allocation failed while sending UL data to RLC");
246       return RFAILED;
247    }
248    memset(ulData, 0, sizeof(RlcMacData));
249    ulData->cellId = cellId; 
250    ulData->rnti = rnti;
251    memcpy(&ulData->slotInfo, &slotInfo, sizeof(SlotIndInfo));
252    ulData->slotInfo.cellId = cellId;
253
254    /* Filling pdu info */
255    if(lcId == SRB1_LCID || lcId == SRB2_LCID)
256    {
257       ulData->pduInfo[ulData->numPdu].commCh = true;
258    }
259    ulData->pduInfo[ulData->numPdu].lcId = lcId;
260    ulData->pduInfo[ulData->numPdu].pduBuf = pdu;
261    ulData->pduInfo[ulData->numPdu].pduLen = pduLen;
262    ulData->numPdu++;
263
264    /* Filling Post and send to RLC */
265    memset(&pst, 0, sizeof(Pst));
266    FILL_PST_MAC_TO_RLC(pst, 0, EVTRLCULDAT);
267    MacSendUlDataToRlc(&pst, ulData);
268
269    return ROK;
270 }
271
272
273 /*******************************************************************
274  *
275  * @brief Processes BO status from RLC
276  *
277  * @details
278  *
279  *    Function : MacRlcProcBOStatus
280  *
281  *    Functionality:
282  *      Processes BO status from RLC
283  *
284  * @params[in] Post structure
285  *             BO status
286  * @return ROK     - success
287  *         RFAILED - failure
288  *
289  * ****************************************************************/
290 uint16_t MacRlcProcBOStatus(Pst* pst, SpId spId, RlcMacBOStatus*      boStatus)
291 {
292    return ROK;
293 }
294
295
296 /*******************************************************************
297  *
298  * @brief Handles cell start reuqest from DU APP
299  *
300  * @details
301  *
302  *    Function : MacProcCellStartReq
303  *
304  *    Functionality:
305  *      Handles cell start reuqest from DU APP
306  *
307  * @params[in] Post structure pointer
308  *             Cell Start Request info pointer 
309  * @return ROK     - success
310  *         RFAILED - failure
311  *
312  * ****************************************************************/
313 uint8_t MacProcCellStartReq(Pst *pst, MacCellStartInfo  *cellStartInfo)
314 {
315    DU_LOG("\nMAC : Handling cell start request");
316    sendToLowerMac(START_REQUEST, 0, cellStartInfo);
317
318    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellStartInfo, \
319          sizeof(MacCellStartInfo));
320
321    return ROK;
322 }
323
324 /*******************************************************************
325  *
326  * @brief Handles cell stop request from DU APP
327  *
328  * @details
329  *
330  *    Function : MacProcCellStartReq
331  *
332  *    Functionality:
333  *        Handles cell stop request from DU APP
334  *
335  * @params[in] Post structure pointer
336  *             Mac Cell stop information
337  * @return ROK     - success
338  *         RFAILED - failure
339  *
340  * ****************************************************************/
341 uint8_t MacProcCellStopReq(Pst *pst, MacCellStopInfo  *cellStopInfo)
342 {
343 #ifdef INTEL_FAPI
344    DU_LOG("\nMAC : Sending cell stop request to Lower Mac");
345    sendToLowerMac(FAPI_STOP_REQUEST, 0, cellStopInfo);
346 #endif
347
348    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellStopInfo, \
349          sizeof(MacCellStopInfo));
350
351    return ROK;
352 }
353
354 /*******************************************************************
355  *
356  * @brief Handles DL CCCH Ind from DU APP
357  *
358  * @details
359  *
360  *    Function : MacProcDlCcchInd 
361  *
362  *    Functionality:
363  *      Handles DL CCCH Ind from DU APP
364  *
365  * @params[in] Post structure pointer
366  *             DL CCCH Ind pointer 
367  * @return ROK     - success
368  *         RFAILED - failure
369  *
370  * ****************************************************************/
371 uint8_t MacProcDlCcchInd(Pst *pst, DlCcchIndInfo *dlCcchIndInfo)
372 {
373    uint16_t cellIdx;
374    uint16_t idx;
375    DlRlcBOInfo  dlBoInfo;
376    memset(&dlBoInfo, 0, sizeof(DlRlcBOInfo));
377
378    DU_LOG("\nMAC : Handling DL CCCH IND");
379
380    GET_CELL_IDX(dlCcchIndInfo->cellId, cellIdx);
381
382    dlBoInfo.cellId = dlCcchIndInfo->cellId;
383    dlBoInfo.crnti = dlCcchIndInfo->crnti;
384    dlBoInfo.numLc = 0;
385
386    if(dlCcchIndInfo->msgType == RRC_SETUP)
387    {
388       dlBoInfo.boInfo[dlBoInfo.numLc].lcId = SRB_ID_0;    // SRB ID 0 for msg4
389       dlBoInfo.boInfo[SRB_ID_0].dataVolume = \
390          dlCcchIndInfo->dlCcchMsgLen;
391       dlBoInfo.numLc++;
392
393       /* storing Msg4 Pdu in raCb */
394       if(macCb.macCell[cellIdx]->macRaCb[0].crnti == dlCcchIndInfo->crnti)
395       {
396          macCb.macCell[cellIdx]->macRaCb[0].msg4PduLen = dlCcchIndInfo->dlCcchMsgLen;
397          MAC_ALLOC(macCb.macCell[cellIdx]->macRaCb[0].msg4Pdu, \
398             macCb.macCell[cellIdx]->macRaCb[0].msg4PduLen);
399          if(macCb.macCell[cellIdx]->macRaCb[0].msg4Pdu)
400          {
401             for(idx = 0; idx < dlCcchIndInfo->dlCcchMsgLen; idx++)
402             {
403                macCb.macCell[cellIdx]->macRaCb[0].msg4Pdu[idx] =\
404                   dlCcchIndInfo->dlCcchMsg[idx];
405             }
406          }
407       }
408    }
409    sendDlRlcBoInfoMacToSch(&dlBoInfo);
410
411    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo->dlCcchMsg, \
412          dlCcchIndInfo->dlCcchMsgLen);
413    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo, sizeof(DlCcchIndInfo));
414    return ROK;
415
416 }
417
418 /*******************************************************************
419  *
420  * @brief Sends UL CCCH Ind to DU APP
421  *
422  * @details
423  *
424  *    Function : macProcUlCcchInd
425  *
426  *    Functionality:
427  *        MAC sends UL CCCH Ind to DU APP
428  *
429  * @params[in] Post structure pointer
430  *            
431  * @return ROK     - success
432  *         RFAILED - failure
433  *
434  * ****************************************************************/
435 uint8_t macProcUlCcchInd(uint16_t cellId, uint16_t crnti, uint16_t rrcContSize, uint8_t *rrcContainer)
436 {
437    Pst pst;
438    uint8_t ret = ROK;
439    UlCcchIndInfo *ulCcchIndInfo = NULLP;
440
441    MAC_ALLOC_SHRABL_BUF(ulCcchIndInfo, sizeof(UlCcchIndInfo));
442    if(!ulCcchIndInfo)
443    {
444       DU_LOG("\nMAC: Memory failed in macProcUlCcchInd");
445       return RFAILED;
446    }
447
448    ulCcchIndInfo->cellId = cellId;
449    ulCcchIndInfo->crnti  = crnti;
450    ulCcchIndInfo->ulCcchMsgLen = rrcContSize;
451    ulCcchIndInfo->ulCcchMsg = rrcContainer;
452
453    /* Fill Pst */
454    FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_UL_CCCH_IND);
455
456    if(MacDuAppUlCcchInd(&pst, ulCcchIndInfo) != ROK)
457    {
458       DU_LOG("\nMAC: Failed to send UL CCCH Ind to DU APP");
459       MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
460       MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
461       ret = RFAILED;
462    }
463    return ret;
464 }
465
466 /*******************************************************************
467  *
468  * @brief Processes received short BSR
469  *
470  * @details
471  *
472  *    Function : macProcShortBsr
473  *
474  *    Functionality:
475  *        MAC sends Short BSR to SCH
476  *
477  * @params[in] cell ID
478  *             crnti
479  *             lcg ID
480  *             buffer size
481  *
482  *
483  * ****************************************************************/
484 uint8_t macProcShortBsr(uint16_t cellId, uint16_t crnti, uint8_t lcgId, uint32_t bufferSize)
485 {
486    Pst                  pst;
487    UlBufferStatusRptInd bsrInd;
488
489    memset(&pst, 0, sizeof(Pst));
490    memset(&bsrInd, 0, sizeof(UlBufferStatusRptInd));
491
492    bsrInd.cellId                 = cellId;
493    bsrInd.crnti                  = crnti;
494    bsrInd.bsrType                = SHORT_BSR;
495    bsrInd.numLcg                 = 1; /* short bsr reports one lcg at a time */
496    bsrInd.dataVolInfo[0].lcgId   = lcgId;
497    bsrInd.dataVolInfo[0].dataVol = bufferSize;
498
499    FILL_PST_MAC_TO_SCH(pst, EVENT_SHORT_BSR);
500    return(*macSchBsrOpts[pst.selector])(&pst, &bsrInd);
501 }
502
503 /*******************************************************************
504  *
505  * @brief Builds and send SR UCI Indication to SCH
506  *
507  * @details
508  *
509  *    Function : buildAndSendSrInd
510  *
511  *    Functionality:
512  *       Builds and send SR UCI Indication to SCH
513  *
514  * @params[in] SrUciIndInfo Pointer
515  *             crnti value
516  * @return ROK     - success
517  *         RFAILED - failure
518  *
519  * ****************************************************************/
520 uint8_t buildAndSendSrInd(UciInd *macUciInd, uint8_t crnti)
521 {
522    uint16_t cellIdx;
523    Pst pst;
524    SrUciIndInfo   srUciInd;
525    memset(&pst, 0, sizeof(Pst));
526    memset(&srUciInd, 0, sizeof(SrUciIndInfo));
527
528    GET_CELL_IDX(macUciInd->cellId, cellIdx);
529    srUciInd.cellId       = macCb.macCell[cellIdx]->cellId;;
530    srUciInd.crnti        = crnti;
531    srUciInd.slotInd.sfn  = macUciInd->slotInd.sfn;
532    srUciInd.slotInd.slot = macUciInd->slotInd.slot;
533    srUciInd.numSrBits++;
534    memset(srUciInd.srPayload, 0, MAX_SR_BITS_IN_BYTES);
535
536    /* Fill Pst */
537    FILL_PST_MAC_TO_SCH(pst, EVENT_UCI_IND_TO_SCH);
538
539    return(*macSchSrUciIndOpts[pst.selector])(&pst, &srUciInd);
540 }
541
542 /*******************************************************************
543  *
544  * @brief Processes UCI Indication from PHY
545  *
546  * @details
547  *
548  *    Function : fapiMacUciInd
549  *
550  *    Functionality:
551  *       Processes UCI Indication from PHY
552  *
553  * @params[in] Post Structure Pointer
554  *             UCI Indication Pointer
555  * @return ROK     - success
556  *         RFAILED - failure
557  *
558  * ****************************************************************/
559 uint8_t FapiMacUciInd(Pst *pst, UciInd *macUciInd)
560 {
561    uint8_t     pduIdx = 0;
562    uint8_t     ret = ROK;
563    uint16_t    nPdus;
564    uint16_t    crnti;
565
566    if(macUciInd)
567    {
568       nPdus = macUciInd->numUcis;
569       while(nPdus)
570       {
571          switch(macUciInd->pdus[pduIdx].pduType)
572          {
573             case UCI_IND_PUSCH:
574                break;
575             case UCI_IND_PUCCH_F0F1:
576                if(macUciInd->pdus[pduIdx].uci.uciPucchF0F1.srInfo.srIndPres)
577                {
578                   DU_LOG("\nMAC : Received SR UCI indication");
579                   crnti = macUciInd->pdus[pduIdx].uci.uciPucchF0F1.crnti; 
580                   ret = buildAndSendSrInd(macUciInd, crnti);
581                }
582                break;
583             case UCI_IND_PUCCH_F2F3F4:
584                break;
585             default:
586                DU_LOG("\nMAC: Invalid Pdu Type %d at FapiMacUciInd", macUciInd->pdus[pduIdx].pduType);
587                ret = RFAILED;
588                break;
589          }
590          pduIdx++;
591          nPdus--;
592       }
593    }
594    else
595    {
596       DU_LOG("\nMAC: Received Uci Ind is NULL at FapiMacUciInd()");
597       ret = RFAILED;
598    }
599    return ret;
600 }
601
602
603 /**********************************************************************
604   End of file
605  **********************************************************************/
606