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