[JIRA ID: ODUHIGH-242] RB configuration for Ue Context Setup for RLC
[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_CONFIG_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
164    cellCb->numActvUe++;
165    SET_ONE_BIT(ueCb->ueIdx, cellCb->actvUeBitMap);
166
167    ueCb->cellCb = cellCb;
168    ueCb->srRcvd = false;
169    for(idx=0; idx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; idx++)
170       ueCb->bsrInfo[idx].dataVol = 0;
171
172    SchSendUeCfgRspToMac(ueCfg, inst, RSP_OK, &cfgRsp);
173    return ROK;
174 }
175
176 /*******************************************************************
177 *
178 * @brief Fills PUSCH UL allocation
179 *
180 * @details
181 *
182 *    Function : schFillPuschAlloc
183 *
184 *    Functionality: fills PUSCH info
185 *
186 * @params[in]
187 * @return ROK     - success
188 *         RFAILED - failure
189 *
190 * ****************************************************************/
191 uint8_t schFillPuschAlloc(SchUeCb *ueCb, uint16_t pdcchSlot, uint32_t dataVol, SchPuschInfo *puschInfo)
192 {
193   uint16_t puschSlot      = 0;
194   uint16_t startRb        = 0;
195   uint8_t  numRb          = 0;
196   uint8_t  mcs            = 4;
197   uint8_t  numPdschSymbols= 14;
198   uint16_t tbSize         = 0;
199   uint8_t  buffer         = 5;
200   uint8_t  idx            = 0;
201   SchCellCb *cellCb       = ueCb->cellCb;
202   SchUlSlotInfo *schUlSlotInfo = NULLP;
203   uint8_t  k2 = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].k2;
204   uint8_t  startSymb = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].startSymbol;
205   uint8_t  symbLen = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].symbolLength;
206
207   puschSlot = (pdcchSlot + k2) % SCH_NUM_SLOTS;
208
209   startRb = cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb;
210   tbSize  = schCalcTbSize(dataVol + buffer); /*  2 bytes header + some buffer */
211   numRb   = schCalcNumPrb(tbSize, mcs, numPdschSymbols);
212   /* increment PUSCH PRB */
213
214   cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb += numRb;
215
216   puschInfo->crnti             = ueCb->crnti; 
217   puschInfo->harqProcId        = SCH_HARQ_PROC_ID;
218   puschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
219   puschInfo->fdAlloc.startPrb  = startRb;
220   puschInfo->fdAlloc.numPrb    = numRb;
221   puschInfo->tdAlloc.startSymb = startSymb;
222   puschInfo->tdAlloc.numSymb   = symbLen;
223   puschInfo->tbInfo.mcs        = mcs;
224   puschInfo->tbInfo.ndi        = 1; /* new transmission */
225   puschInfo->tbInfo.rv         = 0;
226   puschInfo->tbInfo.tbSize     = tbSize;
227   puschInfo->dmrsMappingType   = DMRS_MAP_TYPE_A;  /* Setting Type-A */
228   puschInfo->nrOfDmrsSymbols   = NUM_DMRS_SYMBOLS;
229   puschInfo->dmrsAddPos        = DMRS_ADDITIONAL_POS;
230
231   /* Update pusch in cell */
232   for(idx=startSymb; idx<symbLen; idx++)
233   {
234      cellCb->schUlSlotInfo[puschSlot]->assignedPrb[idx] = startRb + numRb;
235   }
236
237   schUlSlotInfo = cellCb->schUlSlotInfo[puschSlot];
238
239   SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
240   if(!schUlSlotInfo->schPuschInfo)
241   {
242      DU_LOG("SCH: Memory allocation failed in schAllocMsg3Pusch");
243      return RFAILED;
244   }
245   memcpy(schUlSlotInfo->schPuschInfo, puschInfo, sizeof(SchPuschInfo));
246
247   return ROK;
248 }
249
250 /*******************************************************************
251  *
252  * @brief Fills DCI for UL grant
253  *
254  * @details
255  *
256  *    Function : schFillUlDci
257  *
258  *    Functionality: fills DCI for UL grant in response to BSR
259  *
260  * @params[in]
261  * @return ROK     - success
262  *         RFAILED - failure
263  *
264  * ****************************************************************/
265 uint8_t schFillUlDci(SchUeCb *ueCb, SchPuschInfo puschInfo, DciInfo *dciInfo)
266 {
267    SchCellCb         *cellCb  = ueCb->cellCb;
268    SchControlRsrcSet coreset1 = ueCb->ueCfg.spCellCfg.servCellCfg.initDlBwp.pdcchCfg.cRSetToAddModList[0];
269
270    dciInfo->cellId = cellCb->cellId;
271    dciInfo->crnti  = ueCb->crnti;
272
273    /* fill bwp cfg */
274    dciInfo->bwpCfg.subcarrierSpacing  = cellCb->cellCfg.sib1SchCfg.bwp.subcarrierSpacing;
275    dciInfo->bwpCfg.cyclicPrefix       = cellCb->cellCfg.sib1SchCfg.bwp.cyclicPrefix;
276    dciInfo->bwpCfg.freqAlloc.startPrb = 0;
277    dciInfo->bwpCfg.freqAlloc.numPrb   = MAX_NUM_RB; /* whole of BW */
278
279    /*fill coreset cfg */
280    //Considering number of RBs in coreset1 is same as coreset0
281    dciInfo->coresetCfg.coreSetSize      = coresetIdxTable[0][1];
282    //Considering coreset1 also starts from same symbol as coreset0
283    dciInfo->coresetCfg.startSymbolIndex = searchSpaceIdxTable[0][3];
284    dciInfo->coresetCfg.durationSymbols  = coreset1.duration;
285    memcpy(dciInfo->coresetCfg.freqDomainResource, coreset1.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
286    
287    dciInfo->coresetCfg.cceRegMappingType   = coreset1.cceRegMappingType; /* non-interleaved */
288    dciInfo->coresetCfg.regBundleSize       = 6; /* must be 6 for non-interleaved */
289    dciInfo->coresetCfg.interleaverSize     = 0; /* NA for non-interleaved */
290    dciInfo->coresetCfg.coreSetType         = 1; /* non PBCH coreset */
291    dciInfo->coresetCfg.shiftIndex          = cellCb->cellCfg.phyCellId;
292    dciInfo->coresetCfg.precoderGranularity = coreset1.precoderGranularity;
293    dciInfo->coresetCfg.cceIndex            = 0; /* 0-3 for UL and 4-7 for DL */
294    dciInfo->coresetCfg.aggregationLevel    = 4; /* same as for sib1 */
295    
296    dciInfo->formatType = FORMAT0_0;
297    
298    /* fill UL grant */
299    dciInfo->format.format0_0.resourceAllocType   = puschInfo.resAllocType;
300    dciInfo->format.format0_0.freqAlloc.startPrb  = puschInfo.fdAlloc.startPrb;
301    dciInfo->format.format0_0.freqAlloc.numPrb    = puschInfo.fdAlloc.numPrb;
302    dciInfo->format.format0_0.timeAlloc.startSymb = puschInfo.tdAlloc.startSymb;
303    dciInfo->format.format0_0.timeAlloc.numSymb   = puschInfo.tdAlloc.numSymb;
304    dciInfo->format.format0_0.rowIndex            = 0; /* row Index */
305    dciInfo->format.format0_0.mcs                 = puschInfo.tbInfo.mcs;
306    dciInfo->format.format0_0.harqProcId          = puschInfo.harqProcId;
307    dciInfo->format.format0_0.puschHopFlag        = FALSE; /* disabled */
308    dciInfo->format.format0_0.freqHopFlag         = FALSE; /* disabled */
309    dciInfo->format.format0_0.ndi                 = puschInfo.tbInfo.ndi; /* new transmission */
310    dciInfo->format.format0_0.rv                  = puschInfo.tbInfo.rv;
311    dciInfo->format.format0_0.tpcCmd              = 0; //Sphoorthi TODO: check
312    dciInfo->format.format0_0.sUlCfgd             = FALSE; /* SUL not configured */
313    
314    /* Fill DCI Structure */
315    dciInfo->dciInfo.rnti                              = ueCb->crnti;
316    dciInfo->dciInfo.scramblingId                      = cellCb->cellCfg.phyCellId;
317    dciInfo->dciInfo.scramblingRnti                    = 0;
318    dciInfo->dciInfo.cceIndex                          = 0; /* 0-3 for UL and 4-7 for DL */
319    dciInfo->dciInfo.aggregLevel                       = 4;
320    dciInfo->dciInfo.beamPdcchInfo.numPrgs             = 1;
321    dciInfo->dciInfo.beamPdcchInfo.prgSize             = 1;
322    dciInfo->dciInfo.beamPdcchInfo.digBfInterfaces     = 0;
323    dciInfo->dciInfo.beamPdcchInfo.prg[0].pmIdx        = 0;
324    dciInfo->dciInfo.beamPdcchInfo.prg[0].beamIdx[0]   = 0;
325    dciInfo->dciInfo.txPdcchPower.powerValue           = 0;
326    dciInfo->dciInfo.txPdcchPower.powerControlOffsetSS = 0;
327    dciInfo->dciInfo.pdschCfg                          = NULLP; /* No DL data being sent */
328
329    return ROK;
330 }
331
332
333 /*******************************************************************
334  *
335  * @brief Hanles Ue Reconfig request from MAC
336  *
337  * @details
338  *
339  *    Function : MacSchUeReconfigReq
340  *
341  *    Functionality: Hanles Ue Reconfig request from MAC
342  *
343  * @params[in] 
344  * @return ROK     - success
345  *         RFAILED - failure
346  *
347  * ****************************************************************/
348 uint8_t MacSchUeReconfigReq(Pst *pst, SchUeCfg *ueCfg)
349 {
350    //TODO:
351    return ROK;
352 }
353 /**********************************************************************
354   End of file
355  **********************************************************************/