Merge "replaced cmMemSet, cmMemcpy with memset and memcpy resp AND Removed TRC()...
[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 static const char* RLOG_MODULE_NAME="MAC";
36 static int RLOG_FILE_ID=182;
37 static int RLOG_MODULE_ID=4096;
38 /* header include files -- defines (.h) */
39 #include "common_def.h"
40 #include "rgu.h"           /* RGU defines */
41 #include "tfu.h"           /* RGU defines */
42 #include "lrg.h"           /* layer management defines for LTE-MAC */
43 #include "crg.h"           /* layer management defines for LTE-MAC */
44 #include "rg_sch_inf.h"           /* layer management defines for LTE-MAC */
45 #include "rg_env.h"        /* customisable defines and macros for MAC */
46 #include "rg.h"            /* defines and macros for MAC */
47
48 /* header/extern include files (.x) */
49 #include "rgu.x"           /* RGU types */
50 #include "tfu.x"           /* RGU types */
51 #include "lrg.x"           /* layer management typedefs for MAC */
52 #include "crg.x"           /* layer management typedefs for MAC */
53 #include "rg_sch_inf.x"    /* SCH interface typedefs */
54 #include "rg_prg.x"    /* PRG interface typedefs */
55 #include "rg.x"            /* typedefs for MAC */
56
57 /* local externs */
58 #ifdef UNUSED_FUNC
59 PRIVATE S16  rgLIMValidateSap ARGS((Inst inst,SuId suId));
60 #endif
61 PRIVATE Void rgLIMUtlFreeDatIndEvnt ARGS((TfuDatIndInfo *datInd,
62                                           Bool error));
63 #ifdef RG_UNUSED
64 PRIVATE Void rgLIMUtlFreeDatReqEvnt ARGS((TfuDatReqInfo *datReq,
65                                           Bool error));
66 #endif
67 /* forward references */
68
69 /**
70  * @brief This API is invoked to send TFU SAP bind request to PHY.
71  *
72  * @details
73  *
74  *     Function : rgLIMTfuBndReq
75  *      
76  *      This API is invoked to send TFU SAP bind request to PHY. It fills in 
77  *      the Pst structure, spId and suId values and invokes bind request
78  *      primitive at TFU.
79  *           
80  *  @param[in]  Inst        inst
81  *  @param[in]  SuId            suId 
82  *  @param[in]  SpId            spId
83  *  @return  S16
84  *      -# ROK 
85  *      -# RFAILED 
86  **/
87 #ifdef ANSI
88 S16 rgLIMTfuBndReq
89 (
90 Inst    inst,
91 SuId    suId, 
92 SpId    spId
93 )
94 #else
95 S16 rgLIMTfuBndReq(inst,suId, spId)
96 Inst    inst;
97 SuId    suId; 
98 SpId    spId;
99 #endif
100 {
101    S16         ret;
102    RgLowSapCb  *tfuSap;
103    Pst         pst;
104
105
106    /* Get the lower SAP control block from the layer control block. */
107    tfuSap = &(rgCb[inst].tfuSap);
108    memcpy (&pst, &(tfuSap->sapCfg.sapPst), sizeof(Pst));
109    if((ret = RgLiTfuBndReq (&pst, suId, spId)) != ROK)
110    {
111       RLOG0(L_ERROR,"Call to RgLiTfuBndReq() failed");
112    }
113    return (ret);
114 }  /* rgLIMTfuBndReq */
115
116
117 /**
118  * @brief This API is invoked to send TFU SAP unbind request to PHY.
119  *
120  * @details
121  *
122  *     Function : rgLIMTfuBndReq
123  *      
124  *      This API is invoked to send TFU SAP unbind request to PHY. It fills in 
125  *      the Pst structure and spId value and invokes unbind request
126  *      primitive at TFU.
127  *           
128  *  @param[in]  Inst        inst
129  *  @param[in]  SpId            spId
130  *  @param[in]  Reason          reason 
131  *  @return  S16
132  *      -# ROK 
133  *      -# RFAILED 
134  **/
135 #ifdef ANSI
136 S16 rgLIMTfuUbndReq
137 (
138 Inst    inst,
139 SpId    spId, 
140 Reason  reason
141 )
142 #else
143 S16 rgLIMTfuUbndReq(inst,spId, reason)
144 Inst    inst;
145 SpId    spId; 
146 Reason  reason;
147 #endif
148 {
149    S16         ret;
150    RgLowSapCb  *tfuSap;
151    Pst         pst;
152
153
154    /* Get the lower SAP control block from the layer control block. */
155    tfuSap = &(rgCb[inst].tfuSap);
156    memcpy (&pst, &(tfuSap->sapCfg.sapPst), sizeof(Pst));
157    if((ret = RgLiTfuUbndReq (&pst, tfuSap->sapCfg.spId, reason)) != ROK)
158    {
159       RLOG0(L_ERROR,"Call to RgLiTfuUbndReq() failed");
160    }
161    return (ret);
162
163 }  /* rgLIMTfuUbndReq */
164
165
166 /**
167  * @brief Bind confirm API for TFU SAP 
168  *
169  * @details
170  *
171  *     Function : RgLiTfuBndCfm
172  *      
173  *      This API is invoked by PHY to confirm TFU SAP bind. 
174  *     
175  *           
176  *  @param[in]  Pst   *pst 
177  *  @param[in]  SuId  suId 
178  *  @param[in]  U8    status
179  *  @return  S16
180  *      -# ROK 
181  *      -# RFAILED 
182  **/
183 #ifdef ANSI
184 S16 RgLiTfuBndCfm 
185 (
186 Pst     *pst,
187 SuId    suId, 
188 U8      status
189 )
190 #else
191 S16 RgLiTfuBndCfm(pst, suId, status)
192 Pst     *pst; 
193 SuId    suId; 
194 U8      status;
195 #endif
196 {
197    Inst inst;
198    S16 ret;
199    RgLowSapCb  *tfuSap;
200
201
202
203    RG_IS_INST_VALID(pst->dstInst);
204    inst = pst->dstInst - RG_INST_START;
205    /* Lets validate suId first */
206    /* CA_Change */
207    tfuSap = &(rgCb[inst].tfuSap);
208
209    if (suId != tfuSap->sapCfg.suId)
210    {
211       RLOG2(L_ERROR,"Incorrect SuId. Configured (%d) Recieved (%d)",
212             tfuSap->sapCfg.suId, suId);
213       return RFAILED;
214    }
215    ret = rgLMMBndCfm (pst, suId, status);
216    return (ret);
217 }  /* RgLiTfuBndCfm */
218
219  /** @brief This function Validates the SAP information received along with the
220   * primitive from the lower layer. 
221   * Function:
222   *   Validates SAP information.
223  *  @param[in]  Inst        inst
224   * @param  suId The SAP Id
225   * @return 
226   *   -# ROK
227   *   -# RFAILED
228   */
229 #ifdef UNUSED_FUNC
230 #ifdef ANSI
231 PRIVATE S16 rgLIMValidateSap
232 (
233  Inst    inst,
234  SuId    suId
235 )
236 #else
237 PRIVATE S16 rgLIMValidateSap(inst,suId)
238  Inst    inst;
239  SuId    suId;
240 #endif
241 {
242    RgLowSapCb  *tfuSap;
243
244
245    tfuSap = &(rgCb[inst].tfuSap);
246
247    /* First lets check the suId */
248    if( suId != tfuSap->sapCfg.suId)
249    {
250       RLOG2(L_ERROR,"Incorrect SuId. Configured (%d) Recieved (%d)",
251             tfuSap->sapCfg.suId, suId);
252       return RFAILED;
253    }
254    if (tfuSap->sapSta.sapState != LRG_BND)
255    {
256       RLOG1(L_ERROR,"Lower SAP not enabled SuId (%d)",
257             tfuSap->sapCfg.suId);
258       return RFAILED;
259    }
260    return ROK;
261 } /* end of rgLIMValidateSap */
262 #endif
263 /** @brief This function frees up the TfuDatIndInfo structure
264  *
265  * @details
266  *
267  *     Function: rgLIMUtlFreeDatIndEvnt 
268  *       - Function frees up the TfuDatIndInfo structure, in case of error it shall
269  *       free up the buffer's present in the datIndLst.
270  *
271  *         Processing steps:
272  * @param  [in] TfuDatIndInfo *datInd
273  * @param  [in] Bool          *error
274  * @return 
275  */
276 #ifdef ANSI
277 PRIVATE Void rgLIMUtlFreeDatIndEvnt 
278 (
279  TfuDatIndInfo *datInd,
280  Bool          error
281  )
282 #else
283 PRIVATE Void rgLIMUtlFreeDatIndEvnt(datInd, error)
284  TfuDatIndInfo *datInd;
285  Bool          error;
286 #endif
287 {
288
289    TfuDatInfo     *datInfo;
290    CmLList        *node;
291
292    /* Steps of freeing up the TfuDatInd.
293     * 1. loop through the datIndLst and free up all the buffers.
294     * 2. free up the whole event
295     */
296    if ((error == TRUE) && (datInd->datIndLst.count > 0))
297    {
298       node =  datInd->datIndLst.first;
299       while (node)
300       {
301          datInfo = (TfuDatInfo*)node->node;
302          RG_FREE_MSG(datInfo->mBuf);
303          node = node->next;
304       }
305    }
306    RG_FREE_MEM(datInd);
307    RETVOID;
308 } /* end of rgLIMUtlFreeDatIndEvnt*/
309
310 /**
311  * @brief Downlink data indication from PHY.
312  *
313  * @details
314  *
315  *     Function : RgLiTfuDatInd
316  *      
317  *      This API is invoked by PHY to send data indication to MAC on 
318  *      recieving data from UEs.
319  *           
320  *  @param[in]  Pst              *pst
321  *  @param[in]  SuId             suId 
322  *  @param[in]  TfuDatIndInfo    *datInd
323  *  @return  S16
324  *      -# ROK 
325  *      -# RFAILED 
326  **/
327 #ifdef ANSI
328 S16 RgLiTfuDatInd
329 (
330 Pst                *pst, 
331 SuId               suId, 
332 TfuDatIndInfo    *datInd
333 )
334 #else
335 S16 RgLiTfuDatInd(pst, suId, datInd)
336 Pst                *pst; 
337 SuId               suId; 
338 TfuDatIndInfo    *datInd;
339 #endif
340 {
341    Inst             inst;
342    S16              ret;
343    VOLATILE U32     startTime=0;
344
345
346   // printf("5GTF:: DatindRcvd\n");
347
348    RG_IS_INST_VALID(pst->dstInst);
349    inst = pst->dstInst - RG_INST_START;
350    /*starting Task*/
351    SStartTask(&startTime, PID_MAC_TFU_DATIND);
352
353 #ifndef NO_ERRCLS 
354    if ((ret = rgLIMValidateSap (inst,suId)) != ROK)
355    {
356       RLOG_ARG0(L_ERROR,DBG_CELLID,datInd->cellId,"SAP Validation failed");
357       rgLIMUtlFreeDatIndEvnt(datInd, TRUE);
358       return (ret);
359    }
360 #endif
361    /* Now call the TOM (Tfu ownership module) primitive to process further */
362    rgCb[inst].tfuSap.sapSts.numPduRcvd += 
363                     datInd->datIndLst.count;
364    ret = rgTOMDatInd(inst,datInd);
365    /* Fix: sriky memory corruption precautions */
366    /* Free up the memory for the request structure */
367    if (ret == ROK)
368    {
369       rgLIMUtlFreeDatIndEvnt(datInd, FALSE);
370    }
371    else
372    {
373       rgLIMUtlFreeDatIndEvnt(datInd, TRUE);
374    }
375
376    /*stoping Task*/
377    SStopTask(startTime, PID_MAC_TFU_DATIND);
378
379    return (ret);
380 }  /* RgLiTfuDatInd*/
381
382 #ifdef RG_UNUSED
383 /** @brief This function frees up the TfuDatReqInfo structure.
384  *
385  * @details
386  *
387  *     Function: rgLIMUtlFreeDatReqEvnt
388  *       - Function frees up the TfuDatReqInfo structure, in case of error it shall
389  *       free up the buffer's present in the PDUs list.
390  *
391  *         Processing steps:
392  * @param  [in] TfuDatReqInfo *datReq
393  * @param  [in] Bool          *error
394  * @return 
395  */
396 #ifdef ANSI
397 PRIVATE Void rgLIMUtlFreeDatReqEvnt
398 (
399  TfuDatReqInfo *datReq,
400  Bool          error
401  )
402 #else
403 PRIVATE Void rgLIMUtlFreeDatReqEvnt(datReq, error)
404  TfuDatReqInfo *datReq;
405  Bool          error;
406 #endif
407 {
408
409    TfuDatReqPduInfo *datInfo;
410    CmLList          *node;
411    U8               i;
412
413    /* Steps of freeing up the TfuDatReq.
414     * 1. Free the bch buffer.
415     * 2. loop through the pdus list and free up all the buffers.
416     * 3. free up the whole event
417     */
418    if (error)
419    {
420       if (datReq->bchDat.pres == PRSNT_NODEF)
421       {
422          RG_FREE_MSG(datReq->bchDat.val);
423       }
424       if (datReq->pdus.count > 0)
425       {
426          node =  datReq->pdus.first;
427          while (node)
428          {
429             datInfo = (TfuDatReqPduInfo*)node->node;
430             for (i=0; i<datInfo->nmbOfTBs; i++)
431             {
432                if (datInfo->mBuf[i] != NULLP)
433                {
434                   RG_FREE_MSG(datInfo->mBuf[i]);
435                }
436             }
437             node = node->next;
438          }
439       }
440    }
441    RG_FREE_MEM(datReq);
442    RETVOID;
443 } /* end of rgLIMUtlFreeDatReqEvnt*/
444 #endif
445 /**
446  * @brief This API is invoked to send Data to PHY.
447  *
448  * @details
449  *
450  *     Function : rgLIMTfuDatReq
451  *      
452  *      This API is invoked to send Data to PHY. It 
453  *      fills in the Pst structure, spId value and invokes Data
454  *      request primitive at TFU.
455  *           
456  *  @param[in]  Inst        inst
457  *  @param[in]  TfuDatReqInfo *datReq
458  *  @return  S16
459  *      -# ROK 
460  *      -# RFAILED 
461  **/
462 #ifdef ANSI
463 S16 rgLIMTfuDatReq 
464 (
465 Inst          inst,
466 TfuDatReqInfo *datReq
467 )
468 #else
469 S16 rgLIMTfuDatReq(inst,datReq)
470 Inst          inst;
471 TfuDatReqInfo *datReq;
472 #endif
473 {
474    S16         ret;
475    RgLowSapCb  *tfuSap;
476
477
478    /* Get the lower SAP control block from the layer control block. */
479    tfuSap = &(rgCb[inst].tfuSap);
480
481 #ifndef NO_ERRCLS
482    if (tfuSap->sapSta.sapState != LRG_BND)
483    {
484       RLOG_ARG1(L_ERROR,DBG_CELLID,datReq->cellId,"Lower SAP not bound (%d)",
485             tfuSap->sapSta.sapState);
486 #ifdef RG_UNUSED
487       /* This case will never be hit if sap is not bound then we dont get TTI */
488       rgLIMUtlFreeDatReqEvnt(datReq, TRUE);
489 #endif
490       return RFAILED;
491    }
492 #endif
493
494    tfuSap->sapSts.numPduTxmit += datReq->pdus.count; 
495
496    /* Using existing pst - for optimization */
497    if((ret = RgLiTfuDatReq (&tfuSap->sapCfg.sapPst, tfuSap->sapCfg.spId, 
498                             datReq)) != ROK)
499    {
500       RLOG_ARG0(L_ERROR,DBG_CELLID,datReq->cellId,"Call to RgLiTfuDatReq() failed");
501    }
502    return (ret);
503 }  /* rgLIMTfuDatReq*/
504
505 #ifdef L2_OPTMZ
506 /**
507  * @brief This API is invoked to send Data to PHY.
508  *
509  * @details
510  *
511  *     Function : rgLIMTfuDelDatReq
512  *      
513  *      This API is invoked to send Data to PHY. It 
514  *      fills in the Pst structure, spId value and invokes Data
515  *      request primitive at TFU.
516  *           
517  *  @param[in]  Inst        inst
518  *  @param[in]  TfuDelDatReqInfo *datReq
519  *  @return  S16
520  *      -# ROK 
521  *      -# RFAILED 
522  **/
523 #ifdef ANSI
524 S16 rgLIMTfuDelDatReq 
525 (
526 Inst          inst,
527 TfuDelDatReqInfo *delDatReq
528 )
529 #else
530 S16 rgLIMTfuDatReq(inst,delDatReq)
531 Inst          inst;
532 TfuDelDatReqInfo *delDatReq;
533 #endif
534 {
535    S16         ret;
536    RgLowSapCb  *tfuSap;
537
538
539    /* Get the lower SAP control block from the layer control block. */
540    tfuSap = &(rgCb[inst].tfuSap);
541
542 #ifndef NO_ERRCLS
543    if (tfuSap->sapSta.sapState != LRG_BND)
544    {
545       RLOG_ARG1(L_ERROR,DBG_CELLID,delDatReq->cellId,"Lower SAP not bound (%d)",
546             tfuSap->sapSta.sapState);
547       return RFAILED;
548    }
549 #endif
550
551    if((ret = RgLiTfuDelDatReq (&tfuSap->sapCfg.sapPst, tfuSap->sapCfg.spId, 
552                             delDatReq)) != ROK)
553    {
554       RLOG_ARG0(L_ERROR,DBG_CELLID,delDatReq->cellId,"Call to RgLiTfuDelDatReq() failed");
555    }
556    return (ret);
557 }  /* rgLIMTfuDatReq*/
558 #endif /*L2_OPTMZ */
559
560 #if defined(TENB_T2K3K_SPECIFIC_CHANGES) && defined(LTE_TDD)
561  /**
562  * @brief Transmission of non-rt indication from CL.
563  *
564  * @details
565  *
566  *     Function : RgLiTfuNonRtInd 
567  *      
568  *      This API is invoked by CL to indicate non-rt processing indication to MAC for a cell.
569  *           
570  *  @param[in]  Pst            *pst
571  *  @param[in]  SuId           suId 
572  *  @return  S16
573  *      -# ROK 
574  *      -# RFAILED 
575  **/
576 #ifdef ANSI
577 S16 RgLiTfuNonRtInd
578 (
579 Pst                 *pst,
580 SuId                suId
581 )
582 #else
583 S16 RgLiTfuNonRtInd(pst, suId)
584 Pst                 *pst;
585 SuId                suId;
586 #endif
587 {
588
589 #ifdef NO_ERRCLS
590    if (rgLIMValidateSap (pst->dstInst - RG_INST_START, suId) != ROK)
591    {
592       RGDBGERRNEW(pst->dstInst - RG_INST_START, (rgPBuf(pst->dstInst - RG_INST_START),"RgLiTfuNonRtInd() SAP Validation failed.\n"));
593       return RFAILED;
594    }
595 #endif
596    rgDHMFreeTbBufs(pst->dstInst - RG_INST_START);
597    return ROK;
598 }  /* RgLiTfuNonRtInd */
599
600 #endif
601 /**********************************************************************
602  
603          End of file
604 **********************************************************************/