MSG4 scheduling based on k0-k1 [Issue-ID: ODUHIGH-388]
[o-du/l2.git] / src / 5gnrsch / sch_rach.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
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                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
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
19 /************************************************************************
20  
21      Name:     sch_rach.c
22   
23      Type:     C source file
24   
25      Desc:     C source code for rach handling functions
26   
27      File:     sch_rach.c
28   
29 **********************************************************************/
30
31 /** @file sch_rach.c
32 @brief This file implements the rach handling.
33 */
34 #include "common_def.h"
35 #include "tfu.h"
36 #include "lrg.h"
37 #include "tfu.x"
38 #include "lrg.x"
39 #include "du_log.h"
40 #include "du_app_mac_inf.h"
41 #include "mac_sch_interface.h"
42 #include "sch.h"
43 #include "sch_utils.h"
44
45 /**
46  * @brief Checks if PRACH can be scheduled in current slot
47  *
48  * @details
49  *
50  *     Function : schCheckPrachOcc
51  *
52  *     This function checks if PRACH can be scheduled in 
53  *     current slot
54  *
55  *  @param[in]  Cell Cb
56  *              Slot timing
57  *  @return  TRUE
58  *           FALSE
59  **/
60 bool schCheckPrachOcc(SchCellCb *cell, SlotTimingInfo prachOccasionTimingInfo)
61 {
62    uint8_t  prachCfgIdx = 0;
63    uint8_t  x = 0;
64    uint8_t  y = 0;
65    uint8_t  subFrame = 0;
66    uint16_t prachSubframe = 0;
67
68    prachCfgIdx      = cell->cellCfg.schRachCfg.prachCfgIdx;
69
70    /* derive the prachCfgIdx table paramters */
71    x                = prachCfgIdxTable[prachCfgIdx][1];
72    y                = prachCfgIdxTable[prachCfgIdx][2];
73    prachSubframe    = prachCfgIdxTable[prachCfgIdx][3];
74
75    if((prachOccasionTimingInfo.sfn%x) == y)
76    {
77       subFrame = prachOccasionTimingInfo.slot/pow(2, cell->cellCfg.numerology);
78
79       /* check for subFrame number */
80       if ((1 << subFrame) & prachSubframe)
81       {
82          /* prach ocassion present in this subframe */
83 #ifdef NR_TDD
84          if(UL_SLOT != schGetSlotSymbFrmt(prachOccasionTimingInfo.slot % cell->numSlotsInPeriodicity,\
85          cell->slotFrmtBitMap))
86          {
87             DU_LOG("\nERROR  --> SCH : PrachCfgIdx %d doesn't support UL slot", prachCfgIdx);
88             return FALSE;
89          }
90 #endif
91          return TRUE;
92       }
93    }
94    return FALSE;
95 }
96
97 /**
98  * @brief Calculate number of PRBs to be allocated for PRACH 
99  *
100  * @details
101  *
102  *     Function : schCalcPrachNumRb
103  *
104  *     Calculate number of PRBs to be allocated for PRACH
105  *
106  *  @param[in]  SchCellCb *cell, cell cb
107  *  @return  Number of PRBs
108  **/
109 uint8_t schCalcPrachNumRb(SchCellCb *cell)
110 {
111    uint8_t tableIdx = 0;
112    uint16_t puschScs = fetchScsValue(cell->cellCfg.schInitialUlBwp.bwp.scs);
113
114    for(tableIdx=0; tableIdx < MAX_RACH_NUM_RB_IDX; tableIdx++)
115    {
116       if((numRbForPrachTable[tableIdx][0] == cell->cellCfg.schRachCfg.rootSeqLen) &&
117             (numRbForPrachTable[tableIdx][1] == cell->cellCfg.schRachCfg.prachSubcSpacing) &&
118             (numRbForPrachTable[tableIdx][2] == puschScs))
119       {
120          return numRbForPrachTable[tableIdx][3];
121       }
122    }
123    return 0;
124 }
125
126 /**
127  * @brief resource allocation for PRACH
128  *
129  * @details
130  *
131  *     Function : schPrachResAlloc
132  *
133  *     This function handles PRACH allocation
134  *
135  *  @param[in]  SchCellCb *cell, cell cb
136  *  @param[in]  UlSchedInfo *ulSchedInfo, UL scheduling info
137  *  @return  void
138  **/
139 void schPrachResAlloc(SchCellCb *cell, UlSchedInfo *ulSchedInfo, SlotTimingInfo prachOccasionTimingInfo)
140 {
141    uint8_t  numPrachRb = 0;
142    uint8_t  numRa = 0;
143    uint8_t  prachCfgIdx = 0;
144    uint8_t  prachFormat = 0;
145    uint8_t  prachStartSymbol = 0;
146    uint8_t  prachDuration = 0;
147    uint8_t  prachOcas = 0;
148    uint8_t  dataType = 0;
149    uint16_t freqStart = 0;
150
151    /* If this slot is not a PRACH occassion, return */
152    if(!schCheckPrachOcc(cell, prachOccasionTimingInfo))
153       return;
154
155    prachCfgIdx      = cell->cellCfg.schRachCfg.prachCfgIdx;
156    prachFormat      = prachCfgIdxTable[prachCfgIdx][0];
157    prachStartSymbol = prachCfgIdxTable[prachCfgIdx][4];
158    prachOcas        = prachCfgIdxTable[prachCfgIdx][6];
159    prachDuration    = prachCfgIdxTable[prachCfgIdx][7];
160
161    /* numRa determined as 𝑛 belonging {0,1,.., M − 1},
162     * where M is given by msg1Fdm */
163    numRa = (cell->cellCfg.schRachCfg.msg1Fdm - 1);
164
165    /* freq domain resource determination for RACH*/
166    freqStart = cell->cellCfg.schRachCfg.msg1FreqStart;
167    numPrachRb = schCalcPrachNumRb(cell);
168    /* Allocate PRACH resources from the UL resource bitmap */
169    allocatePrbUl(cell, prachOccasionTimingInfo, prachStartSymbol, prachDuration, &freqStart, numPrachRb);
170
171    /* prach info */
172    dataType |= SCH_DATATYPE_PRACH;
173    ulSchedInfo->dataType = dataType;
174    ulSchedInfo->prachSchInfo.numPrachOcas   = prachOcas;
175    ulSchedInfo->prachSchInfo.prachFormat    = prachFormat;
176    ulSchedInfo->prachSchInfo.numRa          = numRa;
177    ulSchedInfo->prachSchInfo.prachStartSymb = prachStartSymbol;
178    DU_LOG("\nINFO  --> SCH : RACH occassion set for slot %d", prachOccasionTimingInfo.slot);
179 }
180
181 /**
182  * @brief calculate ra-rnti function. 
183  *
184  * @details
185  *
186  *     Function : calculateRaRnti
187  *     
188  *     This function calculates ra-rnti
189  *     
190  *  @param[in]  symbol index
191  *  @param[in]  slot index
192  *  @param[in]  frequency index
193  *  @return  ra-rnti
194  **/
195 uint16_t calculateRaRnti(uint8_t symbolIdx, uint8_t slotIdx, uint8_t freqIdx)
196 {
197    uint16_t raRnti = 0;
198    uint8_t  ulCarrierIdx = 0; /* Uplink carrier used for MSG1 transmission. 0:NUL carrier; 1:SUL carrier */
199    
200    /* Refer to spec 38.321, section 5.1.3 */
201    raRnti = (1 + symbolIdx + (14*slotIdx) + (14*80*freqIdx) + (14*80*8*ulCarrierIdx));
202    return raRnti;
203 }
204
205 /**
206  * @brief create raCb function. 
207  *
208  * @details
209  *
210  *     Function : createSchRaCb
211  *     
212  *     This function create raCb
213  *     
214  *  @param[in]  tcrnti
215  *  @param[in]  shed instance
216  *  @return  void
217  **/
218 void createSchRaCb(uint16_t tcrnti, Inst schInst)
219 {
220    uint8_t ueIdx = 0;
221
222    GET_UE_IDX(tcrnti, ueIdx);
223    schCb[schInst].cells[schInst]->raCb[ueIdx -1].tcrnti = tcrnti;
224    schCb[schInst].cells[schInst]->raCb[ueIdx -1].msg4recvd = FALSE;
225 }
226
227 /**
228  * @brief resource allocation for msg3 PUSCH
229  *
230  * @details
231  *
232  *     Function : schAllocMsg3Pusch 
233  *     
234  *     This function handles msg3 PUSCH allocation
235  *     
236  *  @param[in]  Inst schInst, SCH instance
237  *  @param[in]  slot, current slot
238  *  @param[out]  msg3StartRb
239  *  @param[out]  msg3NumRb
240  *  @return  void
241  **/
242 SchPuschInfo* schAllocMsg3Pusch(Inst schInst, uint16_t crnti, uint8_t k2Index, SlotTimingInfo msg3SlotTime)
243 {
244    SchCellCb      *cell          = NULLP;
245    SchUlSlotInfo  *schUlSlotInfo = NULLP;
246    uint8_t    mcs       = DEFAULT_MCS;
247    uint8_t    startSymb = 0;
248    uint8_t    symbLen   = 0; 
249    uint16_t   startRb   = 0;
250    uint16_t   numRb     = 0;
251    uint16_t   tbSize    = 0;
252
253    cell = schCb[schInst].cells[schInst];
254    if(cell == NULL)
255    {
256       DU_LOG("\n\nERROR  -->  SCH :  Failed to find cell in schAllocMsg3Pusch");
257       return NULLP;
258    }
259
260    /* Allocate time-domain and frequency-domain resource for MSG3 PUSCH */
261    startSymb = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].startSymbol;
262    symbLen = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].symbolLength;
263
264    startRb = MAX_NUM_RB;
265    tbSize = schCalcTbSize(8); /* 6 bytes msg3 and 2 bytes header */
266    numRb = schCalcNumPrb(tbSize, mcs, symbLen);
267    numRb++; /* allocating 1 extra RB for now */
268    allocatePrbUl(cell, msg3SlotTime, startSymb, symbLen, &startRb, numRb);
269
270    /* Fill PUSCH scheduling details in Slot structure */
271    schUlSlotInfo = cell->schUlSlotInfo[msg3SlotTime.slot];
272    SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
273    if(!schUlSlotInfo->schPuschInfo)
274    {
275       DU_LOG("\nERROR  -->  SCH :  Memory allocation failed in schAllocMsg3Pusch");
276       return NULLP;
277    }
278
279    tbSize = 0;  /* since nPrb has been incremented, recalculating tbSize */
280    tbSize = schCalcTbSizeFromNPrb(numRb, mcs, NUM_PDSCH_SYMBOL);
281    tbSize = tbSize / 8 ; /*bits to byte conversion*/
282
283    schUlSlotInfo->schPuschInfo->crnti             = crnti;
284    schUlSlotInfo->schPuschInfo->harqProcId        = SCH_HARQ_PROC_ID;
285    schUlSlotInfo->schPuschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
286    schUlSlotInfo->schPuschInfo->fdAlloc.startPrb  = startRb;
287    schUlSlotInfo->schPuschInfo->fdAlloc.numPrb    = numRb;
288    schUlSlotInfo->schPuschInfo->tdAlloc.startSymb = startSymb;
289    schUlSlotInfo->schPuschInfo->tdAlloc.numSymb   = symbLen;
290    schUlSlotInfo->schPuschInfo->tbInfo.qamOrder   = QPSK_MODULATION;  /* QPSK modulation */
291    schUlSlotInfo->schPuschInfo->tbInfo.mcs           = mcs;
292    schUlSlotInfo->schPuschInfo->tbInfo.mcsTable   = SCH_MCS_TABLE_QAM_64;
293    schUlSlotInfo->schPuschInfo->tbInfo.ndi        = NEW_TRANSMISSION; /* new transmission */
294    schUlSlotInfo->schPuschInfo->tbInfo.rv               = 0;
295    schUlSlotInfo->schPuschInfo->tbInfo.tbSize     = tbSize;
296    schUlSlotInfo->schPuschInfo->dmrsMappingType   = DMRS_MAP_TYPE_A;  /* Setting Type-A */
297    schUlSlotInfo->schPuschInfo->nrOfDmrsSymbols   = NUM_DMRS_SYMBOLS;
298    schUlSlotInfo->schPuschInfo->dmrsAddPos        = DMRS_ADDITIONAL_POS;
299
300    return schUlSlotInfo->schPuschInfo;
301 }
302
303 /**
304  * @brief Check if a time frame is in RA Response window
305  *
306  * @details
307  *
308  *     Function : isInRaRspWindow
309  *
310  *     Check if a time frame is in RA Response window
311  *
312  *  @param[in]  RA request
313  *  @param[in]  Time frame to check
314  *  @param[in]  Total number of slot per radio frame
315  *  @return  true 
316  *  @return  false
317  **/
318 RaRspWindowStatus isInRaRspWindow(SchRaReq *raReq, SlotTimingInfo frameToCheck, uint16_t numSlotsPerSystemFrame)
319 {
320    uint32_t winStartTime, winEndTime, timeToCheck;
321    
322    winStartTime = (raReq->winStartTime.sfn * numSlotsPerSystemFrame) + raReq->winStartTime.slot;
323    winEndTime = (raReq->winEndTime.sfn * numSlotsPerSystemFrame) + raReq->winEndTime.slot;
324    timeToCheck = (frameToCheck.sfn * numSlotsPerSystemFrame) + frameToCheck.slot;
325
326    /* TODO : check how to handle the wrap around scenario of MAX_SFN */
327    if((timeToCheck >= winStartTime) && (timeToCheck <= winEndTime))
328       return WITHIN_WINDOW;
329    else if(timeToCheck < winStartTime)
330       return WINDOW_YET_TO_START;
331       
332    return WINDOW_EXPIRED;
333 }
334
335 /**
336  * @brief Processes any pending RA request
337  *
338  * @details
339  *
340  *     Function : schProcessRaReq
341  *
342  *     This function process pending RA request
343  *
344  *  @param[in]  Current timing of the cell
345  *  @return  ROK
346  **/
347 void schProcessRaReq(SlotTimingInfo currTime, SchCellCb *cell)
348 {
349    bool      k2Found = false;
350    uint8_t   ueIdx = 0, k0TblIdx = 0, k2TblIdx = 0;
351    uint8_t   k0Index = 0, k2Index = 0;
352    uint8_t   k0 = 0, k2 = 0;
353    uint8_t   puschMu = 0;
354    uint8_t   msg3Delta = 0, msg3MinSchTime = 0;
355 #ifdef NR_TDD
356    uint8_t   totalCfgSlot = 0;
357 #endif
358    uint16_t             dciSlot = 0, rarSlot = 0;
359    SlotTimingInfo       dciTime, rarTime, msg3Time;
360    RarAlloc             *dciSlotAlloc = NULLP;    /* Stores info for transmission of PDCCH for RAR */
361    RarAlloc             *rarSlotAlloc = NULLP;    /* Stores info for transmission of RAR PDSCH */
362    SchPuschInfo         *msg3PuschInfo = NULLP;   /* Stores MSG3 PUSCH scheduling information */
363    SchK0K1TimingInfoTbl *k0K1InfoTbl=NULLP;    
364    SchK2TimingInfoTbl   *msg3K2InfoTbl=NULLP;
365    RaRspWindowStatus    windowStatus=0;
366
367    while(ueIdx < MAX_NUM_UE)
368    {
369       if(cell->raReq[ueIdx] == NULLP)
370       {
371          ueIdx++;
372          continue;
373       }
374
375 #ifdef NR_TDD
376       totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.ssbSchCfg.scsCommon, cell->cellCfg.tddCfg.tddPeriod);
377 #endif
378       k0K1InfoTbl = &cell->cellCfg.schInitialDlBwp.k0K1InfoTbl;
379       msg3K2InfoTbl = &cell->cellCfg.schInitialUlBwp.msg3K2InfoTbl;
380       puschMu = cell->cellCfg.numerology;
381       msg3Delta = puschDeltaTable[puschMu];
382       msg3MinSchTime = minMsg3SchTime[cell->cellCfg.numerology];
383
384       /* Calculating time frame to send DCI for RAR */
385       ADD_DELTA_TO_TIME(currTime, dciTime, PHY_DELTA_DL + SCHED_DELTA);
386       dciSlot = dciTime.slot;
387 #ifdef NR_TDD
388       /* Consider this slot for sending DCI, only if it is a DL slot */
389       if(schGetSlotSymbFrmt(dciSlot, cell->slotFrmtBitMap) == DL_SLOT)
390 #endif
391       {
392          
393          /* Check if this slot is within RA response window */
394          windowStatus = isInRaRspWindow(cell->raReq[ueIdx], dciTime, cell->numSlots);
395          if(windowStatus == WITHIN_WINDOW)
396          {
397             /* For all k0 values, search for a suitable k2 value to schedule MSG3.
398              * RAR DCI, RAR PDSCH and MSG3 is scheduled only if one such k0-k2 combination
399              * is found. Else no scheduling happens. 
400              */
401             for(k0TblIdx = 0; k0TblIdx < k0K1InfoTbl->k0k1TimingInfo[dciSlot].numK0; k0TblIdx++)
402             {
403                k0Index = k0K1InfoTbl->k0k1TimingInfo[dciSlot].k0Indexes[k0TblIdx].k0Index;
404                k0 = cell->cellCfg.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[k0Index].k0;
405
406                /* Calculating time frame to send RAR PDSCH */
407                ADD_DELTA_TO_TIME(dciTime, rarTime, k0);
408                rarSlot = rarTime.slot;
409
410                for(k2TblIdx = 0; k2TblIdx < msg3K2InfoTbl->k2TimingInfo[rarSlot].numK2; k2TblIdx++)
411                {
412                   k2Index = msg3K2InfoTbl->k2TimingInfo[rarSlot].k2Indexes[k2TblIdx];
413                   k2 = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].k2;
414
415                   /* Delta is added to the slot allocation for msg3 based on 38.214 section 6.1.2.1 */
416                   k2 = k2 + msg3Delta;
417                   if(k2 >= msg3MinSchTime)
418                   {
419                      ADD_DELTA_TO_TIME(rarTime, msg3Time, k2);
420 #ifdef NR_TDD
421                      if(schGetSlotSymbFrmt(msg3Time.slot % totalCfgSlot, cell->slotFrmtBitMap) == DL_SLOT)
422                         continue;
423 #endif
424                      k2Found = true;
425                      break;
426                   }
427                }
428                if(k2Found)
429                   break;
430             }
431          }
432          else if(windowStatus == WINDOW_EXPIRED)
433          {
434             SCH_FREE(cell->raReq[ueIdx]->rachInd, sizeof(RachIndInfo));
435             SCH_FREE(cell->raReq[ueIdx], sizeof(SchRaReq));
436             ueIdx++;
437             continue;
438          }
439
440          /* If K0-K2 combination not found, no scheduling happens */
441          if(!k2Found)
442          {
443             ueIdx++;
444             continue;
445          }
446
447          /* Allocate memory for RAR PDCCH slot, pointer will be checked at schProcessSlotInd() */
448          SCH_ALLOC(dciSlotAlloc, sizeof(RarAlloc));
449          if(dciSlotAlloc == NULLP)
450          {
451             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciSlotAlloc");
452             return;
453          }
454          cell->schDlSlotInfo[dciSlot]->rarAlloc = dciSlotAlloc;
455
456          /* Fill PDCCH and PDSCH scheduling information for RAR */
457          if((schFillRar(cell, rarTime, ueIdx, dciSlotAlloc, k0Index)) != ROK)
458          {
459             DU_LOG("\nERROR  -->  SCH: Scheduling of RAR failed in slot [%d]", rarSlot);
460             SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
461             cell->schDlSlotInfo[dciSlot]->rarAlloc = NULLP;
462             return;
463          }
464
465          /* Allocate resources for msg3 */
466          msg3PuschInfo = schAllocMsg3Pusch(cell->instIdx, cell->raReq[ueIdx]->rachInd->crnti, k2Index, msg3Time);
467          if(msg3PuschInfo)
468          {
469             /* Fill RAR info */
470             dciSlotAlloc->rarInfo.raRnti = cell->raReq[ueIdx]->raRnti;
471             dciSlotAlloc->rarInfo.tcrnti = cell->raReq[ueIdx]->rachInd->crnti;
472             dciSlotAlloc->rarInfo.RAPID = cell->raReq[ueIdx]->rachInd->preambleIdx;
473             dciSlotAlloc->rarInfo.ta = cell->raReq[ueIdx]->rachInd->timingAdv;
474             dciSlotAlloc->rarInfo.ulGrant.bwpSize = cell->cellCfg.schInitialUlBwp.bwp.freqAlloc.numPrb;
475             /* Spec 38.213, section 8.2, 0 : MSG3 PUSCH will be transmitted without frequency hopping */
476             dciSlotAlloc->rarInfo.ulGrant.freqHopFlag = 0;
477             dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.startPrb = msg3PuschInfo->fdAlloc.startPrb;
478             dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.numPrb = msg3PuschInfo->fdAlloc.numPrb;
479             dciSlotAlloc->rarInfo.ulGrant.k2Index = k2Index;
480             dciSlotAlloc->rarInfo.ulGrant.mcs = msg3PuschInfo->tbInfo.mcs;
481             dciSlotAlloc->rarInfo.ulGrant.tpc = 3;  /* TODO : Check appropriate value to be filled */
482             /* Spec 38.213, section 8.2 : In a contention based random access
483              * procedure, the CSI request field is reserved. */
484             dciSlotAlloc->rarInfo.ulGrant.csiReq = 0;
485          }
486
487          /* Check if both DCI and RAR are sent in the same slot.
488           * If not, allocate memory RAR PDSCH slot to store RAR info
489           */
490          if(dciSlot == rarSlot)
491             dciSlotAlloc->pduPres = BOTH;
492          else
493          {
494             /* Allocate memory to schedule rarSlot to send RAR, pointer will be checked at schProcessSlotInd() */
495             SCH_ALLOC(rarSlotAlloc, sizeof(RarAlloc));
496             if(rarSlotAlloc == NULLP)
497             {
498                DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for rarSlotAlloc");
499                SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
500                cell->schDlSlotInfo[dciSlot]->rarAlloc = NULLP;
501                return;
502             }
503             cell->schDlSlotInfo[rarSlot]->rarAlloc = rarSlotAlloc;
504
505             /* Copy all RAR info */
506             memcpy(rarSlotAlloc, dciSlotAlloc, sizeof(RarAlloc));
507             rarSlotAlloc->rarPdcchCfg.dci.pdschCfg = &rarSlotAlloc->rarPdschCfg;
508
509             /* Assign correct PDU types in corresponding slots */
510             rarSlotAlloc->pduPres = PDSCH_PDU;
511             dciSlotAlloc->pduPres = PDCCH_PDU;
512             dciSlotAlloc->pdschSlot = rarSlot;  
513          }
514
515          /* Create raCb at SCH */
516          createSchRaCb(cell->raReq[ueIdx]->rachInd->crnti, cell->instIdx);
517
518          SCH_FREE(cell->raReq[ueIdx]->rachInd, sizeof(RachIndInfo));
519          SCH_FREE(cell->raReq[ueIdx], sizeof(SchRaReq));
520       }
521       ueIdx++;
522    } /* End of while(ueIdx < MAX_NUM_UE) */
523 }
524
525 /**
526  * @brief process rach indication function. 
527  *
528  * @details
529  *
530  *     Function : schProcessRachInd
531  *     
532  *     This function process rach indication
533  *     
534  *  @param[in]  rachInd parameters
535  *  @param[in]  shed instance
536  *  @return  ROK
537  **/
538 uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst)
539 {
540    SchCellCb *cell = schCb[schInst].cells[schInst];
541    SchRaReq  *raReq = NULLP;
542    float    slotDuration;
543    uint8_t  winNumSlots;
544    uint8_t  ueIdx;
545
546    if(cell == NULLP)
547    {
548       DU_LOG("\nERROR  -->  SCH: Failed to find cell in schProcessRachInd");
549       return RFAILED;
550    }
551
552    /* Storing RA request in cellCb */
553    GET_UE_IDX(rachInd->crnti, ueIdx);
554    if(ueIdx <= 0)
555    {
556       DU_LOG("\nERROR  -->  SCH: Invalid CRNTI [%d]", rachInd->crnti);
557       return RFAILED;
558    }
559
560    SCH_ALLOC(raReq, sizeof(SchRaReq));
561    if(!raReq)
562    {
563       DU_LOG("\nERROR  -->  SCH : Memory allocation failure in schProcessRachInd");
564       SCH_FREE(rachInd, sizeof(RachIndInfo));
565       return RFAILED;
566    }
567
568    /* calculate the ra-rnti value */
569    raReq->raRnti = calculateRaRnti(rachInd->symbolIdx, rachInd->slotIdx, rachInd->freqIdx);
570    raReq->rachInd = rachInd;
571    raReq->winStartTime.sfn = rachInd->timingInfo.sfn;
572    raReq->winStartTime.slot = rachInd->timingInfo.slot;
573   
574    /* Converting window size from ms to number of slots */
575    slotDuration = (1 / pow(2, cell->cellCfg.numerology));
576    winNumSlots = (float)cell->cellCfg.schRachCfg.raRspWindow / slotDuration;
577    
578    /* Adding window size to window start time to get window end time */
579    ADD_DELTA_TO_TIME(raReq->winStartTime, raReq->winEndTime, winNumSlots);
580
581    cell->raReq[ueIdx -1] = raReq;
582
583    return ROK;
584 }
585
586 /**
587  * @brief fill RAR info function. 
588  *
589  * @details
590  *
591  *     Function : calculateRaRnti
592  *     
593  *     This function fills pdcch and pdsch info for RAR
594  *     
595  *  @param[in]  rar Allocation info
596  *  @param[in]  ra-rnti
597  *  @param[in]  PCI
598  *  @param[in]  offset to pointA to determine freq alloc
599  *  @return  ROK
600  **/
601 uint8_t schFillRar(SchCellCb *cell, SlotTimingInfo rarTime, uint16_t ueIdx, RarAlloc *rarAlloc, uint8_t k0Index)
602 {
603    uint8_t  coreset0Idx = 0;
604    uint8_t  firstSymbol = 0, numSymbols = 0;
605    uint8_t  mcs = DEFAULT_MCS;  /* MCS fixed to 4 */
606    uint8_t  dmrsStartSymbol, startSymbol, numSymbol ;
607    uint16_t numRbs = 0;
608    uint16_t tbSize = 0;
609
610    SchBwpDlCfg *initialBwp = &cell->cellCfg.schInitialDlBwp;
611    PdcchCfg *pdcch = &rarAlloc->rarPdcchCfg;
612    PdschCfg *pdsch = &rarAlloc->rarPdschCfg;
613    BwpCfg *bwp = &rarAlloc->bwp;
614
615    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
616    coreset0Idx     = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
617    numRbs     = coresetIdxTable[coreset0Idx][1]; 
618    numSymbols = coresetIdxTable[coreset0Idx][2];
619
620    /* calculate time domain parameters */
621    // note: since slot value is made sl1, RAR can be sent at all slots
622    uint16_t mask = 0x2000;
623    for(firstSymbol=0; firstSymbol<MAX_SYMB_PER_SLOT; firstSymbol++)
624    {
625       if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
626          break;
627       else
628          mask = mask>>1;
629    }
630
631    /* fill BWP */
632    bwp->freqAlloc.numPrb   = initialBwp->bwp.freqAlloc.numPrb;
633    bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
634    bwp->subcarrierSpacing  = initialBwp->bwp.scs;
635    bwp->cyclicPrefix       = initialBwp->bwp.cyclicPrefix;
636
637    /* fill the PDCCH PDU */
638    pdcch->coresetCfg.startSymbolIndex = firstSymbol;
639    pdcch->coresetCfg.durationSymbols = numSymbols;
640    memcpy(pdcch->coresetCfg.freqDomainResource, \
641       cell->cellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
642
643    pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
644    pdcch->coresetCfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
645    pdcch->coresetCfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
646    pdcch->coresetCfg.coreSetType = 0;
647    pdcch->coresetCfg.coreSetSize = numRbs;
648    pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
649    pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
650    pdcch->numDlDci = 1;
651    pdcch->dci.rnti = cell->raReq[ueIdx]->raRnti; /* RA-RNTI */
652    pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
653    pdcch->dci.scramblingRnti = 0;
654    pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
655    pdcch->dci.aggregLevel = 4;
656    pdcch->dci.beamPdcchInfo.numPrgs = 1;
657    pdcch->dci.beamPdcchInfo.prgSize = 1;
658    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
659    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
660    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
661    pdcch->dci.txPdcchPower.powerValue = 0;
662    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
663    pdcch->dci.pdschCfg = pdsch;
664
665    /* fill the PDSCH PDU */
666    uint8_t cwCount = 0;
667    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
668    pdsch->rnti = cell->raReq[ueIdx]->raRnti; /* RA-RNTI */
669    pdsch->pduIndex = 0;
670    pdsch->numCodewords = 1;
671    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
672    {
673       pdsch->codeword[cwCount].targetCodeRate = 308;
674       pdsch->codeword[cwCount].qamModOrder = 2;
675       pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
676       pdsch->codeword[cwCount].mcsTable = 0;   /* notqam256 */
677       pdsch->codeword[cwCount].rvIndex = 0;
678       /* RAR PDU length and FAPI payload header length */
679       tbSize = schCalcTbSize(RAR_PAYLOAD_SIZE + TX_PAYLOAD_HDR_LEN);
680       pdsch->codeword[cwCount].tbSize = tbSize;
681    }
682    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
683    pdsch->numLayers = 1;
684    pdsch->transmissionScheme = 0;
685    pdsch->refPoint = 0;
686    pdsch->dmrs.dlDmrsSymbPos = 4;  /* Bitmap value 00000000000100 i.e. using 3rd symbol for PDSCH DMRS */
687    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
688    pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
689    pdsch->dmrs.scid = 0;
690    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
691    pdsch->dmrs.dmrsPorts = 0;
692    pdsch->dmrs.mappingType      = DMRS_MAP_TYPE_A;  /* Type-A */
693    pdsch->dmrs.nrOfDmrsSymbols  = NUM_DMRS_SYMBOLS;
694    pdsch->dmrs.dmrsAddPos       = DMRS_ADDITIONAL_POS;
695
696    pdsch->pdschTimeAlloc.timeAlloc.startSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].startSymbol;
697    pdsch->pdschTimeAlloc.timeAlloc.numSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol;
698
699    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
700    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
701    pdsch->pdschFreqAlloc.freqAlloc.startPrb = MAX_NUM_RB;
702    pdsch->pdschFreqAlloc.freqAlloc.numPrb = \
703       schCalcNumPrb(tbSize, mcs, initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol);
704
705    /* Find total symbols occupied including DMRS */
706    dmrsStartSymbol = findDmrsStartSymbol(pdsch->dmrs.dlDmrsSymbPos);
707    /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT, 
708     * in that case only PDSCH symbols are marked as occupied */
709    if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
710    {
711       startSymbol = pdsch->pdschTimeAlloc.timeAlloc.startSymb;
712       numSymbol = pdsch->pdschTimeAlloc.timeAlloc.numSymb;
713    }
714    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
715    else
716    {
717       startSymbol = dmrsStartSymbol;
718       numSymbol = pdsch->dmrs.nrOfDmrsSymbols + pdsch->pdschTimeAlloc.timeAlloc.numSymb;
719    }
720
721    /* Allocate the number of PRBs required for RAR PDSCH */
722    if((allocatePrbDl(cell, rarTime, startSymbol, numSymbol,\
723       &pdsch->pdschFreqAlloc.freqAlloc.startPrb, pdsch->pdschFreqAlloc.freqAlloc.numPrb)) != ROK)
724    {
725       DU_LOG("\nERROR  --> SCH : allocatePrbDl() failed for RAR");
726       return RFAILED;
727    }
728
729    pdsch->beamPdschInfo.numPrgs = 1;
730    pdsch->beamPdschInfo.prgSize = 1;
731    pdsch->beamPdschInfo.digBfInterfaces = 0;
732    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
733    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
734    pdsch->txPdschPower.powerControlOffset = 0;
735    pdsch->txPdschPower.powerControlOffsetSS = 0;
736
737    return ROK;
738 }
739
740 /**********************************************************************
741          End of file
742 **********************************************************************/