Memory handling fixes
[o-du/l2.git] / src / 5gnrsch / sch.h
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 /* macros */
20 #define SCH_INST_START 1
21 #define SCH_MAX_INST 1
22 #define SCH_MU0_NUM_SLOTS 10 
23 #define SCH_MU1_NUM_SLOTS 20 
24 #define SCH_MU2_NUM_SLOTS 30 
25 #define SCH_MU3_NUM_SLOTS 40 
26 #define SCH_MU4_NUM_SLOTS 50 
27 #define SCH_MAX_SFN 1024
28 #ifdef NR_TDD
29 #define MAX_NUM_RB 275 /* value for numerology 1, 100 MHz */
30 #else
31 #define MAX_NUM_RB 106 /* value for numerology 0, 20 MHz */
32 #endif
33 #define SCH_MIB_TRANS 8  /* MIB transmission as per 38.331 is every 80 ms */
34 #define SCH_SIB1_TRANS 16 /* SIB1 transmission as per 38.331 is every 160 ms */
35 #define SCH_NUM_SC_PRB 12 /* number of SCs in a PRB */
36 #define SCH_MAX_SSB_BEAM 8 /* since we are supporting only SCS=15KHz and 30KHz */
37 #define SCH_SYMBOL_PER_SLOT 14
38 #define SCH_SSB_NUM_SYMB 4
39 #define SCH_SSB_NUM_PRB 20
40 #define SCHED_DELTA 1
41 #define BO_DELTA 1
42 #define RAR_DELAY   2
43 #define MSG4_DELAY  1
44 #define PDSCH_START_RB 10
45 #define PUSCH_START_RB 15
46 #define PUCCH_NUM_PRB_FORMAT_0_1_4 1  /* number of PRBs in freq domain, spec 38.213 - 9.2.1 */
47 #define SI_RNTI 0xFFFF
48 #define P_RNTI  0xFFFE
49 #define DMRS_MAP_TYPE_A 1
50 #define NUM_DMRS_SYMBOLS 1
51 #define DMRS_ADDITIONAL_POS 0
52 #define SCH_DEFAULT_K1 1
53 #define SCH_TQ_SIZE 10
54
55 #define CRC_FAILED 0
56 #define CRC_PASSED 1
57
58 #define MAC_HDR_SIZE  3   /* 3 bytes of MAC Header */
59 #define UL_GRANT_SIZE 224
60
61 typedef struct schCellCb SchCellCb;
62 typedef struct schUeCb SchUeCb;
63
64 typedef enum
65 {
66    SCH_NUMEROLOGY_0,
67    SCH_NUMEROLOGY_1,
68    SCH_NUMEROLOGY_2,
69    SCH_NUMEROLOGY_3,
70    SCH_NUMEROLOGY_4
71 }SchNumerology;
72
73 typedef enum
74 {
75    SCH_UE_STATE_INACTIVE,
76    SCH_UE_STATE_ACTIVE
77 }SchUeState;
78
79 typedef enum
80 {
81    SCH_LC_STATE_INACTIVE,
82    SCH_LC_STATE_ACTIVE
83 }SchLcState;
84
85 /**
86  * @brief
87  * Structure holding LTE MAC's General Configuration information.
88  */
89 typedef struct schGenCb
90 {
91    uint8_t         tmrRes;           /*!< Timer resolution */
92    uint8_t         startCellId;      /*!< Starting Cell Id */
93 #ifdef LTE_ADV
94    bool            forceCntrlSrbBoOnPCel; /*!< value 1 means force scheduling
95                                             of RLC control BO and SRB BO on
96                                             PCell. val 0 means don't force*/
97    bool            isSCellActDeactAlgoEnable; /*!< TRUE will enable activation/deactivation algo at Schd */
98 #endif
99 }SchGenCb;
100
101 /**
102  * @brief
103  * scheduler allocationsfor DL per cell.
104  */
105 typedef struct schDlSlotInfo
106 {
107    uint16_t  totalPrb;                          /*!< Number of RBs in the cell */
108    uint16_t  assignedPrb[SCH_SYMBOL_PER_SLOT];  /*!< Num RBs and corresponding symbols allocated */
109    uint16_t  resAllocBitMap;                    /*!< Resource allocation bitmap */
110    bool      ssbPres;                           /*!< Flag to determine if SSB is present in this slot */
111    uint8_t   ssbIdxSupported;                   /*!< Max SSB index */
112    SsbInfo   ssbInfo[MAX_SSB_IDX];              /*!< SSB info */
113    bool      sib1Pres;                          /*!< Flag to determine if SIB1 is present in this slot */
114    RarInfo   *rarInfo;                          /*!< RAR info */
115    DlMsgInfo *dlMsgInfo;                       /*!< DL dedicated Msg info */
116 }SchDlSlotInfo;
117
118 typedef struct schRaCb
119 {
120    uint16_t tcrnti;
121 }SchRaCb;
122
123 /**
124  * @brief
125  * scheduler allocationsfor UL per cell.
126  */
127 typedef struct schUlSlotInfo
128 {
129    uint16_t     totalPrb;  /*!< Number of RBs in the cell */
130    uint16_t     assignedPrb[SCH_SYMBOL_PER_SLOT]; /*!< Num RBs and corresponding symbols allocated */
131    uint16_t     resAllocBitMap;                    /*!< Resource allocation bitmap */
132    uint8_t      puschCurrentPrb; /* Current PRB for PUSCH allocation */
133    bool         puschPres; /*!< PUSCH presence field */
134    SchPuschInfo *schPuschInfo; /*!< PUSCH info */
135    bool         pucchPres; /*!< PUCCH presence field */
136    SchPucchInfo schPucchInfo; /*!< PUCCH info */
137 }SchUlSlotInfo;
138
139 /**
140 @brief
141 * BSR info per slot per UE.
142 */
143 typedef struct bsrInfo
144 {
145    uint8_t    priority;  /* CG priority */
146    uint32_t   dataVol;   /* Data volume requested in bytes */
147 }BsrInfo;
148
149 typedef struct schLcCtxt
150 {
151    uint8_t lcId;     // logical Channel ID
152    uint8_t lcp;      // logical Channel Prioritization
153    SchLcState lcState;
154    uint32_t bo;
155 }SchDlLcCtxt;
156
157 typedef struct schDlCb
158 {
159    uint8_t       numDlLc;
160    SchDlLcCtxt   dlLcCtxt[MAX_NUM_LC];
161 }SchDlCb;
162
163 typedef struct schUlLcCtxt
164 {
165    SchLcState  lcState;
166    uint8_t lcId;       
167    uint8_t priority;
168    uint8_t lcGroup;
169    uint8_t schReqId;
170    uint8_t pbr;        // prioritisedBitRate
171    uint8_t bsd;        // bucketSizeDuration
172 }SchUlLcCtxt;
173
174 typedef struct schUlCb
175 {
176    uint8_t     numUlLc;
177    SchUlLcCtxt ulLcCtxt[MAX_NUM_LC];
178 }SchUlCb;
179
180 typedef struct schUeCfgCb
181 {
182    uint16_t        cellId;
183    uint16_t        crnti;
184    bool macCellGrpCfgPres;
185    SchMacCellGrpCfg   macCellGrpCfg;
186    bool phyCellGrpCfgPres;
187    SchPhyCellGrpCfg   phyCellGrpCfg;
188    bool spCellCfgPres;
189    SchSpCellCfg       spCellCfg;
190    SchAmbrCfg         *ambrCfg;
191    SchModulationInfo  dlModInfo;
192    SchModulationInfo  ulModInfo;
193 }SchUeCfgCb;
194
195 /**
196  * @brief
197  * UE control block
198  */
199 typedef struct schUeCb
200 {
201    uint16_t   ueIdx;
202    uint16_t   crnti;
203    SchUeCfgCb ueCfg;
204    SchUeState state;
205    SchCellCb  *cellCb;
206    bool       srRcvd;
207    BsrInfo    bsrInfo[MAX_NUM_LOGICAL_CHANNEL_GROUPS];
208    SchUlCb    ulInfo;
209    SchDlCb    dlInfo;
210 }SchUeCb;
211
212 /**
213  * @brief
214  * Cell Control block per cell.
215  */
216 typedef struct schCellCb
217 {
218    uint16_t      cellId;                            /*!< Cell ID */
219    Inst          instIdx;                           /*!< Index of the scheduler instance */
220    Inst          macInst;                           /*!< Index of the MAC instance */
221    uint8_t       numSlots;                          /*!< Number of slots in current frame */
222    SlotIndInfo   slotInfo;                          /*!< SFN, Slot info being processed*/
223    SchDlSlotInfo **schDlSlotInfo;                   /*!< SCH resource allocations in DL */
224    SchUlSlotInfo **schUlSlotInfo;                   /*!< SCH resource allocations in UL */
225    SchCellCfg    cellCfg;                           /*!< Cell ocnfiguration */
226    bool          firstSsbTransmitted;
227    bool          firstSib1Transmitted;
228    uint8_t       ssbStartSymbArr[SCH_MAX_SSB_BEAM]; /*!<start symbol per SSB beam */
229    SchRaCb       raCb[MAX_NUM_UE];                  /*!< Rach Cb */
230    uint16_t      numActvUe;                         /*!<Number of active UEs */
231    uint32_t      actvUeBitMap;                      /*!<Bit map to find active UEs */
232    uint32_t      boIndBitMap;                       /*!<Bit map to indicate UEs that have recevied BO */
233    SchUeCb       ueCb[MAX_NUM_UE];                  /*!<Pointer to UE contexts of this cell */
234 #ifdef NR_TDD
235    uint8_t       numSlotsInPeriodicity;             /*!< number of slots in configured periodicity and SCS */
236    uint32_t      slotFrmtBitMap;                    /*!< 2 bits must be read together to determine D/U/S slots. 00-D, 01-U, 10-S */
237    uint32_t      symbFrmtBitMap;                    /*!< 2 bits must be read together to determine D/U/S symbols. 00-D, 01-U, 10-S */
238 #endif
239 }SchCellCb;
240
241 /**
242  * @brief
243  * Control block for sch
244  */
245 typedef struct schCb
246 {
247    TskInit       schInit;               /*!< Task Init info */
248    SchGenCb      genCfg;                /*!< General Config info */
249    CmTqCp        tmrTqCp;               /*!< Timer Task Queue Cntrl Point */
250    CmTqType      tmrTq[SCH_TQ_SIZE];    /*!< Timer Task Queue */
251    SchCellCb     *cells[MAX_NUM_CELL];  /* Array to store cellCb ptr */  
252 }SchCb;
253
254 /* Declaration for scheduler control blocks */
255 SchCb schCb[SCH_MAX_INST];
256
257 /* function declarations */
258 SchUeCb* schGetUeCb(SchCellCb *cellCb, uint16_t crnti);
259 void schInitUlSlot(SchUlSlotInfo *schUlSlotInfo);
260 void schInitDlSlot(SchDlSlotInfo *schDlSlotInfo);
261 uint8_t SchSendCfgCfm(Pst *pst, RgMngmt *cfm);
262 short int schActvTmr(Ent ent,Inst inst);
263 uint8_t schBroadcastAlloc(SchCellCb *cell, DlBrdcstAlloc *dlBrdcstAlloc,uint16_t slot);
264 uint8_t schProcessSlotInd(SlotIndInfo *slotInd, Inst inst);
265 uint8_t schUlResAlloc(SchCellCb *cell, Inst schInst);
266 uint8_t schDlRsrcAllocMsg4(DlMsgAlloc *msg4Alloc, SchCellCb *cell, uint16_t slot, bool ssbPresent, bool sib1Present);
267 uint16_t schCalcTbSize(uint32_t payLoadSize);
268 uint16_t schCalcNumPrb(uint16_t tbSize, uint16_t mcs, uint8_t numSymbols);
269 uint16_t schAllocPucchResource(SchCellCb *cell, uint16_t crnti, uint16_t slot);
270 uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst);
271 uint8_t schFillUlDci(SchUeCb *ueCb, SchPuschInfo puschInfo, DciInfo *dciInfo);
272 uint8_t schFillPuschAlloc(SchUeCb *ueCb, uint16_t pdcchSlot, uint32_t dataVol, SchPuschInfo *puschInfo);
273 uint8_t schDlRsrcAllocDlMsg(DlMsgAlloc *dlMsgAlloc, SchCellCb *cell, uint16_t crnti,
274    uint32_t *accumalatedSize, uint16_t slot);
275 uint16_t schAccumalateLcBoSize(SchCellCb *cell, uint16_t ueIdx);
276 uint8_t schFillRar(RarAlloc *rarAlloc, uint16_t raRnti, uint16_t pci, uint8_t offsetPointA, bool ssbPresent, bool sib1Present);
277
278 /**********************************************************************
279   End of file
280  **********************************************************************/