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