Merge "DL throughput calculation for UM [Issue-ID: ODUHIGH-319]"
[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   uint8_t  numPdschSymbols= 14;
426   uint16_t tbSize         = 0;
427   uint8_t  buffer         = 5;
428   uint8_t  idx            = 0;
429   SchCellCb *cellCb       = ueCb->cellCb;
430   SchUlSlotInfo *schUlSlotInfo = NULLP;
431   uint8_t k2=0, startSymb=0 , symbLen=0;
432   
433   if(ueCb->ueCfg.spCellCfgPres == true)
434   {
435      k2 = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].k2;
436      startSymb = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].startSymbol;
437      symbLen = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].symbolLength;
438   }
439   puschSlot = (pdcchSlot + k2) % cellCb->numSlots;
440
441   startRb = cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb;
442   tbSize  = schCalcTbSize(dataVol + buffer); /*  2 bytes header + some buffer */
443   numRb   = schCalcNumPrb(tbSize, ueCb->ueCfg.ulModInfo.mcsIndex, numPdschSymbols);
444   /* increment PUSCH PRB */
445
446   cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb += numRb;
447
448   puschInfo->crnti             = ueCb->crnti; 
449   puschInfo->harqProcId        = SCH_HARQ_PROC_ID;
450   puschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
451   puschInfo->fdAlloc.startPrb  = startRb;
452   puschInfo->fdAlloc.numPrb    = numRb;
453   puschInfo->tdAlloc.startSymb = startSymb;
454   puschInfo->tdAlloc.numSymb   = symbLen;
455   puschInfo->tbInfo.qamOrder   = ueCb->ueCfg.ulModInfo.modOrder;
456   puschInfo->tbInfo.mcs        = ueCb->ueCfg.ulModInfo.mcsIndex;
457   puschInfo->tbInfo.mcsTable   = ueCb->ueCfg.ulModInfo.mcsTable;
458   puschInfo->tbInfo.ndi        = 1; /* new transmission */
459   puschInfo->tbInfo.rv         = 0;
460   puschInfo->tbInfo.tbSize     = tbSize;
461   puschInfo->dmrsMappingType   = DMRS_MAP_TYPE_A;  /* Setting Type-A */
462   puschInfo->nrOfDmrsSymbols   = NUM_DMRS_SYMBOLS;
463   puschInfo->dmrsAddPos        = DMRS_ADDITIONAL_POS;
464
465   /* Update pusch in cell */
466   for(idx=startSymb; idx<symbLen; idx++)
467   {
468      cellCb->schUlSlotInfo[puschSlot]->assignedPrb[idx] = startRb + numRb;
469   }
470
471   schUlSlotInfo = cellCb->schUlSlotInfo[puschSlot];
472
473   SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
474   if(!schUlSlotInfo->schPuschInfo)
475   {
476      DU_LOG("\nERROR  -->  SCH: Memory allocation failed in schAllocMsg3Pusch");
477      return RFAILED;
478   }
479   memcpy(schUlSlotInfo->schPuschInfo, puschInfo, sizeof(SchPuschInfo));
480
481   return ROK;
482 }
483
484 /*******************************************************************
485  *
486  * @brief Fills DCI for UL grant
487  *
488  * @details
489  *
490  *    Function : schFillUlDci
491  *
492  *    Functionality: fills DCI for UL grant in response to BSR
493  *
494  * @params[in]
495  * @return ROK     - success
496  *         RFAILED - failure
497  *
498  * ****************************************************************/
499 uint8_t schFillUlDci(SchUeCb *ueCb, SchPuschInfo puschInfo, DciInfo *dciInfo)
500 {
501    SchCellCb         *cellCb  = ueCb->cellCb;
502    SchControlRsrcSet coreset1 ;
503   
504    memset(&coreset1, 0, sizeof(SchControlRsrcSet));
505    if(ueCb->ueCfg.spCellCfgPres == true)
506    {
507      coreset1 = ueCb->ueCfg.spCellCfg.servCellCfg.initDlBwp.pdcchCfg.cRSetToAddModList[0];
508    }
509    
510    dciInfo->cellId = cellCb->cellId;
511    dciInfo->crnti  = ueCb->crnti;
512
513    /* fill bwp cfg */
514    dciInfo->bwpCfg.subcarrierSpacing  = cellCb->cellCfg.sib1SchCfg.bwp.subcarrierSpacing;
515    dciInfo->bwpCfg.cyclicPrefix       = cellCb->cellCfg.sib1SchCfg.bwp.cyclicPrefix;
516    dciInfo->bwpCfg.freqAlloc.startPrb = 0;
517    dciInfo->bwpCfg.freqAlloc.numPrb   = MAX_NUM_RB; /* whole of BW */
518
519    /*fill coreset cfg */
520    //Considering number of RBs in coreset1 is same as coreset0
521    dciInfo->coresetCfg.coreSetSize      = coresetIdxTable[0][1];
522    //Considering coreset1 also starts from same symbol as coreset0
523    dciInfo->coresetCfg.startSymbolIndex = searchSpaceIdxTable[0][3];
524    dciInfo->coresetCfg.durationSymbols  = coreset1.duration;
525    memcpy(dciInfo->coresetCfg.freqDomainResource, coreset1.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
526    
527    dciInfo->coresetCfg.cceRegMappingType   = coreset1.cceRegMappingType; /* non-interleaved */
528    dciInfo->coresetCfg.regBundleSize       = 6; /* must be 6 for non-interleaved */
529    dciInfo->coresetCfg.interleaverSize     = 0; /* NA for non-interleaved */
530    dciInfo->coresetCfg.coreSetType         = 1; /* non PBCH coreset */
531    dciInfo->coresetCfg.shiftIndex          = cellCb->cellCfg.phyCellId;
532    dciInfo->coresetCfg.precoderGranularity = coreset1.precoderGranularity;
533    dciInfo->coresetCfg.cceIndex            = 0; /* 0-3 for UL and 4-7 for DL */
534    dciInfo->coresetCfg.aggregationLevel    = 4; /* same as for sib1 */
535    
536    dciInfo->formatType = FORMAT0_0;
537    
538    /* fill UL grant */
539    dciInfo->format.format0_0.resourceAllocType   = puschInfo.resAllocType;
540    dciInfo->format.format0_0.freqAlloc.startPrb  = puschInfo.fdAlloc.startPrb;
541    dciInfo->format.format0_0.freqAlloc.numPrb    = puschInfo.fdAlloc.numPrb;
542    dciInfo->format.format0_0.timeAlloc.startSymb = puschInfo.tdAlloc.startSymb;
543    dciInfo->format.format0_0.timeAlloc.numSymb   = puschInfo.tdAlloc.numSymb;
544    dciInfo->format.format0_0.rowIndex            = 0; /* row Index */
545    dciInfo->format.format0_0.mcs                 = puschInfo.tbInfo.mcs;
546    dciInfo->format.format0_0.harqProcId          = puschInfo.harqProcId;
547    dciInfo->format.format0_0.puschHopFlag        = FALSE; /* disabled */
548    dciInfo->format.format0_0.freqHopFlag         = FALSE; /* disabled */
549    dciInfo->format.format0_0.ndi                 = puschInfo.tbInfo.ndi; /* new transmission */
550    dciInfo->format.format0_0.rv                  = puschInfo.tbInfo.rv;
551    dciInfo->format.format0_0.tpcCmd              = 0; //Sphoorthi TODO: check
552    dciInfo->format.format0_0.sUlCfgd             = FALSE; /* SUL not configured */
553    
554    /* Fill DCI Structure */
555    dciInfo->dciInfo.rnti                              = ueCb->crnti;
556    dciInfo->dciInfo.scramblingId                      = cellCb->cellCfg.phyCellId;
557    dciInfo->dciInfo.scramblingRnti                    = 0;
558    dciInfo->dciInfo.cceIndex                          = 0; /* 0-3 for UL and 4-7 for DL */
559    dciInfo->dciInfo.aggregLevel                       = 4;
560    dciInfo->dciInfo.beamPdcchInfo.numPrgs             = 1;
561    dciInfo->dciInfo.beamPdcchInfo.prgSize             = 1;
562    dciInfo->dciInfo.beamPdcchInfo.digBfInterfaces     = 0;
563    dciInfo->dciInfo.beamPdcchInfo.prg[0].pmIdx        = 0;
564    dciInfo->dciInfo.beamPdcchInfo.prg[0].beamIdx[0]   = 0;
565    dciInfo->dciInfo.txPdcchPower.powerValue           = 0;
566    dciInfo->dciInfo.txPdcchPower.powerControlOffsetSS = 0;
567    dciInfo->dciInfo.pdschCfg                          = NULLP; /* No DL data being sent */
568
569    return ROK;
570 }
571
572 /*******************************************************************
573  *
574  * @brief Function to Modify Ue Config request from MAC
575  *
576  * @details
577  *
578  *    Function : MacSchModUeConfigReq
579  *
580  *    Functionality: Function to modify Ue Config request from MAC
581  *
582  * @params[in] 
583  * @return ROK     - success
584  *         RFAILED - failure
585  *
586  * ****************************************************************/
587 uint8_t MacSchModUeConfigReq(Pst *pst, SchUeCfg *ueCfg)
588 {
589    uint8_t ueIdx, lcIdx, ret = ROK;
590    SchCellCb    *cellCb = NULLP;
591    SchUeCb      *ueCb = NULLP;
592    SchUeCfgRsp  cfgRsp;
593    Inst         inst = pst->dstInst - 1;
594    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
595
596    if(!ueCfg)
597    {
598       DU_LOG("\nERROR  -->  SCH : Modifying Ue Config request failed at MacSchModUeConfigReq()");
599       return RFAILED;
600    }
601    DU_LOG("\nDEBUG  -->  SCH : Modifying Ue Config Request for CRNTI[%d]", ueCfg->crnti);
602    cellCb = getSchCellCb(pst->event, inst, ueCfg);
603
604    /* Search if UE already configured */
605    GET_UE_IDX(ueCfg->crnti, ueIdx);
606    ueCb = &cellCb->ueCb[ueIdx -1];
607    
608    if(!ueCb)
609    {
610       DU_LOG("\nERROR  -->  SCH : SchUeCb not found at MacSchModUeConfigReq() ");
611       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_NOK, &cfgRsp);
612       return RFAILED;
613    }
614    if((ueCb->crnti == ueCfg->crnti) && (ueCb->state == SCH_UE_STATE_ACTIVE))
615    {
616       /* Found the UeCb to Reconfig */
617       ret = fillSchUeCb(ueCb, ueCfg);
618       if(ret == ROK)
619       {
620          ueCb->cellCb = cellCb;
621          ueCb->srRcvd = false;
622          for(lcIdx=0; lcIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcIdx++)
623             ueCb->bsrInfo[lcIdx].dataVol = 0;
624
625          SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
626       }
627    }
628    return ret;
629 }
630
631 /*******************************************************************
632 *
633 * @brief Fill and send UE delete response to MAC
634 *
635 * @details
636 *
637 *    Function :  SchSendUeDeleteRspToMac
638 *
639 *    Functionality: Fill and send UE delete response to MAC
640 *
641 * @params[in] Inst inst, SchUeDelete  *ueDelete, SchMacRsp result, 
642 *              ErrorCause cause
643 * @return ROK     - success
644 *         RFAILED - failure
645 *
646 * ****************************************************************/
647 void SchSendUeDeleteRspToMac(Inst inst, SchUeDelete  *ueDelete, SchMacRsp result, ErrorCause cause)
648 {
649     Pst rspPst;
650     SchUeDeleteRsp  delRsp;
651     
652     memset(&delRsp, 0, sizeof(SchUeDeleteRsp));
653     delRsp.cellId = ueDelete->cellId;
654     delRsp.crnti = ueDelete->crnti;
655     delRsp.rsp = result; 
656     delRsp.cause = cause; 
657
658     /* Filling response post */
659     memset(&rspPst, 0, sizeof(Pst));
660     FILL_PST_SCH_TO_MAC(rspPst, inst);
661     rspPst.event = EVENT_UE_DELETE_RSP_TO_MAC;
662     SchUeDeleteRspOpts[rspPst.selector](&rspPst, &delRsp);
663 }
664
665 /*******************************************************************
666 *
667 * @brief Function to delete Sch Pucch ResrcCfg
668 *
669 * @details
670 *
671 *    Function : deleteSchPucchResrcCfg 
672 *
673 *    Functionality: Function to delete Sch Pucch ResrcCfg
674 *
675 * @params[in] SchPucchResrcCfg *resrc
676 * @return void 
677 *
678 * ****************************************************************/
679
680 void deleteSchPucchResrcCfg(SchPucchResrcCfg *resrc)
681 {
682    uint8_t rsrcIdx=0;
683    for(rsrcIdx=0; rsrcIdx < resrc->resrcToAddModListCount; rsrcIdx++)
684    {
685       switch(resrc->resrcToAddModList[rsrcIdx].pucchFormat)
686       {
687          case PUCCH_FORMAT_0:
688          {
689             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format0,\
690             sizeof(SchPucchFormat0));
691             break;
692          }
693          case PUCCH_FORMAT_1:
694          {
695             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format1,\
696             sizeof(SchPucchFormat1));
697             break;
698          }
699          case PUCCH_FORMAT_2:
700          {
701             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format2,\
702             sizeof(SchPucchFormat2_3));
703             break;
704          }
705          case PUCCH_FORMAT_3:
706          {
707             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format3,\
708             sizeof(SchPucchFormat2_3));
709             break;
710          }
711          case PUCCH_FORMAT_4:
712          {
713             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format4,\
714             sizeof(SchPucchFormat4));
715             break;
716          }
717       }
718    }
719 }
720
721 /*******************************************************************
722 *
723 * @brief Function to delete SCH Pdsch ServCellCfg
724 *
725 * @details
726 *
727 *    Function : deleteSchPdschServCellCfg
728 *
729 *    Functionality: Function to delete SCH Pdsch ServCellCfg
730 *
731 * @params[in] SchPdschServCellCfg *pdschServCellCfg
732 * @return void 
733 *
734 * ****************************************************************/
735
736 void deleteSchPdschServCellCfg(SchPdschServCellCfg *pdschServCellCfg)
737 {
738    SCH_FREE(pdschServCellCfg->maxMimoLayers, sizeof(uint8_t));
739    SCH_FREE(pdschServCellCfg->maxCodeBlkGrpPerTb, sizeof(SchMaxCodeBlkGrpPerTB));
740    SCH_FREE(pdschServCellCfg->codeBlkGrpFlushInd, sizeof(bool));
741    SCH_FREE(pdschServCellCfg->xOverhead, sizeof(SchPdschXOverhead));
742 }
743
744 /*******************************************************************
745 *
746 * @brief Function to  delete SCH UeCb
747 *
748 * @details
749 *
750 *    Function : deleteSchUeCb 
751 *
752 *    Functionality: Function to delete SCH UeCb
753 *
754 * @params[in]
755 * @return ROK     - success
756 *         RFAILED - failure
757 *
758 * ****************************************************************/
759 void deleteSchUeCb(SchUeCb *ueCb) 
760 {
761    SchPucchCfg *pucchCfg = NULLP;
762    if(ueCb)
763    {
764       SCH_FREE(ueCb->ueCfg.ambrCfg, sizeof(SchAmbrCfg));
765       if(ueCb->ueCfg.spCellCfgPres)
766       {
767          if(ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfgPres == true)
768          {
769             pucchCfg = &ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfg;
770             SCH_FREE(pucchCfg->resrcSet,sizeof(SchPucchResrcSetCfg));
771             if(pucchCfg->resrc)
772             {
773                deleteSchPucchResrcCfg(pucchCfg->resrc);
774                SCH_FREE(pucchCfg->resrc, sizeof(SchPucchResrcCfg));
775             }
776             SCH_FREE(pucchCfg->format1, sizeof(SchPucchFormatCfg));
777             SCH_FREE(pucchCfg->format2, sizeof(SchPucchFormatCfg));
778             SCH_FREE(pucchCfg->format3, sizeof(SchPucchFormatCfg));
779             SCH_FREE(pucchCfg->format4, sizeof(SchPucchFormatCfg));
780             SCH_FREE(pucchCfg->schedReq, sizeof(SchPucchSchedReqCfg));
781             SCH_FREE(pucchCfg->multiCsiCfg, sizeof(SchPucchMultiCsiCfg));
782             SCH_FREE(pucchCfg->spatialInfo, sizeof(SchPucchSpatialCfg));  
783             SCH_FREE(pucchCfg->dlDataToUlAck, sizeof(SchPucchDlDataToUlAck));
784             SCH_FREE(pucchCfg->powerControl,sizeof(SchPucchPowerControl));
785          }
786          SCH_FREE(ueCb->ueCfg.spCellCfg.servCellCfg.bwpInactivityTmr, sizeof(uint8_t));
787          deleteSchPdschServCellCfg(&ueCb->ueCfg.spCellCfg.servCellCfg.pdschServCellCfg);
788       }
789       memset(ueCb, 0, sizeof(SchUeCb));
790    }
791 }
792
793 /*******************************************************************
794 *
795 * @brief Function for Ue Delete request from MAC to SCH
796 *
797 * @details
798 *
799 *    Function : MacSchUeDeleteReq 
800 *
801 *    Functionality: Function for Ue Delete request from MAC to SCH
802 *
803 * @params[in] Pst *pst, SchUeDelete  *ueDelete
804 * @return ROK     - success
805 *         RFAILED - failure
806 *
807 * ****************************************************************/
808 uint8_t MacSchUeDeleteReq(Pst *pst, SchUeDelete  *ueDelete)
809 {
810     uint8_t idx=0, ueIdx=0, ret=ROK;
811     ErrorCause result;
812     SchCellCb    *cellCb = NULLP;
813     Inst         inst = pst->dstInst - 1;
814     
815     if(!ueDelete)
816     {
817        DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): Ue Delete request failed");
818        ret = RFAILED;
819     }
820     DU_LOG("\nDEBUG  -->  SCH : Ue Delete request received for crnti[%d]", ueDelete->crnti);
821     
822     cellCb = schCb[inst].cells[idx];
823
824     if(cellCb->cellId != ueDelete->cellId)
825     {
826        DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): cell Id is not available");
827        result =  INVALID_CELLID;
828     }
829     else
830     {
831        GET_UE_IDX(ueDelete->crnti, ueIdx);
832        if(( cellCb->ueCb[ueIdx-1].crnti == ueDelete->crnti) && ( cellCb->ueCb[ueIdx-1].state == SCH_UE_STATE_ACTIVE))
833        {
834           deleteSchUeCb(&cellCb->ueCb[ueIdx-1]);
835           cellCb->numActvUe--;
836           result = NOT_APPLICABLE;
837        }
838        else
839        {
840           DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): SchUeCb not found");
841           result =  INVALID_UEIDX;
842        }
843     }
844     
845     if(result == NOT_APPLICABLE)
846     {
847        SchSendUeDeleteRspToMac(inst, ueDelete, RSP_OK, result);
848     }
849     else
850     {
851        SchSendUeDeleteRspToMac(inst, ueDelete, RSP_NOK, result);
852        ret = RFAILED;
853     }
854     return ret;
855 }
856
857 /*******************************************************************
858  *
859  * @brief Fill and send Cell delete response to MAC
860  *
861  * @details
862  *
863  *    Function :  SchSendCellDeleteRspToMac
864  *
865  *    Functionality: Fill and send Cell delete response to MAC
866  *
867  * @params[in] SchCellDelete  *ueDelete, Inst inst, SchMacRsp result
868  * @return ROK     - success
869  *         RFAILED - failure
870  *
871  * ****************************************************************/
872 uint8_t SchSendCellDeleteRspToMac(SchCellDelete  *ueDelete, Inst inst, SchMacRsp result)
873 {
874    Pst rspPst;
875    uint8_t ret=0;
876    
877    SchCellDeleteRsp  delRsp;
878
879    DU_LOG("\nINFO   --> SCH : Filling Cell Delete response");
880    memset(&delRsp, 0, sizeof(SchCellDeleteRsp));
881    delRsp.cellId = ueDelete->cellId;
882    delRsp.rsp = result;
883
884    /* Filling response post */
885    memset(&rspPst, 0, sizeof(Pst));
886    FILL_PST_SCH_TO_MAC(rspPst, inst);
887    rspPst.event = EVENT_CELL_DELETE_RSP_TO_MAC;
888    ret =  SchCellDeleteRspOpts[rspPst.selector](&rspPst, &delRsp);
889    if(ret == RFAILED)
890    {
891       DU_LOG("\nERROR  -->  SCH : SchSendCellDeleteRspToMac(): failed to send the Cell Delete response");
892       return ret;
893    }
894    return ret;
895 }
896
897 /*******************************************************************
898  *
899  * @brief Function for cellCb Deletion 
900  *
901  * @details
902  *
903  *    Function : deleteSchCellCb 
904  *
905  *    Functionality: Function for cellCb Deletion 
906  *
907  * @params[in] SchCellDelete  *cellDelete
908  * @return ROK     - success
909  *         RFAILED - failure
910  *
911  * ****************************************************************/
912 void deleteSchCellCb(SchCellCb *cellCb)
913 {
914    uint8_t idx=0;
915    if(cellCb->schDlSlotInfo)
916    {
917       for(idx=0; idx<cellCb->numSlots; idx++)
918       {
919          if(cellCb->schDlSlotInfo[idx])
920          {
921             SCH_FREE(cellCb->schDlSlotInfo[idx]->rarInfo, sizeof(RarInfo));
922             if(cellCb->schDlSlotInfo[idx]->dlMsgInfo)
923             {
924                SCH_FREE(cellCb->schDlSlotInfo[idx]->dlMsgInfo->dlMsgPdu,\
925                      cellCb->schDlSlotInfo[idx]->dlMsgInfo->dlMsgPduLen);
926                SCH_FREE(cellCb->schDlSlotInfo[idx]->dlMsgInfo, sizeof(DlMsgInfo));
927             }
928             SCH_FREE(cellCb->schDlSlotInfo[idx], sizeof(SchDlSlotInfo));
929          }
930       }
931       SCH_FREE(cellCb->schDlSlotInfo, sizeof(SchDlSlotInfo));
932    }
933    if(cellCb->schUlSlotInfo)
934    {
935       for(idx=0; idx<cellCb->numSlots; idx++)
936       {
937          if(cellCb->schUlSlotInfo[idx])
938          {
939             SCH_FREE(cellCb->schUlSlotInfo[idx]->schPuschInfo,sizeof(SchPuschInfo));
940             SCH_FREE(cellCb->schUlSlotInfo[idx], sizeof(SchUlSlotInfo));  
941          }
942       }
943       SCH_FREE(cellCb->schUlSlotInfo, sizeof(SchUlSlotInfo));
944    }
945    SCH_FREE(cellCb->cellCfg.sib1SchCfg.sib1PdcchCfg.dci.pdschCfg, sizeof(PdschCfg));
946    memset(cellCb, 0, sizeof(SchCellCb));
947 }
948
949 /*******************************************************************
950  *
951  * @brief Function for cell Delete request from MAC to SCH
952  *
953  * @details
954  *
955  *    Function : MacSchCellDeleteReq
956  *
957  *    Functionality: Function for cell Delete request from MAC to SCH
958  *
959  * @params[in] Pst *pst, SchCellDelete  *cellDelete
960  * @return ROK     - success
961  *         RFAILED - failure
962  *
963  * ****************************************************************/
964
965 uint8_t MacSchCellDeleteReq(Pst *pst, SchCellDelete  *cellDelete)
966 {
967    uint8_t   cellIdx=0, ret = RFAILED;
968    Inst      inst = pst->dstInst - 1;
969    SchMacRsp result= RSP_OK;
970
971    if(!cellDelete)
972    {
973       DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): Ue Delete request failed");
974    }
975    else
976    {
977       GET_CELL_IDX(cellDelete->cellId, cellIdx);
978       if(schCb[inst].cells[cellIdx] == NULLP)
979       { 
980          DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): cell Id[%d] is not available", cellDelete->cellId);
981          result = RSP_NOK;
982       }
983       else
984       {
985          if(schCb[inst].cells[cellIdx]->cellId == cellDelete->cellId)
986          {
987             deleteSchCellCb(schCb[inst].cells[cellIdx]);
988             result = RSP_OK;
989             ret = ROK;
990             SCH_FREE(schCb[inst].cells[cellIdx], sizeof(SchCellCb));
991             DU_LOG("\nINFO   -->  SCH : Sending Cell Delete response to MAC");
992          }
993          else
994          {
995             DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): cell Id[%d] is not available",cellDelete->cellId);
996             result = RSP_NOK;
997          }
998       }
999
1000       if(SchSendCellDeleteRspToMac(cellDelete, inst, result)!=ROK)
1001       {
1002          DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): failed to send Cell Delete response");
1003          ret =  RFAILED;
1004       }
1005
1006    }
1007    return ret;   
1008 }
1009
1010 /**********************************************************************
1011   End of file
1012  **********************************************************************/