Integration changes in Sync with Intel
[o-du/l2.git] / src / 5gnrsch / sch_common.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:     5G NR SCH layer
22
23 Type:     C source file
24
25 Desc:     C source code for Entry point fucntions
26
27 File:     sch_common.c
28
29  **********************************************************************/
30
31 /** @file sch_common.c
32   @brief This module performs common scheduling
33  */
34 #include "stdbool.h"
35 #include "envopt.h"        /* environment options */
36 #include "envdep.h"        /* environment dependent */
37 #include "envind.h"        /* environment independent */
38 #include "gen.h"           /* general layer */
39 #include "ssi.h"           /* system service interface */
40 #include "cm_tkns.h"       /* Common Token Defines */
41 #include "cm_llist.h"      /* Common Link List Defines */
42 #include "cm_hash.h"       /* Common Hash List Defines */
43 #include "cm_mblk.h"       /* common memory link list library */
44 #include "cm_lte.h"        /* Common LTE Defines */
45 #include "tfu.h"
46 #include "lrg.h"
47
48 #include "gen.x"           /* general layer typedefs */
49 #include "ssi.x"           /* system services typedefs */
50 #include "cm5.x"           /* system services */
51 #include "cm_tkns.x"       /* Common Token Definitions */
52 #include "cm_llist.x"      /* Common Link List Definitions */
53 #include "cm_lib.x"        /* Common Library Definitions */
54 #include "cm_hash.x"       /* Common Hash List Definitions */
55 #include "cm_mblk.x"       /* common memory link list library */
56 #include "cm_lte.x"        /* Common LTE Defines */
57 #include "tfu.x"
58 #include "lrg.x"
59 #include "du_log.h"
60 #include "du_app_mac_inf.h"
61 #include "mac_sch_interface.h"
62 #include "sch.h"
63 #include "sch_utils.h"
64 #include "common_def.h"
65
66 extern SchCb schCb[SCH_MAX_INST];
67 extern uint16_t prachCfgIdxTable[MAX_PRACH_CONFIG_IDX][8];
68 extern uint16_t numRbForPrachTable[MAX_RACH_NUM_RB_IDX][5];
69
70 SchMacUlSchInfoFunc schMacUlSchInfoOpts[] =
71 {
72         packSchMacUlSchInfo,
73         MacProcUlSchInfo,
74         packSchMacUlSchInfo
75 };
76
77 /**
78  * @brief common resource allocation for SSB
79  *
80  * @details
81  *
82  *     Function : schBroadcastAlloc
83  *     
84  *     This function handles common scheduling for DL
85  *     
86  *  @param[in]  SchCellCb *cell, cell cb
87  *  @param[in]  DlBrdcstAlloc *dlBrdcstAlloc, DL brdcst allocation
88  *  @return  void
89  **/
90 uint8_t schBroadcastAlloc(SchCellCb *cell, DlBrdcstAlloc *dlBrdcstAlloc,
91         uint16_t slot)
92 {
93         /* schedule SSB */
94         uint8_t ssbStartPrb, ssbStartSymb, idx;
95         SchDlSlotInfo *schDlSlotInfo;
96         SsbInfo ssbInfo;
97
98         schDlSlotInfo = cell->schDlSlotInfo[slot];
99         if(dlBrdcstAlloc->ssbTrans)
100         {
101                 ssbStartPrb = cell->cellCfg.ssbSchCfg.ssbOffsetPointA;
102       ssbStartSymb = cell->ssbStartSymbArr[dlBrdcstAlloc->ssbIdxSupported-1]; /*since we are
103                 supporting only 1 ssb beam */
104
105                 /* Assign interface structure */
106                 for(idx=0; idx<dlBrdcstAlloc->ssbIdxSupported; idx++)
107                 {
108                         ssbInfo.ssbIdx              = idx;
109                         ssbInfo.fdAlloc.startPrb    = ssbStartPrb;
110                         ssbInfo.fdAlloc.numPrb      = SCH_SSB_NUM_PRB;
111                         ssbInfo.tdAlloc.startSymb   = ssbStartSymb;
112                         ssbInfo.tdAlloc.numSymb     = SCH_SSB_NUM_SYMB;
113                         dlBrdcstAlloc->ssbInfo[idx] = ssbInfo;
114                         schDlSlotInfo->ssbInfo[idx] = ssbInfo;
115                 }
116
117                 schDlSlotInfo->ssbPres = true;
118                 schDlSlotInfo->ssbIdxSupported = dlBrdcstAlloc->ssbIdxSupported;
119                 for(idx=ssbStartSymb; idx<ssbStartSymb+SCH_SSB_NUM_SYMB; idx++)
120                 {
121                         schDlSlotInfo->assignedPrb[idx] = ssbStartPrb + SCH_SSB_NUM_PRB + 1; /* +1 for kSsb */
122                 }
123         }
124
125         /* SIB1 allocation */
126         if(dlBrdcstAlloc->sib1Trans)
127         {
128                 schDlSlotInfo->sib1Pres = true;
129                 for(idx=0; idx<SCH_SYMBOL_PER_SLOT; idx++)
130                 {
131                         schDlSlotInfo->assignedPrb[idx] = ssbStartPrb + SCH_SSB_NUM_PRB + 1 + 10; /* 10 PRBs for sib1 */
132                 }
133            memcpy(&dlBrdcstAlloc->sib1Alloc.bwp, &cell->cellCfg.sib1SchCfg.bwp, sizeof(BwpCfg)); 
134            memcpy(&dlBrdcstAlloc->sib1Alloc.sib1PdcchCfg, &cell->cellCfg.sib1SchCfg.sib1PdcchCfg, sizeof(PdcchCfg)); 
135            memcpy(&dlBrdcstAlloc->sib1Alloc.sib1PdschCfg, &cell->cellCfg.sib1SchCfg.sib1PdschCfg, sizeof(PdschCfg)); 
136         }
137         return ROK;
138 }
139
140 /*******************************************************************
141  *
142  * @brief Handles sending UL scheduler info to MAC 
143  *
144  * @details
145  *
146  *    Function : sendUlSchInfoToMac
147  *
148  *    Functionality:
149  *     Sends UL Sch info to MAC from SCH
150  *
151  * @params[in] 
152  * @return ROK     - success
153  *         RFAILED - failure
154  *
155  * ****************************************************************/
156 int sendUlSchInfoToMac(UlSchedInfo *ulSchedInfo, Inst inst)
157 {
158         Pst pst;
159
160    memset(&pst, 0, sizeof(Pst));
161    SCH_FILL_RSP_PST(pst, inst);
162         pst.event = EVENT_UL_SCH_INFO;
163
164         return(*schMacUlSchInfoOpts[pst.selector])(&pst, ulSchedInfo);
165 }
166 /**
167  * @brief resource allocation for PRACH
168  *
169  * @details
170  *
171  *     Function : schPrachResAlloc
172  *     
173  *     This function handles PRACH allocation
174  *     
175  *  @param[in]  SchCellCb *cell, cell cb
176  *  @param[in]  UlSchedInfo *ulSchedInfo, UL scheduling info
177  *  @return  void
178  **/
179 void schPrachResAlloc(SchCellCb *cell, UlSchedInfo *ulSchedInfo, SlotIndInfo prachOccasionTimingInfo)
180 {
181    uint8_t  puschScs;
182    uint8_t  numPrachRb = 0;
183         uint8_t  numRa = 0;
184         uint8_t  freqStart = 0;
185         uint8_t  prachCfgIdx = 0;
186         uint8_t  prachFormat = 0;
187         uint8_t  x = 0;
188         uint8_t  y = 0;
189         uint16_t prachSubframe = 0;
190         uint8_t  prachStartSymbol = 0;
191         uint8_t  prachOcas = 0;
192         uint8_t  dataType = 0;
193         uint8_t  idx = 0;
194         SchUlSlotInfo *schUlSlotInfo = NULLP;
195
196    puschScs      = cell->cellCfg.schInitialUlBwp.bwp.scs;
197         schUlSlotInfo = cell->schUlSlotInfo[prachOccasionTimingInfo.slot];
198         prachCfgIdx   = cell->cellCfg.schRachCfg.prachCfgIdx;
199
200     /* derive the prachCfgIdx table paramters */
201         x                = prachCfgIdxTable[prachCfgIdx][1];
202         y                = prachCfgIdxTable[prachCfgIdx][2];
203         prachSubframe    = prachCfgIdxTable[prachCfgIdx][3];
204         
205         if((prachOccasionTimingInfo.sfn%x) == y)
206         {
207            /* check for subFrame number */
208            if ((1 << prachOccasionTimingInfo.slot) & prachSubframe)
209            {
210          /* prach ocassion present in this subframe */
211
212                 prachFormat      = prachCfgIdxTable[prachCfgIdx][0];
213                 prachStartSymbol = prachCfgIdxTable[prachCfgIdx][4];
214                 prachOcas        = prachCfgIdxTable[prachCfgIdx][6];
215
216                 /* freq domain resource determination for RACH*/
217                 freqStart = cell->cellCfg.schRachCfg.msg1FreqStart;
218                 /* numRa determined as 𝑛 belonging {0,1,.., M − 1}, 
219                  * where M is given by msg1Fdm */
220                 numRa = (cell->cellCfg.schRachCfg.msg1Fdm - 1);
221                 for(idx=0; idx<MAX_RACH_NUM_RB_IDX; idx++)
222                 {
223                         if(numRbForPrachTable[idx][0] == cell->cellCfg.schRachCfg.rootSeqLen)
224                         {
225                                 if(numRbForPrachTable[idx][1] == cell->cellCfg.schRachCfg.prachSubcSpacing)
226                                 {
227                                         if(numRbForPrachTable[idx][2] == puschScs)
228                                         {
229                                                 break;
230                                         }
231                                 }
232                         }
233                 }
234                 numPrachRb = numRbForPrachTable[idx][3];
235                 dataType |= SCH_DATATYPE_PRACH;
236                 /* Considering first slot in the frame for PRACH */
237                 idx = 0;
238                 schUlSlotInfo->assignedPrb[idx] = freqStart+numPrachRb;
239            }
240            ulSchedInfo->dataType = dataType;
241            /* prach info */
242            ulSchedInfo->prachSchInfo.numPrachOcas   = prachOcas;
243            ulSchedInfo->prachSchInfo.prachFormat    = prachFormat;
244            ulSchedInfo->prachSchInfo.numRa          = numRa;
245            ulSchedInfo->prachSchInfo.prachStartSymb = prachStartSymbol;
246         }
247 }
248
249 /**
250  * @brief resource allocation for UL
251  *
252  * @details
253  *
254  *     Function : schUlResAlloc
255  *     
256  *     This function handles UL Resource allocation
257  *     
258  *  @param[in]  SchCellCb *cell, cellCb
259  *  @return  void
260  **/
261 uint8_t schUlResAlloc(SchCellCb *cell, Inst schInst)
262 {
263    int ret = ROK;
264         UlSchedInfo ulSchedInfo;
265    SchUlSlotInfo  *schUlSlotInfo;
266    SlotIndInfo ulTimingInfo;
267
268    /* add PHY delta */
269    ADD_DELTA_TO_TIME(cell->slotInfo,ulTimingInfo,PHY_DELTA+SCHED_DELTA);
270
271         ulSchedInfo.cellId = cell->cellId;
272         ulSchedInfo.slotIndInfo.sfn = ulTimingInfo.sfn;
273    ulSchedInfo.slotIndInfo.slot = ulTimingInfo.slot;
274
275    /* Schedule resources for PRACH */
276         schPrachResAlloc(cell, &ulSchedInfo, ulTimingInfo);
277
278         schUlSlotInfo = cell->schUlSlotInfo[ulTimingInfo.slot]; 
279         
280         if(schUlSlotInfo->schPuschInfo)
281         {
282       ulSchedInfo.crnti = cell->raCb[0].tcrnti;
283                 ulSchedInfo.dataType |= SCH_DATATYPE_PUSCH;
284                 memcpy(&ulSchedInfo.schPuschInfo, schUlSlotInfo->schPuschInfo,
285                                 sizeof(SchPuschInfo));
286                 SCH_FREE(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
287                 schUlSlotInfo->schPuschInfo = NULL;
288         }
289
290         //send msg to MAC
291    ret = sendUlSchInfoToMac(&ulSchedInfo, schInst);
292    if(ret != ROK)
293    {
294       DU_LOG("\nSending UL Sch info from SCH to MAC failed");
295    }
296     
297         memset(cell->schUlSlotInfo[ulTimingInfo.slot], 0, sizeof(SchUlSlotInfo));
298
299         return ret;
300 }
301
302 /*******************************************************************
303  *
304  * @brief Fills pdcch and pdsch info for msg4
305  *
306  * @details
307  *
308  *    Function : schDlRsrcAllocMsg4
309  *
310  *    Functionality:
311  *       Fills pdcch and pdsch info for msg4
312  *
313  * @params[in] 
314  * @return ROK     - success
315  *         RFAILED - failure
316  *
317  * ****************************************************************/
318 uint8_t schDlRsrcAllocMsg4(Msg4Alloc *msg4Alloc, SchCellCb *cell, uint16_t slot)
319 {
320    uint8_t coreset0Idx = 0;
321    uint8_t numRbs = 0;
322    uint8_t firstSymbol = 0;
323    uint8_t numSymbols = 0;
324    uint8_t offset = 0;
325    uint8_t offsetPointA;
326    uint8_t FreqDomainResource[6] = {0};
327    uint16_t tbSize = 0;
328         uint8_t numPdschSymbols = 12; /* considering pdsch region from 2 to 13 */
329    uint8_t mcs = 4;  /* MCS fixed to 4 */
330    SchBwpDlCfg *initialBwp;
331
332    PdcchCfg *pdcch = &msg4Alloc->msg4PdcchCfg;
333    PdschCfg *pdsch = &msg4Alloc->msg4PdschCfg;
334    BwpCfg *bwp = &msg4Alloc->bwp;
335
336    initialBwp   = &cell->cellCfg.schInitialDlBwp;
337    offsetPointA = cell->cellCfg.ssbSchCfg.ssbOffsetPointA;
338    coreset0Idx  = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
339
340    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
341    numRbs        = coresetIdxTable[coreset0Idx][1];
342    numSymbols    = coresetIdxTable[coreset0Idx][2];
343    offset        = coresetIdxTable[coreset0Idx][3];
344
345    /* calculate time domain parameters */
346    uint16_t mask = 0x2000;
347    for(firstSymbol=0; firstSymbol<14;firstSymbol++)
348    {
349       if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
350          break;
351       else
352          mask = mask>>1;
353    }
354
355    /* calculate the PRBs */
356    schAllocFreqDomRscType0(((offsetPointA-offset)/6), (numRbs/6), FreqDomainResource);
357
358    /* fill BWP */
359    bwp->freqAlloc.numPrb   = initialBwp->bwp.freqAlloc.numPrb;
360    bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
361    bwp->subcarrierSpacing  = initialBwp->bwp.scs;
362    bwp->cyclicPrefix       = initialBwp->bwp.cyclicPrefix;
363
364    /* fill the PDCCH PDU */
365    pdcch->coreset0Cfg.startSymbolIndex = firstSymbol;
366    pdcch->coreset0Cfg.durationSymbols = numSymbols;
367    memcpy(pdcch->coreset0Cfg.freqDomainResource,FreqDomainResource,6);
368    pdcch->coreset0Cfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
369    pdcch->coreset0Cfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
370    pdcch->coreset0Cfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
371    pdcch->coreset0Cfg.coreSetType = 0;
372    pdcch->coreset0Cfg.coreSet0Size = numRbs;
373    pdcch->coreset0Cfg.shiftIndex = cell->cellCfg.phyCellId;
374    pdcch->coreset0Cfg.precoderGranularity = 0; /* sameAsRegBundle */
375    pdcch->numDlDci = 1;
376    pdcch->dci.rnti = cell->schDlSlotInfo[slot]->msg4Info->crnti;
377    pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
378    pdcch->dci.scramblingRnti = 0;
379    pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
380    pdcch->dci.aggregLevel = 4;
381    pdcch->dci.beamPdcchInfo.numPrgs = 1;
382    pdcch->dci.beamPdcchInfo.prgSize = 1;
383    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
384    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
385    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
386    pdcch->dci.txPdcchPower.powerValue = 0;
387    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
388
389    /* fill the PDSCH PDU */
390    uint8_t cwCount = 0;
391    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
392    pdsch->rnti = cell->schDlSlotInfo[slot]->msg4Info->crnti;
393    pdsch->pduIndex = 0;
394    pdsch->numCodewords = 1;
395    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
396    {
397       pdsch->codeword[cwCount].targetCodeRate = 308;
398       pdsch->codeword[cwCount].qamModOrder = 2;
399       pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
400       pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
401       pdsch->codeword[cwCount].rvIndex = 0;
402       /* 38.214: Table 5.1.3.2-1,  divided by 8 to get the value in bytes */
403       /* TODO : Calculate tbSize based of DL CCCH msg size */
404                 tbSize = schCalcTbSize(2664/8); /* send this value to the func in bytes when considering msg4 size */
405       pdsch->codeword[cwCount].tbSize = tbSize;
406    }
407    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
408    pdsch->numLayers = 1;
409    pdsch->transmissionScheme = 0;
410    pdsch->refPoint = 0;
411    pdsch->dmrs.dlDmrsSymbPos = 2;
412    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
413    pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
414    pdsch->dmrs.scid = 0;
415    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
416    pdsch->dmrs.dmrsPorts = 0;
417    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
418    /* the RB numbering starts from coreset0, and PDSCH is always above SSB */
419    pdsch->pdschFreqAlloc.freqAlloc.startPrb = offset + SCH_SSB_NUM_PRB;
420    pdsch->pdschFreqAlloc.freqAlloc.numPrb = schCalcNumPrb(tbSize,mcs,numPdschSymbols);
421    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
422    pdsch->pdschTimeAlloc.timeAlloc.startSymb = 2; /* spec-38.214, Table 5.1.2.1-1 */
423    pdsch->pdschTimeAlloc.timeAlloc.numSymb = 12;
424    pdsch->beamPdschInfo.numPrgs = 1;
425    pdsch->beamPdschInfo.prgSize = 1;
426    pdsch->beamPdschInfo.digBfInterfaces = 0;
427    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
428    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
429    pdsch->txPdschPower.powerControlOffset = 0;
430    pdsch->txPdschPower.powerControlOffsetSS = 0;
431  
432    pdcch->dci.pdschCfg = pdsch;
433    return ROK;
434 }
435
436 /**********************************************************************
437   End of file
438  **********************************************************************/
439