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