New Fixes for cell bring up post cell down for CLA
[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   
217    ueCb->ueCfg.cellId = ueCfg->cellId;
218    ueCb->ueCfg.crnti = ueCfg->crnti;
219    if(ueCfg->macCellGrpCfgPres == true)
220    {
221       memcpy(&ueCb->ueCfg.macCellGrpCfg , &ueCfg->macCellGrpCfg, sizeof(SchMacCellGrpCfg)); 
222       ueCb->ueCfg.macCellGrpCfgPres = true;
223    }
224
225    if(ueCfg->phyCellGrpCfgPres == true)
226    {
227       memcpy(&ueCb->ueCfg.phyCellGrpCfg ,  &ueCfg->phyCellGrpCfg, sizeof(SchPhyCellGrpCfg));
228       ueCb->ueCfg.phyCellGrpCfgPres = true;
229    }
230
231    if(ueCfg->spCellCfgPres == true)
232    {
233       memcpy(&ueCb->ueCfg.spCellCfg , &ueCfg->spCellCfg, sizeof(SchSpCellCfg));
234       ueCb->ueCfg.spCellCfgPres = true;
235    }
236    ueCb->state = SCH_UE_STATE_ACTIVE;
237    
238    ueCb->ueCfg.ambrCfg =  ueCfg->ambrCfg;
239    memcpy(&ueCb->ueCfg.dlModInfo,  &ueCfg->dlModInfo , sizeof(SchModulationInfo));
240    memcpy(&ueCb->ueCfg.ulModInfo,  &ueCfg->ulModInfo , sizeof(SchModulationInfo));
241
242    //Updating SchUlCb and SchDlCb DB in SchUeCb
243    for(lcIdx = 0; lcIdx < ueCfg->numLcs; lcIdx++)
244    {
245       if(ueCfg->schLcCfg[lcIdx].configType == CONFIG_ADD)
246       {
247          fillSchUlLcCtxt(&ueCb->ulInfo.ulLcCtxt[ueCb->ulInfo.numUlLc], &ueCfg->schLcCfg[lcIdx]);
248          ueCb->ulInfo.numUlLc++;
249          fillSchDlLcCtxt(&ueCb->dlInfo.dlLcCtxt[ueCb->dlInfo.numDlLc], &ueCfg->schLcCfg[lcIdx]);
250          ueCb->dlInfo.numDlLc++;
251       }
252       else
253       {
254          for(ueLcIdx = 0; ueLcIdx < ueCb->ulInfo.numUlLc; ueLcIdx++) //searching for Lc to be Mod
255          {
256             if(ueCb->ulInfo.ulLcCtxt[ueLcIdx].lcId == ueCfg->schLcCfg[lcIdx].lcId)
257             {
258                if(ueCfg->schLcCfg[lcIdx].configType == CONFIG_MOD)
259                {
260                   fillSchUlLcCtxt(&ueCb->ulInfo.ulLcCtxt[ueLcIdx], &ueCfg->schLcCfg[lcIdx]);
261                   fillSchDlLcCtxt(&ueCb->dlInfo.dlLcCtxt[ueLcIdx], &ueCfg->schLcCfg[lcIdx]);
262                   break;
263                }
264                if(ueCfg->schLcCfg[ueLcIdx].configType == CONFIG_DEL)
265                {
266                   memset(&ueCb->ulInfo.ulLcCtxt[ueLcIdx], 0, sizeof(SchUlLcCtxt));
267                   ueCb->ulInfo.numUlLc--;
268                   updateSchUlCb(ueLcIdx, &ueCb->ulInfo); //moving arr elements one idx ahead 
269                   memset(&ueCb->dlInfo.dlLcCtxt[ueLcIdx], 0, sizeof(SchDlLcCtxt));
270                   ueCb->dlInfo.numDlLc--;
271                   updateSchDlCb(ueLcIdx, &ueCb->dlInfo); //moving arr elements one idx ahead
272                   break;
273                }
274             }
275          }/*End of inner for loop */
276       }
277    }/* End of outer for loop */
278    return ROK;
279 }
280
281 /*******************************************************************
282  *
283  * @brief Function to get SCH Cell Cb
284  *
285  * @details
286  *
287  *    Function : getSchCellCb
288  *
289  *    Functionality: Function to get SCH Cell Cb
290  *
291  * @params[in] event, SchUeCfg pointer 
292  * @return schUeCb pointer  - success
293  *         NULLP - failure
294  *
295  * ****************************************************************/
296
297 SchCellCb *getSchCellCb(uint16_t srcEvent, Inst inst, SchUeCfg *ueCfg)
298 {
299    uint8_t      idx;
300    SchCellCb    *cellCb = NULLP;
301    SchUeCfgRsp  cfgRsp;
302    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
303
304    /* Search of cell cb */
305    for(idx = 0; idx < MAX_NUM_CELL; idx++)
306    {
307       cellCb = schCb[inst].cells[idx];
308       if(cellCb->cellId == ueCfg->cellId)
309          break;
310    }
311    if(idx == MAX_NUM_CELL)
312    {
313       DU_LOG("\nERROR  -->  SCH : Ue create request failed. Invalid cell id %d", ueCfg->cellId);
314       SchSendUeCfgRspToMac(srcEvent, ueCfg, inst, RSP_NOK, &cfgRsp);
315       return NULLP;
316    }
317
318    /* Check if max number of UE configured */
319    if(cellCb->numActvUe > MAX_NUM_UE)
320    {
321       DU_LOG("\nERROR  -->  SCH :  Max number of UE [%d] already configured", MAX_NUM_UE);
322       SchSendUeCfgRspToMac(srcEvent, ueCfg, inst, RSP_NOK, &cfgRsp);
323       return NULLP;
324    }
325    return cellCb;
326 }
327
328
329 /*******************************************************************
330  *
331  * @brief Function to Add Ue Config Request from MAC
332  *
333  * @details
334  *
335  *    Function : MacSchAddUeConfigReq
336  *
337  *    Functionality: Function to Add Ue config request from MAC
338  *
339  * @params[in] 
340  * @return ROK     - success
341  *         RFAILED - failure
342  *
343  * ****************************************************************/
344 uint8_t MacSchAddUeConfigReq(Pst *pst, SchUeCfg *ueCfg)
345 {
346    uint8_t ueIdx, lcIdx, ret = ROK;
347    SchCellCb    *cellCb = NULLP;
348    SchUeCb      *ueCb = NULLP;
349    SchUeCfgRsp  cfgRsp;
350    Inst         inst = pst->dstInst - 1;
351    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
352
353    if(!ueCfg)
354    {
355       DU_LOG("\nERROR  -->  SCH :  Adding UE Config Request failed at MacSchAddUeConfigReq()");
356       return RFAILED;
357    }
358    DU_LOG("\nDEBUG  -->  SCH :  Adding UE Config Request for CRNTI[%d]", ueCfg->crnti);
359    cellCb = getSchCellCb(pst->event, inst, ueCfg);
360
361    /* Search if UE already configured */
362    GET_UE_IDX(ueCfg->crnti, ueIdx);
363    ueCb = &cellCb->ueCb[ueIdx -1];
364    if(ueCb)
365    {
366       if((ueCb->crnti == ueCfg->crnti) && (ueCb->state == SCH_UE_STATE_ACTIVE))
367       {
368          DU_LOG("\nDEBUG  -->  SCH : CRNTI %d already configured ", ueCfg->crnti);
369          SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
370          return ROK;
371       }
372    }
373    else
374    {
375       DU_LOG("\nERROR  -->  SCH : SchUeCb not found at MacSchAddUeConfigReq() ");
376       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_NOK, &cfgRsp);
377       return RFAILED;
378    }
379
380    /* Fill received Ue Configuration in UeCb */
381    memset(ueCb, 0, sizeof(SchUeCb));
382    GET_UE_IDX(ueCfg->crnti, ueIdx);
383    ueCb->ueIdx = ueIdx;
384    ueCb->crnti = ueCfg->crnti;
385    ueCb->state = SCH_UE_STATE_ACTIVE;
386    ret = fillSchUeCb(ueCb, ueCfg);
387    if(ret == ROK)
388    {
389       cellCb->numActvUe++;
390       SET_ONE_BIT(ueCb->ueIdx, cellCb->actvUeBitMap);
391       ueCb->cellCb = cellCb;
392       ueCb->srRcvd = false;
393       for(lcIdx=0; lcIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcIdx++)
394          ueCb->bsrInfo[lcIdx].dataVol = 0;
395
396       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
397    }
398    return ret;
399 }
400
401 /*******************************************************************
402 *
403 * @brief Fills PUSCH UL allocation
404 *
405 * @details
406 *
407 *    Function : schFillPuschAlloc
408 *
409 *    Functionality: fills PUSCH info
410 *
411 * @params[in]
412 * @return ROK     - success
413 *         RFAILED - failure
414 *
415 * ****************************************************************/
416 uint8_t schFillPuschAlloc(SchUeCb *ueCb, uint16_t pdcchSlot, uint32_t dataVol, SchPuschInfo *puschInfo)
417 {
418   uint16_t puschSlot      = 0;
419   uint16_t startRb        = 0;
420   uint8_t  numRb          = 0;
421   uint16_t tbSize         = 0;
422   uint8_t  buffer         = 5;
423   uint8_t  idx            = 0;
424   SchCellCb *cellCb       = ueCb->cellCb;
425   SchUlSlotInfo *schUlSlotInfo = NULLP;
426   uint8_t k2=0, startSymb=0 , symbLen=11;
427   
428   if(ueCb->ueCfg.spCellCfgPres == true)
429   {
430      k2 = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].k2;
431      startSymb = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].startSymbol;
432      symbLen = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].symbolLength;
433   }
434   puschSlot = (pdcchSlot + k2) % cellCb->numSlots;
435
436   startRb = cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb;
437   tbSize  = schCalcTbSize(dataVol + buffer); /*  2 bytes header + some buffer */
438   numRb   = schCalcNumPrb(tbSize, ueCb->ueCfg.ulModInfo.mcsIndex, symbLen);
439   /* increment PUSCH PRB */
440
441   cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb += numRb;
442
443   puschInfo->crnti             = ueCb->crnti; 
444   puschInfo->harqProcId        = SCH_HARQ_PROC_ID;
445   puschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
446   puschInfo->fdAlloc.startPrb  = startRb;
447   puschInfo->fdAlloc.numPrb    = numRb;
448   puschInfo->tdAlloc.startSymb = startSymb;
449   puschInfo->tdAlloc.numSymb   = symbLen;
450   puschInfo->tbInfo.qamOrder   = ueCb->ueCfg.ulModInfo.modOrder;
451   puschInfo->tbInfo.mcs        = ueCb->ueCfg.ulModInfo.mcsIndex;
452   puschInfo->tbInfo.mcsTable   = ueCb->ueCfg.ulModInfo.mcsTable;
453   puschInfo->tbInfo.ndi        = 1; /* new transmission */
454   puschInfo->tbInfo.rv         = 0;
455   puschInfo->tbInfo.tbSize     = tbSize;
456   puschInfo->dmrsMappingType   = DMRS_MAP_TYPE_A;  /* Setting Type-A */
457   puschInfo->nrOfDmrsSymbols   = NUM_DMRS_SYMBOLS;
458   puschInfo->dmrsAddPos        = DMRS_ADDITIONAL_POS;
459
460   /* Update pusch in cell */
461   for(idx=startSymb; idx<symbLen; idx++)
462   {
463      cellCb->schUlSlotInfo[puschSlot]->assignedPrb[idx] = startRb + numRb;
464   }
465
466   schUlSlotInfo = cellCb->schUlSlotInfo[puschSlot];
467
468   SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
469   if(!schUlSlotInfo->schPuschInfo)
470   {
471      DU_LOG("\nERROR  -->  SCH: Memory allocation failed in schAllocMsg3Pusch");
472      return RFAILED;
473   }
474   memcpy(schUlSlotInfo->schPuschInfo, puschInfo, sizeof(SchPuschInfo));
475
476   return ROK;
477 }
478
479 /*******************************************************************
480  *
481  * @brief Fills DCI for UL grant
482  *
483  * @details
484  *
485  *    Function : schFillUlDci
486  *
487  *    Functionality: fills DCI for UL grant in response to BSR
488  *
489  * @params[in]
490  * @return ROK     - success
491  *         RFAILED - failure
492  *
493  * ****************************************************************/
494 uint8_t schFillUlDci(SchUeCb *ueCb, SchPuschInfo puschInfo, DciInfo *dciInfo)
495 {
496    SchCellCb         *cellCb  = ueCb->cellCb;
497    SchControlRsrcSet coreset1 ;
498   
499    memset(&coreset1, 0, sizeof(SchControlRsrcSet));
500    if(ueCb->ueCfg.spCellCfgPres == true)
501    {
502      coreset1 = ueCb->ueCfg.spCellCfg.servCellCfg.initDlBwp.pdcchCfg.cRSetToAddModList[0];
503    }
504    
505    dciInfo->cellId = cellCb->cellId;
506    dciInfo->crnti  = ueCb->crnti;
507
508    /* fill bwp cfg */
509    dciInfo->bwpCfg.subcarrierSpacing  = cellCb->cellCfg.sib1SchCfg.bwp.subcarrierSpacing;
510    dciInfo->bwpCfg.cyclicPrefix       = cellCb->cellCfg.sib1SchCfg.bwp.cyclicPrefix;
511    dciInfo->bwpCfg.freqAlloc.startPrb = 0;
512    dciInfo->bwpCfg.freqAlloc.numPrb   = MAX_NUM_RB; /* whole of BW */
513
514    /*fill coreset cfg */
515    //Considering number of RBs in coreset1 is same as coreset0
516    dciInfo->coresetCfg.coreSetSize      = coresetIdxTable[0][1];
517    //Considering coreset1 also starts from same symbol as coreset0
518    dciInfo->coresetCfg.startSymbolIndex = searchSpaceIdxTable[0][3];
519    dciInfo->coresetCfg.durationSymbols  = coreset1.duration;
520    memcpy(dciInfo->coresetCfg.freqDomainResource, coreset1.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
521    
522    dciInfo->coresetCfg.cceRegMappingType   = coreset1.cceRegMappingType; /* non-interleaved */
523    dciInfo->coresetCfg.regBundleSize       = 6; /* must be 6 for non-interleaved */
524    dciInfo->coresetCfg.interleaverSize     = 0; /* NA for non-interleaved */
525    dciInfo->coresetCfg.coreSetType         = 1; /* non PBCH coreset */
526    dciInfo->coresetCfg.shiftIndex          = cellCb->cellCfg.phyCellId;
527    dciInfo->coresetCfg.precoderGranularity = coreset1.precoderGranularity;
528    dciInfo->coresetCfg.cceIndex            = 0; /* 0-3 for UL and 4-7 for DL */
529    dciInfo->coresetCfg.aggregationLevel    = 4; /* same as for sib1 */
530    
531    dciInfo->formatType = FORMAT0_0;
532    
533    /* fill UL grant */
534    dciInfo->format.format0_0.resourceAllocType   = puschInfo.resAllocType;
535    dciInfo->format.format0_0.freqAlloc.startPrb  = puschInfo.fdAlloc.startPrb;
536    dciInfo->format.format0_0.freqAlloc.numPrb    = puschInfo.fdAlloc.numPrb;
537    dciInfo->format.format0_0.timeAlloc.startSymb = puschInfo.tdAlloc.startSymb;
538    dciInfo->format.format0_0.timeAlloc.numSymb   = puschInfo.tdAlloc.numSymb;
539    dciInfo->format.format0_0.rowIndex            = 0; /* row Index */
540    dciInfo->format.format0_0.mcs                 = puschInfo.tbInfo.mcs;
541    dciInfo->format.format0_0.harqProcId          = puschInfo.harqProcId;
542    dciInfo->format.format0_0.puschHopFlag        = FALSE; /* disabled */
543    dciInfo->format.format0_0.freqHopFlag         = FALSE; /* disabled */
544    dciInfo->format.format0_0.ndi                 = puschInfo.tbInfo.ndi; /* new transmission */
545    dciInfo->format.format0_0.rv                  = puschInfo.tbInfo.rv;
546    dciInfo->format.format0_0.tpcCmd              = 0; //Sphoorthi TODO: check
547    dciInfo->format.format0_0.sUlCfgd             = FALSE; /* SUL not configured */
548    
549    /* Fill DCI Structure */
550    dciInfo->dciInfo.rnti                              = ueCb->crnti;
551    dciInfo->dciInfo.scramblingId                      = cellCb->cellCfg.phyCellId;
552    dciInfo->dciInfo.scramblingRnti                    = 0;
553    dciInfo->dciInfo.cceIndex                          = 0; /* 0-3 for UL and 4-7 for DL */
554    dciInfo->dciInfo.aggregLevel                       = 4;
555    dciInfo->dciInfo.beamPdcchInfo.numPrgs             = 1;
556    dciInfo->dciInfo.beamPdcchInfo.prgSize             = 1;
557    dciInfo->dciInfo.beamPdcchInfo.digBfInterfaces     = 0;
558    dciInfo->dciInfo.beamPdcchInfo.prg[0].pmIdx        = 0;
559    dciInfo->dciInfo.beamPdcchInfo.prg[0].beamIdx[0]   = 0;
560    dciInfo->dciInfo.txPdcchPower.powerValue           = 0;
561    dciInfo->dciInfo.txPdcchPower.powerControlOffsetSS = 0;
562    dciInfo->dciInfo.pdschCfg                          = NULLP; /* No DL data being sent */
563
564    return ROK;
565 }
566
567 /*******************************************************************
568  *
569  * @brief Function to Modify Ue Config request from MAC
570  *
571  * @details
572  *
573  *    Function : MacSchModUeConfigReq
574  *
575  *    Functionality: Function to modify Ue Config request from MAC
576  *
577  * @params[in] 
578  * @return ROK     - success
579  *         RFAILED - failure
580  *
581  * ****************************************************************/
582 uint8_t MacSchModUeConfigReq(Pst *pst, SchUeCfg *ueCfg)
583 {
584    uint8_t ueIdx, lcIdx, ret = ROK;
585    SchCellCb    *cellCb = NULLP;
586    SchUeCb      *ueCb = NULLP;
587    SchUeCfgRsp  cfgRsp;
588    Inst         inst = pst->dstInst - 1;
589    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
590
591    if(!ueCfg)
592    {
593       DU_LOG("\nERROR  -->  SCH : Modifying Ue Config request failed at MacSchModUeConfigReq()");
594       return RFAILED;
595    }
596    DU_LOG("\nDEBUG  -->  SCH : Modifying Ue Config Request for CRNTI[%d]", ueCfg->crnti);
597    cellCb = getSchCellCb(pst->event, inst, ueCfg);
598
599    /* Search if UE already configured */
600    GET_UE_IDX(ueCfg->crnti, ueIdx);
601    ueCb = &cellCb->ueCb[ueIdx -1];
602    
603    if(!ueCb)
604    {
605       DU_LOG("\nERROR  -->  SCH : SchUeCb not found at MacSchModUeConfigReq() ");
606       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_NOK, &cfgRsp);
607       return RFAILED;
608    }
609    if((ueCb->crnti == ueCfg->crnti) && (ueCb->state == SCH_UE_STATE_ACTIVE))
610    {
611       /* Found the UeCb to Reconfig */
612       ret = fillSchUeCb(ueCb, ueCfg);
613       if(ret == ROK)
614       {
615          ueCb->cellCb = cellCb;
616          ueCb->srRcvd = false;
617          for(lcIdx=0; lcIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcIdx++)
618             ueCb->bsrInfo[lcIdx].dataVol = 0;
619
620          SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
621       }
622    }
623    return ret;
624 }
625
626 /*******************************************************************
627 *
628 * @brief Fill and send UE delete response to MAC
629 *
630 * @details
631 *
632 *    Function :  SchSendUeDeleteRspToMac
633 *
634 *    Functionality: Fill and send UE delete response to MAC
635 *
636 * @params[in] Inst inst, SchUeDelete  *ueDelete, SchMacRsp result, 
637 *              ErrorCause cause
638 * @return ROK     - success
639 *         RFAILED - failure
640 *
641 * ****************************************************************/
642 void SchSendUeDeleteRspToMac(Inst inst, SchUeDelete  *ueDelete, SchMacRsp result, ErrorCause cause)
643 {
644     Pst rspPst;
645     SchUeDeleteRsp  delRsp;
646     
647     memset(&delRsp, 0, sizeof(SchUeDeleteRsp));
648     delRsp.cellId = ueDelete->cellId;
649     delRsp.crnti = ueDelete->crnti;
650     delRsp.rsp = result; 
651     delRsp.cause = cause; 
652
653     /* Filling response post */
654     memset(&rspPst, 0, sizeof(Pst));
655     FILL_PST_SCH_TO_MAC(rspPst, inst);
656     rspPst.event = EVENT_UE_DELETE_RSP_TO_MAC;
657     SchUeDeleteRspOpts[rspPst.selector](&rspPst, &delRsp);
658 }
659
660 /*******************************************************************
661 *
662 * @brief Function to delete Sch Pucch ResrcCfg
663 *
664 * @details
665 *
666 *    Function : deleteSchPucchResrcCfg 
667 *
668 *    Functionality: Function to delete Sch Pucch ResrcCfg
669 *
670 * @params[in] SchPucchResrcCfg *resrc
671 * @return void 
672 *
673 * ****************************************************************/
674
675 void deleteSchPucchResrcCfg(SchPucchResrcCfg *resrc)
676 {
677    uint8_t rsrcIdx=0;
678    for(rsrcIdx=0; rsrcIdx < resrc->resrcToAddModListCount; rsrcIdx++)
679    {
680       switch(resrc->resrcToAddModList[rsrcIdx].pucchFormat)
681       {
682          case PUCCH_FORMAT_0:
683          {
684             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format0,\
685             sizeof(SchPucchFormat0));
686             break;
687          }
688          case PUCCH_FORMAT_1:
689          {
690             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format1,\
691             sizeof(SchPucchFormat1));
692             break;
693          }
694          case PUCCH_FORMAT_2:
695          {
696             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format2,\
697             sizeof(SchPucchFormat2_3));
698             break;
699          }
700          case PUCCH_FORMAT_3:
701          {
702             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format3,\
703             sizeof(SchPucchFormat2_3));
704             break;
705          }
706          case PUCCH_FORMAT_4:
707          {
708             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format4,\
709             sizeof(SchPucchFormat4));
710             break;
711          }
712       }
713    }
714 }
715
716 /*******************************************************************
717 *
718 * @brief Function to delete SCH Pdsch ServCellCfg
719 *
720 * @details
721 *
722 *    Function : deleteSchPdschServCellCfg
723 *
724 *    Functionality: Function to delete SCH Pdsch ServCellCfg
725 *
726 * @params[in] SchPdschServCellCfg *pdschServCellCfg
727 * @return void 
728 *
729 * ****************************************************************/
730
731 void deleteSchPdschServCellCfg(SchPdschServCellCfg *pdschServCellCfg)
732 {
733    SCH_FREE(pdschServCellCfg->maxMimoLayers, sizeof(uint8_t));
734    SCH_FREE(pdschServCellCfg->maxCodeBlkGrpPerTb, sizeof(SchMaxCodeBlkGrpPerTB));
735    SCH_FREE(pdschServCellCfg->codeBlkGrpFlushInd, sizeof(bool));
736    SCH_FREE(pdschServCellCfg->xOverhead, sizeof(SchPdschXOverhead));
737 }
738
739 /*******************************************************************
740 *
741 * @brief Function to  delete SCH UeCb
742 *
743 * @details
744 *
745 *    Function : deleteSchUeCb 
746 *
747 *    Functionality: Function to delete SCH UeCb
748 *
749 * @params[in]
750 * @return ROK     - success
751 *         RFAILED - failure
752 *
753 * ****************************************************************/
754 void deleteSchUeCb(SchUeCb *ueCb) 
755 {
756    SchPucchCfg *pucchCfg = NULLP;
757    if(ueCb)
758    {
759       SCH_FREE(ueCb->ueCfg.ambrCfg, sizeof(SchAmbrCfg));
760       if(ueCb->ueCfg.spCellCfgPres)
761       {
762          if(ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfgPres == true)
763          {
764             pucchCfg = &ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfg;
765             SCH_FREE(pucchCfg->resrcSet,sizeof(SchPucchResrcSetCfg));
766             if(pucchCfg->resrc)
767             {
768                deleteSchPucchResrcCfg(pucchCfg->resrc);
769                SCH_FREE(pucchCfg->resrc, sizeof(SchPucchResrcCfg));
770             }
771             SCH_FREE(pucchCfg->format1, sizeof(SchPucchFormatCfg));
772             SCH_FREE(pucchCfg->format2, sizeof(SchPucchFormatCfg));
773             SCH_FREE(pucchCfg->format3, sizeof(SchPucchFormatCfg));
774             SCH_FREE(pucchCfg->format4, sizeof(SchPucchFormatCfg));
775             SCH_FREE(pucchCfg->schedReq, sizeof(SchPucchSchedReqCfg));
776             SCH_FREE(pucchCfg->multiCsiCfg, sizeof(SchPucchMultiCsiCfg));
777             SCH_FREE(pucchCfg->spatialInfo, sizeof(SchPucchSpatialCfg));  
778             SCH_FREE(pucchCfg->dlDataToUlAck, sizeof(SchPucchDlDataToUlAck));
779             SCH_FREE(pucchCfg->powerControl,sizeof(SchPucchPowerControl));
780          }
781          SCH_FREE(ueCb->ueCfg.spCellCfg.servCellCfg.bwpInactivityTmr, sizeof(uint8_t));
782          deleteSchPdschServCellCfg(&ueCb->ueCfg.spCellCfg.servCellCfg.pdschServCellCfg);
783       }
784       memset(ueCb, 0, sizeof(SchUeCb));
785    }
786 }
787
788 /*******************************************************************
789 *
790 * @brief Function for Ue Delete request from MAC to SCH
791 *
792 * @details
793 *
794 *    Function : MacSchUeDeleteReq 
795 *
796 *    Functionality: Function for Ue Delete request from MAC to SCH
797 *
798 * @params[in] Pst *pst, SchUeDelete  *ueDelete
799 * @return ROK     - success
800 *         RFAILED - failure
801 *
802 * ****************************************************************/
803 uint8_t MacSchUeDeleteReq(Pst *pst, SchUeDelete  *ueDelete)
804 {
805     uint8_t idx=0, ueIdx=0, ret=ROK;
806     ErrorCause result;
807     SchCellCb    *cellCb = NULLP;
808     Inst         inst = pst->dstInst - 1;
809     
810     if(!ueDelete)
811     {
812        DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): Ue Delete request failed");
813        ret = RFAILED;
814     }
815     DU_LOG("\nDEBUG  -->  SCH : Ue Delete request received for crnti[%d]", ueDelete->crnti);
816     
817     cellCb = schCb[inst].cells[idx];
818
819     if(cellCb->cellId != ueDelete->cellId)
820     {
821        DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): cell Id is not available");
822        result =  INVALID_CELLID;
823     }
824     else
825     {
826        GET_UE_IDX(ueDelete->crnti, ueIdx);
827        if(( cellCb->ueCb[ueIdx-1].crnti == ueDelete->crnti) && ( cellCb->ueCb[ueIdx-1].state == SCH_UE_STATE_ACTIVE))
828        {
829           deleteSchUeCb(&cellCb->ueCb[ueIdx-1]);
830           cellCb->numActvUe--;
831           result = NOT_APPLICABLE;
832        }
833        else
834        {
835           DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): SchUeCb not found");
836           result =  INVALID_UEIDX;
837        }
838     }
839     
840     if(result == NOT_APPLICABLE)
841     {
842        SchSendUeDeleteRspToMac(inst, ueDelete, RSP_OK, result);
843     }
844     else
845     {
846        SchSendUeDeleteRspToMac(inst, ueDelete, RSP_NOK, result);
847        ret = RFAILED;
848     }
849     return ret;
850 }
851
852 /*******************************************************************
853  *
854  * @brief Fill and send Cell delete response to MAC
855  *
856  * @details
857  *
858  *    Function :  SchSendCellDeleteRspToMac
859  *
860  *    Functionality: Fill and send Cell delete response to MAC
861  *
862  * @params[in] SchCellDelete  *ueDelete, Inst inst, SchMacRsp result
863  * @return ROK     - success
864  *         RFAILED - failure
865  *
866  * ****************************************************************/
867 uint8_t SchSendCellDeleteRspToMac(SchCellDelete  *ueDelete, Inst inst, SchMacRsp result)
868 {
869    Pst rspPst;
870    uint8_t ret=0;
871    
872    SchCellDeleteRsp  delRsp;
873
874    DU_LOG("\nINFO   --> SCH : Filling Cell Delete response");
875    memset(&delRsp, 0, sizeof(SchCellDeleteRsp));
876    delRsp.cellId = ueDelete->cellId;
877    delRsp.rsp = result;
878
879    /* Filling response post */
880    memset(&rspPst, 0, sizeof(Pst));
881    FILL_PST_SCH_TO_MAC(rspPst, inst);
882    rspPst.event = EVENT_CELL_DELETE_RSP_TO_MAC;
883    ret =  SchCellDeleteRspOpts[rspPst.selector](&rspPst, &delRsp);
884    if(ret == RFAILED)
885    {
886       DU_LOG("\nERROR  -->  SCH : SchSendCellDeleteRspToMac(): failed to send the Cell Delete response");
887       return ret;
888    }
889    return ret;
890 }
891
892 /*******************************************************************
893  *
894  * @brief Function for cellCb Deletion 
895  *
896  * @details
897  *
898  *    Function : deleteSchCellCb 
899  *
900  *    Functionality: Function for cellCb Deletion 
901  *
902  * @params[in] SchCellDelete  *cellDelete
903  * @return ROK     - success
904  *         RFAILED - failure
905  *
906  * ****************************************************************/
907 void deleteSchCellCb(SchCellCb *cellCb)
908 {
909    uint8_t idx=0;
910    if(cellCb->schDlSlotInfo)
911    {
912       for(idx=0; idx<cellCb->numSlots; idx++)
913       {
914          if(cellCb->schDlSlotInfo[idx])
915          {
916             SCH_FREE(cellCb->schDlSlotInfo[idx], sizeof(SchDlSlotInfo));
917          }
918       }
919       SCH_FREE(cellCb->schDlSlotInfo, cellCb->numSlots *sizeof(SchDlSlotInfo*));
920    }
921    if(cellCb->schUlSlotInfo)
922    {
923       for(idx=0; idx<cellCb->numSlots; idx++)
924       {
925          if(cellCb->schUlSlotInfo[idx])
926          {
927             SCH_FREE(cellCb->schUlSlotInfo[idx], sizeof(SchUlSlotInfo));  
928          }
929       }
930       SCH_FREE(cellCb->schUlSlotInfo,  cellCb->numSlots * sizeof(SchUlSlotInfo*));
931    }
932    memset(cellCb, 0, sizeof(SchCellCb));
933
934 }
935
936 /*******************************************************************
937  *
938  * @brief Function for cell Delete request from MAC to SCH
939  *
940  * @details
941  *
942  *    Function : MacSchCellDeleteReq
943  *
944  *    Functionality: Function for cell Delete request from MAC to SCH
945  *
946  * @params[in] Pst *pst, SchCellDelete  *cellDelete
947  * @return ROK     - success
948  *         RFAILED - failure
949  *
950  * ****************************************************************/
951
952 uint8_t MacSchCellDeleteReq(Pst *pst, SchCellDelete  *cellDelete)
953 {
954    uint8_t   cellIdx=0, ret = RFAILED;
955    Inst      inst = pst->dstInst - 1;
956    SchMacRsp result= RSP_OK;
957
958    if(!cellDelete)
959    {
960       DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): Ue Delete request failed");
961    }
962    else
963    {
964       GET_CELL_IDX(cellDelete->cellId, cellIdx);
965       if(schCb[inst].cells[cellIdx] == NULLP)
966       { 
967          DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): cell Id[%d] is not available", cellDelete->cellId);
968          result = RSP_NOK;
969       }
970       else
971       {
972          if(schCb[inst].cells[cellIdx]->cellId == cellDelete->cellId)
973          {
974             deleteSchCellCb(schCb[inst].cells[cellIdx]);
975             result = RSP_OK;
976             ret = ROK;
977             SCH_FREE(schCb[inst].cells[cellIdx], sizeof(SchCellCb));
978             DU_LOG("\nINFO   -->  SCH : Sending Cell Delete response to MAC");
979          }
980          else
981          {
982             DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): cell Id[%d] is not available",cellDelete->cellId);
983             result = RSP_NOK;
984          }
985       }
986
987       if(SchSendCellDeleteRspToMac(cellDelete, inst, result)!=ROK)
988       {
989          DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): failed to send Cell Delete response");
990          ret =  RFAILED;
991       }
992
993    }
994    return ret;   
995 }
996
997 /**********************************************************************
998   End of file
999  **********************************************************************/