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