Automatic PRB allocation using bitmap in Downlink [Issue-ID: ODUHIGH-380]
[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
54 /*******************************************************************
55  *
56  * @brief Handles sending DL broadcast alloc to MAC 
57  *
58  * @details
59  *
60  *    Function : sendDlAllocToMac
61  *
62  *    Functionality:
63  *     Sends DL Broadcast Resource Allocation to MAC from SCH
64  *
65  * @params[in] 
66  * @return ROK     - success
67  *         RFAILED - failure
68  *
69  * ****************************************************************/
70 uint8_t sendDlAllocToMac(DlSchedInfo *dlSchedInfo, Inst inst)
71 {
72    Pst pst;
73
74    memset(&pst, 0, sizeof(Pst));
75    FILL_PST_SCH_TO_MAC(pst, inst);
76    pst.event = EVENT_DL_SCH_INFO;
77
78    return(*schMacDlAllocOpts[pst.selector])(&pst, dlSchedInfo);
79
80 }
81
82 /*******************************************************************
83  *
84  * @brief Handles slot indication at SCH 
85  *
86  * @details
87  *
88  *    Function : schCalcSlotValues
89  *
90  *    Functionality:
91  *     Handles TTI indication received from PHY
92  *
93  * @params[in] 
94  * @return ROK     - success
95  *         RFAILED - failure
96  *
97  * ****************************************************************/
98 void schCalcSlotValues(SlotTimingInfo slotInd, SchSlotValue *schSlotValue)
99 {
100    /****************************************************************
101     * PHY_DELTA - the physical layer delta                         * 
102     * SCHED_DELTA - scheduler schedules one slot ahead             *
103     * BO_DELTA - this delay is considered for BO response and      *
104     *            RLC buffer packet to received at MAC              *
105     * lower-mac (FAPI filling) will be working on PHY_DELTA        *
106     * brdcast scheduler will working on PHY_DELTA + SCHED_DELTA    *
107     * RAR scheduler will working on PHY_DELTA + SCHED_DELTA        *
108     * msg4 scheduler will working on PHY_DELTA + SCHED_DELTA       *
109     * dedicated DL msg scheduler will working                      *
110     *        on PHY_DELTA + SCHED_DELTA + BO_DELTA                 *
111     ****************************************************************/
112
113    ADD_DELTA_TO_TIME(slotInd, schSlotValue->currentTime, PHY_DELTA_DL);
114    ADD_DELTA_TO_TIME(slotInd, schSlotValue->broadcastTime, PHY_DELTA_DL + SCHED_DELTA);
115    ADD_DELTA_TO_TIME(slotInd, schSlotValue->rarTime, PHY_DELTA_DL + SCHED_DELTA);
116    ADD_DELTA_TO_TIME(slotInd, schSlotValue->dlMsgTime, PHY_DELTA_DL + SCHED_DELTA);
117 }
118
119 /*******************************************************************
120  *
121  * @brief Checks if a slot is to be scheduled for SSB transmission
122  *
123  * @details
124  *
125  *    Function : schCheckSsbOcc 
126  *
127  *    Functionality:
128  *       Checks if a slot is to be scheduled for SSB transmission
129  *
130  * @params[in] SlotTimingInfo slotTime
131  *             SchCellCb *cell 
132  * @return  Pdu transmission 
133  *
134  * ****************************************************************/
135 PduTxOccsaion schCheckSsbOcc(SchCellCb *cell, SlotTimingInfo slotTime)
136 {
137    uint8_t  ssb_rep;
138
139    ssb_rep = cell->cellCfg.ssbSchCfg.ssbPeriod;
140
141    /* Identify SSB ocassion*/
142    if ((slotTime.sfn % SCH_MIB_TRANS == 0) && (slotTime.slot ==0))
143    {
144       return NEW_TRANSMISSION;
145    }
146    else if(cell->firstSsbTransmitted) 
147    {
148       if((ssb_rep == 5) && ((slotTime.slot == 0 || slotTime.slot == 10)))
149          return REPEATITION;
150       else if((slotTime.sfn % (ssb_rep/10) == 0) && slotTime.slot == 0)
151          return REPEATITION;
152    }
153    /* not SSB occassion */
154    return NO_TRANSMISSION;
155 }
156
157 /*******************************************************************
158  *
159  * @brief Checks if a slot is to be scheduled for SIB1 transmission
160  *
161  * @details
162  *
163  *    Function : schCheckSib1Occ
164  *
165  *    Functionality:
166  *       Checks if a slot is to be scheduled for SIB1 transmission
167  *
168  * @params[in] SlotTimingInfo slotTime
169  *             SchCellCb *cell
170  * @return  Pdu transmission
171  *
172  * ****************************************************************/
173 PduTxOccsaion schCheckSib1Occ(SchCellCb *cell, SlotTimingInfo slotTime)
174 {
175    /* Identify SIB1 occasions */
176    if((slotTime.sfn % SCH_SIB1_TRANS == 0) && (slotTime.slot ==0))
177    {
178       return NEW_TRANSMISSION;
179    }
180    else if(cell->firstSib1Transmitted) 
181    {
182       if((slotTime.sfn % (cell->cellCfg.sib1SchCfg.sib1RepetitionPeriod/10) == 0) &&
183             (slotTime.slot == 0))
184       {
185          return REPEATITION;
186       }
187    }
188    /* not SIB1 occassion */
189    return NO_TRANSMISSION;
190 }
191
192 /*******************************************************************
193  *
194  * @brief 
195  *
196  * @details
197  *
198  *    Function : 
199  *
200  *    Functionality:
201  
202  *
203  * @params[in] 
204  * @return ROK     - success
205  *         RFAILED - failure
206  *
207  * ****************************************************************/
208 uint8_t schFillBoGrantDlSchedInfo(SchCellCb *cell, DlSchedInfo *dlSchedInfo, DlMsgAlloc *dlMsgAlloc)
209 {
210    uint8_t ueIdx, lcIdx;
211    uint16_t slot;
212    uint16_t crnti = 0;
213    uint32_t accumalatedSize = 0;
214    SchUeCb *ueCb = NULLP;
215
216    while(cell->boIndBitMap)
217    {
218       slot = dlSchedInfo->schSlotValue.dlMsgTime.slot;
219
220       GET_RIGHT_MOST_SET_BIT(cell->boIndBitMap, ueIdx);
221       GET_CRNTI(crnti,ueIdx);
222       ueCb = &cell->ueCb[ueIdx-1];
223
224       /* allocate PDCCH and PDSCH resources for the ue */
225       SCH_ALLOC(dlMsgAlloc, sizeof(DlMsgAlloc));
226       if(!dlMsgAlloc)
227       {
228          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for ded DL msg alloc");
229          return RFAILED;
230       }
231       memset(dlMsgAlloc, 0, sizeof(DlMsgAlloc));
232       dlSchedInfo->dlMsgAlloc = dlMsgAlloc;
233       dlMsgAlloc->crnti = crnti;
234
235       /* Dl ded Msg info is copied, this was earlier filled in macSchDlRlcBoInfo */
236       memcpy(&dlMsgAlloc->dlMsgInfo, cell->schDlSlotInfo[slot]->dlMsgInfo, \
237             sizeof(DlMsgInfo));
238
239       /* scheduled LC data fill */
240       dlMsgAlloc->numLc = 0;
241       for(lcIdx = 0; lcIdx < MAX_NUM_LC; lcIdx++)
242       {
243          if(ueCb->dlInfo.dlLcCtxt[lcIdx].bo)
244          {
245             dlMsgAlloc->lcSchInfo[dlMsgAlloc->numLc].lcId = lcIdx;
246
247             /* calculation for BO includse RLC and MAC header size */
248             dlMsgAlloc->lcSchInfo[dlMsgAlloc->numLc].schBytes = \
249                                                                 ueCb->dlInfo.dlLcCtxt[lcIdx].bo + MAC_HDR_SIZE;
250             accumalatedSize += dlMsgAlloc->lcSchInfo[dlMsgAlloc->numLc].schBytes;
251             dlMsgAlloc->numLc++;
252          }
253          ueCb->dlInfo.dlLcCtxt[lcIdx].bo = 0;
254       }
255       
256       if (!dlMsgAlloc->numLc)
257       {
258          DU_LOG("\nDEBUG  -->  SCH : No pending BO for any LC id\n");
259          /* Free the dl ded msg info allocated in macSchDlRlcBoInfo */
260          SCH_FREE(dlMsgAlloc, sizeof(DlMsgAlloc));
261          dlSchedInfo->dlMsgAlloc = NULLP;
262          return ROK;
263       }
264
265       /* pdcch and pdsch data is filled */
266       if((schDlRsrcAllocDlMsg(cell, dlSchedInfo->schSlotValue.dlMsgTime, crnti, &accumalatedSize, dlMsgAlloc)) != ROK)
267       {
268          DU_LOG("\nERROR  --> SCH : Scheduling of DL dedicated message failed");
269          /* Free the dl ded msg info allocated in macSchDlRlcBoInfo */
270          SCH_FREE(dlMsgAlloc, sizeof(DlMsgAlloc));
271          dlSchedInfo->dlMsgAlloc = NULLP;
272          return RFAILED;
273
274       }
275
276       /* TODO : Update the scheduling byte report for multiple LC based on QCI
277        * and Priority */
278       /* As of now, the total number of bytes scheduled for a slot is divided
279        * equally amongst all LC with pending data. This is avoid starving of any
280        * LC 
281        * */
282       accumalatedSize = accumalatedSize/dlMsgAlloc->numLc;
283       for(lcIdx = 0; lcIdx < dlMsgAlloc->numLc; lcIdx ++)
284          dlMsgAlloc->lcSchInfo[lcIdx].schBytes = accumalatedSize;
285
286       /* PUCCH resource */
287       schAllocPucchResource(cell, dlMsgAlloc->crnti, slot);
288
289       /* after allocation is done, unset the bo bit for that ue */
290       UNSET_ONE_BIT(ueIdx, cell->boIndBitMap);
291    }
292
293    return ROK;
294 }
295
296 /*******************************************************************
297  *
298  * @brief Handles slot indication at SCH 
299  *
300  * @details
301  *
302  *    Function : schProcessSlotInd
303  *
304  *    Functionality:
305  *     Handles TTI indication received from PHY
306  *
307  * @params[in] 
308  * @return ROK     - success
309  *         RFAILED - failure
310  *
311  * ****************************************************************/
312 uint8_t schProcessSlotInd(SlotTimingInfo *slotInd, Inst schInst)
313 {
314    uint8_t  ueIdx, lcgIdx, ret = ROK;
315    uint16_t slot;
316    DlSchedInfo dlSchedInfo;
317    DlBrdcstAlloc *dlBrdcstAlloc = NULLP;
318    DlMsgAlloc  *msg4Alloc = NULLP;
319    DlMsgAlloc *dlMsgAlloc = NULLP;
320    SchCellCb  *cell = NULLP;
321
322    memset(&dlSchedInfo,0,sizeof(DlSchedInfo));
323    dlSchedInfo.dlMsgAlloc = NULLP;
324    schCalcSlotValues(*slotInd, &dlSchedInfo.schSlotValue);
325    dlBrdcstAlloc = &dlSchedInfo.brdcstAlloc;
326    dlBrdcstAlloc->ssbTrans = NO_TRANSMISSION;
327    dlBrdcstAlloc->sib1Trans = NO_TRANSMISSION;
328
329    cell = schCb[schInst].cells[schInst];
330    if(cell == NULLP)
331    {
332       DU_LOG("\nERROR  -->  SCH : Cell Does not exist");
333       return RFAILED;
334    }
335    memcpy(&cell->slotInfo, slotInd, sizeof(SlotTimingInfo));
336    dlBrdcstAlloc->ssbIdxSupported = SSB_IDX_SUPPORTED;
337
338    dlSchedInfo.cellId = cell->cellId;
339
340    slot = dlSchedInfo.schSlotValue.broadcastTime.slot;
341
342    /* Check for SSB occassion */
343    dlBrdcstAlloc->ssbTrans = schCheckSsbOcc(cell, dlSchedInfo.schSlotValue.broadcastTime); 
344    if(dlBrdcstAlloc->ssbTrans)
345    {
346       if(schBroadcastSsbAlloc(cell, dlSchedInfo.schSlotValue.broadcastTime, dlBrdcstAlloc) != ROK)
347       {
348          DU_LOG("\nERROR  -->  SCH : schBroadcastSsbAlloc failed");
349          dlBrdcstAlloc->ssbTrans = NO_TRANSMISSION;
350       }
351       else 
352       {
353          dlSchedInfo.isBroadcastPres = true;
354          if((dlBrdcstAlloc->ssbTrans == NEW_TRANSMISSION) && (!cell->firstSsbTransmitted))
355             cell->firstSsbTransmitted = true;
356       }
357    }
358
359    /* Check for SIB1 occassion */
360    dlBrdcstAlloc->sib1Trans = schCheckSib1Occ(cell, dlSchedInfo.schSlotValue.broadcastTime);
361    if(dlBrdcstAlloc->sib1Trans)
362    {
363       if(schBroadcastSib1Alloc(cell, dlSchedInfo.schSlotValue.broadcastTime, dlBrdcstAlloc) != ROK)
364       {
365          DU_LOG("\nERROR  -->  SCH : schBroadcastSib1Alloc failed");
366          dlBrdcstAlloc->sib1Trans = NO_TRANSMISSION;
367       }
368       else 
369       {
370          dlSchedInfo.isBroadcastPres = true;
371          if((dlBrdcstAlloc->sib1Trans == NEW_TRANSMISSION) && (!cell->firstSib1Transmitted))
372             cell->firstSib1Transmitted = true;
373       }
374    }
375
376    /* Check for Pending RA Requests */
377    schProcessRaReq(*slotInd, cell);
378
379    /* Check for RAR */
380    if(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.rarTime.slot]->rarAlloc != NULLP)
381    {
382       slot = dlSchedInfo.schSlotValue.rarTime.slot;
383       dlSchedInfo.rarAlloc = cell->schDlSlotInfo[slot]->rarAlloc;
384       cell->schDlSlotInfo[slot]->rarAlloc = NULLP;
385    }
386
387    /* Check for MSG4 */
388    if((cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo != NULLP) &&
389       (cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo->isMsg4Pdu))
390    {
391       slot = dlSchedInfo.schSlotValue.dlMsgTime.slot;
392
393       SCH_ALLOC(msg4Alloc, sizeof(DlMsgAlloc));
394       if(!msg4Alloc)
395       {
396          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for msg4 alloc");
397          return RFAILED;
398       }
399
400       dlSchedInfo.dlMsgAlloc = msg4Alloc;
401
402       /* Msg4 info is copied, this was earlier filled in macSchDlRlcBoInfo */
403       memcpy(&msg4Alloc->dlMsgInfo, cell->schDlSlotInfo[slot]->dlMsgInfo, \
404             sizeof(DlMsgInfo));
405
406       /* pdcch and pdsch data is filled */
407       if((schDlRsrcAllocMsg4(cell, dlSchedInfo.schSlotValue.dlMsgTime, msg4Alloc)) != ROK)
408       {
409          DU_LOG("\nERROR  -->  SCH : MSG4 scheduling failed");
410          SCH_FREE(msg4Alloc, sizeof(DlMsgAlloc));
411          dlSchedInfo.dlMsgAlloc = NULLP;
412          SCH_FREE(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo, sizeof(DlMsgInfo));
413          return RFAILED;
414       }
415
416       /* PUCCH resource */
417       schAllocPucchResource(cell, msg4Alloc->dlMsgInfo.crnti, dlSchedInfo.schSlotValue.dlMsgTime.slot);
418
419       SCH_FREE(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo, sizeof(DlMsgInfo));
420    }
421
422    /* Check if UL grant must be sent in this slot for a SR/BSR that had been received */
423    for(ueIdx=0; ueIdx<cell->numActvUe; ueIdx++)
424    {
425       uint32_t totDataReq = 0; /* in bytes */
426       DciInfo  *dciInfo = NULLP;
427       SchUeCb *ueCb = NULLP;
428    
429       ueCb = &cell->ueCb[ueIdx];
430       /* check for SR */
431       if(ueCb->srRcvd)
432       {
433          totDataReq = UL_GRANT_SIZE; /*fixing so that all control msgs can be handled in SR */
434          ueCb->srRcvd = false;
435       }
436       /* check for BSR */
437       for(lcgIdx=0; lcgIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcgIdx++)
438       {
439         totDataReq+= ueCb->bsrInfo[lcgIdx].dataVol;
440         ueCb->bsrInfo[lcgIdx].dataVol = 0;
441       }
442       if(totDataReq > 0) /* UL grant must be provided for this UE in this slot */
443       {
444          SchPuschInfo schPuschInfo;
445          memset(&schPuschInfo, 0, sizeof(SchPuschInfo));
446
447          SCH_ALLOC(dciInfo, sizeof(DciInfo));
448          if(!dciInfo)
449          {
450             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciInfo alloc");
451             return RFAILED;
452          }
453          memset(dciInfo,0,sizeof(DciInfo));
454          /* update the SFN and SLOT */
455          memcpy(&dlSchedInfo.schSlotValue.ulDciTime, slotInd, sizeof(SlotTimingInfo));
456          slot = dlSchedInfo.schSlotValue.ulDciTime.slot;
457          /* Update PUSCH allocation */
458          schFillPuschAlloc(ueCb, slot, totDataReq, &schPuschInfo);
459          /* Fill DCI for UL grant */
460          schFillUlDci(ueCb, schPuschInfo, dciInfo);
461          memcpy(&dciInfo->slotIndInfo, &dlSchedInfo.schSlotValue.ulDciTime, sizeof(SlotTimingInfo));
462          dlSchedInfo.ulGrant = dciInfo;
463       }
464    }
465
466    /* Check for pending BO grant for LC */
467    if((cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo != NULLP) &&
468       (!cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo->isMsg4Pdu))
469    {
470       if((schFillBoGrantDlSchedInfo(cell, &dlSchedInfo, dlMsgAlloc)) != ROK)
471       {
472          DU_LOG("\nERROR  -->  SCH : DL MSG scheduling failed");
473          SCH_FREE(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo, sizeof(DlMsgInfo));
474          return RFAILED;
475       }
476
477       /* Free the dl ded msg info allocated in macSchDlRlcBoInfo */
478       SCH_FREE(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo, sizeof(DlMsgInfo));
479    }
480
481    /* Send msg to MAC */
482    ret = sendDlAllocToMac(&dlSchedInfo, schInst);
483    if(ret != ROK)
484    {
485       DU_LOG("\nERROR  -->  SCH : Sending DL Broadcast allocation from SCH to MAC failed");
486       return (ret);
487    }
488
489    schInitDlSlot(cell->schDlSlotInfo[slot]);
490    schUlResAlloc(cell, schInst);
491
492
493    return ret;
494 }
495
496 /**********************************************************************
497   End of file
498  **********************************************************************/
499
500