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 ind from MAC to SCH */
29 MacSchRachIndFunc macSchRachIndOpts[]=
31 packMacSchRachInd, /* packing for loosely coupled */
32 MacSchRachInd, /* packing for tightly coupled */
33 packMacSchRachInd /* packing for light weight loosely coupled */
36 /* Function pointer for sending RACH resource request from MAC to SCH */
37 MacSchRachRsrcReqFunc macSchRachRsrcReqOpts[] =
39 packMacSchRachRsrcReq, /* packing for loosely coupled */
40 MacSchRachRsrcReq, /* packing for tightly coupled */
41 packMacSchRachRsrcReq /* packing for light weight loosely coupled */
44 /* Function pointer for sending RACH resource response from MAC to DU APP */
45 MacDuRachRsrcRspFunc macDuRachRsrcRspOpts[] =
47 packDuMacRachRsrcRsp, /* packing for loosely coupled */
48 DuProcMacRachRsrcRsp, /* packing for tightly coupled */
49 packDuMacRachRsrcRsp /* packing for light weight loosly coupled */
52 /*******************************************************************
54 * @brief Sends RACH indication to SCH
58 * Function : sendRachIndMacToSch
61 * Sends RACH indication to SCH
63 * @params[in] RACH indication info
64 * @return ROK - success
67 * ****************************************************************/
68 uint8_t sendRachIndMacToSch(RachIndInfo *rachInd)
72 FILL_PST_MAC_TO_SCH(pst, EVENT_RACH_IND_TO_SCH);
73 return(*macSchRachIndOpts[pst.selector])(&pst, rachInd);
76 /*******************************************************************
78 * @brief Adds an entry in list of RA Cb and fills RACH ind info
82 * Function : createMacRaCb
85 * Adds an entry in list of RA Cb and fills RACH ind info
87 * @params[in] Pointer to cellCB,
88 * Pointer to RACH Indication information
89 * @return ROK - SUCCESS
92 * ****************************************************************/
93 uint8_t createMacRaCb(MacCellCb *cellCb, RachIndInfo *rachIndInfo)
98 MacUeCb *ueCb = NULLP;
100 /* Search if a UE CB is already present to which the incoming preamble index was dedicated */
101 for(ueIdx = 0; ueIdx < MAX_NUM_UE; ueIdx++)
103 /* Check if ueCb has a crnti alloted to it. If not, it means this ueIdx
104 * has no UE context stored in it, hence searching for preamble index in
105 * this ueCb is not required */
106 if(cellCb->ueCb[ueIdx].crnti)
108 for(ssbIdx = 0; ssbIdx < cellCb->ueCb[ueIdx].cfraResource.numSsb; ssbIdx++)
110 if(cellCb->ueCb[ueIdx].cfraResource.ssbResource[ssbIdx].raPreambleIdx == rachIndInfo->preambleIdx)
112 ueCb = &cellCb->ueCb[ueIdx];
123 /* If UE context is already present as in case of handover, fetch CRNTI from
124 * UE CB allocated during UE context creation */
129 /* If UE context not present, assign CRNTI to this UE */
130 ueIdx = getFreeBitFromUeBitMap(rachIndInfo->cellId);
133 DU_LOG("\nERROR --> MAC : Failed to find free UE Idx in UE bit map of cell Id [%d]", rachIndInfo->cellId);
137 /* Calculate CRNTI from UE Index */
138 GET_CRNTI(crnti, ueIdx+1);
141 cellCb->macRaCb[ueIdx].cellId = rachIndInfo->cellId;
142 cellCb->macRaCb[ueIdx].crnti = crnti;
145 /* Store in Rach Indication message to be sent to SCH */
146 rachIndInfo->crnti = crnti;
150 /*******************************************************************
152 * @brief Processes RACH indication from PHY
156 * Function : fapiMacRachInd
159 * Processes RACH indication from PHY
161 * @params[in] Post structure
162 * Rach indication message
163 * @return ROK - success
166 * ****************************************************************/
167 uint8_t fapiMacRachInd(Pst *pst, RachInd *rachInd)
171 uint8_t preambleIdx = 0;
172 uint16_t cellIdx = 0;
173 RachIndInfo *rachIndInfo = NULLP;
174 MacCellCb *cellCb = NULLP;
176 DU_LOG("\nINFO --> MAC : Received RACH indication");
177 /* Considering one pdu and one preamble */
181 /* Validate cell Id */
182 GET_CELL_IDX(rachInd->cellId, cellIdx);
183 if(macCb.macCell[cellIdx] && (macCb.macCell[cellIdx]->cellId == rachInd->cellId))
184 cellCb = macCb.macCell[cellIdx];
188 DU_LOG("\nERROR --> MAC : Invalid Cell ID [%d] received in RACH Indication", rachInd->cellId);
192 MAC_ALLOC(rachIndInfo, sizeof(RachIndInfo));
195 DU_LOG("\nERROR --> MAC : Memory allocation failure in fapiMacRachInd");
196 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rachInd, sizeof(RachInd));
200 rachIndInfo->cellId = rachInd->cellId;
201 rachIndInfo->timingInfo.sfn = rachInd->timingInfo.sfn;
202 rachIndInfo->timingInfo.slot = rachInd->timingInfo.slot;
203 rachIndInfo->slotIdx = rachInd->rachPdu[pduIdx].slotIdx;
204 rachIndInfo->symbolIdx = rachInd->rachPdu[pduIdx].symbolIdx;
205 rachIndInfo->freqIdx = rachInd->rachPdu[pduIdx].freqIdx;
206 rachIndInfo->preambleIdx = rachInd->rachPdu[pduIdx].preamInfo[preambleIdx].preamIdx;
207 rachIndInfo->timingAdv = rachInd->rachPdu[pduIdx].preamInfo[preambleIdx].timingAdv;
209 /* Store the value in macRaCb */
210 if((ret = createMacRaCb(cellCb, rachIndInfo)) == ROK)
212 /* Send RACH Indication to SCH */
213 ret = sendRachIndMacToSch(rachIndInfo);
216 /* Free sharable buffer used to send RACH Indication from lower MAC to MAC */
217 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rachInd, sizeof(RachInd));
222 /*******************************************************************
224 * @brief Processes RACH Resource request from DU APP
228 * Function : MacProcRachRsrcReq
230 * Functionality: Processes RACH resource request from DU APP.
231 * Fills and sends RACH resource request towards SCH.
233 * @params[in] Post structure
234 * RACH resource request
235 * @return ROK - success
238 * ****************************************************************/
239 uint8_t MacProcRachRsrcReq(Pst *pst, MacRachRsrcReq *rachRsrcReq)
241 uint8_t ret = RFAILED;
242 uint16_t cellIdx = 0;
244 MacCellCb *cellCb = NULLP;
245 MacUeCb *ueCb = NULLP;
246 SchRachRsrcReq *schRachRsrcReq = NULLP;
248 DU_LOG("\nINFO --> MAC : Recieved RACH Resource Request for Cell ID [%d] UE ID [%d]",\
249 rachRsrcReq->cellId, rachRsrcReq->ueId);
252 GET_CELL_IDX(rachRsrcReq->cellId, cellIdx);
253 if(macCb.macCell[cellIdx] && (macCb.macCell[cellIdx]->cellId == rachRsrcReq->cellId))
255 cellCb = macCb.macCell[cellIdx];
258 if(cellCb->ueCb[rachRsrcReq->ueId-1].ueId == rachRsrcReq->ueId)
260 ueCb = &cellCb->ueCb[rachRsrcReq->ueId-1];
261 /* Allocate memory to RACH resource request to be sent to SCH */
262 MAC_ALLOC(schRachRsrcReq, sizeof(SchRachRsrcReq));
265 /* Fill SCH RACH resource request from information received from DU APP to MAC */
266 schRachRsrcReq->cellId = rachRsrcReq->cellId;
267 schRachRsrcReq->crnti = ueCb->crnti;
268 schRachRsrcReq->numSsb = rachRsrcReq->numSsb;
269 memcpy(schRachRsrcReq->ssbIdx, rachRsrcReq->ssbIdx, sizeof(schRachRsrcReq->ssbIdx));
271 /* Send RACH resource request from MAC to SCH */
272 FILL_PST_MAC_TO_SCH(schPst, EVENT_RACH_RESOURCE_REQUEST_TO_SCH);
273 ret = (*macSchRachRsrcReqOpts[schPst.selector])(&schPst, schRachRsrcReq);
276 DU_LOG("\nERROR --> MAC : Memory allocation failed for RACH resource request to SCH");
279 DU_LOG("\nERROR --> MAC : UE ID [%d] not found", rachRsrcReq->ueId);
282 DU_LOG("\nERROR --> MAC : Cell ID [%d] not found", rachRsrcReq->cellId);
284 /* Free sharable buffer used to send RACH reource request from DU APP to MAC */
285 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rachRsrcReq, sizeof(MacRachRsrcReq));
289 /*******************************************************************
291 * @brief Processes RACH Resource response from SCH
295 * Function : MacProcSchRachRsrcRsp
297 * Functionality: Processes RACH resource response from SCH
298 * Fills and sends RACH resource response towards DU APP
300 * @params[in] Post structure
301 * RACH resource response
302 * @return ROK - success
305 * ****************************************************************/
306 uint8_t MacProcSchRachRsrcRsp(Pst *pst, SchRachRsrcRsp *schRachRsrcRsp)
308 uint16_t cellIdx = 0;
310 MacRachRsrcRsp *rachRsrcRsp = NULLP;
311 MacCellCb *cellCb = NULLP;
312 MacUeCb *ueCb = NULLP;
314 DU_LOG("\nINFO --> MAC : Received RACH resource response from SCH : Cell ID [%d] CRNTI [%d]", \
315 schRachRsrcRsp->cellId, schRachRsrcRsp->crnti);
317 /* Fill RACH resource response to send to DU APP */
318 MAC_ALLOC_SHRABL_BUF(rachRsrcRsp, sizeof(MacRachRsrcRsp));
321 DU_LOG("\nERROR --> MAC : Memory allocation failed for RACH resource response");
322 MAC_FREE(schRachRsrcRsp, sizeof(SchRachRsrcRsp));
325 rachRsrcRsp->cellId = schRachRsrcRsp->cellId;
326 GET_UE_ID(schRachRsrcRsp->crnti, rachRsrcRsp->ueId);
327 rachRsrcRsp->result = MAC_DU_APP_RSP_OK;
329 /* Fill Pst structure to send RACH resource response from MAC to DU APP */
330 FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_RACH_RESOURCE_RSP);
332 /* If negative response is received from SCH, send it further to DU APP */
333 if(schRachRsrcRsp->result == RSP_NOK)
335 DU_LOG("\nINFO --> MAC : RACH Resource response from SCH : Result [FAILURE]");
336 rachRsrcRsp->result = MAC_DU_APP_RSP_NOK;
340 DU_LOG("\nINFO --> MAC : RACH Resource response from SCH : Result [SUCCESS]");
343 GET_CELL_IDX(schRachRsrcRsp->cellId, cellIdx);
344 if(macCb.macCell[cellIdx] && (macCb.macCell[cellIdx]->cellId == schRachRsrcRsp->cellId))
346 cellCb = macCb.macCell[cellIdx];
349 if(cellCb->ueCb[rachRsrcRsp->ueId-1].crnti == schRachRsrcRsp->crnti)
350 ueCb = &cellCb->ueCb[rachRsrcRsp->ueId-1];
353 DU_LOG("\nERROR --> MAC : CRNTI [%d] not found", schRachRsrcRsp->crnti);
354 rachRsrcRsp->result = MAC_DU_APP_RSP_NOK;
359 DU_LOG("\nERROR --> MAC : Cell ID [%d] not found", schRachRsrcRsp->cellId);
360 rachRsrcRsp->result = MAC_DU_APP_RSP_NOK;
364 /* Fill SSB RACH resource info if SCH has sent a positive response and
365 * processing of SCH RACH resource response at MAC has been successful so far */
366 if(rachRsrcRsp->result == MAC_DU_APP_RSP_OK)
368 rachRsrcRsp->newCrnti = ueCb->crnti;
369 rachRsrcRsp->cfraResource.numSsb = schRachRsrcRsp->cfraResource.numSsb;
370 memcpy(rachRsrcRsp->cfraResource.ssbResource, schRachRsrcRsp->cfraResource.ssbResource, \
371 rachRsrcRsp->cfraResource.numSsb * sizeof(MacCfraSsbResource));
373 /* Copy resources to UE CB in MAC */
374 memcpy(&ueCb->cfraResource, &rachRsrcRsp->cfraResource, sizeof(MacCfraResource));
377 /* Free SCH RACH resource response */
378 MAC_FREE(schRachRsrcRsp, sizeof(SchRachRsrcRsp));
380 /* Send RACH resource response to DU APP */
381 FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_RACH_RESOURCE_RSP);
382 return (*macDuRachRsrcRspOpts[rspPst.selector])(&rspPst, rachRsrcRsp);
385 /* spec-38.211 Table 6.3.3.1-7 */
386 uint8_t UnrestrictedSetNcsTable[MAX_ZERO_CORR_CFG_IDX] =
387 {0, 2, 4, 6, 8, 10, 12, 13, 15, 17, 19, 23, 27, 34, 46, 69};
389 /*******************************************************************
391 * @brief Processes UL scheduling info from SCH
395 * Function : MacProcUlSchInfo
397 * Functionality: Processes UL scheduling info from SCH
399 * @params[in] Post structure
401 * @return ROK - success
404 * ****************************************************************/
405 uint8_t MacProcUlSchInfo(Pst *pst, UlSchedInfo *ulSchedInfo)
409 #ifdef CALL_FLOW_DEBUG_LOG
410 DU_LOG("\nCall Flow: ENTSCH -> ENTMAC : EVENT_UL_SCH_INFO\n");
413 GET_CELL_IDX(ulSchedInfo->cellId, cellIdx);
414 if(ulSchedInfo != NULLP)
416 MacUlSlot *currUlSlot =
417 &macCb.macCell[cellIdx]->ulSlot[ulSchedInfo->slotIndInfo.slot % MAX_SLOTS];
418 memcpy(&currUlSlot->ulInfo, ulSchedInfo, sizeof(UlSchedInfo));
423 /**********************************************************************
425 **********************************************************************/