Merge "Cleanup of message sib1"
[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
35 #include "stdbool.h"
36 #include "envopt.h"        /* environment options */
37 #include "envdep.h"        /* environment dependent */
38 #include "envind.h"        /* environment independent */
39 #include "gen.h"           /* general layer */
40 #include "ssi.h"           /* system service interface */
41 #include "cm_tkns.h"       /* Common Token Defines */
42 #include "cm_llist.h"      /* Common Link List Defines */
43 #include "cm_hash.h"       /* Common Hash List Defines */
44 #include "cm_mblk.h"       /* common memory link list library */
45 #include "cm_lte.h"        /* Common LTE Defines */
46 #include "tfu.h"
47 #include "lrg.h"
48
49 #include "gen.x"           /* general layer typedefs */
50 #include "ssi.x"           /* system services typedefs */
51 #include "cm5.x"           /* system services */
52 #include "cm_tkns.x"       /* Common Token Definitions */
53 #include "cm_llist.x"      /* Common Link List Definitions */
54 #include "cm_lib.x"        /* Common Library Definitions */
55 #include "cm_hash.x"       /* Common Hash List Definitions */
56 #include "cm_mblk.x"       /* common memory link list library */
57 #include "cm_lte.x"        /* Common LTE Defines */
58 #include "tfu.x"
59 #include "lrg.x"
60 #include "du_log.h"
61 #include "du_app_mac_inf.h"
62 #include "mac_sch_interface.h"
63 #include "sch.h"
64 #include "sch_utils.h"
65
66 extern SchCb schCb[SCH_MAX_INST];
67 extern uint8_t puschDeltaTable[MAX_MU_PUSCH];
68
69 /**
70  * @brief calculate ra-rnti function. 
71  *
72  * @details
73  *
74  *     Function : calculateRaRnti
75  *     
76  *     This function calculates ra-rnti
77  *     
78  *  @param[in]  symbol index
79  *  @param[in]  slot index
80  *  @param[in]  frequency index
81  *  @return  ra-rnti
82  **/
83 uint16_t calculateRaRnti(uint8_t symbolIdx, uint8_t slotIdx, uint8_t freqIdx)
84 {
85    uint16_t raRnti = 0;
86         uint8_t ulCarrierIdx = 0; /* configured to 0 */
87    raRnti = (1+symbolIdx+(14*slotIdx)+(14*80*freqIdx)+(14*80*8*ulCarrierIdx));
88         return raRnti;
89 }
90
91 /**
92  * @brief create raCb function. 
93  *
94  * @details
95  *
96  *     Function : createSchRaCb
97  *     
98  *     This function create raCb
99  *     
100  *  @param[in]  tcrnti
101  *  @param[in]  shed instance
102  *  @return  void
103  **/
104 void createSchRaCb(uint16_t tcrnti, Inst schInst)
105 {
106         schCb[schInst].cells[schInst]->raCb[0].tcrnti = tcrnti;
107 }
108
109 /**
110  * @brief resource allocation for msg3 PUSCH
111  *
112  * @details
113  *
114  *     Function : schAllocMsg3Pusch 
115  *     
116  *     This function handles msg3 PUSCH allocation
117  *     
118  *  @param[in]  Inst schInst, SCH instance
119  *  @param[in]  slot, current slot
120  *  @param[out]  msg3StartRb
121  *  @param[out]  msg3NumRb
122  *  @return  void
123  **/
124 uint8_t schAllocMsg3Pusch(Inst schInst, uint16_t slot, uint16_t *msg3StartRb,
125 uint8_t *msg3NumRb)
126 {
127         SchCellCb  *cell         = NULLP;
128         SchUlAlloc *ulAlloc      = NULLP;
129         uint8_t    puschMu       = 0;
130         uint8_t    msg3SlotAlloc = 0;
131         uint8_t    delta         = 0;
132         uint8_t    k2            = 0; 
133         uint8_t    startSymb     = 0;
134         uint8_t    symbLen       = 0; 
135         uint8_t    startRb       = 0;
136         uint8_t    numRb         = 0;
137         uint8_t    idx           = 0;
138
139
140    cell = schCb[schInst].cells[schInst];
141         puschMu = cell->cellCfg.puschMu;
142         delta = puschDeltaTable[puschMu];
143         k2 = cell->cellCfg.schInitialUlBwp.puschCommon.k2;
144         startSymb = cell->cellCfg.schInitialUlBwp.puschCommon.startSymbol;
145         symbLen = cell->cellCfg.schInitialUlBwp.puschCommon.lengthSymbol;
146
147         /* Slot allocation for msg3 based on 38.214 section 6.1.2.1 */
148         msg3SlotAlloc = slot + k2 + delta;
149         msg3SlotAlloc = msg3SlotAlloc % SCH_NUM_SLOTS; 
150
151         startRb = PUSCH_START_RB;
152
153         /* formula used for calculation of rbSize, 38.214 section 6.1.4.2 
154          * Ninfo = S.Nre.R.Qm.v
155          * Nre'  = Nsc.NsymPdsch-NdmrsSymb-Noh
156          * Nre   = min(156,Nre').nPrb */
157         numRb = 1; /* based on above calculation */
158
159         /* allocating 1 extra RB for now */
160         numRb++;
161
162         for(idx=startSymb; idx<symbLen; idx++)
163         {
164                 cell->ulAlloc[msg3SlotAlloc]->assignedPrb[idx] = startRb + numRb;
165         }
166         ulAlloc = cell->ulAlloc[msg3SlotAlloc];
167
168    SCH_ALLOC(ulAlloc->schPuschInfo, sizeof(SchPuschInfo));
169         if(!ulAlloc->schPuschInfo)
170         {
171       DU_LOG("SCH: Memory allocation failed in schAllocMsg3Pusch");
172                 return RFAILED;
173         }
174         ulAlloc->schPuschInfo->harqProcId        = SCH_HARQ_PROC_ID;
175         ulAlloc->schPuschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
176         ulAlloc->schPuschInfo->fdAlloc.startPrb  = startRb;
177         ulAlloc->schPuschInfo->fdAlloc.numPrb    = numRb;
178         ulAlloc->schPuschInfo->tdAlloc.startSymb = startSymb;
179         ulAlloc->schPuschInfo->tdAlloc.numSymb   = symbLen;
180         ulAlloc->schPuschInfo->tbInfo.mcs            = 4;
181         ulAlloc->schPuschInfo->tbInfo.ndi        = 1; /* new transmission */
182         ulAlloc->schPuschInfo->tbInfo.rv                = 0;
183         ulAlloc->schPuschInfo->tbInfo.tbSize     = 24; /*Considering 2 PRBs */
184
185         *msg3StartRb = startRb;
186         *msg3NumRb   = numRb;
187
188         return ROK;
189 }
190
191
192
193 /**
194  * @brief process rach indication function. 
195  *
196  * @details
197  *
198  *     Function : schProcessRachInd
199  *     
200  *     This function process rach indication
201  *     
202  *  @param[in]  rachInd parameters
203  *  @param[in]  shed instance
204  *  @return  ROK
205  **/
206 uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst)
207 {
208    SchCellCb *cell = schCb[schInst].cells[schInst];
209         uint16_t raRnti = 0;
210         uint16_t slot;
211         uint16_t msg3StartRb;
212         uint8_t  msg3NumRb;
213    uint8_t  ret = ROK;
214    /* RAR will sent in the next slot */
215         slot = (rachInd->timingInfo.slot+SCHED_DELTA+RAR_DELAY)%SCH_NUM_SLOTS;
216
217    SchDlAlloc *dlAlloc =  cell->dlAlloc[slot];
218         RarInfo *rarInfo = &(dlAlloc->rarInfo);
219
220    /* rar message presense in next slot ind and will be scheduled */
221    dlAlloc->rarPres = true;
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, slot, &msg3StartRb, &msg3NumRb);
231         if(ret == ROK)
232         {
233
234                 /* fill RAR info */
235                 rarInfo->raRnti      = raRnti;
236                 rarInfo->tcrnti      = rachInd->crnti;
237                 rarInfo->RAPID       = rachInd->preambleIdx;
238                 rarInfo->ta          = rachInd->timingAdv;
239                 rarInfo->msg3StartRb = msg3StartRb;
240                 rarInfo->msg3NumRb   = msg3NumRb;
241         }
242    return ret;
243 }
244
245 /**
246  * @brief fill RAR info function. 
247  *
248  * @details
249  *
250  *     Function : calculateRaRnti
251  *     
252  *     This function fills pdcch and pdsch info for RAR
253  *     
254  *  @param[in]  rar Allocation info
255  *  @param[in]  ra-rnti
256  *  @param[in]  PCI
257  *  @param[in]  offset to pointA to determine freq alloc
258  *  @return  ROK
259  **/
260 uint8_t schFillRar(RarAlloc *rarAlloc, uint16_t raRnti, uint16_t pci, uint8_t offsetPointA)
261 {
262    Inst inst = 0;
263    uint8_t coreset0Idx = 0;
264    uint8_t numRbs = 0;
265         uint8_t firstSymbol = 0;
266    uint8_t numSymbols = 0;
267    uint8_t offset = 0;
268    uint8_t FreqDomainResource[6] = {0};
269    SchBwpDlCfg *initialBwp = &schCb[inst].cells[inst]->cellCfg.schInitialDlBwp;
270
271         PdcchCfg *pdcch = &rarAlloc->rarPdcchCfg;
272         PdschCfg *pdsch = &rarAlloc->rarPdschCfg;
273
274    coreset0Idx     = initialBwp->pdcchCommon.raSearchSpace.coresetId;
275
276    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
277    numRbs        = coresetIdxTable[coreset0Idx][1];
278    numSymbols    = coresetIdxTable[coreset0Idx][2];
279    offset        = coresetIdxTable[coreset0Idx][3];
280
281    /* calculate time domain parameters */
282         // note: since slot value is made sl1, RAR can be sent at all slots
283         uint16_t mask = 0x2000;
284         for(firstSymbol=0; firstSymbol<14;firstSymbol++)
285         {
286            if(initialBwp->pdcchCommon.raSearchSpace.monitoringSymbol & mask)
287                    break;
288                 else
289                    mask = mask>>1;
290    }
291
292    /* calculate the PRBs */
293    calculatePRB( ((offsetPointA-offset)/6), (numRbs/6), FreqDomainResource);
294
295    /* fill the PDCCH PDU */
296    pdcch->pdcchBwpCfg.BWPSize = initialBwp->bwp.numPrb;
297    pdcch->pdcchBwpCfg.BWPStart = initialBwp->bwp.firstPrb;
298    pdcch->pdcchBwpCfg.subcarrierSpacing = initialBwp->bwp.scs;
299    pdcch->pdcchBwpCfg.cyclicPrefix = initialBwp->bwp.cyclicPrefix;
300    pdcch->coreset0Cfg.startSymbolIndex = firstSymbol;
301    pdcch->coreset0Cfg.durationSymbols = numSymbols;
302    memcpy(pdcch->coreset0Cfg.freqDomainResource,FreqDomainResource,6);
303    pdcch->coreset0Cfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
304    pdcch->coreset0Cfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
305    pdcch->coreset0Cfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
306    pdcch->coreset0Cfg.coreSetType = 0;
307    pdcch->coreset0Cfg.shiftIndex = pci;
308    pdcch->coreset0Cfg.precoderGranularity = 0; /* sameAsRegBundle */
309    pdcch->numDlDci = 1;
310    pdcch->dci.rnti = raRnti; /* RA-RNTI */
311    pdcch->dci.scramblingId = pci;
312    pdcch->dci.scramblingRnti = 0;
313    pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
314    pdcch->dci.aggregLevel = 4;
315    pdcch->dci.beamPdcchInfo.numPrgs = 1;
316    pdcch->dci.beamPdcchInfo.prgSize = 1;
317    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
318    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
319    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
320    pdcch->dci.txPdcchPower.powerValue = 0;
321    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
322         pdcch->dci.pdschCfg = pdsch;
323
324    /* fill the PDSCH PDU */
325         uint8_t cwCount = 0;
326    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
327    pdsch->rnti = raRnti; /* RA-RNTI */
328    pdsch->pduIndex = 0;
329    pdsch->pdschBwpCfg.BWPSize = initialBwp->bwp.numPrb;
330    pdsch->pdschBwpCfg.BWPStart = initialBwp->bwp.firstPrb;
331    pdsch->numCodewords = 1;
332         for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
333         {
334       pdsch->codeword[cwCount].targetCodeRate = 308;
335       pdsch->codeword[cwCount].qamModOrder = 2;
336       pdsch->codeword[cwCount].mcsIndex = 4; /* mcs configured to 4 */
337       pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
338       pdsch->codeword[cwCount].rvIndex = 0;
339       pdsch->codeword[cwCount].tbSize = 80/8; /* 38.214: Table 5.1.3.2-1,
340                    devided by 8 to get the value in bytes */
341    }
342    pdsch->dataScramblingId = pci;
343    pdsch->numLayers = 1;
344    pdsch->transmissionScheme = 0;
345    pdsch->refPoint = 0;
346    pdsch->dmrs.dlDmrsSymbPos = 2;
347    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
348    pdsch->dmrs.dlDmrsScramblingId = pci;
349    pdsch->dmrs.scid = 0;
350    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
351    pdsch->dmrs.dmrsPorts = 0;
352    pdsch->freqAlloc.resourceAlloc = 1; /* RAT type-1 RIV format */
353    pdsch->freqAlloc.rbStart = offset + SCH_SSB_PRB_DURATION; /* the RB numbering starts from coreset0, and PDSCH is always above SSB */ 
354         /* formula used for calculation of rbSize, 38.213 section 5.1.3.2 *
355          * Ninfo = S . Nre . R . Qm . v                                   *
356          * Nre' = Nsc . NsymPdsch - NdmrsSymb - Noh                       *
357          * Nre = min(156,Nre') . nPrb                                     */
358    pdsch->freqAlloc.rbSize = 1; /* This value is calculated from above formulae */
359    pdsch->freqAlloc.vrbPrbMapping = 0; /* non-interleaved */
360    pdsch->timeAlloc.startSymbolIndex = initialBwp->pdschCommon.startSymbol;
361    pdsch->timeAlloc.numSymbols = initialBwp->pdschCommon.lengthSymbol;
362    pdsch->beamPdschInfo.numPrgs = 1;
363    pdsch->beamPdschInfo.prgSize = 1;
364    pdsch->beamPdschInfo.digBfInterfaces = 0;
365    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
366    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
367    pdsch->txPdschPower.powerControlOffset = 0;
368    pdsch->txPdschPower.powerControlOffsetSS = 0;
369         
370         return ROK;
371 }
372
373 /**********************************************************************
374          End of file
375 **********************************************************************/