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