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 uint16_t cellIdx = 0, txPduLen = 0;
214 MacDlSlot *currDlSlot = NULLP;
215 DlRlcBoInfo dlBoInfo;
217 memset(&macDlData , 0, sizeof(MacDlData));
218 DU_LOG("\nDEBUG --> MAC: Received DL data for sfn=%d slot=%d numPdu= %d", \
219 dlData->slotInfo.sfn, dlData->slotInfo.slot, dlData->numPdu);
221 GET_UE_IDX(dlData->rnti, ueId);
223 /* Copy the pdus to be muxed into mac Dl data */
224 macDlData.ueId = ueId;
225 macDlData.numPdu = dlData->numPdu;
226 for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
228 macDlData.pduInfo[pduIdx].lcId = dlData->pduInfo[pduIdx].lcId;
229 macDlData.pduInfo[pduIdx].pduLen = dlData->pduInfo[pduIdx].pduLen;
230 macDlData.pduInfo[pduIdx].dlPdu = dlData->pduInfo[pduIdx].pduBuf;
233 GET_CELL_IDX(dlData->cellId, cellIdx);
234 /* Store DL data in the scheduled slot */
235 if(macCb.macCell[cellIdx] ==NULLP)
237 DU_LOG("\nERROR --> MAC : MacProcRlcDlData(): macCell does not exists");
240 currDlSlot = &macCb.macCell[cellIdx]->dlSlot[dlData->slotInfo.slot];
241 if(currDlSlot->dlInfo.dlMsgAlloc[ueId-1])
243 txPduLen = currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgPdschCfg.codeword[0].tbSize - TX_PAYLOAD_HDR_LEN;
244 MAC_ALLOC(txPdu, txPduLen);
247 DU_LOG("\nERROR --> MAC : Memory allocation failed in MacProcRlcDlData");
250 macMuxPdu(&macDlData, NULLP, txPdu, txPduLen);
252 currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgInfo.dlMsgPduLen = txPduLen;
253 currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgInfo.dlMsgPdu = txPdu;
256 for(lcIdx = 0; lcIdx < dlData->numLc; lcIdx++)
258 if(dlData->boStatus[lcIdx].bo)
260 memset(&dlBoInfo, 0, sizeof(DlRlcBoInfo));
261 dlBoInfo.cellId = dlData->boStatus[lcIdx].cellId;
262 GET_CRNTI(dlBoInfo.crnti, dlData->boStatus[lcIdx].ueIdx);
263 dlBoInfo.lcId = dlData->boStatus[lcIdx].lcId;
264 dlBoInfo.dataVolume = dlData->boStatus[lcIdx].bo;
265 sendDlRlcBoInfoToSch(&dlBoInfo);
270 for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
272 MAC_FREE_SHRABL_BUF(pstInfo->region, pstInfo->pool, dlData->pduInfo[pduIdx].pduBuf,\
273 dlData->pduInfo[pduIdx].pduLen);
275 if(pstInfo->selector == ODU_SELECTOR_LWLC)
277 MAC_FREE_SHRABL_BUF(pstInfo->region, pstInfo->pool, dlData, sizeof(RlcData));
282 /*******************************************************************
284 * @brief Builds and Sends UL Data to RLC
288 * Function : macProcUlData
290 * Functionality: Builds and Sends UL Data to RLC
295 * LC Id on which payload was received
296 * Pointer to the payload
298 * @return ROK - success
301 * ****************************************************************/
302 uint8_t macProcUlData(uint16_t cellId, uint16_t rnti, SlotTimingInfo slotInfo, \
303 uint8_t lcId, uint16_t pduLen, uint8_t *pdu)
308 /* Filling RLC Ul Data*/
309 MAC_ALLOC_SHRABL_BUF(ulData, sizeof(RlcData));
312 DU_LOG("\nERROR --> MAC : Memory allocation failed while sending UL data to RLC");
315 memset(ulData, 0, sizeof(RlcData));
316 ulData->cellId = cellId;
318 memcpy(&ulData->slotInfo, &slotInfo, sizeof(SlotTimingInfo));
319 ulData->slotInfo.cellId = cellId;
321 /* Filling pdu info */
322 if(lcId == SRB1_LCID || lcId == SRB2_LCID)
324 ulData->pduInfo[ulData->numPdu].commCh = true;
326 ulData->pduInfo[ulData->numPdu].lcId = lcId;
327 ulData->pduInfo[ulData->numPdu].pduBuf = pdu;
328 ulData->pduInfo[ulData->numPdu].pduLen = pduLen;
331 /* Filling Post and send to RLC */
332 memset(&pst, 0, sizeof(Pst));
333 FILL_PST_MAC_TO_RLC(pst, 0, EVENT_UL_DATA_TO_RLC);
334 MacSendUlDataToRlc(&pst, ulData);
340 /*******************************************************************
342 * @brief Processes BO status from RLC
346 * Function : MacProcRlcBOStatus
349 * Processes BO status from RLC
351 * @params[in] Post structure
353 * @return ROK - success
356 * ****************************************************************/
357 uint8_t MacProcRlcBoStatus(Pst* pst, RlcBoStatus* boStatus)
359 DlRlcBoInfo dlBoInfo;
361 dlBoInfo.cellId = boStatus->cellId;
362 GET_CRNTI(dlBoInfo.crnti, boStatus->ueIdx);
363 dlBoInfo.lcId = boStatus->lcId;
364 dlBoInfo.dataVolume = boStatus->bo;
366 sendDlRlcBoInfoToSch(&dlBoInfo);
368 if(pst->selector == ODU_SELECTOR_LWLC)
370 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, boStatus, sizeof(RlcBoStatus));
376 /*******************************************************************
378 * @brief Send LC schedule result report to RLC
382 * Function : sendSchRptToRlc
384 * Functionality: Send LC schedule result report to RLC
387 * @return ROK - success
390 * ****************************************************************/
391 uint8_t sendSchedRptToRlc(DlSchedInfo dlInfo, SlotTimingInfo slotInfo)
394 uint8_t lcIdx, ueIdx;
395 RlcSchedResultRpt *schedRpt = NULLP;
397 MAC_ALLOC_SHRABL_BUF(schedRpt, sizeof(RlcSchedResultRpt));
400 DU_LOG("\nERROR --> MAC: Memory allocation failure in sendSchResultRepToRlc");
404 DU_LOG("\nDEBUG --> MAC: Send scheduled result report for sfn %d slot %d", slotInfo.sfn, slotInfo.slot);
405 schedRpt->cellId = dlInfo.cellId;
406 schedRpt->slotInfo.sfn = slotInfo.sfn;
407 schedRpt->slotInfo.slot = slotInfo.slot;
409 for(ueIdx=0; ueIdx<MAX_NUM_UE; ueIdx++)
411 if(dlInfo.dlMsgAlloc[ueIdx])
413 schedRpt->rnti = dlInfo.dlMsgAlloc[ueIdx]->crnti;
414 schedRpt->numLc = dlInfo.dlMsgAlloc[ueIdx]->numLc;
415 for(lcIdx = 0; lcIdx < schedRpt->numLc; lcIdx++)
417 schedRpt->lcSch[lcIdx].lcId = dlInfo.dlMsgAlloc[ueIdx]->lcSchInfo[lcIdx].lcId;
418 schedRpt->lcSch[lcIdx].bufSize = dlInfo.dlMsgAlloc[ueIdx]->lcSchInfo[lcIdx].schBytes;
419 schedRpt->lcSch[lcIdx].commCh = false;
426 FILL_PST_MAC_TO_RLC(pst, RLC_DL_INST, EVENT_SCHED_RESULT_TO_RLC);
427 if(MacSendSchedResultRptToRlc(&pst, schedRpt) != ROK)
429 DU_LOG("\nERROR --> MAC: Failed to send Schedule result report to RLC");
430 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, schedRpt, sizeof(RlcSchedResultRpt));
437 /*******************************************************************
439 * @brief Handles cell start reuqest from DU APP
443 * Function : MacProcCellStart
446 * Handles cell start reuqest from DU APP
448 * @params[in] Post structure pointer
450 * @return ROK - success
453 * ****************************************************************/
454 uint8_t MacProcCellStart(Pst *pst, OduCellId *cellId)
456 DU_LOG("\nINFO --> MAC : Handling cell start request");
458 sendToLowerMac(START_REQUEST, 0, cellId);
460 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellId, \
466 /*******************************************************************
468 * @brief Handles cell stop from DU APP
472 * Function : MacProcCellStop
475 * Handles cell stop from DU APP
477 * @params[in] Post structure pointer
479 * @return ROK - success
482 * ****************************************************************/
483 uint8_t MacProcCellStop(Pst *pst, OduCellId *cellId)
488 DU_LOG("\nINFO --> MAC : Sending cell stop request to Lower Mac");
489 GET_CELL_IDX(cellId->cellId, cellIdx);
490 if(macCb.macCell[cellIdx])
492 macCb.macCell[cellIdx]->state = CELL_TO_BE_STOPPED;
496 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellId, \
502 /*******************************************************************
504 * @brief Handles DL CCCH Ind from DU APP
508 * Function : MacProcDlCcchInd
511 * Handles DL CCCH Ind from DU APP
513 * @params[in] Post structure pointer
514 * DL CCCH Ind pointer
515 * @return ROK - success
518 * ****************************************************************/
519 uint8_t MacProcDlCcchInd(Pst *pst, DlCcchIndInfo *dlCcchIndInfo)
524 DlRlcBoInfo dlBoInfo;
525 memset(&dlBoInfo, 0, sizeof(DlRlcBoInfo));
527 DU_LOG("\nDEBUG --> MAC : Handling DL CCCH IND");
529 GET_CELL_IDX(dlCcchIndInfo->cellId, cellIdx);
531 dlBoInfo.cellId = dlCcchIndInfo->cellId;
532 dlBoInfo.crnti = dlCcchIndInfo->crnti;
534 if(dlCcchIndInfo->msgType == RRC_SETUP)
536 dlBoInfo.lcId = SRB0_LCID; // SRB ID 0 for msg4
537 /* (MSG4 pdu + 3 bytes sub-header) + (Contention resolution id MAC CE + 1 byte sub-header) */
538 dlBoInfo.dataVolume = (dlCcchIndInfo->dlCcchMsgLen + 3) + (MAX_CRI_SIZE + 1);
540 /* storing Msg4 Pdu in raCb */
541 GET_UE_IDX(dlBoInfo.crnti, ueIdx);
543 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == dlCcchIndInfo->crnti)
545 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen = dlCcchIndInfo->dlCcchMsgLen;
546 MAC_ALLOC(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
547 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
548 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu)
550 for(idx = 0; idx < dlCcchIndInfo->dlCcchMsgLen; idx++)
552 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu[idx] =\
553 dlCcchIndInfo->dlCcchMsg[idx];
558 sendDlRlcBoInfoToSch(&dlBoInfo);
560 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo->dlCcchMsg, \
561 dlCcchIndInfo->dlCcchMsgLen);
562 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo, sizeof(DlCcchIndInfo));
567 /*******************************************************************
569 * @brief Sends UL CCCH Ind to DU APP
573 * Function : macProcUlCcchInd
576 * MAC sends UL CCCH Ind to DU APP
578 * @params[in] Post structure pointer
580 * @return ROK - success
583 * ****************************************************************/
584 uint8_t macProcUlCcchInd(uint16_t cellId, uint16_t crnti, uint16_t rrcContSize, uint8_t *rrcContainer)
588 UlCcchIndInfo *ulCcchIndInfo = NULLP;
590 MAC_ALLOC_SHRABL_BUF(ulCcchIndInfo, sizeof(UlCcchIndInfo));
593 DU_LOG("\nERROR --> MAC: Memory failed in macProcUlCcchInd");
597 ulCcchIndInfo->cellId = cellId;
598 ulCcchIndInfo->crnti = crnti;
599 ulCcchIndInfo->ulCcchMsgLen = rrcContSize;
600 ulCcchIndInfo->ulCcchMsg = rrcContainer;
603 FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_UL_CCCH_IND);
605 if(MacDuAppUlCcchInd(&pst, ulCcchIndInfo) != ROK)
607 DU_LOG("\nERROR --> MAC: Failed to send UL CCCH Ind to DU APP");
608 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
609 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
615 /*******************************************************************
617 * @brief Processes received short BSR
621 * Function : macProcShortBsr
624 * MAC sends Short BSR to SCH
626 * @params[in] cell ID
632 * ****************************************************************/
633 uint8_t macProcShortBsr(uint16_t cellId, uint16_t crnti, uint8_t lcgId, uint32_t bufferSize)
636 UlBufferStatusRptInd bsrInd;
638 memset(&pst, 0, sizeof(Pst));
639 memset(&bsrInd, 0, sizeof(UlBufferStatusRptInd));
641 bsrInd.cellId = cellId;
642 bsrInd.crnti = crnti;
643 bsrInd.bsrType = SHORT_BSR;
644 bsrInd.numLcg = 1; /* short bsr reports one lcg at a time */
645 bsrInd.dataVolInfo[0].lcgId = lcgId;
646 bsrInd.dataVolInfo[0].dataVol = bufferSize;
648 FILL_PST_MAC_TO_SCH(pst, EVENT_SHORT_BSR);
649 return(*macSchBsrOpts[pst.selector])(&pst, &bsrInd);
652 /*******************************************************************
654 * @brief Builds and send SR UCI Indication to SCH
658 * Function : buildAndSendSrInd
661 * Builds and send SR UCI Indication to SCH
663 * @params[in] SrUciIndInfo Pointer
665 * @return ROK - success
668 * ****************************************************************/
669 uint8_t buildAndSendSrInd(UciInd *macUciInd, uint8_t crnti)
673 SrUciIndInfo srUciInd;
674 memset(&pst, 0, sizeof(Pst));
675 memset(&srUciInd, 0, sizeof(SrUciIndInfo));
677 GET_CELL_IDX(macUciInd->cellId, cellIdx);
678 srUciInd.cellId = macCb.macCell[cellIdx]->cellId;
679 srUciInd.crnti = crnti;
680 srUciInd.slotInd.sfn = macUciInd->slotInd.sfn;
681 srUciInd.slotInd.slot = macUciInd->slotInd.slot;
682 srUciInd.numSrBits++;
683 memset(srUciInd.srPayload, 0, MAX_SR_BITS_IN_BYTES);
686 FILL_PST_MAC_TO_SCH(pst, EVENT_UCI_IND_TO_SCH);
688 return(*macSchSrUciIndOpts[pst.selector])(&pst, &srUciInd);
691 /*******************************************************************
693 * @brief Processes UCI Indication from PHY
697 * Function : fapiMacUciInd
700 * Processes UCI Indication from PHY
702 * @params[in] Post Structure Pointer
703 * UCI Indication Pointer
704 * @return ROK - success
707 * ****************************************************************/
708 uint8_t FapiMacUciInd(Pst *pst, UciInd *macUciInd)
717 nPdus = macUciInd->numUcis;
720 switch(macUciInd->pdus[pduIdx].pduType)
724 case UCI_IND_PUCCH_F0F1:
725 if(macUciInd->pdus[pduIdx].uci.uciPucchF0F1.srInfo.srIndPres)
727 DU_LOG("\nDEBUG --> MAC : Received SR UCI indication");
728 crnti = macUciInd->pdus[pduIdx].uci.uciPucchF0F1.crnti;
729 ret = buildAndSendSrInd(macUciInd, crnti);
732 case UCI_IND_PUCCH_F2F3F4:
735 DU_LOG("\nERROR --> MAC: Invalid Pdu Type %d at FapiMacUciInd", macUciInd->pdus[pduIdx].pduType);
745 DU_LOG("\nERROR --> MAC: Received Uci Ind is NULL at FapiMacUciInd()");
748 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macUciInd, sizeof(UciInd));
753 /**********************************************************************
755 **********************************************************************/