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 uint16_t buildMacPdu(RlcData *dlData);
42 void macStubBuildUlData(Buffer *mBuf);
45 /* Function pointer for sending crc ind from MAC to SCH */
46 MacSchCrcIndFunc macSchCrcIndOpts[]=
53 /* Function pointer for sending DL RLC BO Info from MAC to SCH */
54 MacSchDlRlcBoInfoFunc macSchDlRlcBoInfoOpts[]=
56 packMacSchDlRlcBoInfo,
61 /* Function pointer for sending short BSR from MAC to SCH */
62 MacSchBsrFunc macSchBsrOpts[]=
69 /* Function pointer for sending SR Uci ind from MAC to SCH */
70 MacSchSrUciIndFunc macSchSrUciIndOpts[]=
77 /*******************************************************************
79 * @brief Sends DL BO Info to SCH
83 * Function : sendDlRlcBoInfoToSch
86 * Sends DL BO Info to SCH
89 * @return ROK - success
92 ****************************************************************/
93 uint8_t sendDlRlcBoInfoToSch(DlRlcBoInfo *dlBoInfo)
97 FILL_PST_MAC_TO_SCH(pst, EVENT_DL_RLC_BO_INFO_TO_SCH);
98 return(*macSchDlRlcBoInfoOpts[pst.selector])(&pst, dlBoInfo);
101 /*******************************************************************
103 * @brief Sends CRC Indication to SCH
107 * Function : sendCrcIndMacToSch
110 * Sends CRC Indication to SCH
113 * @return ROK - success
116 ****************************************************************/
117 uint8_t sendCrcIndMacToSch(CrcIndInfo *crcInd)
121 FILL_PST_MAC_TO_SCH(pst, EVENT_CRC_IND_TO_SCH);
122 return(*macSchCrcIndOpts[pst.selector])(&pst, crcInd);
125 /*******************************************************************
127 * @brief Processes CRC Indication from PHY
131 * Function : fapiMacCrcInd
134 * Processes CRC Indication from PHY
136 * @params[in] Post Structure Pointer
137 * Crc Indication Pointer
138 * @return ROK - success
141 * ****************************************************************/
142 uint8_t fapiMacCrcInd(Pst *pst, CrcInd *crcInd)
145 CrcIndInfo crcIndInfo;
146 DU_LOG("\nMAC : Received CRC indication");
147 GET_CELL_IDX(crcInd->cellId, cellIdx);
148 /* Considering one pdu and one preamble */
149 crcIndInfo.cellId = macCb.macCell[cellIdx]->cellId;
150 crcIndInfo.crnti = crcInd->crcInfo[0].rnti;
151 crcIndInfo.timingInfo.sfn = crcInd->timingInfo.sfn;
152 crcIndInfo.timingInfo.slot = crcInd->timingInfo.slot;
153 crcIndInfo.numCrcInd = crcInd->crcInfo[0].numCb;
154 crcIndInfo.crcInd[0] = crcInd->crcInfo[0].cbCrcStatus[0];
156 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, crcInd, sizeof(CrcInd));
157 return(sendCrcIndMacToSch(&crcIndInfo));
160 /*******************************************************************
162 * @brief Process Rx Data Ind at MAC
166 * Function : fapiMacRxDataInd
169 * Process Rx Data Ind at MAC
171 * @params[in] Post structure
173 * @return ROK - success
176 * ****************************************************************/
177 uint8_t fapiMacRxDataInd(Pst *pst, RxDataInd *rxDataInd)
180 DU_LOG("\nMAC : Received Rx Data indication");
181 /* TODO : compare the handle received in RxDataInd with handle send in PUSCH
182 * PDU, which is stored in raCb */
184 for(pduIdx = 0; pduIdx < rxDataInd->numPdus; pduIdx++)
186 unpackRxData(rxDataInd->cellId, rxDataInd->timingInfo, &rxDataInd->pdus[pduIdx]);
187 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rxDataInd->pdus[pduIdx].pduData,\
188 rxDataInd->pdus[pduIdx].pduLength);
190 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rxDataInd, sizeof(RxDataInd));
194 /*******************************************************************
196 * @brief Processes DL data from RLC
200 * Function : MacProcRlcDlData
203 * Processes DL data from RLC
205 * @params[in] Post structure
207 * @return ROK - success
210 * ****************************************************************/
211 uint8_t MacProcRlcDlData(Pst* pstInfo, RlcData *dlData)
217 MacDlSlot *currDlSlot = NULLP;
219 DU_LOG("\nMAC: Received DL data for sfn=%d slot=%d", \
220 dlData->slotInfo.sfn, dlData->slotInfo.slot);
221 /* Copy the pdus to be muxed into mac Dl data */
222 macDlData.numPdu = dlData->numPdu;
223 for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
225 macDlData.pduInfo[pduIdx].lcId = dlData->pduInfo[pduIdx].lcId;
226 macDlData.pduInfo[pduIdx].pduLen = dlData->pduInfo[pduIdx].pduLen;
227 macDlData.pduInfo[pduIdx].dlPdu = dlData->pduInfo[pduIdx].pduBuf;
230 /* Store DL data in the scheduled slot */
231 currDlSlot = &macCb.macCell[dlData->cellId -1]->dlSlot[dlData->slotInfo.slot];
234 if(currDlSlot->dlInfo.dlMsgAlloc)
236 tbSize = currDlSlot->dlInfo.dlMsgAlloc->dlMsgPdschCfg.codeword[0].tbSize;
237 MAC_ALLOC(txPdu, tbSize);
240 DU_LOG("\nMAC : Memory allocation failed in MacProcRlcDlData");
243 macMuxPdu(&macDlData, NULLP, txPdu, tbSize);
245 currDlSlot->dlInfo.dlMsgAlloc->dlMsgInfo.dlMsgPduLen = tbSize;
246 currDlSlot->dlInfo.dlMsgAlloc->dlMsgInfo.dlMsgPdu = txPdu;
251 for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
253 MAC_FREE_SHRABL_BUF(pstInfo->region, pstInfo->pool, dlData->pduInfo[pduIdx].pduBuf,\
254 dlData->pduInfo[pduIdx].pduLen);
256 if(pstInfo->selector == ODU_SELECTOR_LWLC)
258 MAC_FREE_SHRABL_BUF(pstInfo->region, pstInfo->pool, dlData, sizeof(RlcData));
264 /*******************************************************************
266 * @brief Builds and Sends UL Data to RLC
270 * Function : macProcUlData
272 * Functionality: Builds and Sends UL Data to RLC
277 * LC Id on which payload was received
278 * Pointer to the payload
280 * @return ROK - success
283 * ****************************************************************/
284 uint8_t macProcUlData(uint16_t cellId, uint16_t rnti, SlotIndInfo slotInfo, \
285 uint8_t lcId, uint16_t pduLen, uint8_t *pdu)
290 /* Filling RLC Ul Data*/
291 MAC_ALLOC_SHRABL_BUF(ulData, sizeof(RlcData));
294 DU_LOG("\nMAC : Memory allocation failed while sending UL data to RLC");
297 memset(ulData, 0, sizeof(RlcData));
298 ulData->cellId = cellId;
300 memcpy(&ulData->slotInfo, &slotInfo, sizeof(SlotIndInfo));
301 ulData->slotInfo.cellId = cellId;
303 /* Filling pdu info */
304 if(lcId == SRB1_LCID || lcId == SRB2_LCID)
306 ulData->pduInfo[ulData->numPdu].commCh = true;
308 ulData->pduInfo[ulData->numPdu].lcId = lcId;
309 ulData->pduInfo[ulData->numPdu].pduBuf = pdu;
310 ulData->pduInfo[ulData->numPdu].pduLen = pduLen;
313 /* Filling Post and send to RLC */
314 memset(&pst, 0, sizeof(Pst));
315 FILL_PST_MAC_TO_RLC(pst, 0, EVENT_UL_DATA_TO_RLC);
316 MacSendUlDataToRlc(&pst, ulData);
322 /*******************************************************************
324 * @brief Processes BO status from RLC
328 * Function : MacProcRlcBOStatus
331 * Processes BO status from RLC
333 * @params[in] Post structure
335 * @return ROK - success
338 * ****************************************************************/
339 uint8_t MacProcRlcBoStatus(Pst* pst, RlcBoStatus* boStatus)
341 DlRlcBoInfo dlBoInfo;
343 dlBoInfo.cellId = boStatus->cellId;
344 GET_CRNTI(dlBoInfo.crnti, boStatus->ueIdx);
345 dlBoInfo.lcId = boStatus->lcId;
346 dlBoInfo.dataVolume = boStatus->bo;
348 sendDlRlcBoInfoToSch(&dlBoInfo);
350 if(pst->selector == ODU_SELECTOR_LWLC)
352 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, boStatus, sizeof(RlcBoStatus));
358 /*******************************************************************
360 * @brief Send LC schedule result report to RLC
364 * Function : sendSchRptToRlc
366 * Functionality: Send LC schedule result report to RLC
369 * @return ROK - success
372 * ****************************************************************/
373 uint8_t sendSchedRptToRlc(DlSchedInfo dlInfo, SlotIndInfo slotInfo)
377 RlcSchedResultRpt *schedRpt = NULLP;
379 MAC_ALLOC_SHRABL_BUF(schedRpt, sizeof(RlcSchedResultRpt));
382 DU_LOG("\nMAC: Memory allocation failure in sendSchResultRepToRlc");
386 DU_LOG("\nMAC: Send scheduled result report for sfn %d slot %d", slotInfo.sfn, slotInfo.slot);
387 schedRpt->cellId = dlInfo.cellId;
388 schedRpt->rnti = dlInfo.dlMsgAlloc->crnti;
389 schedRpt->numLc = dlInfo.dlMsgAlloc->numLc;
390 schedRpt->slotInfo.sfn = slotInfo.sfn;
391 schedRpt->slotInfo.slot = slotInfo.slot;
393 for(lcIdx = 0; lcIdx < schedRpt->numLc; lcIdx++)
395 schedRpt->lcSch[lcIdx].lcId = dlInfo.dlMsgAlloc->lcSchInfo[lcIdx].lcId;
396 schedRpt->lcSch[lcIdx].bufSize = dlInfo.dlMsgAlloc->lcSchInfo[lcIdx].schBytes;
397 schedRpt->lcSch[lcIdx].commCh = false;
401 FILL_PST_MAC_TO_RLC(pst, RLC_DL_INST, EVENT_SCHED_RESULT_TO_RLC);
402 if(MacSendSchedResultRptToRlc(&pst, schedRpt) != ROK)
404 DU_LOG("\nMAC: Failed to send Schedule result report to RLC");
405 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, schedRpt, sizeof(RlcSchedResultRpt));
412 /*******************************************************************
414 * @brief Handles cell start reuqest from DU APP
418 * Function : MacProcCellStartReq
421 * Handles cell start reuqest from DU APP
423 * @params[in] Post structure pointer
424 * Cell Start Request info pointer
425 * @return ROK - success
428 * ****************************************************************/
429 uint8_t MacProcCellStartReq(Pst *pst, MacCellStartInfo *cellStartInfo)
431 DU_LOG("\nMAC : Handling cell start request");
432 sendToLowerMac(START_REQUEST, 0, cellStartInfo);
434 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellStartInfo, \
435 sizeof(MacCellStartInfo));
440 /*******************************************************************
442 * @brief Handles cell stop request from DU APP
446 * Function : MacProcCellStartReq
449 * Handles cell stop request from DU APP
451 * @params[in] Post structure pointer
452 * Mac Cell stop information
453 * @return ROK - success
456 * ****************************************************************/
457 uint8_t MacProcCellStopReq(Pst *pst, MacCellStopInfo *cellStopInfo)
461 SlotIndInfo slotInfo;
463 DU_LOG("\nMAC : Sending cell stop request to Lower Mac");
464 GET_CELL_IDX(cellStopInfo->cellId, cellIdx);
465 slotInfo.cellId = cellStopInfo->cellId;
466 slotInfo.sfn = macCb.macCell[cellIdx]->currTime.sfn;
467 slotInfo.slot = macCb.macCell[cellIdx]->currTime.slot;
468 sendToLowerMac(FAPI_STOP_REQUEST, 0, &slotInfo);
471 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellStopInfo, \
472 sizeof(MacCellStopInfo));
477 /*******************************************************************
479 * @brief Handles DL CCCH Ind from DU APP
483 * Function : MacProcDlCcchInd
486 * Handles DL CCCH Ind from DU APP
488 * @params[in] Post structure pointer
489 * DL CCCH Ind pointer
490 * @return ROK - success
493 * ****************************************************************/
494 uint8_t MacProcDlCcchInd(Pst *pst, DlCcchIndInfo *dlCcchIndInfo)
499 DlRlcBoInfo dlBoInfo;
500 memset(&dlBoInfo, 0, sizeof(DlRlcBoInfo));
502 DU_LOG("\nMAC : Handling DL CCCH IND");
504 GET_CELL_IDX(dlCcchIndInfo->cellId, cellIdx);
506 dlBoInfo.cellId = dlCcchIndInfo->cellId;
507 dlBoInfo.crnti = dlCcchIndInfo->crnti;
509 if(dlCcchIndInfo->msgType == RRC_SETUP)
511 dlBoInfo.lcId = SRB0_LCID; // SRB ID 0 for msg4
512 dlBoInfo.dataVolume = dlCcchIndInfo->dlCcchMsgLen;
514 /* storing Msg4 Pdu in raCb */
515 GET_UE_IDX(dlBoInfo.crnti, ueIdx);
517 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == dlCcchIndInfo->crnti)
519 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen = dlCcchIndInfo->dlCcchMsgLen;
520 MAC_ALLOC(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
521 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
522 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu)
524 for(idx = 0; idx < dlCcchIndInfo->dlCcchMsgLen; idx++)
526 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu[idx] =\
527 dlCcchIndInfo->dlCcchMsg[idx];
532 sendDlRlcBoInfoToSch(&dlBoInfo);
534 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo->dlCcchMsg, \
535 dlCcchIndInfo->dlCcchMsgLen);
536 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo, sizeof(DlCcchIndInfo));
541 /*******************************************************************
543 * @brief Sends UL CCCH Ind to DU APP
547 * Function : macProcUlCcchInd
550 * MAC sends UL CCCH Ind to DU APP
552 * @params[in] Post structure pointer
554 * @return ROK - success
557 * ****************************************************************/
558 uint8_t macProcUlCcchInd(uint16_t cellId, uint16_t crnti, uint16_t rrcContSize, uint8_t *rrcContainer)
562 UlCcchIndInfo *ulCcchIndInfo = NULLP;
564 MAC_ALLOC_SHRABL_BUF(ulCcchIndInfo, sizeof(UlCcchIndInfo));
567 DU_LOG("\nMAC: Memory failed in macProcUlCcchInd");
571 ulCcchIndInfo->cellId = cellId;
572 ulCcchIndInfo->crnti = crnti;
573 ulCcchIndInfo->ulCcchMsgLen = rrcContSize;
574 ulCcchIndInfo->ulCcchMsg = rrcContainer;
577 FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_UL_CCCH_IND);
579 if(MacDuAppUlCcchInd(&pst, ulCcchIndInfo) != ROK)
581 DU_LOG("\nMAC: Failed to send UL CCCH Ind to DU APP");
582 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
583 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
589 /*******************************************************************
591 * @brief Processes received short BSR
595 * Function : macProcShortBsr
598 * MAC sends Short BSR to SCH
600 * @params[in] cell ID
606 * ****************************************************************/
607 uint8_t macProcShortBsr(uint16_t cellId, uint16_t crnti, uint8_t lcgId, uint32_t bufferSize)
610 UlBufferStatusRptInd bsrInd;
612 memset(&pst, 0, sizeof(Pst));
613 memset(&bsrInd, 0, sizeof(UlBufferStatusRptInd));
615 bsrInd.cellId = cellId;
616 bsrInd.crnti = crnti;
617 bsrInd.bsrType = SHORT_BSR;
618 bsrInd.numLcg = 1; /* short bsr reports one lcg at a time */
619 bsrInd.dataVolInfo[0].lcgId = lcgId;
620 bsrInd.dataVolInfo[0].dataVol = bufferSize;
622 FILL_PST_MAC_TO_SCH(pst, EVENT_SHORT_BSR);
623 return(*macSchBsrOpts[pst.selector])(&pst, &bsrInd);
626 /*******************************************************************
628 * @brief Builds and send SR UCI Indication to SCH
632 * Function : buildAndSendSrInd
635 * Builds and send SR UCI Indication to SCH
637 * @params[in] SrUciIndInfo Pointer
639 * @return ROK - success
642 * ****************************************************************/
643 uint8_t buildAndSendSrInd(UciInd *macUciInd, uint8_t crnti)
647 SrUciIndInfo srUciInd;
648 memset(&pst, 0, sizeof(Pst));
649 memset(&srUciInd, 0, sizeof(SrUciIndInfo));
651 GET_CELL_IDX(macUciInd->cellId, cellIdx);
652 srUciInd.cellId = macCb.macCell[cellIdx]->cellId;
653 srUciInd.crnti = crnti;
654 srUciInd.slotInd.sfn = macUciInd->slotInd.sfn;
655 srUciInd.slotInd.slot = macUciInd->slotInd.slot;
656 srUciInd.numSrBits++;
657 memset(srUciInd.srPayload, 0, MAX_SR_BITS_IN_BYTES);
660 FILL_PST_MAC_TO_SCH(pst, EVENT_UCI_IND_TO_SCH);
662 return(*macSchSrUciIndOpts[pst.selector])(&pst, &srUciInd);
665 /*******************************************************************
667 * @brief Processes UCI Indication from PHY
671 * Function : fapiMacUciInd
674 * Processes UCI Indication from PHY
676 * @params[in] Post Structure Pointer
677 * UCI Indication Pointer
678 * @return ROK - success
681 * ****************************************************************/
682 uint8_t FapiMacUciInd(Pst *pst, UciInd *macUciInd)
691 nPdus = macUciInd->numUcis;
694 switch(macUciInd->pdus[pduIdx].pduType)
698 case UCI_IND_PUCCH_F0F1:
699 if(macUciInd->pdus[pduIdx].uci.uciPucchF0F1.srInfo.srIndPres)
701 DU_LOG("\nMAC : Received SR UCI indication");
702 crnti = macUciInd->pdus[pduIdx].uci.uciPucchF0F1.crnti;
703 ret = buildAndSendSrInd(macUciInd, crnti);
706 case UCI_IND_PUCCH_F2F3F4:
709 DU_LOG("\nMAC: Invalid Pdu Type %d at FapiMacUciInd", macUciInd->pdus[pduIdx].pduType);
719 DU_LOG("\nMAC: Received Uci Ind is NULL at FapiMacUciInd()");
722 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macUciInd, sizeof(UciInd));
727 /**********************************************************************
729 **********************************************************************/