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