RARandSib1Transmission
[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 int8_t coresetIdxTable[MAX_CORESET_INDEX][4];
68 extern int8_t searchSpaceIdxTable[MAX_SEARCH_SPACE_INDEX][4];
69
70 /**
71  * @brief calculate ra-rnti function. 
72  *
73  * @details
74  *
75  *     Function : calculateRaRnti
76  *     
77  *     This function calculates ra-rnti
78  *     
79  *  @param[in]  symbol index
80  *  @param[in]  slot index
81  *  @param[in]  frequency index
82  *  @return  ra-rnti
83  **/
84 uint16_t calculateRaRnti(uint8_t symbolIdx, uint8_t slotIdx, uint8_t freqIdx)
85 {
86    uint16_t raRnti = 0;
87         uint8_t ulCarrierIdx = 0; /* configured to 0 */
88    raRnti = (1+symbolIdx+(14*slotIdx)+(14*80*freqIdx)+(14*80*8*ulCarrierIdx));
89         return raRnti;
90 }
91
92 /**
93  * @brief create raCb function. 
94  *
95  * @details
96  *
97  *     Function : createSchRaCb
98  *     
99  *     This function create raCb
100  *     
101  *  @param[in]  tcrnti
102  *  @param[in]  shed instance
103  *  @return  void
104  **/
105 void createSchRaCb(uint16_t tcrnti, Inst schInst)
106 {
107         schCb[schInst].cells[schInst]->raCb[0].tcrnti = tcrnti;
108 }
109
110 /**
111  * @brief process rach indication function. 
112  *
113  * @details
114  *
115  *     Function : schProcessRachInd
116  *     
117  *     This function process rach indication
118  *     
119  *  @param[in]  rachInd parameters
120  *  @param[in]  shed instance
121  *  @return  ROK
122  **/
123 uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst)
124 {
125    SchCellCb *cell = schCb[schInst].cells[schInst];
126         uint16_t raRnti = 0;
127
128    SchDlAlloc *dlAlloc =
129            cell->dlAlloc[(rachInd->timingInfo.slot+SCHED_DELTA+RAR_DELAY)%SCH_NUM_SLOTS]; /* RAR will sent in the next slot */
130         RarInfo *rarInfo = &(dlAlloc->rarInfo);
131
132    /* rar message presense in next slot ind and will be scheduled */
133    dlAlloc->rarPres = true;
134
135    /* calculate the ra-rnti value */
136         raRnti = calculateRaRnti(rachInd->symbolIdx,rachInd->slotIdx,rachInd->freqIdx);
137    
138         /* create raCb at SCH */
139         createSchRaCb(rachInd->crnti,schInst);
140
141         /* fill RAR info */
142         rarInfo->raRnti = raRnti;
143         rarInfo->tcrnti = rachInd->crnti;
144         rarInfo->RAPID = rachInd->preambleIdx;
145         rarInfo->ta = rachInd->timingAdv;
146         rarInfo->msg3StartRb = 0; /* will be set during implementation of msg3 */
147         rarInfo->msg3NumRb = 0; /* will be set during implementation of msg3 */
148
149    return ROK;
150 }
151
152 /**
153  * @brief fill RAR info function. 
154  *
155  * @details
156  *
157  *     Function : calculateRaRnti
158  *     
159  *     This function fills pdcch and pdsch info for RAR
160  *     
161  *  @param[in]  rar Allocation info
162  *  @param[in]  ra-rnti
163  *  @param[in]  PCI
164  *  @param[in]  offset to pointA to determine freq alloc
165  *  @return  ROK
166  **/
167 uint8_t schFillRar(RarAlloc *rarAlloc, uint16_t raRnti, uint16_t pci, uint8_t offsetPointA)
168 {
169    Inst inst = 0;
170    uint8_t coreset0Idx = 0;
171    uint8_t numRbs = 0;
172         uint8_t firstSymbol = 0;
173    uint8_t numSymbols = 0;
174    uint8_t offset = 0;
175    uint8_t FreqDomainResource[6] = {0};
176    SchBwpDlCfg *initialBwp = &schCb[inst].cells[inst]->cellCfg.schInitialBwp;
177
178         PdcchCfg *pdcch = &rarAlloc->rarPdcchCfg;
179         PdschCfg *pdsch = &rarAlloc->rarPdschCfg;
180
181    coreset0Idx     = initialBwp->pdcchCommon.raSearchSpace.coresetId;
182
183    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
184    numRbs        = coresetIdxTable[coreset0Idx][1];
185    numSymbols    = coresetIdxTable[coreset0Idx][2];
186    offset        = coresetIdxTable[coreset0Idx][3];
187
188    /* calculate time domain parameters */
189         // note: since slot value is made sl1, RAR can be sent at all slots
190         uint16_t mask = 0x2000;
191         for(firstSymbol=0; firstSymbol<14;firstSymbol++)
192         {
193            if(initialBwp->pdcchCommon.raSearchSpace.monitoringSymbol & mask)
194                    break;
195                 else
196                    mask = mask>>1;
197    }
198
199    /* calculate the PRBs */
200    freqDomResourceAlloc( ((offsetPointA-offset)/6), (numRbs/6), FreqDomainResource);
201
202    /* fill the PDCCH PDU */
203    pdcch->pdcchBwpCfg.BWPSize = initialBwp->bwp.numPrb;
204    pdcch->pdcchBwpCfg.BWPStart = initialBwp->bwp.firstPrb;
205    pdcch->pdcchBwpCfg.subcarrierSpacing = initialBwp->bwp.scs;
206    pdcch->pdcchBwpCfg.cyclicPrefix = initialBwp->bwp.cyclicPrefix;
207    pdcch->coreset0Cfg.startSymbolIndex = firstSymbol;
208    pdcch->coreset0Cfg.durationSymbols = numSymbols;
209    memcpy(pdcch->coreset0Cfg.freqDomainResource,FreqDomainResource,6);
210    pdcch->coreset0Cfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
211    pdcch->coreset0Cfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
212    pdcch->coreset0Cfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
213    pdcch->coreset0Cfg.coreSetType = 0;
214    pdcch->coreset0Cfg.shiftIndex = pci;
215    pdcch->coreset0Cfg.precoderGranularity = 0; /* sameAsRegBundle */
216    pdcch->numDlDci = 1;
217    pdcch->dci.rnti = raRnti; /* RA-RNTI */
218    pdcch->dci.scramblingId = pci;
219    pdcch->dci.scramblingRnti = 0;
220    pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
221    pdcch->dci.aggregLevel = 4;
222    pdcch->dci.beamPdcchInfo.numPrgs = 1;
223    pdcch->dci.beamPdcchInfo.prgSize = 1;
224    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
225    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
226    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
227    pdcch->dci.txPdcchPower.powerValue = 0;
228    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
229         pdcch->dci.pdschCfg = pdsch;
230
231    /* fill the PDSCH PDU */
232         uint8_t cwCount = 0;
233    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
234    pdsch->rnti = raRnti; /* RA-RNTI */
235    pdsch->pduIndex = 0;
236    pdsch->pdschBwpCfg.BWPSize = initialBwp->bwp.numPrb;
237    pdsch->pdschBwpCfg.BWPStart = initialBwp->bwp.firstPrb;
238    pdsch->numCodewords = 1;
239         for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
240         {
241       pdsch->codeword[cwCount].targetCodeRate = 308;
242       pdsch->codeword[cwCount].qamModOrder = 2;
243       pdsch->codeword[cwCount].mcsIndex = 4; /* mcs configured to 4 */
244       pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
245       pdsch->codeword[cwCount].rvIndex = 0;
246       pdsch->codeword[cwCount].tbSize = 80/8; /* 38.214: Table 5.1.3.2-1,
247                    devided by 8 to get the value in bytes */
248    }
249    pdsch->dataScramblingId = pci;
250    pdsch->numLayers = 1;
251    pdsch->transmissionScheme = 0;
252    pdsch->refPoint = 0;
253    pdsch->dmrs.dlDmrsSymbPos = 2;
254    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
255    pdsch->dmrs.dlDmrsScramblingId = pci;
256    pdsch->dmrs.scid = 0;
257    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
258    pdsch->dmrs.dmrsPorts = 0;
259    pdsch->freqAlloc.resourceAlloc = 1; /* RAT type-1 RIV format */
260    pdsch->freqAlloc.rbStart = offset + SCH_SSB_PRB_DURATION; /* the RB numbering starts from coreset0, and PDSCH is always above SSB */ 
261         /* formula used for calculation of rbSize, 38.213 section 5.1.3.2 *
262          * Ninfo = S . Nre . R . Qm . v                                   *
263          * Nre' = Nsc . NsymPdsch - NdmrsSymb - Noh                       *
264          * Nre = min(156,Nre') . nPrb                                     */
265    pdsch->freqAlloc.rbSize = 1; /* This value is calculated from above formulae */
266    pdsch->freqAlloc.vrbPrbMapping = 0; /* non-interleaved */
267    pdsch->timeAlloc.startSymbolIndex = 2; /* spec-38.214, Table 5.1.2.1-1 */
268    pdsch->timeAlloc.numSymbols = 12;
269    pdsch->beamPdschInfo.numPrgs = 1;
270    pdsch->beamPdschInfo.prgSize = 1;
271    pdsch->beamPdschInfo.digBfInterfaces = 0;
272    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
273    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
274    pdsch->txPdschPower.powerControlOffset = 0;
275    pdsch->txPdschPower.powerControlOffsetSS = 0;
276         
277         return ROK;
278 }
279
280 /**********************************************************************
281          End of file
282 **********************************************************************/