Mux Code Changes
[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 void BuildAndSendMsg4MuxPdu(Msg4Alloc *msg4Alloc)
98
99    MacDlData msg4DlData;
100    MacCeInfo  macCeData;
101
102    memset(&msg4DlData, 0, sizeof(MacDlData));
103    memset(&macCeData, 0, sizeof(MacCeInfo));
104  
105    if(macCb.macCell->macRaCb[0].msg4Pdu != NULLP)
106    {
107       MAC_ALLOC(msg4DlData.pduInfo[0].dlPdu, \
108         macCb.macCell->macRaCb[0].msg4PduLen);
109       if(msg4DlData.pduInfo[0].dlPdu != NULLP)
110       {
111          fillMsg4DlData(&msg4DlData, macCb.macCell->macRaCb[0].msg4Pdu);
112          fillMacCe(&macCeData, &macCb.macCell->macRaCb[0].msg3Pdu);
113          /* Forming Mux Pdu */
114                         macCb.macCell->macRaCb[0].msg4TxPdu = NULLP;
115                         MAC_ALLOC(macCb.macCell->macRaCb[0].msg4TxPdu, macCb.macCell->macRaCb[0].msg4TbSize);
116          if(macCb.macCell->macRaCb[0].msg4TxPdu != NULLP)
117          {
118                            memset(macCb.macCell->macRaCb[0].msg4TxPdu, 0, macCb.macCell->macRaCb[0].msg4TbSize);
119                            macMuxPdu(&msg4DlData, &macCeData, macCb.macCell->macRaCb[0].msg4TxPdu,\
120                                   macCb.macCell->macRaCb[0].msg4TbSize);
121             
122                         }
123                         else
124                         {
125             DU_LOG("\nMAC: Failed allocating memory for msg4TxPdu");
126                         }
127                         /* Free memory allocated */
128          MAC_FREE(msg4DlData.pduInfo[0].dlPdu, macCb.macCell->macRaCb[0].msg4PduLen);
129       }
130    }
131       
132    /* storing msg4 Pdu in macDlSlot */
133    if(macCb.macCell->macRaCb[0].msg4TxPdu)
134    {
135       msg4Alloc->msg4Info.msg4PduLen = macCb.macCell->macRaCb[0].msg4TbSize;
136       MAC_ALLOC(msg4Alloc->msg4Info.msg4Pdu, msg4Alloc->msg4Info.msg4PduLen);
137      if(msg4Alloc->msg4Info.msg4Pdu != NULLP)
138      {
139              memcpy(msg4Alloc->msg4Info.msg4Pdu, macCb.macCell->macRaCb[0].msg4TxPdu, \
140                   msg4Alloc->msg4Info.msg4PduLen);
141      }
142         }
143         else
144         {
145       DU_LOG("\nMAC: Failed at macMuxPdu()");
146         }
147    /* TODO: Free all allocated memory, after the usage */
148    /* MAC_FREE(macCb.macCell->macRaCb[0].msg4TxPdu, \
149      macCb.macCell->macRaCb[0].msg4TbSize); // TODO: To be freed after re-transmission is successful.
150       MAC_FREE(macCb.macCell->macRaCb[0].msg4Pdu, macCb.macCell->macRaCb[0].msg4PduLen); */
151 }
152
153 /**
154  * @brief Transmission time interval indication from PHY.
155  *
156  * @details
157  *
158  *     Function : sendSlotIndMacToSch
159  * 
160  *      This API is invoked by MAC to send slot ind to scheduler.
161  *           
162  *  @param[in]  SlotIndInfo    *slotInd
163  *  @return  S16
164  *      -# ROK 
165  *      -# RFAILED 
166  **/
167 int sendSlotIndMacToSch(SlotIndInfo *slotInd)
168 {
169    /* fill Pst structure to send to lwr_mac to MAC */
170    Pst pst;
171
172    fillMacToSchPst(&pst);
173    pst.event = EVENT_SLOT_IND_TO_SCH;
174
175    return(*macSchSlotIndOpts[pst.selector])(&pst,slotInd);
176 }
177
178 /*******************************************************************
179  *
180  * @brief Send slot indication to DU APP
181  *
182  * @details
183  *
184  *    Function : sendSlotIndMacToDuApp
185  *
186  *    Functionality:
187  *       Send slot indication to DU APP
188  *
189  * @params[in] Slot indication info 
190  * @return ROK     - success
191  *         RFAILED - failure
192  *
193  * ****************************************************************/
194 int sendSlotIndMacToDuApp(SlotIndInfo *slotInd)
195 {
196    Pst pst;
197    uint16_t ret;
198    SlotInfo  *slotInfo;
199  
200    /*  Allocate sharable memory */
201    MAC_ALLOC_SHRABL_BUF(slotInfo, sizeof(SlotInfo));
202    if(!slotInfo)
203    {
204       DU_LOG("\nMAC : Slot Indication memory allocation failed");
205       return RFAILED;
206   }
207  
208   slotInfo->cellId = macCb.macCell->cellId;
209   slotInfo->sfn = slotInd->sfn;
210   slotInfo->slot = slotInd->slot;
211  
212   /* Fill Pst */
213   pst.selector  = ODU_SELECTOR_LWLC;
214   pst.srcEnt    = ENTRG;
215   pst.dstEnt    = ENTDUAPP;
216   pst.dstInst   = 0;
217   pst.srcInst   = macCb.macInst;
218   pst.dstProcId = rgCb[pst.srcInst].rgInit.procId;
219   pst.srcProcId = rgCb[pst.srcInst].rgInit.procId;
220   pst.region = MAC_MEM_REGION;
221   pst.pool = MAC_POOL;
222   pst.event = EVENT_MAC_SLOT_IND;
223   pst.route = 0;
224   pst.prior = 0;
225   pst.intfVer = 0;
226  
227   ret = MacDuAppSlotInd(&pst, slotInfo);
228   if(ret != ROK)
229   {
230      DU_LOG("\nMAC: Failed to send slot indication to DU APP");
231      MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, slotInfo, sizeof(SlotInfo));
232   }
233  
234   return ret;
235 } /* sendSlotIndMacToDuApp */
236
237
238 /**
239  * @brief Transmission time interval indication from PHY.
240  *
241  * @details
242  *
243  *     Function : fapiMacSlotInd 
244  *      
245  *      This API is invoked by PHY to indicate TTI indication to MAC for a cell.
246  *           
247  *  @param[in]  Pst            *pst
248  *  @param[in]  SuId           suId 
249  *  @param[in]  SlotIndInfo    *slotInd
250  *  @return  S16
251  *      -# ROK 
252  *      -# RFAILED 
253  **/
254 PUBLIC S16 fapiMacSlotInd 
255 (
256 Pst                 *pst, 
257 SlotIndInfo         *slotInd
258 )
259 {
260    S16              ret;
261    VOLATILE U32     startTime=0;
262    Inst             inst;
263
264    DU_LOG("\nMAC : Slot Indication received");
265    
266    inst = pst->dstInst;
267    /*starting Task*/
268    SStartTask(&startTime, PID_MAC_TTI_IND);
269
270    /* send slot indication to scheduler */
271    ret = sendSlotIndMacToSch(slotInd);
272    if(ret != ROK)
273    {
274       DU_LOG("\nMAC : Sending of slot ind msg from MAC to SCH failed");
275       RETVALUE(ret);
276    }
277
278    ret = macProcessSlotInd(inst,*slotInd);
279    if(ret != ROK)
280    {
281       DU_LOG("\nMAC : macProcessSlotInd failed");
282       RETVALUE(ret);
283    }
284
285    /* send slot indication to du app */
286    ret = sendSlotIndMacToDuApp(slotInd);
287    if(ret != ROK)
288    {
289       DU_LOG("\nMAC :Sending of slot ind msg from MAC to DU APP failed");
290       RETVALUE(ret);
291    }
292
293    /*stoping Task*/
294    SStopTask(startTime, PID_MAC_TTI_IND);
295
296    RETVALUE(ret);
297 }  /* fapiMacSlotInd */
298
299 /**********************************************************************
300   End of file
301  **********************************************************************/
302