RLC BO, BO response and DL Data handling. [Issue-ID: ODUHIGH-181]
[o-du/l2.git] / src / 5gnrsch / sch_ue_mgr.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 /* This file contains UE management handling functionality for SCH */
19
20 #include "common_def.h"
21 #include "tfu.h"
22 #include "lrg.h"
23
24 #include "tfu.x"
25 #include "lrg.x"
26 #include "du_log.h"
27 #include "du_app_mac_inf.h"
28 #include "mac_sch_interface.h"
29 #include "sch.h"
30 #include "sch_utils.h"
31
32 /* local defines */
33 SchUeCfgRspFunc SchUeCfgRspOpts[] =
34 {
35    packSchUeCfgRsp,      /* LC */
36    MacProcSchUeCfgRsp,   /* TC */
37    packSchUeCfgRsp       /* LWLC */
38 };
39
40
41 /*******************************************************************
42  *
43  * @brief Fill and send UE cfg response to MAC
44  *
45  * @details
46  *
47  *    Function : SchSendUeCfgRspToMac
48  *
49  *    Functionality: Fill and send UE cfg response to MAC
50  *
51  * @params[in] 
52  * @return ROK     - success
53  *         RFAILED - failure
54  *
55  * ****************************************************************/
56 void SchSendUeCfgRspToMac(SchUeCfg *ueCfg, Inst inst,\
57       SchMacRsp result, SchUeCfgRsp *cfgRsp)
58 {
59    Pst rspPst;
60
61    DU_LOG("\nSCH: Sending UE Create response to MAC");
62
63    cfgRsp->cellId = ueCfg->cellId;
64    cfgRsp->crnti = ueCfg->crnti;
65    GET_UE_IDX(ueCfg->crnti, cfgRsp->ueIdx);
66    cfgRsp->rsp = result;   
67
68    /* Filling response post */
69    memset(&rspPst, 0, sizeof(Pst));
70    FILL_PST_SCH_TO_MAC(rspPst, inst);
71    rspPst.event = EVENT_UE_CREATE_RSP_TO_MAC;
72
73    SchUeCfgRspOpts[rspPst.selector](&rspPst, cfgRsp);
74 }
75
76 /*******************************************************************
77  *
78  * @brief Hanles Ue create request from MAC
79  *
80  * @details
81  *
82  *    Function : MacSchUeCreateReq
83  *
84  *    Functionality: Hanles Ue create request from MAC
85  *
86  * @params[in] 
87  * @return ROK     - success
88  *         RFAILED - failure
89  *
90  * ****************************************************************/
91 uint8_t MacSchUeCreateReq(Pst *pst, SchUeCfg *ueCfg)
92 {
93    uint8_t      idx;
94    uint8_t      lcId;
95    uint8_t      lcIdx;
96    uint16_t     ueIdx;
97    SchCellCb    *cellCb;
98    SchUeCb      *ueCb;
99    SchUeCfgRsp  cfgRsp;
100    Inst         inst = pst->dstInst - 1;
101
102    DU_LOG("\nSCH : UE Create Request for CRNTI[%d]", ueCfg->crnti);
103
104    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
105
106    if(!ueCfg)
107    {
108       DU_LOG("\nSCH : UE create request failed");
109       return RFAILED;
110    }
111
112    /* Search of cell cb */
113    for(idx = 0; idx < MAX_NUM_CELL; idx++)
114    {
115       cellCb = schCb[inst].cells[idx];
116       if(cellCb->cellId == ueCfg->cellId)
117          break;
118    }
119    if(idx == MAX_NUM_CELL)
120    {
121       DU_LOG("\nSCH : Ue create request failed. Invalid cell id %d", ueCfg->cellId);
122       SchSendUeCfgRspToMac(ueCfg, inst, RSP_NOK, &cfgRsp);
123       return ROK;
124    }
125
126    /* Check if max number of UE configured */
127    if(cellCb->numActvUe > MAX_NUM_UE)
128    {
129       DU_LOG("SCH : Max number of UE [%d] already configured", MAX_NUM_UE);
130       SchSendUeCfgRspToMac(ueCfg, inst, RSP_NOK, &cfgRsp);
131       return ROK;
132    }
133
134    /* Search if UE already configured */
135    GET_UE_IDX(ueCfg->crnti, ueIdx);
136    ueCb = &cellCb->ueCb[ueIdx -1];
137    if(ueCb)
138    {
139       if((ueCb->crnti == ueCfg->crnti) && (ueCb->state == SCH_UE_STATE_ACTIVE))
140       {
141          DU_LOG("\n SCH : CRNTI %d already configured ", ueCfg->crnti);
142          SchSendUeCfgRspToMac(ueCfg, inst, RSP_OK, &cfgRsp);
143          return ROK;
144       }
145    }
146
147    /* Fill received Ue Configuration in UeCb */
148    memset(ueCb, 0, sizeof(SchUeCb));
149    ueCb->ueIdx = ueIdx;
150    ueCb->crnti = ueCfg->crnti;
151    memcpy(&ueCb->ueCfg, ueCfg, sizeof(SchUeCfg));
152    ueCb->state = SCH_UE_STATE_ACTIVE;
153
154    /* Fill SRB1 info */
155    for(lcIdx = 0; lcIdx < ueCfg->numLc; lcIdx++)
156    {
157        lcId = ueCfg->lcCfgList[lcIdx].lcId;
158        ueCb->dlLcCtxt[lcId].lcp = ueCfg->lcCfgList[lcIdx].dlLcCfg.lcp;
159        ueCb->dlLcCtxt[lcId].lcState = SCH_LC_STATE_ACTIVE;
160        ueCb->dlLcCtxt[lcId].bo = 0;
161        ueCb->numDlLc++;
162        
163        if(ueCfg->lcCfgList[lcIdx].ulLcCfg)
164        {
165           /* TODO : Fill UL LC Cfg. As of now for SRB1, it is null */
166        }
167    }
168
169    cellCb->numActvUe++;
170    SET_ONE_BIT(ueCb->ueIdx, cellCb->actvUeBitMap);
171
172    ueCb->cellCb = cellCb;
173    ueCb->srRcvd = false;
174    for(idx=0; idx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; idx++)
175       ueCb->bsrInfo[idx].dataVol = 0;
176
177    SchSendUeCfgRspToMac(ueCfg, inst, RSP_OK, &cfgRsp);
178    return ROK;
179 }
180
181 /*******************************************************************
182 *
183 * @brief Fills PUSCH UL allocation
184 *
185 * @details
186 *
187 *    Function : schFillPuschAlloc
188 *
189 *    Functionality: fills PUSCH info
190 *
191 * @params[in]
192 * @return ROK     - success
193 *         RFAILED - failure
194 *
195 * ****************************************************************/
196 uint8_t schFillPuschAlloc(SchUeCb *ueCb, uint16_t pdcchSlot, uint32_t dataVol, SchPuschInfo *puschInfo)
197 {
198   uint16_t puschSlot      = 0;
199   uint16_t startRb        = 0;
200   uint8_t  numRb          = 0;
201   uint8_t  mcs            = 4;
202   uint8_t  numPdschSymbols= 14;
203   uint16_t tbSize         = 0;
204   uint8_t  buffer         = 5;
205   uint8_t  idx            = 0;
206   SchCellCb *cellCb       = ueCb->cellCb;
207   SchUlSlotInfo *schUlSlotInfo = NULLP;
208   uint8_t  k2 = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].k2;
209   uint8_t  startSymb = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].startSymbol;
210   uint8_t  symbLen = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].symbolLength;
211
212   puschSlot = (pdcchSlot + k2) % SCH_NUM_SLOTS;
213
214   startRb = cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb;
215   tbSize  = schCalcTbSize(dataVol + buffer); /*  2 bytes header + some buffer */
216   numRb   = schCalcNumPrb(tbSize, mcs, numPdschSymbols);
217   /* increment PUSCH PRB */
218
219   cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb += numRb;
220
221   puschInfo->crnti             = ueCb->crnti; 
222   puschInfo->harqProcId        = SCH_HARQ_PROC_ID;
223   puschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
224   puschInfo->fdAlloc.startPrb  = startRb;
225   puschInfo->fdAlloc.numPrb    = numRb;
226   puschInfo->tdAlloc.startSymb = startSymb;
227   puschInfo->tdAlloc.numSymb   = symbLen;
228   puschInfo->tbInfo.mcs        = mcs;
229   puschInfo->tbInfo.ndi        = 1; /* new transmission */
230   puschInfo->tbInfo.rv         = 0;
231   puschInfo->tbInfo.tbSize     = tbSize;
232   puschInfo->dmrsMappingType   = DMRS_MAP_TYPE_A;  /* Setting Type-A */
233   puschInfo->nrOfDmrsSymbols   = NUM_DMRS_SYMBOLS;
234   puschInfo->dmrsAddPos        = DMRS_ADDITIONAL_POS;
235
236   /* Update pusch in cell */
237   for(idx=startSymb; idx<symbLen; idx++)
238   {
239      cellCb->schUlSlotInfo[puschSlot]->assignedPrb[idx] = startRb + numRb;
240   }
241
242   schUlSlotInfo = cellCb->schUlSlotInfo[puschSlot];
243
244   SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
245   if(!schUlSlotInfo->schPuschInfo)
246   {
247      DU_LOG("SCH: Memory allocation failed in schAllocMsg3Pusch");
248      return RFAILED;
249   }
250   memcpy(schUlSlotInfo->schPuschInfo, puschInfo, sizeof(SchPuschInfo));
251
252   return ROK;
253 }
254
255 /*******************************************************************
256  *
257  * @brief Fills DCI for UL grant
258  *
259  * @details
260  *
261  *    Function : schFillUlDci
262  *
263  *    Functionality: fills DCI for UL grant in response to BSR
264  *
265  * @params[in]
266  * @return ROK     - success
267  *         RFAILED - failure
268  *
269  * ****************************************************************/
270 uint8_t schFillUlDci(SchUeCb *ueCb, SchPuschInfo puschInfo, DciInfo *dciInfo)
271 {
272    SchCellCb         *cellCb  = ueCb->cellCb;
273    SchControlRsrcSet coreset1 = ueCb->ueCfg.spCellCfg.servCellCfg.initDlBwp.pdcchCfg.cRSetToAddModList[0];
274
275    dciInfo->cellId = cellCb->cellId;
276    dciInfo->crnti  = ueCb->crnti;
277
278    /* fill bwp cfg */
279    dciInfo->bwpCfg.subcarrierSpacing  = cellCb->cellCfg.sib1SchCfg.bwp.subcarrierSpacing;
280    dciInfo->bwpCfg.cyclicPrefix       = cellCb->cellCfg.sib1SchCfg.bwp.cyclicPrefix;
281    dciInfo->bwpCfg.freqAlloc.startPrb = 0;
282    dciInfo->bwpCfg.freqAlloc.numPrb   = MAX_NUM_RB; /* whole of BW */
283
284    /*fill coreset cfg */
285    //Considering number of RBs in coreset1 is same as coreset0
286    dciInfo->coresetCfg.coreSetSize      = coresetIdxTable[0][1];
287    //Considering coreset1 also starts from same symbol as coreset0
288    dciInfo->coresetCfg.startSymbolIndex = searchSpaceIdxTable[0][3];
289    dciInfo->coresetCfg.durationSymbols  = coreset1.duration;
290    memcpy(dciInfo->coresetCfg.freqDomainResource, coreset1.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
291    
292    dciInfo->coresetCfg.cceRegMappingType   = coreset1.cceRegMappingType; /* non-interleaved */
293    dciInfo->coresetCfg.regBundleSize       = 6; /* must be 6 for non-interleaved */
294    dciInfo->coresetCfg.interleaverSize     = 0; /* NA for non-interleaved */
295    dciInfo->coresetCfg.coreSetType         = 1; /* non PBCH coreset */
296    dciInfo->coresetCfg.shiftIndex          = cellCb->cellCfg.phyCellId;
297    dciInfo->coresetCfg.precoderGranularity = coreset1.precoderGranularity;
298    dciInfo->coresetCfg.cceIndex            = 0; /* 0-3 for UL and 4-7 for DL */
299    dciInfo->coresetCfg.aggregationLevel    = 4; /* same as for sib1 */
300    
301    dciInfo->formatType = FORMAT0_0;
302    
303    /* fill UL grant */
304    dciInfo->format.format0_0.resourceAllocType   = puschInfo.resAllocType;
305    dciInfo->format.format0_0.freqAlloc.startPrb  = puschInfo.fdAlloc.startPrb;
306    dciInfo->format.format0_0.freqAlloc.numPrb    = puschInfo.fdAlloc.numPrb;
307    dciInfo->format.format0_0.timeAlloc.startSymb = puschInfo.tdAlloc.startSymb;
308    dciInfo->format.format0_0.timeAlloc.numSymb   = puschInfo.tdAlloc.numSymb;
309    dciInfo->format.format0_0.rowIndex            = 0; /* row Index */
310    dciInfo->format.format0_0.mcs                 = puschInfo.tbInfo.mcs;
311    dciInfo->format.format0_0.harqProcId          = puschInfo.harqProcId;
312    dciInfo->format.format0_0.puschHopFlag        = FALSE; /* disabled */
313    dciInfo->format.format0_0.freqHopFlag         = FALSE; /* disabled */
314    dciInfo->format.format0_0.ndi                 = puschInfo.tbInfo.ndi; /* new transmission */
315    dciInfo->format.format0_0.rv                  = puschInfo.tbInfo.rv;
316    dciInfo->format.format0_0.tpcCmd              = 0; //Sphoorthi TODO: check
317    dciInfo->format.format0_0.sUlCfgd             = FALSE; /* SUL not configured */
318    
319    /* Fill DCI Structure */
320    dciInfo->dciInfo.rnti                              = ueCb->crnti;
321    dciInfo->dciInfo.scramblingId                      = cellCb->cellCfg.phyCellId;
322    dciInfo->dciInfo.scramblingRnti                    = 0;
323    dciInfo->dciInfo.cceIndex                          = 0; /* 0-3 for UL and 4-7 for DL */
324    dciInfo->dciInfo.aggregLevel                       = 4;
325    dciInfo->dciInfo.beamPdcchInfo.numPrgs             = 1;
326    dciInfo->dciInfo.beamPdcchInfo.prgSize             = 1;
327    dciInfo->dciInfo.beamPdcchInfo.digBfInterfaces     = 0;
328    dciInfo->dciInfo.beamPdcchInfo.prg[0].pmIdx        = 0;
329    dciInfo->dciInfo.beamPdcchInfo.prg[0].beamIdx[0]   = 0;
330    dciInfo->dciInfo.txPdcchPower.powerValue           = 0;
331    dciInfo->dciInfo.txPdcchPower.powerControlOffsetSS = 0;
332    dciInfo->dciInfo.pdschCfg                          = NULLP; /* No DL data being sent */
333
334    return ROK;
335 }
336
337
338 /**********************************************************************
339   End of file
340  **********************************************************************/