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