Added Call flow debug print for all the layer
[o-du/l2.git] / src / 5gnrmac / mac_slot_ind.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 /* header include files (.h) */
19 #include "common_def.h"
20 #include "lrg.h"
21 #include "lrg.x"
22 #include "du_app_mac_inf.h"
23 #include "mac_sch_interface.h"
24 #include "lwr_mac_upr_inf.h"
25 #include "rlc_mac_inf.h"
26 #include "mac.h"
27 #include "mac_upr_inf_api.h"
28 #include "lwr_mac.h"
29 #include "lwr_mac_fsm.h"
30 #include "mac_utils.h"
31
32 /* function pointers for packing slot ind from mac to sch */
33 MacSchSlotIndFunc macSchSlotIndOpts[] =
34 {
35    packMacSchSlotInd,
36    MacSchSlotInd,
37    packMacSchSlotInd
38 };
39
40 /**
41  * @brief process DL allocation from scheduler
42  *
43  * @details
44  *
45  *     Function : MacProcDlAlloc 
46  *      
47  *      This function copied dl sch info in the mac slot info
48  *           
49  *  @param[in]  Pst            *pst
50  *  @param[in]  DL allocation from scheduler
51  *  @return
52  *      -# ROK 
53  *      -# RFAILED 
54  **/
55 uint8_t MacProcDlAlloc(Pst *pst, DlSchedInfo *dlSchedInfo)
56 {
57    uint16_t  cellIdx;
58    MacDlSlot *currDlSlot = NULLP;
59
60 #ifdef CALL_FLOW_DEBUG_LOG
61    DU_LOG("\nCall Flow: ENTSCH -> ENTMAC : EVENT_DL_SCH_INFO\n");
62 #endif
63    if(dlSchedInfo != NULLP)
64    {
65       GET_CELL_IDX(dlSchedInfo->cellId, cellIdx);
66       if(dlSchedInfo->isBroadcastPres)
67       {
68          currDlSlot = &macCb.macCell[cellIdx]->\
69                       dlSlot[dlSchedInfo->schSlotValue.broadcastTime.slot];
70          currDlSlot->dlInfo.isBroadcastPres = true;
71          memcpy(&currDlSlot->dlInfo.brdcstAlloc, &dlSchedInfo->brdcstAlloc, sizeof(DlBrdcstAlloc));
72          currDlSlot->dlInfo.brdcstAlloc.sib1Alloc.sib1PdcchCfg.dci.pdschCfg = \
73                                             &currDlSlot->dlInfo.brdcstAlloc.sib1Alloc.sib1PdschCfg;
74       }
75
76       if(dlSchedInfo->rarAlloc != NULLP)
77       {
78          currDlSlot = &macCb.macCell[cellIdx]->\
79                       dlSlot[dlSchedInfo->schSlotValue.rarTime.slot];
80          currDlSlot->dlInfo.rarAlloc = dlSchedInfo->rarAlloc;
81
82          /* MUXing of RAR */
83          fillRarPdu(&currDlSlot->dlInfo.rarAlloc->rarInfo);
84       }
85
86       if(dlSchedInfo->dlMsgAlloc != NULLP)
87       {
88          currDlSlot = &macCb.macCell[cellIdx]->\
89                       dlSlot[dlSchedInfo->schSlotValue.dlMsgTime.slot];
90          currDlSlot->dlInfo.dlMsgAlloc = dlSchedInfo->dlMsgAlloc; /* copy msg4 alloc pointer in MAC slot info */
91          currDlSlot->dlInfo.cellId = dlSchedInfo->cellId;
92
93          /* Check if the downlink pdu is msg4 */
94          if(dlSchedInfo->dlMsgAlloc->dlMsgInfo.isMsg4Pdu)
95          {
96             macCb.macCell[cellIdx]->macRaCb[0].msg4TbSize = dlSchedInfo->dlMsgAlloc->dlMsgPdschCfg.codeword[0].tbSize;
97          }
98          else
99          {
100             memcpy(&currDlSlot->dlInfo.schSlotValue, &dlSchedInfo->schSlotValue, sizeof(SchSlotValue));
101             /* Send LC schedule result to RLC */
102             sendSchedRptToRlc(currDlSlot->dlInfo, dlSchedInfo->schSlotValue.dlMsgTime);
103          }
104       }
105
106       if(dlSchedInfo->ulGrant != NULLP)
107       {
108          currDlSlot = &macCb.macCell[cellIdx]->\
109                       dlSlot[dlSchedInfo->schSlotValue.ulDciTime.slot];
110          currDlSlot->dlInfo.ulGrant = dlSchedInfo->ulGrant;
111       }
112    }
113    return ROK;
114 }
115
116 /**
117  * @brief Forming anf filling the MUX Pdu
118  * @details
119  *
120  *     Function : fillMsg4Pdu
121  * 
122  *      Forming and filling of Msg4Pdu
123  *           
124  *  @param[in]  DlMsgAlloc  *msg4Alloc
125  *  @return  void
126  **/
127 void fillMsg4Pdu(uint16_t cellId, DlMsgAlloc *msg4Alloc)
128 {
129    uint8_t   ueIdx;
130    uint16_t  cellIdx;
131    uint16_t  msg4TxPduLen;
132    MacDlData msg4DlData;
133    MacCeInfo  macCeData;
134
135    GET_CELL_IDX(cellId, cellIdx);
136
137    memset(&msg4DlData, 0, sizeof(MacDlData));
138    memset(&macCeData, 0, sizeof(MacCeInfo));
139
140    GET_UE_IDX(msg4Alloc->dlMsgInfo.crnti, ueIdx);
141    ueIdx = ueIdx -1;
142
143    if(macCb.macCell[cellIdx] == NULLP)
144    {
145       DU_LOG("\nERROR -->  MAC: Cell Id[%d] not found", cellId);
146       return;
147    }
148
149    if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu != NULLP)
150    {
151       MAC_ALLOC(msg4DlData.pduInfo[ueIdx].dlPdu, macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
152       if(msg4DlData.pduInfo[ueIdx].dlPdu != NULLP)
153       {
154          msg4TxPduLen = macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TbSize - TX_PAYLOAD_HDR_LEN;
155
156          fillMsg4DlData(&msg4DlData, macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen, \
157             macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu);
158          fillMacCe(&macCeData, macCb.macCell[cellIdx]->macRaCb[ueIdx].msg3Pdu);
159          /* Forming Mux Pdu */
160          macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu = NULLP;
161          MAC_ALLOC(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu, msg4TxPduLen);
162          if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu != NULLP)
163          {
164             memset(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu, 0, msg4TxPduLen);
165             macMuxPdu(&msg4DlData, &macCeData, macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu, msg4TxPduLen);
166          }
167          else
168          {
169             DU_LOG("\nERROR  -->  MAC: Failed allocating memory for msg4TxPdu");
170          }
171          /* Free memory allocated */
172          MAC_FREE(msg4DlData.pduInfo[0].dlPdu, macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
173       }
174    }
175
176    /* storing msg4 Pdu in macDlSlot */
177    if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu)
178    {
179       msg4Alloc->dlMsgInfo.dlMsgPduLen = msg4TxPduLen;
180       MAC_ALLOC(msg4Alloc->dlMsgInfo.dlMsgPdu, msg4Alloc->dlMsgInfo.dlMsgPduLen);
181       if(msg4Alloc->dlMsgInfo.dlMsgPdu != NULLP)
182       {
183          memcpy(msg4Alloc->dlMsgInfo.dlMsgPdu, macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu, \
184                msg4Alloc->dlMsgInfo.dlMsgPduLen);
185       }
186    }
187    else
188    {
189       DU_LOG("\nERROR  -->  MAC: Failed at macMuxPdu()");
190    }
191 }
192
193 /**
194  * @brief Builds and Send the Muxed Pdu to Lower MAC
195  *
196  * @details
197  *
198  *     Function : buildAndSendMuxPdu
199  * 
200  *      Build and Sends the Muxed Pdu to Lower MAC.
201  *           
202  *  @param[in]  SlotIndInfo    *slotInd
203  *  @return  void
204  **/
205
206 void buildAndSendMuxPdu(SlotIndInfo currTimingInfo)
207 {
208    uint16_t  cellIdx;
209    MacDlSlot *currDlSlot = NULLP;
210    SlotIndInfo muxTimingInfo;
211    memset(&muxTimingInfo, 0, sizeof(SlotIndInfo));
212
213    GET_CELL_IDX(currTimingInfo.cellId, cellIdx);
214
215    ADD_DELTA_TO_TIME(currTimingInfo, muxTimingInfo, PHY_DELTA_DL);
216    currDlSlot = &macCb.macCell[cellIdx]->dlSlot[muxTimingInfo.slot];
217    if(currDlSlot->dlInfo.dlMsgAlloc)
218    {
219       if(currDlSlot->dlInfo.dlMsgAlloc->dlMsgInfo.isMsg4Pdu)
220       {
221          fillMsg4Pdu(currTimingInfo.cellId, currDlSlot->dlInfo.dlMsgAlloc);
222          currDlSlot = NULLP;
223       }
224    }
225 }
226
227 /**
228  * @brief Transmission time interval indication from PHY.
229  *
230  * @details
231  *
232  *     Function : sendSlotIndMacToSch
233  * 
234  *      This API is invoked by MAC to send slot ind to scheduler.
235  *           
236  *  @param[in]  SlotIndInfo    *slotInd
237  *  @return  
238  *      -# ROK 
239  *      -# RFAILED 
240  **/
241 int sendSlotIndMacToSch(SlotIndInfo *slotInd)
242 {
243    /* fill Pst structure to send to lwr_mac to MAC */
244    Pst pst;
245
246    FILL_PST_MAC_TO_SCH(pst, EVENT_SLOT_IND_TO_SCH);
247    return(*macSchSlotIndOpts[pst.selector])(&pst,slotInd);
248 }
249
250 /*******************************************************************
251  *
252  * @brief Send cell up indication to DU APP
253  *
254  * @details
255  *
256  *    Function : sendCellUpIndMacToDuApp
257  *
258  *    Functionality:
259  *       Send cell up indication to DU APP
260  *
261  * @params[in] Cell Up indication info 
262  * @return ROK     - success
263  *         RFAILED - failure
264  *
265  * ****************************************************************/
266 int sendCellUpIndMacToDuApp(uint16_t cellId)
267 {
268    Pst pst;
269    uint16_t ret;
270    OduCellId *oduCellId;
271
272    /*  Allocate sharable memory */
273    MAC_ALLOC_SHRABL_BUF(oduCellId, sizeof(OduCellId));
274    if(!oduCellId)
275    {
276       DU_LOG("\nERROR  -->  MAC : Memory allocation failed for cell up indication");
277       return RFAILED;
278    }
279    oduCellId->cellId = cellId;
280
281    /* Fill Pst */
282    FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_CELL_UP_IND);
283
284    ret = MacDuAppCellUpInd(&pst, oduCellId);
285    if(ret != ROK)
286    {
287       DU_LOG("\nERROR  -->  MAC: Failed to send cell up indication to DU APP");
288       MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, oduCellId, sizeof(OduCellId));
289    }
290
291    return ret;
292 } /* sendCellUpIndMacToDuApp */
293
294 /*******************************************************************
295  *
296  * @brief Process slot indication at MAC
297  *
298  * @details
299  *
300  *    Function : macProcSlotInd
301  *
302  *    Functionality: Process slot indication at MAC
303  *
304  * @params[in] Slot indication info
305  * @return ROK     - success
306  *         RFAILED - failure
307  *
308  * ****************************************************************/
309 uint8_t macProcSlotInd(SlotIndInfo slotInd)
310 {
311    uint16_t  cellIdx = 0;
312
313    GET_CELL_IDX(slotInd.cellId, cellIdx);
314    
315    if(macCb.macCell[cellIdx] == NULLP)
316    {
317       DU_LOG("ERROR  --> MAC : macProcSlotInd(): CellId[%d] does not exist. Error occurred at SFN [%d] Slot [%d]",\
318       slotInd.cellId, slotInd.sfn, slotInd.slot);
319       return RFAILED;
320    }
321    /* Store current time info */
322    macCb.macCell[cellIdx]->currTime.cellId = slotInd.cellId;
323    macCb.macCell[cellIdx]->currTime.slot = slotInd.slot;
324    macCb.macCell[cellIdx]->currTime.sfn = slotInd.sfn;
325
326    /* Mux Pdu for Msg4 */
327    buildAndSendMuxPdu(slotInd);
328
329    /* Trigger for DL TTI REQ */
330    fillDlTtiReq(slotInd);
331
332    return ROK;
333 }  /* macProcSlotInd */
334
335 /**
336  * @brief Transmission time interval indication from PHY.
337  *
338  * @details
339  *
340  *     Function : fapiMacSlotInd 
341  *      
342  *      This API is invoked by PHY to indicate TTI indication to MAC for a cell.
343  *           
344  *  @param[in]  Pst            *pst
345  *  @param[in]  SuId           suId 
346  *  @param[in]  SlotIndInfo    *slotInd
347  *  @return  
348  *      -# ROK 
349  *      -# RFAILED 
350  **/
351 uint8_t fapiMacSlotInd(Pst *pst, SlotIndInfo *slotInd)
352 {
353    uint8_t               ret = ROK;
354    uint16_t              cellIdx;
355    volatile uint32_t     startTime=0;
356
357 #ifdef ODU_SLOT_IND_DEBUG_LOG
358    DU_LOG("\nDEBUG  -->  MAC : Slot Indication received. [%d : %d]", slotInd->sfn, slotInd->slot);
359 #endif
360    /*starting Task*/
361    ODU_START_TASK(&startTime, PID_MAC_TTI_IND);
362    gSlotCount++;
363
364    if(gSlotCount == 1)
365    {
366            GET_CELL_IDX(slotInd->cellId, cellIdx);
367            macCb.macCell[cellIdx]->state = CELL_STATE_UP;
368    }
369
370 /* When testing L2 with Intel-L1, any changes specific to 
371  * timer mode testing must be guarded under INTEL_TIMER_MODE*/
372 #ifndef INTEL_TIMER_MODE
373    /* send slot indication to scheduler */
374    ret = sendSlotIndMacToSch(slotInd);
375    if(ret != ROK)
376    {
377       DU_LOG("\nERROR  -->  MAC : Sending of slot ind msg from MAC to SCH failed");
378       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, slotInd, sizeof(SlotIndInfo));
379       return ret;
380    }
381
382    ret = macProcSlotInd(*slotInd);
383    if(ret != ROK)
384    {
385       DU_LOG("\nERROR  -->  MAC : macProcSlotInd failed");
386       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, slotInd, sizeof(SlotIndInfo));
387       return ret;
388    }
389 #endif
390
391    /* send slot indication to du app */
392    if(gSlotCount == 1)   
393    {
394       ret = sendCellUpIndMacToDuApp(slotInd->cellId);
395       if(ret != ROK)
396       {
397          DU_LOG("\nERROR  -->  MAC :Sending of slot ind msg from MAC to DU APP failed");
398          MAC_FREE_SHRABL_BUF(pst->region, pst->pool, slotInd, sizeof(SlotIndInfo));
399          return ret;
400       }
401    }
402
403    /*stoping Task*/
404    ODU_STOP_TASK(startTime, PID_MAC_TTI_IND);
405    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, slotInd, sizeof(SlotIndInfo));
406
407 #ifdef INTEL_WLS_MEM
408    lwrMacCb.phySlotIndCntr++;
409    if(lwrMacCb.phySlotIndCntr > WLS_MEM_FREE_PRD)
410    {
411       lwrMacCb.phySlotIndCntr = 1;
412    }
413    freeWlsBlockList(lwrMacCb.phySlotIndCntr - 1);
414 #endif
415
416    return ret;
417 }  /* fapiMacSlotInd */
418
419 /**********************************************************************
420   End of file
421  **********************************************************************/
422