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("\nDEBUG --> MAC : 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("\nDEBUG --> MAC : 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)
215 uint8_t *txPdu = NULLP;
216 uint16_t cellIdx = 0, txPduLen = 0;
218 MacDlSlot *currDlSlot = NULLP;
219 DlRlcBoInfo dlBoInfo;
221 memset(&macDlData , 0, sizeof(MacDlData));
222 DU_LOG("\nDEBUG --> MAC: Received DL data for sfn=%d slot=%d numPdu= %d", \
223 dlData->slotInfo.sfn, dlData->slotInfo.slot, dlData->numPdu);
224 /* Copy the pdus to be muxed into mac Dl data */
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)
243 txPduLen = currDlSlot->dlInfo.dlMsgAlloc->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->dlMsgInfo.dlMsgPduLen = txPduLen;
253 currDlSlot->dlInfo.dlMsgAlloc->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)
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->rnti = dlInfo.dlMsgAlloc->crnti;
407 schedRpt->numLc = dlInfo.dlMsgAlloc->numLc;
408 schedRpt->slotInfo.sfn = slotInfo.sfn;
409 schedRpt->slotInfo.slot = slotInfo.slot;
411 for(lcIdx = 0; lcIdx < schedRpt->numLc; lcIdx++)
413 schedRpt->lcSch[lcIdx].lcId = dlInfo.dlMsgAlloc->lcSchInfo[lcIdx].lcId;
414 schedRpt->lcSch[lcIdx].bufSize = dlInfo.dlMsgAlloc->lcSchInfo[lcIdx].schBytes;
415 schedRpt->lcSch[lcIdx].commCh = false;
419 FILL_PST_MAC_TO_RLC(pst, RLC_DL_INST, EVENT_SCHED_RESULT_TO_RLC);
420 if(MacSendSchedResultRptToRlc(&pst, schedRpt) != ROK)
422 DU_LOG("\nERROR --> MAC: Failed to send Schedule result report to RLC");
423 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, schedRpt, sizeof(RlcSchedResultRpt));
430 /*******************************************************************
432 * @brief Handles cell start reuqest from DU APP
436 * Function : MacProcCellStart
439 * Handles cell start reuqest from DU APP
441 * @params[in] Post structure pointer
443 * @return ROK - success
446 * ****************************************************************/
447 uint8_t MacProcCellStart(Pst *pst, OduCellId *cellId)
449 DU_LOG("\nINFO --> MAC : Handling cell start request");
451 sendToLowerMac(START_REQUEST, 0, cellId);
453 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellId, \
459 /*******************************************************************
461 * @brief Handles cell stop from DU APP
465 * Function : MacProcCellStop
468 * Handles cell stop from DU APP
470 * @params[in] Post structure pointer
472 * @return ROK - success
475 * ****************************************************************/
476 uint8_t MacProcCellStop(Pst *pst, OduCellId *cellId)
481 DU_LOG("\nINFO --> MAC : Sending cell stop request to Lower Mac");
482 GET_CELL_IDX(cellId->cellId, cellIdx);
483 if(macCb.macCell[cellIdx])
485 macCb.macCell[cellIdx]->state = CELL_TO_BE_STOPPED;
489 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellId, \
495 /*******************************************************************
497 * @brief Handles DL CCCH Ind from DU APP
501 * Function : MacProcDlCcchInd
504 * Handles DL CCCH Ind from DU APP
506 * @params[in] Post structure pointer
507 * DL CCCH Ind pointer
508 * @return ROK - success
511 * ****************************************************************/
512 uint8_t MacProcDlCcchInd(Pst *pst, DlCcchIndInfo *dlCcchIndInfo)
517 DlRlcBoInfo dlBoInfo;
518 memset(&dlBoInfo, 0, sizeof(DlRlcBoInfo));
520 DU_LOG("\nDEBUG --> MAC : Handling DL CCCH IND");
522 GET_CELL_IDX(dlCcchIndInfo->cellId, cellIdx);
524 dlBoInfo.cellId = dlCcchIndInfo->cellId;
525 dlBoInfo.crnti = dlCcchIndInfo->crnti;
527 if(dlCcchIndInfo->msgType == RRC_SETUP)
529 dlBoInfo.lcId = SRB0_LCID; // SRB ID 0 for msg4
530 /* (MSG4 pdu + 3 bytes sub-header) + (Contention resolution id MAC CE + 1 byte sub-header) */
531 dlBoInfo.dataVolume = (dlCcchIndInfo->dlCcchMsgLen + 3) + (MAX_CRI_SIZE + 1);
533 /* storing Msg4 Pdu in raCb */
534 GET_UE_IDX(dlBoInfo.crnti, ueIdx);
536 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == dlCcchIndInfo->crnti)
538 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen = dlCcchIndInfo->dlCcchMsgLen;
539 MAC_ALLOC(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
540 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
541 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu)
543 for(idx = 0; idx < dlCcchIndInfo->dlCcchMsgLen; idx++)
545 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu[idx] =\
546 dlCcchIndInfo->dlCcchMsg[idx];
551 sendDlRlcBoInfoToSch(&dlBoInfo);
553 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo->dlCcchMsg, \
554 dlCcchIndInfo->dlCcchMsgLen);
555 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo, sizeof(DlCcchIndInfo));
560 /*******************************************************************
562 * @brief Sends UL CCCH Ind to DU APP
566 * Function : macProcUlCcchInd
569 * MAC sends UL CCCH Ind to DU APP
571 * @params[in] Post structure pointer
573 * @return ROK - success
576 * ****************************************************************/
577 uint8_t macProcUlCcchInd(uint16_t cellId, uint16_t crnti, uint16_t rrcContSize, uint8_t *rrcContainer)
581 UlCcchIndInfo *ulCcchIndInfo = NULLP;
583 MAC_ALLOC_SHRABL_BUF(ulCcchIndInfo, sizeof(UlCcchIndInfo));
586 DU_LOG("\nERROR --> MAC: Memory failed in macProcUlCcchInd");
590 ulCcchIndInfo->cellId = cellId;
591 ulCcchIndInfo->crnti = crnti;
592 ulCcchIndInfo->ulCcchMsgLen = rrcContSize;
593 ulCcchIndInfo->ulCcchMsg = rrcContainer;
596 FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_UL_CCCH_IND);
598 if(MacDuAppUlCcchInd(&pst, ulCcchIndInfo) != ROK)
600 DU_LOG("\nERROR --> MAC: Failed to send UL CCCH Ind to DU APP");
601 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
602 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
608 /*******************************************************************
610 * @brief Processes received short BSR
614 * Function : macProcShortBsr
617 * MAC sends Short BSR to SCH
619 * @params[in] cell ID
625 * ****************************************************************/
626 uint8_t macProcShortBsr(uint16_t cellId, uint16_t crnti, uint8_t lcgId, uint32_t bufferSize)
629 UlBufferStatusRptInd bsrInd;
631 memset(&pst, 0, sizeof(Pst));
632 memset(&bsrInd, 0, sizeof(UlBufferStatusRptInd));
634 bsrInd.cellId = cellId;
635 bsrInd.crnti = crnti;
636 bsrInd.bsrType = SHORT_BSR;
637 bsrInd.numLcg = 1; /* short bsr reports one lcg at a time */
638 bsrInd.dataVolInfo[0].lcgId = lcgId;
639 bsrInd.dataVolInfo[0].dataVol = bufferSize;
641 FILL_PST_MAC_TO_SCH(pst, EVENT_SHORT_BSR);
642 return(*macSchBsrOpts[pst.selector])(&pst, &bsrInd);
645 /*******************************************************************
647 * @brief Builds and send SR UCI Indication to SCH
651 * Function : buildAndSendSrInd
654 * Builds and send SR UCI Indication to SCH
656 * @params[in] SrUciIndInfo Pointer
658 * @return ROK - success
661 * ****************************************************************/
662 uint8_t buildAndSendSrInd(UciInd *macUciInd, uint8_t crnti)
666 SrUciIndInfo srUciInd;
667 memset(&pst, 0, sizeof(Pst));
668 memset(&srUciInd, 0, sizeof(SrUciIndInfo));
670 GET_CELL_IDX(macUciInd->cellId, cellIdx);
671 srUciInd.cellId = macCb.macCell[cellIdx]->cellId;
672 srUciInd.crnti = crnti;
673 srUciInd.slotInd.sfn = macUciInd->slotInd.sfn;
674 srUciInd.slotInd.slot = macUciInd->slotInd.slot;
675 srUciInd.numSrBits++;
676 memset(srUciInd.srPayload, 0, MAX_SR_BITS_IN_BYTES);
679 FILL_PST_MAC_TO_SCH(pst, EVENT_UCI_IND_TO_SCH);
681 return(*macSchSrUciIndOpts[pst.selector])(&pst, &srUciInd);
684 /*******************************************************************
686 * @brief Processes UCI Indication from PHY
690 * Function : fapiMacUciInd
693 * Processes UCI Indication from PHY
695 * @params[in] Post Structure Pointer
696 * UCI Indication Pointer
697 * @return ROK - success
700 * ****************************************************************/
701 uint8_t FapiMacUciInd(Pst *pst, UciInd *macUciInd)
710 nPdus = macUciInd->numUcis;
713 switch(macUciInd->pdus[pduIdx].pduType)
717 case UCI_IND_PUCCH_F0F1:
718 if(macUciInd->pdus[pduIdx].uci.uciPucchF0F1.srInfo.srIndPres)
720 DU_LOG("\nDEBUG --> MAC : Received SR UCI indication");
721 crnti = macUciInd->pdus[pduIdx].uci.uciPucchF0F1.crnti;
722 ret = buildAndSendSrInd(macUciInd, crnti);
725 case UCI_IND_PUCCH_F2F3F4:
728 DU_LOG("\nERROR --> MAC: Invalid Pdu Type %d at FapiMacUciInd", macUciInd->pdus[pduIdx].pduType);
738 DU_LOG("\nERROR --> MAC: Received Uci Ind is NULL at FapiMacUciInd()");
741 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macUciInd, sizeof(UciInd));
746 /**********************************************************************
748 **********************************************************************/