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