Moving all common header file into common_def.h file
[o-du/l2.git] / src / 5gnrsch / sch_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
19 /************************************************************************
20
21 Name:     5G NR SCH layer
22
23 Type:     C source file
24
25 Desc:     C source code for Entry point fucntions for slot indications
26
27 File:     sch_slot_ind.c
28
29  **********************************************************************/
30
31 /** @file sch_slot_ind.c
32   @brief This module processes slot indications
33  */
34 #include "common_def.h"
35 #include "tfu.h"
36 #include "lrg.h"
37 #include "tfu.x"
38 #include "lrg.x"
39 #include "du_log.h"
40 #include "du_app_mac_inf.h"
41 #include "mac_sch_interface.h"
42 #include "sch.h"
43 #include "sch_utils.h"
44
45 SchMacDlAllocFunc schMacDlAllocOpts[] =
46 {
47         packSchMacDlAlloc,
48         MacProcDlAlloc,
49         packSchMacDlAlloc
50 };
51
52 extern SchCb schCb[SCH_MAX_INST];
53 extern int schFillRar(RarAlloc *rarAlloc, uint16_t raRnti, uint16_t pci, uint8_t
54 offsetPointA);
55
56 /*******************************************************************
57  *
58  * @brief Handles sending DL broadcast alloc to MAC 
59  *
60  * @details
61  *
62  *    Function : sendDlAllocToMac
63  *
64  *    Functionality:
65  *     Sends DL Broadcast Resource Allocation to MAC from SCH
66  *
67  * @params[in] 
68  * @return ROK     - success
69  *         RFAILED - failure
70  *
71  * ****************************************************************/
72 int sendDlAllocToMac(DlSchedInfo *dlSchedInfo, Inst inst)
73 {
74         Pst pst;
75
76    memset(&pst, 0, sizeof(Pst));
77    SCH_FILL_RSP_PST(pst, inst);
78         pst.event = EVENT_DL_ALLOC;
79
80         return(*schMacDlAllocOpts[pst.selector])(&pst, dlSchedInfo);
81
82 }
83
84
85 /*******************************************************************
86  *
87  * @brief Handles slot indication at SCH 
88  *
89  * @details
90  *
91  *    Function : schCalcSlotValues
92  *
93  *    Functionality:
94  *     Handles TTI indication received from PHY
95  *
96  * @params[in] 
97  * @return ROK     - success
98  *         RFAILED - failure
99  *
100  * ****************************************************************/
101 void schCalcSlotValues(SlotIndInfo slotInd, SchSlotValue *schSlotValue)
102 {
103    /****************************************************************
104     * PHY_DELTA - the physical layer delta                         * 
105     * SCHED_DELTA - scheduler schedules one slot ahead             *
106     * BO_DELTA - this delay is considered for BO response and      *
107     *            RLC buffer packet to received at MAC              *
108     * lower-mac (FAPI filling) will be working on PHY_DELTA        *
109     * brdcast scheduler will working on PHY_DELTA + SCHED_DELTA    *
110     * RAR scheduler will working on PHY_DELTA + SCHED_DELTA        *
111     * msg4 scheduler will working on PHY_DELTA + SCHED_DELTA       *
112     * dedicated DL msg scheduler will working                      *
113     *        on PHY_DELTA + SCHED_DELTA + BO_DELTA                 *
114     ****************************************************************/
115
116    ADD_DELTA_TO_TIME(slotInd,schSlotValue->currentTime,PHY_DELTA);
117    ADD_DELTA_TO_TIME(slotInd,schSlotValue->broadcastTime,PHY_DELTA+SCHED_DELTA);
118    ADD_DELTA_TO_TIME(slotInd,schSlotValue->rarTime,PHY_DELTA+SCHED_DELTA);
119    ADD_DELTA_TO_TIME(slotInd,schSlotValue->msg4Time,PHY_DELTA+SCHED_DELTA);
120    ADD_DELTA_TO_TIME(slotInd,schSlotValue->dlMsgTime,PHY_DELTA+SCHED_DELTA+BO_DELTA);
121 }
122
123 /*******************************************************************
124  *
125  * @brief Handles slot indication at SCH 
126  *
127  * @details
128  *
129  *    Function : schProcessSlotInd
130  *
131  *    Functionality:
132  *     Handles TTI indication received from PHY
133  *
134  * @params[in] 
135  * @return ROK     - success
136  *         RFAILED - failure
137  *
138  * ****************************************************************/
139 uint8_t schProcessSlotInd(SlotIndInfo *slotInd, Inst schInst)
140 {
141    int ret = ROK;
142         uint8_t ssb_rep;
143         uint16_t slot, sfnSlot = 0;
144         DlSchedInfo dlSchedInfo;
145         memset(&dlSchedInfo,0,sizeof(DlSchedInfo));
146    DlBrdcstAlloc *dlBrdcstAlloc = &dlSchedInfo.brdcstAlloc;
147         RarAlloc  *rarAlloc;
148    Msg4Alloc *msg4Alloc;
149         dlBrdcstAlloc->ssbTrans = NO_SSB;
150    dlBrdcstAlloc->sib1Trans = NO_SIB1;
151         
152         SchCellCb *cell = schCb[schInst].cells[schInst];
153
154    schCalcSlotValues(*slotInd, &dlSchedInfo.schSlotValue);
155
156         ssb_rep = cell->cellCfg.ssbSchCfg.ssbPeriod;
157         memcpy(&cell->slotInfo, slotInd, sizeof(SlotIndInfo));
158         dlBrdcstAlloc->ssbIdxSupported = 1;
159
160    sfnSlot = ((dlSchedInfo.schSlotValue.broadcastTime.sfn * 10) +
161                     dlSchedInfo.schSlotValue.broadcastTime.slot);
162
163         slot = dlSchedInfo.schSlotValue.currentTime.slot;
164
165         dlSchedInfo.cellId = cell->cellId;
166
167         /* Identify SSB ocassion*/
168         if (sfnSlot % SCH_MIB_TRANS == 0)
169         {
170                 dlBrdcstAlloc->ssbTrans = SSB_TRANSMISSION;
171         }
172         else if (sfnSlot % ssb_rep == 0)
173         {
174                 dlBrdcstAlloc->ssbTrans = SSB_REPEAT;
175         }
176         else
177         {
178            /* not SSB occassion */
179         }
180
181    /* Identify SIB1 occasions */
182    if(sfnSlot % cell->cellCfg.sib1SchCfg.sib1NewTxPeriod == 0)
183         {
184            dlBrdcstAlloc->sib1Trans = SIB1_TRANSMISSION;
185         }
186         else if (sfnSlot % cell->cellCfg.sib1SchCfg.sib1RepetitionPeriod == 0)
187         {
188            dlBrdcstAlloc->sib1Trans = SIB1_REPITITION;
189         }
190         else
191         {
192            /* not SIB1 occassion */
193         }
194
195         if(dlBrdcstAlloc->ssbTrans || dlBrdcstAlloc->sib1Trans)
196         {
197            dlSchedInfo.isBroadcastPres = true;
198                 slot = dlSchedInfo.schSlotValue.broadcastTime.slot;
199            ret = schBroadcastAlloc(cell,dlBrdcstAlloc,slot);
200       if(ret != ROK)
201       {
202          DU_LOG("\nschBroadcastAlloc failed");
203          RETVALUE(ret);
204       }
205    }
206
207    /* check for RAR */
208         if(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.rarTime.slot]->rarInfo != NULLP)
209         {
210            slot = dlSchedInfo.schSlotValue.rarTime.slot;
211       SCH_ALLOC(rarAlloc, sizeof(RarAlloc));
212       if(!rarAlloc)
213       {
214                    DU_LOG("\nMAC: Memory Allocation failed for RAR alloc");
215                    return RFAILED;
216       }
217                  
218       dlSchedInfo.rarAlloc = rarAlloc;
219
220            /* RAR info is copied, this was earlier filled in schProcessRachInd */
221       memcpy(&rarAlloc->rarInfo,cell->schDlSlotInfo[slot]->rarInfo, sizeof(RarInfo));
222
223                  /* pdcch and pdsch data is filled */
224        schFillRar(rarAlloc,
225                     cell->schDlSlotInfo[slot]->rarInfo->raRnti,
226                     cell->cellCfg.phyCellId,
227                     cell->cellCfg.ssbSchCfg.ssbOffsetPointA);
228
229       SCH_FREE(cell->schDlSlotInfo[slot]->rarInfo,sizeof(RarAlloc));
230            cell->schDlSlotInfo[slot]->rarInfo = NULLP;
231    }
232
233    /* check for MSG4 */
234    if(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.msg4Time.slot]->msg4Info != NULLP)
235    {
236             slot = dlSchedInfo.schSlotValue.msg4Time.slot;
237             SCH_ALLOC(msg4Alloc, sizeof(Msg4Alloc));
238                  if(!msg4Alloc)
239                  {
240                     DU_LOG("\nMAC: Memory Allocation failed for msg4 alloc");
241                          return RFAILED;
242                  }
243                  
244                  dlSchedInfo.msg4Alloc = msg4Alloc;
245
246        /* Msg4 info is copied, this was earlier filled in macSchDlRlcBoInfo */
247        memcpy(&msg4Alloc->msg4Info, cell->schDlSlotInfo[slot]->msg4Info, \
248           sizeof(Msg4Info));
249              
250        /* pdcch and pdsch data is filled */
251        schDlRsrcAllocMsg4(msg4Alloc, cell, dlSchedInfo.schSlotValue.msg4Time.slot); 
252
253                  /* PUCCH resource */
254        schAllocPucchResource(cell, msg4Alloc->msg4Info.crnti, dlSchedInfo.schSlotValue.msg4Time.slot);
255
256                  SCH_FREE(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.msg4Time.slot]->msg4Info, sizeof(Msg4Info));
257                  cell->schDlSlotInfo[dlSchedInfo.schSlotValue.msg4Time.slot]->msg4Info = NULL;
258    }
259
260
261         /* send msg to MAC */
262    ret = sendDlAllocToMac(&dlSchedInfo, schInst);
263    if(ret != ROK)
264    {
265       DU_LOG("\nSending DL Broadcast allocation from SCH to MAC failed");
266       RETVALUE(ret);
267    }
268     
269         schUlResAlloc(cell, schInst);
270
271         memset(cell->schDlSlotInfo[slot], 0, sizeof(SchDlSlotInfo));
272
273         return ret;
274 }
275
276 /**********************************************************************
277   End of file
278  **********************************************************************/
279
280