Fixed issues in processing of large DL packets [Issue-ID: ODUHIGH-309]
[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);
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->dlMsgTime, PHY_DELTA + 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  ret = ROK;
229    uint8_t  ssb_rep, ueIdx, lcgIdx;
230    uint16_t slot, sfnSlot = 0;
231    DlSchedInfo dlSchedInfo;
232    DlBrdcstAlloc *dlBrdcstAlloc = NULLP;
233    RarAlloc   *rarAlloc = NULLP;
234    DlMsgAlloc  *msg4Alloc = NULLP;
235    DlMsgAlloc *dlMsgAlloc = NULLP;
236    SchCellCb  *cell = NULLP;
237
238    memset(&dlSchedInfo,0,sizeof(DlSchedInfo));
239    schCalcSlotValues(*slotInd, &dlSchedInfo.schSlotValue);
240    dlBrdcstAlloc = &dlSchedInfo.brdcstAlloc;
241    dlBrdcstAlloc->ssbTrans = NO_SSB;
242    dlBrdcstAlloc->sib1Trans = NO_SIB1;
243
244    cell = schCb[schInst].cells[schInst];
245    ssb_rep = cell->cellCfg.ssbSchCfg.ssbPeriod;
246    memcpy(&cell->slotInfo, slotInd, sizeof(SlotIndInfo));
247    dlBrdcstAlloc->ssbIdxSupported = 1;
248
249    sfnSlot = dlSchedInfo.schSlotValue.broadcastTime.sfn * cell->numSlots +
250       dlSchedInfo.schSlotValue.broadcastTime.slot;
251
252    slot = dlSchedInfo.schSlotValue.currentTime.slot;
253
254    dlSchedInfo.cellId = cell->cellId;
255
256    /* Identify SSB ocassion*/
257    if (sfnSlot % SCH_MIB_TRANS == 0)
258    {
259       dlBrdcstAlloc->ssbTrans = SSB_TRANSMISSION;
260       if(!cell->firstSsbTransmitted)
261          cell->firstSsbTransmitted = true;
262    }
263    else if (cell->firstSsbTransmitted && (sfnSlot % ssb_rep == 0))
264    {
265       dlBrdcstAlloc->ssbTrans = SSB_REPEAT;
266    }
267    else
268    {
269       /* not SSB occassion */
270    }
271
272    /* Identify SIB1 occasions */
273    if(sfnSlot % cell->cellCfg.sib1SchCfg.sib1NewTxPeriod == 0)
274    {
275       dlBrdcstAlloc->sib1Trans = SIB1_TRANSMISSION;
276       if(!cell->firstSib1Transmitted)
277          cell->firstSib1Transmitted = true;
278    }
279    else if (cell->firstSib1Transmitted && (sfnSlot % cell->cellCfg.sib1SchCfg.sib1RepetitionPeriod == 0))
280    {
281       dlBrdcstAlloc->sib1Trans = SIB1_REPITITION;
282    }
283    else
284    {
285       /* not SIB1 occassion */
286    }
287
288    if(dlBrdcstAlloc->ssbTrans || dlBrdcstAlloc->sib1Trans)
289    {
290       dlSchedInfo.isBroadcastPres = true;
291       slot = dlSchedInfo.schSlotValue.broadcastTime.slot;
292       ret = schBroadcastAlloc(cell,dlBrdcstAlloc,slot);
293       if(ret != ROK)
294       {
295          DU_LOG("\nERROR  -->  SCH : schBroadcastAlloc failed");
296          return (ret);
297       }
298    }
299
300    /* check for RAR */
301    if(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.rarTime.slot]->rarInfo != NULLP)
302    {
303       slot = dlSchedInfo.schSlotValue.rarTime.slot;
304       SCH_ALLOC(rarAlloc, sizeof(RarAlloc));
305       if(!rarAlloc)
306       {
307          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for RAR alloc");
308          return RFAILED;
309       }
310
311       dlSchedInfo.rarAlloc = rarAlloc;
312
313       /* RAR info is copied, this was earlier filled in schProcessRachInd */
314       memcpy(&rarAlloc->rarInfo,cell->schDlSlotInfo[slot]->rarInfo, sizeof(RarInfo));
315
316       /* pdcch and pdsch data is filled */
317       schFillRar(rarAlloc,
318             cell->schDlSlotInfo[slot]->rarInfo->raRnti,
319             cell->cellCfg.phyCellId,
320             cell->cellCfg.ssbSchCfg.ssbOffsetPointA);
321
322       SCH_FREE(cell->schDlSlotInfo[slot]->rarInfo,sizeof(RarAlloc));
323       cell->schDlSlotInfo[slot]->rarInfo = NULLP;
324    }
325
326    /* check for MSG4 */
327    if((cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo != NULLP) &&
328       (cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo->isMsg4Pdu))
329    {
330       slot = dlSchedInfo.schSlotValue.dlMsgTime.slot;
331
332       SCH_ALLOC(msg4Alloc, sizeof(DlMsgAlloc));
333       if(!msg4Alloc)
334       {
335          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for msg4 alloc");
336          return RFAILED;
337       }
338
339       dlSchedInfo.dlMsgAlloc = msg4Alloc;
340
341       /* Msg4 info is copied, this was earlier filled in macSchDlRlcBoInfo */
342       memcpy(&msg4Alloc->dlMsgInfo, cell->schDlSlotInfo[slot]->dlMsgInfo, \
343             sizeof(DlMsgInfo));
344
345       /* pdcch and pdsch data is filled */
346       schDlRsrcAllocMsg4(msg4Alloc, cell, dlSchedInfo.schSlotValue.dlMsgTime.slot); 
347
348       /* PUCCH resource */
349       schAllocPucchResource(cell, msg4Alloc->dlMsgInfo.crnti, dlSchedInfo.schSlotValue.dlMsgTime.slot);
350
351       SCH_FREE(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo, sizeof(DlMsgInfo));
352       cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo = NULL;
353    }
354    /* check if UL grant must be sent in this slot for a SR/BSR that had been received */
355    for(ueIdx=0; ueIdx<cell->numActvUe; ueIdx++)
356    {
357       uint32_t totDataReq = 0; /* in bytes */
358       DciInfo  *dciInfo = NULLP;
359       SchUeCb *ueCb = NULLP;
360    
361       ueCb = &cell->ueCb[ueIdx];
362       /* check for SR */
363       if(ueCb->srRcvd)
364       {
365          totDataReq = UL_GRANT_SIZE; /*fixing so that all control msgs can be handled in SR */
366          ueCb->srRcvd = false;
367       }
368       /* check for BSR */
369       for(lcgIdx=0; lcgIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcgIdx++)
370       {
371         totDataReq+= ueCb->bsrInfo[lcgIdx].dataVol;
372         ueCb->bsrInfo[lcgIdx].dataVol = 0;
373       }
374       if(totDataReq > 0) /* UL grant must be provided for this UE in this slot */
375       {
376          SchPuschInfo schPuschInfo;
377          memset(&schPuschInfo, 0, sizeof(SchPuschInfo));
378
379          SCH_ALLOC(dciInfo, sizeof(DciInfo));
380          if(!dciInfo)
381          {
382             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciInfo alloc");
383             return RFAILED;
384          }
385          memset(dciInfo,0,sizeof(DciInfo));
386          /* update the SFN and SLOT */
387          memcpy(&dlSchedInfo.schSlotValue.ulDciTime, slotInd, sizeof(SlotIndInfo));
388          slot = dlSchedInfo.schSlotValue.ulDciTime.slot;
389          /* Update PUSCH allocation */
390          schFillPuschAlloc(ueCb, slot, totDataReq, &schPuschInfo);
391          /* Fill DCI for UL grant */
392          schFillUlDci(ueCb, schPuschInfo, dciInfo);
393          memcpy(&dciInfo->slotIndInfo, &dlSchedInfo.schSlotValue.ulDciTime, sizeof(SlotIndInfo));
394          dlSchedInfo.ulGrant = dciInfo;
395       }
396    }
397
398    /* Check for pending BO grant for LC */
399    if((cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo != NULLP) &&
400       (!cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo->isMsg4Pdu))
401    {
402       schFillBoGrantDlSchedInfo(cell, &dlSchedInfo, dlMsgAlloc);
403    }
404
405    /* send msg to MAC */
406    ret = sendDlAllocToMac(&dlSchedInfo, schInst);
407    if(ret != ROK)
408    {
409       DU_LOG("\nERROR  -->  SCH : Sending DL Broadcast allocation from SCH to MAC failed");
410       return (ret);
411    }
412
413    schInitDlSlot(cell->schDlSlotInfo[slot]);
414    schUlResAlloc(cell, schInst);
415
416
417    return ret;
418 }
419
420 /**********************************************************************
421   End of file
422  **********************************************************************/
423
424