e426f8f9761668205d4ad9cc7cc28575cc91f39d
[o-du/l2.git] / src / 5gnrmac / rg_lim.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_lim.c 
28   
29 **********************************************************************/
30
31 /** @file rg_lim.c.
32 @brief It has APIs exposed by Lower Interface Modulue of MAC. It acts as an 
33 Interface handler for lower interface APIs.
34 */
35 /* header include files -- defines (.h) */
36 #include "common_def.h"
37 #include "rgu.h"           /* RGU defines */
38 #include "tfu.h"           /* RGU defines */
39 #include "lrg.h"           /* layer management defines for LTE-MAC */
40 #include "crg.h"           /* layer management defines for LTE-MAC */
41 #include "rg_sch_inf.h"           /* layer management defines for LTE-MAC */
42 #include "rg_env.h"        /* customisable defines and macros for MAC */
43 #include "rg.h"            /* defines and macros for MAC */
44
45 /* header/extern include files (.x) */
46 #include "rgu.x"           /* RGU types */
47 #include "tfu.x"           /* RGU types */
48 #include "lrg.x"           /* layer management typedefs for MAC */
49 #include "crg.x"           /* layer management typedefs for MAC */
50 #include "rg_sch_inf.x"    /* SCH interface typedefs */
51 #include "rg_prg.x"    /* PRG interface typedefs */
52 #include "rg.x"            /* typedefs for MAC */
53
54 /* local externs */
55 #ifdef UNUSED_FUNC
56 static S16  rgLIMValidateSap ARGS((Inst inst,SuId suId));
57 #endif
58 static Void rgLIMUtlFreeDatIndEvnt ARGS((TfuDatIndInfo *datInd,
59                                           Bool error));
60 #ifdef RG_UNUSED
61 static Void rgLIMUtlFreeDatReqEvnt ARGS((TfuDatReqInfo *datReq,
62                                           Bool error));
63 #endif
64 /* forward references */
65
66 /**
67  * @brief Bind confirm API for TFU SAP 
68  *
69  * @details
70  *
71  *     Function : RgLiTfuBndCfm
72  *      
73  *      This API is invoked by PHY to confirm TFU SAP bind. 
74  *     
75  *           
76  *  @param[in]  Pst   *pst 
77  *  @param[in]  SuId  suId 
78  *  @param[in]  uint8_t    status
79  *  @return  S16
80  *      -# ROK 
81  *      -# RFAILED 
82  **/
83 S16 RgLiTfuBndCfm 
84 (
85 Pst     *pst,
86 SuId    suId, 
87 uint8_t status
88 )
89 {
90    Inst inst;
91    S16 ret;
92    RgLowSapCb  *tfuSap;
93
94
95
96    RG_IS_INST_VALID(pst->dstInst);
97    inst = pst->dstInst - RG_INST_START;
98    /* Lets validate suId first */
99    /* CA_Change */
100    tfuSap = &(rgCb[inst].tfuSap);
101
102    if (suId != tfuSap->sapCfg.suId)
103    {
104       DU_LOG("\nERROR  -->  MAC : Incorrect SuId. Configured (%d) Recieved (%d)",
105             tfuSap->sapCfg.suId, suId);
106       return RFAILED;
107    }
108    ret = rgLMMBndCfm (pst, suId, status);
109    return (ret);
110 }  /* RgLiTfuBndCfm */
111
112  /** @brief This function Validates the SAP information received along with the
113   * primitive from the lower layer. 
114   * Function:
115   *   Validates SAP information.
116  *  @param[in]  Inst        inst
117   * @param  suId The SAP Id
118   * @return 
119   *   -# ROK
120   *   -# RFAILED
121   */
122 #ifdef UNUSED_FUNC
123 static S16 rgLIMValidateSap(Inst  inst,SuId suId)
124 {
125    RgLowSapCb  *tfuSap;
126
127
128    tfuSap = &(rgCb[inst].tfuSap);
129
130    /* First lets check the suId */
131    if( suId != tfuSap->sapCfg.suId)
132    {
133       DU_LOG("\nERROR  -->  MAC : Incorrect SuId. Configured (%d) Recieved (%d)",
134             tfuSap->sapCfg.suId, suId);
135       return RFAILED;
136    }
137    if (tfuSap->sapSta.sapState != LRG_BND)
138    {
139       DU_LOG("\nERROR  -->  MAC : Lower SAP not enabled SuId (%d)",
140             tfuSap->sapCfg.suId);
141       return RFAILED;
142    }
143    return ROK;
144 } /* end of rgLIMValidateSap */
145 #endif
146 /** @brief This function frees up the TfuDatIndInfo structure
147  *
148  * @details
149  *
150  *     Function: rgLIMUtlFreeDatIndEvnt 
151  *       - Function frees up the TfuDatIndInfo structure, in case of error it shall
152  *       free up the buffer's present in the datIndLst.
153  *
154  *         Processing steps:
155  * @param  [in] TfuDatIndInfo *datInd
156  * @param  [in] Bool          *error
157  * @return 
158  */
159 static Void rgLIMUtlFreeDatIndEvnt(TfuDatIndInfo *datInd,Bool error)
160 {
161
162    TfuDatInfo     *datInfo;
163    CmLList        *node;
164
165    /* Steps of freeing up the TfuDatInd.
166     * 1. loop through the datIndLst and free up all the buffers.
167     * 2. free up the whole event
168     */
169    if ((error == TRUE) && (datInd->datIndLst.count > 0))
170    {
171       node =  datInd->datIndLst.first;
172       while (node)
173       {
174          datInfo = (TfuDatInfo*)node->node;
175          RG_FREE_MSG(datInfo->mBuf);
176          node = node->next;
177       }
178    }
179    RG_FREE_MEM(datInd);
180    return;
181 } /* end of rgLIMUtlFreeDatIndEvnt*/
182
183 /**
184  * @brief Downlink data indication from PHY.
185  *
186  * @details
187  *
188  *     Function : RgLiTfuDatInd
189  *      
190  *      This API is invoked by PHY to send data indication to MAC on 
191  *      recieving data from UEs.
192  *           
193  *  @param[in]  Pst              *pst
194  *  @param[in]  SuId             suId 
195  *  @param[in]  TfuDatIndInfo    *datInd
196  *  @return  S16
197  *      -# ROK 
198  *      -# RFAILED 
199  **/
200 S16 RgLiTfuDatInd
201 (
202 Pst                *pst, 
203 SuId               suId, 
204 TfuDatIndInfo    *datInd
205 )
206 {
207    Inst             inst;
208    S16              ret;
209    volatile uint32_t     startTime=0;
210
211
212   // DU_LOG("5GTF:: DatindRcvd\n");
213
214    RG_IS_INST_VALID(pst->dstInst);
215    inst = pst->dstInst - RG_INST_START;
216    /*starting Task*/
217    SStartTask(&startTime, PID_MAC_TFU_DATIND);
218
219 #ifndef NO_ERRCLS 
220    if ((ret = rgLIMValidateSap (inst,suId)) != ROK)
221    {
222       DU_LOG("\nERROR  -->  MAC : SAP Validation failed");
223       rgLIMUtlFreeDatIndEvnt(datInd, TRUE);
224       return (ret);
225    }
226 #endif
227    /* Now call the TOM (Tfu ownership module) primitive to process further */
228    rgCb[inst].tfuSap.sapSts.numPduRcvd += 
229                     datInd->datIndLst.count;
230    ret = rgTOMDatInd(inst,datInd);
231    /* Fix: sriky memory corruption precautions */
232    /* Free up the memory for the request structure */
233    if (ret == ROK)
234    {
235       rgLIMUtlFreeDatIndEvnt(datInd, FALSE);
236    }
237    else
238    {
239       rgLIMUtlFreeDatIndEvnt(datInd, TRUE);
240    }
241
242    /*stoping Task*/
243    SStopTask(startTime, PID_MAC_TFU_DATIND);
244
245    return (ret);
246 }  /* RgLiTfuDatInd*/
247
248 #ifdef RG_UNUSED
249 /** @brief This function frees up the TfuDatReqInfo structure.
250  *
251  * @details
252  *
253  *     Function: rgLIMUtlFreeDatReqEvnt
254  *       - Function frees up the TfuDatReqInfo structure, in case of error it shall
255  *       free up the buffer's present in the PDUs list.
256  *
257  *         Processing steps:
258  * @param  [in] TfuDatReqInfo *datReq
259  * @param  [in] Bool          *error
260  * @return 
261  */
262 static Void rgLIMUtlFreeDatReqEvnt(TfuDatReqInfo *datReq,Bool  error)
263 {
264
265    TfuDatReqPduInfo *datInfo;
266    CmLList          *node;
267    uint8_t          i;
268
269    /* Steps of freeing up the TfuDatReq.
270     * 1. Free the bch buffer.
271     * 2. loop through the pdus list and free up all the buffers.
272     * 3. free up the whole event
273     */
274    if (error)
275    {
276       if (datReq->bchDat.pres == PRSNT_NODEF)
277       {
278          RG_FREE_MSG(datReq->bchDat.val);
279       }
280       if (datReq->pdus.count > 0)
281       {
282          node =  datReq->pdus.first;
283          while (node)
284          {
285             datInfo = (TfuDatReqPduInfo*)node->node;
286             for (i=0; i<datInfo->nmbOfTBs; i++)
287             {
288                if (datInfo->mBuf[i] != NULLP)
289                {
290                   RG_FREE_MSG(datInfo->mBuf[i]);
291                }
292             }
293             node = node->next;
294          }
295       }
296    }
297    RG_FREE_MEM(datReq);
298    return;
299 } /* end of rgLIMUtlFreeDatReqEvnt*/
300 #endif
301
302 #ifdef L2_OPTMZ
303 /**
304  * @brief This API is invoked to send Data to PHY.
305  *
306  * @details
307  *
308  *     Function : rgLIMTfuDelDatReq
309  *      
310  *      This API is invoked to send Data to PHY. It 
311  *      fills in the Pst structure, spId value and invokes Data
312  *      request primitive at TFU.
313  *           
314  *  @param[in]  Inst        inst
315  *  @param[in]  TfuDelDatReqInfo *datReq
316  *  @return  S16
317  *      -# ROK 
318  *      -# RFAILED 
319  **/
320 S16 rgLIMTfuDelDatReq(Inst inst,TfuDelDatReqInfo *delDatReq)
321 {
322    S16         ret;
323    RgLowSapCb  *tfuSap;
324
325
326    /* Get the lower SAP control block from the layer control block. */
327    tfuSap = &(rgCb[inst].tfuSap);
328
329 #ifndef NO_ERRCLS
330    if (tfuSap->sapSta.sapState != LRG_BND)
331    {
332       DU_LOG("\nERROR  -->  MAC : Lower SAP not bound (%d)",
333             tfuSap->sapSta.sapState);
334       return RFAILED;
335    }
336 #endif
337
338    if((ret = RgLiTfuDelDatReq (&tfuSap->sapCfg.sapPst, tfuSap->sapCfg.spId, 
339                             delDatReq)) != ROK)
340    {
341       DU_LOG("\nERROR  -->  MAC : Call to RgLiTfuDelDatReq() failed");
342    }
343    return (ret);
344 }  /* rgLIMTfuDatReq*/
345 #endif /*L2_OPTMZ */
346
347 #if defined(TENB_T2K3K_SPECIFIC_CHANGES) && defined(LTE_TDD)
348  /**
349  * @brief Transmission of non-rt indication from CL.
350  *
351  * @details
352  *
353  *     Function : RgLiTfuNonRtInd 
354  *      
355  *      This API is invoked by CL to indicate non-rt processing indication to MAC for a cell.
356  *           
357  *  @param[in]  Pst            *pst
358  *  @param[in]  SuId           suId 
359  *  @return  S16
360  *      -# ROK 
361  *      -# RFAILED 
362  **/
363 S16 RgLiTfuNonRtInd(Pst  *pst,SuId  suId)
364 {
365
366 #ifdef NO_ERRCLS
367    if (rgLIMValidateSap (pst->dstInst - RG_INST_START, suId) != ROK)
368    {
369       DU_LOG("\nERROR  -->  MAC : RgLiTfuNonRtInd() SAP Validation failed.\n");
370       return RFAILED;
371    }
372 #endif
373    rgDHMFreeTbBufs(pst->dstInst - RG_INST_START);
374    return ROK;
375 }  /* RgLiTfuNonRtInd */
376
377 #endif
378 /**********************************************************************
379  
380          End of file
381 **********************************************************************/