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