[Epic-ID: ODUHIGH-464][Task-ID: ODUHIGH-483] Memeory related fix in FDD and TDD mode
[o-du/l2.git] / src / 5gnrmac / rg_ram.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_ram.c
28   
29 **********************************************************************/
30
31 /** @file rg_ram.c
32 @brief This file has APIs to handle the random access procedure functionality.
33 */
34
35 /* header include files (.h) */
36 #include "common_def.h"
37 #include "rg_env.h"        /* MAC Environment Defines */
38 #include "crg.h"           /* CRG Interface defines */
39 #include "rgu.h"           /* RGU Interface defines */
40 #include "tfu.h"           /* TFU Interface defines */
41 #include "rg_sch_inf.h"           /* RGR Interface defines */
42 #include "lrg.h"           /* LRG Interface defines */
43
44 #include "rg.h"            /* MAC defines */
45 #include "rg_err.h"        /* MAC error defines */
46
47 /* header/extern include files (.x) */
48
49 #include "crg.x"           /* CRG Interface includes */
50 #include "rgu.x"           /* RGU Interface includes */
51 #include "tfu.x"           /* TFU Interface includes */
52 #include "rg_sch_inf.x"    /* SCH Interface includes */
53 #include "rg_prg.x"        /* PRG Interface includes */
54 #include "lrg.x"           /* LRG Interface includes */
55
56 #include "du_app_mac_inf.h"
57 #include "rg.x"            /* MAC includes */
58
59 /* local defines */
60
61 /* local typedefs */
62  
63 /* forward references */
64
65 /***********************************************************
66  *
67  *     Func : rgRAMFreeUeCb
68  *
69  *
70  *     Desc :
71  *     - Processing Steps:
72  *        - Frees UE control block.
73  *
74  *     Ret  : Void
75  *
76  *     Notes:
77  *
78  *     File :
79  *
80  **********************************************************/
81 Void rgRAMFreeUeCb(Inst  inst,RgUeCb *ue)
82 {
83    rgDHMFreeUe(inst,&ue->dl.hqEnt);
84
85    /*ccpu00117052 - MOD - Passing double pointer for proper NULLP 
86                           assignment */
87    /* De-allocate the Ue */
88    rgFreeSBuf(inst,(Data **)&ue, sizeof(*ue));
89
90    /* Stack Crash problem for TRACE5 changes. Added the return below */
91    return;
92
93 }  /* rgRAMFreeUeCb */
94
95 /**
96  * @brief Handler for Random Access control block creation.
97  *
98  * @details
99  *
100  *     Function : rgRAMCreateUeCb
101  *                Creates a raCb and gives the same to scheduler for its updation.
102  *     
103  *
104  *  @param[in]       RgCellCb       *cell 
105  *  @param[in]       CmLteRnti      tmpCrnti 
106  *  @param[out]      RgErrInfo      *err
107  *  @return  RgUeCb*
108  **/
109 RgUeCb* rgRAMCreateUeCb(RgCellCb *cell,CmLteRnti  tmpCrnti,Bool insert,RgErrInfo *err)
110 {
111    Inst       inst = cell->macInst - RG_INST_START;
112    RgUeCb    *ueCb = NULLP;
113
114    DU_LOG("\nINFO  -->  MAC : CREATE UECB FOR CRNTI:%d",
115              tmpCrnti);
116    /* Allocate the Ue control block */
117    if (rgAllocSBuf(inst,(Data **)&ueCb, sizeof(*ueCb)) != ROK)
118    {
119       DU_LOG("\nERROR  -->  MAC : Memory allocation FAILED for CRNTI:%d",tmpCrnti);
120       err->errCause = RGERR_RAM_MEM_EXHAUST;
121       return (NULLP);
122    }
123
124    /* Inititialize Ue control block */
125    ueCb->ueId = tmpCrnti;
126
127    /* Initialize the lists of the UE */
128    rgDBMInitUe(ueCb); 
129    
130    if(insert == TRUE)
131    {
132       /* MS_FIX : Remove stale UEs if present */
133       RgUeCb *staleUe = NULLP;
134       /* Insert the created raCb into raCb list of cell */
135       ueCb->rachLstEnt.next = NULLP;
136       ueCb->rachLstEnt.prev = NULLP;
137       ueCb->rachLstEnt.node = (PTR)(ueCb);
138       /* MS_FIX : Remove stale UEs if present */
139       staleUe = rgDBMGetUeCbFromRachLst (cell, tmpCrnti);
140       if (staleUe)
141       {
142          rgDBMDelUeCbFromRachLst(cell, staleUe);
143          rgRAMFreeUeCb(inst,staleUe);
144       }
145       rgDBMInsUeCbInRachLst(cell, ueCb);
146    }
147
148    return (ueCb);
149 }  /* rgRAMCreateUeCb */
150
151 /**
152  * @brief Function for handling cell delete.
153  *
154  * @details
155  *
156  *     Function : rgRAMFreeCell
157  *     
158  *     This function shall be invoked whenever a cell needs to be deleted.
159  *     This shall remove raCbs and raReqs stored in cell.
160  *     
161  *           
162  *  @param[in,out] RgCellCb  *cell
163  *  @return  S16
164  *      -# ROK 
165  **/
166 S16 rgRAMFreeCell(RgCellCb *cell)
167 {
168    Inst    inst = cell->macInst - RG_INST_START;
169    RgUeCb  *ueCb;
170
171    /* Free CURRENT CRG cfg list */
172    while ((ueCb = rgDBMGetNextUeCbFromRachLst(cell, NULLP)) != NULLP)
173    {
174       rgDBMDelUeCbFromRachLst(cell, ueCb);
175       rgRAMFreeUeCb(inst,ueCb);
176    }
177
178    return ROK; 
179 } /* rgRAMFreeCell */
180 /**
181  * @brief Function for handling RA response scheduled for a subframe.
182  *
183  * @details
184  *
185  *     Function : rgHndlRaResp
186  *     
187  *     This function shall be invoked whenever scheduler is done with the
188  *     allocations of random access responses for a subframe RgSchMacSfAllocReq.
189  *
190  *     Processing steps :
191  *
192  *     This shall invoke RAM to create ueCbs for all the rapIds allocated and 
193  *     shall invoke MUX to create RAR PDUs for raRntis allocated.
194  *     
195  *           
196  *  @param[in] RgCellCb          *cell,
197  *  @param[in] CmLteTimingInfo   timingInfo,
198  *  @param[in] RgInfRarInfo      *rarInfo
199  *  @param[in/out] RgErrInfo     *err
200  *  @return  S16
201  *      -# ROK 
202  **/
203 S16 rgHndlRaResp(RgCellCb *cell,CmLteTimingInfo timingInfo,RgInfRarInfo *rarInfo,RgErrInfo *err)
204 {
205    uint8_t  idx1,idx2;
206    Buffer   *rarPdu;
207    RgDlSf   *dlSf;
208    uint8_t  idx;
209
210    if(NULLP == rarInfo->raRntiInfo)
211    {
212       return RFAILED;
213    }
214
215    idx = (timingInfo.slot % RG_NUM_SUB_FRAMES);
216    dlSf = &cell->subFrms[idx];
217
218    /* Create RAR PDUs for all the allocated RA-RNTIs */
219    for(idx1 = 0; idx1 < rarInfo->numRaRntis; idx1++)
220    {
221       if(ROK == (rgMUXBldRarPdu(cell, 
222                      &rarInfo->raRntiInfo[idx1], &rarPdu, err)))
223       {
224          /* Create RaCbs for all the rapIds allocated */
225          for(idx2 = 0; idx2 < rarInfo->raRntiInfo[idx1].numCrnti; idx2++)
226          {
227             if(FALSE == rarInfo->raRntiInfo[idx1].crntiInfo[idx2].isContFree)
228             {
229                if(rgRAMCreateUeCb(cell,
230                   rarInfo->raRntiInfo[idx1].crntiInfo[idx2].tmpCrnti, 
231                   TRUE, err) == NULLP)
232                {
233                   return RFAILED;
234                }
235             }
236          }
237          /* Store the created RAR PDU */
238          dlSf->raRsp[dlSf->numRaRsp].pdcch.rnti = 
239             rarInfo->raRntiInfo[idx1].raRnti;
240
241          dlSf->raRsp[dlSf->numRaRsp].pdcch.dci = 
242             rarInfo->raRntiInfo[idx1].dciInfo;
243
244          dlSf->raRsp[dlSf->numRaRsp].rar = rarPdu;
245          /* ccpu00132314-ADD-Adding txPower offset for the PDSCH transmission */
246          dlSf->raRsp[dlSf->numRaRsp].txPwrOffset =
247                rarInfo->txPwrOffset;
248
249          dlSf->numRaRsp++;
250       }
251       else
252       {
253          DU_LOG("\nERROR  -->  MAC : RARNTI:%d Creation of RAR"
254                   "PDU for failed", rarInfo->raRntiInfo[idx1].raRnti);
255          continue;
256       }
257    } /* end of raRntis loop */
258    return ROK;
259 } /* end of rgHndlRaResp */
260
261 /**********************************************************************
262  
263          End of file
264 **********************************************************************/