Merge "replaced cmMemSet, cmMemcpy with memset and memcpy resp AND Removed TRC()...
[o-du/l2.git] / src / 5gnrmac / rg_dux.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_dux.c 
28   
29 **********************************************************************/
30
31 /** @file rg_dux.c
32 @brief This module handles de-multiplexing of the data recieved at MAC.
33 */
34
35 static const char* RLOG_MODULE_NAME="MAC";
36 static int RLOG_FILE_ID=176;
37 static int RLOG_MODULE_ID=4096;
38 /* header include files -- defines (.h) */
39 #include "common_def.h"
40   
41 #include "rg_env.h"        /* MAC Environment Defines */
42 #include "tfu.h"           /* CRG Interface defines */
43 #include "crg.h"           /* CRG Interface defines */
44 #include "rg_sch_inf.h"           /* RGR Interface defines */
45 #include "rgu.h"           /* RGU Interface defines */
46 #include "lrg.h"           /* LRG Interface defines */
47
48 #include "rg.h"            /* MAC defines */
49 #include "rg_err.h"        /* MAC error defines */
50
51 /* header/extern include files (.x) */
52
53 #include "rgu.x"           /* RGU types */
54 #include "tfu.x"           /* CRG Interface includes */
55 #include "crg.x"           /* CRG Interface includes */
56 #include "rg_sch_inf.x"    /* SCH Interface includes */
57 #include "rg_prg.x"        /* PRG interface includes */
58 #include "rgu.x"           /* RGU Interface includes */
59 #include "lrg.x"           /* LRG Interface includes */
60
61 #include "du_app_mac_inf.h"
62 #include "rg.x"            /* MAC includes */
63
64 /* local defines */
65
66 /* local typedefs */
67  
68 /* local externs */
69  
70 /* forward references */
71
72 #define RG_DUX_ALLOC(_pdu, _size, _dataPtr, _ret) {\
73    _ret = cmGetMem((Ptr)&_pdu->memCp, _size, (Ptr *)&_dataPtr); \
74 }
75
76 #define RG_INIT_SDU(_sdu, _lcId, _len) {\
77    (_sdu)->lcId = (_lcId); \
78    (_sdu)->len = (_len); \
79    (_sdu)->mBuf = NULLP; \
80    (_sdu)->sduLstEnt.next = NULLP; \
81    (_sdu)->sduLstEnt.prev = NULLP; \
82    (_sdu)->sduLstEnt.node = (PTR)(_sdu); \
83 }
84
85 #define RG_EXT_BS(_bsr, _bs1, _bs2, _bs3, _bs4)  {\
86    _bs1 = _bsr[0] >> 2; \
87    _bs2 = (((_bsr[0] & 0x3) << 4) | (_bsr[1] >> 4)); \
88    _bs3 = (((_bsr[1] & 0x0F) << 2) | (_bsr[2] >> 6)); \
89    _bs4 = _bsr[2] & 0x3F; \
90 }
91
92 #define RG_UNPACK_LONG_BSR(_bsr, _mBuf, _ret) {\
93    _ret = SRemPreMsgMult((_bsr), 3, (_mBuf)); \
94 }
95
96 #define RG_UNPACK_SHORT_BSR(_bsr, _mBuf, _ret) {\
97    _ret = oduPackUInt8((_bsr), (_mBuf)); \
98 }
99
100 #define RG_UNPACK_TRUNC_BSR(_bsr, _mBuf, _ret) {\
101    _ret = oduPackUInt8((_bsr), (_mBuf)); \
102 }
103
104 #define RG_UNPACK_PHR(_phr, _mBuf, _ret) {\
105    _ret = oduPackUInt8((_phr), (_mBuf)); \
106 }
107
108 #define RG_UNPACK_CRNTI(_rnti, _mBuf, _ret) {\
109    Data _unpkArray[2];\
110    *_rnti = 0;\
111    _ret = SRemPreMsgMult(_unpkArray, (MsgLen) 2, _mBuf);\
112    if (_ret == ROK)\
113    {\
114       *_rnti = (U16) PutHiByte(*_rnti, (U8) _unpkArray[0]);\
115       *_rnti = (U16) PutLoByte(*_rnti, (U8) _unpkArray[1]);\
116    }\
117 }
118
119 /* For EXT PHR DEMUX */
120 #define RG_UNPACK_EXT_PHR_CI(_ci, _mBuf, _ret) {\
121    _ret = oduPackUInt8((_ci), (_mBuf)); \
122 }
123
124 #define RG_UNPACK_EXT_PHR(_extPhr, _mBuf, _ret) {\
125    _ret = oduPackUInt8((_extPhr), (_mBuf)); \
126 }
127
128
129
130 /**
131  * @brief Handles the insertion of SDU in to PDU.
132  *
133  * @details
134  *
135  *     Function: rgDUXInsSdu
136  *     
137  *     This API handles the insertion of SDU in to PDU.
138  *     
139  *     Processing Steps: 
140  *      - Append the sdu to the sduLst of pdu.
141  *
142  *  @param[in]  Inst        inst
143  *  @param[out] *pdu
144  *  @param[in]  lcId
145  *  @param[in]  sduLen
146  *  @param[out] *err
147  *  @return  S16
148  *      -# ROK 
149  *      -# RFAILED 
150  **/
151 #ifdef ANSI
152 PRIVATE S16 rgDUXInsSdu
153 (
154 Inst        inst,
155 RgMacPdu    *pdu,
156 RgMacSdu    **sdu,
157 U8          lcId,
158 U16         sduLen,
159 RgErrInfo   *err
160 )
161 #else
162 PRIVATE S16 rgDUXInsSdu(inst,pdu, sdu, lcId, sduLen, err)
163 Inst        inst;
164 RgMacPdu    *pdu;
165 RgMacSdu    **sdu,
166 U8          lcId;
167 U16         sduLen;
168 RgErrInfo   *err;
169 #endif
170 {
171    S16         ret;
172    RgMacSdu    *sduAloc = NULLP;
173
174
175    RG_DUX_ALLOC(pdu, sizeof(RgMacSdu), sduAloc, ret);
176    if(ret != ROK)
177    {
178       RLOG1(L_ERROR, "Allocation of RgSubHdr failed for LCID:%d",lcId);
179       err->errCause = RGERR_DUX_MEM_EXHAUST;
180       return RFAILED;
181    }
182    *sdu = sduAloc; 
183    RG_INIT_SDU(sduAloc, lcId, sduLen);
184    cmLListAdd2Tail(&pdu->sduLst, &sduAloc->sduLstEnt);
185    return ROK;
186 }
187
188 /**
189  * @brief Handles extracting the CE sub headers from the MAC PDU.
190  *
191  * @details
192  *
193  *     Function: rgDUXExtSubHdr
194  *     
195  *     This API handles extracting the  sub headers from the MAC PDU.
196  *     
197  *     Processing Steps: 
198  *      - Extract the each sub header.
199  *
200  *  @param[in]  Inst        inst
201  *  @param[out]  *pdu
202  *  @param[in]  *mBuf
203  *  @param[out] *lcId
204  *  @param[out] *len
205  *  @param[out] *err
206  *  @return  S16
207  *      -# ROK 
208  *      -# RFAILED 
209  **/
210 #ifdef ANSI
211 PRIVATE S16 rgDUXExtSubHdr
212 (
213 Inst        inst,
214 RgMacPdu    *pdu,
215 Buffer      *mBuf,
216 U8          *lcId,
217 U16         *len,
218 RgErrInfo   *err
219 )
220 #else
221 PRIVATE S16 rgDUXExtSubHdr(inst,pdu, mBuf, lcId,
222 len, err)
223 Inst        inst;
224 RgMacPdu    *pdu;
225 Buffer      *mBuf;
226 U8          *lcId;
227 U16         *len;
228 RgErrInfo   *err;
229 #endif
230 {
231    U8             byte;
232    U8             fmt=0;
233  
234
235    *len = 0;   
236    if(oduPackUInt8(&byte,mBuf) != ROK)
237    {
238       RLOG0(L_ERROR, "oduPackUInt8 failed");
239       err->errCause = RGERR_DUX_UNPACK_FAILURE;
240       return RFAILED;
241    }
242    /* Extract the lcid */
243    RG_EXT_LCID(*lcId, byte);
244
245    /*note: RG_EXT_PHR_LCID currently not considered */
246    if(*lcId <= RG_DEDLC_MAX_LCID)
247    {  /* variable size MAC Sub PDU */
248       RG_EXT_FORMT_BIT(fmt,byte);
249       if(oduPackUInt8(&byte, mBuf) != ROK)
250       {
251          RLOG0(L_ERROR, "oduPackUInt8 failed");
252          err->errCause = RGERR_DUX_UNPACK_FAILURE;
253          return RFAILED;
254       }
255       *len = byte;
256       if(fmt)
257       {
258          if(oduPackUInt8(&byte,mBuf) != ROK)
259          {
260             RLOG0(L_ERROR, "oduPackUInt8 failed");
261             err->errCause = RGERR_DUX_UNPACK_FAILURE;
262             return RFAILED;
263          }
264          *len = (*len << 8) | byte;
265       }
266    }
267    return ROK;
268 } /* rgDUXExtSubHdr */
269
270 /**
271  * @brief Handles extracting the CEs from the MAC PDU.
272  *
273  * @details
274  *
275  *     Function: rgDUXExtCe
276  *     
277  *     This API handles extracting the CEs from the MAC PDU.
278  *     
279  *     Processing Steps: 
280  *      - Based on the ce sub header extract the ce.
281  *
282  *  @param[in]  Inst        inst
283  *  @param[in,out]  *pdu
284  *  @param[out] *ceInfo
285  *  @param[in]  *mBuf
286  *  @param[in] lcId
287  *  @param[in] subPduLen
288  *  @param[out] *err
289  *  @return  S16
290  *      -# ROK 
291  *      -# RFAILED 
292  **/
293 #ifdef ANSI
294 PRIVATE S16 rgDUXExtCe
295 (
296 Inst        inst,
297 RgMacPdu    *pdu,
298 RgInfCeInfo  *ceInfo,
299 Buffer       *mBuf,
300 U8          lcId,
301 U16         subPduLen,
302 RgErrInfo    *err
303 )
304 #else
305 PRIVATE S16 rgDUXExtCe(inst,pdu, ceInfo, mBuf,lcId,subPduLen, err)
306 Inst        inst;
307 RgMacPdu    *pdu;
308 RgInfCeInfo  *ceInfo;
309 Buffer       *mBuf;
310 U8          lcId;
311 U16         subPduLen;
312 RgErrInfo    *err;
313 #endif
314 {
315    S16            ret;
316
317
318    switch(lcId)
319    {
320       case RG_EXT_PHR_LCID:
321          {
322             U8 Ci;
323             U8 sCellIdx;
324             U8 extPhrOctet;
325             U8 extPhrPCmax;
326             RgInfExtPhrCEInfo *extPhr;
327
328             RG_UNPACK_EXT_PHR_CI(&Ci,mBuf,ret);
329             if(ret != ROK)
330             {
331                RLOG1(L_ERROR,"Unpacking of EXT PHR failed LCID:%d",lcId);
332                err->errCause = RGERR_DUX_UNPACK_FAILURE;
333                return RFAILED;
334             }
335
336             /* Not handling Type 2 PHR report as simultaneous PUSCH/PUCCH
337                is not supported as of now */
338             extPhr = &ceInfo->ces.extPhr;
339             extPhr->numServCells = 0;
340
341             /* Setting first BIT as PCELL field even though reserved is always
342                reported by UE */
343             Ci |= 0x1;
344             for (sCellIdx = 0; (Ci && sCellIdx < CM_LTE_MAX_CELLS); sCellIdx++)
345             {
346                if (Ci & 0x1)
347                {
348                   extPhr->servCellPhr[extPhr->numServCells].sCellIdx = sCellIdx;
349                   RG_UNPACK_EXT_PHR(&extPhrOctet,mBuf,ret);
350                   if(ret != ROK)
351                   {
352                      RLOG1(L_ERROR,"Unpacking of EXT PHR failed LCID:%d",lcId);
353                      err->errCause = RGERR_DUX_UNPACK_FAILURE;
354                      return RFAILED;
355                   }
356
357                   /* extPhrOctet: Bits : 7 6 5 4 3 2 1 0
358                    *                     P V x x x x x x
359                    *                         <6x Bit phr>
360                    */
361                   /* P : P Back off applied or not */
362                   extPhr->servCellPhr[extPhr->numServCells].phr = (extPhrOctet & 0x3F);
363                   extPhr->servCellPhr[extPhr->numServCells].pBackOff = 
364                      ((extPhrOctet >> 7) & 0x01);
365
366                   /* V: Virtual PCMAX or Real Tx PCMAX */
367                   if (extPhrOctet & 0x40)
368                   {
369                      extPhr->servCellPhr[extPhr->numServCells].pCmax = RG_REF_PCMAX;
370                   }
371                   else
372                   {
373                      RG_UNPACK_EXT_PHR(&extPhrPCmax,mBuf,ret);
374                      if(ret != ROK)
375                      {
376                         RLOG1(L_ERROR,"Unpacking of EXT PHR failed LCID:%d",lcId);
377                         err->errCause = RGERR_DUX_UNPACK_FAILURE;
378                         return RFAILED;
379                      }
380                      extPhr->servCellPhr[extPhr->numServCells].pCmax = (extPhrPCmax & 0x3F);
381                   }
382                   extPhr->numServCells++;
383                }
384                Ci >>= 1;
385             }
386
387             ceInfo->bitMask |= RG_EXT_PHR_CE_PRSNT;
388          }
389          break;
390
391       case RG_PHR_LCID:
392          {
393             RG_UNPACK_PHR(&ceInfo->ces.phr,mBuf,ret);
394             if(ret != ROK)
395             {
396                RLOG1(L_ERROR,"Unpacking of PHR failed LCID:%d",lcId);
397                err->errCause = RGERR_DUX_UNPACK_FAILURE;
398                return RFAILED;
399             }
400             ceInfo->bitMask |= RG_PHR_CE_PRSNT;
401          }
402          break;
403       case RG_TRUNC_BSR_LCID:
404          {
405             RG_UNPACK_TRUNC_BSR(&ceInfo->ces.bsr.truncBsr,mBuf,ret);
406             if(ret != ROK)
407             {
408                RLOG1(L_ERROR,"Unpacking of Trunc BSR failed LCID:%d",lcId);
409                err->errCause = RGERR_DUX_UNPACK_FAILURE;
410                return RFAILED;
411             }
412             ceInfo->bitMask |= RG_TRUNC_BSR_CE_PRSNT;
413          }
414          break;
415       case RG_SHORT_BSR_LCID:
416          {
417             RG_UNPACK_SHORT_BSR(&ceInfo->ces.bsr.shortBsr,mBuf,ret);
418             if(ret != ROK)
419             {
420                RLOG1(L_ERROR,"Unpacking of Short BSR failed LCID:%d",lcId);
421                err->errCause = RGERR_DUX_UNPACK_FAILURE;
422                return RFAILED;
423             }
424             ceInfo->bitMask |= RG_SHORT_BSR_CE_PRSNT;
425          }
426          break;
427       case RG_LONG_BSR_LCID:
428          {
429             U8 longBsr[3] = {0}; /* KW_FIXX */
430             RG_UNPACK_LONG_BSR(longBsr,mBuf,ret);
431             if(ret != ROK)
432             {
433                RLOG1(L_ERROR,"Unpacking of Long BSR failed LCID:%d",lcId);
434                err->errCause = RGERR_DUX_UNPACK_FAILURE;
435                return RFAILED;
436             }
437             RG_EXT_BS(longBsr, 
438                   ceInfo->ces.bsr.longBsr.bs1, 
439                   ceInfo->ces.bsr.longBsr.bs2, 
440                   ceInfo->ces.bsr.longBsr.bs3, 
441                   ceInfo->ces.bsr.longBsr.bs4);
442             ceInfo->bitMask |= RG_LONG_BSR_CE_PRSNT;
443          }
444          break;
445       case RG_CRNTI_LCID:
446          {
447             RG_UNPACK_CRNTI(&ceInfo->ces.cRnti,mBuf,ret);
448             if(ret != ROK)
449             {
450                RLOG1(L_ERROR,"Unpacking of C-RNTI failed LCID:%d",lcId);
451                err->errCause = RGERR_DUX_UNPACK_FAILURE;
452                return RFAILED;
453             }
454             ceInfo->bitMask |= RG_CRNTI_CE_PRSNT;
455          }
456          break;
457       default:
458          RLOG1(L_ERROR, "Invalid LCID:%u received",lcId); 
459          err->errCause = RGERR_DUX_INV_LCID_RX;
460          return RFAILED;
461    }
462    return ROK;
463 } /* rgDUXExtCe  */
464
465
466 /**
467  * @brief Handles extracting the SDU from the MAC PDU.
468  *
469  * @details
470  *
471  *     Function: rgDUXExtSdu
472  *     
473  *     This API handles extracting the SDU corresponding to a logical channel.
474  *     
475  *     Processing Steps: 
476  *      - Based on the length stored in the sub header extract the SDU.
477  *
478  *  @param[in]  Inst        inst
479  *  @param[in,out]  *pdu
480  *  @param[out] *ceInfo
481  *  @param[in]  *mBuf
482  *  @param[in] lcId
483  *  @param[in] subPduLen
484  *  @param[out] *err
485  *  @return  S16
486  *      -# ROK 
487  *      -# RFAILED 
488  **/
489 #ifdef ANSI
490 PRIVATE S16 rgDUXExtSdu
491 (
492 Inst        inst,
493 RgMacPdu    *pdu,
494 RgInfCeInfo  *ceInfo,
495 Buffer      **mBuf,
496 U8          lcId,
497 U16         subPduLen,
498 RgErrInfo   *err
499 )
500 #else
501 PRIVATE S16 rgDUXExtSdu(inst,pdu, ceInfo,mBuf,lcId,subPduLen,err)
502 Inst        inst;
503 RgMacPdu    *pdu;
504 RgInfCeInfo  *ceInfo;
505 Buffer      **mBuf;
506 U8          lcId;
507 U16         subPduLen;
508 RgErrInfo   *err;
509 #endif
510 {
511    S16         ret;
512    Buffer      *tmpBuf1;
513    Buffer      *tmpBuf2 = NULLP;
514    RgMacSdu    *sdu;
515
516
517    if(lcId == RG_CCCH_LCID)
518    {
519       ceInfo->bitMask |= RG_CCCH_SDU_PRSNT;
520    }
521
522    if(rgDUXInsSdu(inst,pdu, &sdu,lcId, subPduLen, err) != ROK)
523    {
524       RG_FREE_MSG(*mBuf);
525       return RFAILED;
526    }
527
528    tmpBuf1 = *mBuf;
529    {
530       ret = SSegMsg(tmpBuf1,subPduLen,&tmpBuf2);
531       if((ret != ROK) && (!((ret == ROKDNA) )))
532       {
533          RG_FREE_MSG(tmpBuf1);
534          RLOG0(L_ERROR,"SSegMsg failed");
535          err->errCause = RGERR_DUX_RLC_PDU_CREAT_FAIL;
536          return RFAILED;
537       }
538       sdu->mBuf = tmpBuf1;
539       *mBuf = tmpBuf2;
540    }
541    return ROK;
542 }   /* rgDUXExtSdu */
543
544 /**
545  * @brief Handles de-multiplexing of the data recieved at MAC.
546  *
547  * @details
548  *
549  *     Function: rgDUXDemuxData
550  *     
551  *     This API handles de-multiplexing of the data recieved at MAC.
552  *     
553  *     Invoked by: rgTOMTfuDatInd of TOM 
554  *     
555  *     Processing Steps: 
556  *      - De-multiplex the mBuf
557  *
558  *  @param[in]  Inst        inst
559  *  @param[in]  *pdu 
560  *  @param[in]  *mBuf 
561  *  @param[out] *err 
562  *  @return  S16
563  *      -# ROK 
564  *      -# RFAILED 
565  **/
566 #ifdef ANSI
567 S16 rgDUXDemuxData
568 (
569 Inst          inst,
570 RgMacPdu      *pdu,
571 RgInfCeInfo   *ceInfo,
572 Buffer        **mBuf,
573 RgErrInfo     *err
574 )
575 #else
576 S16 rgDUXDemuxData(inst,pdu, ceInfo, mBuf, err)
577 Inst          inst;
578 RgMacPdu      *pdu;
579 RgInfCeInfo   *ceInfo;
580 Buffer        **mBuf;
581 RgErrInfo     *err;
582 #endif
583 {
584    U8          lcId;
585    U16          subPduLen;
586    MsgLen      len;  
587
588    ceInfo->bitMask = 0x0000;
589
590    /* Initialize the sdu list */
591    cmLListInit(&pdu->sduLst);
592
593    if(*mBuf == NULLP) 
594    {
595       RLOG0(L_ERROR, "Null Buffer Recevived");
596       return RFAILED;
597    }
598    do
599    {
600       /* UL Message format  order : 
601            PduSubHdr+SubPDU,PduSubHdr+SubPDU,...CeSubHdr+Ce,CeSubPdu+Ce,...,PADSubHdr+PAD */
602       /* Extract the Sub headers */
603       if(rgDUXExtSubHdr(inst,pdu, *mBuf, &lcId, 
604                &subPduLen, err) != ROK)
605       {
606          RG_FREE_MSG(*mBuf);          
607          RLOG0(L_ERROR, "Failed to extract pad sub headers");
608          return RFAILED;
609       }
610       if(lcId == RG_PAD_LCID)
611       { /*at end of MAC PDU,  Padding started */ 
612          RG_FREE_MSG(*mBuf);          
613          return ROK;
614       }
615       if(lcId <= RG_DEDLC_MAX_LCID)
616       {
617          /* Extract the sdus */
618          if(rgDUXExtSdu(inst,pdu,ceInfo, mBuf,lcId,subPduLen, err) != ROK)
619          {
620             /* Fix : syed rgDUXExtSdu would have segmented mBuf and hence
621              * will be responsible for freeing mBuf */
622             *mBuf = NULLP;
623             RLOG0(L_ERROR, "failed to Extract the sdus");
624             return RFAILED;
625          }
626          if(*mBuf == NULLP) /* if message read completes then return */
627          {
628             return ROK;
629          }
630       }
631       else
632       {
633          /* Extract the ces */
634          if(rgDUXExtCe(inst,pdu,ceInfo,*mBuf, lcId,subPduLen, err) != ROK)
635          {
636             RG_FREE_MSG(*mBuf);       
637             RLOG0(L_ERROR, " failed to Extract the ces");
638             return RFAILED;
639          }
640       }
641       if(SFndLenMsg(*mBuf,&len) != ROK)
642       {
643          RG_FREE_MSG(*mBuf);
644          RLOG0(L_ERROR,"mBuf length check failed");
645          err->errCause = RGERR_DUX_UNPACK_FAILURE;
646          return RFAILED;
647       }
648    }while(len);
649
650    RG_FREE_MSG(*mBuf);
651    return ROK;
652 }  /* rgDUXDemuxData */
653
654 /**********************************************************************
655  
656          End of file
657 **********************************************************************/