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 /************************************************************************
25 Desc: C source code for rach handling functions
29 **********************************************************************/
32 @brief This file implements the rach handling.
34 #include "common_def.h"
40 #include "du_app_mac_inf.h"
41 #include "mac_sch_interface.h"
43 #include "sch_utils.h"
45 SchCb schCb[SCH_MAX_INST];
46 uint8_t puschDeltaTable[MAX_MU_PUSCH];
49 * @brief calculate ra-rnti function.
53 * Function : calculateRaRnti
55 * This function calculates ra-rnti
57 * @param[in] symbol index
58 * @param[in] slot index
59 * @param[in] frequency index
62 uint16_t calculateRaRnti(uint8_t symbolIdx, uint8_t slotIdx, uint8_t freqIdx)
65 uint8_t ulCarrierIdx = 0; /* Uplink carrier used for MSG1 transmission. 0:NUL carrier; 1:SUL carrier */
67 /* Refer to spec 38.321, section 5.1.3 */
68 raRnti = (1 + symbolIdx + (14*slotIdx) + (14*80*freqIdx) + (14*80*8*ulCarrierIdx));
73 * @brief create raCb function.
77 * Function : createSchRaCb
79 * This function create raCb
82 * @param[in] shed instance
85 void createSchRaCb(uint16_t tcrnti, Inst schInst)
89 GET_UE_IDX(tcrnti, ueIdx);
90 schCb[schInst].cells[schInst]->raCb[ueIdx -1].tcrnti = tcrnti;
94 * @brief resource allocation for msg3 PUSCH
98 * Function : schAllocMsg3Pusch
100 * This function handles msg3 PUSCH allocation
102 * @param[in] Inst schInst, SCH instance
103 * @param[in] slot, current slot
104 * @param[out] msg3StartRb
105 * @param[out] msg3NumRb
108 uint8_t schAllocMsg3Pusch(Inst schInst, uint16_t slot, uint16_t crnti, \
109 uint16_t *msg3StartRb, uint8_t *msg3NumRb, uint16_t msg3SlotAlloc)
111 SchCellCb *cell = NULLP;
112 SchUlSlotInfo *schUlSlotInfo = NULLP;
113 uint8_t startSymb = 0;
119 uint8_t numPdschSymbols= 14;
122 cell = schCb[schInst].cells[schInst];
123 startSymb = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[0].startSymbol;
124 symbLen = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[0].symbolLength;
126 startRb = cell->schUlSlotInfo[msg3SlotAlloc]->puschCurrentPrb;
127 tbSize = schCalcTbSize(8); /* 6 bytes msg3 and 2 bytes header */
128 numRb = schCalcNumPrb(tbSize, mcs, numPdschSymbols);
130 /* allocating 1 extra RB for now */
132 /* increment PUSCH PRB */
133 cell->schUlSlotInfo[msg3SlotAlloc]->puschCurrentPrb += numRb;
135 for(idx=startSymb; idx<symbLen; idx++)
137 cell->schUlSlotInfo[msg3SlotAlloc]->assignedPrb[idx] = startRb + numRb;
139 schUlSlotInfo = cell->schUlSlotInfo[msg3SlotAlloc];
141 SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
142 if(!schUlSlotInfo->schPuschInfo)
144 DU_LOG("\nERROR --> SCH : Memory allocation failed in schAllocMsg3Pusch");
147 tbSize = 0; /* since nPrb has been incremented, recalculating tbSize */
148 tbSize = schCalcTbSizeFromNPrb(numRb, mcs, numPdschSymbols);
149 tbSize = tbSize/8;/*bits to byte conversion*/
150 schUlSlotInfo->schPuschInfo->crnti = crnti;
151 schUlSlotInfo->schPuschInfo->harqProcId = SCH_HARQ_PROC_ID;
152 schUlSlotInfo->schPuschInfo->resAllocType = SCH_ALLOC_TYPE_1;
153 schUlSlotInfo->schPuschInfo->fdAlloc.startPrb = startRb;
154 schUlSlotInfo->schPuschInfo->fdAlloc.numPrb = numRb;
155 schUlSlotInfo->schPuschInfo->tdAlloc.startSymb = startSymb;
156 schUlSlotInfo->schPuschInfo->tdAlloc.numSymb = symbLen;
157 schUlSlotInfo->schPuschInfo->tbInfo.qamOrder = 2; /* QPSK modulation */
158 schUlSlotInfo->schPuschInfo->tbInfo.mcs = mcs;
159 schUlSlotInfo->schPuschInfo->tbInfo.mcsTable = SCH_MCS_TABLE_QAM_64;
160 schUlSlotInfo->schPuschInfo->tbInfo.ndi = 1; /* new transmission */
161 schUlSlotInfo->schPuschInfo->tbInfo.rv = 0;
162 schUlSlotInfo->schPuschInfo->tbInfo.tbSize = tbSize; /*Considering 2 PRBs */
163 schUlSlotInfo->schPuschInfo->dmrsMappingType = DMRS_MAP_TYPE_A; /* Setting Type-A */
164 schUlSlotInfo->schPuschInfo->nrOfDmrsSymbols = NUM_DMRS_SYMBOLS;
165 schUlSlotInfo->schPuschInfo->dmrsAddPos = DMRS_ADDITIONAL_POS;
167 *msg3StartRb = startRb;
173 * @brief Processes any pending RA request
177 * Function : schProcessRaReq
179 * This function process pending RA request
181 * @param[in] Current timing of the cell
184 void schProcessRaReq(SlotTimingInfo currTime, SchCellCb *cell)
187 RarInfo *rarInfo = NULLP;
189 uint16_t rarSlot = 0;
190 uint16_t msg3StartRb;
196 uint16_t msg3Slot = 0;
198 uint16_t slotIdx = 0;
201 while(ueIdx < MAX_NUM_UE)
203 if(cell->raReq[ueIdx] == NULLP)
209 //puschMu = cell->cellCfg.puschMu;
210 delta = puschDeltaTable[puschMu];
211 k2 = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[0].k2;
213 /* RAR will sent with a delay of RAR_DELAY */
214 rarSlot = (currTime.slot + RAR_DELAY + PHY_DELTA_DL) % cell->numSlots;
216 for(slotIdx=0; slotIdx<cell->numSlots;slotIdx++)
218 /* Slot allocation for msg3 based on 38.214 section 6.1.2.1 */
219 msg3Slot = (rarSlot+delta+k2)%cell->numSlots;
221 if((schGetSlotSymbFrmt(rarSlot, cell->slotFrmtBitMap) != DL_SLOT) &&\
222 (schGetSlotSymbFrmt(msg3Slot, cell->slotFrmtBitMap) != UL_SLOT))
224 rarSlot = (rarSlot + 1) % cell->numSlots;
229 if(slotIdx>=cell->numSlots)
231 DU_LOG("\nERROR --> SCH : NO Slot for Msg2 with Msg3 Grant\n");
235 /* Slot allocation for msg3 based on 38.214 section 6.1.2.1 */
236 msg3Slot = rarSlot + k2 + delta;
237 msg3Slot = msg3Slot % cell->numSlots;
240 SchDlSlotInfo *schDlSlotInfo = cell->schDlSlotInfo[rarSlot]; /* RAR will sent in the next slot */
242 /* Allocate the rarInfo, this pointer will be checked at schProcessSlotInd function */
243 SCH_ALLOC(rarInfo, sizeof(RarInfo));
246 DU_LOG("\nERROR --> SCH : Memory Allocation failed for rarInfo");
250 schDlSlotInfo->rarInfo = rarInfo;
252 /* create raCb at SCH */
253 createSchRaCb(cell->raReq[ueIdx]->rachInd->crnti, cell->instIdx);
255 /* allocate resources for msg3 */
256 ret = schAllocMsg3Pusch(cell->instIdx, rarSlot, cell->raReq[ueIdx]->rachInd->crnti, &msg3StartRb, &msg3NumRb, msg3Slot);
260 rarInfo->raRnti = cell->raReq[ueIdx]->raRnti;
261 rarInfo->tcrnti = cell->raReq[ueIdx]->rachInd->crnti;
262 rarInfo->RAPID = cell->raReq[ueIdx]->rachInd->preambleIdx;
263 rarInfo->ta = cell->raReq[ueIdx]->rachInd->timingAdv;
264 rarInfo->msg3FreqAlloc.startPrb = msg3StartRb;
265 rarInfo->msg3FreqAlloc.numPrb = msg3NumRb;
268 SCH_FREE(cell->raReq[ueIdx]->rachInd, sizeof(RachIndInfo));
269 SCH_FREE(cell->raReq[ueIdx], sizeof(SchRaReq));
272 } /* End of while(ueIdx < MAX_NUM_UE) */
276 * @brief process rach indication function.
280 * Function : schProcessRachInd
282 * This function process rach indication
284 * @param[in] rachInd parameters
285 * @param[in] shed instance
288 uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst)
290 SchCellCb *cell = schCb[schInst].cells[schInst];
291 SchRaReq *raReq = NULLP;
296 SCH_ALLOC(raReq, sizeof(SchRaReq));
299 DU_LOG("\nERROR --> SCH : Memory allocation failure in schProcessRachInd");
300 SCH_FREE(rachInd, sizeof(RachIndInfo));
304 /* calculate the ra-rnti value */
305 raReq->raRnti = calculateRaRnti(rachInd->symbolIdx, rachInd->slotIdx, rachInd->freqIdx);
306 raReq->rachInd = rachInd;
307 raReq->winStartTime.sfn = rachInd->timingInfo.sfn;
308 raReq->winStartTime.slot = rachInd->timingInfo.slot;
310 /* Converting window size from ms to number of slots */
311 slotDuration = (1 / pow(2, cell->cellCfg.numerology));
312 winNumSlots = (float)cell->cellCfg.schRachCfg.raRspWindow / slotDuration;
314 /* Adding window size to window start time to get window end time */
315 ADD_DELTA_TO_TIME(raReq->winStartTime, raReq->winEndTime, winNumSlots);
317 /* Storing RA request in cellCb */
318 GET_UE_IDX(rachInd->crnti, ueIdx);
319 cell->raReq[ueIdx] = raReq;
325 * @brief fill RAR info function.
329 * Function : calculateRaRnti
331 * This function fills pdcch and pdsch info for RAR
333 * @param[in] rar Allocation info
336 * @param[in] offset to pointA to determine freq alloc
339 uint8_t schFillRar(RarAlloc *rarAlloc, uint16_t raRnti, uint16_t pci, uint8_t offsetPointA, \
340 bool ssbPresent, bool sib1Present)
343 uint8_t coreset0Idx = 0;
345 uint8_t firstSymbol = 0;
346 uint8_t numSymbols = 0;
348 uint8_t FreqDomainResource[6] = {0};
350 uint8_t mcs = 4; /* MCS fixed to 4 */
352 SchBwpDlCfg *initialBwp = &schCb[inst].cells[inst]->cellCfg.schInitialDlBwp;
353 PdcchCfg *pdcch = &rarAlloc->rarPdcchCfg;
354 PdschCfg *pdsch = &rarAlloc->rarPdschCfg;
355 BwpCfg *bwp = &rarAlloc->bwp;
356 FreqDomainAlloc *sib1PdschFreqAlloc = NULL;
358 coreset0Idx = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
360 /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
361 numRbs = coresetIdxTable[coreset0Idx][1];
362 numSymbols = coresetIdxTable[coreset0Idx][2];
363 offset = coresetIdxTable[coreset0Idx][3];
365 /* calculate time domain parameters */
366 // note: since slot value is made sl1, RAR can be sent at all slots
367 uint16_t mask = 0x2000;
368 for(firstSymbol=0; firstSymbol<14;firstSymbol++)
370 if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
376 /* calculate the PRBs */
377 freqDomRscAllocType0(((offsetPointA-offset)/6), (numRbs/6), FreqDomainResource);
380 bwp->freqAlloc.numPrb = initialBwp->bwp.freqAlloc.numPrb;
381 bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
382 bwp->subcarrierSpacing = initialBwp->bwp.scs;
383 bwp->cyclicPrefix = initialBwp->bwp.cyclicPrefix;
385 /* fill the PDCCH PDU */
386 pdcch->coresetCfg.startSymbolIndex = firstSymbol;
387 pdcch->coresetCfg.durationSymbols = numSymbols;
388 memcpy(pdcch->coresetCfg.freqDomainResource, FreqDomainResource, FREQ_DOM_RSRC_SIZE);
389 pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
390 pdcch->coresetCfg.regBundleSize = 6; /* spec-38.211 sec 7.3.2.2 */
391 pdcch->coresetCfg.interleaverSize = 2; /* spec-38.211 sec 7.3.2.2 */
392 pdcch->coresetCfg.coreSetType = 0;
393 pdcch->coresetCfg.shiftIndex = pci;
394 pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
396 pdcch->dci.rnti = raRnti; /* RA-RNTI */
397 pdcch->dci.scramblingId = pci;
398 pdcch->dci.scramblingRnti = 0;
399 pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
400 pdcch->dci.aggregLevel = 4;
401 pdcch->dci.beamPdcchInfo.numPrgs = 1;
402 pdcch->dci.beamPdcchInfo.prgSize = 1;
403 pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
404 pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
405 pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
406 pdcch->dci.txPdcchPower.powerValue = 0;
407 pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
408 pdcch->dci.pdschCfg = pdsch;
410 /* fill the PDSCH PDU */
412 pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
413 pdsch->rnti = raRnti; /* RA-RNTI */
415 pdsch->numCodewords = 1;
416 for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
418 pdsch->codeword[cwCount].targetCodeRate = 308;
419 pdsch->codeword[cwCount].qamModOrder = 2;
420 pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
421 pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
422 pdsch->codeword[cwCount].rvIndex = 0;
423 /* RAR PDU length and FAPI payload header length */
424 tbSize = schCalcTbSize(RAR_PAYLOAD_SIZE + TX_PAYLOAD_HDR_LEN);
425 pdsch->codeword[cwCount].tbSize = tbSize;
427 pdsch->dataScramblingId = pci;
428 pdsch->numLayers = 1;
429 pdsch->transmissionScheme = 0;
431 pdsch->dmrs.dlDmrsSymbPos = 4; /* Bitmap value 00000000000100 i.e. using 3rd symbol for PDSCH DMRS */
432 pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
433 pdsch->dmrs.dlDmrsScramblingId = pci;
434 pdsch->dmrs.scid = 0;
435 pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
436 pdsch->dmrs.dmrsPorts = 0;
437 pdsch->dmrs.mappingType = DMRS_MAP_TYPE_A; /* Type-A */
438 pdsch->dmrs.nrOfDmrsSymbols = NUM_DMRS_SYMBOLS;
439 pdsch->dmrs.dmrsAddPos = DMRS_ADDITIONAL_POS;
440 pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
441 /* The RB numbering starts from coreset0 */
442 pdsch->pdschFreqAlloc.freqAlloc.startPrb = PDSCH_START_RB;
445 /* PDSCH is always above SSB */
446 pdsch->pdschFreqAlloc.freqAlloc.startPrb = offsetPointA + SCH_SSB_NUM_PRB + 1;
450 /* Must not overlap with SIB1 */
451 sib1PdschFreqAlloc = &schCb[inst].cells[inst]->cellCfg.sib1SchCfg.sib1PdschCfg.pdschFreqAlloc.freqAlloc;
452 pdsch->pdschFreqAlloc.freqAlloc.startPrb = sib1PdschFreqAlloc->startPrb + sib1PdschFreqAlloc->numPrb + 1;
454 pdsch->pdschFreqAlloc.freqAlloc.numPrb = schCalcNumPrb(tbSize, mcs, \
455 initialBwp->pdschCommon.timeDomRsrcAllocList[0].lengthSymbol);
456 pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
457 pdsch->pdschTimeAlloc.timeAlloc.startSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[0].startSymbol;
458 pdsch->pdschTimeAlloc.timeAlloc.numSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[0].lengthSymbol;
459 pdsch->beamPdschInfo.numPrgs = 1;
460 pdsch->beamPdschInfo.prgSize = 1;
461 pdsch->beamPdschInfo.digBfInterfaces = 0;
462 pdsch->beamPdschInfo.prg[0].pmIdx = 0;
463 pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
464 pdsch->txPdschPower.powerControlOffset = 0;
465 pdsch->txPdschPower.powerControlOffsetSS = 0;
470 /**********************************************************************
472 **********************************************************************/