Removed ANSI from multiple folder Part - 1 [JIRA ID - ODUHIGH-249]
[o-du/l2.git] / src / 5gnrrlc / kw_utl_dl.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-RLC Layer 
22   
23      Type:     C file
24   
25      Desc:     Source code for RLC Utility Module
26                This file contains following functions
27
28                   --rlcUtlSendToMac
29                   --rlcUtlRcvFrmMac
30                   --rlcUtlEmptySduQ
31                   --rlcUtlSendDedLcBoStatus
32                   --rlcUtlSendUlDataToDu 
33                   --kwUtlShutDown
34
35      File:     kw_utl_dl.c
36
37 **********************************************************************/
38 static const char* RLOG_MODULE_NAME="UTL";
39 static int RLOG_MODULE_ID=2048;
40 static int RLOG_FILE_ID=209;
41
42 /** @file kw_utl_dl.c
43 @brief RLC Utility Module 
44 */
45
46 /* header (.h) include files */
47
48 #include "common_def.h"
49 /* kw005.201 added support for L2 Measurement */
50 #ifdef LTE_L2_MEAS
51 #include <sys/time.h>
52 #endif
53 #include "ckw.h"           /* CKW defines */
54 #include "kwu.h"           /* CKW defines */
55 #include "lkw.h"           /* LKW defines */
56 #include "rgu.h"           /* RGU defiens */
57
58 #include "kw_env.h"        /* RLC environment options */
59 #include "kw.h"            /* RLC defines */
60 #include "kw_err.h"        /* Error defines */
61 #include "kw_udx.h"
62 #include "kw_dl.h"
63
64 /* extern (.x) include files */
65 #include "ckw.x"           /* CKW includes */
66 #include "kwu.x"           /* KWU includes */
67 #include "lkw.x"           /* LKW inlcudes */
68 #include "rgu.x"           /* RGU includes */
69
70 #include "kw.x"            /* RLC includes */
71 #include "kw_udx.x"        /* UDX interface includes */
72 #include "kw_dl.x"         /* RLC downlink includes */
73
74 #include "rlc_utils.h"
75 #include "rlc_mac_inf.h"
76 #include "rlc_lwr_inf_api.h"
77
78 #include "ss_rbuf.h"
79 #include "ss_rbuf.x" 
80
81 SsRngBufCnt rngCb;
82
83 #if (defined(MAC_RLC_HARQ_STA_RBUF) && defined(LTE_L2_MEAS))
84 uint32_t isDatReqProcessed;
85 #endif
86 #define RLC_MODULE (RLC_DBGMASK_DUT | RLC_DBGMASK_DL) /* for debugging purpose */
87 #if (defined(MAC_RLC_HARQ_STA_RBUF) && defined(LTE_L2_MEAS)) || defined (SS_RBUF)
88 void rlcUtlDlBatchProcHqStaInd ARGS ((Void));
89 #endif
90 Void ResetRLCStats(Void)
91 {
92    RlcCb* dlInst = rlcCb[1]; 
93    RlcCb* ulInst = rlcCb[0]; 
94    memset(&gRlcStats, 0, sizeof(RLCStats));
95    memset(&dlInst->genSts,0,sizeof(RlcGenSts));
96    memset(&ulInst->genSts,0,sizeof(RlcGenSts));
97 }
98
99 #ifndef ALIGN_64BIT
100 Void PrintRLCStats(Void)
101 {
102    RlcCb* dlInst = rlcCb[1]; 
103    RlcCb* ulInst = rlcCb[0]; 
104
105    RLOG4(L_ALWAYS,"RLC Stats: PDUs Sent = (%ld), PdusRext = (%ld), TimeOut = (%ld), SduDiscarded = (%ld)",
106                      dlInst->genSts.pdusSent,
107                      dlInst->genSts.pdusRetx,
108                      dlInst->genSts.protTimeOut,
109                      dlInst->genSts.numSduDisc);
110    RLOG3(L_ALWAYS,"RLC Stats: PDUs Rcvd = (%ld), unexpPdus = (%ld), errorPdus = (%ld)",
111                      ulInst->genSts.pdusRecv,
112                      ulInst->genSts.unexpPdusRecv,
113                      ulInst->genSts.errorPdusRecv);
114    RLOG4(L_ALWAYS,"RLC Stats: AMDL: "
115                      "StaPduSent:%lu NacksInStaPdu:%lu BytesUnused:%lu PollTimerExpires SRB:%lu ",
116                      gRlcStats.amRlcStats.numDLStaPduSent, gRlcStats.amRlcStats.numDLNacksInStaPdu, 
117                      gRlcStats.amRlcStats.numDLBytesUnused, gRlcStats.amRlcStats.numDLPollTimerExpiresSrb);
118    RLOG3(L_ALWAYS,"RLC Stats: AMDL: "
119                      "DRB:%lu MaxRetx:%lu RetransPdus:%lu", 
120                      gRlcStats.amRlcStats.numDLPollTimerExpiresDrb, gRlcStats.amRlcStats.numDLMaxRetx, 
121                      gRlcStats.amRlcStats.numDLRetransPdus);
122    RLOG4(L_ALWAYS,"RLC Stats: AMUL: "
123                      " PdusDiscarded:%lu ReOrdTimerExpires:%lu StaPduRcvd:%lu NackInStaPduRcvd:%lu ", 
124                      gRlcStats.amRlcStats.numULPdusDiscarded, gRlcStats.amRlcStats.numULReOrdTimerExpires, 
125                      gRlcStats.amRlcStats.numULStaPduRcvd, gRlcStats.amRlcStats.numULNackInStaPduRcvd);
126
127    RTLIN_DUMP_DEBUG("RLC Stats: PDUs Sent = (%ld), PdusRext = (%ld), TimeOut = (%ld), SduDiscarded = (%ld)\n",
128                      dlInst->genSts.pdusSent,
129                      dlInst->genSts.pdusRetx,
130                      dlInst->genSts.protTimeOut,
131                      dlInst->genSts.numSduDisc);
132    RTLIN_DUMP_DEBUG("RLC Stats: PDUs Rcvd = (%ld), unexpPdus = (%ld), errorPdus = (%ld)\n",
133                      ulInst->genSts.pdusRecv,
134                      ulInst->genSts.unexpPdusRecv,
135                      ulInst->genSts.errorPdusRecv);
136    RTLIN_DUMP_DEBUG("RLC Stats:"
137                     "AMDL: StaPduSent:%lu NacksInStaPdu:%lu BytesUnused:%lu PollTimerExpires SRB:%lu DRB:%lu MaxRetx:%lu RetransPdus:%lu \n"
138                     "AMUL: PdusDiscarded:%lu ReOrdTimerExpires:%lu StaPduRcvd:%lu NackInStaPduRcvd:%lu \n", 
139                      gRlcStats.amRlcStats.numDLStaPduSent, gRlcStats.amRlcStats.numDLNacksInStaPdu, gRlcStats.amRlcStats.numDLBytesUnused, 
140                      gRlcStats.amRlcStats.numDLPollTimerExpiresSrb, gRlcStats.amRlcStats.numDLPollTimerExpiresDrb, gRlcStats.amRlcStats.numDLMaxRetx, 
141                      gRlcStats.amRlcStats.numDLRetransPdus, gRlcStats.amRlcStats.numULPdusDiscarded, gRlcStats.amRlcStats.numULReOrdTimerExpires, 
142                      gRlcStats.amRlcStats.numULStaPduRcvd, gRlcStats.amRlcStats.numULNackInStaPduRcvd);
143 }
144 #else
145 Void PrintRLCStats(Void)
146 {
147    RlcCb* dlInst = rlcCb[1]; 
148    RlcCb* ulInst = rlcCb[0]; 
149
150    printf ("\n================================ RLC STATS ===========================\n");
151    RLOG4(L_ALWAYS,"RLC Stats: PDUs Sent = (%ld), PdusRext = (%ld), TimeOut = (%ld), SduDiscarded = (%ld)",
152                      dlInst->genSts.pdusSent,
153                      dlInst->genSts.pdusRetx,
154                      dlInst->genSts.protTimeOut,
155                      dlInst->genSts.numSduDisc);
156    RLOG3(L_ALWAYS,"RLC Stats: PDUs Rcvd = (%ld), unexpPdus = (%ld), errorPdus = (%ld)",
157                      ulInst->genSts.pdusRecv,
158                      ulInst->genSts.unexpPdusRecv,
159                      ulInst->genSts.errorPdusRecv);
160    RLOG4(L_ALWAYS,"RLC Stats: AMDL: "
161                      "StaPduSent:%lu NacksInStaPdu:%lu BytesUnused:%lu PollTimerExpires SRB:%lu ",
162                      gRlcStats.amRlcStats.numDLStaPduSent, gRlcStats.amRlcStats.numDLNacksInStaPdu, 
163                      gRlcStats.amRlcStats.numDLBytesUnused, gRlcStats.amRlcStats.numDLPollTimerExpiresSrb);
164    RLOG3(L_ALWAYS,"RLC Stats: AMDL: "
165                      "DRB:%lu MaxRetx:%lu RetransPdus:%lu", 
166                      gRlcStats.amRlcStats.numDLPollTimerExpiresDrb, gRlcStats.amRlcStats.numDLMaxRetx, 
167                      gRlcStats.amRlcStats.numDLRetransPdus);
168    RLOG4(L_ALWAYS,"RLC Stats: AMUL: "
169                      " PdusDiscarded:%lu ReOrdTimerExpires:%lu StaPduRcvd:%lu NackInStaPduRcvd:%lu ", 
170                      gRlcStats.amRlcStats.numULPdusDiscarded, gRlcStats.amRlcStats.numULReOrdTimerExpires, 
171                      gRlcStats.amRlcStats.numULStaPduRcvd, gRlcStats.amRlcStats.numULNackInStaPduRcvd);
172  /*  RTLIN_DUMP_DEBUG("AM RLC Stats:"
173                     "AMDL: SDUs Tx :(%u) SDU Bytes Tx :(%u) SDUs Retx :(%u) MaxRetx:(%u) WindowStalls: (%u) \n"
174                     "AMUL: DropOutWinRx :(%u) SDUs Rx :(%u) SDU Bytes Rx :(%u) SDUNack Rx :(%u) Duplicate Pdu Rx :(%u) \n",
175                      gRlcStats.amRlcStats.numRlcAmCellSduTx, gRlcStats.amRlcStats.numRlcAmCellSduBytesTx,
176                      gRlcStats.amRlcStats.numRlcAmCellRetxPdu, gRlcStats.amRlcStats.numRlcAmMaxRetx, gRlcStats.amRlcStats.numRlcAmCellWinStall,
177                      gRlcStats.amRlcStats.numRlcAmCellDropOutWinRx, gRlcStats.amRlcStats.numRlcAmCellSduRx,
178                      gRlcStats.amRlcStats.numRlcAmCellSduBytesRx, gRlcStats.amRlcStats.numRlcAmCellNackRx, gRlcStats.amRlcStats.numRlcAmCellDupPduRx);
179 */
180    RTLIN_DUMP_DEBUG("RLC Stats: PDUs Sent = (%d), PdusRext = (%d), TimeOut = (%d), SduDiscarded = (%d)\n",
181                      dlInst->genSts.pdusSent,
182                      dlInst->genSts.pdusRetx,
183                      dlInst->genSts.protTimeOut,
184                      dlInst->genSts.numSduDisc);
185    RTLIN_DUMP_DEBUG("RLC Stats: PDUs Rcvd = (%d), unexpPdus = (%d), errorPdus = (%d)\n",
186                      ulInst->genSts.pdusRecv,
187                      ulInst->genSts.unexpPdusRecv,
188                      ulInst->genSts.errorPdusRecv);
189    RTLIN_DUMP_DEBUG("AMDL: StaPduSent:%u NacksInStaPdu:%u BytesUnused:%u PollTimerExpires SRB:%u DRB:%u MaxRetx:%u RetransPdus:%u \n"
190                     "      SDUs Tx :(%u) SDU Bytes Tx :(%u) SDUs Retx :(%u) WindowStalls: (%u) \n"
191
192                     "AMUL: PdusDiscarded:%u ReOrdTimerExpires:%u StaPduRcvd:%u NackInStaPduRcvd:%u \n"
193                     "      DropOutWinRx :(%u) SDUs Rx :(%u) SDU Bytes Rx :(%u) SDUNack Rx :(%u) Duplicate Pdu Rx:(%u) \n", 
194                      gRlcStats.amRlcStats.numDLStaPduSent, gRlcStats.amRlcStats.numDLNacksInStaPdu, gRlcStats.amRlcStats.numDLBytesUnused, 
195                      gRlcStats.amRlcStats.numDLPollTimerExpiresSrb, gRlcStats.amRlcStats.numDLPollTimerExpiresDrb,
196                      gRlcStats.amRlcStats.numDLMaxRetx, gRlcStats.amRlcStats.numDLRetransPdus,
197                      gRlcStats.amRlcStats.numRlcAmCellSduTx, gRlcStats.amRlcStats.numRlcAmCellSduBytesTx,
198                      gRlcStats.amRlcStats.numRlcAmCellRetxPdu, gRlcStats.amRlcStats.numRlcAmCellWinStall,
199                      gRlcStats.amRlcStats.numULPdusDiscarded, gRlcStats.amRlcStats.numULReOrdTimerExpires, 
200                      gRlcStats.amRlcStats.numULStaPduRcvd, gRlcStats.amRlcStats.numULNackInStaPduRcvd,
201                      gRlcStats.amRlcStats.numRlcAmCellDropOutWinRx, gRlcStats.amRlcStats.numRlcAmCellSduRx,
202                      gRlcStats.amRlcStats.numRlcAmCellSduBytesRx, gRlcStats.amRlcStats.numRlcAmCellNackRx, gRlcStats.amRlcStats.numRlcAmCellDupPduRx);
203 }
204 #endif
205
206 /*******************************************************************
207  *
208  * @brief 
209  *    Handler for storing all DL PDU Info into RLC-MAC interface
210  *    struct and sending to lower interface
211  *
212  * @details
213  *    This function stores DL PDU info for all logical channels
214  *    of per UE grant per TTI and sends to MAC
215  *
216  *    Function : rlcSendDedLcDlData 
217  *
218  * @params[in] 
219  * @return ROK     - success
220  *         RFAILED - failure
221  *
222  * ****************************************************************/
223 uint8_t rlcSendDedLcDlData(Pst *post, SpId spId, RguDDatReqInfo *datReqInfo)
224 {
225    uint8_t          ueIdx;      /* UE info list iterator */ 
226    uint8_t          tbIdx;      /* TB info list iterator */
227    uint8_t          lchIdx;     /* Lch info list iterator */
228    uint8_t          pduIdx;     /* RLC PDU list iterator */
229    RguDDatReqPerUe  datPerUe;   /* DL data info per UE */
230    RguDatReqTb      datPerTb;   /* DL data info per TB */
231    RguLchDatReq     datPerLch;  /* DL data info per Lch */
232    RlcData          *dlData;    /* DL data to be sent to MAC */
233    Pst              pst;        /* Post structure */
234    uint16_t         pduLen;     /* PDU length */
235    uint16_t         copyLen;    /* Number of bytes copied */
236
237    dlData = NULLP;
238    RLC_ALLOC_SHRABL_BUF(RLC_MEM_REGION_DL, RLC_POOL,
239                        dlData, sizeof(RlcData));
240 #if (ERRCLASS & ERRCLS_ADD_RES)
241    if ( dlData == NULLP )
242    {
243       DU_LOG("\nRLC: rlcSendDedLcDlData: Memory allocation failed for dl data");
244       RLC_FREE_SHRABL_BUF(RLC_MEM_REGION_DL, RLC_POOL,
245           datReqInfo, sizeof(RguDDatReqInfo));
246       return RFAILED;
247    }
248 #endif /* ERRCLASS & ERRCLS_ADD_RES */
249
250    for(ueIdx = 0; ueIdx < datReqInfo->nmbOfUeGrantPerTti; ueIdx++)
251    {
252       datPerUe = datReqInfo->datReq[ueIdx];
253
254       memset(dlData, 0, sizeof(RlcData));
255
256       dlData->cellId = datReqInfo->cellId;
257       dlData->rnti = datPerUe.rnti;
258
259       /* Retrieving sfn/slot from transId. It was filled in RlcProcSchedResultRpt */
260       dlData->slotInfo.sfn = datPerUe.transId >> 16;
261       dlData->slotInfo.slot = datPerUe.transId & 0xffff;
262       dlData->numPdu = 0;
263
264       for(tbIdx = 0; tbIdx < datPerUe.nmbOfTbs; tbIdx++)
265       {
266          datPerTb = datPerUe.datReqTb[tbIdx];
267          for(lchIdx = 0; lchIdx < datPerTb.nmbLch; lchIdx++)
268          {
269             datPerLch = datPerTb.lchData[lchIdx];
270             for(pduIdx = 0; pduIdx < datPerLch.pdu.numPdu; pduIdx++)
271             {
272                dlData->pduInfo[dlData->numPdu].commCh = FALSE;
273                dlData->pduInfo[dlData->numPdu].lcId = datPerLch.lcId;
274
275                /* Copy Message to fixed buffer to send */
276                ODU_GET_MSG_LEN(datPerLch.pdu.mBuf[pduIdx], (MsgLen *)&pduLen);
277                RLC_ALLOC_SHRABL_BUF(RLC_MEM_REGION_DL, RLC_POOL,
278                   dlData->pduInfo[dlData->numPdu].pduBuf, pduLen);
279
280                if (dlData->pduInfo[dlData->numPdu].pduBuf == NULLP )
281                {
282                   DU_LOG("\nRLC: rlcSendDedLcDlData: Memory allocation failed");
283                   for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
284                   {
285                      RLC_FREE_SHRABL_BUF(pst.region, pst.pool, dlData->pduInfo[pduIdx].pduBuf,\
286                        dlData->pduInfo[pduIdx].pduLen);
287                   }
288                   RLC_FREE_SHRABL_BUF(RLC_MEM_REGION_DL, RLC_POOL,
289                      dlData, sizeof(RlcData));
290                   RLC_FREE_SHRABL_BUF(RLC_MEM_REGION_DL, RLC_POOL,
291                      datReqInfo, sizeof(RguDDatReqInfo));
292                   return RFAILED;
293                }
294
295                ODU_COPY_MSG_TO_FIX_BUF(datPerLch.pdu.mBuf[pduIdx], 0, pduLen, \
296                   dlData->pduInfo[dlData->numPdu].pduBuf, (MsgLen *)&copyLen);
297                dlData->pduInfo[dlData->numPdu].pduLen = pduLen;
298
299                /* Free message */
300                ODU_PUT_MSG_BUF(datPerLch.pdu.mBuf[pduIdx]);
301
302                dlData->numPdu++;
303             }/* For per PDU */
304          }/* For Data per Lch */
305       }/* For Data per Tb */
306
307       /* Sending DL Data per UE to MAC */
308       memset(&pst, 0, sizeof(Pst));
309       FILL_PST_RLC_TO_MAC(pst, RLC_DL_INST, EVENT_DL_DATA_TO_MAC);
310       if(RlcSendDlDataToMac(&pst, dlData) != ROK)
311       {
312          for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
313          {
314             RLC_FREE_SHRABL_BUF(pst.region, pst.pool, dlData->pduInfo[pduIdx].pduBuf,\
315                dlData->pduInfo[pduIdx].pduLen);
316          }
317          RLC_FREE_SHRABL_BUF(pst.region, pst.pool, dlData, sizeof(RlcData));
318       }
319    } /* For Data per UE */
320
321    RLC_FREE_SHRABL_BUF(RLC_MEM_REGION_DL, RLC_POOL,
322             datReqInfo, sizeof(RguDDatReqInfo));
323    
324    return ROK;
325 }/* End of rlcSendDedLcDlData */
326
327 /**
328  *
329  * @brief 
330  *        Handler for sending the data to multiple logical channels of a UE
331  *
332  * @details:
333  *        This function sends the data for one or more logical channels
334  *        after processing the SDUs and forming the PDUs.It calls
335  *        UMM or AMM functions to form the PDUs for the requested sizes
336  *        by MAC. 
337  *
338  * @param[in] gCb          RLC instance Control block 
339  * @param[in] staIndInfo   Status Indication Information containing the 
340  *                         size of PDU(s) for one or more logical channels  
341  *
342  * @return  S16
343  *      -# ROK 
344  *      -# RFAILED
345  *
346  */
347 uint8_t rlcUtlSendToMac(RlcCb *gCb, SuId suId, KwDStaIndInfo *staIndInfo)
348 {
349    uint8_t           numPdu = 0;
350    uint16_t          ueIdx;
351    RlcDlUeCb         *ueCb;         /* UE control block */
352    uint32_t          count;         /* Loop Counter */
353    uint32_t          numTb;         /* Number of Tbs */
354    RlcDlRbCb         *rbCb;         /* RB Control Block */
355    RlcDatReq         datReq;        /* PDUs Information */
356    RguDDatReqInfo   *datReqInfo;   /* Data Request Information */
357    RlcRguSapCb      *rguSap;       /* MAC SAP CB */
358    uint32_t         totNumPdu;     /* Total number of PDUS */
359    RguStaIndTb      *staIndTb = NULLP;
360    RguDatReqTb      *datReqTb = NULLP;
361    RguDStaIndPerUe  *staInd = NULLP;
362 #ifdef LTE_L2_MEAS   
363    uint32_t   grantPerLch[RGU_MAX_LC] = {0};
364 #endif
365 /* kw005.201 added support for L2 Measurement */
366 #ifdef LTE_L2_MEAS_LOSS_DELAY
367    uint8_t        snIdx1;
368    uint8_t        snIdx2;
369 #endif /* LTE_L2_MEAS */
370    uint32_t  idx;
371
372 //Debug
373    uint32_t staIndSz=0,datIndSz = 0;
374
375    datReqInfo = NULLP;
376    RLC_ALLOC_SHRABL_BUF(RLC_MEM_REGION_DL, RLC_POOL,
377                        datReqInfo,sizeof(RguDDatReqInfo));
378 #if (ERRCLASS & ERRCLS_ADD_RES)
379       if ( datReqInfo == NULLP )
380       {
381          DU_LOG("\nRLC: rlcUtlSendToMac: Memory allocation failed");
382          return RFAILED;
383       }
384 #endif /* ERRCLASS & ERRCLS_ADD_RES */
385    for(idx = 0; idx < staIndInfo->nmbOfUeGrantPerTti; idx++)
386    {
387       staInd = &staIndInfo->staInd[idx];
388       /* Fetch Ue control block */
389       GET_UE_IDX(staInd->rnti, ueIdx);
390       if(ROK != rlcDbmFetchDlUeCb(gCb, ueIdx, staIndInfo->cellId,&ueCb))
391       {
392          /* Fetch UeCb failed */
393          DU_LOG("\nRLC: rlcUtlSendToMac: UeId[%u]:ueCb not found",
394             staInd->rnti);
395          /* If ueCb is not found for current rnti then continue to look for next rnti*/
396          continue; 
397       }
398       /* kw002.201 Removed the allocation of RlcDatReq */
399       /* kw004.201 Used SSI function to initialize the variable */
400       memset(&datReq, 0, sizeof(RlcDatReq) ); 
401       totNumPdu = 0;
402       for (numTb = 0; numTb < staInd->nmbOfTbs; numTb++)
403       {
404          staIndTb = &(staInd->staIndTb[numTb]);
405          datReqTb = &(datReqInfo->datReq[idx].datReqTb[numTb]);
406 #ifdef LTE_L2_MEAS
407          ueCb->tbIdx = (ueCb->tbIdx+1) % RLC_MAX_TB_PER_UE;
408 #endif   
409          for (count = 0;count < staIndTb->nmbLch; count++)
410          {
411 #ifdef LTE_L2_MEAS
412             /*Calculate the total grant size from MAC */
413             if((staIndTb->lchStaInd[count].lcId >= RGU_MAX_LC) 
414                 || (staIndTb->lchStaInd[count].lcId == 0))
415             {
416                /* TODO : Need to figure out why this is happening */
417                break;
418             }
419             else
420             {
421                grantPerLch[staIndTb->lchStaInd[count].lcId] += staIndTb->lchStaInd[count].totBufSize;
422             }
423 #endif   
424             rbCb = ueCb->lCh[staIndTb->lchStaInd[count].lcId - 1].dlRbCb;
425
426             if (rbCb && (!rlcDlUtlIsReestInProgress(rbCb)))
427             { 
428 //Debug
429                staIndSz += staIndTb->lchStaInd[count].totBufSize;
430                datReq.pduSz = staIndTb->lchStaInd[count].totBufSize;
431 #ifdef LTE_L2_MEAS            
432                datReq.totMacGrant = grantPerLch[staIndTb->lchStaInd[count].lcId];
433 #endif
434                rlcUtlGetCurrTime(&datReq.boRep.oldestSduArrTime);
435                if ( CM_LTE_MODE_UM == rbCb->mode )
436                {
437                   rlcUmmProcessSdus(gCb,rbCb,&datReq);
438                }
439                else if ( CM_LTE_MODE_AM == rbCb->mode )
440                {
441                   rlcAmmProcessSdus(gCb,rbCb,&datReq,staInd->fillCtrlPdu);
442                }
443 #ifdef LTE_L2_MEAS            
444                grantPerLch[staIndTb->lchStaInd[count].lcId] = datReq.totMacGrant;
445 #endif
446                if ( 0 == datReq.pduInfo.numPdu )
447                {
448                   continue;
449                }
450                totNumPdu += datReq.pduInfo.numPdu;
451                memcpy(&(datReqTb->lchData[count].pdu),
452                   &(datReq.pduInfo),sizeof(KwPduInfo));
453
454                for (;numPdu < datReqTb->lchData[count].pdu.numPdu ; numPdu ++)
455                {
456                   MsgLen len = 0;
457                   ODU_GET_MSG_LEN(datReqTb->lchData[count].pdu.mBuf[numPdu],&len);
458                   datIndSz += len; 
459                }
460                datReqTb->lchData[count].setMaxUlPrio = FALSE;
461                if (RLC_AM_IS_POLL_BIT_SET(AMDL) && 
462                    (AMDL.sduQ.count > 1))
463                { 
464                   /* Poll bit is set indicate to MAC*/
465                   datReqTb->lchData[count].setMaxUlPrio = TRUE;
466                }
467                datReqTb->lchData[count].boReport.bo = datReq.boRep.bo;
468
469 #ifdef CCPU_OPT
470                datReqTb->lchData[count].boReport.estRlcHdrSz = 
471                   datReq.boRep.estHdrSz;
472                datReqTb->lchData[count].boReport.staPduPrsnt = 
473                   datReq.boRep.staPduPrsnt;
474 #endif /* CCPU_OPT */
475                datReqTb->lchData[count].boReport.staPduBo = 
476                   datReq.boRep.staPduBo;
477                datReqTb->lchData[count].lcId = staIndTb->lchStaInd[count].lcId;
478
479 #ifdef L2_OPTMZ
480                /* Set if Bearer is UM */
481                if ( CM_LTE_MODE_UM == rbCb->mode )
482                {
483                   datReqTb->lchData[count].freeBuff = TRUE;
484                }
485                else
486                {
487                   datReqTb->lchData[count].freeBuff = FALSE;
488                }
489 #endif
490
491                /* kw005.201 added support for L2 Measurement */
492 #ifdef LTE_L2_MEAS_LOSS_DELAY
493                datReqTb->rguSnInfo->lchMap[count].lChId =
494                   staIndTb->lchStaInd[count].lcId;
495                /* In some cases L2 Measurement for few of the lcId may be off, 
496                 * in this case we are assigning snList to 0xffff 
497                 */
498                for(snIdx1 = 0; snIdx1 < RGU_MAX_PDU; snIdx1++)
499                {
500                   datReqTb->rguSnInfo->lchMap[count].snList[snIdx1] = 0xffff;
501                }
502                if(tbSnMap->numSn != 0)
503                {
504                   snIdx2 = 0;
505                   for(snIdx1=tbSnMap->prevNumSn;snIdx1 < tbSnMap->numSn;snIdx1++)
506                   {
507                      datReqTb->rguSnInfo->lchMap[count].snList[snIdx2++] = 
508                         tbSnMap->snSduMap[snIdx1].sn;
509                   }
510                   tbSnMap->prevNumSn = tbSnMap->numSn;
511                }
512 #endif
513                datReqTb->lchData[count].boReport.oldestSduArrTime = 
514                                              datReq.boRep.oldestSduArrTime;
515                /* kw004.201 Used SSI function to initialize the variable */
516                memset(&datReq, 0, sizeof(RlcDatReq) ); 
517             }
518          }
519 #ifdef LTE_L2_MEAS
520          if(ueCb->l2MeasTbCb[ueCb->tbIdx]!= NULLP)
521          {
522             datReqTb->tbId = ueCb->tbIdx;
523          }
524          else
525          {
526             datReqTb->tbId = RLC_INVALID_TBID; 
527          }
528 #endif
529          datReqTb->nmbLch = staIndTb->nmbLch;
530          /*adding the check to make sure that lcId is not sent as 0
531           * when no data is there in datReq */
532          if ( 0 == totNumPdu )
533          {
534             datReqTb->lchData[0].lcId = staIndTb->lchStaInd[0].lcId;
535          }
536          /* kw005.201 added support for L2 Measurement */
537 #ifdef LTE_L2_MEAS_LOSS_DELAY
538          if(tbSnMap->numSn == 0)
539          {
540             RLC_FREE(tbSnMap,sizeof(RlcTbSnMap));
541             RLC_FREE(datReqTb->rguSnInfo,sizeof(RguSnMapInfo));
542             datReqTb->rguSnInfo = NULLP;
543             rlcCb.rlcL2Cb.curTbSnMap = NULLP;
544             datReqTb->snMapPres = FALSE;
545          }
546          else
547          {
548             cmHashListInsert(&(rlcCb.rlcL2Cb.tbHlCp),(PTR)tbSnMap,
549                   (uint8_t *) &(tbSnMap->tbId), (uint16_t)sizeof(tbSnMap->tbId));
550             rlcCb.rlcL2Cb.curTbSnMap = NULLP;
551          }
552 #endif /* LTE_L2_MEAS */
553       }
554       datReqInfo->datReq[idx].nmbOfTbs = staInd->nmbOfTbs;
555       datReqInfo->datReq[idx].transId = staInd->transId;
556       datReqInfo->datReq[idx].rnti    = staInd->rnti;
557    }
558    datReqInfo->cellId  = staIndInfo->cellId;
559    datReqInfo->nmbOfUeGrantPerTti = staIndInfo->nmbOfUeGrantPerTti;
560
561    rguSap = &(gCb->u.dlCb->rguDlSap[suId]);
562    rlcSendDedLcDlData(&rguSap->pst,rguSap->spId,datReqInfo); 
563    return ROK;
564 }
565
566 /**
567  *
568  * @brief 
569  *        Handler for sending Status Response to MAC. 
570  *
571  * @detail:
572  *        This function is used by  RLC entity for sending
573  *        status response to MAC after receiving a SDU from
574  *        PDCP.
575  *
576  * @param[in]  gCb           RLC instance Control block
577  * @param[in]  rbCb          Radio Bearer Control Block 
578  * @param[in]  bo            Buffer Occupancy
579  * @param[in]  estHdrSz      Estimated Header Size
580  * @param[in]  staPduPrsnt   Status PDU present or not
581  *
582  * @return  S16
583  *      -# ROK 
584  *      -# RFAILED
585  */
586 uint8_t rlcUtlSendDedLcBoStatus(RlcCb *gCb, RlcDlRbCb *rbCb, int32_t bo, \
587    int32_t estHdrSz, bool staPduPrsnt, uint32_t staPduBo)
588 {
589    Pst           pst;           /* Post info */
590    RlcBoStatus   *boStatus;      /* Buffer occupancy status information */
591
592 #ifndef TENB_ACC
593    if ((rbCb->lastRprtdBoToMac > (uint32_t)8000) && (rbCb->boUnRprtdCnt < (uint32_t)5) 
594        && (!staPduPrsnt) && ((CM_LTE_MODE_AM == rbCb->mode ) && (AMDL.nxtRetx == NULLP)))
595    {
596       rbCb->boUnRprtdCnt++;
597       return ROK;
598    }
599 #endif
600    rbCb->boUnRprtdCnt = (uint32_t)0;
601    rbCb->lastRprtdBoToMac = (uint32_t)bo;
602
603    RLC_ALLOC_SHRABL_BUF(RLC_MEM_REGION_DL, RLC_POOL, \
604       boStatus, sizeof(RlcBoStatus));
605
606    boStatus->cellId = rbCb->rlcId.cellId;
607    boStatus->ueIdx = rbCb->rlcId.ueId;
608    boStatus->commCh = FALSE; 
609    boStatus->lcId = rbCb->lch.lChId;
610    boStatus->bo = bo;
611
612    FILL_PST_RLC_TO_MAC(pst, RLC_DL_INST, EVENT_BO_STATUS_TO_MAC);
613    /* Send Status Response to MAC layer */
614    if(RlcSendBoStatusToMac(&pst, boStatus) != ROK)
615    {
616       RLC_FREE_SHRABL_BUF(pst.region, pst.pool, boStatus, sizeof(RlcBoStatus));
617    }
618
619    return ROK;
620 }
621
622 /**
623  *
624  * @brief 
625  *        Handler for emptying the SDU queue. 
626  * 
627  * @detail:
628  *        This function is used to empty the SDU queue when
629  *        a re-establishment request is received from the 
630  *        upper layer.
631  *
632  * @param[in]  gCb    RLC instance control block
633  * @param[in]  rbCb   Radio bearer control block
634  * @param[in]  sduQ   SDU queue to be emptied
635  *
636  * @return Void 
637  */
638 /* kw005.201 added support for L2 Measurement */
639 #ifdef LTE_L2_MEAS_RLC
640 Void rlcUtlEmptySduQ
641 (
642 RlcCb        *gCb, 
643 RlcDlRbCb    *rbCb,
644 CmLListCp   *sduQ 
645 )
646 #else
647 Void rlcUtlEmptySduQ
648 (
649 RlcCb        *gCb,
650 CmLListCp   *sduQ 
651 )
652 #endif
653 {
654 #ifdef LTE_L2_MEAS_RLC
655    CmLListCp   *sduSnMapQ;             /* SDU Sequence number map queue */
656    CmLList     *firstSduSnMap;         /* First Node in SDU SnMap Queue */
657    RlcSduSnMap  *sduSnMap;              /* SDU Sn Map */
658
659    sduSnMapQ = NULLP;
660    firstSduSnMap = NULLP;
661    sduSnMap = NULLP;
662
663
664    sduSnMapQ = &rbCb->sduSnMapQ;
665    CM_LLIST_FIRST_NODE(sduSnMapQ, firstSduSnMap);
666
667    while(firstSduSnMap)
668    {
669       sduSnMap = (RlcSduSnMap *)firstSduSnMap->node;
670       if(sduSnMap != NULLP)
671       {
672          cmLListDelFrm(&(rbCb->sduSnMapQ), &(sduSnMap->lstEnt));
673          RLC_FREE(sduSnMap, sizeof(RlcSduSnMap));
674          CM_LLIST_FIRST_NODE(sduSnMapQ, firstSduSnMap);
675       }
676       else
677       {
678          CM_LLIST_NEXT_NODE(sduSnMapQ, firstSduSnMap);
679       }
680    }
681 #endif
682    return;
683 }
684
685 /**
686  *
687  * @brief 
688  *    Handler for calculating the Length Indicator (LI) length for a SDU 
689  * 
690  * @detail:
691  *    This function is used to calculate the LI (Length Indicator) length
692  *    which has to be substracted from the pduSize to correctly match the
693  *    formed PDU(s) size with the  size requested by MAC.
694  *
695  * @param[in]      gCb      RLC instance control block 
696  * @param[in]      numLi    Number of LIs already present 
697  * @param[in]      msgLen   Size of the SDU
698  * @param[in/out]  pduSz    Size of the pDU to be formed
699  *
700  * @return  void 
701  */
702 void rlcUtlCalcLiForSdu(RlcCb *gCb, uint16_t numLi, MsgLen msgLen, int16_t *pduSz)
703 {
704    if ( (*pduSz > msgLen)  && (msgLen < RLC_2K_BYTE))
705    {
706       if(0 == (numLi & RLC_BIT0)) /* check if number of LIs are odd or even */
707       {  
708          /* if number of LI's are even 2 bytes needed */
709          *pduSz -= 2;
710       }
711       else
712       {
713          /* if number of LI's are odd one byte needed */
714          *pduSz -= 1;
715       }
716    }
717    return;
718 }
719
720 /**
721  *
722  * @brief 
723  *    Function to set that re-establishment has started for an RB
724  * 
725  * @detail:
726  *    This function is used to set the reestInProgress flag to TRUE.
727  *    This also sets the estimated header size to 0 and sends bo as
728  *    0 to MAC so that RLC does not need to transmit any data.
729  *    If the poll re-transmit timer is running for the RB;
730  *    it is stopped
731  *
732  * @param[in]      gCb      RLC instance control block 
733  * @param[in]      rbCb     RB for which re-establishment has started 
734  *
735  * @return  void 
736  */
737 Void rlcDlUtlSetReestInProgressForRB(RlcCb *gCb,RlcDlRbCb *rbCb)
738 {
739       
740    rbCb->reestInProgress = TRUE;
741    
742    if(rbCb->mode == CM_LTE_MODE_AM )
743    {
744       rbCb->m.amDl.estHdrSz = 0;
745
746       if(rlcChkTmr(gCb, (PTR)rbCb, RLC_EVT_AMDL_POLL_RETX_TMR))
747       {
748          rlcStopTmr(gCb, (PTR)rbCb, RLC_EVT_AMDL_POLL_RETX_TMR);
749       }
750    }
751    else
752    {
753       rbCb->m.umDl.estHdrSz= 0;
754    }
755
756    rlcUtlSendDedLcBoStatus(gCb, rbCb, 0, 0, FALSE,0);
757
758    return;
759 }
760
761 /**
762  *
763  * @brief 
764  *    Function to check if re-establishment is ongoing for an RB
765  * 
766  * @param[in]      rbCb     RB for which re-establishment is to be checked 
767  *
768  * @return  Bool
769  *          TRUE  : Re-establishment is in progress
770  *          FALSE : Re-establishment is not in progress
771  */
772 bool rlcDlUtlIsReestInProgress(RlcDlRbCb *rbCb)
773 {
774    return (rbCb->reestInProgress);
775 }
776
777 /**
778  *
779  * @brief 
780  *    Function to set re-establishment to FALSE
781  * 
782  * @param[in]      rbCb     RB for which re-establishment is to be reset 
783  *
784  * @return  Void
785  */
786 Void rlcDlUtlResetReestInProgress(RlcDlRbCb *rbCb)
787 {
788
789    rbCb->reestInProgress = FALSE;
790 }
791
792 /**
793  *
794  * @brief 
795  *    Function to set that re-establishment has started for all the RBs
796  *    of an UE; except for SRB1
797  *
798  * @detail: For SRB1 only the poll-retransmit timer is stopped
799  * 
800  * @param[in]      gCb      RLC instance control block 
801  * @param[in]      ueCb     UE for which re-establishment has started 
802  *
803  * @return  void 
804  */
805 Void rlcDlUtlSetReestInProgressForAllRBs(RlcCb *gCb,RlcDlUeCb *ueCb)
806 {
807    uint32_t        rbIdx;
808    
809    for(rbIdx = 0;rbIdx < RLC_MAX_SRB_PER_UE;rbIdx++)
810    {
811       if(ueCb->srbCb[rbIdx] != NULLP)
812       {
813          if(ueCb->srbCb[rbIdx]->rlcId.rbId != 1)
814          {
815             rlcDlUtlSetReestInProgressForRB(gCb,ueCb->srbCb[rbIdx]); 
816          }
817          else
818          {
819             /* For SRB we just need to stop the poll re-transmit timer */
820             if(rlcChkTmr(gCb, (PTR)ueCb->srbCb[rbIdx], RLC_EVT_AMDL_POLL_RETX_TMR))
821             {
822                rlcStopTmr(gCb, (PTR)ueCb->srbCb[rbIdx], RLC_EVT_AMDL_POLL_RETX_TMR);
823             }
824          }
825       }
826    }
827    
828    for(rbIdx = 0;rbIdx < RLC_MAX_DRB_PER_UE;rbIdx++)
829    {
830       if(ueCb->drbCb[rbIdx] != NULLP)
831       {
832          rlcDlUtlSetReestInProgressForRB(gCb,ueCb->drbCb[rbIdx]);
833       }
834    }
835       
836    return;
837 }
838
839 /**
840  * @brief  Function to increment number of SDUs transmitted 
841  *         in KWU SAP statistics
842  *
843  *
844  * @param[in]  rlckwuSap     KWU SAP in which to increment the counter
845  *
846  * @return  Void
847  */
848 void rlcUtlIncrementKwuStsSduTx(RlcKwuSapCb *rlckwuSap)
849 {
850    rlckwuSap->sts.sduTx++;
851    return;
852 }
853
854 /**
855  * @brief  Function to increment number of bytes and PDUs transmitted 
856  *         in General statistics
857  *
858  *
859  * @param[in]  genSts   KWU SAP in which to increment the counter
860  * @param[in]  pdu      The PDU which is sent
861  *
862  * @return  Void
863  */
864 void rlcUtlIncrementGenStsBytesAndPdusSent(RlcGenSts *genSts, Buffer *pdu)
865 {
866    MsgLen bytesSent;
867    ODU_GET_MSG_LEN(pdu, &bytesSent);
868    genSts->bytesSent += bytesSent;
869    genSts->pdusSent++;
870    return;
871 }
872
873 /**
874  * @brief  Function to initialize the data structures used to free memory
875  *
876  *
877  * @param[in]  gCb        RLC instance control block
878  * @param[out] toBeFreed  Pointer to the freeing structure. This is 
879  *                        initialized here
880  *
881  * @return  Void
882  */
883 Void rlcUtlInitToBeFreed(RlcCb *gCb,RlcDlDataToBeFreed *toBeFreed)
884 {
885    cmLListInit(&(toBeFreed->sduLst));
886    cmLListInit(&(toBeFreed->rbLst));
887    cmLListInit(&(toBeFreed->reTxLst));
888    cmLListInit(&(toBeFreed->txLst));
889    
890    return;
891 }
892
893 /**
894  * @brief  Function to initialize the DL self Pst structure
895  *
896  *
897  * @param[in]  gCb   RLC instance control block
898  *
899  * @return  Void
900  */
901 Void rlcUtlInitializeSelfPst(RlcCb *gCb)
902 {
903    Pst *selfPst = &gCb->u.dlCb->selfPst;
904    
905    RLC_MEM_SET(selfPst, 0, sizeof(Pst));
906    selfPst->srcProcId = SFndProcId();
907    selfPst->dstProcId = SFndProcId();
908    selfPst->dstEnt = gCb->init.ent;
909    selfPst->dstInst = gCb->init.inst; /* this is for the DL instance */
910    selfPst->srcEnt = gCb->init.ent;
911    selfPst->srcInst = gCb->init.inst;  /* DL instance will send to itself */
912    selfPst->prior  = PRIOR3;
913    selfPst->event  = UDX_EVT_DL_CLEANUP_MEM;
914 }
915
916 /**
917  * @brief  Function to send a DL cleanup event
918  *
919  *
920  * @param[in]  gCb   RLC instance control block
921  *
922  * @return  Void
923  */
924 void rlcUtlRaiseDlCleanupEvent(RlcCb *gCb)
925 {
926 #ifdef KWSELFPSTDLCLEAN
927    if(!gCb->u.dlCb->eventInQueue)
928    {
929       ODU_POST_TASK(&gCb->u.dlCb->selfPst, gCb->u.dlCb->selfPstMBuf);
930       gCb->u.dlCb->eventInQueue = TRUE;
931    }
932 #endif
933    return;
934 }
935
936 /**
937  * @brief  Function to add a SDU to the to be freed sdu list
938  *
939  *
940  * @param[in]  gCb   RLC instance control block
941  * @param[in]  sdu   SDU to be added to the list
942  *
943  * @return  Void
944  */
945 void rlcUtlAddSduToBeFreedQueue(RlcCb *gCb, RlcSdu *sdu)
946 {
947    cmLListAdd2Tail(&(gCb->u.dlCb->toBeFreed.sduLst), &(sdu->lstEnt));
948    return;
949 }
950
951 /**
952  * @brief  Function to add a re-transmitted pdu to the to be freed list
953  *
954  *
955  * @param[in]  gCb   RLC instance control block
956  * @param[in]  retx  Re-transmitted pdu to be added to the list
957  *
958  * @return  Void
959  */
960 Void rlcUtlAddReTxPduToBeFreedQueue(RlcCb *gCb,RlcRetx *retx)
961 {
962    cmLListAdd2Tail(&(gCb->u.dlCb->toBeFreed.reTxLst), &(retx->lstEnt));
963    return;
964 }
965
966 /**
967  * @brief  Function to add a transmitted pdu to the to be freed list
968  *
969  *
970  * @param[in]  gCb   RLC instance control block
971  * @param[in]  pdu   PDU to be added to the list
972  *
973  * @return  Void
974  */
975 Void rlcUtlAddTxPduToBeFreedQueue(RlcCb   *gCb,RlcTx   *pdu)
976 {
977    pdu->rlsLnk.node = (PTR)pdu;
978    cmLListAdd2Tail(&(gCb->u.dlCb->toBeFreed.txLst), &(pdu->rlsLnk));
979    return;
980 }
981
982 /*
983  * @brief
984  *    function to free/release the Acknowledged mode RBCB buffers
985  *
986  * @details
987  *    This primitive Frees the Acknowledged Mode RbCb transmission Buffer,
988  *    retransmission Buffer and reciption Buffers
989  *
990  * @param [in]      gCb       - RLC instance control block
991  * @param [in]      rbCb      - Downlink RB Control Block
992  * @param [in,out]  toBeFreed - Number of buffers to be freed
993  *
994  * @return Bool
995  *     - TRUE if more data to be freed
996  *     - FALSE if all the data has been freed
997  */
998 static Bool rlcUtlFreeDlAmRbMemory(RlcCb *gCb,RlcDlRbCb *rbCb,uint32_t *toBeFreed)
999 {
1000    RlcRetx   *retx;   /* retransmission buffer */
1001    RlcSn     mTxNext;    /* send state variable */
1002    RlcTx      *txBuf;
1003
1004    MODAMT(AMDL.txNext, mTxNext, AMDL.txNextAck,AMDL.snModMask);
1005
1006    /* TODO : to be checked changed from <= to < */
1007    while ((0 < mTxNext) && *toBeFreed)
1008    {
1009       txBuf =  rlcUtlGetTxBuf(AMDL.txBufLst, AMDL.txNextAck);
1010       if (txBuf && txBuf->pduLst.first)
1011       {
1012          while(txBuf->pduLst.first)
1013          {
1014             RlcDlPduInfo *pduInfo = (RlcDlPduInfo *)(txBuf->pduLst.first->node);
1015             RLC_FREE_BUF(pduInfo->pdu);
1016             /* Delete node from the txBuf Pdu lst */
1017             cmLListDelFrm(&txBuf->pduLst, txBuf->pduLst.first);
1018             RLC_FREE_WC(gCb, pduInfo, sizeof(RlcDlPduInfo));
1019          }
1020          rlcUtlDelTxBuf(AMDL.txBufLst, txBuf, gCb);
1021          if(gCb->u.dlCb->shutdownReceived == 0)
1022          {   
1023             (*toBeFreed)--;
1024          }
1025       }
1026       AMDL.txNextAck = (AMDL.txNextAck + 1) & AMDL.snModMask;
1027       MODAMT(AMDL.txNext, mTxNext, AMDL.txNextAck,AMDL.snModMask);
1028    }
1029    if(*toBeFreed == 0)
1030    {
1031       return (TRUE);
1032    }
1033    
1034 #ifndef LTE_TDD 
1035       RLC_FREE(gCb,AMDL.txBufLst, (RLC_TX_BUF_BIN_SIZE * sizeof(CmLListCp)));
1036 #endif
1037
1038    RLC_LLIST_FIRST_RETX(AMDL.retxLst, retx);
1039    while (retx && (*toBeFreed)) /* Till to be freed becomes 0 */
1040    {
1041
1042       RLC_FREE_BUF(retx->seg);
1043
1044       cmLListDelFrm(&AMDL.retxLst, &retx->lstEnt);
1045       RLC_FREE_WC(gCb, retx, sizeof(RlcRetx));
1046
1047       RLC_LLIST_FIRST_RETX(AMDL.retxLst, retx);
1048       if(gCb->u.dlCb->shutdownReceived == 0)
1049       {
1050       (*toBeFreed)--;
1051    }
1052
1053    }
1054
1055    AMDL.nxtRetx = NULLP;
1056
1057    /* clean up if there is info about STATUS PDU to be sent */
1058    if(AMDL.pStaPdu)
1059    {
1060       Pst *udxPst;
1061       udxPst = &gCb->u.dlCb->udxDlSap->pst;
1062       RLC_FREE_SHRABL_BUF_WC(udxPst->region,
1063                             udxPst->pool,
1064                             AMDL.pStaPdu, 
1065                             sizeof(RlcUdxDlStaPdu));
1066       AMDL.pStaPdu = NULLP;
1067    }
1068
1069    if(*toBeFreed == 0)
1070    {
1071       return (TRUE);
1072    }  
1073    if(gCb->u.dlCb->shutdownReceived)
1074    {   
1075       (*toBeFreed)--;
1076    }
1077    
1078    return (FALSE);
1079
1080
1081 /**
1082  * @brief  Function to free memory from the DL instance
1083  *
1084  *
1085  * @param[in]  gCb   RLC instance control block
1086  *
1087  * @return  Void
1088  */
1089 Void rlcUtlFreeDlMemory(RlcCb *gCb)
1090 {
1091    uint32_t toBeFreed; 
1092
1093    /* safety check, in case some event was still lying in the queue after
1094       the dlCb was deleted*/
1095    if(!gCb->u.dlCb)
1096    {
1097       return;
1098    }
1099
1100    RlcDlDataToBeFreed* pToBeFreed = &gCb->u.dlCb->toBeFreed;
1101    /* ccpu00136940 */
1102    if(gCb->u.dlCb->shutdownReceived)
1103    {
1104       toBeFreed = pToBeFreed->txLst.count + pToBeFreed->reTxLst.count + pToBeFreed->sduLst.count + pToBeFreed->rbLst.count; 
1105    }
1106    else
1107    {
1108       if ((pToBeFreed->txLst.count + pToBeFreed->reTxLst.count + pToBeFreed->sduLst.count) > (3 * RLC_MAX_TO_BE_FREED))
1109       {
1110 #if !defined(KWSELFPSTDLCLEAN) && defined(MAC_RLC_HARQ_STA_RBUF) && defined(LTE_L2_MEAS)
1111          if (isDatReqProcessed)
1112          {
1113             toBeFreed = (2 *RLC_MAX_TO_BE_FREED);
1114          }
1115          else
1116 #endif
1117          {
1118             toBeFreed = (3 *RLC_MAX_TO_BE_FREED)/2;
1119          }
1120       }
1121       else
1122       {
1123          toBeFreed = RLC_MAX_TO_BE_FREED;
1124       }
1125    }   
1126    CmLListCp        *lst;
1127
1128    gCb->u.dlCb->eventInQueue = FALSE; /* reset as we have received the event
1129                                          and are processing it */
1130
1131    /* Free from the ReTx list */
1132    lst  = &pToBeFreed->reTxLst;
1133 #ifndef L2_OPTMZ
1134    while((lst->first) && toBeFreed && (pToBeFreed->reTxLst.count > 100))
1135 #else
1136    while((lst->first) && toBeFreed)
1137 #endif
1138    {
1139       RlcRetx* seg = (RlcRetx *)(lst->first->node);
1140       cmLListDelFrm(lst, lst->first);
1141       RLC_FREE_BUF_WC(seg->seg);
1142       RLC_FREE_WC(gCb,seg, sizeof(RlcRetx));
1143       toBeFreed--;
1144    }   
1145
1146    /* Free from the Tx list */
1147    lst  = &pToBeFreed->txLst;
1148 #ifndef L2_OPTMZ
1149    while((lst->first) && toBeFreed && (pToBeFreed->txLst.count > 100))
1150 #else
1151    while((lst->first) && toBeFreed)
1152 #endif
1153    {
1154       RlcTx* pdu = (RlcTx *)(lst->first->node);
1155       cmLListDelFrm(lst, lst->first);
1156       while(pdu->pduLst.first)
1157       {
1158          RlcDlPduInfo *pduInfo = (RlcDlPduInfo *)(pdu->pduLst.first->node);
1159          
1160          cmLListDelFrm(&pdu->pduLst, pdu->pduLst.first);
1161          RLC_FREE_BUF_WC(pduInfo->pdu);
1162          RLC_FREE_WC(gCb, pduInfo, sizeof(RlcDlPduInfo));
1163       }
1164       RLC_FREE_WC(gCb,pdu, sizeof(RlcTx));
1165       toBeFreed--;
1166    }
1167
1168    /* Free from the SDU queue */
1169    lst  = &pToBeFreed->sduLst;
1170 #ifndef L2_OPTMZ
1171    while((lst->first) && toBeFreed && (pToBeFreed->sduLst.count > 100))
1172 #else
1173    while((lst->first) && toBeFreed)
1174 #endif
1175    {
1176       RlcSdu* sdu = (RlcSdu *)(lst->first->node);
1177       RLC_RMV_SDU(gCb, lst, sdu);
1178       toBeFreed--;
1179    }      
1180
1181    /* Free from the RBs */
1182    lst  = &pToBeFreed->rbLst;
1183 #ifndef L2_OPTMZ
1184    while((lst->first) && toBeFreed && (pToBeFreed->rbLst.count > 100))
1185 #else
1186    while((lst->first) && toBeFreed)
1187 #endif
1188    {
1189       RlcDlRbCb* rbCb = (RlcDlRbCb *)(lst->first->node);
1190       Bool moreToBeFreed = rlcUtlFreeDlAmRbMemory(gCb, rbCb,&toBeFreed);
1191       if(!moreToBeFreed)
1192       {
1193          cmLListDelFrm(lst, lst->first);
1194          RLC_FREE_WC(gCb, rbCb, sizeof(RlcDlRbCb));
1195       }
1196    } 
1197
1198    if ((toBeFreed == 0) && !(gCb->u.dlCb->shutdownReceived))
1199    {
1200       rlcUtlRaiseDlCleanupEvent(gCb);
1201    }
1202
1203    return;
1204 }
1205
1206
1207 #ifdef LTE_L2_MEAS
1208 /**
1209  *
1210  * @brief Function to initialise measurement
1211  *
1212  * @b Description
1213  *
1214  * @param[in]  gCb     RLC Instance Control Block
1215  *
1216  *  @return  Void
1217  *
1218  */
1219 S16 rlcUtlL2MeasDlInit(RlcCb *gCb)
1220 {
1221    uint16_t   cntr;
1222
1223    gCb->u.dlCb->rlcL2Cb.rlcNumMeas=0;
1224    for(cntr = 0; cntr < LKW_MAX_L2MEAS; cntr++)
1225    {
1226       memset(&(gCb->u.dlCb->rlcL2Cb.rlcL2EvtCb[cntr]), 0, sizeof(RlcL2MeasEvtCb));
1227    }
1228    gCb->u.dlCb->rlcL2Cb.rlcL2EvtCb[RLC_L2MEAS_DL_DISC].measCb.measType = LKW_L2MEAS_DL_DISC;
1229    gCb->u.dlCb->rlcL2Cb.rlcL2EvtCb[RLC_L2MEAS_DL_IP].measCb.measType = LKW_L2MEAS_DL_IP;
1230    gCb->u.dlCb->rlcL2Cb.rlcL2EvtCb[RLC_L2MEAS_DL_DELAY].measCb.measType= LKW_L2MEAS_DL_DELAY;
1231    gCb->u.dlCb->rlcL2Cb.rlcL2EvtCb[RLC_L2MEAS_UU_LOSS].measCb.measType= LKW_L2MEAS_UU_LOSS;
1232    gCb->u.dlCb->rlcL2Cb.rlcL2EvtCb[RLC_L2MEAS_ACT_UE].measCb.measType= LKW_L2MEAS_ACT_UE;
1233
1234    return ROK;
1235 }
1236 /**
1237  *
1238  * @brief Function to detect the data Burst start Condition in a DTCH
1239  *
1240  * @b Description
1241  *
1242  * @param[in]  rbCb     RB control block
1243  * @param[in]  contSduLst  Array of Contained SDUs in the DTCH   
1244  * @param[in]  dataVol  Available data in the DTCH
1245  * @param[in]  schPduSz    Total grant Size given by MAC
1246  *
1247  *  @return  Void
1248  *
1249  */
1250 Void rlcUtlUpdateBurstSdus
1251 (
1252 RlcCb            *gCb,
1253 RlcDlRbCb        *rbCb,
1254 RlcContSduLst    *contSduLst,
1255 S32              dataVol,
1256 uint32_t         schPduSz
1257 )
1258 {
1259
1260    RlcL2MeasDlIpTh   *l2MeasDlIpThruput = NULLP;
1261    RlcL2MeasTb       *l2MeasTb = NULLP;
1262    uint8_t           idx;
1263    uint8_t           currTbIdx;
1264    volatile uint32_t startTime = 0;
1265    RlcContSduLst   *dstContSduLst;
1266
1267
1268    /*starting Task*/
1269    SStartTask(&startTime, PID_RLC_DLIP_TPT_BURSTCALC);
1270
1271    l2MeasDlIpThruput = &rbCb->l2MeasIpThruput.dlIpTh;
1272
1273    if(RLC_MEAS_IS_DL_IP_MEAS_ON_FOR_RB(gCb, rbCb))
1274    {
1275       if(dataVol > schPduSz)
1276       {
1277          if(l2MeasDlIpThruput->isBurstAct == FALSE)
1278          {
1279             l2MeasDlIpThruput->burstStartTime = glblTtiCnt;
1280             l2MeasDlIpThruput->isBurstAct = TRUE;
1281             l2MeasDlIpThruput->burstEndSduId = 0;
1282          }
1283          else
1284          {  /* This is the case when another burst started before RLC gets the 
1285                l2MeasDlIpThruput->burstEndSduId = 0; */
1286          }
1287       }
1288       else
1289       {  /* Store the burstEndSduId here */
1290          if((l2MeasDlIpThruput->isBurstAct == TRUE) &&
1291                (!l2MeasDlIpThruput->burstEndSduId))
1292          {
1293             l2MeasDlIpThruput->burstEndSduId = 
1294                l2MeasDlIpThruput->outStngSduArr[l2MeasDlIpThruput->lastSduIdx].sduId;
1295          }
1296       }
1297       if(l2MeasDlIpThruput->isBurstAct == TRUE)
1298       {
1299          l2MeasTb = rlcUtlGetCurMeasTb(gCb,rbCb);
1300          /* Get the lChId from index 0, because index 0 is always for DL */
1301          if(l2MeasTb->numLcId >= RLC_MAX_ACTV_DRB) 
1302          {
1303             /* ccpu00143043 */
1304             return;
1305          }
1306          l2MeasTb->sduInfo[l2MeasTb->numLcId].lcId = rbCb->lch.lChId;
1307          /* Copy all the sduIdx from sduInfo to tb sduInfo */
1308          currTbIdx = l2MeasTb->sduInfo[l2MeasTb->numLcId].numSdus;
1309          dstContSduLst = &l2MeasTb->sduInfo[l2MeasTb->numLcId];
1310          /* ccpu00143043 */
1311          for(idx = 0; ((idx < contSduLst->numSdus)
1312                   && (currTbIdx < RLC_L2MEAS_MAX_OUTSTNGSDU)) ; idx++)
1313          {
1314             dstContSduLst->sduIdx[currTbIdx++] = contSduLst->sduIdx[idx];
1315          }
1316          l2MeasTb->sduInfo[l2MeasTb->numLcId].numSdus += idx;
1317          l2MeasTb->numLcId++;
1318       }/* End of isBurstAct */
1319    }/* End of if measOn */
1320
1321    /*stopping Task*/
1322    SStopTask(startTime, PID_RLC_DLIP_TPT_BURSTCALC);
1323    return;
1324 }
1325 /**
1326  * @brief
1327  *        This function is used to store locally the sduIdx of the sdu in the 
1328  *        outstanding SDU array
1329  * 
1330  * @b Description:
1331  *        Stores the Sdu Idx in the contained  SDU Array and increments 
1332  *        the num contained  Sdus 
1333  *
1334  *  @param[in]  sduIdx  the Index of the SDU in the outstanding SDU array
1335  *  @param[out] contSduLst This stores the indices of the SDUs 
1336  *
1337  *  @return  Void
1338  */
1339 Void rlcUtlUpdateContainedSduLst(uint8_t sduIdx,RlcContSduLst *contSduLst)
1340 {
1341    if (contSduLst->numSdus < RLC_L2MEAS_MAX_OUTSTNGSDU)
1342    {
1343     contSduLst->sduIdx[contSduLst->numSdus] = sduIdx;
1344     contSduLst->numSdus++;
1345    }
1346     return;
1347 }
1348
1349 /**
1350  * @brief
1351  *        This function is used to store the sduId of the sdu in the 
1352  *        outstanding SDU array
1353  * 
1354  * @b Description:
1355  *        Stores the Sdu Id in the outstanding SDU Array and increments 
1356  *        the num contained  Sdus 
1357  *
1358  *  @param[out] dlIpThPut  The structure in which the outstanding sdus are 
1359  *                         updated
1360  *  @param[in] sduIdx   The Idx at which the sdu ID is stored  
1361  *  @param[in] sduLen   The size if sdu in bytes  
1362  *  @param[in] newIdx   Indicates if the sdu is already present in the 
1363  *                      outstanding array  
1364  *
1365  *  @return  Void
1366  */
1367 Void rlcUtlUpdateOutStandingSduLst
1368 (
1369 RlcL2MeasDlIpTh   *dlIpThPut, 
1370 uint8_t           sduIdx, 
1371 MsgLen            sduLen, 
1372 uint32_t          sduId,
1373 Bool              newIdx
1374 )
1375 {
1376    if (sduIdx < RLC_L2MEAS_MAX_OUTSTNGSDU)
1377    {
1378    if(newIdx == TRUE)
1379    {      
1380      dlIpThPut->outStngSduArr[sduIdx].numTb = 0;
1381    }
1382    dlIpThPut->outStngSduArr[sduIdx].numTb++;
1383    dlIpThPut->outStngSduArr[sduIdx].sduId = sduId;
1384    dlIpThPut->outStngSduArr[sduIdx].sduLen = sduLen;
1385    }
1386    return;
1387 }
1388 RlcL2MeasTb * rlcUtlGetCurMeasTb(RlcCb *gCb,RlcDlRbCb *rbCb)
1389 {
1390    RlcL2MeasTb  *curL2MeasTb;
1391    uint16_t         idx;
1392
1393    if((curL2MeasTb = rbCb->ueCb->l2MeasTbCb[rbCb->ueCb->tbIdx]) == NULLP)
1394       {
1395          /* Intentionally avoiding the RLC_ALLOC macro to avoid  memset */
1396          if (SGetSBuf(gCb->init.region,
1397                   gCb->init.pool,
1398                   (Data **)&curL2MeasTb,
1399                   (Size)sizeof(RlcL2MeasTb)) != ROK)
1400          {
1401             return (NULLP);
1402          }
1403          rbCb->ueCb->l2MeasTbCb[rbCb->ueCb->tbIdx] = curL2MeasTb;
1404          /* Initialize the Meas Tb details */
1405          curL2MeasTb->numLcId = 0;
1406          curL2MeasTb->numLchInfo = 0;
1407          curL2MeasTb->txSegSduCnt = 0;
1408          for (idx = 0; idx < RLC_MAX_ACTV_DRB; idx++)
1409          {
1410             curL2MeasTb->sduInfo[idx].numSdus = 0;
1411          }
1412          for (idx = 0; idx < RLC_MAX_ACTV_DRB; idx++)
1413          {
1414             curL2MeasTb->lchInfo[idx].numSdus = 0;
1415          }
1416       }
1417    return (curL2MeasTb);
1418 }
1419
1420
1421 /**
1422  *
1423  * @brief Handler for Processing harq status indication
1424  *
1425  *
1426  * @b Description
1427  *        This function is called when the MAC sends a harq ind Mesage.
1428  *        This is used only for UuLoss and Dl Delay and DL Ipthoughput
1429  *        L2 Measurements.
1430  *
1431  *  @param[in] staInd Harq status indication received from MAC.
1432  *  @param[in] ueCb   UeCb corresponding to the Tb Id.
1433  *  @param[in] tbIdx  TB index, 0 for SISO and 0,1 for MIMO.
1434  *
1435  *
1436  *  @return  S16
1437  *      -# ROK
1438  */
1439
1440 S16 rlcUtlProcHarqInd
1441 (
1442 RlcCb            *gCb,
1443 RguHarqStatusInd *hqStaInd,
1444 RlcDlUeCb        *ueCb,
1445 uint8_t          tbIdx
1446 )
1447 {
1448 #ifdef LTE_L2_MEAS
1449    RlcDlRbCb        *rlcRbCb;           /* KW Control Block */
1450    RlcL2MeasTb       *l2MeasTb = NULLP;         /* Measurement TbCb */
1451    uint8_t           lcIdx;             /* Logical channel id index */
1452    uint8_t           sduIndx;            /* sdu index to out standing sdu list in rbCb */
1453    uint32_t          numSdus;           /* number of sdus in the outstanding sdu list */
1454    RlcOutStngSduInfo *outStngSduArr;  /* Outstanding sdu list */
1455    Ticks             ackTime;
1456    Ticks             delay;
1457    uint32_t          totlSduCnt = 0;
1458 #endif
1459    uint8_t           timeAddedFlag;
1460    S16               ret;
1461    volatile uint32_t     startTime = 0;
1462    /*kw005.201 Code added for DL IP thruput measurement*/
1463
1464    /*starting Task*/
1465    SStartTask(&startTime, PID_RLC_DLIP_TPT_PRCHARQIND);
1466
1467    ret = ROK;
1468    if(hqStaInd->tbId[tbIdx] >= RLC_INVALID_TBID)
1469    {
1470       return ROK;
1471    }
1472
1473    /* Find the L2 measurement tbCb to process DL Ip thruput*/
1474    l2MeasTb = ueCb->l2MeasTbCb[hqStaInd->tbId[tbIdx]];
1475    if(l2MeasTb == NULLP)
1476    {
1477       return ROK;
1478    }
1479    /* For each logical channel in the tbCb, process 
1480     * and get the DL IP thruput */
1481    ackTime = SGetTtiCount();
1482    for(lcIdx = 0; ((lcIdx < l2MeasTb->numLcId) && (lcIdx < RLC_MAX_ACTV_DRB)); lcIdx++)
1483    {
1484       timeAddedFlag = FALSE;
1485       if((rlcRbCb = ueCb->lCh[l2MeasTb->sduInfo[lcIdx].lcId - 1].dlRbCb)
1486             == NULLP)
1487       {
1488          continue;
1489       }
1490       /* fix for DL IP stop*/
1491       if (!gCb->u.dlCb->rlcL2Cb.measOn[rlcRbCb->qci]
1492             || (rlcRbCb->rlcId.rbType == CM_LTE_SRB))
1493       {
1494          continue;
1495       }
1496       
1497       /* Get the outstanding SDUs using sdu index stored in Container sduList
1498        * and check for HARQ ACK/NACK */
1499       numSdus = l2MeasTb->sduInfo[lcIdx].numSdus;
1500       /* ccpu00143043 */
1501       if ((numSdus >= RLC_L2MEAS_MAX_OUTSTNGSDU) || (numSdus == 0))
1502       {
1503          break;
1504       }
1505       totlSduCnt += numSdus;
1506
1507       if (RLC_MEAS_IS_DL_IP_MEAS_ON_FOR_RB(gCb,rlcRbCb))
1508       {
1509          for(sduIndx = 0; sduIndx < numSdus; sduIndx++)
1510          {
1511             outStngSduArr =&(rlcRbCb->l2MeasIpThruput.dlIpTh.outStngSduArr[\
1512                   l2MeasTb->sduInfo[lcIdx].sduIdx[sduIndx]]);
1513             if(hqStaInd->status[tbIdx] == TRUE)
1514             {
1515                /* If ACK is for burst End Sdu Id set burstActive 
1516                 * to FALSE  and accumulate time */
1517                if((rlcRbCb->l2MeasIpThruput.dlIpTh.burstEndSduId == 
1518                         outStngSduArr->sduId) && (outStngSduArr->numTb == 1))
1519                {
1520                   rlcRbCb->l2MeasIpThruput.dlIpTh.isBurstAct = FALSE;
1521                   /*Update the l2Sts structure for calculating throughput*/
1522                   rlcRbCb->rbL2Cb.l2Sts[RLC_L2MEAS_DL_IP]->dlIpThruput.volSummation
1523                      += outStngSduArr->sduLen;
1524
1525                   rlcRbCb->rbL2Cb.l2Sts[RLC_L2MEAS_DL_IP]->dlIpThruput.timeSummation
1526                      += glblTtiCnt - rlcRbCb->l2MeasIpThruput.dlIpTh.burstStartTime;
1527                   outStngSduArr->sduId = 0;
1528                   outStngSduArr->sduLen = 0;
1529                   outStngSduArr->numTb = 0;
1530                   rlcRbCb->l2MeasIpThruput.dlIpTh.burstEndSduId = 0;
1531                }
1532
1533                /* If burst is active and this sdu is only transmitted in single TB then
1534                 * accumulate volume  and clear the outstanding sduList */
1535                if((rlcRbCb->l2MeasIpThruput.dlIpTh.isBurstAct == TRUE) &&
1536                      (--(outStngSduArr->numTb) == 0))
1537                {
1538                   rlcRbCb->rbL2Cb.l2Sts[RLC_L2MEAS_DL_IP]->dlIpThruput.volSummation
1539                      += outStngSduArr->sduLen;
1540
1541                   if(timeAddedFlag == FALSE)
1542                   {
1543                      rlcRbCb->rbL2Cb.l2Sts[RLC_L2MEAS_DL_IP]->dlIpThruput.timeSummation
1544                         += glblTtiCnt - rlcRbCb->l2MeasIpThruput.dlIpTh.burstStartTime;
1545                      rlcRbCb->l2MeasIpThruput.dlIpTh.burstStartTime = glblTtiCnt;
1546                      timeAddedFlag = TRUE;
1547                   }
1548                   outStngSduArr->sduId = 0;
1549                   outStngSduArr->sduLen = 0;
1550                }
1551             }/* End of status == TRUE */
1552             else
1553             {
1554                if(rlcRbCb->l2MeasIpThruput.dlIpTh.isBurstAct == TRUE)
1555                {
1556                   if((rlcRbCb->l2MeasIpThruput.dlIpTh.burstEndSduId == 
1557                            outStngSduArr->sduId))
1558                   {
1559                      rlcRbCb->l2MeasIpThruput.dlIpTh.isBurstAct = FALSE;
1560                      rlcRbCb->l2MeasIpThruput.dlIpTh.burstEndSduId = 0;
1561                   }
1562                   /* Clear the outstanding sdu list */
1563                   outStngSduArr->sduId = 0;
1564                   outStngSduArr->sduLen = 0;
1565                   outStngSduArr->numTb = 0;
1566                }
1567             }
1568          }
1569       } 
1570    }
1571
1572    for(lcIdx = 0; ((lcIdx < l2MeasTb->numLchInfo) && (lcIdx < RLC_MAX_ACTV_DRB)); lcIdx++)
1573    {
1574       if((rlcRbCb = ueCb->lCh[l2MeasTb->lchInfo[lcIdx].lcId - 1].dlRbCb)
1575             == NULLP)
1576       {
1577          continue;
1578       }
1579       numSdus = l2MeasTb->lchInfo[lcIdx].numSdus;
1580       if ( numSdus == 0 )
1581       {
1582          continue;
1583       }
1584       /* ccpu00143043 */
1585       if ((numSdus > RLC_L2MEAS_MAX_OUTSTNGSDU) || (numSdus == 0))
1586       {
1587          break;
1588       }
1589       /* Update stats */
1590       if(hqStaInd->status[tbIdx] == TRUE)
1591       {
1592          for(sduIndx = 0; sduIndx < numSdus; sduIndx++)
1593          {
1594             delay = RLC_TIME_DIFF(ackTime,l2MeasTb->lchInfo[lcIdx].sduInfo[sduIndx].arvlTime);
1595             RLC_UPD_PDCP_L2_DLDELAY_STS(gCb,rlcRbCb, delay); 
1596          }
1597          /* Case of sduInfo not updated */
1598          if (totlSduCnt == 0)
1599          {
1600             totlSduCnt = numSdus;
1601          }
1602          RLC_UPD_L2_UU_LOSS_POS_PKTS(gCb,rlcRbCb, (totlSduCnt + l2MeasTb->txSegSduCnt));
1603       }
1604       else
1605       {
1606          /* Case of sduInfo not updated */
1607          if (totlSduCnt == 0)
1608          {
1609             totlSduCnt = numSdus;
1610          }
1611          RLC_UPD_L2_UU_LOSS_PKTS(gCb,rlcRbCb, (totlSduCnt + l2MeasTb->txSegSduCnt));
1612       }
1613    }
1614    /* Free this tb, deallocate the memory */
1615    RLC_FREE(gCb, l2MeasTb, sizeof(RlcL2MeasTb));
1616    ueCb->l2MeasTbCb[hqStaInd->tbId[tbIdx]] = NULLP;
1617    
1618    /*stopping Task*/
1619    SStopTask(startTime, PID_RLC_DLIP_TPT_PRCHARQIND);
1620
1621    return (ret);
1622 }/* end of  rlcUtlProcHarqInd */ 
1623
1624 /**
1625  *
1626  * @brief Handler for Sending L2 Measurement confirm.
1627  *
1628  *
1629  * @b Description
1630  *        This function sends a consolidates the mesaurements taken during
1631  *        this time and sends the confirm .
1632  *
1633  *  @param[in] measEvtCb    Measurement Event Control Block.
1634  *
1635  *
1636  *  @return  S16
1637  *      -# ROK
1638  */
1639
1640 S16 rlcUtlSndDlL2MeasCfm(RlcCb *gCb,RlcL2MeasEvtCb *measEvtCb)
1641 {
1642    uint32_t         qciIdx;
1643    RlcL2MeasCb      *measCb = NULLP;
1644    RlcL2MeasCfmEvt   measCfmEvt;
1645    uint32_t          posPkts;
1646    uint32_t          dLoss;
1647    uint64_t          dlDataVol;
1648    uint64_t          dlTime;
1649    uint16_t          cntr;
1650    /* Discard new changes starts */
1651    uint8_t           qci = 0;
1652    uint32_t          cfmIdx =0;
1653    /* Discard new changes ends */
1654
1655    /* kw006.201 ccpu00120058 emoved 64 bit compilation warning */
1656 #ifndef ALIGN_64BIT
1657    RLOG1(L_DEBUG,"rlcUtlSndL2MeasCfm(transId(%ld))", measEvtCb->transId);
1658 #else
1659    RLOG1(L_DEBUG,"rlcUtlSndL2MeasCfm(transId(%d))", measEvtCb->transId);
1660 #endif
1661
1662    /* Clean up the RB data structures */
1663    measCb = &measEvtCb->measCb;
1664    
1665    memset(&measCfmEvt, 0, sizeof(RlcL2MeasCfmEvt));
1666    measCfmEvt.transId = measEvtCb->transId;
1667
1668    measCfmEvt.measType = measCb->measType;
1669    measCfmEvt.status.status = LCM_PRIM_OK;
1670    measCfmEvt.status.reason = LCM_REASON_NOT_APPL;
1671    
1672    if(measCb->measType & LKW_L2MEAS_DL_IP)
1673    {
1674       RlcL2MeasCbUeMeasInfo *pUeInfoLstCb  = measCb->val.ipThMeas.ueInfoLst;
1675       RlcL2MeasCfmUeInfoLst *pUeInfoLstCfm = measCfmEvt.val.ipThMeas.ueInfoLst;
1676
1677       for(cntr = 0;(cntr < measCb->val.ipThMeas.numUes) && (cntr < gCb->genCfg.maxUe);cntr++)        
1678       {
1679          pUeInfoLstCfm[cfmIdx].numCfm = 0;
1680          if (pUeInfoLstCb[cntr].isValid == TRUE)
1681          {
1682             pUeInfoLstCfm[cfmIdx].ueId = pUeInfoLstCb[cntr].ueId;
1683             pUeInfoLstCfm[cfmIdx].cellId = pUeInfoLstCb[cntr].cellId;
1684             for(qciIdx = 0; qciIdx < pUeInfoLstCb[cntr].numQci; qciIdx++)           
1685             {
1686                qci = pUeInfoLstCb[cntr].qci[qciIdx];
1687                pUeInfoLstCfm[cfmIdx].measCfm[pUeInfoLstCfm[cfmIdx].numCfm].qci = qci;
1688
1689                dlDataVol = pUeInfoLstCb[cntr].measData[qci].dlIpThruput.volSummation;
1690                dlTime = pUeInfoLstCb[cntr].measData[qci].dlIpThruput.timeSummation;
1691
1692                if((0 == dlTime) || !(gCb->u.dlCb->rlcL2Cb.measOn[qci] & LKW_L2MEAS_DL_IP) )
1693                {
1694                   pUeInfoLstCfm[cfmIdx].measCfm[pUeInfoLstCfm[cfmIdx].numCfm].val.ipThrput.dlIpThPut = 0;
1695                }
1696                else
1697                {
1698                   pUeInfoLstCfm[cfmIdx].measCfm[pUeInfoLstCfm[cfmIdx].numCfm].val.ipThrput.dlIpThPut = 
1699                      (dlDataVol / dlTime);
1700                }
1701                pUeInfoLstCfm[cfmIdx].measCfm[pUeInfoLstCfm[cfmIdx].numCfm].val.ipThrput.dlIpThPut *= 8;
1702
1703                /* Reset the values after reporting to Application */
1704                pUeInfoLstCb[cntr].measData[qci].dlIpThruput.volSummation = 0;
1705                pUeInfoLstCb[cntr].measData[qci].dlIpThruput.timeSummation = 0;
1706
1707                measCfmEvt.val.ipThMeas.ueInfoLst[cfmIdx].numCfm++;
1708             }
1709             cfmIdx++;
1710          }
1711       }
1712       measCfmEvt.val.ipThMeas.numUes = cfmIdx; 
1713    }
1714    else
1715    {
1716       RlcL2Cntr *pMeasData = measCb->val.nonIpThMeas.measData;
1717       RlcL2MeasCfmNonIpThMeas *pMeasCfmNonIp = &measCfmEvt.val.nonIpThMeas;
1718
1719       pMeasCfmNonIp->numCfm = 0;
1720
1721       for(qciIdx = 0; qciIdx < LKW_MAX_QCI; qciIdx++)
1722       {
1723          qci = measCb->val.nonIpThMeas.qci[qciIdx];
1724          if (qci > 0)
1725          {
1726             pMeasCfmNonIp->measCfm[pMeasCfmNonIp->numCfm].qci = qci;
1727
1728             if(measCb->measType & LKW_L2MEAS_UU_LOSS)
1729             {
1730                dLoss = pMeasData[qci].uuLoss.dLoss;
1731                posPkts = pMeasData[qci].uuLoss.posPkts;
1732                if(((posPkts + dLoss) != 0))
1733                {
1734                   pMeasCfmNonIp->measCfm[pMeasCfmNonIp->numCfm].val.nonIpThrput.uuLoss  =
1735                      ((dLoss  * 1000000) / (posPkts + dLoss));
1736                }
1737                pMeasData[qci].uuLoss.dLoss = 0;
1738                pMeasData[qci].uuLoss.posPkts = 0;
1739             }
1740             if(measCb->measType & LKW_L2MEAS_DL_DISC)
1741             {
1742
1743                pMeasCfmNonIp->measCfm[pMeasCfmNonIp->numCfm].val.nonIpThrput.dlDiscRate = 0;
1744                if(pMeasData[qci].dlDisc.totSdus != 0)
1745                {
1746                   pMeasCfmNonIp->measCfm[pMeasCfmNonIp->numCfm].val.nonIpThrput.dlDiscRate = 
1747                      (((pMeasData[qci].dlDisc.discSdus)  * 1000000) / (pMeasData[qci].dlDisc.totSdus));
1748                }
1749
1750                pMeasData[qci].dlDisc.totSdus = 0;
1751                pMeasData[qci].dlDisc.discSdus = 0;
1752             }
1753             if(measCb->measType & LKW_L2MEAS_DL_DELAY)
1754             {
1755                if (pMeasData[qci].dlPjSduDelay.numSdus > 0)
1756                {
1757                   pMeasCfmNonIp->measCfm[pMeasCfmNonIp->numCfm].val.nonIpThrput.dlSduDelay = 
1758                     (pMeasData[qci].dlPjSduDelay.sduDelay / pMeasData[qci].dlPjSduDelay.numSdus);
1759                   pMeasData[qci].dlPjSduDelay.sduDelay = 0;
1760                   pMeasData[qci].dlPjSduDelay.numSdus = 0;
1761                }
1762             }
1763             pMeasCfmNonIp->numCfm++;
1764          }
1765       }
1766    }
1767    /* Fix Klock warning */
1768    RlcMiLkwL2MeasCfm(&gCb->genCfg.lmPst, &measCfmEvt);
1769    return ROK;
1770 } /* rlcUtlSndL2MeasCfm */
1771 /**
1772  *
1773  * @brief Handler for Sending Negative confirm .
1774  *
1775  *
1776   @b Description
1777  *        This function is called when the l2 measurement cannot be started
1778  *        This function sends  negative confirm for all the requests
1779  *
1780  *  @param[in] measReqEvt    Measurement Req Structure
1781  *
1782  *
1783  *  @return  S16
1784  *      -# ROK
1785  */
1786
1787 S16 rlcUtlSndDlL2MeasNCfm(RlcCb *gCb,RlcL2MeasReqEvt *measReqEvt,RlcL2MeasCfmEvt *measCfmEvt)
1788 {
1789
1790    RlcMiLkwL2MeasCfm(&gCb->genCfg.lmPst, measCfmEvt);
1791    return ROK;
1792 } /* kwUtlSndL2MeasNCfm */
1793 /**
1794  *
1795  * @brief Handler for resetting the RB data structures
1796  *
1797  *
1798  * @b Description
1799  *        This function resets the RB data structure after the expiry of 
1800  *        measurement timer.
1801  *
1802  *  @param[in] measCb    Measurement Control Block.
1803  *
1804  *
1805  *  @return  Void
1806  */
1807
1808 Void rlcUtlResetDlL2MeasInRlcRb(RlcCb *gCb,RlcL2MeasCb *measCb,uint8_t measType)
1809 {
1810    uint32_t           ueIdx;
1811    uint32_t           qciIdx;
1812    RlcDlUeCb      *ueCb = NULL;
1813
1814
1815
1816    if (measCb->measType & LKW_L2MEAS_DL_IP)
1817    {
1818       for(ueIdx = 0; ueIdx < measCb->val.ipThMeas.numUes; ueIdx++)
1819       {           
1820          if (measCb->val.ipThMeas.ueInfoLst[ueIdx].isValid == TRUE)
1821          {
1822             for (qciIdx =0; qciIdx < measCb->val.ipThMeas.ueInfoLst[ueIdx].numQci; qciIdx++)
1823             {
1824                if (measType & LKW_L2MEAS_DL_IP)
1825                {
1826                   measCb->val.ipThMeas.ueInfoLst[ueIdx].measData[qciIdx].dlIpThruput.volSummation = 0;
1827                   measCb->val.ipThMeas.ueInfoLst[ueIdx].measData[qciIdx].dlIpThruput.timeSummation = 0;
1828                   gCb->u.dlCb->rlcL2Cb.measOn[qciIdx] &= ~measType; 
1829                }
1830             }
1831
1832             if(ROK  != rlcDbmFetchDlUeCb(gCb,measCb->val.ipThMeas.ueInfoLst[ueIdx].ueId,
1833                      measCb->val.ipThMeas.ueInfoLst[ueIdx].cellId, &ueCb))
1834             {
1835                continue;
1836             }
1837
1838          }
1839       }
1840    }
1841    else
1842    {
1843       /* for now the only meas should be DL discard in this case */
1844       if (measCb->measType & LKW_L2MEAS_DL_DISC)
1845       {
1846          uint32_t i;
1847          for(i = 0; i < measCb->val.nonIpThMeas.numQci; i++)
1848          {
1849             uint8_t qciVal = measCb->val.nonIpThMeas.qci[i];
1850
1851             measCb->val.nonIpThMeas.measData[qciVal].dlDisc.discSdus = 0;
1852             measCb->val.nonIpThMeas.measData[qciVal].dlDisc.totSdus  = 0;
1853          }
1854          
1855       }
1856       if (measCb->measType & LKW_L2MEAS_DL_DELAY)
1857       {
1858          uint32_t i;
1859          for(i = 0; i < measCb->val.nonIpThMeas.numQci; i++)
1860          {
1861             uint8_t qciVal = measCb->val.nonIpThMeas.qci[i];
1862
1863             measCb->val.nonIpThMeas.measData[qciVal].dlPjSduDelay.sduDelay = 0;
1864          }
1865       }
1866       measCb->val.nonIpThMeas.numQci = 0;
1867    }
1868 } /* rlcUtlResetDlL2MeasInRlcRb */
1869 #endif
1870
1871 static Void dumpRLCDlRbInformation(RlcDlRbCb* dlRbCb)
1872 {
1873    if(dlRbCb->mode == CM_LTE_MODE_UM)
1874    {
1875       RLOG_ARG3(L_DEBUG,DBG_RBID,dlRbCb->rlcId.rbId,
1876                "UM Downlink UEID:%d CELLID:%d Q size = %d",
1877                        dlRbCb->rlcId.ueId,
1878                        dlRbCb->rlcId.cellId,
1879                        (int)dlRbCb->m.umDl.sduQ.count);
1880    }
1881    else if(dlRbCb->mode == CM_LTE_MODE_AM)
1882    {
1883       uint32_t j, numTxPdus=0;
1884       for(j = 0; j <= (RLC_AM_GET_WIN_SZ(dlRbCb->m.amDl.snLen)); j++)
1885       {
1886          RlcTx *txBuf = rlcUtlGetTxBuf(dlRbCb->m.amDl.txBufLst, j);
1887          if(txBuf != NULLP)
1888          {
1889             numTxPdus++;
1890          }
1891       }
1892       RLOG_ARG4(L_DEBUG,DBG_RBID,dlRbCb->rlcId.rbId,
1893                "AM Downlink UEID:%d CELLID:%d Sizes SDU Q = %d TX Q = %d ",
1894                        dlRbCb->rlcId.ueId,
1895                        dlRbCb->rlcId.cellId,
1896                        (int)dlRbCb->m.amDl.sduQ.count,
1897                        (int)numTxPdus);
1898       RLOG_ARG3(L_DEBUG,DBG_RBID,dlRbCb->rlcId.rbId,
1899                "AM Downlink UEID:%d CELLID:%d RETX Q= %d",
1900                        dlRbCb->rlcId.ueId,
1901                        dlRbCb->rlcId.cellId,
1902                        (int)dlRbCb->m.amDl.retxLst.count);
1903    }
1904 }
1905
1906 Void DumpRLCDlDebugInformation(Void)
1907 {
1908    RlcCb* dlInst = rlcCb[1]; /* TODO : Check whether DL is 0 or 1 */
1909
1910    RlcDlCb *dlCb = dlInst->u.dlCb;
1911
1912    RlcDlUeCb *ueCb = NULLP; 
1913    RTLIN_DUMP_DEBUG("RLC Information\n");
1914    RTLIN_DUMP_DEBUG("===============\n");
1915    /* Until no more ueCb is ueLstCp hash list get and delete ueCb */
1916    while (ROK == cmHashListGetNext(&dlCb->ueLstCp, 
1917                                    (PTR) ueCb, 
1918                                    (PTR *)&ueCb))
1919    {
1920       uint32_t i;
1921       for(i = 0; i< RLC_MAX_SRB_PER_UE; i++)
1922       {
1923          RlcDlRbCb* dlRbCb = ueCb->srbCb[i]; 
1924          if( dlRbCb != NULLP)
1925          {
1926             dumpRLCDlRbInformation(dlRbCb);
1927          }
1928       }
1929       for(i = 0; i< RLC_MAX_DRB_PER_UE; i++)
1930       {
1931          RlcDlRbCb* dlRbCb = ueCb->drbCb[i]; 
1932          if( dlRbCb != NULLP)
1933          {
1934             dumpRLCDlRbInformation(dlRbCb);
1935          }
1936       }
1937    }
1938
1939    RlcDlDataToBeFreed* pToBeFreed = &dlCb->toBeFreed;
1940
1941    RTLIN_DUMP_DEBUG("toBeFreed RETX list size = %d\n",(int)pToBeFreed->reTxLst.count);
1942    RTLIN_DUMP_DEBUG("toBeFreed TX list size   = %d\n",(int)pToBeFreed->txLst.count);
1943    RTLIN_DUMP_DEBUG("toBeFreed SDU list size  = %d\n",(int)pToBeFreed->sduLst.count);
1944    RTLIN_DUMP_DEBUG("toBeFreed RB list size   = %d\n",(int)pToBeFreed->rbLst.count);
1945 }
1946
1947 /**
1948  *
1949  * @b Description
1950  *        This function frees downlink memory 
1951  *
1952  *  @param[in] Void
1953  *
1954  *
1955  *  @return  Void
1956  */
1957
1958 void rlcUtlFreeDlMem( Void)
1959 {
1960   rlcUtlFreeDlMemory(RLC_GET_RLCCB(RLC_DL_INST));
1961 }
1962
1963 /**
1964  *
1965  * @b Description
1966  *        This function returns current time
1967  *
1968  *  @param[in] uint32_t 
1969  *
1970  *
1971  *  @return  Void
1972  */
1973
1974 void rlcUtlGetCurrTime(uint32_t  *currTime)
1975 {
1976    /* Need t define else part for PAL */
1977    *currTime = SGetTtiCount();
1978 }
1979
1980 #if defined(MAC_RLC_HARQ_STA_RBUF) || defined (SS_RBUF)
1981 #ifdef LTE_L2_MEAS
1982 void rlcUtlDlBatchProcHqStaInd( Void)
1983 {
1984    /* Read from Ring Buffer and process PDCP packets */
1985    //Pst pst = {0};
1986
1987    Void *elmIndx = NULLP;
1988    RguHarqStaInd *staInd = NULLP;
1989
1990 #if defined(MAC_RLC_HARQ_STA_RBUF) && defined(LTE_L2_MEAS)
1991    isDatReqProcessed = TRUE;
1992 #endif
1993    elmIndx = (Void *)SRngGetRIndx(SS_RNG_BUF_MAC_HARQ);
1994    while(NULLP != elmIndx)
1995    {
1996       staInd = (RguHarqStaInd *)elmIndx;
1997       RlcLiRguHqStaInd(&(staInd->pst), 0, &(staInd->hqStatusInd));
1998
1999       elmIndx = NULLP;
2000       staInd  = NULLP;
2001       SRngIncrRIndx(SS_RNG_BUF_MAC_HARQ);
2002
2003       if((elmIndx = (Void *)SRngGetRIndx(SS_RNG_BUF_MAC_HARQ)) == NULLP)
2004       {
2005 #if defined(MAC_RLC_HARQ_STA_RBUF) && defined(LTE_L2_MEAS)
2006          isDatReqProcessed = FALSE;
2007 #endif
2008          break;
2009       }
2010    }
2011 }
2012 #endif
2013 #endif
2014
2015 /**
2016  * @brief evaluate and trigger PDB based flow control to PDCP 
2017  *
2018  * @details 
2019  *
2020  * @param[in]   rbCb  RB control block 
2021  *
2022  *
2023  *  @return  Void
2024  *
2025  */
2026 Void rlcUtlTrigPdbFlowCntrl(RlcCb  *gCb,RlcDlRbCb *rbCb,uint32_t pktAdmitCnt)
2027 {
2028    KwuFlowCntrlIndInfo    *flowCntrlInfo;
2029    RlcKwuSapCb*            rlckwuSap;
2030
2031    rlckwuSap = gCb->u.dlCb->rlcKwuDlSap + RLC_UI_PDCP;
2032
2033    RLC_SHRABL_STATIC_BUF_ALLOC(rlckwuSap->pst.region, 
2034                               rlckwuSap->pst.pool, 
2035                               flowCntrlInfo, 
2036                               sizeof(KwuFlowCntrlIndInfo));
2037    flowCntrlInfo->rlcId       = rbCb->rlcId;
2038    flowCntrlInfo->pktAdmitCnt = pktAdmitCnt;
2039    RlcUiKwuFlowCntrlInd(&rlckwuSap->pst, rlckwuSap->suId, flowCntrlInfo);
2040 }
2041
2042 /**
2043  *
2044  * @brief Store the DL buffer in hashList  
2045  *
2046  *
2047  * @b Description
2048  *
2049  *   Use the SN % binSize as key and store the received UL buffer
2050  *  @param[in] txBufLst       List CP array
2051  *  @param[in] txBuf          transmitted buffer
2052  *  @param[in] sn              sn of the received buffer 
2053  *
2054  *
2055  *  @return  Void
2056  */
2057 void rlcUtlStoreTxBuf(CmLListCp *txBufLst, RlcTx *txBuf, RlcSn sn)
2058 {
2059    uint32_t   hashKey; 
2060
2061    hashKey = (sn % RLC_TX_BUF_BIN_SIZE );
2062    txBuf->sn = sn; 
2063    txBuf->lnk.node = (PTR)txBuf;
2064    cmLListAdd2Tail(&(txBufLst[hashKey]), &txBuf->lnk);
2065
2066    return;
2067 } /* rlcUtlStoreRecBuf */
2068
2069 /**
2070  *
2071  * @brief Retrieve the DL buffer from the list
2072  *
2073  *
2074  * @Description
2075  *
2076  *   Use the SN % binSize as key and retrieve the DL buffer
2077  *  @param[in] txBufLst       List CP array
2078  *  @param[in] sn              sn of the transmitted buffer 
2079  *
2080  *
2081  *  @return  Void
2082  */
2083 RlcTx* rlcUtlGetTxBuf(CmLListCp *txBufLst, RlcSn sn)
2084 {
2085    uint32_t            hashKey; 
2086    CmLListCp           *txBufLstCp;
2087    RlcTx               *txBuf;
2088    CmLList             *node = NULLP;
2089
2090    hashKey = (sn % RLC_TX_BUF_BIN_SIZE ); 
2091  
2092    txBufLstCp = &txBufLst[hashKey];
2093    CM_LLIST_FIRST_NODE(txBufLstCp, node);
2094    while(node)
2095    {
2096       txBuf = (RlcTx *) node->node;
2097       if(txBuf->sn == sn)
2098       {
2099          return (txBuf);
2100       }
2101       CM_LLIST_NEXT_NODE(txBufLstCp, node);
2102    }
2103    return NULLP;
2104 } /* rlcUtlStoreTxBuf */
2105 /**
2106  *
2107  * @brief Delete the DL buffer from the list
2108  *
2109  *
2110  * @Description
2111  *
2112  *   Use the SN % binSize as key and retrieve the DL buffer
2113  *  @param[in] txBufLst       List CP array
2114  *  @param[in] sn              sn of the transmitted bffer 
2115  *
2116  *
2117  *  @return  Void
2118  */
2119 Void rlcUtlDelTxBuf (CmLListCp *txBufLst,RlcTx  *txBuf,RlcCb *gCb)
2120 {
2121    uint32_t            hashKey; 
2122    CmLListCp           *txBufLstCp;
2123
2124    hashKey = (txBuf->sn % RLC_TX_BUF_BIN_SIZE ); 
2125  
2126    txBufLstCp = &txBufLst[hashKey];
2127    //printf("D-sn(%d)\n", txBuf->hdr.sn);
2128    cmLListDelFrm(txBufLstCp, &txBuf->lnk);
2129    RLC_FREE_WC(gCb, txBuf, sizeof(RlcTx));
2130    return;
2131 } /* rlcUtlDelTxBuf */
2132
2133 /**
2134  *
2135  * @brief Remove the DL buffer from the list
2136  *
2137  *
2138  * @Description
2139  *
2140  *   Use the SN % binSize as key and retrieve the DL buffer
2141  *  @param[in] txBufLst       List CP array
2142  *  @param[in] sn              sn of the transmitted bffer 
2143  *
2144  *
2145  *  @return  Void
2146  */
2147 Void rlcUtlRemovTxBuf(CmLListCp *txBufLst,RlcTx  *txBuf,RlcCb *gCb)
2148 {
2149    uint32_t            hashKey; 
2150    CmLListCp           *txBufLstCp;
2151
2152    hashKey = (txBuf->sn % RLC_TX_BUF_BIN_SIZE ); 
2153  
2154    txBufLstCp = &txBufLst[hashKey];
2155    //printf("D-sn(%d)\n", txBuf->hdr.sn);
2156    cmLListDelFrm(txBufLstCp, &txBuf->lnk);
2157    return;
2158 } /* rlcUtlRemovTxBuf */
2159
2160
2161
2162 /********************************************************************30**
2163          End of file
2164 **********************************************************************/