[Epic-ID: ODUHIGH-516][Task-ID: ODUHIGH-523] Statistics Indication between DU APP...
[o-du/l2.git] / src / 5gnrmac / mac_msg_hdl.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 /* header include files -- defines (.h)  */
20 #include "common_def.h"
21 #include "lrg.h"
22 #include "lrg.x"
23 #include "du_app_mac_inf.h"
24 #include "mac_sch_interface.h"
25 #include "rlc_mac_inf.h"
26 #include "mac_upr_inf_api.h"
27 #include "lwr_mac.h"
28 #ifdef INTEL_FAPI
29 #include "fapi_interface.h"
30 #endif
31 #include "lwr_mac_fsm.h"
32 #include "lwr_mac_upr_inf.h"
33 #include "mac.h"
34 #include "mac_utils.h"
35 #include "mac_harq_dl.h"
36
37 /* This file contains message handling functionality for MAC */
38
39 MacCb  macCb;
40
41 MacDuStatsIndFunc macDuStatsIndOpts[] =
42 {
43    packDuMacStatsInd,   /* packing for loosely coupled */
44    DuProcMacStatsInd,   /* packing for tightly coupled */
45    packDuMacStatsInd   /* packing for light weight loosly coupled */
46 };
47
48
49 /*******************************************************************
50  *
51  * @brief Sends DL BO Info to SCH
52  *
53  * @details
54  *
55  *    Function : sendDlRlcBoInfoToSch
56  *
57  *    Functionality:
58  *       Sends DL BO Info to SCH
59  *
60  * @params[in] 
61  * @return ROK     - success
62  *         RFAILED - failure
63  *
64  ****************************************************************/
65 uint8_t sendDlRlcBoInfoToSch(DlRlcBoInfo *dlBoInfo)
66 {
67    Pst pst;
68
69    FILL_PST_MAC_TO_SCH(pst, EVENT_DL_RLC_BO_INFO_TO_SCH);
70    return(SchMessageRouter(&pst, (void *)dlBoInfo));
71 }
72
73 /*******************************************************************
74  *
75  * @brief Sends CRC Indication to SCH
76  *
77  * @details
78  *
79  *    Function : sendCrcIndMacToSch 
80  *
81  *    Functionality:
82  *       Sends CRC Indication to SCH
83  *
84  * @params[in] 
85  * @return ROK     - success
86  *         RFAILED - failure
87  *
88  ****************************************************************/
89 uint8_t sendCrcIndMacToSch(CrcIndInfo *crcInd)
90 {
91    Pst pst;
92
93    FILL_PST_MAC_TO_SCH(pst, EVENT_CRC_IND_TO_SCH);
94    return(SchMessageRouter(&pst, (void *)crcInd));
95 }
96
97 /*******************************************************************
98  *
99  * @brief Sends UL CQI Indication to SCH
100  *
101  * @details
102  *
103  *    Function : sendUlCqiIndMacToSch 
104  *
105  *    Functionality:
106  *       Sends Ul Cqi Indication to SCH
107  *
108  * @params[in] 
109  * @return ROK     - success
110  *         RFAILED - failure
111  *
112  ****************************************************************/
113 uint8_t sendUlCqiIndMacToSch(SchUlCqiInd *ulCqiInd)
114 {
115    Pst pst;
116
117    FILL_PST_MAC_TO_SCH(pst, EVENT_UL_CQI_TO_SCH);
118    return(SchMessageRouter(&pst, (void *)ulCqiInd));
119 }
120
121 /*******************************************************************
122  *
123  * @brief Sends DL CQI Indication to SCH
124  *
125  * @details
126  *
127  *    Function : sendDlCqiIndMacToSch 
128  *
129  *    Functionality:
130  *       Sends Dl Cqi Indication to SCH
131  *
132  * @params[in] 
133  * @return ROK     - success
134  *         RFAILED - failure
135  *
136  ****************************************************************/
137 uint8_t sendDlCqiIndMacToSch(SchDlCqiInd *dlCqiInd)
138 {
139    Pst pst;
140
141    FILL_PST_MAC_TO_SCH(pst, EVENT_DL_CQI_TO_SCH);
142    return(SchMessageRouter(&pst, (void *)dlCqiInd));
143 }
144
145 /*******************************************************************
146  *
147  * @brief Sends Power Headroom Indication to SCH
148  *
149  * @details
150  *
151  *    Function : sendPhrIndToSch 
152  *
153  *    Functionality:
154  *       Sends Phr Indication to SCH
155  *
156  * @params[in] 
157  * @return ROK     - success
158  *         RFAILED - failure
159  *
160  ****************************************************************/
161 uint8_t sendPhrIndToSch(SchPwrHeadroomInd *macPhrInd)
162 {
163    Pst pst;
164
165    FILL_PST_MAC_TO_SCH(pst, EVENT_PHR_IND_TO_SCH);
166    return(SchMessageRouter(&pst, (void *)macPhrInd));
167 }
168
169 /*******************************************************************
170  *
171  * @brief Processes CRC Indication from PHY
172  *
173  * @details
174  *
175  *    Function : fapiMacCrcInd
176  *
177  *    Functionality:
178  *       Processes CRC Indication from PHY
179  *
180  * @params[in] Post Structure Pointer
181  *             Crc Indication Pointer
182  * @return ROK     - success
183  *         RFAILED - failure
184  *
185  * ****************************************************************/
186 uint8_t fapiMacCrcInd(Pst *pst, CrcInd *crcInd)
187 {
188    uint16_t     cellIdx;
189    CrcIndInfo   crcIndInfo;
190    DU_LOG("\nDEBUG  -->  MAC : Received CRC indication");
191    GET_CELL_IDX(crcInd->cellId, cellIdx);
192    /* Considering one pdu and one preamble */ 
193    crcIndInfo.cellId = macCb.macCell[cellIdx]->cellId;
194    crcIndInfo.crnti = crcInd->crcInfo[0].rnti;
195    crcIndInfo.timingInfo.sfn = crcInd->timingInfo.sfn;
196    crcIndInfo.timingInfo.slot = crcInd->timingInfo.slot;
197    crcIndInfo.numCrcInd = crcInd->crcInfo[0].numCb;
198    crcIndInfo.crcInd[0] = crcInd->crcInfo[0].cbCrcStatus[0];
199
200    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, crcInd, sizeof(CrcInd));
201    return(sendCrcIndMacToSch(&crcIndInfo));
202 }
203
204 /*******************************************************************
205  *
206  * @brief Process Rx Data Ind at MAC
207  *
208  * @details
209  *
210  *    Function : fapiMacRxDataInd
211  *
212  *    Functionality:
213  *       Process Rx Data Ind at MAC
214  *
215  * @params[in] Post structure
216  *             Rx Data Indication
217  * @return ROK     - success
218  *         RFAILED - failure
219  *
220  * ****************************************************************/
221 uint8_t fapiMacRxDataInd(Pst *pst, RxDataInd *rxDataInd)
222 {
223    uint8_t ueId = 0;
224    uint16_t pduIdx, cellIdx = 0;
225    DU_LOG("\nDEBUG  -->  MAC : Received Rx Data indication");
226    /* TODO : compare the handle received in RxDataInd with handle send in PUSCH
227     * PDU, which is stored in raCb */
228
229    for(pduIdx = 0; pduIdx < rxDataInd->numPdus; pduIdx++)
230    {
231
232       GET_CELL_IDX(rxDataInd->cellId, cellIdx);
233       GET_UE_ID(rxDataInd->pdus[pduIdx].rnti, ueId);
234       
235       if(macCb.macCell[cellIdx] && macCb.macCell[cellIdx]->ueCb[ueId -1].transmissionAction == STOP_TRANSMISSION)
236       {
237          DU_LOG("\nINFO   -->  MAC : UL data transmission not allowed for UE %d", macCb.macCell[cellIdx]->ueCb[ueId -1].ueId);
238       }
239       else
240       {
241          unpackRxData(rxDataInd->cellId, rxDataInd->timingInfo, &rxDataInd->pdus[pduIdx]);
242       }
243       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rxDataInd->pdus[pduIdx].pduData,\
244          rxDataInd->pdus[pduIdx].pduLength);
245    }
246    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rxDataInd, sizeof(RxDataInd));
247    return ROK;
248 }
249
250 /*******************************************************************
251  *
252  * @brief Processes DL data from RLC
253  *
254  * @details
255  *
256  *    Function : MacProcRlcDlData 
257  *
258  *    Functionality:
259  *      Processes DL data from RLC
260  *
261  * @params[in] Post structure
262  *             DL data
263  * @return ROK     - success
264  *         RFAILED - failure
265  *
266  * ****************************************************************/
267 uint8_t MacProcRlcDlData(Pst* pstInfo, RlcDlData *dlData)
268 {
269    uint8_t   pduIdx = 0;
270    uint8_t   ueId  = 0;
271    uint8_t   lcIdx = 0;
272    uint8_t   *txPdu = NULLP;
273    uint16_t  cellIdx = 0, txPduLen = 0;
274    MacDlData macDlData;
275    MacDlSlot *currDlSlot = NULLP;
276    DlRlcBoInfo dlBoInfo;
277
278    memset(&macDlData , 0, sizeof(MacDlData));
279    DU_LOG("\nDEBUG  -->  MAC: Received DL data for sfn=%d slot=%d numPdu= %d", \
280       dlData->slotInfo.sfn, dlData->slotInfo.slot, dlData->numPdu);
281
282    GET_UE_ID(dlData->rnti, ueId);   
283
284    /* Copy the pdus to be muxed into mac Dl data */
285    macDlData.ueId = ueId;
286    macDlData.numPdu = dlData->numPdu;
287    for(pduIdx = 0;  pduIdx < dlData->numPdu; pduIdx++)
288    {
289       macDlData.pduInfo[pduIdx].lcId = dlData->pduInfo[pduIdx].lcId;
290       macDlData.pduInfo[pduIdx].pduLen = dlData->pduInfo[pduIdx].pduLen;
291       macDlData.pduInfo[pduIdx].dlPdu = dlData->pduInfo[pduIdx].pduBuf;
292    }
293
294    GET_CELL_IDX(dlData->cellId, cellIdx);
295    /* Store DL data in the scheduled slot */
296    if(macCb.macCell[cellIdx] ==NULLP)
297    {
298       DU_LOG("\nERROR  -->  MAC : MacProcRlcDlData(): macCell does not exists");
299       return RFAILED;
300    }
301    currDlSlot = &macCb.macCell[cellIdx]->dlSlot[dlData->slotInfo.slot];
302    if(currDlSlot->dlInfo.dlMsgAlloc[ueId-1])
303    {
304       if(currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgPdschCfg)
305       {
306          txPduLen = currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgPdschCfg->codeword[0].tbSize\
307                     - TX_PAYLOAD_HDR_LEN;
308          MAC_ALLOC(txPdu, txPduLen);
309          if(!txPdu)
310          {
311             DU_LOG("\nERROR  -->  MAC : Memory allocation failed in MacProcRlcDlData");
312             return RFAILED;
313          }
314          macMuxPdu(&macDlData, NULLP, txPdu, txPduLen);
315
316          currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgPduLen = txPduLen;
317          currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgPdu = txPdu;
318          /* Add muxed TB to DL HARQ Proc CB. This will be used if retranmission of
319           * TB is requested in future. */
320          updateNewTbInDlHqProcCb(dlData->slotInfo, &macCb.macCell[cellIdx]->ueCb[ueId -1], \
321                currDlSlot->dlInfo.dlMsgAlloc[ueId-1]->dlMsgPdschCfg->codeword[0].tbSize, txPdu);
322       }
323    }
324
325    for(lcIdx = 0; lcIdx < dlData->numLc; lcIdx++)
326    {
327       if(dlData->boStatus[lcIdx].bo)
328       {
329          memset(&dlBoInfo, 0, sizeof(DlRlcBoInfo));
330          dlBoInfo.cellId = dlData->boStatus[lcIdx].cellId;
331          GET_CRNTI(dlBoInfo.crnti, dlData->boStatus[lcIdx].ueId);
332          dlBoInfo.lcId = dlData->boStatus[lcIdx].lcId;
333          dlBoInfo.dataVolume = dlData->boStatus[lcIdx].bo;
334          sendDlRlcBoInfoToSch(&dlBoInfo);
335       }
336    }
337
338    /* Free memory */
339    for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
340    {
341       MAC_FREE_SHRABL_BUF(pstInfo->region, pstInfo->pool, dlData->pduInfo[pduIdx].pduBuf,\
342             dlData->pduInfo[pduIdx].pduLen);
343    }
344    if(pstInfo->selector == ODU_SELECTOR_LWLC)
345    {
346       MAC_FREE_SHRABL_BUF(pstInfo->region, pstInfo->pool, dlData, sizeof(RlcDlData));
347    }
348    return ROK;
349 }
350
351 /*******************************************************************
352  *
353  * @brief Builds and Sends UL Data to RLC
354  *
355  * @details
356  *
357  *    Function : macProcUlData
358  *
359  *    Functionality: Builds and Sends UL Data to RLC
360  *
361  * @params[in] CellId
362  *             CRNTI
363  *             Slot information
364  *             LC Id on which payload was received
365  *             Pointer to the payload
366  *             Length of payload
367  * @return ROK     - success
368  *         RFAILED - failure
369  *
370  * ****************************************************************/
371 uint8_t macProcUlData(uint16_t cellId, uint16_t rnti, SlotTimingInfo slotInfo, \
372 uint8_t lcId, uint16_t pduLen, uint8_t *pdu)
373 {
374    Pst         pst;
375    RlcUlData     *ulData;
376
377    /* Filling RLC Ul Data*/
378    MAC_ALLOC_SHRABL_BUF(ulData, sizeof(RlcUlData));
379    if(!ulData)
380    {
381       DU_LOG("\nERROR  -->  MAC : Memory allocation failed while sending UL data to RLC");
382       return RFAILED;
383    }
384    memset(ulData, 0, sizeof(RlcUlData));
385    ulData->cellId = cellId; 
386    ulData->rnti = rnti;
387    memcpy(&ulData->slotInfo, &slotInfo, sizeof(SlotTimingInfo));
388    ulData->slotInfo.cellId = cellId;
389
390    /* Filling pdu info */
391    ulData->pduInfo[ulData->numPdu].lcId = lcId;
392    ulData->pduInfo[ulData->numPdu].pduBuf = pdu;
393    ulData->pduInfo[ulData->numPdu].pduLen = pduLen;
394    ulData->numPdu++;
395
396    /* Filling Post and send to RLC */
397    memset(&pst, 0, sizeof(Pst));
398    FILL_PST_MAC_TO_RLC(pst, 0, EVENT_UL_DATA_TO_RLC);
399    MacSendUlDataToRlc(&pst, ulData);
400
401    return ROK;
402 }
403
404
405 /*******************************************************************
406  *
407  * @brief Processes BO status from RLC
408  *
409  * @details
410  *
411  *    Function : MacProcRlcBOStatus
412  *
413  *    Functionality:
414  *      Processes BO status from RLC
415  *
416  * @params[in] Post structure
417  *             BO status
418  * @return ROK     - success
419  *         RFAILED - failure
420  *
421  * ****************************************************************/
422 uint8_t MacProcRlcBoStatus(Pst* pst, RlcBoStatus* boStatus)
423 {
424    DlRlcBoInfo  dlBoInfo;
425
426    dlBoInfo.cellId = boStatus->cellId;
427    GET_CRNTI(dlBoInfo.crnti, boStatus->ueId);
428    dlBoInfo.lcId = boStatus->lcId;
429    dlBoInfo.dataVolume = boStatus->bo;
430    
431    sendDlRlcBoInfoToSch(&dlBoInfo); 
432
433    if(pst->selector == ODU_SELECTOR_LWLC)
434    {
435       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, boStatus, sizeof(RlcBoStatus));
436    }
437
438    return ROK;
439 }
440
441 /*******************************************************************
442  *
443  * @brief Send LC schedule result report to RLC
444  *
445  * @details
446  *
447  *    Function : sendSchRptToRlc 
448  *
449  *    Functionality: Send LC schedule result report to RLC
450  *
451  * @params[in] 
452  * @return ROK     - success
453  *         RFAILED - failure
454  *
455  * ****************************************************************/
456 uint8_t sendSchedRptToRlc(DlSchedInfo dlInfo, SlotTimingInfo slotInfo, uint8_t ueIdx, uint8_t schInfoIdx)
457 {
458    Pst      pst;
459    uint8_t  lcIdx;
460    RlcSchedResultRpt  *schedRpt = NULLP;
461    
462    MAC_ALLOC_SHRABL_BUF(schedRpt, sizeof(RlcSchedResultRpt));
463    if(!schedRpt)
464    {
465       DU_LOG("\nERROR  -->  MAC: Memory allocation failure in sendSchResultRepToRlc");
466       return RFAILED;
467    }
468
469    DU_LOG("\nDEBUG  -->  MAC: Send scheduled result report for sfn %d slot %d", slotInfo.sfn, slotInfo.slot);
470    schedRpt->cellId = dlInfo.cellId;
471    schedRpt->slotInfo.sfn = slotInfo.sfn;
472    schedRpt->slotInfo.slot = slotInfo.slot;
473
474    if(dlInfo.dlMsgAlloc[ueIdx])
475    {
476       schedRpt->rnti = dlInfo.dlMsgAlloc[ueIdx]->crnti;
477       schedRpt->numLc = dlInfo.dlMsgAlloc[ueIdx]->transportBlock[0].numLc;
478       for(lcIdx = 0; lcIdx < schedRpt->numLc; lcIdx++)
479       {
480          schedRpt->lcSch[lcIdx].lcId = dlInfo.dlMsgAlloc[ueIdx]->transportBlock[0].lcSchInfo[lcIdx].lcId;
481          schedRpt->lcSch[lcIdx].bufSize = dlInfo.dlMsgAlloc[ueIdx]->transportBlock[0].lcSchInfo[lcIdx].schBytes;
482       }
483    }
484
485    /* Fill Pst */
486    FILL_PST_MAC_TO_RLC(pst, RLC_DL_INST, EVENT_SCHED_RESULT_TO_RLC);
487    if(MacSendSchedResultRptToRlc(&pst, schedRpt) != ROK)
488    {
489       DU_LOG("\nERROR  -->  MAC: Failed to send Schedule result report to RLC");
490       MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, schedRpt, sizeof(RlcSchedResultRpt));
491       return RFAILED;
492    }
493
494    return ROK;
495 }
496
497 /*******************************************************************
498  *
499  * @brief Handles cell start reuqest from DU APP
500  *
501  * @details
502  *
503  *    Function : MacProcCellStart
504  *
505  *    Functionality:
506  *      Handles cell start reuqest from DU APP
507  *
508  * @params[in] Post structure pointer
509  *             Cell Id 
510  * @return ROK     - success
511  *         RFAILED - failure
512  *
513  * ****************************************************************/
514 uint8_t MacProcCellStart(Pst *pst, CellStartInfo  *cellStart)
515 {
516    DU_LOG("\nINFO  -->  MAC : Handling cell start request");
517    gSlotCount = 0;
518    sendToLowerMac(START_REQUEST, 0, cellStart);
519
520    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellStart, \
521          sizeof(CellStartInfo));
522
523    return ROK;
524 }
525
526 /*******************************************************************
527  *
528  * @brief Handles cell stop from DU APP
529  *
530  * @details
531  *
532  *    Function : MacProcCellStop
533  *
534  *    Functionality:
535  *        Handles cell stop from DU APP
536  *
537  * @params[in] Post structure pointer
538  *             Cell Id
539  * @return ROK     - success
540  *         RFAILED - failure
541  *
542  * ****************************************************************/
543 uint8_t MacProcCellStop(Pst *pst, CellStopInfo  *cellStop)
544 {
545 #ifdef INTEL_FAPI
546    uint16_t      cellIdx; 
547
548    DU_LOG("\nINFO  -->  MAC : Sending cell stop request to Lower Mac");
549    GET_CELL_IDX(cellStop->cellId, cellIdx);
550    if(macCb.macCell[cellIdx])
551    {
552       macCb.macCell[cellIdx]->state = CELL_TO_BE_STOPPED;
553    }
554 #endif
555
556    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellStop, \
557          sizeof(CellStopInfo));
558
559    return ROK;
560 }
561
562 /*******************************************************************
563  *
564  * @brief Handles DL CCCH Ind from DU APP
565  *
566  * @details
567  *
568  *    Function : MacProcDlCcchInd 
569  *
570  *    Functionality:
571  *      Handles DL CCCH Ind from DU APP
572  *
573  * @params[in] Post structure pointer
574  *             DL CCCH Ind pointer 
575  * @return ROK     - success
576  *         RFAILED - failure
577  *
578  * ****************************************************************/
579 uint8_t MacProcDlCcchInd(Pst *pst, DlCcchIndInfo *dlCcchIndInfo)
580 {
581    uint8_t      ueId = 0, ueIdx = 0;
582    uint16_t     cellIdx;
583    uint16_t     idx;
584    DlRlcBoInfo  dlBoInfo;
585    memset(&dlBoInfo, 0, sizeof(DlRlcBoInfo));
586
587    DU_LOG("\nDEBUG  -->  MAC : Handling DL CCCH IND");
588
589    GET_CELL_IDX(dlCcchIndInfo->cellId, cellIdx);
590
591    dlBoInfo.cellId = dlCcchIndInfo->cellId;
592    dlBoInfo.crnti = dlCcchIndInfo->crnti;
593
594    if(dlCcchIndInfo->msgType == RRC_SETUP)
595    {
596       dlBoInfo.lcId = SRB0_LCID;    // SRB ID 0 for msg4
597       /* (MSG4 pdu + 3 bytes sub-header) + (Contention resolution id MAC CE + 1 byte sub-header) */
598       dlBoInfo.dataVolume = (dlCcchIndInfo->dlCcchMsgLen + 3) + (MAX_CRI_SIZE + 1);
599
600       /* storing Msg4 Pdu in raCb */
601       GET_UE_ID(dlBoInfo.crnti, ueId);
602       ueIdx = ueId -1;
603       if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == dlCcchIndInfo->crnti)
604       {
605          macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen = dlCcchIndInfo->dlCcchMsgLen;
606          MAC_ALLOC(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
607             macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
608          if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu)
609          {
610             for(idx = 0; idx < dlCcchIndInfo->dlCcchMsgLen; idx++)
611             {
612                macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu[idx] =\
613                   dlCcchIndInfo->dlCcchMsg[idx];
614             }
615          }
616       }
617    }
618    sendDlRlcBoInfoToSch(&dlBoInfo);
619
620    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo->dlCcchMsg, \
621          dlCcchIndInfo->dlCcchMsgLen);
622    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo, sizeof(DlCcchIndInfo));
623    return ROK;
624
625 }
626
627 /*******************************************************************
628  *
629  * @brief Sends UL CCCH Ind to DU APP
630  *
631  * @details
632  *
633  *    Function : macProcUlCcchInd
634  *
635  *    Functionality:
636  *        MAC sends UL CCCH Ind to DU APP
637  *
638  * @params[in] Post structure pointer
639  *            
640  * @return ROK     - success
641  *         RFAILED - failure
642  *
643  * ****************************************************************/
644 uint8_t macProcUlCcchInd(uint16_t cellId, uint16_t crnti, uint16_t rrcContSize, uint8_t *rrcContainer)
645 {
646    Pst pst;
647    uint8_t ret = ROK;
648    UlCcchIndInfo *ulCcchIndInfo = NULLP;
649
650    MAC_ALLOC_SHRABL_BUF(ulCcchIndInfo, sizeof(UlCcchIndInfo));
651    if(!ulCcchIndInfo)
652    {
653       DU_LOG("\nERROR  -->  MAC: Memory failed in macProcUlCcchInd");
654       return RFAILED;
655    }
656
657    ulCcchIndInfo->cellId = cellId;
658    ulCcchIndInfo->crnti  = crnti;
659    ulCcchIndInfo->ulCcchMsgLen = rrcContSize;
660    ulCcchIndInfo->ulCcchMsg = rrcContainer;
661
662    /* Fill Pst */
663    FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_UL_CCCH_IND);
664
665    if(MacDuAppUlCcchInd(&pst, ulCcchIndInfo) != ROK)
666    {
667       DU_LOG("\nERROR  -->  MAC: Failed to send UL CCCH Ind to DU APP");
668       MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
669       MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
670       ret = RFAILED;
671    }
672    return ret;
673 }
674
675 /*******************************************************************
676  *
677  * @brief Processes received short BSR
678  *
679  * @details
680  *
681  *    Function : macProcShortBsr
682  *
683  *    Functionality:
684  *        MAC sends Short BSR to SCH
685  *
686  * @params[in] cell ID
687  *             crnti
688  *             lcg ID
689  *             buffer size
690  *
691  * @return ROK     - success
692  *         RFAILED - failure
693  *
694  * ****************************************************************/
695 uint8_t macProcShortBsr(uint16_t cellId, uint16_t crnti, uint8_t lcgId, uint32_t bufferSize)
696 {
697    Pst                  pst;
698    UlBufferStatusRptInd bsrInd;
699
700    memset(&pst, 0, sizeof(Pst));
701    memset(&bsrInd, 0, sizeof(UlBufferStatusRptInd));
702
703    bsrInd.cellId                 = cellId;
704    bsrInd.crnti                  = crnti;
705    bsrInd.bsrType                = SHORT_BSR;
706    bsrInd.numLcg                 = 1; /* short bsr reports one lcg at a time */
707    bsrInd.dataVolInfo[0].lcgId   = lcgId;
708    bsrInd.dataVolInfo[0].dataVol = bufferSize;
709
710    FILL_PST_MAC_TO_SCH(pst, EVENT_SHORT_BSR);
711    return(SchMessageRouter(&pst, (void *)&bsrInd));
712 }
713
714 /*******************************************************************
715  *
716  * @brief Processes received short BSR
717  *
718  * @details
719  *
720  *    Function : macProcShortBsr
721  *
722  *    Functionality:
723  *        MAC sends Short BSR to SCH
724  *
725  * @params[in] cell ID
726  *             crnti
727  *             lcg ID
728  *             buffer size
729  *
730  * @return ROK     - success
731  *         RFAILED - failure
732  *
733  * ****************************************************************/
734 uint8_t macProcLongBsr(uint16_t cellId, uint16_t crnti,uint8_t numLcg,\
735                          DataVolInfo dataVolInfo[MAX_NUM_LOGICAL_CHANNEL_GROUPS])
736 {
737    Pst                  pst;
738    UlBufferStatusRptInd bsrInd;
739    uint8_t lcgIdx = 0;
740
741    memset(&pst, 0, sizeof(Pst));
742    memset(&bsrInd, 0, sizeof(UlBufferStatusRptInd));
743
744    bsrInd.cellId                 = cellId;
745    bsrInd.crnti                  = crnti;
746    bsrInd.bsrType                = LONG_BSR;
747    bsrInd.numLcg                 = numLcg; 
748
749    for(lcgIdx = 0; lcgIdx < numLcg; lcgIdx++)
750       memcpy(&(bsrInd.dataVolInfo[lcgIdx]), &(dataVolInfo[lcgIdx]), sizeof(DataVolInfo));
751
752    FILL_PST_MAC_TO_SCH(pst, EVENT_LONG_BSR);
753    return(SchMessageRouter(&pst, (void *)&bsrInd));
754 }
755
756 /*******************************************************************
757  *
758  * @brief Builds and send DL HARQ Indication to SCH
759  *
760  * @details
761  *
762  *    Function : buildAndSendHarqInd
763  *
764  *    Functionality:
765  *       Builds and send HARQ UCI Indication to SCH
766  *
767  * @params[in] harqInfo Pointer
768  *             crnti value
769  *             cell Index value
770  *             slot Ind Pointer
771  * @return ROK     - success
772  *         RFAILED - failure
773  *
774  * ****************************************************************/
775 uint8_t buildAndSendHarqInd(HarqInfoF0F1 *harqInfo, uint16_t crnti, uint16_t cellIdx, SlotTimingInfo *slotInd)
776 {
777    uint16_t harqCounter=0;
778    Pst pst;
779    DlHarqInd   dlHarqInd;
780    memset(&pst, 0, sizeof(Pst));
781    memset(&dlHarqInd, 0, sizeof(DlHarqInd));
782
783    dlHarqInd.cellId       = macCb.macCell[cellIdx]->cellId;
784    dlHarqInd.crnti        = crnti;
785    dlHarqInd.slotInd.sfn  = slotInd->sfn;
786    dlHarqInd.slotInd.slot = slotInd->slot;
787    dlHarqInd.numHarq      = harqInfo->numHarq;
788    memset(dlHarqInd.harqPayload, 0, MAX_SR_BITS_IN_BYTES);
789    for(harqCounter = 0; harqCounter < harqInfo->numHarq; harqCounter++)
790    {
791       dlHarqInd.harqPayload[harqCounter] = harqInfo->harqValue[harqCounter];
792    }
793    
794    /* Fill Pst */
795    FILL_PST_MAC_TO_SCH(pst, EVENT_DL_HARQ_IND_TO_SCH);
796
797    return SchMessageRouter(&pst, (void *)&dlHarqInd);
798 }
799
800
801 /*******************************************************************
802  *
803  * @brief Builds and send SR UCI Indication to SCH
804  *
805  * @details
806  *
807  *    Function : buildAndSendSrInd
808  *
809  *    Functionality:
810  *       Builds and send SR UCI Indication to SCH
811  *
812  * @params[in] SrUciIndInfo Pointer
813  *             crnti value
814  * @return ROK     - success
815  *         RFAILED - failure
816  *
817  * ****************************************************************/
818 uint8_t buildAndSendSrInd(UciInd *macUciInd, uint16_t crnti)
819 {
820    uint16_t cellIdx;
821    Pst pst;
822    SrUciIndInfo   srUciInd;
823    memset(&pst, 0, sizeof(Pst));
824    memset(&srUciInd, 0, sizeof(SrUciIndInfo));
825
826    GET_CELL_IDX(macUciInd->cellId, cellIdx);
827    srUciInd.cellId       = macCb.macCell[cellIdx]->cellId;
828    srUciInd.crnti        = crnti;
829    srUciInd.slotInd.sfn  = macUciInd->slotInd.sfn;
830    srUciInd.slotInd.slot = macUciInd->slotInd.slot;
831    srUciInd.numSrBits++;
832    memset(srUciInd.srPayload, 0, MAX_SR_BITS_IN_BYTES);
833
834    /* Fill Pst */
835    FILL_PST_MAC_TO_SCH(pst, EVENT_UCI_IND_TO_SCH);
836
837    return(SchMessageRouter(&pst, (void *)&srUciInd));
838 }
839
840 /*******************************************************************
841  *
842  * @brief Processes UCI Indication from PHY
843  *
844  * @details
845  *
846  *    Function : fapiMacUciInd
847  *
848  *    Functionality:
849  *       Processes UCI Indication from PHY
850  *
851  * @params[in] Post Structure Pointer
852  *             UCI Indication Pointer
853  * @return ROK     - success
854  *         RFAILED - failure
855  *
856  * ****************************************************************/
857 uint8_t FapiMacUciInd(Pst *pst, UciInd *macUciInd)
858 {
859    uint8_t     pduIdx = 0, ret = ROK;
860    uint16_t    nPdus = 0, crnti = 0, cellIdx = 0;
861
862    if(macUciInd)
863    {
864       nPdus = macUciInd->numUcis;
865       while(nPdus)
866       {
867          switch(macUciInd->pdus[pduIdx].pduType)
868          {
869             case UCI_IND_PUSCH:
870                break;
871             case UCI_IND_PUCCH_F0F1:
872             {
873                {
874                   DU_LOG("\nDEBUG  -->  MAC : Received HARQ UCI Indication\n");
875                   GET_CELL_IDX(macUciInd->cellId, cellIdx);
876                   buildAndSendHarqInd(&macUciInd->pdus[pduIdx].uci.uciPucchF0F1.harqInfo, macUciInd->pdus[pduIdx].uci.uciPucchF0F1.crnti, cellIdx, &macUciInd->slotInd);
877                }
878                if(macUciInd->pdus[pduIdx].uci.uciPucchF0F1.srInfo.srIndPres)
879                {
880                   DU_LOG("\nDEBUG  -->  MAC : Received SR UCI indication");
881                   crnti = macUciInd->pdus[pduIdx].uci.uciPucchF0F1.crnti; 
882                   ret = buildAndSendSrInd(macUciInd, crnti);
883                }
884             }
885                break;
886             case UCI_IND_PUCCH_F2F3F4:
887                break;
888             default:
889                DU_LOG("\nERROR  -->  MAC: Invalid Pdu Type %d at FapiMacUciInd", macUciInd->pdus[pduIdx].pduType);
890                ret = RFAILED;
891                break;
892          }
893          pduIdx++;
894          nPdus--;
895       }
896    }
897    else
898    {
899       DU_LOG("\nERROR  -->  MAC: Received Uci Ind is NULL at FapiMacUciInd()");
900       ret = RFAILED;
901    }
902    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macUciInd, sizeof(UciInd));
903    return ret;
904 }
905
906 /*******************************************************************
907  *
908  * @brief fill Slice Cfg Request info in shared structre
909  * 
910  * @details
911  *
912  *    Function : fillSliceCfgInfo 
913  *
914  *    Functionality:
915  *       fill Slice Cfg Request info in shared structre
916  *
917  * @params[in] SchSliceCfgReq *schSliceCfgReq 
918  *             MacSliceCfgReq *macSliceCfgReq;
919  * @return ROK     - success
920  *         RFAILED - failure
921  *
922  **********************************************************************/
923 uint8_t fillSliceCfgInfo(SchSliceCfgReq *schSliceCfgReq, MacSliceCfgReq *macSliceCfgReq)
924 {
925    uint8_t rrmPolicyIdx= 0,cfgIdx = 0, memberListIdx = 0, totalSliceCfgRecvd = 0;
926  
927    if(macSliceCfgReq->listOfRrmPolicy)
928    {
929       for(cfgIdx = 0; cfgIdx<macSliceCfgReq->numOfRrmPolicy; cfgIdx++)
930       {
931           totalSliceCfgRecvd += macSliceCfgReq->listOfRrmPolicy[cfgIdx]->numOfRrmPolicyMem;  
932       }
933
934       schSliceCfgReq->numOfConfiguredSlice =  totalSliceCfgRecvd;
935       MAC_ALLOC(schSliceCfgReq->listOfSlices, schSliceCfgReq->numOfConfiguredSlice *sizeof(SchRrmPolicyOfSlice*));
936       if(schSliceCfgReq->listOfSlices == NULLP)
937       {
938          DU_LOG("\nERROR  -->  MAC : Memory allocation failed in fillSliceCfgInfo");
939          return RFAILED;
940       }
941       cfgIdx = 0; 
942
943       for(rrmPolicyIdx = 0; rrmPolicyIdx<macSliceCfgReq->numOfRrmPolicy; rrmPolicyIdx++)
944       {
945          for(memberListIdx = 0; memberListIdx<macSliceCfgReq->listOfRrmPolicy[rrmPolicyIdx]->numOfRrmPolicyMem; memberListIdx++)
946          {
947             if(macSliceCfgReq->listOfRrmPolicy[rrmPolicyIdx]->rRMPolicyMemberList[memberListIdx])
948             {
949
950                MAC_ALLOC(schSliceCfgReq->listOfSlices[cfgIdx], sizeof(SchRrmPolicyOfSlice));
951                if(schSliceCfgReq->listOfSlices[cfgIdx] == NULLP)
952                {
953                   DU_LOG("\nERROR  -->  MAC : Memory allocation failed in fillSliceCfgInfo");
954                   return RFAILED;
955                }
956
957                memcpy(&schSliceCfgReq->listOfSlices[cfgIdx]->snssai, &macSliceCfgReq->listOfRrmPolicy[rrmPolicyIdx]->rRMPolicyMemberList[memberListIdx]->snssai, sizeof(Snssai));
958
959                schSliceCfgReq->listOfSlices[cfgIdx]->rrmPolicyRatioInfo.maxRatio = macSliceCfgReq->listOfRrmPolicy[rrmPolicyIdx]->policyRatio.maxRatio;
960                schSliceCfgReq->listOfSlices[cfgIdx]->rrmPolicyRatioInfo.minRatio = macSliceCfgReq->listOfRrmPolicy[rrmPolicyIdx]->policyRatio.minRatio;
961                schSliceCfgReq->listOfSlices[cfgIdx]->rrmPolicyRatioInfo.dedicatedRatio = macSliceCfgReq->listOfRrmPolicy[rrmPolicyIdx]->policyRatio.dedicatedRatio;
962                cfgIdx++;
963             }
964          }
965       }
966    }
967    return ROK;
968 }
969
970 /*******************************************************************
971  *
972  * @brief Processes Slice Cfg Request recived from DU
973  *
974  * @details
975  *
976  *    Function : MacProcSliceCfgReq 
977  *
978  *    Functionality:
979  *       Processes Processes Slice Cfg Request recived from DU
980  *
981  * @params[in] Post Structure Pointer
982  *             MacSliceCfgReq *macSliceCfgReq;
983  * @return ROK     - success
984  *         RFAILED - failure
985  *
986  **********************************************************************/
987 uint8_t MacProcSliceCfgReq(Pst *pst, MacSliceCfgReq *macSliceCfgReq)
988 {
989    uint8_t ret = ROK;
990    Pst schPst;
991    SchSliceCfgReq *schSliceCfgReq;
992
993    DU_LOG("\nINFO  -->  MAC : Received Slice Cfg request from DU APP");
994    if(macSliceCfgReq)
995    {
996       MAC_ALLOC(schSliceCfgReq, sizeof(SchSliceCfgReq));
997       if(schSliceCfgReq == NULLP)
998       {
999          DU_LOG("\nERROR -->  MAC : Memory allocation failed in MacProcSliceCfgReq");
1000          ret = RFAILED;
1001       }
1002       else
1003       {
1004          if(fillSliceCfgInfo(schSliceCfgReq, macSliceCfgReq) == ROK)
1005          {
1006             FILL_PST_MAC_TO_SCH(schPst, EVENT_SLICE_CFG_REQ_TO_SCH);
1007             ret = SchMessageRouter(&schPst, (void *)schSliceCfgReq);
1008          }
1009       }
1010       freeMacSliceCfgReq(macSliceCfgReq, pst); 
1011    }
1012    else
1013    {
1014       DU_LOG("\nINFO  -->  MAC : Received MacSliceCfgReq is NULL");
1015    }
1016    return ret;
1017 }
1018
1019 /*******************************************************************
1020  *
1021  * @brief Processes Slice ReCfg Request recived from DU
1022  *
1023  * @details
1024  *
1025  *    Function : MacProcSliceRecfgReq 
1026  *
1027  *    Functionality:
1028  *       Processes Processes Slice ReCfg Request recived from DU
1029  *
1030  * @params[in] Post Structure Pointer
1031  *             MacSliceCfgReq *macSliceRecfgReq;
1032  * @return ROK     - success
1033  *         RFAILED - failure
1034  *
1035  **********************************************************************/
1036 uint8_t MacProcSliceRecfgReq(Pst *pst, MacSliceRecfgReq *macSliceRecfgReq)
1037 {
1038    uint8_t ret = ROK;
1039    Pst schPst;
1040    SchSliceRecfgReq *schSliceRecfgReq;
1041
1042    DU_LOG("\nINFO  -->  MAC : Received Slice ReCfg request from DU APP");
1043    if(macSliceRecfgReq)
1044    {
1045       MAC_ALLOC(schSliceRecfgReq, sizeof(SchSliceRecfgReq));
1046       if(schSliceRecfgReq == NULLP)
1047       {
1048          DU_LOG("\nERROR -->  MAC : Memory allocation failed in MacProcSliceRecfgReq");
1049          ret = RFAILED;
1050       }
1051       else
1052       {
1053          if(fillSliceCfgInfo(schSliceRecfgReq, macSliceRecfgReq) == ROK)
1054          {
1055             FILL_PST_MAC_TO_SCH(schPst, EVENT_SLICE_RECFG_REQ_TO_SCH);
1056             ret = SchMessageRouter(&schPst, (void *)schSliceRecfgReq);
1057          }
1058       }
1059       freeMacSliceCfgReq(macSliceRecfgReq, pst);
1060    }
1061    else
1062    {
1063       DU_LOG("\nINFO  -->  MAC : Received MacSliceRecfgReq is NULL");
1064    }
1065    return ret;
1066 }
1067
1068 /**
1069  * @brief Mac process the statistics indication received from sch.
1070  *
1071  * @details
1072  *
1073  *     Function : MacProcSchStatsInd
1074  *
1075  *     This function process the statistics indication received from sch
1076  *
1077  *  @param[in]  Pst           *pst
1078  *  @param[in]  SchStatsInd   *schStatsInd
1079  *  @return  int
1080  *      -# ROK
1081  **/
1082 uint8_t MacProcSchStatsInd(Pst *pst, SchStatsInd *schStatsInd)
1083 {
1084    Pst indPst;
1085    MacStatsInd *macStatsInd;
1086
1087 #ifdef DEBUG_PRINT
1088    DU_LOG("\nDEBUG  -->  MAC : MacProcSchStatsInd: Received Statistics Indication from SCH");
1089 #endif   
1090
1091    if(schStatsInd == NULLP)
1092    {
1093       DU_LOG("\nERROR  -->  MAC : MacProcSchStatsInd: NULL pointer :schStatsInd");
1094       return RFAILED;
1095    }
1096
1097    MAC_ALLOC_SHRABL_BUF(macStatsInd, sizeof(MacStatsInd));
1098    if(macStatsInd == NULLP)
1099    {
1100       DU_LOG("\nERROR  -->  MAC : Failed to allocate memory in MacProcSchStatsInd");
1101       return RFAILED;
1102    }
1103
1104    switch(schStatsInd->type)
1105    {
1106       case SCH_DL_TOTAL_PRB_USAGE:
1107          {
1108             macStatsInd->type = MAC_DL_TOTAL_PRB_USAGE;
1109             break;
1110          }
1111       case SCH_UL_TOTAL_PRB_USAGE:
1112          {
1113             macStatsInd->type = MAC_UL_TOTAL_PRB_USAGE;
1114             break;
1115          }
1116       default:
1117          {
1118             DU_LOG("\nERROR  -->  MAC : MacProcSchStatsInd: Invalid measurement type [%d]", schStatsInd->type);
1119             MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, macStatsInd, sizeof(MacStatsInd));
1120             return RFAILED;
1121          }
1122    }
1123    macStatsInd->value = schStatsInd->value;
1124
1125    memset(&indPst, 0, sizeof(Pst));
1126    FILL_PST_MAC_TO_DUAPP(indPst, EVENT_MAC_STATISTICS_IND);
1127    return (*macDuStatsIndOpts[indPst.selector])(&indPst, macStatsInd);
1128 }
1129
1130 /**********************************************************************
1131   End of file
1132  **********************************************************************/
1133