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