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