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 *******************************************************************************/
18 /* header include files (.h) */
19 #include "common_def.h"
22 #include "du_app_mac_inf.h"
23 #include "mac_sch_interface.h"
24 #include "lwr_mac_upr_inf.h"
25 #include "rlc_mac_inf.h"
27 #include "mac_upr_inf_api.h"
29 #include "lwr_mac_fsm.h"
30 #include "mac_utils.h"
31 #include "mac_harq_dl.h"
32 #include "lwr_mac_phy.h"
35 * @brief process DL allocation from scheduler
39 * Function : MacProcDlAlloc
41 * This function copied dl sch info in the mac slot info
44 * @param[in] DL allocation from scheduler
49 uint8_t MacProcDlAlloc(Pst *pst, DlSchedInfo *dlSchedInfo)
51 uint8_t schInfoIdx = 0, cwIdx = 0;
52 uint8_t ueId = 0, ueIdx = 0;
54 uint8_t *retxTb = NULLP, *txPdu = NULLP;
55 uint16_t txPduLen = 0;
56 MacDlSlot *currDlSlot = NULLP;
57 DlMsgSchInfo schedInfo;
58 DlHarqProcCb *hqProcCb = NULLP;
60 #ifdef CALL_FLOW_DEBUG_LOG
61 DU_LOG("\nCall Flow: ENTSCH -> ENTMAC : EVENT_DL_SCH_INFO\n");
63 if(dlSchedInfo != NULLP)
65 GET_CELL_IDX(dlSchedInfo->cellId, cellIdx);
66 if(dlSchedInfo->isBroadcastPres)
68 currDlSlot = &macCb.macCell[cellIdx]->\
69 dlSlot[dlSchedInfo->schSlotValue.broadcastTime.slot];
70 currDlSlot->dlInfo.isBroadcastPres = true;
71 memcpy(&currDlSlot->dlInfo.brdcstAlloc, &dlSchedInfo->brdcstAlloc, sizeof(DlBrdcstAlloc));
74 for(ueIdx=0; ueIdx<MAX_NUM_UE; ueIdx++)
76 if(dlSchedInfo->rarAlloc[ueIdx] != NULLP)
78 currDlSlot = &macCb.macCell[cellIdx]->dlSlot[dlSchedInfo->schSlotValue.rarTime.slot];
79 currDlSlot->dlInfo.rarAlloc[ueIdx] = dlSchedInfo->rarAlloc[ueIdx];
82 fillRarPdu(&currDlSlot->dlInfo.rarAlloc[ueIdx]->rarInfo);
85 if(dlSchedInfo->dlMsgAlloc[ueIdx] != NULLP)
87 currDlSlot = &macCb.macCell[cellIdx]->\
88 dlSlot[dlSchedInfo->schSlotValue.dlMsgTime.slot];
89 currDlSlot->dlInfo.dlMsgAlloc[ueIdx] = dlSchedInfo->dlMsgAlloc[ueIdx]; /* copy msg4 alloc pointer in MAC slot info */
90 currDlSlot->dlInfo.cellId = dlSchedInfo->cellId;
92 /* Check if the downlink pdu is msg4 */
93 for(schInfoIdx=0; schInfoIdx < dlSchedInfo->dlMsgAlloc[ueIdx]->numSchedInfo; schInfoIdx++)
95 if(dlSchedInfo->dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].dlMsgInfo.isMsg4Pdu)
97 GET_UE_ID(dlSchedInfo->dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].dlMsgInfo.crnti, ueId);
99 schedInfo = dlSchedInfo->dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx];
100 hqProcCb = &macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4HqInfo;
102 if(!dlSchedInfo->dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].isRetx)
104 /* First transmission of MSG4 */
105 hqProcCb->procId = schedInfo.dlMsgInfo.harqProcNum;
106 for(cwIdx = 0; cwIdx < schedInfo.dlMsgPdschCfg.numCodewords; cwIdx++)
108 memcpy(&hqProcCb->tbInfo[hqProcCb->numTb].txTime, &dlSchedInfo->schSlotValue.dlMsgTime, \
109 sizeof(SlotTimingInfo));
110 hqProcCb->tbInfo[hqProcCb->numTb].tbSize = schedInfo.dlMsgPdschCfg.codeword[cwIdx].tbSize;
116 /* MSG4 retransmission */
117 if(hqProcCb->procId == schedInfo.dlMsgInfo.harqProcNum)
119 memcpy(&hqProcCb->tbInfo[0].txTime, &dlSchedInfo->schSlotValue.dlMsgTime, \
120 sizeof(SlotTimingInfo));
126 memcpy(&currDlSlot->dlInfo.schSlotValue, &dlSchedInfo->schSlotValue, sizeof(SchSlotValue));
128 if(!dlSchedInfo->dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].isRetx)
130 /* If new data transmission is scheduled, send schedule results to RLC */
131 if((dlSchedInfo->dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].pduPres == PDSCH_PDU) ||
132 (dlSchedInfo->dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].pduPres == BOTH))
134 sendSchedRptToRlc(currDlSlot->dlInfo, dlSchedInfo->schSlotValue.dlMsgTime, ueIdx, schInfoIdx);
136 /* Add HARQ Proc to DL HARQ Proc Entity in UE */
137 addDlHqProcInUe(currDlSlot->dlInfo.schSlotValue.dlMsgTime, &macCb.macCell[cellIdx]->ueCb[ueIdx], \
138 dlSchedInfo->dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx]);
143 /* For retransmission, fetch PDU to be retransmitted from DL HARQ entity and schedule on corresponding slot */
145 /* As of now this loop will run only once for one TB.
146 * TODO : update handling of fetched TB appropriately when support for two TB is added
149 cwIdx < dlSchedInfo->dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].dlMsgPdschCfg.numCodewords;\
152 /* Fetch TB to be retransmitted */
153 txPduLen = dlSchedInfo->dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].dlMsgPdschCfg.codeword[cwIdx].tbSize;
154 retxTb = fetchTbfromDlHarqProc(currDlSlot->dlInfo.schSlotValue.dlMsgTime, \
155 &macCb.macCell[cellIdx]->ueCb[ueIdx], \
156 dlSchedInfo->dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].dlMsgInfo.harqProcNum, txPduLen);
158 /* Store PDU in corresponding DL slot */
159 MAC_ALLOC(txPdu, txPduLen);
162 DU_LOG("\nERROR --> MAC : Memory allocation failed in MacProcDlAlloc");
165 memcpy(txPdu, retxTb, txPduLen);
167 currDlSlot->dlInfo.dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].dlMsgInfo.dlMsgPduLen = txPduLen;
168 currDlSlot->dlInfo.dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].dlMsgInfo.dlMsgPdu = txPdu;
176 if(dlSchedInfo->ulGrant != NULLP)
178 currDlSlot = &macCb.macCell[cellIdx]->dlSlot[dlSchedInfo->schSlotValue.ulDciTime.slot];
179 currDlSlot->dlInfo.ulGrant = dlSchedInfo->ulGrant;
186 * @brief process DL Paging allocation from scheduler
190 * Function : MacProcDlPageAlloc
192 * This function copied dl Pag info in the mac slot info
194 * @param[in] Pst *pst
195 * @param[in] DL Paging allocation from scheduler
200 uint8_t MacProcDlPageAlloc(Pst *pst, DlPageAlloc *dlPageAlloc)
202 uint16_t cellIdx = 0;
203 MacDlSlot *currDlSlot = NULLP;
205 #ifdef CALL_FLOW_DEBUG_LOG
206 DU_LOG("\nCall Flow: ENTSCH -> ENTMAC : EVENT_DL_PAGING_ALLOC\n");
208 if(dlPageAlloc != NULLP)
210 GET_CELL_IDX(dlPageAlloc->cellId, cellIdx);
212 currDlSlot = &macCb.macCell[cellIdx]->dlSlot[dlPageAlloc->dlPageTime.slot];
213 MAC_ALLOC(currDlSlot->pageAllocInfo, sizeof(DlPageAlloc));
214 if(currDlSlot->pageAllocInfo == NULLP)
216 DU_LOG("\nERROR --> MAC : MacProcDlPageAlloc : Memory Allocation is failed!");
219 memcpy(currDlSlot->pageAllocInfo, dlPageAlloc, sizeof(DlPageAlloc));
220 currDlSlot->pageAllocInfo->pagePdcchCfg.dci.pdschCfg = \
221 &currDlSlot->pageAllocInfo->pagePdschCfg;
225 DU_LOG("\nERROR --> MAC : DL Paging Allocation is failed!");
232 * @brief Forming and filling the MUX Pdu
235 * Function : fillMsg4Pdu
237 * Forming and filling of Msg4Pdu
239 * @param[in] DlMsgAlloc *msg4Alloc
242 void fillMsg4Pdu(uint16_t cellId, DlMsgSchInfo *msg4SchInfo)
244 uint8_t ueId = 0, ueIdx = 0;
246 uint16_t msg4TxPduLen;
247 MacDlData msg4DlData;
249 DlHarqProcCb *hqProcCb;
251 GET_CELL_IDX(cellId, cellIdx);
253 memset(&msg4DlData, 0, sizeof(MacDlData));
254 memset(&macCeData, 0, sizeof(MacCeInfo));
256 GET_UE_ID(msg4SchInfo->dlMsgInfo.crnti, ueId);
259 if(macCb.macCell[cellIdx] == NULLP)
261 DU_LOG("\nERROR --> MAC: Cell Id[%d] not found", cellId);
265 hqProcCb = &macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4HqInfo;
266 msg4TxPduLen = hqProcCb->tbInfo[0].tbSize - TX_PAYLOAD_HDR_LEN;
268 if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu != NULLP)
270 MAC_ALLOC(msg4DlData.pduInfo[msg4DlData.numPdu].dlPdu, macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
271 if(msg4DlData.pduInfo[msg4DlData.numPdu].dlPdu != NULLP)
273 fillMsg4DlData(&msg4DlData, macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen, \
274 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu);
275 fillMacCe(&macCeData, macCb.macCell[cellIdx]->macRaCb[ueIdx].msg3Pdu);
277 /* Forming Mux Pdu */
278 hqProcCb->tbInfo[0].tb = NULLP;
279 MAC_ALLOC(hqProcCb->tbInfo[0].tb, msg4TxPduLen);
280 if(hqProcCb->tbInfo[0].tb != NULLP)
282 memset(hqProcCb->tbInfo[0].tb, 0, msg4TxPduLen);
283 macMuxPdu(&msg4DlData, &macCeData, hqProcCb->tbInfo[0].tb, msg4TxPduLen);
287 DU_LOG("\nERROR --> MAC: Failed allocating memory for msg4TxPdu");
289 /* Free memory allocated */
290 MAC_FREE(msg4DlData.pduInfo[msg4DlData.numPdu-1].dlPdu, macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
291 MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
292 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu = NULLP;
293 macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen = 0;
299 /* storing msg4 Pdu in macDlSlot */
300 if(hqProcCb->tbInfo[0].tb)
302 msg4SchInfo->dlMsgInfo.dlMsgPduLen = msg4TxPduLen;
303 MAC_ALLOC(msg4SchInfo->dlMsgInfo.dlMsgPdu, msg4SchInfo->dlMsgInfo.dlMsgPduLen);
304 if(msg4SchInfo->dlMsgInfo.dlMsgPdu != NULLP)
306 memcpy(msg4SchInfo->dlMsgInfo.dlMsgPdu, hqProcCb->tbInfo[0].tb, \
307 msg4SchInfo->dlMsgInfo.dlMsgPduLen);
312 DU_LOG("\nERROR --> MAC: Failed at fillMsg4Pdu()");
317 * @brief Builds and Send the Muxed Pdu to Lower MAC
321 * Function : buildAndSendMuxPdu
323 * Build and Sends the Muxed Pdu to Lower MAC.
325 * @param[in] SlotTimingInfo *slotInd
329 void buildAndSendMuxPdu(SlotTimingInfo currTimingInfo)
334 MacDlSlot *currDlSlot = NULLP;
335 SlotTimingInfo muxTimingInfo;
336 memset(&muxTimingInfo, 0, sizeof(SlotTimingInfo));
338 GET_CELL_IDX(currTimingInfo.cellId, cellIdx);
340 ADD_DELTA_TO_TIME(currTimingInfo, muxTimingInfo, PHY_DELTA_DL, macCb.macCell[cellIdx]->numOfSlots);
341 currDlSlot = &macCb.macCell[cellIdx]->dlSlot[muxTimingInfo.slot];
343 for(ueIdx=0; ueIdx<MAX_NUM_UE; ueIdx++)
345 if(currDlSlot->dlInfo.dlMsgAlloc[ueIdx])
347 for(schInfoIdx=0; schInfoIdx<currDlSlot->dlInfo.dlMsgAlloc[ueIdx]->numSchedInfo; schInfoIdx++)
349 if((currDlSlot->dlInfo.dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].dlMsgInfo.isMsg4Pdu) &&
350 ((currDlSlot->dlInfo.dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].pduPres == PDSCH_PDU) ||
351 (currDlSlot->dlInfo.dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx].pduPres == BOTH)))
353 fillMsg4Pdu(currTimingInfo.cellId, &currDlSlot->dlInfo.dlMsgAlloc[ueIdx]->dlMsgSchedInfo[schInfoIdx]);
361 * @brief Transmission time interval indication from PHY.
365 * Function : sendSlotIndMacToSch
367 * This API is invoked by MAC to send slot ind to scheduler.
369 * @param[in] SlotTimingInfo *slotInd
374 int sendSlotIndMacToSch(SlotTimingInfo *slotInd)
376 /* fill Pst structure to send to lwr_mac to MAC */
379 FILL_PST_MAC_TO_SCH(pst, EVENT_SLOT_IND_TO_SCH);
380 return(SchMessageRouter(&pst, (void *)slotInd));
383 /*******************************************************************
385 * @brief Send cell up indication to DU APP
389 * Function : sendCellUpIndMacToDuApp
392 * Send cell up indication to DU APP
394 * @params[in] Cell Up indication info
395 * @return ROK - success
398 * ****************************************************************/
399 int sendCellUpIndMacToDuApp(uint16_t cellId)
403 OduCellId *oduCellId;
405 /* Allocate sharable memory */
406 MAC_ALLOC_SHRABL_BUF(oduCellId, sizeof(OduCellId));
409 DU_LOG("\nERROR --> MAC : Memory allocation failed for cell up indication");
412 oduCellId->cellId = cellId;
415 FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_CELL_UP_IND);
417 ret = MacDuAppCellUpInd(&pst, oduCellId);
420 DU_LOG("\nERROR --> MAC: Failed to send cell up indication to DU APP");
421 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, oduCellId, sizeof(OduCellId));
425 } /* sendCellUpIndMacToDuApp */
427 /*******************************************************************
429 * @brief Send slot indication to DU APP
433 * Function : sendSlotIndToDuApp
436 * Send cell up indication to DU APP
438 * @params[in] Cell Up indication info
439 * @return ROK - success
442 * ****************************************************************/
443 uint8_t sendSlotIndToDuApp(SlotTimingInfo *slotInd)
447 SlotTimingInfo *slotIndInfo;
449 /* Allocate sharable memory */
450 MAC_ALLOC_SHRABL_BUF(slotIndInfo, sizeof(SlotTimingInfo));
453 DU_LOG("\nERROR --> MAC : Memory allocation failed for slot indication");
456 memcpy(slotIndInfo, slotInd,sizeof(SlotTimingInfo));
459 FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_SLOT_IND);
461 ret = MacDuAppSlotInd(&pst, slotIndInfo);
464 DU_LOG("\nERROR --> MAC: Failed to send slot up indication to DU APP");
465 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, slotIndInfo, sizeof(SlotTimingInfo));
470 /*******************************************************************
472 * @brief Process slot indication at MAC
476 * Function : macProcSlotInd
478 * Functionality: Process slot indication at MAC
480 * @params[in] Slot indication info
481 * @return ROK - success
484 * ****************************************************************/
485 uint8_t macProcSlotInd(SlotTimingInfo slotInd)
487 uint16_t cellIdx = 0;
489 GET_CELL_IDX(slotInd.cellId, cellIdx);
491 if(macCb.macCell[cellIdx] == NULLP)
493 DU_LOG("ERROR --> MAC : macProcSlotInd(): CellId[%d] does not exist. Error occurred at SFN [%d] Slot [%d]",\
494 slotInd.cellId, slotInd.sfn, slotInd.slot);
497 /* Store current time info */
498 macCb.macCell[cellIdx]->currTime.cellId = slotInd.cellId;
499 macCb.macCell[cellIdx]->currTime.slot = slotInd.slot;
500 macCb.macCell[cellIdx]->currTime.sfn = slotInd.sfn;
502 /* Mux Pdu for Msg4 */
503 buildAndSendMuxPdu(slotInd);
505 /* Trigger for DL TTI REQ */
506 fillDlTtiReq(slotInd);
509 } /* macProcSlotInd */
512 * @brief Transmission time interval indication from PHY.
516 * Function : fapiMacSlotInd
518 * This API is invoked by PHY to indicate TTI indication to MAC for a cell.
520 * @param[in] Pst *pst
521 * @param[in] SuId suId
522 * @param[in] SlotTimingInfo *slotInd
527 uint8_t fapiMacSlotInd(Pst *pst, SlotTimingInfo *slotInd)
531 volatile uint32_t startTime=0;
533 #ifdef ODU_SLOT_IND_DEBUG_LOG
534 DU_LOG("\nDEBUG --> MAC : Slot Indication received. [%d : %d]", slotInd->sfn, slotInd->slot);
537 ODU_START_TASK(&startTime, PID_MAC_TTI_IND);
542 GET_CELL_IDX(slotInd->cellId, cellIdx);
543 macCb.macCell[cellIdx]->state = CELL_STATE_UP;
546 /* When testing L2 with Intel-L1, any changes specific to
547 * timer mode testing must be guarded under INTEL_TIMER_MODE*/
548 #ifndef INTEL_TIMER_MODE
549 /* send slot indication to scheduler */
550 ret = sendSlotIndMacToSch(slotInd);
553 DU_LOG("\nERROR --> MAC : Sending of slot ind msg from MAC to SCH failed");
554 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, slotInd, sizeof(SlotTimingInfo));
558 ret = macProcSlotInd(*slotInd);
561 DU_LOG("\nERROR --> MAC : macProcSlotInd failed");
562 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, slotInd, sizeof(SlotTimingInfo));
567 /*First Slot Ind is for CellUp. Any other Slot, will be notified to DUAPP as
571 /* send cell up indication to du app */
572 ret = sendCellUpIndMacToDuApp(slotInd->cellId);
577 /* send slot indication to du app */
578 ret = sendSlotIndToDuApp(slotInd);
583 DU_LOG("\nERROR --> MAC :Sending of slot ind msg from MAC to DU APP failed");
584 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, slotInd, sizeof(SlotTimingInfo));
589 ODU_STOP_TASK(startTime, PID_MAC_TTI_IND);
590 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, slotInd, sizeof(SlotTimingInfo));
593 lwrMacCb.phySlotIndCntr++;
594 if(lwrMacCb.phySlotIndCntr > WLS_MEM_FREE_PRD)
596 lwrMacCb.phySlotIndCntr = 1;
598 freeWlsBlockList(lwrMacCb.phySlotIndCntr - 1);
602 } /* fapiMacSlotInd */
604 /**********************************************************************
606 **********************************************************************/