1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2017-2019] [Radisys] #
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 #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 *******************************************************************************/
19 /* header include files -- defines (.h) */
20 #include "common_def.h"
23 #include "du_app_mac_inf.h"
24 #include "mac_sch_interface.h"
25 #include "rlc_mac_inf.h"
26 #include "mac_upr_inf_api.h"
29 #include "fapi_interface.h"
31 #include "lwr_mac_fsm.h"
32 #include "lwr_mac_upr_inf.h"
34 #include "mac_utils.h"
36 /* This file contains message handling functionality for MAC */
40 /* Function pointer for sending crc ind from MAC to SCH */
41 MacSchCrcIndFunc macSchCrcIndOpts[]=
48 /* Function pointer for sending DL RLC BO Info from MAC to SCH */
49 MacSchDlRlcBoInfoFunc macSchDlRlcBoInfoOpts[]=
51 packMacSchDlRlcBoInfo,
56 /* Function pointer for sending short BSR from MAC to SCH */
57 MacSchBsrFunc macSchBsrOpts[]=
64 /* Function pointer for sending SR Uci ind from MAC to SCH */
65 MacSchSrUciIndFunc macSchSrUciIndOpts[]=
72 /*******************************************************************
74 * @brief Sends DL BO Info to SCH
78 * Function : sendDlRlcBoInfoToSch
81 * Sends DL BO Info to SCH
84 * @return ROK - success
87 ****************************************************************/
88 uint8_t sendDlRlcBoInfoToSch(DlRlcBoInfo *dlBoInfo)
92 FILL_PST_MAC_TO_SCH(pst, EVENT_DL_RLC_BO_INFO_TO_SCH);
93 return(*macSchDlRlcBoInfoOpts[pst.selector])(&pst, dlBoInfo);
96 /*******************************************************************
98 * @brief Sends CRC Indication to SCH
102 * Function : sendCrcIndMacToSch
105 * Sends CRC Indication to SCH
108 * @return ROK - success
111 ****************************************************************/
112 uint8_t sendCrcIndMacToSch(CrcIndInfo *crcInd)
116 FILL_PST_MAC_TO_SCH(pst, EVENT_CRC_IND_TO_SCH);
117 return(*macSchCrcIndOpts[pst.selector])(&pst, crcInd);
120 /*******************************************************************
122 * @brief Processes CRC Indication from PHY
126 * Function : fapiMacCrcInd
129 * Processes CRC Indication from PHY
131 * @params[in] Post Structure Pointer
132 * Crc Indication Pointer
133 * @return ROK - success
136 * ****************************************************************/
137 uint8_t fapiMacCrcInd(Pst *pst, CrcInd *crcInd)
140 CrcIndInfo crcIndInfo;
141 DU_LOG("\nDEBUG --> MAC : Received CRC indication");
142 GET_CELL_IDX(crcInd->cellId, cellIdx);
143 /* Considering one pdu and one preamble */
144 crcIndInfo.cellId = macCb.macCell[cellIdx]->cellId;
145 crcIndInfo.crnti = crcInd->crcInfo[0].rnti;
146 crcIndInfo.timingInfo.sfn = crcInd->timingInfo.sfn;
147 crcIndInfo.timingInfo.slot = crcInd->timingInfo.slot;
148 crcIndInfo.numCrcInd = crcInd->crcInfo[0].numCb;
149 crcIndInfo.crcInd[0] = crcInd->crcInfo[0].cbCrcStatus[0];
151 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, crcInd, sizeof(CrcInd));
152 return(sendCrcIndMacToSch(&crcIndInfo));
155 /*******************************************************************
157 * @brief Process Rx Data Ind at MAC
161 * Function : fapiMacRxDataInd
164 * Process Rx Data Ind at MAC
166 * @params[in] Post structure
168 * @return ROK - success
171 * ****************************************************************/
172 uint8_t fapiMacRxDataInd(Pst *pst, RxDataInd *rxDataInd)
175 DU_LOG("\nDEBUG --> MAC : Received Rx Data indication");
176 /* TODO : compare the handle received in RxDataInd with handle send in PUSCH
177 * PDU, which is stored in raCb */
179 for(pduIdx = 0; pduIdx < rxDataInd->numPdus; pduIdx++)
181 unpackRxData(rxDataInd->cellId, rxDataInd->timingInfo, &rxDataInd->pdus[pduIdx]);
182 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rxDataInd->pdus[pduIdx].pduData,\
183 rxDataInd->pdus[pduIdx].pduLength);
185 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rxDataInd, sizeof(RxDataInd));
189 /*******************************************************************
191 * @brief Processes DL data from RLC
195 * Function : MacProcRlcDlData
198 * Processes DL data from RLC
200 * @params[in] Post structure
202 * @return ROK - success
205 * ****************************************************************/
206 uint8_t MacProcRlcDlData(Pst* pstInfo, RlcData *dlData)
211 uint8_t *txPdu = NULLP;
212 uint8_t schInfoIdx = 0 ;
213 uint16_t cellIdx = 0, txPduLen = 0;
215 MacDlSlot *currDlSlot = NULLP;
216 DlRlcBoInfo dlBoInfo;
218 memset(&macDlData , 0, sizeof(MacDlData));
219 DU_LOG("\nDEBUG --> MAC: Received DL data for sfn=%d slot=%d numPdu= %d", \
220 dlData->slotInfo.sfn, dlData->slotInfo.slot, dlData->numPdu);
222 GET_UE_IDX(dlData->rnti, ueId);
224 /* Copy the pdus to be muxed into mac Dl data */
225 macDlData.ueId = ueId;
226 macDlData.numPdu = dlData->numPdu;
227 for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
229 macDlData.pduInfo[pduIdx].lcId = dlData->pduInfo[pduIdx].lcId;
230 macDlData.pduInfo[pduIdx].pduLen = dlData->pduInfo[pduIdx].pduLen;
231 macDlData.pduInfo[pduIdx].dlPdu = dlData->pduInfo[pduIdx].pduBuf;
234 GET_CELL_IDX(dlData->cellId, cellIdx);
235 /* Store DL data in the scheduled slot */
236 if(macCb.macCell[cellIdx] ==NULLP)
238 DU_LOG("\nERROR --> MAC : MacProcRlcDlData(): macCell does not exists");
241 currDlSlot = &macCb.macCell[cellIdx]->dlSlot[dlData->slotInfo.slot];
242 if(currDlSlot->dlInfo.dlMsgAlloc[ueId-1])
244 for(schInfoIdx=0; schInfoIdx<currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->numSchedInfo; schInfoIdx++)
246 if((currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgSchedInfo[schInfoIdx].pduPres == PDSCH_PDU) ||
247 (currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgSchedInfo[schInfoIdx].pduPres == BOTH))
251 txPduLen = currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgSchedInfo[schInfoIdx].dlMsgPdschCfg.codeword[0].tbSize\
252 - TX_PAYLOAD_HDR_LEN;
253 MAC_ALLOC(txPdu, txPduLen);
256 DU_LOG("\nERROR --> MAC : Memory allocation failed in MacProcRlcDlData");
259 macMuxPdu(&macDlData, NULLP, txPdu, txPduLen);
261 currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgSchedInfo[schInfoIdx].dlMsgInfo.dlMsgPduLen = txPduLen;
262 currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgSchedInfo[schInfoIdx].dlMsgInfo.dlMsgPdu = txPdu;
265 for(lcIdx = 0; lcIdx < dlData->numLc; lcIdx++)
267 if(dlData->boStatus[lcIdx].bo)
269 memset(&dlBoInfo, 0, sizeof(DlRlcBoInfo));
270 dlBoInfo.cellId = dlData->boStatus[lcIdx].cellId;
271 GET_CRNTI(dlBoInfo.crnti, dlData->boStatus[lcIdx].ueIdx);
272 dlBoInfo.lcId = dlData->boStatus[lcIdx].lcId;
273 dlBoInfo.dataVolume = dlData->boStatus[lcIdx].bo;
274 sendDlRlcBoInfoToSch(&dlBoInfo);
279 for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
281 MAC_FREE_SHRABL_BUF(pstInfo->region, pstInfo->pool, dlData->pduInfo[pduIdx].pduBuf,\
282 dlData->pduInfo[pduIdx].pduLen);
284 if(pstInfo->selector == ODU_SELECTOR_LWLC)
286 MAC_FREE_SHRABL_BUF(pstInfo->region, pstInfo->pool, dlData, sizeof(RlcData));
291 /*******************************************************************
293 * @brief Builds and Sends UL Data to RLC
297 * Function : macProcUlData
299 * Functionality: Builds and Sends UL Data to RLC
304 * LC Id on which payload was received
305 * Pointer to the payload
307 * @return ROK - success
310 * ****************************************************************/
311 uint8_t macProcUlData(uint16_t cellId, uint16_t rnti, SlotTimingInfo slotInfo, \
312 uint8_t lcId, uint16_t pduLen, uint8_t *pdu)
317 /* Filling RLC Ul Data*/
318 MAC_ALLOC_SHRABL_BUF(ulData, sizeof(RlcData));
321 DU_LOG("\nERROR --> MAC : Memory allocation failed while sending UL data to RLC");
324 memset(ulData, 0, sizeof(RlcData));
325 ulData->cellId = cellId;
327 memcpy(&ulData->slotInfo, &slotInfo, sizeof(SlotTimingInfo));
328 ulData->slotInfo.cellId = cellId;
330 /* Filling pdu info */
331 if(lcId == SRB1_LCID || lcId == SRB2_LCID)
333 ulData->pduInfo[ulData->numPdu].commCh = true;
335 ulData->pduInfo[ulData->numPdu].lcId = lcId;
336 ulData->pduInfo[ulData->numPdu].pduBuf = pdu;
337 ulData->pduInfo[ulData->numPdu].pduLen = pduLen;
340 /* Filling Post and send to RLC */
341 memset(&pst, 0, sizeof(Pst));
342 FILL_PST_MAC_TO_RLC(pst, 0, EVENT_UL_DATA_TO_RLC);
343 MacSendUlDataToRlc(&pst, ulData);
349 /*******************************************************************
351 * @brief Processes BO status from RLC
355 * Function : MacProcRlcBOStatus
358 * Processes BO status from RLC
360 * @params[in] Post structure
362 * @return ROK - success
365 * ****************************************************************/
366 uint8_t MacProcRlcBoStatus(Pst* pst, RlcBoStatus* boStatus)
368 DlRlcBoInfo dlBoInfo;
370 dlBoInfo.cellId = boStatus->cellId;
371 GET_CRNTI(dlBoInfo.crnti, boStatus->ueIdx);
372 dlBoInfo.lcId = boStatus->lcId;
373 dlBoInfo.dataVolume = boStatus->bo;
375 sendDlRlcBoInfoToSch(&dlBoInfo);
377 if(pst->selector == ODU_SELECTOR_LWLC)
379 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, boStatus, sizeof(RlcBoStatus));
385 /*******************************************************************
387 * @brief Send LC schedule result report to RLC
391 * Function : sendSchRptToRlc
393 * Functionality: Send LC schedule result report to RLC
396 * @return ROK - success
399 * ****************************************************************/
400 uint8_t sendSchedRptToRlc(DlSchedInfo dlInfo, SlotTimingInfo slotInfo, uint8_t ueIdx, uint8_t schInfoIdx)
404 RlcSchedResultRpt *schedRpt = NULLP;
406 MAC_ALLOC_SHRABL_BUF(schedRpt, sizeof(RlcSchedResultRpt));
409 DU_LOG("\nERROR --> MAC: Memory allocation failure in sendSchResultRepToRlc");
413 DU_LOG("\nDEBUG --> MAC: Send scheduled result report for sfn %d slot %d", slotInfo.sfn, slotInfo.slot);
414 schedRpt->cellId = dlInfo.cellId;
415 schedRpt->slotInfo.sfn = slotInfo.sfn;
416 schedRpt->slotInfo.slot = slotInfo.slot;
418 if(dlInfo.dlMsgAlloc[ueIdx])
420 schedRpt->rnti = dlInfo.dlMsgAlloc[ueIdx]->crnti;
421 schedRpt->numLc = dlInfo.dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].numLc;
422 for(lcIdx = 0; lcIdx < schedRpt->numLc; lcIdx++)
424 schedRpt->lcSch[lcIdx].lcId = dlInfo.dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].lcSchInfo[lcIdx].lcId;
425 schedRpt->lcSch[lcIdx].bufSize = dlInfo.dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].lcSchInfo[lcIdx].schBytes;
426 schedRpt->lcSch[lcIdx].commCh = false;
431 FILL_PST_MAC_TO_RLC(pst, RLC_DL_INST, EVENT_SCHED_RESULT_TO_RLC);
432 if(MacSendSchedResultRptToRlc(&pst, schedRpt) != ROK)
434 DU_LOG("\nERROR --> MAC: Failed to send Schedule result report to RLC");
435 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, schedRpt, sizeof(RlcSchedResultRpt));
442 /*******************************************************************
444 * @brief Handles cell start reuqest from DU APP
448 * Function : MacProcCellStart
451 * Handles cell start reuqest from DU APP
453 * @params[in] Post structure pointer
455 * @return ROK - success
458 * ****************************************************************/
459 uint8_t MacProcCellStart(Pst *pst, OduCellId *cellId)
461 DU_LOG("\nINFO --> MAC : Handling cell start request");
463 sendToLowerMac(START_REQUEST, 0, cellId);
465 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellId, \
471 /*******************************************************************
473 * @brief Handles cell stop from DU APP
477 * Function : MacProcCellStop
480 * Handles cell stop from DU APP
482 * @params[in] Post structure pointer
484 * @return ROK - success
487 * ****************************************************************/
488 uint8_t MacProcCellStop(Pst *pst, OduCellId *cellId)
493 DU_LOG("\nINFO --> MAC : Sending cell stop request to Lower Mac");
494 GET_CELL_IDX(cellId->cellId, cellIdx);
495 if(macCb.macCell[cellIdx])
497 macCb.macCell[cellIdx]->state = CELL_TO_BE_STOPPED;
501 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellId, \
507 /*******************************************************************
509 * @brief Handles DL CCCH Ind from DU APP
513 * Function : MacProcDlCcchInd
516 * Handles DL CCCH Ind from DU APP
518 * @params[in] Post structure pointer
519 * DL CCCH Ind pointer
520 * @return ROK - success
523 * ****************************************************************/
524 uint8_t MacProcDlCcchInd(Pst *pst, DlCcchIndInfo *dlCcchIndInfo)
529 DlRlcBoInfo dlBoInfo;
530 memset(&dlBoInfo, 0, sizeof(DlRlcBoInfo));
532 DU_LOG("\nDEBUG --> MAC : Handling DL CCCH IND");
534 GET_CELL_IDX(dlCcchIndInfo->cellId, cellIdx);
536 dlBoInfo.cellId = dlCcchIndInfo->cellId;
537 dlBoInfo.crnti = dlCcchIndInfo->crnti;
539 if(dlCcchIndInfo->msgType == RRC_SETUP)
541 dlBoInfo.lcId = SRB0_LCID; // SRB ID 0 for msg4
542 /* (MSG4 pdu + 3 bytes sub-header) + (Contention resolution id MAC CE + 1 byte sub-header) */
543 dlBoInfo.dataVolume = (dlCcchIndInfo->dlCcchMsgLen + 3) + (MAX_CRI_SIZE + 1);
545 /* storing Msg4 Pdu in raCb */
546 GET_UE_IDX(dlBoInfo.crnti, ueIdx);
548 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == dlCcchIndInfo->crnti)
550 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen = dlCcchIndInfo->dlCcchMsgLen;
551 MAC_ALLOC(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
552 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
553 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu)
555 for(idx = 0; idx < dlCcchIndInfo->dlCcchMsgLen; idx++)
557 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu[idx] =\
558 dlCcchIndInfo->dlCcchMsg[idx];
563 sendDlRlcBoInfoToSch(&dlBoInfo);
565 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo->dlCcchMsg, \
566 dlCcchIndInfo->dlCcchMsgLen);
567 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo, sizeof(DlCcchIndInfo));
572 /*******************************************************************
574 * @brief Sends UL CCCH Ind to DU APP
578 * Function : macProcUlCcchInd
581 * MAC sends UL CCCH Ind to DU APP
583 * @params[in] Post structure pointer
585 * @return ROK - success
588 * ****************************************************************/
589 uint8_t macProcUlCcchInd(uint16_t cellId, uint16_t crnti, uint16_t rrcContSize, uint8_t *rrcContainer)
593 UlCcchIndInfo *ulCcchIndInfo = NULLP;
595 MAC_ALLOC_SHRABL_BUF(ulCcchIndInfo, sizeof(UlCcchIndInfo));
598 DU_LOG("\nERROR --> MAC: Memory failed in macProcUlCcchInd");
602 ulCcchIndInfo->cellId = cellId;
603 ulCcchIndInfo->crnti = crnti;
604 ulCcchIndInfo->ulCcchMsgLen = rrcContSize;
605 ulCcchIndInfo->ulCcchMsg = rrcContainer;
608 FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_UL_CCCH_IND);
610 if(MacDuAppUlCcchInd(&pst, ulCcchIndInfo) != ROK)
612 DU_LOG("\nERROR --> MAC: Failed to send UL CCCH Ind to DU APP");
613 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
614 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
620 /*******************************************************************
622 * @brief Processes received short BSR
626 * Function : macProcShortBsr
629 * MAC sends Short BSR to SCH
631 * @params[in] cell ID
637 * ****************************************************************/
638 uint8_t macProcShortBsr(uint16_t cellId, uint16_t crnti, uint8_t lcgId, uint32_t bufferSize)
641 UlBufferStatusRptInd bsrInd;
643 memset(&pst, 0, sizeof(Pst));
644 memset(&bsrInd, 0, sizeof(UlBufferStatusRptInd));
646 bsrInd.cellId = cellId;
647 bsrInd.crnti = crnti;
648 bsrInd.bsrType = SHORT_BSR;
649 bsrInd.numLcg = 1; /* short bsr reports one lcg at a time */
650 bsrInd.dataVolInfo[0].lcgId = lcgId;
651 bsrInd.dataVolInfo[0].dataVol = bufferSize;
653 FILL_PST_MAC_TO_SCH(pst, EVENT_SHORT_BSR);
654 return(*macSchBsrOpts[pst.selector])(&pst, &bsrInd);
657 /*******************************************************************
659 * @brief Builds and send SR UCI Indication to SCH
663 * Function : buildAndSendSrInd
666 * Builds and send SR UCI Indication to SCH
668 * @params[in] SrUciIndInfo Pointer
670 * @return ROK - success
673 * ****************************************************************/
674 uint8_t buildAndSendSrInd(UciInd *macUciInd, uint8_t crnti)
678 SrUciIndInfo srUciInd;
679 memset(&pst, 0, sizeof(Pst));
680 memset(&srUciInd, 0, sizeof(SrUciIndInfo));
682 GET_CELL_IDX(macUciInd->cellId, cellIdx);
683 srUciInd.cellId = macCb.macCell[cellIdx]->cellId;
684 srUciInd.crnti = crnti;
685 srUciInd.slotInd.sfn = macUciInd->slotInd.sfn;
686 srUciInd.slotInd.slot = macUciInd->slotInd.slot;
687 srUciInd.numSrBits++;
688 memset(srUciInd.srPayload, 0, MAX_SR_BITS_IN_BYTES);
691 FILL_PST_MAC_TO_SCH(pst, EVENT_UCI_IND_TO_SCH);
693 return(*macSchSrUciIndOpts[pst.selector])(&pst, &srUciInd);
696 /*******************************************************************
698 * @brief Processes UCI Indication from PHY
702 * Function : fapiMacUciInd
705 * Processes UCI Indication from PHY
707 * @params[in] Post Structure Pointer
708 * UCI Indication Pointer
709 * @return ROK - success
712 * ****************************************************************/
713 uint8_t FapiMacUciInd(Pst *pst, UciInd *macUciInd)
722 nPdus = macUciInd->numUcis;
725 switch(macUciInd->pdus[pduIdx].pduType)
729 case UCI_IND_PUCCH_F0F1:
730 if(macUciInd->pdus[pduIdx].uci.uciPucchF0F1.srInfo.srIndPres)
732 DU_LOG("\nDEBUG --> MAC : Received SR UCI indication");
733 crnti = macUciInd->pdus[pduIdx].uci.uciPucchF0F1.crnti;
734 ret = buildAndSendSrInd(macUciInd, crnti);
737 case UCI_IND_PUCCH_F2F3F4:
740 DU_LOG("\nERROR --> MAC: Invalid Pdu Type %d at FapiMacUciInd", macUciInd->pdus[pduIdx].pduType);
750 DU_LOG("\nERROR --> MAC: Received Uci Ind is NULL at FapiMacUciInd()");
753 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macUciInd, sizeof(UciInd));
758 /**********************************************************************
760 **********************************************************************/