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