PRACH ocassion tracking
[o-du/l2.git] / src / 5gnrsch / sch_common.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
26
27 File:     sch_common.c
28
29  **********************************************************************/
30
31 /** @file sch_common.c
32   @brief This module performs common scheduling
33  */
34 #include "stdbool.h"
35 #include "envopt.h"        /* environment options */
36 #include "envdep.h"        /* environment dependent */
37 #include "envind.h"        /* environment independent */
38 #include "gen.h"           /* general layer */
39 #include "ssi.h"           /* system service interface */
40 #include "cm_tkns.h"       /* Common Token Defines */
41 #include "cm_llist.h"      /* Common Link List Defines */
42 #include "cm_hash.h"       /* Common Hash List Defines */
43 #include "cm_mblk.h"       /* common memory link list library */
44 #include "cm_lte.h"        /* Common LTE Defines */
45 #include "tfu.h"
46 #include "lrg.h"
47
48 #include "gen.x"           /* general layer typedefs */
49 #include "ssi.x"           /* system services typedefs */
50 #include "cm5.x"           /* system services */
51 #include "cm_tkns.x"       /* Common Token Definitions */
52 #include "cm_llist.x"      /* Common Link List Definitions */
53 #include "cm_lib.x"        /* Common Library Definitions */
54 #include "cm_hash.x"       /* Common Hash List Definitions */
55 #include "cm_mblk.x"       /* common memory link list library */
56 #include "cm_lte.x"        /* Common LTE Defines */
57 #include "tfu.x"
58 #include "lrg.x"
59 #include "du_log.h"
60 #include "du_app_mac_inf.h"
61 #include "mac_sch_interface.h"
62 #include "sch.h"
63 #include "sch_utils.h"
64
65 extern SchCb schCb[SCH_MAX_INST];
66 extern uint16_t prachCfgIdxTable[MAX_PRACH_CONFIG_IDX][8];
67 extern uint16_t numRbForPrachTable[MAX_RACH_NUM_RB_IDX][5];
68
69 SchMacUlSchInfoFunc schMacUlSchInfoOpts[] =
70 {
71         packSchMacUlSchInfo,
72         MacProcUlSchInfo,
73         packSchMacUlSchInfo
74 };
75
76 /**
77  * @brief Time domain allocation for SSB
78  *
79  * @details
80  *
81  *     Function : ssbDlTdAlloc 
82  *     
83  *     This function handles common scheduling for DL
84  *     
85  *  @param[in]  schCellCb *cell, cell cb
86  *  @param[in]  DlBrdcstAlloc *dlBrdcstAlloc, DL brdcst allocation
87  *  @return  void
88  **/
89 void ssbDlTdAlloc(uint8_t scs, uint8_t *ssbStartSymb)
90 {
91    uint8_t n;
92         /* Determine value of "n" based on Section 4.1 of 3GPP TS 38.213 */
93         switch(scs)
94         {
95                 case SCH_SCS_15KHZ:
96                         {
97                            uint8_t symbIdx=0;
98                            n = 2;/* n = 0, 1 for SCS = 15KHz */
99                                 for(uint8_t idx=0; idx<n; idx++)
100                                 {
101                /* start symbol determined using {2, 8} + 14n */
102                                         ssbStartSymb[symbIdx++] = 2 + SCH_SYMBOL_PER_SLOT*idx;
103                                         ssbStartSymb[symbIdx++] = 8 + SCH_SYMBOL_PER_SLOT*idx;
104                                 }
105                         }
106                         break;
107                 default:
108                         DU_LOG("\nSCS %d is currently not supported", scs);
109         }
110 }
111
112 /**
113  * @brief common resource allocation for SSB
114  *
115  * @details
116  *
117  *     Function : schCmnDlAlloc
118  *     
119  *     This function handles common scheduling for DL
120  *     
121  *  @param[in]  SchCellCb *cell, cell cb
122  *  @param[in]  DlBrdcstAlloc *dlBrdcstAlloc, DL brdcst allocation
123  *  @return  void
124  **/
125 uint8_t schCmnDlAlloc(SchCellCb *cell, DlBrdcstAlloc *dlBrdcstAlloc)
126 {
127         /* schedule SSB */
128         uint8_t scs, ssbStartPrb, ssbStartSymb, idx;
129         uint8_t ssbStartSymbArr[SCH_MAX_SSB_BEAM];
130         SchDlAlloc *dlAlloc;
131         SsbInfo ssbInfo;
132
133         dlAlloc = cell->dlAlloc[cell->slotInfo.slot];
134         if(dlBrdcstAlloc->ssbTrans)
135         {
136                 scs = cell->cellCfg.ssbSchCfg.scsCommon;
137                 ssbStartPrb = \
138                    (cell->cellCfg.ssbSchCfg.ssbOffsetPointA)/SCH_NUM_SC_PRB;
139
140                 memset(ssbStartSymbArr, 0, SCH_MAX_SSB_BEAM);
141                 ssbDlTdAlloc(scs, ssbStartSymbArr);
142                 ssbStartSymb = ssbStartSymbArr[dlBrdcstAlloc->ssbIdxSupported-1]; /*since we are supporting only 1 ssb beam */
143                 /* Assign interface structure */
144                 for(idx=0; idx<dlBrdcstAlloc->ssbIdxSupported; idx++)
145                 {
146                         ssbInfo.ssbIdx = idx;
147                         ssbInfo.fdAlloc.ssbStartPrbIdx = ssbStartPrb;
148                         ssbInfo.fdAlloc.ssbPrbDuration = SCH_SSB_PRB_DURATION;
149                         ssbInfo.tdAlloc.ssbStartSymbIdx = ssbStartSymb;
150                         ssbInfo.tdAlloc.ssbSymbolDuration = SCH_SSB_SYMB_DURATION;
151                         dlBrdcstAlloc->ssbInfo[idx] = ssbInfo;
152                         dlAlloc->ssbInfo[idx] = ssbInfo;
153
154                 }
155
156                 dlAlloc->ssbPres = true;
157                 dlAlloc->ssbIdxSupported = dlBrdcstAlloc->ssbIdxSupported;
158                 for(idx=ssbStartSymb; idx<ssbStartSymb+SCH_SSB_SYMB_DURATION; idx++)
159                 {
160                         dlAlloc->assignedPrb[idx] = SCH_SSB_PRB_DURATION + 1; /* +1 for kSsb */
161                 }
162
163         }
164
165         /* SIB1 allocation */
166         if(dlBrdcstAlloc->sib1Trans)
167         {
168            memcpy(&dlBrdcstAlloc->sib1Alloc.sib1PdcchCfg, &cell->cellCfg.sib1SchCfg.sib1PdcchCfg, sizeof(Sib1PdcchCfg)); 
169            memcpy(&dlBrdcstAlloc->sib1Alloc.sib1PdschCfg, &cell->cellCfg.sib1SchCfg.sib1PdschCfg, sizeof(Sib1PdschCfg)); 
170         }
171         return ROK;
172 }
173
174 /*******************************************************************
175  *
176  * @brief Handles sending UL scheduler info to MAC 
177  *
178  * @details
179  *
180  *    Function : sendUlSchInfoToMac
181  *
182  *    Functionality:
183  *     Sends UL Sch info to MAC from SCH
184  *
185  * @params[in] 
186  * @return ROK     - success
187  *         RFAILED - failure
188  *
189  * ****************************************************************/
190 int sendUlSchInfoToMac(UlSchInfo *ulSchInfo, Inst inst)
191 {
192         Pst pst;
193
194    memset(&pst, 0, sizeof(Pst));
195    SCH_FILL_RSP_PST(pst, inst);
196         pst.event = EVENT_UL_SCH_INFO;
197
198         return(*schMacUlSchInfoOpts[pst.selector])(&pst, ulSchInfo);
199
200 }
201 /**
202  * @brief resource allocation for PRACH
203  *
204  * @details
205  *
206  *     Function : schPrachResAlloc
207  *     
208  *     This function handles PRACH allocation
209  *     
210  *  @param[in]  SchCellCb *cell, cell cb
211  *  @param[in]  UlSchInfo *ulSchInfo, UL scheduling info
212  *  @return  void
213  **/
214 int schPrachResAlloc(SchCellCb *cell, UlSchInfo *ulSchInfo)
215 {
216    uint8_t  numPrachRb;
217         uint8_t  numRa;
218         uint8_t  freqStart;
219         uint16_t sfn;
220         uint16_t slot;
221         uint8_t  prachCfgIdx = 0;
222         uint8_t  prachFormat = 0;
223         uint8_t  x = 0;
224         uint8_t  y = 0;
225         uint8_t  prachSubframe = 0;
226         uint8_t  prachStartSymbol = 0;
227         uint8_t  prachOcas = 0;
228         uint8_t  dataType = 0;
229         uint8_t  idx = 0;
230         SchUlAlloc *ulAlloc = NULLP;
231
232         sfn  = cell->slotInfo.sfn;
233         slot = cell->slotInfo.slot; 
234         ulAlloc = cell->ulAlloc[cell->slotInfo.slot];
235         prachCfgIdx = cell->cellCfg.schRachCfg.prachCfgIdx;
236
237     /* derive the prachCfgIdx table paramters */
238         x                = prachCfgIdxTable[prachCfgIdx][1];
239         y                = prachCfgIdxTable[prachCfgIdx][2];
240         prachSubframe    = prachCfgIdxTable[prachCfgIdx][3];
241         
242         if((sfn%x) != y)
243         {
244            /* prach occasion does not lie in this SFN */
245                 DU_LOG("\nPRACH ocassion doesn't lie in this SFN");
246            return RFAILED;
247         }
248         /* check for subFrame number */
249         if ((1 << slot) & prachSubframe)
250         {
251       /* prach ocassion present in this subframe */
252
253                 prachFormat      = prachCfgIdxTable[prachCfgIdx][0];
254                 prachStartSymbol = prachCfgIdxTable[prachCfgIdx][4];
255                 prachOcas        = prachCfgIdxTable[prachCfgIdx][6];
256
257                 /* freq domain resource determination for RACH*/
258                 freqStart = cell->cellCfg.schRachCfg.msg1FreqStart;
259                 /* numRa determined as 𝑛 belonging {0,1,.., M − 1}, 
260                  * where M is given by msg1Fdm */
261                 numRa = (cell->cellCfg.schRachCfg.msg1Fdm - 1);
262                 for(idx=0; idx<MAX_RACH_NUM_RB_IDX; idx++)
263                 {
264          if(numRbForPrachTable[idx][0] == cell->cellCfg.schRachCfg.rootSeqIdx)
265                            break;
266                 }
267                 numPrachRb = numRbForPrachTable[idx][3];
268                 /* Considering first slot in the frame for PRACH */
269                 idx = 0;
270                 ulAlloc->assignedPrb[idx] = freqStart+numPrachRb;
271         }
272
273         /* Fill UL SCH Info */
274         ulSchInfo->cellId = cell->cellId;
275         ulSchInfo->slotIndInfo.sfn = sfn;
276    ulSchInfo->slotIndInfo.slot = slot;
277         ulSchInfo->dataType = dataType | SCH_DATATYPE_PRACH;
278         /* prach info */
279         ulSchInfo->prachSchInfo.numPrachOcas   = prachOcas;
280         ulSchInfo->prachSchInfo.prachFormat    = prachFormat;
281         ulSchInfo->prachSchInfo.numRa          = numRa;
282         ulSchInfo->prachSchInfo.prachStartSymb = prachStartSymbol;
283
284         return ROK;
285 }
286
287 /**
288  * @brief resource allocation for UL
289  *
290  * @details
291  *
292  *     Function : schUlResAlloc
293  *     
294  *     This function handles UL Resource allocation
295  *     
296  *  @param[in]  SchCellCb *cell, cellCb
297  *  @return  void
298  **/
299 int schUlResAlloc(SchCellCb *cell, Inst schInst)
300 {
301    int ret = ROK;
302    UlSchInfo ulSchInfo;
303    /* Schedule resources for PRACH */
304         schPrachResAlloc(cell, &ulSchInfo);
305         //send msg to MAC
306    ret = sendUlSchInfoToMac(&ulSchInfo, schInst);
307    if(ret != ROK)
308    {
309       DU_LOG("\nSending UL Sch info from SCH to MAC failed");
310    }
311     
312         return ret;
313 }
314 /**********************************************************************
315   End of file
316  **********************************************************************/
317