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