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)
210 uint8_t *txPdu = NULLP;
211 uint16_t cellIdx = 0, txPduLen = 0;
213 MacDlSlot *currDlSlot = NULLP;
214 DlRlcBoInfo dlBoInfo;
216 memset(&macDlData , 0, sizeof(MacDlData));
217 DU_LOG("\nDEBUG --> MAC: Received DL data for sfn=%d slot=%d numPdu= %d", \
218 dlData->slotInfo.sfn, dlData->slotInfo.slot, dlData->numPdu);
219 /* Copy the pdus to be muxed into mac Dl data */
220 macDlData.numPdu = dlData->numPdu;
221 for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
223 macDlData.pduInfo[pduIdx].lcId = dlData->pduInfo[pduIdx].lcId;
224 macDlData.pduInfo[pduIdx].pduLen = dlData->pduInfo[pduIdx].pduLen;
225 macDlData.pduInfo[pduIdx].dlPdu = dlData->pduInfo[pduIdx].pduBuf;
228 GET_CELL_IDX(dlData->cellId, cellIdx);
229 /* Store DL data in the scheduled slot */
230 if(macCb.macCell[cellIdx] ==NULLP)
232 DU_LOG("\nERROR --> MAC : MacProcRlcDlData(): macCell does not exists");
235 currDlSlot = &macCb.macCell[cellIdx]->dlSlot[dlData->slotInfo.slot];
236 if(currDlSlot->dlInfo.dlMsgAlloc)
238 txPduLen = currDlSlot->dlInfo.dlMsgAlloc->dlMsgPdschCfg.codeword[0].tbSize - TX_PAYLOAD_HDR_LEN;
239 MAC_ALLOC(txPdu, txPduLen);
242 DU_LOG("\nERROR --> MAC : Memory allocation failed in MacProcRlcDlData");
245 macMuxPdu(&macDlData, NULLP, txPdu, txPduLen);
247 currDlSlot->dlInfo.dlMsgAlloc->dlMsgInfo.dlMsgPduLen = txPduLen;
248 currDlSlot->dlInfo.dlMsgAlloc->dlMsgInfo.dlMsgPdu = txPdu;
251 for(lcIdx = 0; lcIdx < dlData->numLc; lcIdx++)
253 if(dlData->boStatus[lcIdx].bo)
255 memset(&dlBoInfo, 0, sizeof(DlRlcBoInfo));
256 dlBoInfo.cellId = dlData->boStatus[lcIdx].cellId;
257 GET_CRNTI(dlBoInfo.crnti, dlData->boStatus[lcIdx].ueIdx);
258 dlBoInfo.lcId = dlData->boStatus[lcIdx].lcId;
259 dlBoInfo.dataVolume = dlData->boStatus[lcIdx].bo;
260 sendDlRlcBoInfoToSch(&dlBoInfo);
265 for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
267 MAC_FREE_SHRABL_BUF(pstInfo->region, pstInfo->pool, dlData->pduInfo[pduIdx].pduBuf,\
268 dlData->pduInfo[pduIdx].pduLen);
270 if(pstInfo->selector == ODU_SELECTOR_LWLC)
272 MAC_FREE_SHRABL_BUF(pstInfo->region, pstInfo->pool, dlData, sizeof(RlcData));
277 /*******************************************************************
279 * @brief Builds and Sends UL Data to RLC
283 * Function : macProcUlData
285 * Functionality: Builds and Sends UL Data to RLC
290 * LC Id on which payload was received
291 * Pointer to the payload
293 * @return ROK - success
296 * ****************************************************************/
297 uint8_t macProcUlData(uint16_t cellId, uint16_t rnti, SlotTimingInfo slotInfo, \
298 uint8_t lcId, uint16_t pduLen, uint8_t *pdu)
303 /* Filling RLC Ul Data*/
304 MAC_ALLOC_SHRABL_BUF(ulData, sizeof(RlcData));
307 DU_LOG("\nERROR --> MAC : Memory allocation failed while sending UL data to RLC");
310 memset(ulData, 0, sizeof(RlcData));
311 ulData->cellId = cellId;
313 memcpy(&ulData->slotInfo, &slotInfo, sizeof(SlotTimingInfo));
314 ulData->slotInfo.cellId = cellId;
316 /* Filling pdu info */
317 if(lcId == SRB1_LCID || lcId == SRB2_LCID)
319 ulData->pduInfo[ulData->numPdu].commCh = true;
321 ulData->pduInfo[ulData->numPdu].lcId = lcId;
322 ulData->pduInfo[ulData->numPdu].pduBuf = pdu;
323 ulData->pduInfo[ulData->numPdu].pduLen = pduLen;
326 /* Filling Post and send to RLC */
327 memset(&pst, 0, sizeof(Pst));
328 FILL_PST_MAC_TO_RLC(pst, 0, EVENT_UL_DATA_TO_RLC);
329 MacSendUlDataToRlc(&pst, ulData);
335 /*******************************************************************
337 * @brief Processes BO status from RLC
341 * Function : MacProcRlcBOStatus
344 * Processes BO status from RLC
346 * @params[in] Post structure
348 * @return ROK - success
351 * ****************************************************************/
352 uint8_t MacProcRlcBoStatus(Pst* pst, RlcBoStatus* boStatus)
354 DlRlcBoInfo dlBoInfo;
356 dlBoInfo.cellId = boStatus->cellId;
357 GET_CRNTI(dlBoInfo.crnti, boStatus->ueIdx);
358 dlBoInfo.lcId = boStatus->lcId;
359 dlBoInfo.dataVolume = boStatus->bo;
361 sendDlRlcBoInfoToSch(&dlBoInfo);
363 if(pst->selector == ODU_SELECTOR_LWLC)
365 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, boStatus, sizeof(RlcBoStatus));
371 /*******************************************************************
373 * @brief Send LC schedule result report to RLC
377 * Function : sendSchRptToRlc
379 * Functionality: Send LC schedule result report to RLC
382 * @return ROK - success
385 * ****************************************************************/
386 uint8_t sendSchedRptToRlc(DlSchedInfo dlInfo, SlotTimingInfo slotInfo)
390 RlcSchedResultRpt *schedRpt = NULLP;
392 MAC_ALLOC_SHRABL_BUF(schedRpt, sizeof(RlcSchedResultRpt));
395 DU_LOG("\nERROR --> MAC: Memory allocation failure in sendSchResultRepToRlc");
399 DU_LOG("\nDEBUG --> MAC: Send scheduled result report for sfn %d slot %d", slotInfo.sfn, slotInfo.slot);
400 schedRpt->cellId = dlInfo.cellId;
401 schedRpt->rnti = dlInfo.dlMsgAlloc->crnti;
402 schedRpt->numLc = dlInfo.dlMsgAlloc->numLc;
403 schedRpt->slotInfo.sfn = slotInfo.sfn;
404 schedRpt->slotInfo.slot = slotInfo.slot;
406 for(lcIdx = 0; lcIdx < schedRpt->numLc; lcIdx++)
408 schedRpt->lcSch[lcIdx].lcId = dlInfo.dlMsgAlloc->lcSchInfo[lcIdx].lcId;
409 schedRpt->lcSch[lcIdx].bufSize = dlInfo.dlMsgAlloc->lcSchInfo[lcIdx].schBytes;
410 schedRpt->lcSch[lcIdx].commCh = false;
414 FILL_PST_MAC_TO_RLC(pst, RLC_DL_INST, EVENT_SCHED_RESULT_TO_RLC);
415 if(MacSendSchedResultRptToRlc(&pst, schedRpt) != ROK)
417 DU_LOG("\nERROR --> MAC: Failed to send Schedule result report to RLC");
418 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, schedRpt, sizeof(RlcSchedResultRpt));
425 /*******************************************************************
427 * @brief Handles cell start reuqest from DU APP
431 * Function : MacProcCellStart
434 * Handles cell start reuqest from DU APP
436 * @params[in] Post structure pointer
438 * @return ROK - success
441 * ****************************************************************/
442 uint8_t MacProcCellStart(Pst *pst, OduCellId *cellId)
444 DU_LOG("\nINFO --> MAC : Handling cell start request");
446 sendToLowerMac(START_REQUEST, 0, cellId);
448 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellId, \
454 /*******************************************************************
456 * @brief Handles cell stop from DU APP
460 * Function : MacProcCellStop
463 * Handles cell stop from DU APP
465 * @params[in] Post structure pointer
467 * @return ROK - success
470 * ****************************************************************/
471 uint8_t MacProcCellStop(Pst *pst, OduCellId *cellId)
476 DU_LOG("\nINFO --> MAC : Sending cell stop request to Lower Mac");
477 GET_CELL_IDX(cellId->cellId, cellIdx);
478 if(macCb.macCell[cellIdx])
480 macCb.macCell[cellIdx]->state = CELL_TO_BE_STOPPED;
484 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellId, \
490 /*******************************************************************
492 * @brief Handles DL CCCH Ind from DU APP
496 * Function : MacProcDlCcchInd
499 * Handles DL CCCH Ind from DU APP
501 * @params[in] Post structure pointer
502 * DL CCCH Ind pointer
503 * @return ROK - success
506 * ****************************************************************/
507 uint8_t MacProcDlCcchInd(Pst *pst, DlCcchIndInfo *dlCcchIndInfo)
512 DlRlcBoInfo dlBoInfo;
513 memset(&dlBoInfo, 0, sizeof(DlRlcBoInfo));
515 DU_LOG("\nDEBUG --> MAC : Handling DL CCCH IND");
517 GET_CELL_IDX(dlCcchIndInfo->cellId, cellIdx);
519 dlBoInfo.cellId = dlCcchIndInfo->cellId;
520 dlBoInfo.crnti = dlCcchIndInfo->crnti;
522 if(dlCcchIndInfo->msgType == RRC_SETUP)
524 dlBoInfo.lcId = SRB0_LCID; // SRB ID 0 for msg4
525 /* (MSG4 pdu + 3 bytes sub-header) + (Contention resolution id MAC CE + 1 byte sub-header) */
526 dlBoInfo.dataVolume = (dlCcchIndInfo->dlCcchMsgLen + 3) + (MAX_CRI_SIZE + 1);
528 /* storing Msg4 Pdu in raCb */
529 GET_UE_IDX(dlBoInfo.crnti, ueIdx);
531 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == dlCcchIndInfo->crnti)
533 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen = dlCcchIndInfo->dlCcchMsgLen;
534 MAC_ALLOC(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
535 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
536 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu)
538 for(idx = 0; idx < dlCcchIndInfo->dlCcchMsgLen; idx++)
540 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu[idx] =\
541 dlCcchIndInfo->dlCcchMsg[idx];
546 sendDlRlcBoInfoToSch(&dlBoInfo);
548 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo->dlCcchMsg, \
549 dlCcchIndInfo->dlCcchMsgLen);
550 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo, sizeof(DlCcchIndInfo));
555 /*******************************************************************
557 * @brief Sends UL CCCH Ind to DU APP
561 * Function : macProcUlCcchInd
564 * MAC sends UL CCCH Ind to DU APP
566 * @params[in] Post structure pointer
568 * @return ROK - success
571 * ****************************************************************/
572 uint8_t macProcUlCcchInd(uint16_t cellId, uint16_t crnti, uint16_t rrcContSize, uint8_t *rrcContainer)
576 UlCcchIndInfo *ulCcchIndInfo = NULLP;
578 MAC_ALLOC_SHRABL_BUF(ulCcchIndInfo, sizeof(UlCcchIndInfo));
581 DU_LOG("\nERROR --> MAC: Memory failed in macProcUlCcchInd");
585 ulCcchIndInfo->cellId = cellId;
586 ulCcchIndInfo->crnti = crnti;
587 ulCcchIndInfo->ulCcchMsgLen = rrcContSize;
588 ulCcchIndInfo->ulCcchMsg = rrcContainer;
591 FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_UL_CCCH_IND);
593 if(MacDuAppUlCcchInd(&pst, ulCcchIndInfo) != ROK)
595 DU_LOG("\nERROR --> MAC: Failed to send UL CCCH Ind to DU APP");
596 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
597 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
603 /*******************************************************************
605 * @brief Processes received short BSR
609 * Function : macProcShortBsr
612 * MAC sends Short BSR to SCH
614 * @params[in] cell ID
620 * ****************************************************************/
621 uint8_t macProcShortBsr(uint16_t cellId, uint16_t crnti, uint8_t lcgId, uint32_t bufferSize)
624 UlBufferStatusRptInd bsrInd;
626 memset(&pst, 0, sizeof(Pst));
627 memset(&bsrInd, 0, sizeof(UlBufferStatusRptInd));
629 bsrInd.cellId = cellId;
630 bsrInd.crnti = crnti;
631 bsrInd.bsrType = SHORT_BSR;
632 bsrInd.numLcg = 1; /* short bsr reports one lcg at a time */
633 bsrInd.dataVolInfo[0].lcgId = lcgId;
634 bsrInd.dataVolInfo[0].dataVol = bufferSize;
636 FILL_PST_MAC_TO_SCH(pst, EVENT_SHORT_BSR);
637 return(*macSchBsrOpts[pst.selector])(&pst, &bsrInd);
640 /*******************************************************************
642 * @brief Builds and send SR UCI Indication to SCH
646 * Function : buildAndSendSrInd
649 * Builds and send SR UCI Indication to SCH
651 * @params[in] SrUciIndInfo Pointer
653 * @return ROK - success
656 * ****************************************************************/
657 uint8_t buildAndSendSrInd(UciInd *macUciInd, uint8_t crnti)
661 SrUciIndInfo srUciInd;
662 memset(&pst, 0, sizeof(Pst));
663 memset(&srUciInd, 0, sizeof(SrUciIndInfo));
665 GET_CELL_IDX(macUciInd->cellId, cellIdx);
666 srUciInd.cellId = macCb.macCell[cellIdx]->cellId;
667 srUciInd.crnti = crnti;
668 srUciInd.slotInd.sfn = macUciInd->slotInd.sfn;
669 srUciInd.slotInd.slot = macUciInd->slotInd.slot;
670 srUciInd.numSrBits++;
671 memset(srUciInd.srPayload, 0, MAX_SR_BITS_IN_BYTES);
674 FILL_PST_MAC_TO_SCH(pst, EVENT_UCI_IND_TO_SCH);
676 return(*macSchSrUciIndOpts[pst.selector])(&pst, &srUciInd);
679 /*******************************************************************
681 * @brief Processes UCI Indication from PHY
685 * Function : fapiMacUciInd
688 * Processes UCI Indication from PHY
690 * @params[in] Post Structure Pointer
691 * UCI Indication Pointer
692 * @return ROK - success
695 * ****************************************************************/
696 uint8_t FapiMacUciInd(Pst *pst, UciInd *macUciInd)
705 nPdus = macUciInd->numUcis;
708 switch(macUciInd->pdus[pduIdx].pduType)
712 case UCI_IND_PUCCH_F0F1:
713 if(macUciInd->pdus[pduIdx].uci.uciPucchF0F1.srInfo.srIndPres)
715 DU_LOG("\nDEBUG --> MAC : Received SR UCI indication");
716 crnti = macUciInd->pdus[pduIdx].uci.uciPucchF0F1.crnti;
717 ret = buildAndSendSrInd(macUciInd, crnti);
720 case UCI_IND_PUCCH_F2F3F4:
723 DU_LOG("\nERROR --> MAC: Invalid Pdu Type %d at FapiMacUciInd", macUciInd->pdus[pduIdx].pduType);
733 DU_LOG("\nERROR --> MAC: Received Uci Ind is NULL at FapiMacUciInd()");
736 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macUciInd, sizeof(UciInd));
741 /**********************************************************************
743 **********************************************************************/