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