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 pdschSlot = 0;
213 uint8_t schInfoIdx = 0 ;
214 uint16_t cellIdx = 0, txPduLen = 0;
216 MacDlSlot *currDlSlot = NULLP;
217 DlRlcBoInfo dlBoInfo;
219 memset(&macDlData , 0, sizeof(MacDlData));
220 DU_LOG("\nDEBUG --> MAC: Received DL data for sfn=%d slot=%d numPdu= %d", \
221 dlData->slotInfo.sfn, dlData->slotInfo.slot, dlData->numPdu);
223 GET_UE_IDX(dlData->rnti, ueId);
225 /* Copy the pdus to be muxed into mac Dl data */
226 macDlData.ueId = ueId;
227 macDlData.numPdu = dlData->numPdu;
228 for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
230 macDlData.pduInfo[pduIdx].lcId = dlData->pduInfo[pduIdx].lcId;
231 macDlData.pduInfo[pduIdx].pduLen = dlData->pduInfo[pduIdx].pduLen;
232 macDlData.pduInfo[pduIdx].dlPdu = dlData->pduInfo[pduIdx].pduBuf;
235 GET_CELL_IDX(dlData->cellId, cellIdx);
236 /* Store DL data in the scheduled slot */
237 if(macCb.macCell[cellIdx] ==NULLP)
239 DU_LOG("\nERROR --> MAC : MacProcRlcDlData(): macCell does not exists");
242 currDlSlot = &macCb.macCell[cellIdx]->dlSlot[dlData->slotInfo.slot];
243 if(currDlSlot->dlInfo.dlMsgAlloc[ueId-1])
245 for(schInfoIdx=0; schInfoIdx<currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->numSchedInfo; schInfoIdx++)
247 if((currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgSchedInfo[schInfoIdx].pduPres == PDSCH_PDU) ||
248 (currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgSchedInfo[schInfoIdx].pduPres == BOTH))
252 txPduLen = currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgSchedInfo[schInfoIdx].dlMsgPdschCfg.codeword[0].tbSize\
253 - TX_PAYLOAD_HDR_LEN;
254 MAC_ALLOC(txPdu, txPduLen);
257 DU_LOG("\nERROR --> MAC : Memory allocation failed in MacProcRlcDlData");
260 macMuxPdu(&macDlData, NULLP, txPdu, txPduLen);
262 currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgSchedInfo[schInfoIdx].dlMsgInfo.dlMsgPduLen = txPduLen;
263 currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgSchedInfo[schInfoIdx].dlMsgInfo.dlMsgPdu = txPdu;
266 for(lcIdx = 0; lcIdx < dlData->numLc; lcIdx++)
268 if(dlData->boStatus[lcIdx].bo)
270 memset(&dlBoInfo, 0, sizeof(DlRlcBoInfo));
271 dlBoInfo.cellId = dlData->boStatus[lcIdx].cellId;
272 GET_CRNTI(dlBoInfo.crnti, dlData->boStatus[lcIdx].ueIdx);
273 dlBoInfo.lcId = dlData->boStatus[lcIdx].lcId;
274 dlBoInfo.dataVolume = dlData->boStatus[lcIdx].bo;
275 sendDlRlcBoInfoToSch(&dlBoInfo);
280 for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
282 MAC_FREE_SHRABL_BUF(pstInfo->region, pstInfo->pool, dlData->pduInfo[pduIdx].pduBuf,\
283 dlData->pduInfo[pduIdx].pduLen);
285 if(pstInfo->selector == ODU_SELECTOR_LWLC)
287 MAC_FREE_SHRABL_BUF(pstInfo->region, pstInfo->pool, dlData, sizeof(RlcData));
292 /*******************************************************************
294 * @brief Builds and Sends UL Data to RLC
298 * Function : macProcUlData
300 * Functionality: Builds and Sends UL Data to RLC
305 * LC Id on which payload was received
306 * Pointer to the payload
308 * @return ROK - success
311 * ****************************************************************/
312 uint8_t macProcUlData(uint16_t cellId, uint16_t rnti, SlotTimingInfo slotInfo, \
313 uint8_t lcId, uint16_t pduLen, uint8_t *pdu)
318 /* Filling RLC Ul Data*/
319 MAC_ALLOC_SHRABL_BUF(ulData, sizeof(RlcData));
322 DU_LOG("\nERROR --> MAC : Memory allocation failed while sending UL data to RLC");
325 memset(ulData, 0, sizeof(RlcData));
326 ulData->cellId = cellId;
328 memcpy(&ulData->slotInfo, &slotInfo, sizeof(SlotTimingInfo));
329 ulData->slotInfo.cellId = cellId;
331 /* Filling pdu info */
332 if(lcId == SRB1_LCID || lcId == SRB2_LCID)
334 ulData->pduInfo[ulData->numPdu].commCh = true;
336 ulData->pduInfo[ulData->numPdu].lcId = lcId;
337 ulData->pduInfo[ulData->numPdu].pduBuf = pdu;
338 ulData->pduInfo[ulData->numPdu].pduLen = pduLen;
341 /* Filling Post and send to RLC */
342 memset(&pst, 0, sizeof(Pst));
343 FILL_PST_MAC_TO_RLC(pst, 0, EVENT_UL_DATA_TO_RLC);
344 MacSendUlDataToRlc(&pst, ulData);
350 /*******************************************************************
352 * @brief Processes BO status from RLC
356 * Function : MacProcRlcBOStatus
359 * Processes BO status from RLC
361 * @params[in] Post structure
363 * @return ROK - success
366 * ****************************************************************/
367 uint8_t MacProcRlcBoStatus(Pst* pst, RlcBoStatus* boStatus)
369 DlRlcBoInfo dlBoInfo;
371 dlBoInfo.cellId = boStatus->cellId;
372 GET_CRNTI(dlBoInfo.crnti, boStatus->ueIdx);
373 dlBoInfo.lcId = boStatus->lcId;
374 dlBoInfo.dataVolume = boStatus->bo;
376 sendDlRlcBoInfoToSch(&dlBoInfo);
378 if(pst->selector == ODU_SELECTOR_LWLC)
380 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, boStatus, sizeof(RlcBoStatus));
386 /*******************************************************************
388 * @brief Send LC schedule result report to RLC
392 * Function : sendSchRptToRlc
394 * Functionality: Send LC schedule result report to RLC
397 * @return ROK - success
400 * ****************************************************************/
401 uint8_t sendSchedRptToRlc(DlSchedInfo dlInfo, SlotTimingInfo slotInfo, uint8_t ueIdx, uint8_t schInfoIdx)
405 RlcSchedResultRpt *schedRpt = NULLP;
407 MAC_ALLOC_SHRABL_BUF(schedRpt, sizeof(RlcSchedResultRpt));
410 DU_LOG("\nERROR --> MAC: Memory allocation failure in sendSchResultRepToRlc");
414 DU_LOG("\nDEBUG --> MAC: Send scheduled result report for sfn %d slot %d", slotInfo.sfn, slotInfo.slot);
415 schedRpt->cellId = dlInfo.cellId;
416 schedRpt->slotInfo.sfn = slotInfo.sfn;
417 schedRpt->slotInfo.slot = slotInfo.slot;
419 if(dlInfo.dlMsgAlloc[ueIdx])
421 schedRpt->rnti = dlInfo.dlMsgAlloc[ueIdx]->crnti;
422 schedRpt->numLc = dlInfo.dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].numLc;
423 for(lcIdx = 0; lcIdx < schedRpt->numLc; lcIdx++)
425 schedRpt->lcSch[lcIdx].lcId = dlInfo.dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].lcSchInfo[lcIdx].lcId;
426 schedRpt->lcSch[lcIdx].bufSize = dlInfo.dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].lcSchInfo[lcIdx].schBytes;
427 schedRpt->lcSch[lcIdx].commCh = false;
432 FILL_PST_MAC_TO_RLC(pst, RLC_DL_INST, EVENT_SCHED_RESULT_TO_RLC);
433 if(MacSendSchedResultRptToRlc(&pst, schedRpt) != ROK)
435 DU_LOG("\nERROR --> MAC: Failed to send Schedule result report to RLC");
436 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, schedRpt, sizeof(RlcSchedResultRpt));
443 /*******************************************************************
445 * @brief Handles cell start reuqest from DU APP
449 * Function : MacProcCellStart
452 * Handles cell start reuqest from DU APP
454 * @params[in] Post structure pointer
456 * @return ROK - success
459 * ****************************************************************/
460 uint8_t MacProcCellStart(Pst *pst, OduCellId *cellId)
462 DU_LOG("\nINFO --> MAC : Handling cell start request");
464 sendToLowerMac(START_REQUEST, 0, cellId);
466 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellId, \
472 /*******************************************************************
474 * @brief Handles cell stop from DU APP
478 * Function : MacProcCellStop
481 * Handles cell stop from DU APP
483 * @params[in] Post structure pointer
485 * @return ROK - success
488 * ****************************************************************/
489 uint8_t MacProcCellStop(Pst *pst, OduCellId *cellId)
494 DU_LOG("\nINFO --> MAC : Sending cell stop request to Lower Mac");
495 GET_CELL_IDX(cellId->cellId, cellIdx);
496 if(macCb.macCell[cellIdx])
498 macCb.macCell[cellIdx]->state = CELL_TO_BE_STOPPED;
502 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellId, \
508 /*******************************************************************
510 * @brief Handles DL CCCH Ind from DU APP
514 * Function : MacProcDlCcchInd
517 * Handles DL CCCH Ind from DU APP
519 * @params[in] Post structure pointer
520 * DL CCCH Ind pointer
521 * @return ROK - success
524 * ****************************************************************/
525 uint8_t MacProcDlCcchInd(Pst *pst, DlCcchIndInfo *dlCcchIndInfo)
530 DlRlcBoInfo dlBoInfo;
531 memset(&dlBoInfo, 0, sizeof(DlRlcBoInfo));
533 DU_LOG("\nDEBUG --> MAC : Handling DL CCCH IND");
535 GET_CELL_IDX(dlCcchIndInfo->cellId, cellIdx);
537 dlBoInfo.cellId = dlCcchIndInfo->cellId;
538 dlBoInfo.crnti = dlCcchIndInfo->crnti;
540 if(dlCcchIndInfo->msgType == RRC_SETUP)
542 dlBoInfo.lcId = SRB0_LCID; // SRB ID 0 for msg4
543 /* (MSG4 pdu + 3 bytes sub-header) + (Contention resolution id MAC CE + 1 byte sub-header) */
544 dlBoInfo.dataVolume = (dlCcchIndInfo->dlCcchMsgLen + 3) + (MAX_CRI_SIZE + 1);
546 /* storing Msg4 Pdu in raCb */
547 GET_UE_IDX(dlBoInfo.crnti, ueIdx);
549 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == dlCcchIndInfo->crnti)
551 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen = dlCcchIndInfo->dlCcchMsgLen;
552 MAC_ALLOC(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
553 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
554 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu)
556 for(idx = 0; idx < dlCcchIndInfo->dlCcchMsgLen; idx++)
558 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu[idx] =\
559 dlCcchIndInfo->dlCcchMsg[idx];
564 sendDlRlcBoInfoToSch(&dlBoInfo);
566 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo->dlCcchMsg, \
567 dlCcchIndInfo->dlCcchMsgLen);
568 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo, sizeof(DlCcchIndInfo));
573 /*******************************************************************
575 * @brief Sends UL CCCH Ind to DU APP
579 * Function : macProcUlCcchInd
582 * MAC sends UL CCCH Ind to DU APP
584 * @params[in] Post structure pointer
586 * @return ROK - success
589 * ****************************************************************/
590 uint8_t macProcUlCcchInd(uint16_t cellId, uint16_t crnti, uint16_t rrcContSize, uint8_t *rrcContainer)
594 UlCcchIndInfo *ulCcchIndInfo = NULLP;
596 MAC_ALLOC_SHRABL_BUF(ulCcchIndInfo, sizeof(UlCcchIndInfo));
599 DU_LOG("\nERROR --> MAC: Memory failed in macProcUlCcchInd");
603 ulCcchIndInfo->cellId = cellId;
604 ulCcchIndInfo->crnti = crnti;
605 ulCcchIndInfo->ulCcchMsgLen = rrcContSize;
606 ulCcchIndInfo->ulCcchMsg = rrcContainer;
609 FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_UL_CCCH_IND);
611 if(MacDuAppUlCcchInd(&pst, ulCcchIndInfo) != ROK)
613 DU_LOG("\nERROR --> MAC: Failed to send UL CCCH Ind to DU APP");
614 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
615 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
621 /*******************************************************************
623 * @brief Processes received short BSR
627 * Function : macProcShortBsr
630 * MAC sends Short BSR to SCH
632 * @params[in] cell ID
638 * ****************************************************************/
639 uint8_t macProcShortBsr(uint16_t cellId, uint16_t crnti, uint8_t lcgId, uint32_t bufferSize)
642 UlBufferStatusRptInd bsrInd;
644 memset(&pst, 0, sizeof(Pst));
645 memset(&bsrInd, 0, sizeof(UlBufferStatusRptInd));
647 bsrInd.cellId = cellId;
648 bsrInd.crnti = crnti;
649 bsrInd.bsrType = SHORT_BSR;
650 bsrInd.numLcg = 1; /* short bsr reports one lcg at a time */
651 bsrInd.dataVolInfo[0].lcgId = lcgId;
652 bsrInd.dataVolInfo[0].dataVol = bufferSize;
654 FILL_PST_MAC_TO_SCH(pst, EVENT_SHORT_BSR);
655 return(*macSchBsrOpts[pst.selector])(&pst, &bsrInd);
658 /*******************************************************************
660 * @brief Builds and send SR UCI Indication to SCH
664 * Function : buildAndSendSrInd
667 * Builds and send SR UCI Indication to SCH
669 * @params[in] SrUciIndInfo Pointer
671 * @return ROK - success
674 * ****************************************************************/
675 uint8_t buildAndSendSrInd(UciInd *macUciInd, uint8_t crnti)
679 SrUciIndInfo srUciInd;
680 memset(&pst, 0, sizeof(Pst));
681 memset(&srUciInd, 0, sizeof(SrUciIndInfo));
683 GET_CELL_IDX(macUciInd->cellId, cellIdx);
684 srUciInd.cellId = macCb.macCell[cellIdx]->cellId;
685 srUciInd.crnti = crnti;
686 srUciInd.slotInd.sfn = macUciInd->slotInd.sfn;
687 srUciInd.slotInd.slot = macUciInd->slotInd.slot;
688 srUciInd.numSrBits++;
689 memset(srUciInd.srPayload, 0, MAX_SR_BITS_IN_BYTES);
692 FILL_PST_MAC_TO_SCH(pst, EVENT_UCI_IND_TO_SCH);
694 return(*macSchSrUciIndOpts[pst.selector])(&pst, &srUciInd);
697 /*******************************************************************
699 * @brief Processes UCI Indication from PHY
703 * Function : fapiMacUciInd
706 * Processes UCI Indication from PHY
708 * @params[in] Post Structure Pointer
709 * UCI Indication Pointer
710 * @return ROK - success
713 * ****************************************************************/
714 uint8_t FapiMacUciInd(Pst *pst, UciInd *macUciInd)
723 nPdus = macUciInd->numUcis;
726 switch(macUciInd->pdus[pduIdx].pduType)
730 case UCI_IND_PUCCH_F0F1:
731 if(macUciInd->pdus[pduIdx].uci.uciPucchF0F1.srInfo.srIndPres)
733 DU_LOG("\nDEBUG --> MAC : Received SR UCI indication");
734 crnti = macUciInd->pdus[pduIdx].uci.uciPucchF0F1.crnti;
735 ret = buildAndSendSrInd(macUciInd, crnti);
738 case UCI_IND_PUCCH_F2F3F4:
741 DU_LOG("\nERROR --> MAC: Invalid Pdu Type %d at FapiMacUciInd", macUciInd->pdus[pduIdx].pduType);
751 DU_LOG("\nERROR --> MAC: Received Uci Ind is NULL at FapiMacUciInd()");
754 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macUciInd, sizeof(UciInd));
759 /**********************************************************************
761 **********************************************************************/