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