Modified SCH design to process pending RACH Ind when Slot Ind is received [Issue...
[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 SchCb schCb[SCH_MAX_INST];
46 uint8_t puschDeltaTable[MAX_MU_PUSCH];
47
48 /**
49  * @brief calculate ra-rnti function. 
50  *
51  * @details
52  *
53  *     Function : calculateRaRnti
54  *     
55  *     This function calculates ra-rnti
56  *     
57  *  @param[in]  symbol index
58  *  @param[in]  slot index
59  *  @param[in]  frequency index
60  *  @return  ra-rnti
61  **/
62 uint16_t calculateRaRnti(uint8_t symbolIdx, uint8_t slotIdx, uint8_t freqIdx)
63 {
64    uint16_t raRnti = 0;
65    uint8_t  ulCarrierIdx = 0; /* Uplink carrier used for MSG1 transmission. 0:NUL carrier; 1:SUL carrier */
66    
67    /* Refer to spec 38.321, section 5.1.3 */
68    raRnti = (1 + symbolIdx + (14*slotIdx) + (14*80*freqIdx) + (14*80*8*ulCarrierIdx));
69    return raRnti;
70 }
71
72 /**
73  * @brief create raCb function. 
74  *
75  * @details
76  *
77  *     Function : createSchRaCb
78  *     
79  *     This function create raCb
80  *     
81  *  @param[in]  tcrnti
82  *  @param[in]  shed instance
83  *  @return  void
84  **/
85 void createSchRaCb(uint16_t tcrnti, Inst schInst)
86 {
87    uint8_t ueIdx = 0;
88
89    GET_UE_IDX(tcrnti, ueIdx);
90    schCb[schInst].cells[schInst]->raCb[ueIdx -1].tcrnti = tcrnti;
91 }
92
93 /**
94  * @brief resource allocation for msg3 PUSCH
95  *
96  * @details
97  *
98  *     Function : schAllocMsg3Pusch 
99  *     
100  *     This function handles msg3 PUSCH allocation
101  *     
102  *  @param[in]  Inst schInst, SCH instance
103  *  @param[in]  slot, current slot
104  *  @param[out]  msg3StartRb
105  *  @param[out]  msg3NumRb
106  *  @return  void
107  **/
108 uint8_t schAllocMsg3Pusch(Inst schInst, uint16_t slot, uint16_t crnti, \
109    uint16_t *msg3StartRb, uint8_t *msg3NumRb, uint16_t msg3SlotAlloc)
110 {
111    SchCellCb      *cell         = NULLP;
112    SchUlSlotInfo  *schUlSlotInfo    = NULLP;
113    uint8_t    startSymb     = 0;
114    uint8_t    symbLen       = 0; 
115    uint8_t    startRb       = 0;
116    uint8_t    numRb         = 0;
117    uint8_t    idx           = 0;
118    uint8_t    mcs            = 4;
119    uint8_t    numPdschSymbols= 14;
120    uint16_t   tbSize         = 0;
121
122    cell = schCb[schInst].cells[schInst];
123    startSymb = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[0].startSymbol;
124    symbLen = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[0].symbolLength;
125
126    startRb = cell->schUlSlotInfo[msg3SlotAlloc]->puschCurrentPrb;
127    tbSize = schCalcTbSize(8); /* 6 bytes msg3  and 2 bytes header */
128    numRb = schCalcNumPrb(tbSize, mcs, numPdschSymbols);
129
130    /* allocating 1 extra RB for now */
131    numRb++;
132    /* increment PUSCH PRB */
133    cell->schUlSlotInfo[msg3SlotAlloc]->puschCurrentPrb += numRb;
134
135    for(idx=startSymb; idx<symbLen; idx++)
136    {
137       cell->schUlSlotInfo[msg3SlotAlloc]->assignedPrb[idx] = startRb + numRb;
138    }
139    schUlSlotInfo = cell->schUlSlotInfo[msg3SlotAlloc];
140
141    SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
142    if(!schUlSlotInfo->schPuschInfo)
143    {
144       DU_LOG("\nERROR  -->  SCH :  Memory allocation failed in schAllocMsg3Pusch");
145       return RFAILED;
146    }
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;
166
167    *msg3StartRb = startRb;
168    *msg3NumRb   = numRb;
169    return ROK;
170 }
171
172 /**
173  * @brief Processes any pending RA request
174  *
175  * @details
176  *
177  *     Function : schProcessRaReq
178  *
179  *     This function process pending RA request
180  *
181  *  @param[in]  Current timing of the cell
182  *  @return  ROK
183  **/
184 void schProcessRaReq(SlotTimingInfo currTime, SchCellCb *cell)
185 {
186    uint8_t ueIdx = 0;
187    RarInfo *rarInfo = NULLP;
188    uint16_t raRnti = 0;
189    uint16_t rarSlot = 0;
190    uint16_t msg3StartRb;
191    uint8_t  msg3NumRb;
192    uint8_t  ret = ROK;
193    uint8_t delta = 0;
194    uint8_t k2 = 0;
195    uint8_t puschMu = 0;
196    uint16_t msg3Slot = 0;
197 #ifdef NR_TDD
198    uint16_t slotIdx = 0;
199 #endif
200
201    while(ueIdx < MAX_NUM_UE)
202    {
203       if(cell->raReq[ueIdx] == NULLP)
204       {
205          ueIdx++;
206          continue;
207       }
208
209       //puschMu = cell->cellCfg.puschMu;
210       delta = puschDeltaTable[puschMu];
211       k2 = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[0].k2;
212
213       /* RAR will sent with a delay of RAR_DELAY */
214       rarSlot = (currTime.slot + RAR_DELAY + PHY_DELTA_DL) % cell->numSlots;
215 #ifdef NR_TDD
216       for(slotIdx=0; slotIdx<cell->numSlots;slotIdx++)
217       {
218          /* Slot allocation for msg3 based on 38.214 section 6.1.2.1 */
219          msg3Slot = (rarSlot+delta+k2)%cell->numSlots;
220
221          if((schGetSlotSymbFrmt(rarSlot, cell->slotFrmtBitMap) != DL_SLOT) &&\
222                (schGetSlotSymbFrmt(msg3Slot, cell->slotFrmtBitMap) != UL_SLOT))
223          {
224             rarSlot = (rarSlot + 1) % cell->numSlots;
225             continue;
226          }
227          break;
228       }
229       if(slotIdx>=cell->numSlots)
230       {
231          DU_LOG("\nERROR  -->  SCH : NO Slot for Msg2 with Msg3 Grant\n");
232          return RFAILED;
233       }
234 #else
235       /* Slot allocation for msg3 based on 38.214 section 6.1.2.1 */
236       msg3Slot = rarSlot + k2 + delta;
237       msg3Slot = msg3Slot % cell->numSlots; 
238 #endif
239
240       SchDlSlotInfo *schDlSlotInfo = cell->schDlSlotInfo[rarSlot]; /* RAR will sent in the next slot */
241
242       /* Allocate the rarInfo, this pointer will be checked at schProcessSlotInd function */
243       SCH_ALLOC(rarInfo, sizeof(RarInfo));
244       if(rarInfo == NULLP)
245       {
246          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for rarInfo");
247          return RFAILED;
248       }
249
250       schDlSlotInfo->rarInfo = rarInfo;
251
252       /* create raCb at SCH */
253       createSchRaCb(cell->raReq[ueIdx]->rachInd->crnti, cell->instIdx);
254
255       /* allocate resources for msg3 */
256       ret = schAllocMsg3Pusch(cell->instIdx, rarSlot, cell->raReq[ueIdx]->rachInd->crnti, &msg3StartRb, &msg3NumRb, msg3Slot);
257       if(ret == ROK)
258       {
259          /* fill RAR info */
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;
266       }
267
268       SCH_FREE(cell->raReq[ueIdx]->rachInd, sizeof(RachIndInfo));
269       SCH_FREE(cell->raReq[ueIdx], sizeof(SchRaReq));
270       ueIdx++;
271
272    } /* End of while(ueIdx < MAX_NUM_UE) */
273 }
274
275 /**
276  * @brief process rach indication function. 
277  *
278  * @details
279  *
280  *     Function : schProcessRachInd
281  *     
282  *     This function process rach indication
283  *     
284  *  @param[in]  rachInd parameters
285  *  @param[in]  shed instance
286  *  @return  ROK
287  **/
288 uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst)
289 {
290    SchCellCb *cell = schCb[schInst].cells[schInst];
291    SchRaReq  *raReq = NULLP;
292    float    slotDuration;
293    uint8_t  winNumSlots;
294    uint8_t  ueIdx;
295
296    SCH_ALLOC(raReq, sizeof(SchRaReq));
297    if(!raReq)
298    {
299       DU_LOG("\nERROR  -->  SCH : Memory allocation failure in schProcessRachInd");
300       SCH_FREE(rachInd, sizeof(RachIndInfo));
301       return RFAILED;
302    }
303
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;
309   
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;
313    
314    /* Adding window size to window start time to get window end time */
315    ADD_DELTA_TO_TIME(raReq->winStartTime, raReq->winEndTime, winNumSlots);
316
317    /* Storing RA request in cellCb */
318    GET_UE_IDX(rachInd->crnti, ueIdx);
319    cell->raReq[ueIdx] = raReq;
320
321    return ROK;
322 }
323
324 /**
325  * @brief fill RAR info function. 
326  *
327  * @details
328  *
329  *     Function : calculateRaRnti
330  *     
331  *     This function fills pdcch and pdsch info for RAR
332  *     
333  *  @param[in]  rar Allocation info
334  *  @param[in]  ra-rnti
335  *  @param[in]  PCI
336  *  @param[in]  offset to pointA to determine freq alloc
337  *  @return  ROK
338  **/
339 uint8_t schFillRar(RarAlloc *rarAlloc, uint16_t raRnti, uint16_t pci, uint8_t offsetPointA, \
340 bool ssbPresent, bool sib1Present)
341 {
342    Inst inst = 0;
343    uint8_t coreset0Idx = 0;
344    uint8_t numRbs = 0;
345    uint8_t firstSymbol = 0;
346    uint8_t numSymbols = 0;
347    uint8_t offset = 0;
348    uint8_t FreqDomainResource[6] = {0};
349    uint16_t tbSize = 0;
350    uint8_t mcs = 4;  /* MCS fixed to 4 */
351
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;
357
358    coreset0Idx     = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
359
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];
364
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++)
369    {
370       if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
371          break;
372       else
373          mask = mask>>1;
374    }
375
376    /* calculate the PRBs */
377    freqDomRscAllocType0(((offsetPointA-offset)/6), (numRbs/6), FreqDomainResource);
378
379    /* fill BWP */
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;
384
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 */
395    pdcch->numDlDci = 1;
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;
409
410    /* fill the PDSCH PDU */
411    uint8_t cwCount = 0;
412    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
413    pdsch->rnti = raRnti; /* RA-RNTI */
414    pdsch->pduIndex = 0;
415    pdsch->numCodewords = 1;
416    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
417    {
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;
426    }
427    pdsch->dataScramblingId = pci;
428    pdsch->numLayers = 1;
429    pdsch->transmissionScheme = 0;
430    pdsch->refPoint = 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;
443    if(ssbPresent)
444    {
445       /* PDSCH is always above SSB */
446       pdsch->pdschFreqAlloc.freqAlloc.startPrb = offsetPointA + SCH_SSB_NUM_PRB + 1;
447    }
448    if(sib1Present)
449    {
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; 
453    }
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;
466
467    return ROK;
468 }
469
470 /**********************************************************************
471          End of file
472 **********************************************************************/