Implementation of function for filling k0 and k1 table [Issue-ID: ODUHIGH-341]
[o-du/l2.git] / src / 5gnrsch / sch_ue_mgr.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 /* This file contains UE management handling functionality for SCH */
19
20 #include "common_def.h"
21 #include "tfu.h"
22 #include "lrg.h"
23
24 #include "tfu.x"
25 #include "lrg.x"
26 #include "du_log.h"
27 #include "du_app_mac_inf.h"
28 #include "mac_sch_interface.h"
29 #include "sch.h"
30 #include "sch_utils.h"
31
32 /* local defines */
33 SchUeCfgRspFunc SchUeCfgRspOpts[] =
34 {
35    packSchUeCfgRsp,      /* LC */
36    MacProcSchUeCfgRsp,   /* TC */
37    packSchUeCfgRsp       /* LWLC */
38 };
39
40 SchUeDeleteRspFunc SchUeDeleteRspOpts[] =
41 {
42    packSchUeDeleteRsp,      /* LC */
43    MacProcSchUeDeleteRsp,   /* TC */
44    packSchUeDeleteRsp       /* LWLC */
45 };
46
47 SchCellDeleteRspFunc SchCellDeleteRspOpts[]=
48 {
49    packSchCellDeleteRsp,      /* LC */
50    MacProcSchCellDeleteRsp,   /* TC */
51    packSchCellDeleteRsp       /* LWLC */
52 };
53 /*******************************************************************
54  *
55  * @brief Fill and send UE cfg response to MAC
56  *
57  * @details
58  *
59  *    Function : SchSendUeCfgRspToMac
60  *
61  *    Functionality: Fill and send UE cfg response to MAC
62  *
63  * @params[in] 
64  * @return ROK     - success
65  *         RFAILED - failure
66  *
67  * ****************************************************************/
68 void SchSendUeCfgRspToMac(uint16_t event, SchUeCfg *ueCfg, Inst inst,\
69       SchMacRsp result, SchUeCfgRsp *cfgRsp)
70 {
71    Pst rspPst;
72
73    cfgRsp->cellId = ueCfg->cellId;
74    cfgRsp->crnti = ueCfg->crnti;
75    GET_UE_IDX(ueCfg->crnti, cfgRsp->ueIdx);
76    cfgRsp->rsp = result;   
77
78    /* Filling response post */
79    memset(&rspPst, 0, sizeof(Pst));
80    FILL_PST_SCH_TO_MAC(rspPst, inst);
81    if(event == EVENT_ADD_UE_CONFIG_REQ_TO_SCH)
82    {
83       rspPst.event = EVENT_UE_CONFIG_RSP_TO_MAC;
84       DU_LOG("\nINFO  -->  SCH :  Sending UE Config response to MAC");
85    }
86    else if(event == EVENT_MODIFY_UE_CONFIG_REQ_TO_SCH)
87    {
88       rspPst.event = EVENT_UE_RECONFIG_RSP_TO_MAC;
89       DU_LOG("\nINFO  -->  SCH :  Sending UE Reconfig response to MAC");
90    }
91    SchUeCfgRspOpts[rspPst.selector](&rspPst, cfgRsp);
92 }
93
94 /*******************************************************************
95  
96  *
97  * @brief Function to fill Dl Lc Context in SCH Ue Cb
98  *
99  * @details
100  *
101  *    Function : fillSchDlLcCtxt
102  *
103  *    Functionality: Function to fill Dl Lc Context in SCH Ue Cb
104  *
105  * @params[in] SchDlLcCtxt pointer,
106  *             SchLcCfg pointer
107  * @return void
108  *
109  * ****************************************************************/
110
111 void fillSchDlLcCtxt(SchDlLcCtxt *ueCbLcCfg, SchLcCfg *lcCfg)
112 {
113    ueCbLcCfg->lcId = lcCfg->lcId;
114    ueCbLcCfg->lcp = lcCfg->dlLcCfg.lcp;
115    ueCbLcCfg->lcState = SCH_LC_STATE_ACTIVE;
116    ueCbLcCfg->bo = 0;
117 }
118
119 /*******************************************************************
120  *
121  * @brief Function to fill Ul Lc Context in SCH Ue Cb
122  *
123  * @details
124  *
125  *    Function : fillSchUlLcCtxt
126  *
127  *    Functionality: Function to fill Ul Lc Context in SCH Ue Cb
128  *
129  * @params[in] SchUlLcCtxt pointer,
130  *             SchLcCfg pointer
131  * @return void
132  *
133  * ****************************************************************/
134
135 void fillSchUlLcCtxt(SchUlLcCtxt *ueCbLcCfg, SchLcCfg *lcCfg)
136 {
137    ueCbLcCfg->lcId = lcCfg->lcId;
138    ueCbLcCfg->lcState = SCH_LC_STATE_ACTIVE;
139    ueCbLcCfg->priority = lcCfg->ulLcCfg.priority;
140    ueCbLcCfg->lcGroup = lcCfg->ulLcCfg.lcGroup;
141    ueCbLcCfg->schReqId = lcCfg->ulLcCfg.schReqId;
142    ueCbLcCfg->pbr     = lcCfg->ulLcCfg.pbr;
143    ueCbLcCfg->bsd     = lcCfg->ulLcCfg.bsd;
144
145 }
146
147 /*******************************************************************
148  *
149  * @brief Function to update Sch Ul Lc Cb
150  *
151  * @details
152  *
153  *    Function : updateSchUlCb
154  *
155  *    Functionality: Function to update SCH Ul Lc Cb
156  *
157  * @returns void
158  *
159  * ****************************************************************/
160
161 void updateSchUlCb(uint8_t delIdx, SchUlCb *ulInfo)
162 {
163    uint8_t lcIdx = 0;
164
165    for(lcIdx = delIdx; lcIdx < ulInfo->numUlLc; lcIdx++)
166    {
167       memcpy(&ulInfo->ulLcCtxt[lcIdx], &ulInfo->ulLcCtxt[lcIdx+1], sizeof(SchUlLcCtxt));
168       memset(&ulInfo->ulLcCtxt[lcIdx+1], 0, sizeof(SchUlLcCtxt));
169    }
170 }
171
172 /*******************************************************************
173  *
174  * @brief Function to update SCH Dl Lc Cb
175  *
176  * @details
177  *
178  *    Function : updateSchDlCb
179  *
180  *    Functionality: Function to update SCH DL Lc Cb
181  *
182  * @returns void
183  *
184  * ****************************************************************/
185
186 void updateSchDlCb(uint8_t delIdx, SchDlCb *dlInfo)
187 {
188    uint8_t lcIdx = 0;
189
190    for(lcIdx = delIdx; lcIdx < dlInfo->numDlLc; lcIdx++)
191    {
192       memcpy(&dlInfo->dlLcCtxt[lcIdx], &dlInfo->dlLcCtxt[lcIdx+1], sizeof(SchDlLcCtxt));
193       memset(&dlInfo->dlLcCtxt[lcIdx+1], 0, sizeof(SchDlLcCtxt));
194    }
195 }
196
197 /*******************************************************************
198  *
199  * @brief Function to fill SchUeCb
200  *
201  * @details
202  *
203  *    Function : fillSchUeCb
204  *
205  *    Functionality: Function to fill SchUeCb
206  *
207  * @params[in] SchUeCb pointer,
208  *             SchUeCfg pointer
209  * @return ROK/RFAILED
210  *
211  * ****************************************************************/
212
213 uint8_t fillSchUeCb(SchUeCb *ueCb, SchUeCfg *ueCfg)
214 {
215    uint8_t   lcIdx, ueLcIdx;
216    SchPdschCfgCmn pdschCfg;
217    SchPucchDlDataToUlAck *dlDataToUlAck;
218
219    ueCb->ueCfg.cellId = ueCfg->cellId;
220    ueCb->ueCfg.crnti = ueCfg->crnti;
221    if(ueCfg->macCellGrpCfgPres == true)
222    {
223       memcpy(&ueCb->ueCfg.macCellGrpCfg , &ueCfg->macCellGrpCfg, sizeof(SchMacCellGrpCfg)); 
224       ueCb->ueCfg.macCellGrpCfgPres = true;
225    }
226
227    if(ueCfg->phyCellGrpCfgPres == true)
228    {
229       memcpy(&ueCb->ueCfg.phyCellGrpCfg ,  &ueCfg->phyCellGrpCfg, sizeof(SchPhyCellGrpCfg));
230       ueCb->ueCfg.phyCellGrpCfgPres = true;
231    }
232
233    if(ueCfg->spCellCfgPres == true)
234    {
235       memcpy(&ueCb->ueCfg.spCellCfg , &ueCfg->spCellCfg, sizeof(SchSpCellCfg));
236       ueCb->ueCfg.spCellCfgPres = true;
237       dlDataToUlAck = ueCfg->spCellCfg.servCellCfg.initUlBwp.pucchCfg.dlDataToUlAck;
238       if(dlDataToUlAck)
239       {
240          BuildK0K1Table(ueCb->cellCb, &ueCb->ueCfg.spCellCfg.servCellCfg.initDlBwp.k0K1InfoTbl, false, pdschCfg,\
241          ueCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg, dlDataToUlAck->dlDataToUlAckListCount,\
242          dlDataToUlAck->dlDataToUlAckList);
243       }
244    }
245
246    ueCb->state = SCH_UE_STATE_ACTIVE;
247    if(ueCfg->ambrCfg)
248    {
249       SCH_FREE(ueCb->ueCfg.ambrCfg, sizeof(SchAmbrCfg));
250       ueCb->ueCfg.ambrCfg =  ueCfg->ambrCfg;
251    }
252    memcpy(&ueCb->ueCfg.dlModInfo,  &ueCfg->dlModInfo , sizeof(SchModulationInfo));
253    memcpy(&ueCb->ueCfg.ulModInfo,  &ueCfg->ulModInfo , sizeof(SchModulationInfo));
254
255    //Updating SchUlCb and SchDlCb DB in SchUeCb
256    for(lcIdx = 0; lcIdx < ueCfg->numLcs; lcIdx++)
257    {
258       if(ueCfg->schLcCfg[lcIdx].configType == CONFIG_ADD)
259       {
260          fillSchUlLcCtxt(&ueCb->ulInfo.ulLcCtxt[ueCb->ulInfo.numUlLc], &ueCfg->schLcCfg[lcIdx]);
261          ueCb->ulInfo.numUlLc++;
262          fillSchDlLcCtxt(&ueCb->dlInfo.dlLcCtxt[ueCb->dlInfo.numDlLc], &ueCfg->schLcCfg[lcIdx]);
263          ueCb->dlInfo.numDlLc++;
264       }
265       else
266       {
267          for(ueLcIdx = 0; ueLcIdx < ueCb->ulInfo.numUlLc; ueLcIdx++) //searching for Lc to be Mod
268          {
269             if(ueCb->ulInfo.ulLcCtxt[ueLcIdx].lcId == ueCfg->schLcCfg[lcIdx].lcId)
270             {
271                if(ueCfg->schLcCfg[lcIdx].configType == CONFIG_MOD)
272                {
273                   fillSchUlLcCtxt(&ueCb->ulInfo.ulLcCtxt[ueLcIdx], &ueCfg->schLcCfg[lcIdx]);
274                   fillSchDlLcCtxt(&ueCb->dlInfo.dlLcCtxt[ueLcIdx], &ueCfg->schLcCfg[lcIdx]);
275                   break;
276                }
277                if(ueCfg->schLcCfg[ueLcIdx].configType == CONFIG_DEL)
278                {
279                   memset(&ueCb->ulInfo.ulLcCtxt[ueLcIdx], 0, sizeof(SchUlLcCtxt));
280                   ueCb->ulInfo.numUlLc--;
281                   updateSchUlCb(ueLcIdx, &ueCb->ulInfo); //moving arr elements one idx ahead 
282                   memset(&ueCb->dlInfo.dlLcCtxt[ueLcIdx], 0, sizeof(SchDlLcCtxt));
283                   ueCb->dlInfo.numDlLc--;
284                   updateSchDlCb(ueLcIdx, &ueCb->dlInfo); //moving arr elements one idx ahead
285                   break;
286                }
287             }
288          }/*End of inner for loop */
289       }
290
291       SCH_FREE(ueCfg->schLcCfg[lcIdx].drbQos, sizeof(SchDrbQosInfo));
292       SCH_FREE(ueCfg->schLcCfg[lcIdx].snssai, sizeof(SchSnssai));
293
294    }/* End of outer for loop */
295    return ROK;
296 }
297
298 /*******************************************************************
299  *
300  * @brief Function to get SCH Cell Cb
301  *
302  * @details
303  *
304  *    Function : getSchCellCb
305  *
306  *    Functionality: Function to get SCH Cell Cb
307  *
308  * @params[in] event, SchUeCfg pointer 
309  * @return schUeCb pointer  - success
310  *         NULLP - failure
311  *
312  * ****************************************************************/
313
314 SchCellCb *getSchCellCb(uint16_t srcEvent, Inst inst, SchUeCfg *ueCfg)
315 {
316    uint8_t      idx;
317    SchCellCb    *cellCb = NULLP;
318    SchUeCfgRsp  cfgRsp;
319    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
320
321    /* Search of cell cb */
322    for(idx = 0; idx < MAX_NUM_CELL; idx++)
323    {
324       cellCb = schCb[inst].cells[idx];
325       if(cellCb->cellId == ueCfg->cellId)
326          break;
327    }
328    if(idx == MAX_NUM_CELL)
329    {
330       DU_LOG("\nERROR  -->  SCH : Ue create request failed. Invalid cell id %d", ueCfg->cellId);
331       SchSendUeCfgRspToMac(srcEvent, ueCfg, inst, RSP_NOK, &cfgRsp);
332       return NULLP;
333    }
334
335    /* Check if max number of UE configured */
336    if(cellCb->numActvUe > MAX_NUM_UE)
337    {
338       DU_LOG("\nERROR  -->  SCH :  Max number of UE [%d] already configured", MAX_NUM_UE);
339       SchSendUeCfgRspToMac(srcEvent, ueCfg, inst, RSP_NOK, &cfgRsp);
340       return NULLP;
341    }
342    return cellCb;
343 }
344
345
346 /*******************************************************************
347  *
348  * @brief Function to Add Ue Config Request from MAC
349  *
350  * @details
351  *
352  *    Function : MacSchAddUeConfigReq
353  *
354  *    Functionality: Function to Add Ue config request from MAC
355  *
356  * @params[in] 
357  * @return ROK     - success
358  *         RFAILED - failure
359  *
360  * ****************************************************************/
361 uint8_t MacSchAddUeConfigReq(Pst *pst, SchUeCfg *ueCfg)
362 {
363    uint8_t ueIdx, lcIdx, ret = ROK;
364    SchCellCb    *cellCb = NULLP;
365    SchUeCb      *ueCb = NULLP;
366    SchUeCfgRsp  cfgRsp;
367    Inst         inst = pst->dstInst - 1;
368    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
369   
370 #ifdef CALL_FLOW_DEBUG_LOG
371    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_ADD_UE_CONFIG_REQ_TO_SCH\n");
372 #endif
373
374    if(!ueCfg)
375    {
376       DU_LOG("\nERROR  -->  SCH :  Adding UE Config Request failed at MacSchAddUeConfigReq()");
377       return RFAILED;
378    }
379    DU_LOG("\nDEBUG  -->  SCH :  Adding UE Config Request for CRNTI[%d]", ueCfg->crnti);
380    cellCb = getSchCellCb(pst->event, inst, ueCfg);
381
382    /* Search if UE already configured */
383    GET_UE_IDX(ueCfg->crnti, ueIdx);
384    ueCb = &cellCb->ueCb[ueIdx -1];
385    if(ueCb)
386    {
387       if((ueCb->crnti == ueCfg->crnti) && (ueCb->state == SCH_UE_STATE_ACTIVE))
388       {
389          DU_LOG("\nDEBUG  -->  SCH : CRNTI %d already configured ", ueCfg->crnti);
390          SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
391          return ROK;
392       }
393    }
394    else
395    {
396       DU_LOG("\nERROR  -->  SCH : SchUeCb not found at MacSchAddUeConfigReq() ");
397       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_NOK, &cfgRsp);
398       return RFAILED;
399    }
400
401    /* Fill received Ue Configuration in UeCb */
402    memset(ueCb, 0, sizeof(SchUeCb));
403    GET_UE_IDX(ueCfg->crnti, ueIdx);
404    ueCb->ueIdx = ueIdx;
405    ueCb->crnti = ueCfg->crnti;
406    ueCb->state = SCH_UE_STATE_ACTIVE;
407    ret = fillSchUeCb(ueCb, ueCfg);
408    if(ret == ROK)
409    {
410       cellCb->numActvUe++;
411       SET_ONE_BIT(ueCb->ueIdx, cellCb->actvUeBitMap);
412       ueCb->cellCb = cellCb;
413       ueCb->srRcvd = false;
414       for(lcIdx=0; lcIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcIdx++)
415          ueCb->bsrInfo[lcIdx].dataVol = 0;
416
417       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
418    }
419    return ret;
420 }
421
422 /*******************************************************************
423 *
424 * @brief Fills PUSCH UL allocation
425 *
426 * @details
427 *
428 *    Function : schFillPuschAlloc
429 *
430 *    Functionality: fills PUSCH info
431 *
432 * @params[in]
433 * @return ROK     - success
434 *         RFAILED - failure
435 *
436 * ****************************************************************/
437 uint8_t schFillPuschAlloc(SchUeCb *ueCb, uint16_t pdcchSlot, uint32_t dataVol, SchPuschInfo *puschInfo)
438 {
439   uint16_t puschSlot      = 0;
440   uint16_t startRb        = 0;
441   uint8_t  numRb          = 0;
442   uint16_t tbSize         = 0;
443   uint8_t  buffer         = 5;
444   uint8_t  idx            = 0;
445   SchCellCb *cellCb       = ueCb->cellCb;
446   SchUlSlotInfo *schUlSlotInfo = NULLP;
447   uint8_t k2=0, startSymb=0 , symbLen=11;
448   
449   if(ueCb->ueCfg.spCellCfgPres == true)
450   {
451      k2 = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].k2;
452      startSymb = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].startSymbol;
453      symbLen = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].symbolLength;
454   }
455   puschSlot = (pdcchSlot + k2) % cellCb->numSlots;
456
457   startRb = cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb;
458   tbSize  = schCalcTbSize(dataVol + buffer); /*  2 bytes header + some buffer */
459   numRb   = schCalcNumPrb(tbSize, ueCb->ueCfg.ulModInfo.mcsIndex, symbLen);
460   /* increment PUSCH PRB */
461
462   cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb += numRb;
463
464   puschInfo->crnti             = ueCb->crnti; 
465   puschInfo->harqProcId        = SCH_HARQ_PROC_ID;
466   puschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
467   puschInfo->fdAlloc.startPrb  = startRb;
468   puschInfo->fdAlloc.numPrb    = numRb;
469   puschInfo->tdAlloc.startSymb = startSymb;
470   puschInfo->tdAlloc.numSymb   = symbLen;
471   puschInfo->tbInfo.qamOrder   = ueCb->ueCfg.ulModInfo.modOrder;
472   puschInfo->tbInfo.mcs        = ueCb->ueCfg.ulModInfo.mcsIndex;
473   puschInfo->tbInfo.mcsTable   = ueCb->ueCfg.ulModInfo.mcsTable;
474   puschInfo->tbInfo.ndi        = 1; /* new transmission */
475   puschInfo->tbInfo.rv         = 0;
476   puschInfo->tbInfo.tbSize     = tbSize;
477   puschInfo->dmrsMappingType   = DMRS_MAP_TYPE_A;  /* Setting Type-A */
478   puschInfo->nrOfDmrsSymbols   = NUM_DMRS_SYMBOLS;
479   puschInfo->dmrsAddPos        = DMRS_ADDITIONAL_POS;
480
481   /* Update pusch in cell */
482   for(idx=startSymb; idx<symbLen; idx++)
483   {
484      cellCb->schUlSlotInfo[puschSlot]->assignedPrb[idx] = startRb + numRb;
485   }
486
487   schUlSlotInfo = cellCb->schUlSlotInfo[puschSlot];
488
489   SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
490   if(!schUlSlotInfo->schPuschInfo)
491   {
492      DU_LOG("\nERROR  -->  SCH: Memory allocation failed in schAllocMsg3Pusch");
493      return RFAILED;
494   }
495   memcpy(schUlSlotInfo->schPuschInfo, puschInfo, sizeof(SchPuschInfo));
496
497   return ROK;
498 }
499
500 /*******************************************************************
501  *
502  * @brief Fills DCI for UL grant
503  *
504  * @details
505  *
506  *    Function : schFillUlDci
507  *
508  *    Functionality: fills DCI for UL grant in response to BSR
509  *
510  * @params[in]
511  * @return ROK     - success
512  *         RFAILED - failure
513  *
514  * ****************************************************************/
515 uint8_t schFillUlDci(SchUeCb *ueCb, SchPuschInfo puschInfo, DciInfo *dciInfo)
516 {
517    SchCellCb         *cellCb  = ueCb->cellCb;
518    SchControlRsrcSet coreset1 ;
519   
520    memset(&coreset1, 0, sizeof(SchControlRsrcSet));
521    if(ueCb->ueCfg.spCellCfgPres == true)
522    {
523      coreset1 = ueCb->ueCfg.spCellCfg.servCellCfg.initDlBwp.pdcchCfg.cRSetToAddModList[0];
524    }
525    
526    dciInfo->cellId = cellCb->cellId;
527    dciInfo->crnti  = ueCb->crnti;
528
529    /* fill bwp cfg */
530    dciInfo->bwpCfg.subcarrierSpacing  = cellCb->cellCfg.sib1SchCfg.bwp.subcarrierSpacing;
531    dciInfo->bwpCfg.cyclicPrefix       = cellCb->cellCfg.sib1SchCfg.bwp.cyclicPrefix;
532    dciInfo->bwpCfg.freqAlloc.startPrb = 0;
533    dciInfo->bwpCfg.freqAlloc.numPrb   = MAX_NUM_RB; /* whole of BW */
534
535    /*fill coreset cfg */
536    //Considering number of RBs in coreset1 is same as coreset0
537    dciInfo->coresetCfg.coreSetSize      = coresetIdxTable[0][1];
538    //Considering coreset1 also starts from same symbol as coreset0
539    dciInfo->coresetCfg.startSymbolIndex = searchSpaceIdxTable[0][3];
540    dciInfo->coresetCfg.durationSymbols  = coreset1.duration;
541    memcpy(dciInfo->coresetCfg.freqDomainResource, coreset1.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
542    
543    dciInfo->coresetCfg.cceRegMappingType   = coreset1.cceRegMappingType; /* non-interleaved */
544    dciInfo->coresetCfg.regBundleSize       = 6; /* must be 6 for non-interleaved */
545    dciInfo->coresetCfg.interleaverSize     = 0; /* NA for non-interleaved */
546    dciInfo->coresetCfg.coreSetType         = 1; /* non PBCH coreset */
547    dciInfo->coresetCfg.shiftIndex          = cellCb->cellCfg.phyCellId;
548    dciInfo->coresetCfg.precoderGranularity = coreset1.precoderGranularity;
549    dciInfo->coresetCfg.cceIndex            = 0; /* 0-3 for UL and 4-7 for DL */
550    dciInfo->coresetCfg.aggregationLevel    = 4; /* same as for sib1 */
551    
552    dciInfo->formatType = FORMAT0_0;
553    
554    /* fill UL grant */
555    dciInfo->format.format0_0.resourceAllocType   = puschInfo.resAllocType;
556    dciInfo->format.format0_0.freqAlloc.startPrb  = puschInfo.fdAlloc.startPrb;
557    dciInfo->format.format0_0.freqAlloc.numPrb    = puschInfo.fdAlloc.numPrb;
558    dciInfo->format.format0_0.timeAlloc.startSymb = puschInfo.tdAlloc.startSymb;
559    dciInfo->format.format0_0.timeAlloc.numSymb   = puschInfo.tdAlloc.numSymb;
560    dciInfo->format.format0_0.rowIndex            = 0; /* row Index */
561    dciInfo->format.format0_0.mcs                 = puschInfo.tbInfo.mcs;
562    dciInfo->format.format0_0.harqProcId          = puschInfo.harqProcId;
563    dciInfo->format.format0_0.puschHopFlag        = FALSE; /* disabled */
564    dciInfo->format.format0_0.freqHopFlag         = FALSE; /* disabled */
565    dciInfo->format.format0_0.ndi                 = puschInfo.tbInfo.ndi; /* new transmission */
566    dciInfo->format.format0_0.rv                  = puschInfo.tbInfo.rv;
567    dciInfo->format.format0_0.tpcCmd              = 0; //Sphoorthi TODO: check
568    dciInfo->format.format0_0.sUlCfgd             = FALSE; /* SUL not configured */
569    
570    /* Fill DCI Structure */
571    dciInfo->dciInfo.rnti                              = ueCb->crnti;
572    dciInfo->dciInfo.scramblingId                      = cellCb->cellCfg.phyCellId;
573    dciInfo->dciInfo.scramblingRnti                    = 0;
574    dciInfo->dciInfo.cceIndex                          = 0; /* 0-3 for UL and 4-7 for DL */
575    dciInfo->dciInfo.aggregLevel                       = 4;
576    dciInfo->dciInfo.beamPdcchInfo.numPrgs             = 1;
577    dciInfo->dciInfo.beamPdcchInfo.prgSize             = 1;
578    dciInfo->dciInfo.beamPdcchInfo.digBfInterfaces     = 0;
579    dciInfo->dciInfo.beamPdcchInfo.prg[0].pmIdx        = 0;
580    dciInfo->dciInfo.beamPdcchInfo.prg[0].beamIdx[0]   = 0;
581    dciInfo->dciInfo.txPdcchPower.powerValue           = 0;
582    dciInfo->dciInfo.txPdcchPower.powerControlOffsetSS = 0;
583    dciInfo->dciInfo.pdschCfg                          = NULLP; /* No DL data being sent */
584
585    return ROK;
586 }
587
588 /*******************************************************************
589  *
590  * @brief Function to Modify Ue Config request from MAC
591  *
592  * @details
593  *
594  *    Function : MacSchModUeConfigReq
595  *
596  *    Functionality: Function to modify Ue Config request from MAC
597  *
598  * @params[in] 
599  * @return ROK     - success
600  *         RFAILED - failure
601  *
602  * ****************************************************************/
603 uint8_t MacSchModUeConfigReq(Pst *pst, SchUeCfg *ueCfg)
604 {
605    uint8_t ueIdx, lcIdx, ret = ROK;
606    SchCellCb    *cellCb = NULLP;
607    SchUeCb      *ueCb = NULLP;
608    SchUeCfgRsp  cfgRsp;
609    Inst         inst = pst->dstInst - 1;
610    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
611   
612 #ifdef CALL_FLOW_DEBUG_LOG
613    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_MODIFY_UE_CONFIG_REQ_TO_SCH\n");
614 #endif
615
616    if(!ueCfg)
617    {
618       DU_LOG("\nERROR  -->  SCH : Modifying Ue Config request failed at MacSchModUeConfigReq()");
619       return RFAILED;
620    }
621    DU_LOG("\nDEBUG  -->  SCH : Modifying Ue Config Request for CRNTI[%d]", ueCfg->crnti);
622    cellCb = getSchCellCb(pst->event, inst, ueCfg);
623
624    /* Search if UE already configured */
625    GET_UE_IDX(ueCfg->crnti, ueIdx);
626    ueCb = &cellCb->ueCb[ueIdx -1];
627    
628    if(!ueCb)
629    {
630       DU_LOG("\nERROR  -->  SCH : SchUeCb not found at MacSchModUeConfigReq() ");
631       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_NOK, &cfgRsp);
632       return RFAILED;
633    }
634    if((ueCb->crnti == ueCfg->crnti) && (ueCb->state == SCH_UE_STATE_ACTIVE))
635    {
636       /* Found the UeCb to Reconfig */
637       ret = fillSchUeCb(ueCb, ueCfg);
638       if(ret == ROK)
639       {
640          ueCb->cellCb = cellCb;
641          ueCb->srRcvd = false;
642          for(lcIdx=0; lcIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcIdx++)
643             ueCb->bsrInfo[lcIdx].dataVol = 0;
644
645          SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
646       }
647    }
648    return ret;
649 }
650
651 /*******************************************************************
652 *
653 * @brief Fill and send UE delete response to MAC
654 *
655 * @details
656 *
657 *    Function :  SchSendUeDeleteRspToMac
658 *
659 *    Functionality: Fill and send UE delete response to MAC
660 *
661 * @params[in] Inst inst, SchUeDelete  *ueDelete, SchMacRsp result, 
662 *              ErrorCause cause
663 * @return ROK     - success
664 *         RFAILED - failure
665 *
666 * ****************************************************************/
667 void SchSendUeDeleteRspToMac(Inst inst, SchUeDelete  *ueDelete, SchMacRsp result, ErrorCause cause)
668 {
669     Pst rspPst;
670     SchUeDeleteRsp  delRsp;
671     
672     memset(&delRsp, 0, sizeof(SchUeDeleteRsp));
673     delRsp.cellId = ueDelete->cellId;
674     delRsp.crnti = ueDelete->crnti;
675     delRsp.rsp = result; 
676     delRsp.cause = cause; 
677
678     /* Filling response post */
679     memset(&rspPst, 0, sizeof(Pst));
680     FILL_PST_SCH_TO_MAC(rspPst, inst);
681     rspPst.event = EVENT_UE_DELETE_RSP_TO_MAC;
682     SchUeDeleteRspOpts[rspPst.selector](&rspPst, &delRsp);
683 }
684
685 /*******************************************************************
686 *
687 * @brief Function to delete Sch Pucch ResrcCfg
688 *
689 * @details
690 *
691 *    Function : deleteSchPucchResrcCfg 
692 *
693 *    Functionality: Function to delete Sch Pucch ResrcCfg
694 *
695 * @params[in] SchPucchResrcCfg *resrc
696 * @return void 
697 *
698 * ****************************************************************/
699
700 void deleteSchPucchResrcCfg(SchPucchResrcCfg *resrc)
701 {
702    uint8_t rsrcIdx=0;
703    for(rsrcIdx=0; rsrcIdx < resrc->resrcToAddModListCount; rsrcIdx++)
704    {
705       switch(resrc->resrcToAddModList[rsrcIdx].pucchFormat)
706       {
707          case PUCCH_FORMAT_0:
708          {
709             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format0,\
710             sizeof(SchPucchFormat0));
711             break;
712          }
713          case PUCCH_FORMAT_1:
714          {
715             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format1,\
716             sizeof(SchPucchFormat1));
717             break;
718          }
719          case PUCCH_FORMAT_2:
720          {
721             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format2,\
722             sizeof(SchPucchFormat2_3));
723             break;
724          }
725          case PUCCH_FORMAT_3:
726          {
727             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format3,\
728             sizeof(SchPucchFormat2_3));
729             break;
730          }
731          case PUCCH_FORMAT_4:
732          {
733             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format4,\
734             sizeof(SchPucchFormat4));
735             break;
736          }
737       }
738    }
739 }
740
741 /*******************************************************************
742 *
743 * @brief Function to delete SCH Pdsch ServCellCfg
744 *
745 * @details
746 *
747 *    Function : deleteSchPdschServCellCfg
748 *
749 *    Functionality: Function to delete SCH Pdsch ServCellCfg
750 *
751 * @params[in] SchPdschServCellCfg *pdschServCellCfg
752 * @return void 
753 *
754 * ****************************************************************/
755
756 void deleteSchPdschServCellCfg(SchPdschServCellCfg *pdschServCellCfg)
757 {
758    SCH_FREE(pdschServCellCfg->maxMimoLayers, sizeof(uint8_t));
759    SCH_FREE(pdschServCellCfg->maxCodeBlkGrpPerTb, sizeof(SchMaxCodeBlkGrpPerTB));
760    SCH_FREE(pdschServCellCfg->codeBlkGrpFlushInd, sizeof(bool));
761    SCH_FREE(pdschServCellCfg->xOverhead, sizeof(SchPdschXOverhead));
762 }
763
764 /*******************************************************************
765 *
766 * @brief Function to  delete SCH UeCb
767 *
768 * @details
769 *
770 *    Function : deleteSchUeCb 
771 *
772 *    Functionality: Function to delete SCH UeCb
773 *
774 * @params[in]
775 * @return ROK     - success
776 *         RFAILED - failure
777 *
778 * ****************************************************************/
779 void deleteSchUeCb(SchUeCb *ueCb) 
780 {
781    uint8_t timeDomRsrcIdx;
782    SchPucchCfg *pucchCfg = NULLP;
783    SchPdschConfig *pdschCfg = NULLP;
784
785    if(ueCb)
786    {
787       SCH_FREE(ueCb->ueCfg.ambrCfg, sizeof(SchAmbrCfg));
788       if(ueCb->ueCfg.spCellCfgPres)
789       {
790          if(ueCb->ueCfg.spCellCfg.servCellCfg.initDlBwp.pdschCfgPres == true)
791          {
792             pdschCfg = &ueCb->ueCfg.spCellCfg.servCellCfg.initDlBwp.pdschCfg;
793             for(timeDomRsrcIdx = 0; timeDomRsrcIdx < pdschCfg->numTimeDomRsrcAlloc; timeDomRsrcIdx++)
794                SCH_FREE(pdschCfg->timeDomRsrcAllociList[timeDomRsrcIdx].k0, sizeof(uint8_t));
795          }
796
797          if(ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfgPres == true)
798          {
799             pucchCfg = &ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfg;
800             SCH_FREE(pucchCfg->resrcSet,sizeof(SchPucchResrcSetCfg));
801             if(pucchCfg->resrc)
802             {
803                deleteSchPucchResrcCfg(pucchCfg->resrc);
804                SCH_FREE(pucchCfg->resrc, sizeof(SchPucchResrcCfg));
805             }
806             SCH_FREE(pucchCfg->format1, sizeof(SchPucchFormatCfg));
807             SCH_FREE(pucchCfg->format2, sizeof(SchPucchFormatCfg));
808             SCH_FREE(pucchCfg->format3, sizeof(SchPucchFormatCfg));
809             SCH_FREE(pucchCfg->format4, sizeof(SchPucchFormatCfg));
810             SCH_FREE(pucchCfg->schedReq, sizeof(SchPucchSchedReqCfg));
811             SCH_FREE(pucchCfg->multiCsiCfg, sizeof(SchPucchMultiCsiCfg));
812             SCH_FREE(pucchCfg->spatialInfo, sizeof(SchPucchSpatialCfg));  
813             SCH_FREE(pucchCfg->dlDataToUlAck, sizeof(SchPucchDlDataToUlAck));
814             SCH_FREE(pucchCfg->powerControl,sizeof(SchPucchPowerControl));
815          }
816          SCH_FREE(ueCb->ueCfg.spCellCfg.servCellCfg.bwpInactivityTmr, sizeof(uint8_t));
817          deleteSchPdschServCellCfg(&ueCb->ueCfg.spCellCfg.servCellCfg.pdschServCellCfg);
818       }
819       memset(ueCb, 0, sizeof(SchUeCb));
820    }
821 }
822
823 /*******************************************************************
824 *
825 * @brief Function for Ue Delete request from MAC to SCH
826 *
827 * @details
828 *
829 *    Function : MacSchUeDeleteReq 
830 *
831 *    Functionality: Function for Ue Delete request from MAC to SCH
832 *
833 * @params[in] Pst *pst, SchUeDelete  *ueDelete
834 * @return ROK     - success
835 *         RFAILED - failure
836 *
837 * ****************************************************************/
838 uint8_t MacSchUeDeleteReq(Pst *pst, SchUeDelete  *ueDelete)
839 {
840     uint8_t idx=0, ueIdx=0, ret=ROK;
841     ErrorCause result;
842     SchCellCb    *cellCb = NULLP;
843     Inst         inst = pst->dstInst - 1;
844    
845 #ifdef CALL_FLOW_DEBUG_LOG
846     DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_UE_DELETE_REQ_TO_SCH\n");
847 #endif
848
849     if(!ueDelete)
850     {
851        DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): Ue Delete request failed");
852        ret = RFAILED;
853     }
854     DU_LOG("\nDEBUG  -->  SCH : Ue Delete request received for crnti[%d]", ueDelete->crnti);
855     
856     cellCb = schCb[inst].cells[idx];
857
858     if(cellCb->cellId != ueDelete->cellId)
859     {
860        DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): cell Id is not available");
861        result =  INVALID_CELLID;
862     }
863     else
864     {
865        GET_UE_IDX(ueDelete->crnti, ueIdx);
866        if(( cellCb->ueCb[ueIdx-1].crnti == ueDelete->crnti) && ( cellCb->ueCb[ueIdx-1].state == SCH_UE_STATE_ACTIVE))
867        {
868           deleteSchUeCb(&cellCb->ueCb[ueIdx-1]);
869           cellCb->numActvUe--;
870           result = NOT_APPLICABLE;
871        }
872        else
873        {
874           DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): SchUeCb not found");
875           result =  INVALID_UEIDX;
876        }
877     }
878     
879     if(result == NOT_APPLICABLE)
880     {
881        SchSendUeDeleteRspToMac(inst, ueDelete, RSP_OK, result);
882     }
883     else
884     {
885        SchSendUeDeleteRspToMac(inst, ueDelete, RSP_NOK, result);
886        ret = RFAILED;
887     }
888     return ret;
889 }
890
891 /*******************************************************************
892  *
893  * @brief Fill and send Cell delete response to MAC
894  *
895  * @details
896  *
897  *    Function :  SchSendCellDeleteRspToMac
898  *
899  *    Functionality: Fill and send Cell delete response to MAC
900  *
901  * @params[in] SchCellDelete  *ueDelete, Inst inst, SchMacRsp result
902  * @return ROK     - success
903  *         RFAILED - failure
904  *
905  * ****************************************************************/
906 uint8_t SchSendCellDeleteRspToMac(SchCellDelete  *ueDelete, Inst inst, SchMacRsp result)
907 {
908    Pst rspPst;
909    uint8_t ret=0;
910    
911    SchCellDeleteRsp  delRsp;
912
913    DU_LOG("\nINFO   --> SCH : Filling Cell Delete response");
914    memset(&delRsp, 0, sizeof(SchCellDeleteRsp));
915    delRsp.cellId = ueDelete->cellId;
916    delRsp.rsp = result;
917
918    /* Filling response post */
919    memset(&rspPst, 0, sizeof(Pst));
920    FILL_PST_SCH_TO_MAC(rspPst, inst);
921    rspPst.event = EVENT_CELL_DELETE_RSP_TO_MAC;
922    ret =  SchCellDeleteRspOpts[rspPst.selector](&rspPst, &delRsp);
923    if(ret == RFAILED)
924    {
925       DU_LOG("\nERROR  -->  SCH : SchSendCellDeleteRspToMac(): failed to send the Cell Delete response");
926       return ret;
927    }
928    return ret;
929 }
930
931 /*******************************************************************
932  *
933  * @brief Function for cellCb Deletion 
934  *
935  * @details
936  *
937  *    Function : deleteSchCellCb 
938  *
939  *    Functionality: Function for cellCb Deletion 
940  *
941  * @params[in] SchCellDelete  *cellDelete
942  * @return ROK     - success
943  *         RFAILED - failure
944  *
945  * ****************************************************************/
946 void deleteSchCellCb(SchCellCb *cellCb)
947 {
948    uint8_t idx=0;
949    if(cellCb->schDlSlotInfo)
950    {
951       for(idx=0; idx<cellCb->numSlots; idx++)
952       {
953          if(cellCb->schDlSlotInfo[idx])
954          {
955             SCH_FREE(cellCb->schDlSlotInfo[idx], sizeof(SchDlSlotInfo));
956          }
957       }
958       SCH_FREE(cellCb->schDlSlotInfo, cellCb->numSlots *sizeof(SchDlSlotInfo*));
959    }
960    if(cellCb->schUlSlotInfo)
961    {
962       for(idx=0; idx<cellCb->numSlots; idx++)
963       {
964          if(cellCb->schUlSlotInfo[idx])
965          {
966             SCH_FREE(cellCb->schUlSlotInfo[idx], sizeof(SchUlSlotInfo));  
967          }
968       }
969       SCH_FREE(cellCb->schUlSlotInfo,  cellCb->numSlots * sizeof(SchUlSlotInfo*));
970    }
971    memset(cellCb, 0, sizeof(SchCellCb));
972
973 }
974
975 /*******************************************************************
976  *
977  * @brief Function for cell Delete request from MAC to SCH
978  *
979  * @details
980  *
981  *    Function : MacSchCellDeleteReq
982  *
983  *    Functionality: Function for cell Delete request from MAC to SCH
984  *
985  * @params[in] Pst *pst, SchCellDelete  *cellDelete
986  * @return ROK     - success
987  *         RFAILED - failure
988  *
989  * ****************************************************************/
990
991 uint8_t MacSchCellDeleteReq(Pst *pst, SchCellDelete  *cellDelete)
992 {
993    uint8_t   cellIdx=0, ret = RFAILED;
994    Inst      inst = pst->dstInst - 1;
995    SchMacRsp result= RSP_OK;
996    
997 #ifdef CALL_FLOW_DEBUG_LOG
998    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_CELL_DELETE_REQ_TO_SCH\n");
999 #endif   
1000
1001    if(!cellDelete)
1002    {
1003       DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): Ue Delete request failed");
1004    }
1005    else
1006    {
1007       GET_CELL_IDX(cellDelete->cellId, cellIdx);
1008       if(schCb[inst].cells[cellIdx] == NULLP)
1009       { 
1010          DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): cell Id[%d] is not available", cellDelete->cellId);
1011          result = RSP_NOK;
1012       }
1013       else
1014       {
1015          if(schCb[inst].cells[cellIdx]->cellId == cellDelete->cellId)
1016          {
1017             deleteSchCellCb(schCb[inst].cells[cellIdx]);
1018             result = RSP_OK;
1019             ret = ROK;
1020             SCH_FREE(schCb[inst].cells[cellIdx], sizeof(SchCellCb));
1021             DU_LOG("\nINFO   -->  SCH : Sending Cell Delete response to MAC");
1022          }
1023          else
1024          {
1025             DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): cell Id[%d] is not available",cellDelete->cellId);
1026             result = RSP_NOK;
1027          }
1028       }
1029
1030       if(SchSendCellDeleteRspToMac(cellDelete, inst, result)!=ROK)
1031       {
1032          DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): failed to send Cell Delete response");
1033          ret =  RFAILED;
1034       }
1035
1036    }
1037    return ret;   
1038 }
1039
1040 /**********************************************************************
1041   End of file
1042  **********************************************************************/