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