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