Merge "Restructure O1 module to run as a thread in O-DU High binary [Issue-Id: ODUHIG...
[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 SchCb schCb[SCH_MAX_INST];
53 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 uint8_t sendDlAllocToMac(DlSchedInfo *dlSchedInfo, Inst inst)
73 {
74    Pst pst;
75
76    memset(&pst, 0, sizeof(Pst));
77    FILL_PST_SCH_TO_MAC(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_DL);
117    ADD_DELTA_TO_TIME(slotInd, schSlotValue->broadcastTime, PHY_DELTA_DL + SCHED_DELTA);
118    ADD_DELTA_TO_TIME(slotInd, schSlotValue->rarTime, PHY_DELTA_DL + SCHED_DELTA);
119    ADD_DELTA_TO_TIME(slotInd, schSlotValue->dlMsgTime, PHY_DELTA_DL + SCHED_DELTA);
120 }
121
122 /*******************************************************************
123  *
124  * @brief 
125  *
126  * @details
127  *
128  *    Function : 
129  *
130  *    Functionality:
131  
132  *
133  * @params[in] 
134  * @return ROK     - success
135  *         RFAILED - failure
136  *
137  * ****************************************************************/
138 uint8_t schFillBoGrantDlSchedInfo(SchCellCb *cell, DlSchedInfo *dlSchedInfo, DlMsgAlloc *dlMsgAlloc)
139 {
140    uint8_t ueIdx, lcIdx;
141    uint16_t slot;
142    uint16_t crnti = 0;
143    uint32_t accumalatedSize = 0;
144    SchUeCb *ueCb = NULLP;
145
146    while(cell->boIndBitMap)
147    {
148       slot = dlSchedInfo->schSlotValue.dlMsgTime.slot;
149
150       GET_RIGHT_MOST_SET_BIT(cell->boIndBitMap, ueIdx);
151       GET_CRNTI(crnti,ueIdx);
152       ueCb = &cell->ueCb[ueIdx-1];
153
154       /* allocate PDCCH and PDSCH resources for the ue */
155       SCH_ALLOC(dlMsgAlloc, sizeof(DlMsgAlloc));
156       if(!dlMsgAlloc)
157       {
158          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for ded DL msg alloc");
159          return RFAILED;
160       }
161       memset(dlMsgAlloc, 0, sizeof(DlMsgAlloc));
162       dlSchedInfo->dlMsgAlloc = dlMsgAlloc;
163       dlMsgAlloc->crnti = crnti;
164
165       /* Dl ded Msg info is copied, this was earlier filled in macSchDlRlcBoInfo */
166       memcpy(&dlMsgAlloc->dlMsgInfo, cell->schDlSlotInfo[slot]->dlMsgInfo, \
167             sizeof(DlMsgInfo));
168
169       /* scheduled LC data fill */
170       dlMsgAlloc->numLc = 0;
171       for(lcIdx = 0; lcIdx < MAX_NUM_LC; lcIdx++)
172       {
173          if(ueCb->dlInfo.dlLcCtxt[lcIdx].bo)
174          {
175             dlMsgAlloc->lcSchInfo[dlMsgAlloc->numLc].lcId = lcIdx;
176
177             /* calculation for BO includse RLC and MAC header size */
178             dlMsgAlloc->lcSchInfo[dlMsgAlloc->numLc].schBytes = \
179                                                                 ueCb->dlInfo.dlLcCtxt[lcIdx].bo + MAC_HDR_SIZE;
180             accumalatedSize += dlMsgAlloc->lcSchInfo[dlMsgAlloc->numLc].schBytes;
181             dlMsgAlloc->numLc++;
182          }
183          ueCb->dlInfo.dlLcCtxt[lcIdx].bo = 0;
184       }
185
186       /* pdcch and pdsch data is filled */
187       schDlRsrcAllocDlMsg(dlMsgAlloc, cell, crnti, &accumalatedSize, slot);
188       /* Calculated TB size could be less than the total size requested.
189        * Hence, updated the scheduled bytes report. Following is valid only for
190        * one LC.
191        * TODO : Update the scheduling byte report for multiple LC based on QCI
192        * and Priority */
193       dlMsgAlloc->lcSchInfo[dlMsgAlloc->numLc -1].schBytes = accumalatedSize;
194
195       /* PUCCH resource */
196       schAllocPucchResource(cell, dlMsgAlloc->crnti, slot);
197
198       /* Free the dl ded msg info allocated in macSchDlRlcBoInfo */
199       SCH_FREE(cell->schDlSlotInfo[dlSchedInfo->schSlotValue.dlMsgTime.slot]->dlMsgInfo, \
200             sizeof(DlMsgInfo));
201       cell->schDlSlotInfo[dlSchedInfo->schSlotValue.dlMsgTime.slot]->dlMsgInfo = NULL;
202
203       /* after allocation is done, unset the bo bit for that ue */
204       UNSET_ONE_BIT(ueIdx, cell->boIndBitMap);
205    }
206
207    return ROK;
208 }
209
210 /*******************************************************************
211  *
212  * @brief Handles slot indication at SCH 
213  *
214  * @details
215  *
216  *    Function : schProcessSlotInd
217  *
218  *    Functionality:
219  *     Handles TTI indication received from PHY
220  *
221  * @params[in] 
222  * @return ROK     - success
223  *         RFAILED - failure
224  *
225  * ****************************************************************/
226 uint8_t schProcessSlotInd(SlotIndInfo *slotInd, Inst schInst)
227 {
228    uint8_t  ssb_rep, ueIdx, lcgIdx, ret = ROK;
229    uint16_t slot;
230    DlSchedInfo dlSchedInfo;
231    DlBrdcstAlloc *dlBrdcstAlloc = NULLP;
232    RarAlloc   *rarAlloc = NULLP;
233    DlMsgAlloc  *msg4Alloc = NULLP;
234    DlMsgAlloc *dlMsgAlloc = NULLP;
235    SchCellCb  *cell = NULLP;
236
237
238    memset(&dlSchedInfo,0,sizeof(DlSchedInfo));
239    dlSchedInfo.dlMsgAlloc = NULLP;
240    schCalcSlotValues(*slotInd, &dlSchedInfo.schSlotValue);
241    dlBrdcstAlloc = &dlSchedInfo.brdcstAlloc;
242    dlBrdcstAlloc->ssbTrans = NO_SSB;
243    dlBrdcstAlloc->sib1Trans = NO_SIB1;
244
245    cell = schCb[schInst].cells[schInst];
246    ssb_rep = cell->cellCfg.ssbSchCfg.ssbPeriod;
247    memcpy(&cell->slotInfo, slotInd, sizeof(SlotIndInfo));
248    dlBrdcstAlloc->ssbIdxSupported = 1;
249
250    slot = dlSchedInfo.schSlotValue.currentTime.slot;
251
252    dlSchedInfo.cellId = cell->cellId;
253
254    /* Identify SSB ocassion*/
255    if ((dlSchedInfo.schSlotValue.broadcastTime.sfn % SCH_MIB_TRANS == 0) && (dlSchedInfo.schSlotValue.broadcastTime.slot ==0))
256    {
257       dlBrdcstAlloc->ssbTrans = SSB_TRANSMISSION;
258       if(!cell->firstSsbTransmitted)
259          cell->firstSsbTransmitted = true;
260    }
261    else if (cell->firstSsbTransmitted) 
262    {
263       if((ssb_rep == 5) && ((dlSchedInfo.schSlotValue.broadcastTime.slot == 0 || dlSchedInfo.schSlotValue.broadcastTime.slot == 10)))
264          dlBrdcstAlloc->ssbTrans = SSB_REPEAT;
265       else if((dlSchedInfo.schSlotValue.broadcastTime.sfn % (ssb_rep/10) == 0) && dlSchedInfo.schSlotValue.broadcastTime.slot == 0)
266          dlBrdcstAlloc->ssbTrans = SSB_REPEAT;
267    }
268    else
269    {
270       /* not SSB occassion */
271    }
272
273    /* Identify SIB1 occasions */
274    if((dlSchedInfo.schSlotValue.broadcastTime.sfn % SCH_SIB1_TRANS == 0) && (dlSchedInfo.schSlotValue.broadcastTime.slot ==0))
275    {
276       dlBrdcstAlloc->sib1Trans = SIB1_TRANSMISSION;
277       if(!cell->firstSib1Transmitted)
278          cell->firstSib1Transmitted = true;
279    }
280    else if (cell->firstSib1Transmitted) 
281    {
282       if((dlSchedInfo.schSlotValue.broadcastTime.sfn % (cell->cellCfg.sib1SchCfg.sib1RepetitionPeriod/10) == 0) &&
283             (dlSchedInfo.schSlotValue.broadcastTime.slot == 0))
284       {
285          dlBrdcstAlloc->sib1Trans = SIB1_REPITITION;
286       }
287    }
288    else
289    {
290       /* not SIB1 occassion */
291    }
292
293
294    if(dlBrdcstAlloc->ssbTrans || dlBrdcstAlloc->sib1Trans)
295    {
296       dlSchedInfo.isBroadcastPres = true;
297       slot = dlSchedInfo.schSlotValue.broadcastTime.slot;
298       ret = schBroadcastAlloc(cell,dlBrdcstAlloc,slot);
299       if(ret != ROK)
300       {
301          DU_LOG("\nERROR  -->  SCH : schBroadcastAlloc failed");
302          return ret;
303       }
304    }
305
306    /* check for RAR */
307    if(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.rarTime.slot]->rarInfo != NULLP)
308    {
309       slot = dlSchedInfo.schSlotValue.rarTime.slot;
310       SCH_ALLOC(rarAlloc, sizeof(RarAlloc));
311       if(!rarAlloc)
312       {
313          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for RAR alloc");
314          return RFAILED;
315       }
316
317       dlSchedInfo.rarAlloc = rarAlloc;
318
319       /* RAR info is copied, this was earlier filled in schProcessRachInd */
320       memcpy(&rarAlloc->rarInfo,cell->schDlSlotInfo[slot]->rarInfo, sizeof(RarInfo));
321
322       /* pdcch and pdsch data is filled */
323       schFillRar(rarAlloc,
324             cell->schDlSlotInfo[slot]->rarInfo->raRnti,
325             cell->cellCfg.phyCellId,
326             cell->cellCfg.ssbSchCfg.ssbOffsetPointA);
327
328       SCH_FREE(cell->schDlSlotInfo[slot]->rarInfo,sizeof(RarAlloc));
329       cell->schDlSlotInfo[slot]->rarInfo = NULLP;
330    }
331
332    /* check for MSG4 */
333    if((cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo != NULLP) &&
334       (cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo->isMsg4Pdu))
335    {
336       slot = dlSchedInfo.schSlotValue.dlMsgTime.slot;
337
338       SCH_ALLOC(msg4Alloc, sizeof(DlMsgAlloc));
339       if(!msg4Alloc)
340       {
341          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for msg4 alloc");
342          return RFAILED;
343       }
344
345       dlSchedInfo.dlMsgAlloc = msg4Alloc;
346
347       /* Msg4 info is copied, this was earlier filled in macSchDlRlcBoInfo */
348       memcpy(&msg4Alloc->dlMsgInfo, cell->schDlSlotInfo[slot]->dlMsgInfo, \
349             sizeof(DlMsgInfo));
350
351       /* pdcch and pdsch data is filled */
352       schDlRsrcAllocMsg4(msg4Alloc, cell, dlSchedInfo.schSlotValue.dlMsgTime.slot); 
353
354       /* PUCCH resource */
355       schAllocPucchResource(cell, msg4Alloc->dlMsgInfo.crnti, dlSchedInfo.schSlotValue.dlMsgTime.slot);
356
357       SCH_FREE(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo, sizeof(DlMsgInfo));
358       cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo = NULL;
359    }
360    /* check if UL grant must be sent in this slot for a SR/BSR that had been received */
361    for(ueIdx=0; ueIdx<cell->numActvUe; ueIdx++)
362    {
363       uint32_t totDataReq = 0; /* in bytes */
364       DciInfo  *dciInfo = NULLP;
365       SchUeCb *ueCb = NULLP;
366    
367       ueCb = &cell->ueCb[ueIdx];
368       /* check for SR */
369       if(ueCb->srRcvd)
370       {
371          totDataReq = UL_GRANT_SIZE; /*fixing so that all control msgs can be handled in SR */
372          ueCb->srRcvd = false;
373       }
374       /* check for BSR */
375       for(lcgIdx=0; lcgIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcgIdx++)
376       {
377         totDataReq+= ueCb->bsrInfo[lcgIdx].dataVol;
378         ueCb->bsrInfo[lcgIdx].dataVol = 0;
379       }
380       if(totDataReq > 0) /* UL grant must be provided for this UE in this slot */
381       {
382          SchPuschInfo schPuschInfo;
383          memset(&schPuschInfo, 0, sizeof(SchPuschInfo));
384
385          SCH_ALLOC(dciInfo, sizeof(DciInfo));
386          if(!dciInfo)
387          {
388             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciInfo alloc");
389             return RFAILED;
390          }
391          memset(dciInfo,0,sizeof(DciInfo));
392          /* update the SFN and SLOT */
393          memcpy(&dlSchedInfo.schSlotValue.ulDciTime, slotInd, sizeof(SlotIndInfo));
394          slot = dlSchedInfo.schSlotValue.ulDciTime.slot;
395          /* Update PUSCH allocation */
396          schFillPuschAlloc(ueCb, slot, totDataReq, &schPuschInfo);
397          /* Fill DCI for UL grant */
398          schFillUlDci(ueCb, schPuschInfo, dciInfo);
399          memcpy(&dciInfo->slotIndInfo, &dlSchedInfo.schSlotValue.ulDciTime, sizeof(SlotIndInfo));
400          dlSchedInfo.ulGrant = dciInfo;
401       }
402    }
403
404    /* Check for pending BO grant for LC */
405    if((cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo != NULLP) &&
406       (!cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo->isMsg4Pdu))
407    {
408       schFillBoGrantDlSchedInfo(cell, &dlSchedInfo, dlMsgAlloc);
409    }
410
411    /* send msg to MAC */
412    ret = sendDlAllocToMac(&dlSchedInfo, schInst);
413    if(ret != ROK)
414    {
415       DU_LOG("\nERROR  -->  SCH : Sending DL Broadcast allocation from SCH to MAC failed");
416       return (ret);
417    }
418
419    schInitDlSlot(cell->schDlSlotInfo[slot]);
420    schUlResAlloc(cell, schInst);
421
422
423    return ret;
424 }
425
426 /**********************************************************************
427   End of file
428  **********************************************************************/
429
430