544e3250dfcae2201a1cb7d883bcc3d1c6276299
[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 #define SCH_MIB_TRANS 8  /* MIB transmission as per 38.331 is every 80 ms */
29 #define SCH_SIB1_TRANS 16 /* SIB1 transmission as per 38.331 is every 160 ms */
30 #define SCH_NUM_SC_PRB 12 /* number of SCs in a PRB */
31 #define SCH_MAX_SSB_BEAM 8 /* since we are supporting only SCS=15KHz and 30KHz */
32 #define SCH_SSB_NUM_SYMB 4
33 #define SCH_SSB_NUM_PRB 21 /* One extra PRB as buffer */
34 #define SCHED_DELTA 1
35 #define BO_DELTA 1
36 #define RAR_DELAY   2
37 #define MSG4_DELAY  1
38 #define PDSCH_START_RB 10
39 /* Considering pdsch region from 3 to 13, DMRS exclued.
40  * Overlapping of PDSCH DRMS and PDSCH not supported by Intel L1 */
41 #define NUM_PDSCH_SYMBOL 11
42 #define PUSCH_START_RB 15
43 #define PUCCH_NUM_PRB_FORMAT_0_1_4 1  /* number of PRBs in freq domain, spec 38.213 - 9.2.1 */
44 #define SI_RNTI 0xFFFF
45 #define P_RNTI  0xFFFE
46 #define DMRS_MAP_TYPE_A 1
47 #define NUM_DMRS_SYMBOLS 1
48 #define DMRS_ADDITIONAL_POS 0
49 #define SCH_DEFAULT_K1 1
50 #define SCH_TQ_SIZE 10
51 #define SSB_IDX_SUPPORTED 1
52
53 #define CRC_FAILED 0
54 #define CRC_PASSED 1
55
56 #define MAC_HDR_SIZE  3   /* 3 bytes of MAC Header */
57 #define UL_GRANT_SIZE 224
58
59 #define PRB_BITMAP_IDX_LEN 64
60 #define PRB_BITMAP_MAX_IDX ((MAX_NUM_RB + PRB_BITMAP_IDX_LEN-1) / PRB_BITMAP_IDX_LEN)
61
62 typedef struct schCellCb SchCellCb;
63 typedef struct schUeCb SchUeCb;
64
65 typedef enum
66 {
67    SCH_NUMEROLOGY_0,
68    SCH_NUMEROLOGY_1,
69    SCH_NUMEROLOGY_2,
70    SCH_NUMEROLOGY_3,
71    SCH_NUMEROLOGY_4
72 }SchNumerology;
73
74 typedef enum
75 {
76    SCH_UE_STATE_INACTIVE,
77    SCH_UE_STATE_ACTIVE
78 }SchUeState;
79
80 typedef enum
81 {
82    SCH_LC_STATE_INACTIVE,
83    SCH_LC_STATE_ACTIVE
84 }SchLcState;
85
86 typedef enum
87 {
88    WINDOW_YET_TO_START,
89    WITHIN_WINDOW,
90    WINDOW_EXPIRED
91 }RaRspWindowStatus;
92
93 typedef enum 
94 {
95    SEARCH,
96    CREATE,
97    DELETE
98 }ActionTypeLcLL;
99 /**
100  * @brief
101  * Structure holding LTE MAC's General Configuration information.
102  */
103 typedef struct schGenCb
104 {
105    uint8_t         tmrRes;           /*!< Timer resolution */
106    uint8_t         startCellId;      /*!< Starting Cell Id */
107 #ifdef LTE_ADV
108    bool            forceCntrlSrbBoOnPCel; /*!< value 1 means force scheduling
109                                             of RLC control BO and SRB BO on
110                                             PCell. val 0 means don't force*/
111    bool            isSCellActDeactAlgoEnable; /*!< TRUE will enable activation/deactivation algo at Schd */
112 #endif
113 }SchGenCb;
114
115 typedef struct freePrbBlock
116 {
117    uint16_t numFreePrb;
118    uint16_t startPrb;
119    uint16_t endPrb;
120 }FreePrbBlock;
121
122 /**
123  * @brief
124  * PRB allocations for a symbol within a slot
125  */
126 typedef struct schPrbAlloc
127 {
128    CmLListCp freePrbBlockList;           /*!< List of continuous blocks for available PRB */
129    uint64_t  prbBitMap[ MAX_SYMB_PER_SLOT][PRB_BITMAP_MAX_IDX];  /*!< BitMap to store the allocated PRBs */
130 }SchPrbAlloc;
131
132 /**
133  * @brief
134  * scheduler allocationsfor DL per cell.
135  */
136 typedef struct schDlSlotInfo
137 {
138    SchPrbAlloc  prbAlloc;                 /*!< PRB allocated/available in this slot */
139    bool         ssbPres;                  /*!< Flag to determine if SSB is present in this slot */
140    uint8_t      ssbIdxSupported;          /*!< Max SSB index */
141    SsbInfo      ssbInfo[MAX_SSB_IDX];     /*!< SSB info */
142    bool         sib1Pres;                 /*!< Flag to determine if SIB1 is present in this slot */
143    uint8_t      pdcchUe;                  /*!< UE for which PDCCH is scheduled in this slot */
144    uint8_t      pdschUe;                  /*!< UE for which PDSCH is scheduled in this slot */
145    RarAlloc     *rarAlloc[MAX_NUM_UE];    /*!< RAR allocation per UE*/
146    DlMsgAlloc   *dlMsgAlloc;              /*!< Dl msg allocation */
147 }SchDlSlotInfo;
148
149 typedef struct schRaCb
150 {
151    bool      msg4recvd;
152    DlMsgInfo dlMsgInfo;
153    uint16_t  tcrnti;
154 }SchRaCb;
155
156 /**
157  * @brief
158  * scheduler allocationsfor UL per cell.
159  */
160 typedef struct schUlSlotInfo
161 {
162    SchPrbAlloc  prbAlloc;         /*!< PRB allocated/available per symbol */
163    uint8_t      puschCurrentPrb;  /*!< Current PRB for PUSCH allocation */
164    bool         puschPres;        /*!< PUSCH presence field */
165    SchPuschInfo *schPuschInfo;    /*!< PUSCH info */
166    bool         pucchPres;        /*!< PUCCH presence field */
167    SchPucchInfo schPucchInfo;     /*!< PUCCH info */
168    uint8_t      pucchUe;          /*!< Store UE id for which PUCCH is scheduled */
169    uint8_t      puschUe;          /*!< Store UE id for which PUSCH is scheduled */
170 }SchUlSlotInfo;
171
172 /**
173 @brief
174 * BSR info per slot per UE.
175 */
176 typedef struct bsrInfo
177 {
178    uint8_t    priority;  /* CG priority */
179    uint32_t   dataVol;   /* Data volume requested in bytes */
180 }BsrInfo;
181
182 typedef struct schLcCtxt
183 {
184    uint8_t lcId;     // logical Channel ID
185    uint8_t lcp;      // logical Channel Prioritization
186    SchLcState lcState;
187    uint32_t bo;
188    uint16_t   pduSessionId; /*Pdu Session Id*/
189    Snssai  *snssai;      /*S-NSSAI assoc with LCID*/
190    bool isDedicated;     /*Flag containing Dedicated S-NSSAI or not*/
191 }SchDlLcCtxt;
192
193 typedef struct schDlCb
194 {
195    SchDlLcCtxt   dlLcCtxt[MAX_NUM_LC];
196 }SchDlCb;
197
198 typedef struct schUlLcCtxt
199 {
200    SchLcState  lcState;
201    uint8_t lcId;       
202    uint8_t priority;
203    uint8_t lcGroup;
204    uint8_t schReqId;
205    uint8_t pbr;        // prioritisedBitRate
206    uint8_t bsd;        // bucketSizeDuration
207    uint16_t   pduSessionId; /*Pdu Session Id*/
208    Snssai  *snssai;      /*S-NSSAI assoc with LCID*/
209    bool isDedicated;     /*Flag containing Dedicated S-NSSAI or not*/
210 }SchUlLcCtxt;
211
212 typedef struct schUlCb
213 {
214    SchUlLcCtxt ulLcCtxt[MAX_NUM_LC];
215 }SchUlCb;
216
217 typedef struct schUeCfgCb
218 {
219    uint16_t        cellId;
220    uint16_t        crnti;
221    bool macCellGrpCfgPres;
222    SchMacCellGrpCfg   macCellGrpCfg;
223    bool phyCellGrpCfgPres;
224    SchPhyCellGrpCfg   phyCellGrpCfg;
225    bool spCellCfgPres;
226    SchSpCellCfg       spCellCfg;
227    SchAmbrCfg         *ambrCfg;
228    SchModulationInfo  dlModInfo;
229    SchModulationInfo  ulModInfo;
230 }SchUeCfgCb;
231
232 /*Following structures to keep record and estimations of PRB allocated for each
233  * LC taking into consideration the RRM policies*/
234 typedef struct lcInfo
235 {
236    uint8_t  lcId;     /*LCID for which BO are getting recorded*/
237    uint32_t reqBO;    /*Size of the BO requested/to be allocated for this LC*/
238    uint32_t allocBO;  /*TBS/BO Size which is actually allocated*/
239    uint8_t  allocPRB; /*PRB count which is allocated based on RRM policy/FreePRB*/
240 }LcInfo;
241
242 typedef struct dedicatedLCInfo
243 {
244    CmLListCp dedLcList;             /*Linklist of LC assoc with RRMPolicyMemberList*/
245    uint16_t      rsvdDedicatedPRB; /*Number of PRB reserved for this Dedicated S-NSSAI*/
246 }DedicatedLCInfo;
247
248 typedef struct schLcPrbEstimate
249 {
250    /* TODO: For Multiple RRMPolicies, Make DedicatedLcInfo as array/Double Pointer 
251     * and have separate DedLCInfo for each RRMPolcyMemberList*/
252    /* Dedicated LC List will be allocated, if any available*/
253    DedicatedLCInfo *dedLcInfo;  /*Contain LCInfo per RRMPolicy*/
254
255    CmLListCp defLcList; /*Linklist of LC assoc with Default S-NSSAI(s)*/
256
257    /* SharedPRB number can be used by any LC.
258     * Need to calculate in every Slot based on PRB availability*/
259    uint16_t sharedNumPrb;  
260 }SchLcPrbEstimate;
261
262 /**
263  * @brief
264  * UE control block
265  */
266 typedef struct schUeCb
267 {
268    uint16_t   ueIdx;
269    uint16_t   crnti;
270    SchUeCfgCb ueCfg;
271    SchUeState state;
272    SchCellCb  *cellCb;
273    bool       srRcvd;
274    BsrInfo    bsrInfo[MAX_NUM_LOGICAL_CHANNEL_GROUPS];
275    SchUlCb    ulInfo;
276    SchDlCb    dlInfo;
277    SchLcPrbEstimate dlLcPrbEst; /*DL PRB Alloc Estimate among different LC*/
278    SchLcPrbEstimate ulLcPrbEst; /*UL PRB Alloc Estimate among different LC*/
279 }SchUeCb;
280
281 /**
282  * @brief
283  * RA Request Info
284  */
285 typedef struct schRaReq
286 {
287    uint32_t        raRnti;
288    RachIndInfo     *rachInd;
289    SlotTimingInfo  winStartTime;
290    SlotTimingInfo  winEndTime;
291 }SchRaReq;
292
293 /**
294  * @brief
295  * Cell Control block per cell.
296  */
297 typedef struct schCellCb
298 {
299    uint16_t      cellId;                            /*!< Cell ID */
300    Inst          instIdx;                           /*!< Index of the scheduler instance */
301    Inst          macInst;                           /*!< Index of the MAC instance */
302    uint8_t       numSlots;                          /*!< Number of slots in current frame */
303    SlotTimingInfo   slotInfo;                          /*!< SFN, Slot info being processed*/
304    SchDlSlotInfo **schDlSlotInfo;                   /*!< SCH resource allocations in DL */
305    SchUlSlotInfo **schUlSlotInfo;                   /*!< SCH resource allocations in UL */
306    SchCellCfg    cellCfg;                           /*!< Cell ocnfiguration */
307    bool          firstSsbTransmitted;
308    bool          firstSib1Transmitted;
309    uint8_t       ssbStartSymbArr[SCH_MAX_SSB_BEAM]; /*!<start symbol per SSB beam */
310    SchRaReq      *raReq[MAX_NUM_UE];                /*!< Pending RA request */
311    SchRaCb       raCb[MAX_NUM_UE];                  /*!< RA Cb */
312    uint16_t      numActvUe;                         /*!<Number of active UEs */
313    uint32_t      actvUeBitMap;                      /*!<Bit map to find active UEs */
314    uint32_t      boIndBitMap;                       /*!<Bit map to indicate UEs that have recevied BO */
315    SchUeCb       ueCb[MAX_NUM_UE];                  /*!<Pointer to UE contexts of this cell */
316    CmLListCp     ueToBeScheduled;                   /*!<Linked list to store UEs pending to be scheduled, */
317 #ifdef NR_TDD
318    uint8_t       numSlotsInPeriodicity;             /*!< number of slots in configured periodicity and SCS */
319    uint32_t      slotFrmtBitMap;                    /*!< 2 bits must be read together to determine D/U/S slots. 00-D, 01-U, 10-S */
320    uint32_t      symbFrmtBitMap;                    /*!< 2 bits must be read together to determine D/U/S symbols. 00-D, 01-U, 10-S */
321 #endif
322 }SchCellCb;
323
324 /**
325  * @brief
326  * Control block for sch
327  */
328 typedef struct schCb
329 {
330    TskInit       schInit;               /*!< Task Init info */
331    SchGenCb      genCfg;                /*!< General Config info */
332    CmTqCp        tmrTqCp;               /*!< Timer Task Queue Cntrl Point */
333    CmTqType      tmrTq[SCH_TQ_SIZE];    /*!< Timer Task Queue */
334    SchCellCb     *cells[MAX_NUM_CELL];  /* Array to store cellCb ptr */  
335 }SchCb;
336
337 /* Declaration for scheduler control blocks */
338 SchCb schCb[SCH_MAX_INST];
339
340 /* function declarations */
341 short int schActvTmr(Ent ent,Inst inst);
342
343 /* Configuration related function declarations */
344 void schInitUlSlot(SchUlSlotInfo *schUlSlotInfo);
345 void schInitDlSlot(SchDlSlotInfo *schDlSlotInfo);
346 void BuildK0K1Table(SchCellCb *cell, SchK0K1TimingInfoTbl *k0K1InfoTbl, bool pdschCfgCmnPres, \
347    SchPdschCfgCmn pdschCmnCfg,SchPdschConfig pdschDedCfg, uint8_t ulAckListCount, uint8_t *UlAckTbl);
348 void BuildK2InfoTable(SchCellCb *cell, SchPuschTimeDomRsrcAlloc timeDomRsrcAllocList[], \
349    uint16_t puschSymTblSize, SchK2TimingInfoTbl *msg3K2InfoTbl, SchK2TimingInfoTbl *k2InfoTbl);
350 uint8_t SchSendCfgCfm(Pst *pst, RgMngmt *cfm);
351 SchUeCb* schGetUeCb(SchCellCb *cellCb, uint16_t crnti);
352 uint8_t addUeToBeScheduled(SchCellCb *cell, uint8_t ueId);
353
354 /* Incoming message handler function declarations */
355 uint8_t schProcessSlotInd(SlotTimingInfo *slotInd, Inst inst);
356 uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst);
357
358 /* DL scheduling related function declarations */
359 PduTxOccsaion schCheckSsbOcc(SchCellCb *cell, SlotTimingInfo slotTime);
360 PduTxOccsaion schCheckSib1Occ(SchCellCb *cell, SlotTimingInfo slotTime);
361 uint8_t schBroadcastSsbAlloc(SchCellCb *cell, SlotTimingInfo slotTime, DlBrdcstAlloc *dlBrdcstAlloc);
362 uint8_t schBroadcastSib1Alloc(SchCellCb *cell, SlotTimingInfo slotTime, DlBrdcstAlloc *dlBrdcstAlloc);
363 bool schProcessRaReq(SchCellCb *cellCb, SlotTimingInfo currTime, uint8_t ueId);
364 uint8_t schProcessMsg4Req(SchCellCb *cell, SlotTimingInfo currTime);
365 uint8_t schFillRar(SchCellCb *cell, SlotTimingInfo rarTime, uint16_t ueIdx, RarAlloc *rarAlloc, uint8_t k0Index);
366 uint8_t schDlRsrcAllocDlMsg(SchCellCb *cell, SlotTimingInfo slotTime, uint16_t crnti,\
367    uint32_t tbSize, DlMsgAlloc *dlMsgAlloc, uint16_t startPRB);
368 uint8_t schDlRsrcAllocMsg4(SchCellCb *cell, SlotTimingInfo msg4Time, uint8_t ueIdx, DlMsgAlloc *msg4Alloc, uint8_t k0Idx);
369 uint16_t schAccumalateLcBoSize(SchCellCb *cell, uint16_t ueIdx);
370 uint8_t allocatePrbDl(SchCellCb *cell, SlotTimingInfo slotTime, uint8_t startSymbol, uint8_t symbolLength, \
371    uint16_t *startPrb, uint16_t numPrb);
372 uint16_t searchLargestFreeBlockDL(SchCellCb *cell, SlotTimingInfo slotTime,uint16_t *startPrb);
373 void updateGrantSizeForBoRpt(CmLListCp *lcLL, DlMsgAlloc *dlMsgAlloc, uint32_t *accumalatedBOSize);
374
375 /* UL scheduling related function declarations */
376 uint8_t schUlResAlloc(SchCellCb *cell, Inst schInst);
377 bool schCheckPrachOcc(SchCellCb *cell, SlotTimingInfo prachOccasionTimingInfo);
378 uint8_t schCalcPrachNumRb(SchCellCb *cell);
379 void schPrachResAlloc(SchCellCb *cell, UlSchedInfo *ulSchedInfo, SlotTimingInfo prachOccasionTimingInfo);
380 uint16_t schAllocPucchResource(SchCellCb *cell, SlotTimingInfo pucchTime, uint16_t crnti);
381 uint8_t schFillUlDci(SchUeCb *ueCb, SchPuschInfo puschInfo, DciInfo *dciInfo);
382 uint8_t schFillPuschAlloc(SchUeCb *ueCb, SlotTimingInfo pdcchSlotTime, uint32_t dataVol, SchPuschInfo *puschInfo);
383 uint8_t allocatePrbUl(SchCellCb *cell, SlotTimingInfo slotTime, uint8_t startSymbol, uint8_t symbolLength, \
384    uint16_t *startPrb, uint16_t numPrb);
385
386 /*Generic Functions*/
387 LcInfo* handleLcLList(CmLListCp *lcLL, uint8_t lcId, ActionTypeLcLL action);
388 void prbAllocUsingRRMPolicy(CmLListCp *lcLL, bool dedicatedPRB, uint16_t mcsIdx,uint8_t numSymbols,\
389                              uint16_t *sharedPRB, uint16_t *reservedPRB, bool *isTxPayloadLenAdded);
390
391 /**********************************************************************
392   End of file
393  **********************************************************************/