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