Merge "Part -7 Classification of log [JIRA-ID ODUHIGH-275]"
[o-du/l2.git] / src / 5gnrmac / rg_utl.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /************************************************************************
20  
21      Name:     LTE-MAC layer
22   
23      Type:     C source file
24   
25      Desc:     C source code for Entry point fucntions
26   
27      File:     rg_utl.c
28   
29 **********************************************************************/
30
31 /** @file rg_utl.c
32 @brief This file implements utility functions for LTE MAC
33 */
34
35
36 /* header include files (.h) */
37 #include "common_def.h"
38 #include "rg_env.h"        /* MAC Environment Defines */
39 #include "crg.h"           /* CRG Interface defines */
40 #include "rgu.h"           /* RGU Interface defines */
41 #include "tfu.h"           /* TFU Interface defines */
42 #include "rg_sch_inf.h"    /* RGR Interface defines */
43 #include "lrg.h"           /* LRG Interface defines */
44
45 #include "rg_prg.h"        /* PRG(MAC-MAC) Interface includes */
46 #include "rg.h"            /* MAC defines */
47 #include "rg_err.h"        /* MAC error defines */
48
49 /* header/extern include files (.x) */
50 #include "cm5.x"           /* Timer */
51
52 #include "crg.x"           /* CRG Interface includes */
53 #include "rgu.x"           /* RGU Interface includes */
54 #include "tfu.x"           /* TFU Interface includes */
55 #include "rg_sch_inf.x"    /* RGR Interface includes */
56 #include "lrg.x"           /* LRG Interface includes */
57 #include "rg_prg.x"        /* PRG(MAC-MAC) Interface includes */
58
59 #include "du_app_mac_inf.h"
60 #include "rg.x"            /* MAC includes */
61
62 /* local defines */
63 #define RG_NON_MIMO_IDX 0
64
65 /* local typedefs */
66  
67 /* local externs */
68  
69 /* forward references */
70 static S16 rgUtlHndlCrntiChng ARGS((
71          Inst            inst,
72          RgCellCb        *cell,
73          CmLteRnti       rnti,
74          CmLteRnti       newRnti
75          ));
76 static Void rgUtlHndlCrntiRls ARGS((
77          RgCellCb        *cell,
78          RgInfRlsRnti    *rlsRnti
79          ));
80
81 S16 rgDelUeFrmAllSCell ARGS((
82          RgCellCb       *cell,
83          RgUeCb         *ue
84          ));
85
86 #ifdef LTE_ADV
87 static S16 rgUtlSndCrntiChngReq2AllSMacs ARGS((
88          RgCellCb        *cell,
89          CmLteRnti       rnti,
90          CmLteRnti       newRnti
91          ));
92 #endif
93 /***********************************************************
94  *
95  *     Func : rgAllocShrablSBuf
96  *
97  *     Desc : Utility Function to Allocate static buffer which is 
98  *            sharable among different layers if YYYY flag is enabled. 
99  *            else it allocates from normal static region
100  *            Memory allocated is assumed contiguous.
101  *            
102  *
103  *     Ret  : ROK
104  *            RFAILED
105  *
106  *     Notes: Caller doesnt need to raise the alarm in case of memory
107  *            allocation gets failed. 
108  *
109  *     File : rg_utl.c
110  *
111  **********************************************************/
112 S16 rgAllocShrablSBuf
113 (
114 Inst    inst,
115 Data    **pData,            /* Pointer of the data to be returned */
116 Size    size                /* size */
117 )
118 {
119    RgUstaDgn dgn;      /* Alarm diagnostics structure */
120
121    /* Initialize the param to NULLP */
122    *pData = NULLP;
123
124    if (size == 0)
125    {
126       return RFAILED;
127    }
128
129    /* allocate buffer */
130    if (SGetStaticBuffer(rgCb[inst].rgInit.region, rgCb[inst].rgInit.pool, pData, size, 0) != ROK)
131    {
132      dgn.type = LRG_USTA_DGNVAL_MEM;
133      dgn.u.mem.region = rgCb[inst].rgInit.region;
134      dgn.u.mem.pool = rgCb[inst].rgInit.pool;
135      /*  Send an alarm to Layer Manager */
136      rgLMMStaInd(inst,LCM_CATEGORY_RESOURCE, LCM_EVENT_SMEM_ALLOC_FAIL,
137                                        LCM_CAUSE_MEM_ALLOC_FAIL, &dgn);
138       DU_LOG("\nERROR  -->  MAC : Unable to Allocate Buffer");
139       return RFAILED;
140    }
141
142 #ifndef ALIGN_64BIT
143    DU_LOG("\nDEBUG  -->  MAC : SGetSBuf(Region (%d), Pool (%d), Size (%ld)), Data (0x%p))\n",
144              rgCb[inst].rgInit.region, rgCb[inst].rgInit.pool, size, *pData);
145 #else
146    DU_LOG("\nDEBUG  -->  MAC : SGetSBuf(Region (%d), Pool (%d), Size (%d)), Data (0x%p))\n",
147              rgCb[inst].rgInit.region, rgCb[inst].rgInit.pool, size, *pData);
148 #endif
149
150    /* zero out the allocated memory */
151    memset(*pData, 0x00, size);
152
153    return ROK;
154
155 } /* end of rgAllocSBuf */
156
157
158 /***********************************************************
159  *
160  *     Func : rgAllocSBuf
161  *
162  *     Desc : Utility Function to Allocate static buffer. 
163  *            Memory allocated is assumed contiguous.
164  *            
165  *
166  *     Ret  : ROK
167  *            RFAILED
168  *
169  *     Notes: Caller doesnt need to raise the alarm in case of memory
170  *            allocation gets failed. 
171  *
172  *     File : rg_utl.c
173  *
174  **********************************************************/
175 S16 rgAllocSBuf
176 (
177 Inst    inst,
178 Data    **pData,            /* Pointer of the data to be returned */
179 Size    size                /* size */
180 )
181 {
182    RgUstaDgn dgn;      /* Alarm diagnostics structure */
183
184    /* Initialize the param to NULLP */
185    *pData = NULLP;
186
187    if (size == 0)
188    {
189       return RFAILED;
190    }
191
192    /* allocate buffer */
193 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
194    MS_BUF_ADD_ALLOC_CALLER();
195 #endif /* */
196    if (SGetSBuf(rgCb[inst].rgInit.region, rgCb[inst].rgInit.pool, pData, size) != ROK)
197    {
198      dgn.type = LRG_USTA_DGNVAL_MEM;
199      dgn.u.mem.region = rgCb[inst].rgInit.region;
200      dgn.u.mem.pool = rgCb[inst].rgInit.pool;
201      /*  Send an alarm to Layer Manager */
202      rgLMMStaInd(inst,LCM_CATEGORY_RESOURCE, LCM_EVENT_SMEM_ALLOC_FAIL,
203                                        LCM_CAUSE_MEM_ALLOC_FAIL, &dgn);
204       DU_LOG("\nERROR  -->  MAC : Unable to Allocate Buffer");
205       return RFAILED;
206    }
207
208    /* zero out the allocated memory */
209    memset(*pData, 0x00, size);
210
211    return ROK;
212
213 } /* end of rgAllocSBuf */
214
215 /*
216 *
217 *       Fun:   rgFreeSharableSBuf
218 *
219 *       Desc:  The argument to rgFreeSBuf() is a pointer to a block
220 *              previously allocated by rgAllocSBuf() and size. It 
221 *              deallocates the memory. 
222 *
223 *       Ret:   void
224 *
225 *       Notes: ccpu00117052 - MOD- changed the Data parameter from 
226 *                             pointer to address of pointer so that
227 *                             the freed memory could be set to NULLP
228 *
229 *       File:  rg_utl.c
230 */
231 Void rgFreeSharableSBuf
232 (
233 Inst inst,
234 Data **data,         /* address of pointer to data */
235 Size size            /* size */
236 )
237 {
238
239    S16 ret;
240
241    if ((data == NULLP) || (*data == NULLP) || (size == 0))
242    {
243       return;
244    }
245
246    /* Deallocate buffer */
247    ret = SPutStaticBuffer(rgCb[inst].rgInit.region, rgCb[inst].rgInit.pool, *data, size, SS_SHARABLE_MEMORY);
248
249    if (ret != ROK)
250    {
251       return;
252    }
253
254    *data = NULLP;
255
256    return;
257
258 } /* end of rgFreeSharableBuf */
259
260
261
262 /*
263 *
264 *       Fun:   rgFreeSBuf
265 *
266 *       Desc:  The argument to rgFreeSBuf() is a pointer to a block
267 *              previously allocated by rgAllocSBuf() and size. It 
268 *              deallocates the memory. 
269 *
270 *       Ret:   void
271 *
272 *       Notes: ccpu00117052 - MOD- changed the Data parameter from 
273 *                             pointer to address of pointer so that
274 *                             the freed memory could be set to NULLP
275 *
276 *       File:  rg_utl.c
277 */
278 Void rgFreeSBuf
279 (
280 Inst inst,
281 Data **data,         /* address of pointer to data */
282 Size size            /* size */
283 )
284 {
285
286    S16 ret;
287
288    if ((data == NULLP) || (*data == NULLP) || (size == 0))
289    {
290       return;
291    }
292
293
294 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
295    MS_BUF_ADD_CALLER();
296 #endif /* */
297    /* Deallocate buffer */
298    ret = SPutSBuf(rgCb[inst].rgInit.region, rgCb[inst].rgInit.pool, *data, size);
299
300    if (ret != ROK)
301    {
302       DU_LOG("\nERROR  -->  MAC : rgFreeSBuf failed.\n");
303       return;
304    }
305
306    *data = NULLP;
307
308    return;
309
310 } /* end of rgFreeSBuf */
311
312
313 /***********************************************************
314  *
315  *     Func : rgGetMsg
316  *
317  *     Desc : Utility Function to Allocate message buffer. 
318  *            
319  *
320  *     Ret  : ROK
321  *            RFAILED
322  *
323  *     Notes: Caller doesnt need to raise the alarm in case of memory
324  *            allocation gets failed. 
325  *
326  *     File : rg_utl.c
327  *
328  **********************************************************/
329 S16 rgGetMsg
330 (
331 Inst    inst,
332 Buffer  **mBuf            /* Message Buffer pointer be returned */
333 )
334 {
335    S16         ret;
336
337 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
338    MS_BUF_ADD_ALLOC_CALLER();
339 #endif /* */
340    ret = SGetMsg(RG_GET_MEM_REGION(rgCb[inst]), RG_GET_MEM_POOL(rgCb[inst]), mBuf);
341
342    if (ROK != ret) 
343    {
344       /* Moving diagnostics structure to limited scope for optimization */
345       RgUstaDgn   dgn;      /* Alarm diagnostics structure */
346
347       rgFillDgnParams(inst,&dgn, LRG_USTA_DGNVAL_MEM);
348
349       /*  Send an alarm to Layer Manager */
350       rgLMMStaInd(inst,LCM_CATEGORY_RESOURCE, LCM_EVENT_DMEM_ALLOC_FAIL,
351                                        LCM_CAUSE_MEM_ALLOC_FAIL, &dgn);
352       DU_LOG("\nERROR  -->  MAC : Unable to Allocate Buffer");
353       return RFAILED;
354    }
355
356    return ROK;
357
358 } /* end of rgGetMsg */
359
360
361 /***********************************************************
362  *
363  *     Func : rgFillDgnParams
364  *
365  *     Desc : Utility Function to Fill Diagonostic params. 
366  *
367  *     Ret  : None.
368  *
369  *     Notes: None.
370  *
371  *     File : rg_utl.c
372  *
373  **********************************************************/
374 Void rgFillDgnParams
375 (
376 Inst        inst,
377 RgUstaDgn   *dgn,
378 uint8_t     dgnType
379 )
380 {
381
382    switch(dgnType)
383    {
384       case LRG_USTA_DGNVAL_MEM:
385          dgn->type = (uint8_t) LRG_USTA_DGNVAL_MEM;
386          dgn->u.mem.region  = rgCb[inst].rgInit.region;
387          dgn->u.mem.pool    = rgCb[inst].rgInit.pool;
388       break;
389
390       default:
391       break;
392    }
393
394    return;
395 } /* end of rgFillDgnParams */
396
397
398 /***********************************************************
399  *
400  *     Func : rgUpdtRguDedSts
401  *
402  *     Desc : Utility Function to update rgu sap statistics for dedicated
403  *            DatReqs.
404  *            
405  *
406  *     Ret  : ROK
407  *            RFAILED
408  *
409  *     Notes: 
410  *
411  *     File : rg_utl.c
412  *
413  **********************************************************/
414 Void rgUpdtRguDedSts
415 (
416 Inst           inst,
417 RgUpSapCb      *rguDlSap,
418 uint8_t        stsType,   /* Statistics type to update */
419 RgRguDedDatReq *datReq    /* DatReq pointer */
420 )
421 {
422    uint8_t idx1,idx2;
423    uint32_t idx;
424
425    switch(stsType)
426    {
427       case RG_RGU_SDU_RCVD:
428       for(idx = 0; idx < datReq->nmbOfUeGrantPerTti; idx++)
429       {
430          RguDDatReqPerUe *datReqPerUe = &datReq->datReq[idx];
431          for (idx1 = 0; idx1 < datReqPerUe->nmbOfTbs; idx1++) 
432          {
433             for(idx2 = 0; idx2 < datReqPerUe->datReqTb[idx1].nmbLch; idx2++) 
434             {
435                rguDlSap->sapSts.numPduRcvd +=
436                   datReqPerUe->datReqTb[idx1].lchData[idx2].pdu.numPdu;
437             }
438          }
439       }
440
441          break;
442       case RG_RGU_SDU_DROP:
443       for(idx = 0; idx < datReq->nmbOfUeGrantPerTti; idx++)
444       {
445          RguDDatReqPerUe *datReqPerUe = &datReq->datReq[idx];
446          for (idx1 = 0; idx1 < datReqPerUe->nmbOfTbs; idx1++) 
447          {
448             for(idx2 = 0; idx2 < datReqPerUe->datReqTb[idx1].nmbLch; idx2++) 
449             {
450                rguDlSap->sapSts.numPduRcvd +=
451                   datReqPerUe->datReqTb[idx1].lchData[idx2].pdu.numPdu;
452                rguDlSap->sapSts.numPduDrop +=
453                   datReqPerUe->datReqTb[idx1].lchData[idx2].pdu.numPdu;
454             }
455          }
456       }
457
458          break;
459    }
460    
461    return;
462 } /* rgUpdtRguDedSts */
463
464
465 /***********************************************************
466  *
467  *     Func : rgUpdtRguCmnSts
468  *
469  *     Desc : Utility Function to update rgu sap statistics for common
470  *            DatReqs.
471  *            
472  *
473  *     Ret  : ROK
474  *            RFAILED
475  *
476  *     Notes: 
477  *
478  *     File : rg_utl.c
479  *
480  **********************************************************/
481 Void rgUpdtRguCmnSts
482 (
483 Inst           inst,
484 RgUpSapCb     *rguDlSap,
485 uint8_t       stsType   /* Statistics type to update */
486 )
487 {
488
489    switch(stsType)
490    {
491       case RG_RGU_SDU_RCVD:
492          rguDlSap->sapSts.numPduRcvd ++;
493          break;
494       case RG_RGU_SDU_DROP:
495          rguDlSap->sapSts.numPduRcvd ++;
496          rguDlSap->sapSts.numPduDrop ++;
497          break;
498    }
499    
500    return;
501 } /* rgUpdtRguCmnSts */
502
503
504 /***********************************************************
505  *
506  *     Func : rgUpdtCellCnt
507  *
508  *     Desc : Utility Function to update cell count. It gives number of active
509  *     cells
510  *            
511  *
512  *     Ret  : ROK
513  *            RFAILED
514  *
515  *     Notes: This function should be called only after cell is added/deleted 
516  *     from the globlal hashlist
517  *
518  *     File : rg_utl.c
519  *
520  **********************************************************/
521 Void rgUpdtCellCnt(Inst inst,uint8_t updtType)
522 {
523
524    switch (updtType)
525    {
526       case RG_CFG_ADD:
527          rgCb[inst].genSts.numCellCfg++;
528          break;
529       case RG_CFG_DEL:
530          rgCb[inst].genSts.numCellCfg--;
531          break;
532       default:
533          break;
534    }
535
536    return;
537 } /* rgUpdtCellCnt */
538
539
540 /***********************************************************
541  *
542  *     Func : rgUpdtUeCnt
543  *
544  *     Desc : Utility Function to update ue count. It gives number of active
545  *     Ues.
546  *            
547  *
548  *     Ret  : ROK
549  *            RFAILED
550  *
551  *     Notes: This function should be called only after ue is added/deleted 
552  *     from the globlal hashlist
553  *
554  *     File : rg_utl.c
555  *
556  **********************************************************/
557 Void rgUpdtUeCnt(Inst inst,uint8_t updtType)
558 {
559    switch (updtType)
560    {
561       case RG_CFG_ADD:
562          rgCb[inst].genSts.numUeCfg++;
563          break;
564       case RG_CFG_DEL:
565          rgCb[inst].genSts.numUeCfg--;
566          break;
567       default:
568          break;
569    }
570    return;
571 } /* rgUpdtUeCnt */
572
573 /*
574 *
575 *       Fun:   rgAllocEventMem
576 *
577 *       Desc:  This function allocates event memory 
578 *
579 *       Ret:   ROK      - on success
580 *              RFAILED  - on failure
581 *
582 *       Notes: None
583 *
584 *       File:  rg_utl.c
585 *
586 */
587 S16 rgAllocEventMem
588 (
589 Inst     inst,
590 Ptr       *memPtr,
591 Size      memSize
592 )
593 {
594    Mem               sMem;
595    volatile uint32_t startTime=0;
596
597
598    sMem.region = rgCb[inst].rgInit.region;
599    sMem.pool = rgCb[inst].rgInit.pool;
600
601 #if (ERRCLASS & ERRCLS_DEBUG)
602    if (memSize<= 0)
603    {
604       DU_LOG("\nERROR  -->  MAC : rgAllocEventMem(): memSize invalid\n");
605       return  (RFAILED);
606    }
607 #endif /* ERRCLASS & ERRCLS_DEBUG */
608
609
610    /*starting Task*/
611    SStartTask(&startTime, PID_MACUTL_CMALLCEVT);
612
613 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
614    MS_BUF_ADD_ALLOC_CALLER();
615 #endif /* */
616 #ifdef TFU_ALLOC_EVENT_NO_INIT
617    if(ROK != cmAllocEvntNoInit(memSize, TFU_MAX_MEMBLK_SIZE, &sMem, memPtr))
618 #else
619    if(ROK != cmAllocEvnt(memSize, TFU_MAX_MEMBLK_SIZE, &sMem, memPtr))
620 #endif /* */
621    {
622       DU_LOG("\nERROR  -->  MAC : cmAllocEvnt Failed"); 
623       return RFAILED;
624    }
625
626    /*stoping Task*/
627    SStopTask(startTime, PID_MACUTL_CMALLCEVT);
628
629    return ROK;
630 } /* end of rgAllocEventMem*/
631
632 /*
633 *
634 *       Fun:   rgGetEventMem
635 *
636 *       Desc:  This function allocates event memory 
637 *
638 *       Ret:   ROK      - on success
639 *              RFAILED  - on failure
640 *
641 *       Notes: None
642 *
643 *       File:  rg_utl.c
644 *
645 */
646 S16 rgGetEventMem
647 (
648 Inst      inst,
649 Ptr       *ptr,
650 Size      len,
651 Ptr       memCp
652 )
653 {
654    S16   ret;
655
656 #ifdef TFU_ALLOC_EVENT_NO_INIT
657    ret = cmGetMemNoInit(memCp, len, (Ptr *)ptr);
658 #else
659    ret = cmGetMem(memCp, len, (Ptr *)ptr);
660 #endif /* */
661    return (ret);
662 } /* end of rgGetEventMem*/
663
664 /***********************************************************
665  *
666  *     Func : rgGetPstToInst
667  *
668  *     Desc : Utility Function to get the pst structure to post a message to
669  *     scheduler instance
670  *            
671  *
672  *     Ret  : ROK
673  *            RFAILED
674  *
675  *     Notes: This function should be called while sending a msg from 
676  *     MAC to a scheduler instance
677  *
678  *     File : rg_utl.c
679  *
680  **********************************************************/
681 Void rgGetPstToInst
682 (
683 Pst           *pst,
684 Inst          srcInst,
685 Inst          dstInst
686 )
687 {
688
689    pst->srcEnt = rgCb[srcInst].rgInit.ent; 
690    pst->srcInst = rgCb[srcInst].rgInit.inst;
691    pst->srcProcId = rgCb[srcInst].rgInit.procId;
692    pst->region = rgCb[srcInst].rgInit.region;
693    pst->pool = rgCb[srcInst].rgInit.pool;
694
695    pst->dstProcId = rgCb[dstInst].rgInit.procId;
696    pst->dstEnt = rgCb[dstInst].rgInit.ent; 
697    pst->dstInst = dstInst;
698    pst->selector = 0;
699    pst->prior     = PRIOR0;
700    pst->intfVer   = 0;
701    pst->route   = RTESPEC;
702
703    return; 
704 } /* end of rgGetPstToInst */
705
706 /***********************************************************
707  *
708  *     Func : RgSchMacLcgRegReq
709  *
710  *     Desc : Utility Function to register the set of GBR LCG.
711  *        Invoked at the time of LCG configuration/Reconfiguration at Schedular.
712  * 
713  *     Processing Steps: 
714  *           - Fetch the ueCb using the crnti given in lcInfo            
715  *           - Store the if LCG is GBR or not.
716  *
717  *     Ret  : ROK
718  *            RFAILED
719  *
720  *     Notes: This function should be called at the time of LCG 
721  *     configuration/Reconfiguration at Schedular. 
722  *     
723  *
724  *     File : rg_utl.c
725  *
726  **********************************************************/
727 S16 RgSchMacLcgRegReq(Pst *pst,RgInfLcgRegReq *lcgRegReq)
728 {
729    Inst       inst;
730    RgCellCb   *cell = NULLP;
731    RgUeCb     *ue;
732
733    RG_IS_INST_VALID(pst->dstInst);
734    inst   = pst->dstInst - RG_INST_START;
735    cell   = rgCb[inst].cell;
736    /* Fetch the cell and then the UE */
737    if((cell == NULLP) ||
738       (cell->cellId != lcgRegReq->cellId))
739    {
740       
741       DU_LOG("\nERROR  -->  MAC : Cell does not exist ");
742       return RFAILED;
743    }
744
745    if ((ue = rgDBMGetUeCb(cell, lcgRegReq->crnti)) == NULLP)
746    {
747      DU_LOG("\nERROR  -->  MAC : CRNTI:%d does not exist", 
748                          lcgRegReq->crnti);
749       return RFAILED;
750    }
751    ue->ul.lcgArr[lcgRegReq->lcgId].isGbr = lcgRegReq->isGbr;
752
753    return ROK; 
754 } /* end of RgSchMacLcgRegReq */
755
756 #ifdef LTEMAC_SPS
757
758 /***********************************************************
759  *
760  *     Func : RgSchMacUlSpsResetReq
761  *
762  *     Desc : Utility Function to reset SPS params for a UE
763  * 
764  *     Processing Steps: 
765  *           - Fetch the ueCb using the crnti 
766  *           - reset implRelCnt and explRelCnt
767  *
768  *     Ret  : ROK
769  *            RFAILED
770  *
771  *     File : rg_utl.c
772  *
773  **********************************************************/
774 S16 RgSchMacUlSpsResetReq(Pst *pst,RgInfUlSpsReset *ulSpsResetInfo)
775 {
776    Inst       inst;
777    RgCellCb   *cell = NULLP;
778    RgUeCb     *ue;
779
780    RG_IS_INST_VALID(pst->dstInst);
781    inst   = pst->dstInst - RG_INST_START;
782    cell   = rgCb[inst].cell;
783    /* Fetch the cell and then the UE */
784    if((cell == NULLP)||
785       (cell->cellId != ulSpsResetInfo->cellId))
786    {
787       
788       DU_LOG("\nERROR  -->  MAC : Cell does not exist ");
789       return RFAILED;
790    }
791
792    if ((ue = rgDBMGetUeCb(cell, ulSpsResetInfo->crnti)) == NULLP)
793    {
794       DU_LOG("\nERROR  -->  MAC : CRNTI:%d does not exist", 
795                       ulSpsResetInfo->crnti);
796       return RFAILED;
797    }
798
799    ue->ul.implRelCntr = 0;
800    ue->ul.explRelCntr = 0;
801
802    return ROK; 
803 } /* end of RgSchMacUlSpsResetReq */
804
805
806
807
808 /***********************************************************
809  *
810  *     Func : RgSchMacSpsLcRegReq
811  *
812  *     Desc : Utility Function to register the set of uplink SPS logical
813  *        channels for a SPS UE.
814  *        Invoked at the time of activation of a UE for UL-SPS.
815  *        Whenever there is data on these LCs MAC shall inform scheduler
816  * 
817  *     Processing Steps: 
818  *           - Fetch the ueCb using the crnti given in lcInfo            
819  *           - Store the sps-rnti and set the bits corresponding to the
820  *             logical channel ids in ueUlCb->spsLcMask.
821  *
822  *     Ret  : ROK
823  *            RFAILED
824  *
825  *     Notes: This function should be called at the time UL SPS is activated 
826  *     for a UE at scheduler
827  *     
828  *
829  *     File : rg_utl.c
830  *
831  **********************************************************/
832 S16 RgSchMacSpsLcRegReq(Pst *pst,RgInfSpsLcInfo *lcInfo)
833 {
834    Inst       inst;
835    RgCellCb   *cell= NULLP;
836    RgUeCb     *ue;
837    uint8_t    idx;
838
839    RG_IS_INST_VALID(pst->dstInst);
840    inst   = pst->dstInst - RG_INST_START;
841    cell   = rgCb[inst].cell;
842    /* Fetch the cell and then the UE */
843    if((cell == NULLP) ||
844       (cell->cellId != lcInfo->cellId))
845    {
846       
847       DU_LOG("\nERROR  -->  MAC : Cell does not exist ");
848       return RFAILED;
849    }
850
851    if ((ue = rgDBMGetUeCb(cell, lcInfo->crnti)) == NULLP)
852    {
853       DU_LOG("\nERROR  -->  MAC : CRNTI:%d does not exist", 
854                       lcInfo->crnti);
855       return RFAILED;
856    }
857
858    /* Store the sps-rnti and SPS LC information in the UE */ 
859    ue->spsRnti = lcInfo->spsRnti;
860    for (idx=0; idx < lcInfo->spsLcCnt; idx++)
861    {
862       /* KWORK_FIX: Modified the index from lcId to lcId-1 for handling lcId 10 properly */
863       ue->ul.spsLcId[(lcInfo->spsLcId[idx])-1] = TRUE;
864    }
865    ue->ul.implRelCnt = lcInfo->implRelCnt;
866    ue->ul.explRelCnt = ue->ul.implRelCnt + 1; /*(lcInfo->implRelCnt * lcInfo->spsPrd);*/
867
868    /* Insert the UE into SPS UE List */
869    if (rgDBMInsSpsUeCb(cell, ue) == RFAILED)
870    {
871       DU_LOG("\nERROR  -->  MAC : Ue insertion into SPS list failed SPS CRNTI:%d", ue->spsRnti);
872       return RFAILED;
873    } 
874
875    return ROK; 
876 } /* end of RgSchMacSpsLcRegReq */
877
878
879 /***********************************************************
880  *
881  *     Func : RgSchMacSpsLcDeregReq
882  *
883  *     Desc : Utility Function to deregister the set of uplink SPS 
884  *        logical channels for a UE.
885  *        Invoked at the time of release of UL-SPS for an activated UE.
886  * 
887  *     Processing Steps: 
888  *           - Fetch the ueCb using the crnti given
889  *           - Reset the bits corresponding to the logical channel ids in
890  *             ueUlCb->spsLcMask.
891  *
892  *     Ret  : ROK
893  *            RFAILED
894  *
895  *     Notes: This function should be called at the time UL SPS is released 
896  *     for a UE at scheduler
897  *     
898  *
899  *     File : rg_utl.c
900  *
901  **********************************************************/
902 S16 RgSchMacSpsLcDeregReq(Pst  *pst,CmLteCellId cellId,CmLteRnti crnti)
903 {
904    Inst       inst;
905    RgCellCb   *cell = NULLP;
906    RgUeCb      *ue;
907
908    RG_IS_INST_VALID(pst->dstInst);
909    inst   = pst->dstInst - RG_INST_START;
910    cell   = rgCb[inst].cell;
911    /* Fetch the cell and then the UE */
912    if((cell == NULLP) ||
913       (cell->cellId != cellId))
914    {
915       
916       DU_LOG("\nERROR  -->  MAC : Cell does not exist ");
917       return RFAILED;
918    }
919
920    if ((ue = rgDBMGetUeCb(cell, crnti)) == NULLP)
921    {
922       DU_LOG("\nERROR  -->  MAC : CRNTI:%d Ue does not exist", crnti);
923       return RFAILED;
924    }
925
926    /* No need to reset the SPS LC Ids as they will not be looked at*/
927
928    /* Delete UE from the SPS UE List */
929    rgDBMDelSpsUeCb(cell, ue);
930    
931    return ROK; 
932 } /* end of RgSchMacSpsLcDeregReq */
933
934 #endif /* LTEMAC_SPS */
935
936 /**
937  * @brief Function for handling CRNTI change request 
938  * received from scheduler to MAC.
939  *
940  * @details
941  *
942  *     Function : rgUtlHndlCrntiChng
943  *     
944  *        - Delete old UE from the list.
945  *        - Update the new rnti and re-insert the UE in the list.
946  *     
947  *           
948  *  @param[in] Inst        inst
949  *  @param[in] RgCellCb      *cell,
950  *  @param[in] CmLteRnti     rnti,
951  *  @param[in] CmLteRnti     newRnti
952  *  @return  S16
953  *      -# ROK 
954  *      -# RFAILED
955  **/
956 static S16 rgUtlHndlCrntiChng 
957 (
958 Inst            inst,
959 RgCellCb        *cell,
960 CmLteRnti       rnti,
961 CmLteRnti       newRnti
962 )
963 {
964    RgUeCb         *ue = NULLP;
965    RgUeCb         *newUe = NULLP;
966
967    ue = rgDBMGetUeCb(cell, rnti);
968    newUe = rgDBMGetUeCbFromRachLst(cell, newRnti);
969    if ((ue == NULLP) || (newUe == NULLP))
970    {
971       DU_LOG("\nERROR  -->  MAC : RNTI:%d Failed to get UECB[%lu:%lu] or NEW RNTI:%d", 
972                rnti, ((PTR)ue), ((PTR)newUe), newRnti);
973       return RFAILED;
974    }
975 #ifdef XEON_SPECIFIC_CHANGES
976    DU_LOG("\nDEBUG  -->  MAC : MAC:UE[%d] id changed to %d\n", rnti, newRnti);
977 #endif
978    rgDBMDelUeCb(cell, ue);
979
980    ue->ueId = newRnti;
981
982    memcpy(&(ue->contResId), &(newUe->contResId), 
983                    sizeof(newUe->contResId));
984    /* Fix : syed MSG4 might be RETXing need to store the
985     * HARQ context. */
986    rgDHMFreeUe(inst,&ue->dl.hqEnt);
987    ue->dl.hqEnt = newUe->dl.hqEnt;
988        
989    rgDBMInsUeCb(cell, ue);
990
991    rgDBMDelUeCbFromRachLst(cell, newUe);
992    rgFreeSBuf(inst,(Data **)&newUe, sizeof(*newUe));
993
994    return ROK;
995 } /* end of rgUtlHndlCrntiChng */
996
997 #ifdef LTE_ADV
998 /**
999  * @brief Function for handling UE  release for SCELL
1000  * triggered from SCH to MAC.
1001  *
1002  * @details
1003  *
1004  *     Function : rgDelUeFrmAllSCell
1005  *     
1006  *        - This Function should be invoked by PCell of UE
1007  *        - Remove the UE context from SCELL corresponding to rnti.
1008  *           
1009  *  @param[in] Inst      *macInst,
1010  *  @param[in] RgUeCb    *ue
1011  *  @return  ROK is SUCCESS 
1012  **/
1013 S16 rgDelUeFrmAllSCell(RgCellCb *cell,RgUeCb *ue)
1014 {
1015    Inst        inst     = cell->macInst - RG_INST_START;
1016    uint8_t     idx      = 0;
1017    Inst        sCellInstIdx;
1018    Pst         dstInstPst;
1019    RgPrgUeSCellDelInfo ueSCellDelInfo;
1020
1021    /* To Delete the SCells if exisits for that UE */
1022    for(idx = 0; idx < RG_MAX_SCELL_PER_UE ; idx++)
1023    {
1024       if(TRUE == ue->sCelInfo[idx].isSCellAdded)
1025       {
1026          sCellInstIdx = ue->sCelInfo[idx].macInst - RG_INST_START;
1027
1028          rgGetPstToInst(&dstInstPst, inst, sCellInstIdx);
1029          ueSCellDelInfo.ueId = ue->ueId;
1030          ueSCellDelInfo.sCellId = ue->sCelInfo[idx].sCellId;
1031
1032          /* Filling same ueId in newRnti so that SMAC will check if newRnti
1033           *and old UeId is same that means its a UeSCell delete request*/
1034          ueSCellDelInfo.newRnti = ue->ueId;
1035
1036          RgPrgPMacSMacUeSCellDel(&dstInstPst, &ueSCellDelInfo);
1037          ue->sCelInfo[idx].isSCellAdded = FALSE;
1038       } /* loop of if */
1039    } /* loop of for */
1040
1041    return ROK;
1042 } /* rgDelUeFrmAllSCell */
1043
1044 /**
1045  * @brief Function to validate AddSCellCfg.
1046  *
1047  * @details
1048  *
1049  *     Function : rgUtlVltdAddSCellCfg
1050  *     
1051  *           
1052  *  @param[in] ueSCellCb   secondary cell CB for validation
1053  *  @param[in] cell        cell control block
1054  *  @param[in] inst        instance number to fetch rgCb instance
1055  *  @return  S16
1056  *      -# ROK 
1057  **/
1058 S16 rgUtlVltdAddSCellCfg(RgPrgUeSCellCfgInfo *ueSCellCb,RgCellCb *cell,Inst inst)
1059 {
1060   S16 ret = ROK; 
1061   
1062   /* To Validate the CellID presence */
1063   if((cell == NULLP) ||
1064         (cell->cellId != ueSCellCb->cellId))
1065   {
1066      DU_LOG("\nERROR  -->  MAC : [%d]Sec Cell does not exit %d\n",
1067               ueSCellCb->ueId, ueSCellCb->cellId);
1068      ret = RFAILED;
1069   }
1070 #ifdef TENB_MULT_CELL_SUPPRT
1071    if((ueSCellCb->rguDlSapId > rgCb[inst].numRguSaps) ||
1072       (ueSCellCb->rguUlSapId > rgCb[inst].numRguSaps))
1073    {
1074       DU_LOG("\nERROR  -->  MAC : Invald Sap Id: DL %d UL %d for ueId %d failed\n",
1075                ueSCellCb->rguDlSapId,
1076                ueSCellCb->rguUlSapId,
1077                ueSCellCb->cellId);
1078      ret = RFAILED;
1079    }
1080 #endif
1081    return (ret);
1082 } /* rgUtlVltdAddSCellCfg */
1083
1084 /**
1085  * @brief Function to build CrntiChangeReq and send to all SMACs.
1086  *
1087  * @details
1088  *
1089  *     Function : rgUtlSndCrntiChngReq2AllSMacs
1090  *     
1091  *        - This Function should be invoked by PCell of UE
1092  *        - It sends RgPrgPMacSMacUeSCellDelReq to all SMACs with newRnti sent
1093  *          by SCH. SMAC will check if newRnti is not equal to old UeId then it
1094  *          do only UeId change else it will delete the UeScell context
1095  *           
1096  *  @param[in] cell    Cell CB to get Ue control block
1097  *  @param[in] rnti    Ue Identifier used to fill in UeId Change req
1098  *  @param[in] newRnti UE new identifier, to be used in UeId Change req
1099  *  @return  S16
1100  *      -# ROK 
1101  **/
1102 static S16 rgUtlSndCrntiChngReq2AllSMacs
1103 (
1104 RgCellCb        *cell,
1105 CmLteRnti       rnti,
1106 CmLteRnti       newRnti
1107 )
1108 {
1109    Inst                inst = cell->macInst - RG_INST_START;
1110    Inst                sCellInstIdx;
1111    Pst                 dstInstPst;
1112    RgPrgUeSCellDelInfo ueIdChngReq;
1113    RgUeCb              *ue;
1114    uint8_t                  idx;
1115 #ifdef L2_OPTMZ
1116 TfuDelDatReqInfo delDatReq;
1117 #endif
1118
1119    /* use newRnti to get UeCb in PMac because rnti is already changed in PMac*/
1120    ue = rgDBMGetUeCb(cell, newRnti);
1121    if (ue == NULLP)
1122    {
1123       DU_LOG("\nERROR  -->  MAC : [%d]RNTI:Failed to get ueCb \
1124                newRnti=%d\n", rnti, newRnti);
1125       return RFAILED;
1126    }
1127    /* For all added SCells, prepare and send ueIdChngReq */
1128    for(idx = 0; idx < RG_MAX_SCELL_PER_UE ; idx++)
1129    {
1130       if(TRUE == ue->sCelInfo[idx].isSCellAdded)
1131       {
1132          sCellInstIdx = ue->sCelInfo[idx].macInst - RG_INST_START;
1133
1134          rgGetPstToInst(&dstInstPst, inst, sCellInstIdx);
1135          /* fill old rnti*/
1136          ueIdChngReq.ueId = rnti;
1137          ueIdChngReq.sCellId = ue->sCelInfo[idx].sCellId;
1138          
1139          /* Filling newRnti so that SMAC can check if old ueId and new UeId
1140           *(newRnti) is different then its a UeId change request from PMAC.
1141           * RgPrgPMacSMacUeSCellDelReq is being reused for UeId change req
1142           * from PMAC to SMAC*/
1143          ueIdChngReq.newRnti = newRnti;
1144          
1145          /* Re-using UeSCellDelReq API for UeId change*/
1146          RgPrgPMacSMacUeSCellDel(&dstInstPst, &ueIdChngReq);
1147 #ifdef L2_OPTMZ
1148       /* Sending delDatReq to CL to clear the Pdus for old UeId present in CL*/
1149       delDatReq.cellId = ueIdChngReq.sCellId;
1150       delDatReq.ueId = ueIdChngReq.ueId;
1151       rgLIMTfuDelDatReq(sCellInstIdx, &delDatReq);
1152 #endif
1153
1154       } /* loop of if */
1155    } /* loop of for */
1156
1157    return ROK;
1158 } /* rgUtlSndCrntiChngReq2AllSMacs */
1159
1160 #endif /* LTE_ADV */
1161
1162 /**
1163  * @brief Function for handling CRNTI Context release 
1164  * triggered from SCH to MAC.
1165  *
1166  * @details
1167  *
1168  *     Function : rgUtlHndlCrntiRls
1169  *     
1170  *        - Remove the UE context from MAC corresponding to rnti.
1171  *     
1172  *           
1173  *  @param[in] RgCellCb      *cell,
1174  *  @param[in] CmLteRnti     rnti
1175  *  @return  Void 
1176  **/
1177 static Void rgUtlHndlCrntiRls(RgCellCb *cell,RgInfRlsRnti *rlsRnti)
1178 {
1179    Inst           inst = cell->macInst - RG_INST_START;
1180    RgUeCb        *ue = NULLP;
1181 #ifdef LTEMAC_SPS
1182    RgUeCb         *spsUeCb = NULLP;
1183 #endif
1184
1185    if ((ue = rgDBMGetUeCb(cell, rlsRnti->rnti)) == NULLP)
1186    {
1187       /* Check in RachLst */
1188       if((ue=rgDBMGetUeCbFromRachLst (cell, rlsRnti->rnti)) != NULLP)
1189       {
1190          /* Delete Ue from the UE list */
1191          rgDBMDelUeCbFromRachLst(cell, ue);
1192
1193          /* Free Ue */
1194          rgRAMFreeUeCb(inst,ue);
1195       }
1196       else
1197       {
1198           DU_LOG("\nERROR  -->  MAC : RNTI:%d No ueCb found in RachLst",rlsRnti->rnti);
1199       }
1200    }
1201    else
1202    {
1203 #ifdef LTE_ADV
1204       if(FALSE == rlsRnti->isUeSCellDel)
1205       {
1206          rgDelUeFrmAllSCell(cell, ue);
1207       }
1208 #endif /* LTE_ADV */
1209
1210       /* Delete Ue from the UE list */
1211       rgDBMDelUeCb(cell, ue);
1212 #ifdef LTEMAC_SPS
1213       spsUeCb = rgDBMGetSpsUeCb (cell, ue->spsRnti);
1214       if (spsUeCb)
1215       {
1216          rgDBMDelSpsUeCb(cell, spsUeCb);
1217       }
1218 #endif
1219
1220       /* Free Ue */
1221       rgCFGFreeUeCb(cell, ue);     
1222       /* MS_REMOVE : syed Check in RachLst */
1223       {
1224          if((ue=rgDBMGetUeCbFromRachLst (cell, rlsRnti->rnti)) != NULLP)
1225          {
1226              DU_LOG("\nERROR  -->  MAC : RNTI:%d STALE UE is still present", rlsRnti->rnti);         
1227          }
1228       }
1229    }
1230
1231    return;
1232 } /* end of rgUtlHndlCrntiRls */
1233
1234 /**
1235  * @brief Function for handling RaResp request received from scheduler to MAC.
1236  *
1237  * @details
1238  *
1239  *     Function : RgSchMacRlsRntiReq
1240  *     
1241  *     This function shall be invoked whenever scheduler is done with the
1242  *     allocations of random access responses for a subframe.
1243  *     This shall invoke RAM to create ueCbs for all the rapIds allocated and 
1244  *     shall invoke MUX to create RAR PDUs for raRntis allocated.
1245  *     
1246  *           
1247  *  @param[in] CmLteCellId         cellId,
1248  *  @param[in] CmLteTimingInfo     timingInfo,
1249  *  @param[in] RaRespInfo          *rarInfo
1250  *  @return  S16
1251  *      -# ROK 
1252  **/
1253 S16 RgSchMacRlsRntiReq(Pst *pst,RgInfRlsRnti *rlsRnti)
1254 {
1255 //   Pst            schPst;
1256 //   RgInfUeDelInd  ueDelInd;
1257    Inst           macInst;
1258    RgCellCb       *cell;
1259 #ifdef L2_OPTMZ
1260 TfuDelDatReqInfo delDatReq;
1261 #endif
1262
1263    RG_IS_INST_VALID(pst->dstInst);
1264    macInst   = pst->dstInst - RG_INST_START;
1265    cell   = rgCb[macInst].cell;
1266
1267    if(NULLP == rlsRnti)
1268    {
1269       return RFAILED;
1270    }
1271
1272    if((cell == NULLP) ||
1273       (cell->cellId != rlsRnti->cellId))
1274    {
1275       
1276       DU_LOG("\nERROR  -->  MAC : No cellCb found with cellId for RNTI:%d", 
1277                          rlsRnti->rnti);
1278       return RFAILED;
1279    }
1280    /* Fix : syed Clearing UE context when SCH indicates to do so
1281     * UE DEL from CRG interface is now dummy. */
1282    if (rlsRnti->ueIdChng)
1283    {
1284       /* Fix : syed ueId change as part of reestablishment.
1285        * Now SCH to trigger this. CRG ueRecfg for ueId change 
1286        * is dummy */       
1287       if (rgUtlHndlCrntiChng(macInst,cell, rlsRnti->rnti, rlsRnti->newRnti) != ROK)        
1288       {
1289           DU_LOG("\nERROR  -->  MAC : CRNTI change failed for RNTI:%d new RNTI:%d",
1290                    rlsRnti->rnti,rlsRnti->newRnti);
1291          return RFAILED;
1292       }
1293
1294 #ifdef LTE_ADV
1295       /*PMAC_Reest: Prepare CrntiChngReq and then send to all SMACs to change
1296        *rnti in all Scells
1297        */
1298       if(rgUtlSndCrntiChngReq2AllSMacs(cell, rlsRnti->rnti, rlsRnti->newRnti) != ROK)
1299       {
1300          /* TODO: do we need to send DelInd to SCH in failure case*/ 
1301          return RFAILED;
1302       }
1303 #endif
1304 #ifdef L2_OPTMZ
1305       /* Sending delDatReq to CL to clear the Pdus for old UeId present in CL*/
1306       delDatReq.cellId = cell->cellId;
1307       delDatReq.ueId = rlsRnti->rnti;
1308       rgLIMTfuDelDatReq(macInst, &delDatReq);
1309 #endif
1310    }
1311    else
1312    {
1313       rgUtlHndlCrntiRls(cell, rlsRnti);
1314    }
1315    /* Fix : syed Send delete confirmation to SCH */
1316    /* Send RgMacSchUeDelInd to SCH only if it is Rnti release to PMAC.
1317     * Basically dont send DelInd to SCH incase of Ue SCell Del*/
1318 #ifdef LTE_ADV
1319    if(FALSE == rlsRnti->isUeSCellDel)
1320 #endif
1321    {
1322       //TODO: commented for compilation without SCH 
1323 #if 0
1324       ueDelInd.cellSapId  = cell->schInstMap.cellSapId;
1325       ueDelInd.cellId  = rlsRnti->cellId;
1326       ueDelInd.rnti    = rlsRnti->rnti; 
1327       rgGetPstToInst(&schPst,macInst, cell->schInstMap.schInst);
1328       RgMacSchUeDel(&schPst, &ueDelInd);
1329 #endif
1330    }
1331
1332    return ROK;
1333 } /* end of RgSchMacRlsRntiReq */
1334
1335 #ifdef L2_OPTMZ
1336 Bool RgUtlIsTbMuxed(TfuDatReqTbInfo *tb)
1337 {
1338    MsgLen len = 0;
1339    SFndLenMsg(tb->macHdr, &len);
1340    return (len?TRUE : FALSE);
1341 }
1342 #endif
1343
1344 /**********************************************************************
1345  
1346          End of file
1347 **********************************************************************/