Jira id - ODUHIGH-241 calculation of tb size
[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 extern SchCb schCb[SCH_MAX_INST];
46 extern 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; /* configured to 0 */
66    raRnti = (1+symbolIdx+(14*slotIdx)+(14*80*freqIdx)+(14*80*8*ulCarrierIdx));
67         return raRnti;
68 }
69
70 /**
71  * @brief create raCb function. 
72  *
73  * @details
74  *
75  *     Function : createSchRaCb
76  *     
77  *     This function create raCb
78  *     
79  *  @param[in]  tcrnti
80  *  @param[in]  shed instance
81  *  @return  void
82  **/
83 void createSchRaCb(uint16_t tcrnti, Inst schInst)
84 {
85    schCb[schInst].cells[schInst]->raCb[0].tcrnti = tcrnti;
86 }
87
88 /**
89  * @brief resource allocation for msg3 PUSCH
90  *
91  * @details
92  *
93  *     Function : schAllocMsg3Pusch 
94  *     
95  *     This function handles msg3 PUSCH allocation
96  *     
97  *  @param[in]  Inst schInst, SCH instance
98  *  @param[in]  slot, current slot
99  *  @param[out]  msg3StartRb
100  *  @param[out]  msg3NumRb
101  *  @return  void
102  **/
103 uint8_t schAllocMsg3Pusch(Inst schInst, uint16_t slot, uint16_t *msg3StartRb,
104 uint8_t *msg3NumRb)
105 {
106    SchCellCb      *cell         = NULLP;
107    SchUlSlotInfo  *schUlSlotInfo    = NULLP;
108    uint8_t    puschMu       = 0;
109    uint8_t    msg3SlotAlloc = 0;
110    uint8_t    delta         = 0;
111    uint8_t    k2            = 0; 
112    uint8_t    startSymb     = 0;
113    uint8_t    symbLen       = 0; 
114    uint8_t    startRb       = 0;
115    uint8_t    numRb         = 0;
116    uint8_t    idx           = 0;
117    uint8_t    mcs            = 4;
118    uint8_t    numPdschSymbols= 14;
119    uint16_t   tbSize         = 0;
120
121    cell = schCb[schInst].cells[schInst];
122    //   puschMu = cell->cellCfg.puschMu;
123    delta = puschDeltaTable[puschMu];
124    k2 = cell->cellCfg.schInitialUlBwp.puschCommon.k2;
125    startSymb = cell->cellCfg.schInitialUlBwp.puschCommon.startSymbol;
126    symbLen = cell->cellCfg.schInitialUlBwp.puschCommon.lengthSymbol;
127
128    /* Slot allocation for msg3 based on 38.214 section 6.1.2.1 */
129    msg3SlotAlloc = slot + k2 + delta;
130    msg3SlotAlloc = msg3SlotAlloc % SCH_NUM_SLOTS; 
131
132    startRb = cell->schUlSlotInfo[msg3SlotAlloc]->puschCurrentPrb;
133    tbSize = schCalcTbSize(8); /* 6 bytes msg3  and 2 bytes header */
134    numRb = schCalcNumPrb(tbSize, mcs, numPdschSymbols);
135
136    /* allocating 1 extra RB for now */
137    numRb++;
138    /* increment PUSCH PRB */
139    cell->schUlSlotInfo[msg3SlotAlloc]->puschCurrentPrb += numRb;
140
141    for(idx=startSymb; idx<symbLen; idx++)
142    {
143       cell->schUlSlotInfo[msg3SlotAlloc]->assignedPrb[idx] = startRb + numRb;
144    }
145    schUlSlotInfo = cell->schUlSlotInfo[msg3SlotAlloc];
146
147    SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
148    if(!schUlSlotInfo->schPuschInfo)
149    {
150       DU_LOG("SCH: Memory allocation failed in schAllocMsg3Pusch");
151       return RFAILED;
152    }
153    tbSize = 0;  /* since nPrb has been incremented, recalculating tbSize */
154    tbSize = schCalcTbSizeFromNPrb(numRb, mcs, numPdschSymbols);
155
156    schUlSlotInfo->schPuschInfo->harqProcId        = SCH_HARQ_PROC_ID;
157    schUlSlotInfo->schPuschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
158    schUlSlotInfo->schPuschInfo->fdAlloc.startPrb  = startRb;
159    schUlSlotInfo->schPuschInfo->fdAlloc.numPrb    = numRb;
160    schUlSlotInfo->schPuschInfo->tdAlloc.startSymb = startSymb;
161    schUlSlotInfo->schPuschInfo->tdAlloc.numSymb   = symbLen;
162    schUlSlotInfo->schPuschInfo->tbInfo.mcs             = mcs;
163    schUlSlotInfo->schPuschInfo->tbInfo.ndi        = 1; /* new transmission */
164    schUlSlotInfo->schPuschInfo->tbInfo.rv         = 0;
165    schUlSlotInfo->schPuschInfo->tbInfo.tbSize     = tbSize; /*Considering 2 PRBs */
166    schUlSlotInfo->schPuschInfo->dmrsMappingType   = DMRS_MAP_TYPE_A;  /* Setting Type-A */
167    schUlSlotInfo->schPuschInfo->nrOfDmrsSymbols   = NUM_DMRS_SYMBOLS;
168    schUlSlotInfo->schPuschInfo->dmrsAddPos        = DMRS_ADDITIONAL_POS;
169
170    *msg3StartRb = startRb;
171    *msg3NumRb   = numRb;
172
173    return ROK;
174 }
175
176
177
178 /**
179  * @brief process rach indication function. 
180  *
181  * @details
182  *
183  *     Function : schProcessRachInd
184  *     
185  *     This function process rach indication
186  *     
187  *  @param[in]  rachInd parameters
188  *  @param[in]  shed instance
189  *  @return  ROK
190  **/
191 uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst)
192 {
193    SchCellCb *cell = schCb[schInst].cells[schInst];
194    RarInfo *rarInfo = NULLP;
195    uint16_t raRnti = 0;
196    uint16_t rarSlot = 0;
197    uint16_t msg3StartRb;
198    uint8_t  msg3NumRb;
199    uint8_t  ret = ROK;
200
201    /* RAR will sent with a delay of RAR_DELAY */
202    rarSlot = (rachInd->timingInfo.slot+RAR_DELAY+PHY_DELTA)%SCH_NUM_SLOTS;
203
204    SchDlSlotInfo *schDlSlotInfo = cell->schDlSlotInfo[rarSlot]; /* RAR will sent in the next slot */
205
206    /* Allocate the rarInfo, this pointer will be checked at schProcessSlotInd function */
207    SCH_ALLOC(rarInfo, sizeof(RarInfo));
208    if(rarInfo == NULLP)
209    {
210       DU_LOG("\nMAC: Memory Allocation failed for rarInfo");
211       return RFAILED;
212    }
213
214    schDlSlotInfo->rarInfo = rarInfo;
215
216    /* calculate the ra-rnti value */
217    raRnti = calculateRaRnti(rachInd->symbolIdx,rachInd->slotIdx,rachInd->freqIdx);
218
219    /* create raCb at SCH */
220    createSchRaCb(rachInd->crnti,schInst);
221
222    /* allocate resources for msg3 */
223    ret = schAllocMsg3Pusch(schInst, rarSlot, &msg3StartRb, &msg3NumRb);
224    if(ret == ROK)
225    {
226       /* fill RAR info */
227       rarInfo->raRnti                 = raRnti;
228       rarInfo->tcrnti                 = rachInd->crnti;
229       rarInfo->RAPID                  = rachInd->preambleIdx;
230       rarInfo->ta                     = rachInd->timingAdv;
231       rarInfo->msg3FreqAlloc.startPrb = msg3StartRb;
232       rarInfo->msg3FreqAlloc.numPrb   = msg3NumRb;
233    }
234    return ret;
235 }
236
237 /**
238  * @brief fill RAR info function. 
239  *
240  * @details
241  *
242  *     Function : calculateRaRnti
243  *     
244  *     This function fills pdcch and pdsch info for RAR
245  *     
246  *  @param[in]  rar Allocation info
247  *  @param[in]  ra-rnti
248  *  @param[in]  PCI
249  *  @param[in]  offset to pointA to determine freq alloc
250  *  @return  ROK
251  **/
252 uint8_t schFillRar(RarAlloc *rarAlloc, uint16_t raRnti, uint16_t pci, uint8_t offsetPointA)
253 {
254    Inst inst = 0;
255    uint8_t coreset0Idx = 0;
256    uint8_t numRbs = 0;
257    uint8_t firstSymbol = 0;
258    uint8_t numSymbols = 0;
259    uint8_t offset = 0;
260    uint8_t FreqDomainResource[6] = {0};
261    uint16_t tbSize = 0;
262    uint8_t numPdschSymbols = 12; /* considering pdsch region from 2 to 13 */
263    uint8_t mcs = 4;  /* MCS fixed to 4 */
264
265    SchBwpDlCfg *initialBwp = &schCb[inst].cells[inst]->cellCfg.schInitialDlBwp;
266
267    PdcchCfg *pdcch = &rarAlloc->rarPdcchCfg;
268    PdschCfg *pdsch = &rarAlloc->rarPdschCfg;
269    BwpCfg *bwp = &rarAlloc->bwp;
270
271    coreset0Idx     = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
272
273    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
274    numRbs        = coresetIdxTable[coreset0Idx][1];
275    numSymbols    = coresetIdxTable[coreset0Idx][2];
276    offset        = coresetIdxTable[coreset0Idx][3];
277
278    /* calculate time domain parameters */
279    // note: since slot value is made sl1, RAR can be sent at all slots
280    uint16_t mask = 0x2000;
281    for(firstSymbol=0; firstSymbol<14;firstSymbol++)
282    {
283       if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
284          break;
285       else
286          mask = mask>>1;
287    }
288
289    /* calculate the PRBs */
290    schAllocFreqDomRscType0(((offsetPointA-offset)/6), (numRbs/6), FreqDomainResource);
291
292    /* fill BWP */
293    bwp->freqAlloc.numPrb   = initialBwp->bwp.freqAlloc.numPrb;
294    bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
295    bwp->subcarrierSpacing  = initialBwp->bwp.scs;
296    bwp->cyclicPrefix       = initialBwp->bwp.cyclicPrefix;
297
298    /* fill the PDCCH PDU */
299    pdcch->coreset0Cfg.startSymbolIndex = firstSymbol;
300    pdcch->coreset0Cfg.durationSymbols = numSymbols;
301    memcpy(pdcch->coreset0Cfg.freqDomainResource,FreqDomainResource,6);
302    pdcch->coreset0Cfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
303    pdcch->coreset0Cfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
304    pdcch->coreset0Cfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
305    pdcch->coreset0Cfg.coreSetType = 0;
306    pdcch->coreset0Cfg.shiftIndex = pci;
307    pdcch->coreset0Cfg.precoderGranularity = 0; /* sameAsRegBundle */
308    pdcch->numDlDci = 1;
309    pdcch->dci.rnti = raRnti; /* RA-RNTI */
310    pdcch->dci.scramblingId = pci;
311    pdcch->dci.scramblingRnti = 0;
312    pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
313    pdcch->dci.aggregLevel = 4;
314    pdcch->dci.beamPdcchInfo.numPrgs = 1;
315    pdcch->dci.beamPdcchInfo.prgSize = 1;
316    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
317    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
318    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
319    pdcch->dci.txPdcchPower.powerValue = 0;
320    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
321    pdcch->dci.pdschCfg = pdsch;
322
323    /* fill the PDSCH PDU */
324    uint8_t cwCount = 0;
325    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
326    pdsch->rnti = raRnti; /* RA-RNTI */
327    pdsch->pduIndex = 0;
328    pdsch->numCodewords = 1;
329    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
330    {
331       pdsch->codeword[cwCount].targetCodeRate = 308;
332       pdsch->codeword[cwCount].qamModOrder = 2;
333       pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
334       pdsch->codeword[cwCount].mcsTable = 0;   /* notqam256 */
335       pdsch->codeword[cwCount].rvIndex = 0;
336       tbSize = schCalcTbSize(10); /* 8 bytes RAR and 2 bytes padding */
337       pdsch->codeword[cwCount].tbSize = tbSize;
338    }
339    pdsch->dataScramblingId = pci;
340    pdsch->numLayers = 1;
341    pdsch->transmissionScheme = 0;
342    pdsch->refPoint = 0;
343    pdsch->dmrs.dlDmrsSymbPos = 2;
344    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
345    pdsch->dmrs.dlDmrsScramblingId = pci;
346    pdsch->dmrs.scid = 0;
347    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
348    pdsch->dmrs.dmrsPorts = 0;
349    pdsch->dmrs.mappingType      = DMRS_MAP_TYPE_A;  /* Type-A */
350    pdsch->dmrs.nrOfDmrsSymbols  = NUM_DMRS_SYMBOLS;
351    pdsch->dmrs.dmrsAddPos       = DMRS_ADDITIONAL_POS;
352    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
353    pdsch->pdschFreqAlloc.freqAlloc.startPrb = offset + SCH_SSB_NUM_PRB; /* the RB numbering starts from coreset0, and PDSCH is always above SSB */ 
354    pdsch->pdschFreqAlloc.freqAlloc.numPrb = schCalcNumPrb(tbSize,mcs,numPdschSymbols);
355    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
356    pdsch->pdschTimeAlloc.timeAlloc.startSymb = initialBwp->pdschCommon.startSymbol;
357    pdsch->pdschTimeAlloc.timeAlloc.numSymb = initialBwp->pdschCommon.lengthSymbol;
358    pdsch->beamPdschInfo.numPrgs = 1;
359    pdsch->beamPdschInfo.prgSize = 1;
360    pdsch->beamPdschInfo.digBfInterfaces = 0;
361    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
362    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
363    pdsch->txPdschPower.powerControlOffset = 0;
364    pdsch->txPdschPower.powerControlOffsetSS = 0;
365
366    return ROK;
367 }
368
369 /**********************************************************************
370          End of file
371 **********************************************************************/