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"
26 #include "mac_utils.h"
28 /* Function pointer for sending RACH resource response from MAC to DU APP */
29 MacDuRachRsrcRspFunc macDuRachRsrcRspOpts[] =
31 packDuMacRachRsrcRsp, /* packing for loosely coupled */
32 DuProcMacRachRsrcRsp, /* packing for tightly coupled */
33 packDuMacRachRsrcRsp /* packing for light weight loosly coupled */
36 /*******************************************************************
38 * @brief Sends RACH indication to SCH
42 * Function : sendRachIndMacToSch
45 * Sends RACH indication to SCH
47 * @params[in] RACH indication info
48 * @return ROK - success
51 * ****************************************************************/
52 uint8_t sendRachIndMacToSch(RachIndInfo *rachInd)
56 FILL_PST_MAC_TO_SCH(pst, EVENT_RACH_IND_TO_SCH);
57 return(SchMessageRouter(&pst, (void *)rachInd));
60 /*******************************************************************
62 * @brief Adds an entry in list of RA Cb and fills RACH ind info
66 * Function : createMacRaCb
69 * Adds an entry in list of RA Cb and fills RACH ind info
71 * @params[in] Pointer to cellCB,
72 * Pointer to RACH Indication information
73 * @return ROK - SUCCESS
76 * ****************************************************************/
77 uint8_t createMacRaCb(MacCellCb *cellCb, RachIndInfo *rachIndInfo)
82 MacUeCb *ueCb = NULLP;
84 /* Search if a UE CB is already present to which the incoming preamble index was dedicated */
85 for(ueIdx = 0; ueIdx < MAX_NUM_UE; ueIdx++)
87 /* Check if ueCb has a crnti alloted to it. If not, it means this ueIdx
88 * has no UE context stored in it, hence searching for preamble index in
89 * this ueCb is not required */
90 if(cellCb->ueCb[ueIdx].crnti)
92 for(ssbIdx = 0; ssbIdx < cellCb->ueCb[ueIdx].cfraResource.numSsb; ssbIdx++)
94 if(cellCb->ueCb[ueIdx].cfraResource.ssbResource[ssbIdx].raPreambleIdx == rachIndInfo->preambleIdx)
96 ueCb = &cellCb->ueCb[ueIdx];
107 /* If UE context is already present as in case of handover, fetch CRNTI from
108 * UE CB allocated during UE context creation */
113 /* If UE context not present, assign CRNTI to this UE */
114 ueIdx = getFreeBitFromUeBitMap(rachIndInfo->cellId);
117 DU_LOG("\nERROR --> MAC : Failed to find free UE Idx in UE bit map of cell Id [%d]", rachIndInfo->cellId);
121 /* Calculate CRNTI from UE Index */
122 GET_CRNTI(crnti, ueIdx+1);
125 memset(&cellCb->macRaCb[ueIdx], 0, sizeof(MacRaCbInfo));
126 cellCb->macRaCb[ueIdx].cellId = rachIndInfo->cellId;
127 cellCb->macRaCb[ueIdx].crnti = crnti;
129 /* Initialize MSG4 HARQ PROC CB */
130 cellCb->macRaCb[ueIdx].msg4HqInfo.procId = MAX_NUM_HARQ_PROC;
134 /* Store in Rach Indication message to be sent to SCH */
135 rachIndInfo->crnti = crnti;
139 /*******************************************************************
141 * @brief Processes RACH indication from PHY
145 * Function : fapiMacRachInd
148 * Processes RACH indication from PHY
150 * @params[in] Post structure
151 * Rach indication message
152 * @return ROK - success
155 * ****************************************************************/
156 uint8_t fapiMacRachInd(Pst *pst, RachInd *rachInd)
160 uint8_t preambleIdx = 0;
161 uint16_t cellIdx = 0;
162 RachIndInfo *rachIndInfo = NULLP;
163 MacCellCb *cellCb = NULLP;
165 DU_LOG("\nINFO --> MAC : Received RACH indication");
166 /* Considering one pdu and one preamble */
170 /* Validate cell Id */
171 GET_CELL_IDX(rachInd->cellId, cellIdx);
172 if(macCb.macCell[cellIdx] && (macCb.macCell[cellIdx]->cellId == rachInd->cellId))
173 cellCb = macCb.macCell[cellIdx];
177 DU_LOG("\nERROR --> MAC : Invalid Cell ID [%d] received in RACH Indication", rachInd->cellId);
181 MAC_ALLOC(rachIndInfo, sizeof(RachIndInfo));
184 DU_LOG("\nERROR --> MAC : Memory allocation failure in fapiMacRachInd");
185 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rachInd, sizeof(RachInd));
189 rachIndInfo->cellId = rachInd->cellId;
190 rachIndInfo->timingInfo.sfn = rachInd->timingInfo.sfn;
191 rachIndInfo->timingInfo.slot = rachInd->timingInfo.slot;
192 rachIndInfo->slotIdx = rachInd->rachPdu[pduIdx].slotIdx;
193 rachIndInfo->symbolIdx = rachInd->rachPdu[pduIdx].symbolIdx;
194 rachIndInfo->freqIdx = rachInd->rachPdu[pduIdx].freqIdx;
195 rachIndInfo->preambleIdx = rachInd->rachPdu[pduIdx].preamInfo[preambleIdx].preamIdx;
196 rachIndInfo->timingAdv = rachInd->rachPdu[pduIdx].preamInfo[preambleIdx].timingAdv;
198 /* Store the value in macRaCb */
199 if((ret = createMacRaCb(cellCb, rachIndInfo)) == ROK)
201 /* Send RACH Indication to SCH */
202 ret = sendRachIndMacToSch(rachIndInfo);
205 /* Free sharable buffer used to send RACH Indication from lower MAC to MAC */
206 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rachInd, sizeof(RachInd));
211 /*******************************************************************
213 * @brief Processes RACH Resource request from DU APP
217 * Function : MacProcRachRsrcReq
219 * Functionality: Processes RACH resource request from DU APP.
220 * Fills and sends RACH resource request towards SCH.
222 * @params[in] Post structure
223 * RACH resource request
224 * @return ROK - success
227 * ****************************************************************/
228 uint8_t MacProcRachRsrcReq(Pst *pst, MacRachRsrcReq *rachRsrcReq)
230 uint8_t ret = RFAILED;
231 uint16_t cellIdx = 0;
233 MacCellCb *cellCb = NULLP;
234 MacUeCb *ueCb = NULLP;
235 SchRachRsrcReq *schRachRsrcReq = NULLP;
237 DU_LOG("\nINFO --> MAC : Recieved RACH Resource Request for Cell ID [%d] UE ID [%d]",\
238 rachRsrcReq->cellId, rachRsrcReq->ueId);
241 GET_CELL_IDX(rachRsrcReq->cellId, cellIdx);
242 if(macCb.macCell[cellIdx] && (macCb.macCell[cellIdx]->cellId == rachRsrcReq->cellId))
244 cellCb = macCb.macCell[cellIdx];
247 if(cellCb->ueCb[rachRsrcReq->ueId-1].ueId == rachRsrcReq->ueId)
249 ueCb = &cellCb->ueCb[rachRsrcReq->ueId-1];
250 /* Allocate memory to RACH resource request to be sent to SCH */
251 MAC_ALLOC(schRachRsrcReq, sizeof(SchRachRsrcReq));
254 /* Fill SCH RACH resource request from information received from DU APP to MAC */
255 schRachRsrcReq->cellId = rachRsrcReq->cellId;
256 schRachRsrcReq->crnti = ueCb->crnti;
257 schRachRsrcReq->numSsb = rachRsrcReq->numSsb;
258 memcpy(schRachRsrcReq->ssbIdx, rachRsrcReq->ssbIdx, sizeof(schRachRsrcReq->ssbIdx));
260 /* Send RACH resource request from MAC to SCH */
261 FILL_PST_MAC_TO_SCH(schPst, EVENT_RACH_RESOURCE_REQUEST_TO_SCH);
262 ret = SchMessageRouter(&schPst, (void *)schRachRsrcReq);
265 DU_LOG("\nERROR --> MAC : Memory allocation failed for RACH resource request to SCH");
268 DU_LOG("\nERROR --> MAC : UE ID [%d] not found", rachRsrcReq->ueId);
271 DU_LOG("\nERROR --> MAC : Cell ID [%d] not found", rachRsrcReq->cellId);
273 /* Free sharable buffer used to send RACH reource request from DU APP to MAC */
274 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rachRsrcReq, sizeof(MacRachRsrcReq));
278 /*******************************************************************
280 * @brief Processes RACH Resource response from SCH
284 * Function : MacProcSchRachRsrcRsp
286 * Functionality: Processes RACH resource response from SCH
287 * Fills and sends RACH resource response towards DU APP
289 * @params[in] Post structure
290 * RACH resource response
291 * @return ROK - success
294 * ****************************************************************/
295 uint8_t MacProcSchRachRsrcRsp(Pst *pst, SchRachRsrcRsp *schRachRsrcRsp)
297 uint16_t cellIdx = 0;
299 MacRachRsrcRsp *rachRsrcRsp = NULLP;
300 MacCellCb *cellCb = NULLP;
301 MacUeCb *ueCb = NULLP;
303 DU_LOG("\nINFO --> MAC : Received RACH resource response from SCH : Cell ID [%d] CRNTI [%d]", \
304 schRachRsrcRsp->cellId, schRachRsrcRsp->crnti);
306 /* Fill RACH resource response to send to DU APP */
307 MAC_ALLOC_SHRABL_BUF(rachRsrcRsp, sizeof(MacRachRsrcRsp));
310 DU_LOG("\nERROR --> MAC : Memory allocation failed for RACH resource response");
311 MAC_FREE(schRachRsrcRsp, sizeof(SchRachRsrcRsp));
314 rachRsrcRsp->cellId = schRachRsrcRsp->cellId;
315 GET_UE_ID(schRachRsrcRsp->crnti, rachRsrcRsp->ueId);
316 rachRsrcRsp->result = MAC_DU_APP_RSP_OK;
318 /* Fill Pst structure to send RACH resource response from MAC to DU APP */
319 FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_RACH_RESOURCE_RSP);
321 /* If negative response is received from SCH, send it further to DU APP */
322 if(schRachRsrcRsp->result == RSP_NOK)
324 DU_LOG("\nINFO --> MAC : RACH Resource response from SCH : Result [FAILURE]");
325 rachRsrcRsp->result = MAC_DU_APP_RSP_NOK;
329 DU_LOG("\nINFO --> MAC : RACH Resource response from SCH : Result [SUCCESS]");
332 GET_CELL_IDX(schRachRsrcRsp->cellId, cellIdx);
333 if(macCb.macCell[cellIdx] && (macCb.macCell[cellIdx]->cellId == schRachRsrcRsp->cellId))
335 cellCb = macCb.macCell[cellIdx];
338 if(cellCb->ueCb[rachRsrcRsp->ueId-1].crnti == schRachRsrcRsp->crnti)
339 ueCb = &cellCb->ueCb[rachRsrcRsp->ueId-1];
342 DU_LOG("\nERROR --> MAC : CRNTI [%d] not found", schRachRsrcRsp->crnti);
343 rachRsrcRsp->result = MAC_DU_APP_RSP_NOK;
348 DU_LOG("\nERROR --> MAC : Cell ID [%d] not found", schRachRsrcRsp->cellId);
349 rachRsrcRsp->result = MAC_DU_APP_RSP_NOK;
353 /* Fill SSB RACH resource info if SCH has sent a positive response and
354 * processing of SCH RACH resource response at MAC has been successful so far */
355 if(rachRsrcRsp->result == MAC_DU_APP_RSP_OK)
357 rachRsrcRsp->newCrnti = ueCb->crnti;
358 rachRsrcRsp->cfraResource.numSsb = schRachRsrcRsp->cfraResource.numSsb;
359 memcpy(rachRsrcRsp->cfraResource.ssbResource, schRachRsrcRsp->cfraResource.ssbResource, \
360 rachRsrcRsp->cfraResource.numSsb * sizeof(MacCfraSsbResource));
362 /* Copy resources to UE CB in MAC */
363 memcpy(&ueCb->cfraResource, &rachRsrcRsp->cfraResource, sizeof(MacCfraResource));
366 /* Free SCH RACH resource response */
367 MAC_FREE(schRachRsrcRsp, sizeof(SchRachRsrcRsp));
369 /* Send RACH resource response to DU APP */
370 FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_RACH_RESOURCE_RSP);
371 return (*macDuRachRsrcRspOpts[rspPst.selector])(&rspPst, rachRsrcRsp);
375 /*******************************************************************
377 * @brief Processes RACH Resource release from DU APP
381 * Function : MacProcRachRsrcRel
383 * Functionality: Processes RACH resource release from DU APP.
384 * Fills and sends RACH resource release towards SCH.
386 * @params[in] Post structure
387 * RACH resource release
388 * @return ROK - success
391 * ****************************************************************/
392 uint8_t MacProcRachRsrcRel(Pst *pst, MacRachRsrcRel *rachRsrcRel)
394 uint8_t ret = RFAILED;
395 uint16_t cellIdx = 0;
397 MacCellCb *cellCb = NULLP;
398 MacUeCb *ueCb = NULLP;
399 SchRachRsrcRel *schRachRsrcRel = NULLP;
401 DU_LOG("\nINFO --> MAC : Recieved RACH Resource Release for Cell ID [%d] UE ID [%d]",\
402 rachRsrcRel->cellId, rachRsrcRel->ueId);
405 GET_CELL_IDX(rachRsrcRel->cellId, cellIdx);
406 if(macCb.macCell[cellIdx] && (macCb.macCell[cellIdx]->cellId == rachRsrcRel->cellId))
408 cellCb = macCb.macCell[cellIdx];
411 if(cellCb->ueCb[rachRsrcRel->ueId-1].ueId == rachRsrcRel->ueId)
413 ueCb = &cellCb->ueCb[rachRsrcRel->ueId-1];
414 /* Allocate memory to RACH resource release to be sent to SCH */
415 MAC_ALLOC(schRachRsrcRel, sizeof(SchRachRsrcRel));
418 /* Fill SCH RACH resource release from information received from DU APP to MAC */
419 memset(schRachRsrcRel, 0, sizeof(SchRachRsrcRel));
420 schRachRsrcRel->cellId = rachRsrcRel->cellId;
421 schRachRsrcRel->crnti = ueCb->crnti;
422 memcpy(&schRachRsrcRel->cfraResource, &ueCb->cfraResource, sizeof(schRachRsrcRel->cfraResource));
424 /* Release RACH resources at MAC */
425 memset(&ueCb->cfraResource, 0, sizeof(MacCfraResource));
427 /* Send RACH resource release from MAC to SCH */
428 FILL_PST_MAC_TO_SCH(schPst, EVENT_RACH_RESOURCE_RELEASE_TO_SCH);
429 ret = SchMessageRouter(&schPst, (void *)schRachRsrcRel);
432 DU_LOG("\nERROR --> MAC : Memory allocation failed for RACH resource release to SCH");
435 DU_LOG("\nERROR --> MAC : UE ID [%d] not found", rachRsrcRel->ueId);
438 DU_LOG("\nERROR --> MAC : Cell ID [%d] not found", rachRsrcRel->cellId);
440 /* Free sharable buffer used to send RACH reource release from DU APP to MAC */
441 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rachRsrcRel, sizeof(MacRachRsrcRel));
445 /* spec-38.211 Table 6.3.3.1-7 */
446 uint8_t UnrestrictedSetNcsTable[MAX_ZERO_CORR_CFG_IDX] =
447 {0, 2, 4, 6, 8, 10, 12, 13, 15, 17, 19, 23, 27, 34, 46, 69};
449 /*******************************************************************
451 * @brief Processes UL scheduling info from SCH
455 * Function : MacProcUlSchInfo
457 * Functionality: Processes UL scheduling info from SCH
459 * @params[in] Post structure
461 * @return ROK - success
464 * ****************************************************************/
465 uint8_t MacProcUlSchInfo(Pst *pst, UlSchedInfo *ulSchedInfo)
469 #ifdef CALL_FLOW_DEBUG_LOG
470 DU_LOG("\nCall Flow: ENTSCH -> ENTMAC : EVENT_UL_SCH_INFO\n");
473 GET_CELL_IDX(ulSchedInfo->cellId, cellIdx);
474 if(ulSchedInfo != NULLP)
476 MacUlSlot *currUlSlot =
477 &macCb.macCell[cellIdx]->ulSlot[ulSchedInfo->slotIndInfo.slot % macCb.macCell[cellIdx]->numOfSlots];
478 memcpy(&currUlSlot->ulInfo, ulSchedInfo, sizeof(UlSchedInfo));
483 /**********************************************************************
485 **********************************************************************/