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