MAC Clean-up [Issue-ID: ODUHIGH-212]
[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 "mac.h"
26 #include "mac_upr_inf_api.h"
27 #include "lwr_mac_fsm.h"
28 #include "mac_utils.h"
29
30 /* Function declarations */
31 extern uint16_t fillUlTtiReq(SlotIndInfo currTimingInfo);
32 extern uint16_t fillDlTtiReq(SlotIndInfo currTimingInfo);
33
34 /* function pointers for packing slot ind from mac to sch */
35 MacSchSlotIndFunc macSchSlotIndOpts[] =
36 {
37    packMacSchSlotInd,
38    macSchSlotInd,
39    packMacSchSlotInd
40 };
41
42 /**
43  * @brief process DL allocation from scheduler
44  *
45  * @details
46  *
47  *     Function : MacProcDlAlloc 
48  *      
49  *      This function copied dl sch info in the mac slot info
50  *           
51  *  @param[in]  Pst            *pst
52  *  @param[in]  DL allocation from scheduler
53  *  @return
54  *      -# ROK 
55  *      -# RFAILED 
56  **/
57 uint8_t MacProcDlAlloc(Pst *pst, DlSchedInfo *dlSchedInfo)
58 {
59    uint16_t  cellIdx;
60    MacDlSlot *currDlSlot = NULLP;
61
62    if(dlSchedInfo != NULLP)
63    {
64       GET_CELL_IDX(dlSchedInfo->cellId, cellIdx);
65       if(dlSchedInfo->isBroadcastPres)
66       {
67          currDlSlot = &macCb.macCell[cellIdx]->\
68             dlSlot[dlSchedInfo->schSlotValue.broadcastTime.slot];
69          currDlSlot->dlInfo.isBroadcastPres = true;
70          memcpy(&currDlSlot->dlInfo.brdcstAlloc, &dlSchedInfo->brdcstAlloc, sizeof(DlBrdcstAlloc));
71       }
72
73       if(dlSchedInfo->rarAlloc != NULLP)
74       {
75          currDlSlot = &macCb.macCell[cellIdx]->\
76             dlSlot[dlSchedInfo->schSlotValue.rarTime.slot];
77          currDlSlot->dlInfo.rarAlloc = dlSchedInfo->rarAlloc;
78
79          /* MUXing of RAR */
80          fillRarPdu(&currDlSlot->dlInfo.rarAlloc->rarInfo);
81       }
82
83       if(dlSchedInfo->msg4Alloc != NULLP)
84       {
85          Msg4Alloc *msg4Alloc = NULLP;
86          currDlSlot = &macCb.macCell[cellIdx]->\
87             dlSlot[dlSchedInfo->schSlotValue.msg4Time.slot];
88          currDlSlot->dlInfo.msg4Alloc = dlSchedInfo->msg4Alloc; /* copy msg4 alloc pointer in MAC slot info */
89          msg4Alloc = dlSchedInfo->msg4Alloc;
90          macCb.macCell[cellIdx]->macRaCb[0].msg4TbSize = msg4Alloc->msg4PdschCfg.codeword[0].tbSize;
91       }
92    }
93    return ROK;
94 }
95
96 /**
97  * @brief Forming anf filling the MUX Pdu
98  * @details
99  *
100  *     Function : fillMsg4Pdu
101  * 
102  *      Forming and filling of Msg4Pdu
103  *           
104  *  @param[in]  Msg4Alloc  *msg4Alloc
105  *  @return  void
106  **/
107 void fillMsg4Pdu(uint16_t cellId, Msg4Alloc *msg4Alloc)
108 {
109    uint16_t  cellIdx;
110    MacDlData msg4DlData;
111    MacCeInfo  macCeData;
112
113    GET_CELL_IDX(cellId, cellIdx);
114
115    memset(&msg4DlData, 0, sizeof(MacDlData));
116    memset(&macCeData, 0, sizeof(MacCeInfo));
117
118    if(macCb.macCell[cellIdx]->macRaCb[0].msg4Pdu != NULLP)
119    {
120       MAC_ALLOC(msg4DlData.pduInfo[0].dlPdu, \
121             macCb.macCell[cellIdx]->macRaCb[0].msg4PduLen);
122       if(msg4DlData.pduInfo[0].dlPdu != NULLP)
123       {
124          fillMsg4DlData(cellId, &msg4DlData, macCb.macCell[cellIdx]->macRaCb[0].msg4Pdu);
125          fillMacCe(&macCeData, macCb.macCell[cellIdx]->macRaCb[0].msg3Pdu);
126          /* Forming Mux Pdu */
127          macCb.macCell[cellIdx]->macRaCb[0].msg4TxPdu = NULLP;
128          MAC_ALLOC(macCb.macCell[cellIdx]->macRaCb[0].msg4TxPdu, \
129             macCb.macCell[cellIdx]->macRaCb[0].msg4TbSize);
130          if(macCb.macCell[cellIdx]->macRaCb[0].msg4TxPdu != NULLP)
131          {
132             memset(macCb.macCell[cellIdx]->macRaCb[0].msg4TxPdu, 0, \
133                macCb.macCell[cellIdx]->macRaCb[0].msg4TbSize);
134             macMuxPdu(&msg4DlData, &macCeData, macCb.macCell[cellIdx]->macRaCb[0].msg4TxPdu,\
135                   macCb.macCell[cellIdx]->macRaCb[0].msg4TbSize);
136
137          }
138          else
139          {
140             DU_LOG("\nMAC: Failed allocating memory for msg4TxPdu");
141          }
142          /* Free memory allocated */
143          MAC_FREE(msg4DlData.pduInfo[0].dlPdu, macCb.macCell[cellIdx]->macRaCb[0].msg4PduLen);
144       }
145    }
146
147    /* storing msg4 Pdu in macDlSlot */
148    if(macCb.macCell[cellIdx]->macRaCb[0].msg4TxPdu)
149    {
150       msg4Alloc->msg4Info.msg4PduLen = macCb.macCell[cellIdx]->macRaCb[0].msg4TbSize;
151       MAC_ALLOC(msg4Alloc->msg4Info.msg4Pdu, msg4Alloc->msg4Info.msg4PduLen);
152       if(msg4Alloc->msg4Info.msg4Pdu != NULLP)
153       {
154          memcpy(msg4Alloc->msg4Info.msg4Pdu, macCb.macCell[cellIdx]->macRaCb[0].msg4TxPdu, \
155                msg4Alloc->msg4Info.msg4PduLen);
156       }
157    }
158    else
159    {
160       DU_LOG("\nMAC: Failed at macMuxPdu()");
161    }
162    /* TODO: Free all allocated memory, after the usage */
163    /* MAC_FREE(macCb.macCell->macRaCb[0].msg4TxPdu, \
164       macCb.macCell->macRaCb[0].msg4TbSize); // TODO: To be freed after re-transmission is successful.
165       MAC_FREE(macCb.macCell->macRaCb[0].msg4Pdu, macCb.macCell->macRaCb[0].msg4PduLen); */
166 }
167
168 /**
169  * @brief Builds and Send the Muxed Pdu to Lower MAC
170  *
171  * @details
172  *
173  *     Function : buildAndSendMuxPdu
174  * 
175  *      Build and Sends the Muxed Pdu to Lower MAC.
176  *           
177  *  @param[in]  SlotIndInfo    *slotInd
178  *  @return  void
179  **/
180
181 void buildAndSendMuxPdu(SlotIndInfo currTimingInfo)
182 {
183    uint16_t  cellIdx;
184    MacDlSlot *currDlSlot = NULLP;
185    SlotIndInfo muxTimingInfo;
186    memset(&muxTimingInfo, 0, sizeof(SlotIndInfo));
187
188    GET_CELL_IDX(currTimingInfo.cellId, cellIdx);
189
190    ADD_DELTA_TO_TIME(currTimingInfo, muxTimingInfo, PHY_DELTA);
191    currDlSlot = &macCb.macCell[cellIdx]->dlSlot[muxTimingInfo.slot];
192    if(currDlSlot->dlInfo.msg4Alloc)
193    {
194       fillMsg4Pdu(currTimingInfo.cellId, currDlSlot->dlInfo.msg4Alloc);
195       currDlSlot = NULLP;
196    }
197 }
198
199 /**
200  * @brief Transmission time interval indication from PHY.
201  *
202  * @details
203  *
204  *     Function : sendSlotIndMacToSch
205  * 
206  *      This API is invoked by MAC to send slot ind to scheduler.
207  *           
208  *  @param[in]  SlotIndInfo    *slotInd
209  *  @return  
210  *      -# ROK 
211  *      -# RFAILED 
212  **/
213 int sendSlotIndMacToSch(SlotIndInfo *slotInd)
214 {
215    /* fill Pst structure to send to lwr_mac to MAC */
216    Pst pst;
217
218    FILL_PST_MAC_TO_SCH(pst, EVENT_SLOT_IND_TO_SCH);
219    return(*macSchSlotIndOpts[pst.selector])(&pst,slotInd);
220 }
221
222 /*******************************************************************
223  *
224  * @brief Send slot indication to DU APP
225  *
226  * @details
227  *
228  *    Function : sendSlotIndMacToDuApp
229  *
230  *    Functionality:
231  *       Send slot indication to DU APP
232  *
233  * @params[in] Slot indication info 
234  * @return ROK     - success
235  *         RFAILED - failure
236  *
237  * ****************************************************************/
238 int sendSlotIndMacToDuApp(SlotIndInfo *slotInd)
239 {
240    Pst pst;
241    uint16_t ret;
242    SlotIndInfo  *slotInfo;
243
244    /*  Allocate sharable memory */
245    MAC_ALLOC_SHRABL_BUF(slotInfo, sizeof(SlotIndInfo));
246    if(!slotInfo)
247    {
248       DU_LOG("\nMAC : Slot Indication memory allocation failed");
249       return RFAILED;
250    }
251
252    slotInfo->cellId = slotInd->cellId;
253    slotInfo->sfn = slotInd->sfn;
254    slotInfo->slot = slotInd->slot;
255
256    /* Fill Pst */
257    FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_SLOT_IND);
258
259    ret = MacDuAppSlotInd(&pst, slotInfo);
260    if(ret != ROK)
261    {
262       DU_LOG("\nMAC: Failed to send slot indication to DU APP");
263       MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, slotInfo, sizeof(SlotIndInfo));
264    }
265
266    return ret;
267 } /* sendSlotIndMacToDuApp */
268
269 /*******************************************************************
270  *
271  * @brief Process slot indication at MAC
272  *
273  * @details
274  *
275  *    Function : macProcSlotInd
276  *
277  *    Functionality: Process slot indication at MAC
278  *
279  * @params[in] Slot indication info
280  * @return ROK     - success
281  *         RFAILED - failure
282  *
283  * ****************************************************************/
284 uint8_t macProcSlotInd(SlotIndInfo slotInd)
285 {
286    uint16_t  cellIdx;
287
288    GET_CELL_IDX(slotInd.cellId, cellIdx);
289
290    /* Store current time info */
291    macCb.macCell[cellIdx]->currTime.cellId = slotInd.cellId;
292    macCb.macCell[cellIdx]->currTime.slot = slotInd.slot;
293    macCb.macCell[cellIdx]->currTime.sfn = slotInd.sfn;
294
295    /* Mux Pdu for Msg4 */
296    buildAndSendMuxPdu(slotInd);
297
298    /* Trigger for DL TTI REQ */
299    fillDlTtiReq(slotInd);
300
301    /* Trigger for UL TTI REQ */
302    fillUlTtiReq(slotInd);
303
304    return ROK;
305 }  /* macProcSlotInd */
306
307 /**
308  * @brief Transmission time interval indication from PHY.
309  *
310  * @details
311  *
312  *     Function : fapiMacSlotInd 
313  *      
314  *      This API is invoked by PHY to indicate TTI indication to MAC for a cell.
315  *           
316  *  @param[in]  Pst            *pst
317  *  @param[in]  SuId           suId 
318  *  @param[in]  SlotIndInfo    *slotInd
319  *  @return  
320  *      -# ROK 
321  *      -# RFAILED 
322  **/
323 uint8_t fapiMacSlotInd(Pst *pst, SlotIndInfo *slotInd)
324 {
325    uint8_t               ret;
326    VOLATILE uint32_t     startTime=0;
327
328    DU_LOG("\nMAC : Slot Indication received");
329
330    /*starting Task*/
331    SStartTask(&startTime, PID_MAC_TTI_IND);
332
333    /* send slot indication to scheduler */
334    ret = sendSlotIndMacToSch(slotInd);
335    if(ret != ROK)
336    {
337       DU_LOG("\nMAC : Sending of slot ind msg from MAC to SCH failed");
338       return ret;
339    }
340
341    ret = macProcSlotInd(*slotInd);
342    if(ret != ROK)
343    {
344       DU_LOG("\nMAC : macProcSlotInd failed");
345       return ret;
346    }
347
348    /* send slot indication to du app */
349    ret = sendSlotIndMacToDuApp(slotInd);
350    if(ret != ROK)
351    {
352       DU_LOG("\nMAC :Sending of slot ind msg from MAC to DU APP failed");
353       return ret;
354    }
355
356    /*stoping Task*/
357    SStopTask(startTime, PID_MAC_TTI_IND);
358
359    return ret;
360 }  /* fapiMacSlotInd */
361
362 /**********************************************************************
363   End of file
364  **********************************************************************/
365