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