fea12b3f6e467bbcd08a4366ce740ebbb48b8c32
[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 "common_def.h"
35 #include "tfu.h"
36 #include "lrg.h"
37
38 #include "tfu.x"
39 #include "lrg.x"
40 #include "du_log.h"
41 #include "du_app_mac_inf.h"
42 #include "mac_sch_interface.h"
43 #include "sch.h"
44 #include "sch_utils.h"
45
46 SchCb schCb[SCH_MAX_INST];
47 uint16_t prachCfgIdxTable[MAX_PRACH_CONFIG_IDX][8];
48 uint16_t numRbForPrachTable[MAX_RACH_NUM_RB_IDX][5];
49 uint8_t pucchResourceSet[MAX_PUCCH_RES_SET_IDX][4];
50
51 SchMacUlSchInfoFunc schMacUlSchInfoOpts[] =
52 {
53    packSchMacUlSchInfo,
54    MacProcUlSchInfo,
55    packSchMacUlSchInfo
56 };
57
58 /**
59  * @brief common resource allocation for SSB
60  *
61  * @details
62  *
63  *     Function : schBroadcastAlloc
64  *     
65  *     This function handles common scheduling for DL
66  *     
67  *  @param[in]  SchCellCb *cell, cell cb
68  *  @param[in]  DlBrdcstAlloc *dlBrdcstAlloc, DL brdcst allocation
69  *  @return  void
70  **/
71 uint8_t schBroadcastAlloc(SchCellCb *cell, DlBrdcstAlloc *dlBrdcstAlloc,
72       uint16_t slot)
73 {
74    /* schedule SSB */
75    uint8_t ssbStartPrb, ssbStartSymb, idx;
76    SchDlSlotInfo *schDlSlotInfo;
77    SsbInfo ssbInfo;
78
79    schDlSlotInfo = cell->schDlSlotInfo[slot];
80
81    if(dlBrdcstAlloc->ssbTrans)
82    {
83       ssbStartPrb = cell->cellCfg.ssbSchCfg.ssbOffsetPointA; //+Kssb
84       ssbStartSymb = cell->ssbStartSymbArr[dlBrdcstAlloc->ssbIdxSupported-1]; /*since we are
85                                                                                 supporting only 1 ssb beam */
86
87       /* Assign interface structure */
88       for(idx=0; idx<dlBrdcstAlloc->ssbIdxSupported; idx++)
89       {
90          ssbInfo.ssbIdx              = idx;
91          ssbInfo.fdAlloc.startPrb    = ssbStartPrb;
92          ssbInfo.fdAlloc.numPrb      = SCH_SSB_NUM_PRB;
93          ssbInfo.tdAlloc.startSymb   = ssbStartSymb;
94          ssbInfo.tdAlloc.numSymb     = SCH_SSB_NUM_SYMB;
95          dlBrdcstAlloc->ssbInfo[idx] = ssbInfo;
96          schDlSlotInfo->ssbInfo[idx] = ssbInfo;
97       }
98
99       schDlSlotInfo->ssbPres = true;
100       schDlSlotInfo->ssbIdxSupported = dlBrdcstAlloc->ssbIdxSupported;
101       for(idx=ssbStartSymb; idx<ssbStartSymb+SCH_SSB_NUM_SYMB; idx++)
102       {
103          schDlSlotInfo->assignedPrb[idx] = ssbStartPrb + SCH_SSB_NUM_PRB + 1; /* +1 for kSsb */
104       }
105    }
106
107    /* SIB1 allocation */
108    if(dlBrdcstAlloc->sib1Trans)
109    {
110       uint16_t tbSize         = 0;
111       uint8_t numPdschSymbols = 12; /* considering pdsch region from 2 to 13 */
112       uint8_t mcs             = 4;  /* MCS fixed to 4 */
113       uint8_t numSib1Prb      = 0;
114       schDlSlotInfo->sib1Pres = true;
115
116       tbSize = schCalcTbSize(cell->cellCfg.sib1SchCfg.sib1PduLen); /* send this value to the func in bytes when considering sib1 size */
117       numSib1Prb = schCalcNumPrb(tbSize,mcs,numPdschSymbols);
118       for(idx=0; idx<SCH_SYMBOL_PER_SLOT; idx++)
119       {
120          schDlSlotInfo->assignedPrb[idx] = ssbStartPrb + SCH_SSB_NUM_PRB + 1 + numSib1Prb; /* 10 PRBs for sib1 */
121       }
122       memcpy(&dlBrdcstAlloc->sib1Alloc.bwp, &cell->cellCfg.sib1SchCfg.bwp, sizeof(BwpCfg)); 
123       memcpy(&dlBrdcstAlloc->sib1Alloc.sib1PdcchCfg, &cell->cellCfg.sib1SchCfg.sib1PdcchCfg, sizeof(PdcchCfg)); 
124       memcpy(&dlBrdcstAlloc->sib1Alloc.sib1PdschCfg, &cell->cellCfg.sib1SchCfg.sib1PdschCfg, sizeof(PdschCfg)); 
125       dlBrdcstAlloc->sib1Alloc.sib1PdcchCfg.dci.pdschCfg = &dlBrdcstAlloc->sib1Alloc.sib1PdschCfg;
126    }
127    return ROK;
128 }
129
130 /*******************************************************************
131  *
132  * @brief Handles sending UL scheduler info to MAC 
133  *
134  * @details
135  *
136  *    Function : sendUlSchInfoToMac
137  *
138  *    Functionality:
139  *     Sends UL Sch info to MAC from SCH
140  *
141  * @params[in] 
142  * @return ROK     - success
143  *         RFAILED - failure
144  *
145  * ****************************************************************/
146 int sendUlSchInfoToMac(UlSchedInfo *ulSchedInfo, Inst inst)
147 {
148    Pst pst;
149
150    memset(&pst, 0, sizeof(Pst));
151    FILL_PST_SCH_TO_MAC(pst, inst);
152    pst.event = EVENT_UL_SCH_INFO;
153
154    return(*schMacUlSchInfoOpts[pst.selector])(&pst, ulSchedInfo);
155 }
156 /**
157  * @brief resource allocation for PRACH
158  *
159  * @details
160  *
161  *     Function : schPrachResAlloc
162  *     
163  *     This function handles PRACH allocation
164  *     
165  *  @param[in]  SchCellCb *cell, cell cb
166  *  @param[in]  UlSchedInfo *ulSchedInfo, UL scheduling info
167  *  @return  void
168  **/
169 void schPrachResAlloc(SchCellCb *cell, UlSchedInfo *ulSchedInfo, SlotTimingInfo prachOccasionTimingInfo)
170 {
171    uint8_t  puschScs;
172    uint8_t  numPrachRb = 0;
173    uint8_t  numRa = 0;
174    uint8_t  freqStart = 0;
175    uint8_t  prachCfgIdx = 0;
176    uint8_t  prachFormat = 0;
177    uint8_t  x = 0;
178    uint8_t  y = 0;
179    uint16_t prachSubframe = 0;
180    uint8_t  prachStartSymbol = 0;
181    uint8_t  prachOcas = 0;
182    uint8_t  dataType = 0;
183    uint8_t  idx = 0;
184    uint8_t  subFrame = 0;
185    SchUlSlotInfo *schUlSlotInfo = NULLP;
186
187    puschScs      = cell->cellCfg.schInitialUlBwp.bwp.scs;
188    schUlSlotInfo = cell->schUlSlotInfo[prachOccasionTimingInfo.slot];
189    prachCfgIdx   = cell->cellCfg.schRachCfg.prachCfgIdx;
190
191    /* derive the prachCfgIdx table paramters */
192    x                = prachCfgIdxTable[prachCfgIdx][1];
193    y                = prachCfgIdxTable[prachCfgIdx][2];
194    prachSubframe    = prachCfgIdxTable[prachCfgIdx][3];
195
196    if((prachOccasionTimingInfo.sfn%x) == y)
197    {
198 #ifdef NR_TDD
199       subFrame = prachOccasionTimingInfo.slot/2;
200 #else
201       subFrame = prachOccasionTimingInfo.slot;
202 #endif
203       /* check for subFrame number */
204       if ((1 << subFrame) & prachSubframe)
205       {
206          /* prach ocassion present in this subframe */
207 #ifdef NR_TDD
208          if(UL_SLOT != schGetSlotSymbFrmt(prachOccasionTimingInfo.slot%cell->numSlotsInPeriodicity,\
209          cell->slotFrmtBitMap))
210          {
211             DU_LOG("\nERROR  --> SCH : PrachCfgIdx %d doesn't support UL slot", prachCfgIdx);
212          }
213          else
214 #endif
215          {
216             prachFormat      = prachCfgIdxTable[prachCfgIdx][0];
217             prachStartSymbol = prachCfgIdxTable[prachCfgIdx][4];
218             prachOcas        = prachCfgIdxTable[prachCfgIdx][6];
219
220             /* freq domain resource determination for RACH*/
221             freqStart = cell->cellCfg.schRachCfg.msg1FreqStart;
222             /* numRa determined as 𝑛 belonging {0,1,.., M − 1}, 
223              * where M is given by msg1Fdm */
224             numRa = (cell->cellCfg.schRachCfg.msg1Fdm - 1);
225             for(idx=0; idx<MAX_RACH_NUM_RB_IDX; idx++)
226             {
227                if(numRbForPrachTable[idx][0] == cell->cellCfg.schRachCfg.rootSeqLen)
228                {
229                   if(numRbForPrachTable[idx][1] == cell->cellCfg.schRachCfg.prachSubcSpacing)
230                   {
231                      if(numRbForPrachTable[idx][2] == puschScs)
232                      {
233                         break;
234                      }
235                   }
236                }
237             }
238             numPrachRb = numRbForPrachTable[idx][3];
239             dataType |= SCH_DATATYPE_PRACH;
240             /* Considering first slot in the frame for PRACH */
241             idx = 0;
242             schUlSlotInfo->assignedPrb[idx] = freqStart+numPrachRb;
243          }
244          ulSchedInfo->dataType = dataType;
245          /* prach info */
246          ulSchedInfo->prachSchInfo.numPrachOcas   = prachOcas;
247          ulSchedInfo->prachSchInfo.prachFormat    = prachFormat;
248          ulSchedInfo->prachSchInfo.numRa          = numRa;
249          ulSchedInfo->prachSchInfo.prachStartSymb = prachStartSymbol;
250          DU_LOG("\nINFO  --> SCH : RACH occassion set for slot %d", prachOccasionTimingInfo.slot);
251       }
252    }
253 }
254
255
256 /**
257  * @brief Function to fill Pucch Format 0
258  *
259  * @details
260  *
261  *     Function : fillPucchFormat0
262  *     
263  *     Function to fill Pucch format 0
264  *     
265  *  @param[in]  SchPucchInfo pointer, SchPucchResrcInfo pointer
266  *  @return  void
267  **/
268
269 void fillPucchFormat0(SchPucchInfo *ulSchedPucch, SchPucchResrcInfo *resrcInfo)
270 {
271    if(resrcInfo->SchPucchFormat.format0)
272    {
273       ulSchedPucch->fdAlloc.numPrb = PUCCH_NUM_PRB_FORMAT_0_1_4;
274       ulSchedPucch->pucchFormat  = PUCCH_FORMAT_0;
275       ulSchedPucch->initialCyclicShift =  resrcInfo->SchPucchFormat.format0->initialCyclicShift;
276       ulSchedPucch->tdAlloc.numSymb = resrcInfo->SchPucchFormat.format0->numSymbols;
277       ulSchedPucch->tdAlloc.startSymb = resrcInfo->SchPucchFormat.format0->startSymbolIdx;
278    }
279 }
280
281 /**
282  * @brief Function to fill Pucch Format 1
283  *
284  * @details
285  *
286  *     Function : fillPucchFormat1
287  *     
288  *     Function to fill Pucch format 1
289  *     
290  *  @param[in]  SchPucchInfo pointer, SchPucchResrcInfo pointer
291  *  @return  void
292  **/
293
294 void fillPucchFormat1(SchPucchInfo *ulSchedPucch, SchPucchResrcInfo *resrcInfo)
295 {
296    if(resrcInfo->SchPucchFormat.format1)
297    {
298       ulSchedPucch->fdAlloc.numPrb = PUCCH_NUM_PRB_FORMAT_0_1_4;
299       ulSchedPucch->pucchFormat  = PUCCH_FORMAT_1;
300       ulSchedPucch->initialCyclicShift =  resrcInfo->SchPucchFormat.format1->initialCyclicShift;
301       ulSchedPucch->tdAlloc.numSymb = resrcInfo->SchPucchFormat.format1->numSymbols;
302       ulSchedPucch->tdAlloc.startSymb = resrcInfo->SchPucchFormat.format1->startSymbolIdx;
303       ulSchedPucch->timeDomOCC =  resrcInfo->SchPucchFormat.format1->timeDomOCC;
304   }
305 }
306
307 /**
308  * @brief Function to fill Pucch format for UL Sched Info
309  *
310  * @details
311  *
312  *     Function : fillUlSchedPucchFormat
313  *     
314  *     Function to fill Pucch format for UL Sched Info
315  *     
316  *  @param[in]  pucchFormat , SchPucchInfo pointer,
317  *  @param[in]  SchPucchFormatCfg pointer, SchPucchResrcInfo pointer
318  *  @return  void
319  **/
320
321 uint8_t fillUlSchedPucchFormat(uint8_t pucchFormat, SchPucchInfo *ulSchedPucch,\
322    SchPucchResrcInfo *resrcInfo, SchPucchFormatCfg *formatCfg)
323 {
324    uint8_t ret = ROK;
325
326    switch(pucchFormat)
327    {
328       case PUCCH_FORMAT_0:
329          {
330             if(resrcInfo)
331                fillPucchFormat0(ulSchedPucch, resrcInfo);
332             return ret;
333          }
334       case PUCCH_FORMAT_1:
335          {
336             if(resrcInfo)
337             {
338                fillPucchFormat1(ulSchedPucch, resrcInfo);
339             }
340             if(formatCfg)
341             {
342                memcpy(&ulSchedPucch->cmnFormatCfg, formatCfg, sizeof(SchPucchFormatCfg));
343             }
344             return ret;
345          }/* To Add support for more Pucch Format */
346       
347       default:
348          DU_LOG("\nERROR  --> SCH : Invalid PUCCH format[%d] in fillUlSchedPucchFormatCfg()", pucchFormat);
349          ret = RFAILED;
350          return ret;
351    }
352    return ret;
353 }
354
355 /**
356  * @brief Function to fill Pucch Dedicated Cfg for UL Sched Info
357  *
358  * @details
359  *
360  *     Function : fillUlSchedPucchDedicatedCfg
361  *     
362  *     Function to fill Pucch Dedicated Cfg for UL Sched Info
363  *     
364  *  @param[in]  pucchFormat to be filled
365  *  @param[in]  SchPucchFormatCfg pointer, SchPucchCfg pointer
366  *  @return  void
367  **/
368
369 uint8_t fillUlSchedPucchDedicatedCfg(uint16_t numSlots, SchPucchCfg *pucchDedCfg,\
370    SlotTimingInfo *slotInfo, SchPucchInfo *ulSchedPucch)
371 {
372    uint8_t ret, resrcSetIdx, resrcIdx, schedReqIdx, srPeriodicity = 0;
373    uint16_t srOffset = 0;
374
375    ret = ROK;
376    if(pucchDedCfg->resrcSet && pucchDedCfg->resrc)
377    {
378       //Assuming one entry in the list
379       for(resrcSetIdx = 0; resrcSetIdx < pucchDedCfg->resrcSet->resrcSetToAddModListCount; resrcSetIdx++)
380       {
381          for(resrcIdx = 0; resrcIdx < pucchDedCfg->resrc->resrcToAddModListCount; resrcIdx++)
382          {
383             if(pucchDedCfg->resrcSet->resrcSetToAddModList[resrcSetIdx].resrcList[resrcSetIdx] ==\
384                pucchDedCfg->resrc->resrcToAddModList[resrcIdx].resrcId)
385             {
386                ulSchedPucch->intraFreqHop = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].intraFreqHop;
387                ulSchedPucch->secondPrbHop = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].secondPrbHop;
388                ulSchedPucch->fdAlloc.startPrb = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].startPrb;
389                ulSchedPucch->pucchFormat = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].pucchFormat;
390                ret = fillUlSchedPucchFormat(ulSchedPucch->pucchFormat, ulSchedPucch,\
391                         &pucchDedCfg->resrc->resrcToAddModList[resrcIdx], NULLP);
392                if(ret == RFAILED)
393                   return ret;
394             }
395          }
396       }
397    }
398    if(pucchDedCfg->format1)
399    {
400       memset(&ulSchedPucch->cmnFormatCfg, 0, sizeof(SchPucchFormatCfg));
401       ret = fillUlSchedPucchFormat(ulSchedPucch->pucchFormat, ulSchedPucch, NULLP, pucchDedCfg->format1);
402       if(ret == RFAILED)
403          return ret;
404    }
405    
406    /* setting SR and UCI flag */
407    if(pucchDedCfg->schedReq)
408    {
409       for(schedReqIdx = 0; schedReqIdx < pucchDedCfg->schedReq->schedAddModListCount; schedReqIdx++)
410       {
411          srPeriodicity = pucchDedCfg->schedReq->schedAddModList[schedReqIdx].periodicity;
412          srOffset      = pucchDedCfg->schedReq->schedAddModList[schedReqIdx].offset;
413          break;
414       }
415       if(((numSlots * slotInfo->sfn + slotInfo->slot - srOffset) % srPeriodicity) == 0)
416       {
417          ulSchedPucch->srFlag  = true;
418          ulSchedPucch->uciFlag = true;
419       }
420    }
421    return ret;
422 }
423
424 /**
425  * @brief Function to fill Pucch Resource Info
426  *
427  * @details
428  *
429  *     Function : fillPucchResourceInfo
430  *     
431  *     Function to fill Pucch Resource Info
432  *     
433  *  @param[in]  SchPucchInfo *schPucchInfo, Inst inst
434  *  @return  ROK/RFAILED
435  **/
436
437 uint16_t fillPucchResourceInfo(SchPucchInfo *schPucchInfo, Inst inst)
438 {
439    uint8_t ret = ROK, ueIdx = 0, pucchIdx = 0;
440    SchCellCb  *cell = schCb[inst].cells[inst];
441    SchPucchCfgCmn *pucchCfg = NULLP;
442    SchBwpParams *ulBwp = NULLP;
443
444    GET_UE_IDX(schPucchInfo->rnti, ueIdx);
445    if(cell->ueCb[ueIdx].ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfgPres)
446    {
447       /* fill pucch dedicated cfg */
448       ret = fillUlSchedPucchDedicatedCfg(cell->numSlots,\
449        &cell->ueCb[ueIdx].ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfg, &cell->slotInfo, schPucchInfo);
450       if(ret == RFAILED)
451       {
452          memset(schPucchInfo, 0, sizeof(SchPucchInfo));
453          DU_LOG("\nERROR  --> SCH : Filling PUCCH dedicated cfg failed at fillPucchResourceInfo()");
454          return ret;
455       }
456    }
457    else
458    {
459       /* fill pucch common cfg */
460       /* derive pucchResourceSet from schCellCfg */
461       pucchCfg = &cell->cellCfg.schInitialUlBwp.pucchCommon;
462       pucchIdx = pucchCfg->pucchResourceCommon;
463       ulBwp = &cell->cellCfg.schInitialUlBwp.bwp;
464       schPucchInfo->fdAlloc.startPrb = ulBwp->freqAlloc.startPrb + pucchResourceSet[pucchIdx][3];
465       schPucchInfo->fdAlloc.numPrb = PUCCH_NUM_PRB_FORMAT_0_1_4;
466       schPucchInfo->tdAlloc.startSymb = pucchResourceSet[pucchIdx][1];
467       schPucchInfo->tdAlloc.numSymb = pucchResourceSet[pucchIdx][2];
468       schPucchInfo->pucchFormat = pucchResourceSet[pucchIdx][0];
469
470       /* set SR and UCI flag to false */
471       schPucchInfo->srFlag  = true;
472       schPucchInfo->uciFlag = true;
473    }
474    /* set HARQ flag to true */
475    schPucchInfo->harqFlag = true;
476    schPucchInfo->numHarqBits = 1; /* 1 bit for HARQ */
477
478    return ROK;
479 }
480
481 /**
482  * @brief resource allocation for UL
483  *
484  * @details
485  *
486  *     Function : schUlResAlloc
487  *     
488  *     This function handles UL Resource allocation
489  *     
490  *  @param[in]  SchCellCb *cell, cellCb
491  *  @return  void
492  **/
493 uint8_t schUlResAlloc(SchCellCb *cell, Inst schInst)
494 {
495    int ret = ROK;
496    UlSchedInfo ulSchedInfo;
497    SchUlSlotInfo  *schUlSlotInfo = NULLP;
498    SlotTimingInfo ulTimingInfo;
499    memset(&ulSchedInfo, 0, sizeof(UlSchedInfo));
500
501    /* add PHY delta */
502    ADD_DELTA_TO_TIME(cell->slotInfo,ulTimingInfo,PHY_DELTA_UL+SCHED_DELTA);
503
504    ulSchedInfo.cellId = cell->cellId;
505    ulSchedInfo.slotIndInfo.cellId = ulSchedInfo.cellId;
506    ulSchedInfo.slotIndInfo.sfn = ulTimingInfo.sfn;
507    ulSchedInfo.slotIndInfo.slot = ulTimingInfo.slot;
508
509    /* Schedule resources for PRACH */
510    if(cell->firstSib1Transmitted)
511     schPrachResAlloc(cell, &ulSchedInfo, ulTimingInfo);
512
513    schUlSlotInfo = cell->schUlSlotInfo[ulTimingInfo.slot]; 
514    if(schUlSlotInfo->schPuschInfo)
515    {
516       ulSchedInfo.crnti = schUlSlotInfo->schPuschInfo->crnti;
517       ulSchedInfo.dataType |= SCH_DATATYPE_PUSCH;
518       memcpy(&ulSchedInfo.schPuschInfo, schUlSlotInfo->schPuschInfo,
519             sizeof(SchPuschInfo));
520       SCH_FREE(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
521       schUlSlotInfo->schPuschInfo = NULL;
522    }
523
524    if(schUlSlotInfo->pucchPres)
525    {
526       ulSchedInfo.dataType |= SCH_DATATYPE_UCI;
527       fillPucchResourceInfo(&schUlSlotInfo->schPucchInfo, schInst);
528       memcpy(&ulSchedInfo.schPucchInfo, &schUlSlotInfo->schPucchInfo,
529             sizeof(SchPucchInfo));
530       memset(&schUlSlotInfo->schPucchInfo, 0, sizeof(SchPucchInfo));
531    }
532
533    //send msg to MAC
534    ret = sendUlSchInfoToMac(&ulSchedInfo, schInst);
535    if(ret != ROK)
536    {
537       DU_LOG("\nERROR  -->  SCH : Sending UL Sch info from SCH to MAC failed");
538    }
539
540    schInitUlSlot(schUlSlotInfo);
541    return ret;
542 }
543
544 /*******************************************************************
545  *
546  * @brief Fills pdcch and pdsch info for msg4
547  *
548  * @details
549  *
550  *    Function : schDlRsrcAllocMsg4
551  *
552  *    Functionality:
553  *       Fills pdcch and pdsch info for msg4
554  *
555  * @params[in] 
556  * @return ROK     - success
557  *         RFAILED - failure
558  *
559  * ****************************************************************/
560 uint8_t schDlRsrcAllocMsg4(DlMsgAlloc *msg4Alloc, SchCellCb *cell, uint16_t slot, bool ssbPresent, bool sib1Present)
561 {
562    uint8_t coreset0Idx = 0;
563    uint8_t numRbs = 0;
564    uint8_t firstSymbol = 0;
565    uint8_t numSymbols = 0;
566    uint8_t offset = 0;
567    uint8_t offsetPointA;
568    uint8_t FreqDomainResource[6] = {0};
569    uint16_t tbSize = 0;
570    uint8_t numPdschSymbols = 11;            /* considering pdsch region from 3 to 13 */
571    uint8_t mcs = 4;                         /* MCS fixed to 4 */
572    SchBwpDlCfg *initialBwp;
573    FreqDomainAlloc *sib1PdschFreqAlloc = NULL;
574
575    PdcchCfg *pdcch = &msg4Alloc->dlMsgPdcchCfg;
576    PdschCfg *pdsch = &msg4Alloc->dlMsgPdschCfg;
577    BwpCfg *bwp = &msg4Alloc->bwp;
578
579    initialBwp   = &cell->cellCfg.schInitialDlBwp;
580    offsetPointA = cell->cellCfg.ssbSchCfg.ssbOffsetPointA;
581    coreset0Idx  = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
582
583    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
584    numRbs        = coresetIdxTable[coreset0Idx][1];
585    numSymbols    = coresetIdxTable[coreset0Idx][2];
586    offset        = coresetIdxTable[coreset0Idx][3];
587
588    /* calculate time domain parameters */
589    uint16_t mask = 0x2000;
590    for(firstSymbol=0; firstSymbol<14;firstSymbol++)
591    {
592       if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
593          break;
594       else
595          mask = mask>>1;
596    }
597
598    /* calculate the PRBs */
599    freqDomRscAllocType0(((offsetPointA-offset)/6), (numRbs/6), FreqDomainResource);
600
601    /* fill BWP */
602    bwp->freqAlloc.numPrb   = initialBwp->bwp.freqAlloc.numPrb;
603    bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
604    bwp->subcarrierSpacing  = initialBwp->bwp.scs;
605    bwp->cyclicPrefix       = initialBwp->bwp.cyclicPrefix;
606
607    /* fill the PDCCH PDU */
608    pdcch->coresetCfg.startSymbolIndex = firstSymbol;
609    pdcch->coresetCfg.durationSymbols = numSymbols;
610    memcpy(pdcch->coresetCfg.freqDomainResource,FreqDomainResource,6);
611    pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
612    pdcch->coresetCfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
613    pdcch->coresetCfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
614    pdcch->coresetCfg.coreSetType = 0;
615    pdcch->coresetCfg.coreSetSize = numRbs;
616    pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
617    pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
618    pdcch->numDlDci = 1;
619    pdcch->dci.rnti = cell->schDlSlotInfo[slot]->dlMsgInfo->crnti;
620    pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
621    pdcch->dci.scramblingRnti = 0;
622    pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
623    pdcch->dci.aggregLevel = 4;
624    pdcch->dci.beamPdcchInfo.numPrgs = 1;
625    pdcch->dci.beamPdcchInfo.prgSize = 1;
626    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
627    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
628    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
629    pdcch->dci.txPdcchPower.powerValue = 0;
630    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
631
632    /* fill the PDSCH PDU */
633    uint8_t cwCount = 0;
634    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
635    pdsch->rnti = cell->schDlSlotInfo[slot]->dlMsgInfo->crnti;
636    pdsch->pduIndex = 0;
637    pdsch->numCodewords = 1;
638    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
639    {
640       pdsch->codeword[cwCount].targetCodeRate = 308;
641       pdsch->codeword[cwCount].qamModOrder = 2;
642       pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
643       pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
644       pdsch->codeword[cwCount].rvIndex = 0;
645       tbSize = schCalcTbSize(msg4Alloc->dlMsgInfo.dlMsgPduLen + TX_PAYLOAD_HDR_LEN); /* MSG4 size + FAPI header size*/
646       pdsch->codeword[cwCount].tbSize = tbSize;
647    }
648    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
649    pdsch->numLayers = 1;
650    pdsch->transmissionScheme = 0;
651    pdsch->refPoint = 0;
652    pdsch->dmrs.dlDmrsSymbPos = 4; /* Bitmap value 00000000000100 i.e. using 3rd symbol for PDSCH DMRS */
653    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
654    pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
655    pdsch->dmrs.scid = 0;
656    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
657    pdsch->dmrs.dmrsPorts = 0;
658    pdsch->dmrs.mappingType      = DMRS_MAP_TYPE_A; /* Setting to Type-A */
659    pdsch->dmrs.nrOfDmrsSymbols  = NUM_DMRS_SYMBOLS;
660    pdsch->dmrs.dmrsAddPos       = DMRS_ADDITIONAL_POS;
661    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
662    /* The RB numbering starts from coreset0 */ 
663    pdsch->pdschFreqAlloc.freqAlloc.startPrb = PDSCH_START_RB;
664    if(ssbPresent)
665    {
666       /* PDSCH is always above SSB */
667       pdsch->pdschFreqAlloc.freqAlloc.startPrb = offsetPointA + SCH_SSB_NUM_PRB + 1;
668    }
669    if(sib1Present)
670    {
671       /* Must not overlap with SIB1 */
672       sib1PdschFreqAlloc = &cell->cellCfg.sib1SchCfg.sib1PdschCfg.pdschFreqAlloc.freqAlloc;
673       pdsch->pdschFreqAlloc.freqAlloc.startPrb = sib1PdschFreqAlloc->startPrb + sib1PdschFreqAlloc->numPrb + 1; 
674    }
675    pdsch->pdschFreqAlloc.freqAlloc.numPrb = schCalcNumPrb(tbSize, mcs, numPdschSymbols);
676    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
677    pdsch->pdschTimeAlloc.timeAlloc.startSymb = 3; /* spec-38.214, Table 5.1.2.1-1 */
678    pdsch->pdschTimeAlloc.timeAlloc.numSymb = numPdschSymbols;
679    pdsch->beamPdschInfo.numPrgs = 1;
680    pdsch->beamPdschInfo.prgSize = 1;
681    pdsch->beamPdschInfo.digBfInterfaces = 0;
682    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
683    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
684    pdsch->txPdschPower.powerControlOffset = 0;
685    pdsch->txPdschPower.powerControlOffsetSS = 0;
686
687    pdcch->dci.pdschCfg = pdsch;
688
689    return ROK;
690 }
691  
692 uint16_t schAllocPucchResource(SchCellCb *cell, uint16_t crnti, uint16_t slot)
693 {
694    uint8_t k1 = SCH_DEFAULT_K1, ueIdx = 0, dlToUlAckIdx;
695    uint16_t pucchSlot = 0;
696    SchUlSlotInfo  *schUlSlotInfo = NULLP;
697    SchPucchCfg    *schPucchCfg = NULLP;
698
699    GET_UE_IDX(crnti, ueIdx);
700    if(cell->ueCb[ueIdx].ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfgPres)
701    {
702       schPucchCfg = &(cell->ueCb[ueIdx].ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfg);
703      if(schPucchCfg->dlDataToUlAck)
704      {
705         for(dlToUlAckIdx = 0; dlToUlAckIdx < schPucchCfg->dlDataToUlAck->dlDataToUlAckListCount; dlToUlAckIdx++)
706         {
707            //For now considering only the first value in the list
708            k1 = schPucchCfg->dlDataToUlAck->dlDataToUlAckList[dlToUlAckIdx];
709            break;
710         }
711      }
712    }
713    
714    pucchSlot = (slot + k1)  % cell->numSlots;
715    schUlSlotInfo = cell->schUlSlotInfo[pucchSlot];
716    memset(&schUlSlotInfo->schPucchInfo, 0, sizeof(SchPucchInfo));
717
718    schUlSlotInfo->pucchPres = true;
719    schUlSlotInfo->schPucchInfo.rnti = crnti;
720
721    return ROK;
722 }
723
724 /*******************************************************************
725  *
726  * @brief Fills pdcch and pdsch info for dedicated DL msg
727  *
728  * @details
729  *
730  *    Function : schDlRsrcAllocDlMsg
731  *
732  *    Functionality:
733  *       Fills pdcch and pdsch info for dl msg
734  *
735  * @params[in]
736  * @return ROK     - success
737  *         RFAILED - failure
738  *
739  * ****************************************************************/
740 uint8_t schDlRsrcAllocDlMsg(DlMsgAlloc *dlMsgAlloc, SchCellCb *cell, uint16_t crnti,
741       uint32_t *accumalatedSize, uint16_t slot)
742 {
743    uint8_t ueIdx;
744    uint16_t tbSize = 0;
745    PdcchCfg *pdcch = NULLP;
746    PdschCfg *pdsch = NULLP;
747    BwpCfg *bwp = NULLP;
748    SchUeCb ueCb;
749    SchControlRsrcSet coreset1;
750    SchPdschConfig pdschCfg;
751
752    pdcch = &dlMsgAlloc->dlMsgPdcchCfg;
753    pdsch = &dlMsgAlloc->dlMsgPdschCfg;
754    bwp = &dlMsgAlloc->bwp;
755
756    GET_UE_IDX(crnti, ueIdx);
757    ueCb  = cell->ueCb[ueIdx-1];
758    coreset1 = ueCb.ueCfg.spCellCfg.servCellCfg.initDlBwp.pdcchCfg.cRSetToAddModList[0];
759    pdschCfg = ueCb.ueCfg.spCellCfg.servCellCfg.initDlBwp.pdschCfg;
760
761    /* fill BWP */
762    bwp->freqAlloc.numPrb = MAX_NUM_RB;
763    bwp->freqAlloc.startPrb = 0;
764    bwp->subcarrierSpacing = cell->cellCfg.sib1SchCfg.bwp.subcarrierSpacing;
765    bwp->cyclicPrefix = cell->cellCfg.sib1SchCfg.bwp.cyclicPrefix;
766
767    /* fill the PDCCH PDU */
768    //Considering coreset1 also starts from same symbol as coreset0
769    pdcch->coresetCfg.startSymbolIndex = coresetIdxTable[0][3];
770    pdcch->coresetCfg.durationSymbols = coreset1.duration;
771    memcpy(pdcch->coresetCfg.freqDomainResource, coreset1.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
772    pdcch->coresetCfg.cceRegMappingType = coreset1.cceRegMappingType; /* non-interleaved */
773    pdcch->coresetCfg.regBundleSize = 6;   /* must be 6 for non-interleaved */
774    pdcch->coresetCfg.interleaverSize = 0; /* NA for non-interleaved */
775    pdcch->coresetCfg.coreSetType = 1; /* non PBCH coreset */
776    //Considering number of RBs in coreset1 is same as coreset0
777    pdcch->coresetCfg.coreSetSize = coresetIdxTable[0][1];
778    pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
779    pdcch->coresetCfg.precoderGranularity =  coreset1.precoderGranularity;
780    pdcch->numDlDci = 1;
781    pdcch->dci.rnti = ueCb.crnti;
782    pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
783    pdcch->dci.scramblingRnti = 0;
784    pdcch->dci.cceIndex = 0; /* 0-3 for UL and 4-7 for DL */
785    pdcch->dci.aggregLevel = 4;
786    pdcch->dci.beamPdcchInfo.numPrgs = 1;
787    pdcch->dci.beamPdcchInfo.prgSize = 1;
788    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
789    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
790    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
791    pdcch->dci.txPdcchPower.powerValue = 0;
792    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
793
794    /* fill the PDSCH PDU */
795    uint8_t cwCount = 0;
796    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
797    pdsch->rnti = ueCb.crnti;
798    pdsch->pduIndex = 0;
799    pdsch->numCodewords = 1;
800    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
801    {
802       pdsch->codeword[cwCount].targetCodeRate = 308;
803       pdsch->codeword[cwCount].qamModOrder = ueCb.ueCfg.dlModInfo.modOrder;
804       pdsch->codeword[cwCount].mcsIndex = ueCb.ueCfg.dlModInfo.mcsIndex;
805       pdsch->codeword[cwCount].mcsTable = ueCb.ueCfg.dlModInfo.mcsTable;
806       pdsch->codeword[cwCount].rvIndex = 0;
807       tbSize = schCalcTbSize(*accumalatedSize + TX_PAYLOAD_HDR_LEN);
808       if(tbSize < *accumalatedSize)
809          *accumalatedSize = tbSize - TX_PAYLOAD_HDR_LEN;
810       pdsch->codeword[cwCount].tbSize = tbSize;
811    }
812    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
813    pdsch->numLayers = 1;
814    pdsch->transmissionScheme = 0;
815    pdsch->refPoint = 0;
816    pdsch->dmrs.dlDmrsSymbPos = 4; /* Bitmap value 00000000000100 i.e. using 3rd symbol for PDSCH DMRS */
817    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
818    pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
819    pdsch->dmrs.scid = 0;
820    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
821    pdsch->dmrs.dmrsPorts = 0;
822    pdsch->dmrs.mappingType      = DMRS_MAP_TYPE_A; /* Setting to Type-A */
823    pdsch->dmrs.nrOfDmrsSymbols  = NUM_DMRS_SYMBOLS;
824    pdsch->dmrs.dmrsAddPos       = pdschCfg.dmrsDlCfgForPdschMapTypeA.addPos;
825    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
826    pdsch->pdschFreqAlloc.freqAlloc.startPrb = PDSCH_START_RB;
827    pdsch->pdschFreqAlloc.freqAlloc.numPrb = schCalcNumPrb(tbSize, ueCb.ueCfg.dlModInfo.mcsIndex, \
828                    pdschCfg.timeDomRsrcAllociList[0].symbolLength);
829    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
830    pdsch->pdschTimeAlloc.timeAlloc.startSymb = pdschCfg.timeDomRsrcAllociList[0].startSymbol;
831    pdsch->pdschTimeAlloc.timeAlloc.numSymb = pdschCfg.timeDomRsrcAllociList[0].symbolLength;
832    pdsch->beamPdschInfo.numPrgs = 1;
833    pdsch->beamPdschInfo.prgSize = 1;
834    pdsch->beamPdschInfo.digBfInterfaces = 0;
835    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
836    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
837    pdsch->txPdschPower.powerControlOffset = 0;
838    pdsch->txPdschPower.powerControlOffsetSS = 0;
839
840    pdcch->dci.pdschCfg = pdsch;
841    return ROK;
842 }
843
844 /*******************************************************************
845  *
846  * @brief Fills k0 and k1 information table for FDD 
847  *
848  * @details
849  *
850  *    Function : BuildK0K1TableForFdd 
851  *
852  *    Functionality:
853  *      Fills k0 and k1 information table for FDD
854  *
855  * @params[in] SchCellCb *cell,SchK0K1TimingInfoTbl *k0K1InfoTbl,bool
856  * pdschCfgCmnPres,uint8_t numTimeDomAlloc, SchPdschCfgCmnTimeDomRsrcAlloc
857  * cmnTimeDomRsrcAllocList[], SchPdschTimeDomRsrcAlloc
858  * dedTimeDomRsrcAllocList[], uint8_t ulAckListCount, uint8_t *UlAckTbl
859  * @return ROK     - success
860  *         RFAILED - failure
861  *
862  * ****************************************************************/
863 void BuildK0K1TableForFdd(SchCellCb *cell, SchK0K1TimingInfoTbl *k0K1InfoTbl, bool pdschCfgCmnPres,SchPdschCfgCmn pdschCmnCfg,\
864 SchPdschConfig pdschDedCfg, uint8_t ulAckListCount, uint8_t *UlAckTbl)
865 {
866    uint8_t k0TmpVal = 0, k1TmpVal =0, cfgIdx=0;
867    uint8_t slotIdx=0, k0Index=0, k1Index=0, numK0=0, numK1=0, numTimeDomAlloc=0;
868    SchPdschCfgCmnTimeDomRsrcAlloc cmnTimeDomRsrcAllocList[MAX_NUM_DL_ALLOC];
869    SchPdschTimeDomRsrcAlloc dedTimeDomRsrcAllocList[MAX_NUM_DL_ALLOC];
870
871    /* Initialization the structure and storing the total slot values. */
872    memset(k0K1InfoTbl, 0, sizeof(SchK0K1TimingInfoTbl));
873    k0K1InfoTbl->tblSize = cell->numSlots;
874    
875    /* Storing time domain resource allocation list based on common or dedicated configuration. */
876    if(pdschCfgCmnPres == true)
877    {
878       numTimeDomAlloc = pdschCmnCfg.numTimeDomAlloc;
879       for(cfgIdx = 0; cfgIdx<numTimeDomAlloc; cfgIdx++)
880       {
881          cmnTimeDomRsrcAllocList[cfgIdx] = pdschCmnCfg.timeDomRsrcAllocList[cfgIdx];
882       }
883    }
884    else
885    {
886       numTimeDomAlloc = pdschDedCfg.numTimeDomRsrcAlloc;
887       for(cfgIdx = 0; cfgIdx<numTimeDomAlloc; cfgIdx++)
888       {
889          dedTimeDomRsrcAllocList[cfgIdx] = pdschDedCfg.timeDomRsrcAllociList[cfgIdx];
890       }
891    }
892    
893    /* Checking all the slots for K0 and K1 values. */
894    for(slotIdx = 0; slotIdx < cell->numSlots; slotIdx++)
895    {
896       numK0 = 0;
897       /* Storing the values of k0 based on time domain resource
898        * allocation list. If the value is unavailable then fill default values,
899        * As per 38.331 PDSCH-TimeDomainResourceAllocation field descriptions. */
900       for(k0Index = 0; ((k0Index < numTimeDomAlloc) && (k0Index < MAX_NUM_K0_IDX));  k0Index++)
901       {
902          if(pdschCfgCmnPres == true)
903          {
904             k0TmpVal = cmnTimeDomRsrcAllocList[k0Index].k0;
905          }
906          else
907          {
908             if(dedTimeDomRsrcAllocList[k0Index].k0 != NULLP)
909             {
910                k0TmpVal = *(dedTimeDomRsrcAllocList[k0Index].k0);
911             }
912             else
913             { 
914                k0TmpVal = DEFAULT_K0_VALUE;
915             }
916          }
917          
918          /* Checking all the Ul Alloc values. If value is less than MIN_NUM_K1_IDX
919           * then skip else continue storing the values. */
920          numK1 = 0;
921          for(k1Index = 0; k1Index < ulAckListCount; k1Index++)
922          {
923             k1TmpVal = UlAckTbl[k1Index];
924             if(k1TmpVal <= MIN_NUM_K1_IDX)
925             {
926                continue;
927             }
928
929             k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k1TimingInfo.k1Indexes[numK1++] = k1Index;
930             /* TODO Store K1 index where harq feedback will be received in harq table. */ 
931          }
932          if(numK1)
933          {
934             k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k1TimingInfo.numK1 = numK1;
935             k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k0Index = k0Index;
936             numK0++;
937          }
938       }
939       if(numK0)
940       {
941          k0K1InfoTbl->k0k1TimingInfo[slotIdx].numK0 = numK0;
942       }
943    }
944 }
945
946 /*******************************************************************
947  *
948  * @brief Fills k0 and k1 information table  
949  *
950  * @details
951  *
952  *    Function : BuildK0K1Table
953  *
954  *    Functionality:
955  *       Fills K0 and k1 information table 
956  *
957  * @params[in] SchCellCb *cell,SchK0K1TimingInfoTbl *k0K1InfoTbl,bool
958  * pdschCfgCmnPres,uint8_t numTimeDomAlloc, SchPdschCfgCmnTimeDomRsrcAlloc
959  * cmnTimeDomRsrcAllocList[], SchPdschTimeDomRsrcAlloc
960  * dedTimeDomRsrcAllocList[], uint8_t ulAckListCount, uint8_t *UlAckTbl
961  * @return ROK     - success
962  *         RFAILED - failure
963  *
964  * ****************************************************************/
965 void BuildK0K1Table(SchCellCb *cell, SchK0K1TimingInfoTbl *k0K1InfoTbl, bool pdschCfgCmnPres, SchPdschCfgCmn pdschCmnCfg,\
966 SchPdschConfig pdschDedCfg, uint8_t ulAckListCount, uint8_t *UlAckTbl)
967 {
968
969 #ifdef NR_TDD
970    SlotConfig  slotCfg;
971    bool ulSlotPresent = false;
972    uint8_t k0TmpVal = 0, k1TmpVal =0, tmpSlot=0, startSymbol=0, endSymbol=0, checkSymbol=0;
973    uint8_t slotIdx=0, k0Index=0, k1Index=0, numK0=0, numK1=0, cfgIdx=0, numTimeDomAlloc =0, totalCfgSlot =0;
974    SchPdschCfgCmnTimeDomRsrcAlloc cmnTimeDomRsrcAllocList[MAX_NUM_DL_ALLOC];
975    SchPdschTimeDomRsrcAlloc dedTimeDomRsrcAllocList[MAX_NUM_DL_ALLOC];
976 #endif
977
978    if(cell->cellCfg.dupMode == DUPLEX_MODE_FDD)
979    {
980       BuildK0K1TableForFdd(cell, k0K1InfoTbl, pdschCfgCmnPres, pdschCmnCfg, pdschDedCfg, ulAckListCount, UlAckTbl);
981    }
982    else
983    {
984 #ifdef NR_TDD
985       
986       /* Initialization the K0K1 structure, total num of slot and calculating the slot pattern length. */
987       memset(k0K1InfoTbl, 0, sizeof(SchK0K1TimingInfoTbl));
988       k0K1InfoTbl->tblSize = cell->numSlots;
989       totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.ssbSchCfg.scsCommon, cell->cellCfg.tddCfg.tddPeriod);
990       
991       /* Storing time domain resource allocation list based on common or 
992        * dedicated configuration availability. */
993       if(pdschCfgCmnPres == true)
994       {
995          numTimeDomAlloc = pdschCmnCfg.numTimeDomAlloc;
996          for(cfgIdx = 0; cfgIdx<numTimeDomAlloc; cfgIdx++)
997          {
998             cmnTimeDomRsrcAllocList[cfgIdx] = pdschCmnCfg.timeDomRsrcAllocList[cfgIdx];
999          }
1000       }
1001       else
1002       {
1003          numTimeDomAlloc = pdschDedCfg.numTimeDomRsrcAlloc;
1004          for(cfgIdx = 0; cfgIdx<numTimeDomAlloc; cfgIdx++)
1005          {
1006             dedTimeDomRsrcAllocList[cfgIdx] = pdschDedCfg.timeDomRsrcAllociList[cfgIdx];
1007          }
1008       }
1009
1010       /* Checking all possible indexes for K0 and K1 values. */
1011       for(slotIdx = 0; slotIdx < cell->numSlots; slotIdx++)
1012       {
1013          /* If current slot is UL or FLEXI then Skip because PDCCH is sent only in DL slots. */
1014          slotCfg = schGetSlotSymbFrmt(slotIdx%totalCfgSlot, cell->slotFrmtBitMap);
1015          if(slotCfg == UL_SLOT || slotCfg == FLEXI_SLOT)
1016          {
1017             continue;
1018          }
1019          
1020          /* Storing K0 , start symbol and length symbol for further processing.
1021           * If K0 value is not available then we can fill the default values
1022           * given in spec 38.331. */
1023          numK0 = 0;
1024          for(k0Index = 0; ((k0Index < numTimeDomAlloc) && (k0Index < MAX_NUM_K0_IDX)); k0Index++)
1025          {
1026             if(pdschCfgCmnPres == true)
1027             {
1028                k0TmpVal = cmnTimeDomRsrcAllocList[k0Index].k0;
1029                startSymbol = cmnTimeDomRsrcAllocList[k0Index].startSymbol;
1030                endSymbol = startSymbol + cmnTimeDomRsrcAllocList[k0Index].lengthSymbol;
1031             }
1032             else
1033             {
1034                if(dedTimeDomRsrcAllocList[k0Index].k0 != NULLP)
1035                {
1036                   k0TmpVal =  *(dedTimeDomRsrcAllocList[k0Index].k0);
1037                }
1038                else
1039                {
1040                   k0TmpVal = DEFAULT_K0_VALUE;
1041                }
1042                startSymbol = dedTimeDomRsrcAllocList[k0Index].startSymbol;
1043                endSymbol = startSymbol + dedTimeDomRsrcAllocList[k0Index].symbolLength;
1044             }
1045             
1046             /* If current slot + k0 is UL then skip the slot
1047              * else if it is DL slot then continue the next steps
1048              * else if it is a FLEXI slot then check symbols of slot, It should not
1049              * contain any UL slot. */
1050             tmpSlot = (slotIdx+k0TmpVal) % totalCfgSlot;
1051             slotCfg = schGetSlotSymbFrmt(tmpSlot, cell->slotFrmtBitMap);
1052             if(slotCfg == UL_SLOT)
1053             {
1054                continue;
1055             }
1056             if(slotCfg == FLEXI_SLOT)
1057             {
1058                for(checkSymbol = startSymbol; checkSymbol<endSymbol; checkSymbol ++)
1059                {
1060                   slotCfg = cell->cellCfg.tddCfg.slotCfg[tmpSlot][checkSymbol];
1061                   if(slotCfg == UL_SLOT)
1062                   {
1063                      continue;
1064                   }
1065                }
1066             }
1067
1068             /* If current slot + k0 + k1 is a DL slot then skip the slot
1069              * else if it is UL slot then store the information 
1070              * else if it is FLEXI slot then check the symbols, it must have
1071              * at least one UL symbol. */
1072             numK1 = 0;
1073             for(k1Index = 0; k1Index < ulAckListCount; k1Index++)
1074             {
1075                k1TmpVal = UlAckTbl[k1Index];
1076                if(k1TmpVal > MIN_NUM_K1_IDX)
1077                {
1078                   tmpSlot = (slotIdx+k0TmpVal+k1TmpVal) % totalCfgSlot;
1079                   slotCfg =  schGetSlotSymbFrmt(tmpSlot, cell->slotFrmtBitMap);
1080                   if(slotCfg == DL_SLOT) 
1081                   {
1082                      continue;
1083                   }   
1084                   if(slotCfg == FLEXI_SLOT)
1085                   {
1086                      for(checkSymbol = 0; checkSymbol<SCH_SYMBOL_PER_SLOT;checkSymbol++)
1087                      {
1088                         if(cell->cellCfg.tddCfg.slotCfg[tmpSlot][checkSymbol] == UL_SLOT)
1089                         {
1090                            ulSlotPresent = true;
1091                            break;
1092                         }
1093                      }
1094                   }
1095                   if(ulSlotPresent == true || slotCfg ==  UL_SLOT)
1096                   {
1097                      k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k1TimingInfo.k1Indexes[numK1++] = k1Index;
1098                      /* TODO Store K1 index where harq feedback will be received
1099                       * in harq table. */
1100                   }
1101                }
1102             }
1103             
1104             /* Store all the values if all condition satisfies. */
1105             if(numK1)
1106             {
1107                k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k1TimingInfo.numK1 = numK1;
1108                k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k0Index = k0Index;
1109                numK0++;
1110             }
1111          }
1112          if(numK0)
1113          {
1114             k0K1InfoTbl->k0k1TimingInfo[slotIdx].numK0 = numK0;
1115          }
1116       }
1117 #endif
1118    }
1119 }
1120
1121 /*******************************************************************
1122 *
1123 * @brief Fills K2 information table for FDD
1124 *
1125 * @details
1126 *
1127 *    Function : BuildK2InfoTableForFdd 
1128 *
1129 *    Functionality:
1130 *       Fills K2 information table for FDD
1131 *
1132 * @params[in] SchCellCb *cell,SchPuschTimeDomRsrcAlloc timeDomRsrcAllocList[],
1133 * uint16_t puschSymTblSize,SchK2TimingInfoTbl *k2InfoTbl
1134 * @return ROK     - success
1135 *         RFAILED - failure
1136 *
1137 * ****************************************************************/
1138 void BuildK2InfoTableForFdd(SchCellCb *cell, SchPuschTimeDomRsrcAlloc timeDomRsrcAllocList[], uint16_t puschSymTblSize,\
1139 SchK2TimingInfoTbl *k2InfoTbl)
1140 {
1141    uint16_t slotIdx=0, k2Index=0, k2TmpVal=0;
1142
1143    /* Initialization the structure and storing the total slot values. */
1144    memset(k2InfoTbl, 0, sizeof(SchK2TimingInfoTbl));
1145    k2InfoTbl->tblSize = cell->numSlots;
1146    
1147    /* Checking all possible indexes for K2. */
1148    for(slotIdx = 0; slotIdx < cell->numSlots; slotIdx++)
1149    {
1150       /* Storing K2 values. */
1151       for(k2Index = 0; ((k2Index < puschSymTblSize) && (k2Index < MAX_NUM_K2_IDX)); k2Index++)
1152       {
1153          k2TmpVal= k2InfoTbl->k2TimingInfo[slotIdx].numK2;
1154          k2InfoTbl->k2TimingInfo[slotIdx].k2Indexes[k2TmpVal] = k2Index;
1155          k2InfoTbl->k2TimingInfo[slotIdx].numK2++;
1156       }
1157    }
1158 }
1159
1160 /*******************************************************************
1161  *
1162  * @brief Fills K2 information table
1163  *
1164  * @details
1165  *
1166  *    Function : BuildK2InfoTable 
1167  *
1168  *    Functionality:
1169  *       Fills K2 information table
1170  *
1171  * @params[in] SchCellCb *cell,SchPuschTimeDomRsrcAlloc timeDomRsrcAllocList[],
1172  * uint16_t puschSymTblSize, SchK2TimingInfoTbl *k2InfoTbl
1173  * @return ROK     - success
1174  *         RFAILED - failure
1175  *
1176  * ****************************************************************/
1177 void BuildK2InfoTable(SchCellCb *cell, SchPuschTimeDomRsrcAlloc timeDomRsrcAllocList[], uint16_t puschSymTblSize,\
1178 SchK2TimingInfoTbl *k2InfoTbl)
1179 {
1180
1181 #ifdef NR_TDD
1182    bool dlSymbolPresent = false;
1183    uint8_t slotIdx=0, k2Index=0, k2TmpVal=0, numK2 =0, currentSymbol =0;
1184    uint8_t startSymbol =0, endSymbol =0, checkSymbol=0, totalCfgSlot=0, slotCfg=0;
1185    SlotConfig currentSlot;
1186 #endif
1187
1188    if(cell->cellCfg.dupMode == DUPLEX_MODE_FDD)
1189    {
1190       BuildK2InfoTableForFdd(cell, timeDomRsrcAllocList, puschSymTblSize, k2InfoTbl);
1191    }
1192    else
1193    {
1194 #ifdef NR_TDD
1195
1196       /* Initialization the structure and storing the total slot values. */
1197       memset(k2InfoTbl, 0, sizeof(SchK2TimingInfoTbl));
1198       k2InfoTbl->tblSize = cell->numSlots;
1199       totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.ssbSchCfg.scsCommon, cell->cellCfg.tddCfg.tddPeriod);
1200
1201       /* Checking all possible indexes for K2. */
1202       for(slotIdx = 0; slotIdx < k2InfoTbl->tblSize; slotIdx++)
1203       {
1204          currentSlot = schGetSlotSymbFrmt(slotIdx % totalCfgSlot, cell->slotFrmtBitMap);
1205          
1206          /* If current slot is UL then skip because PDCCH is sent only in DL slots */
1207          if(currentSlot != UL_SLOT)
1208          {
1209             for(k2Index = 0; ((k2Index < puschSymTblSize) && (k2Index < MAX_NUM_K2_IDX)); k2Index++)
1210             {
1211                /* Storing k2, startSymbol, endSymbol information for further processing.
1212                 * If k2 is absent then fill the default values given in spec 38.331
1213                 * PUSCH-TimeDomainResourceAllocationList field descriptions */
1214                k2TmpVal = timeDomRsrcAllocList[k2Index].k2;
1215                if(!k2TmpVal)
1216                {
1217                   switch(cell->cellCfg.ssbSchCfg.scsCommon)
1218                   {
1219                      case SCS_15KHZ:
1220                         k2TmpVal = DEFAULT_K2_VALUE_FOR_SCS15;
1221                         break;
1222                      case SCS_30KHZ:
1223                         k2TmpVal = DEFAULT_K2_VALUE_FOR_SCS30;
1224                         break;
1225                      case SCS_60KHZ:
1226                         k2TmpVal = DEFAULT_K2_VALUE_FOR_SCS60;
1227                         break;
1228                      case SCS_120KHZ:
1229                         k2TmpVal = DEFAULT_K2_VALUE_FOR_SCS120;
1230                         break;
1231                   }
1232                }
1233                
1234                /* Current slot + k2 should be either UL or FLEXI slot.
1235                 * If slot is FLEXI then check all the symbols of that slot,
1236                 * it should not contain any DL or FLEXI slot */
1237                k2TmpVal = (slotIdx + k2TmpVal) % totalCfgSlot;
1238                slotCfg = schGetSlotSymbFrmt(k2TmpVal, cell->slotFrmtBitMap);
1239                if(slotCfg == DL_SLOT)
1240                {
1241                   continue;
1242                }
1243                if(slotCfg == FLEXI_SLOT)
1244                {
1245                   startSymbol =  timeDomRsrcAllocList[k2Index].startSymbol;
1246                   endSymbol   =  startSymbol+ timeDomRsrcAllocList[k2Index].symbolLength;
1247                   dlSymbolPresent = false;
1248                   for(checkSymbol= startSymbol; checkSymbol<endSymbol; checkSymbol++)
1249                   {
1250                      currentSymbol = cell->cellCfg.tddCfg.slotCfg[k2TmpVal][checkSymbol];
1251                      if(currentSymbol == DL_SLOT || currentSymbol == FLEXI_SLOT)
1252                      {
1253                         dlSymbolPresent = true;
1254                         break;
1255                      }
1256                   }
1257                }
1258                /* Store all the values if all condition satisfies. */
1259                if(dlSymbolPresent != true || slotCfg == UL_SLOT)
1260                {
1261                   numK2 = k2InfoTbl->k2TimingInfo[slotIdx].numK2;
1262                   k2InfoTbl->k2TimingInfo[slotIdx].k2Indexes[numK2] = k2Index;
1263                   k2InfoTbl->k2TimingInfo[slotIdx].numK2++;
1264                }
1265             }
1266          }
1267       }
1268 #endif
1269    }
1270 }
1271
1272 /**********************************************************************
1273   End of file
1274  **********************************************************************/
1275