1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2017-2019] [Radisys] #
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 #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 *******************************************************************************/
19 /************************************************************************
25 Desc: C source code for scheduler fucntions
29 **********************************************************************/
32 @brief This file implements the schedulers main access to MAC layer code.
34 #include "common_def.h"
35 #include "du_app_mac_inf.h"
40 #include "rg_sch_inf.h"
43 #include "tfu.x" /* TFU types */
44 #include "lrg.x" /* layer management typedefs for MAC */
45 #include "rgr.x" /* layer management typedefs for MAC */
46 #include "rg_sch_inf.x" /* typedefs for Scheduler */
47 #include "mac_sch_interface.h"
49 #include "sch_utils.h"
51 extern SchCb schCb[SCH_MAX_INST];
52 void SchFillCfmPst(Pst *reqPst,Pst *cfmPst,RgMngmt *cfm);
54 SchCellCfgCfmFunc SchCellCfgCfmOpts[] =
56 packSchCellCfgCfm, /* LC */
57 MacProcSchCellCfgCfm, /* TC */
58 packSchCellCfgCfm /* LWLC */
63 * @brief Task Initiation function.
67 * Function : schActvInit
69 * This function is supplied as one of parameters during MAC's
70 * task registration. MAC will invoke this function once, after
71 * it creates and attaches this TAPA Task to a system task.
73 * @param[in] Ent Entity, the entity ID of this task.
74 * @param[in] Inst Inst, the instance ID of this task.
75 * @param[in] Region Region, the region ID registered for memory
77 * @param[in] Reason Reason.
83 Ent entity, /* entity */
84 Inst instId, /* instance */
85 Region region, /* region */
86 Reason reason /* reason */
89 Inst inst = (instId - SCH_INST_START);
91 /* Initialize the MAC TskInit structure to zero */
92 cmMemset ((uint8_t *)&schCb[inst], 0, sizeof(schCb));
94 /* Initialize the MAC TskInit with received values */
95 schCb[inst].schInit.ent = entity;
96 schCb[inst].schInit.inst = inst;
97 schCb[inst].schInit.region = region;
98 schCb[inst].schInit.pool = 0;
99 schCb[inst].schInit.reason = reason;
100 schCb[inst].schInit.cfgDone = FALSE;
101 schCb[inst].schInit.acnt = FALSE;
102 schCb[inst].schInit.usta = FALSE;
103 schCb[inst].schInit.trc = FALSE;
104 schCb[inst].schInit.procId = SFndProcId();
110 * @brief Scheduler instance Configuration Handler.
114 * Function : SchInstCfg
116 * This function in called by HandleSchGenCfgReq(). It handles the
117 * general configurations of the scheduler instance. Returns
118 * reason for success/failure of this function.
120 * @param[in] RgCfg *cfg, the Configuaration information
122 * -# LCM_REASON_NOT_APPL
123 * -# LCM_REASON_INVALID_MSGTYPE
124 * -# LCM_REASON_MEM_NOAVAIL
126 PUBLIC U16 SchInstCfg
128 RgCfg *cfg, /* Configuaration information */
132 uint16_t ret = LCM_REASON_NOT_APPL;
133 Inst inst = (dInst - SCH_INST_START);
135 printf("\nEntered SchInstCfg()");
136 /* Check if Instance Configuration is done already */
137 if (schCb[inst].schInit.cfgDone == TRUE)
139 RETVALUE(LCM_REASON_INVALID_MSGTYPE);
141 /* Update the Pst structure for LM interface */
142 cmMemcpy((U8 *)&schCb[inst].schInit.lmPst,
143 (U8 *)&cfg->s.schInstCfg.genCfg.lmPst,
146 schCb[inst].schInit.inst = inst;
147 schCb[inst].schInit.lmPst.srcProcId = schCb[inst].schInit.procId;
148 schCb[inst].schInit.lmPst.srcEnt = schCb[inst].schInit.ent;
149 schCb[inst].schInit.lmPst.srcInst = schCb[inst].schInit.inst +
151 schCb[inst].schInit.lmPst.event = EVTNONE;
153 schCb[inst].schInit.region = cfg->s.schInstCfg.genCfg.mem.region;
154 schCb[inst].schInit.pool = cfg->s.schInstCfg.genCfg.mem.pool;
155 schCb[inst].genCfg.tmrRes = cfg->s.schInstCfg.genCfg.tmrRes;
157 schCb[inst].genCfg.forceCntrlSrbBoOnPCel = cfg->s.schInstCfg.genCfg.forceCntrlSrbBoOnPCel;
158 schCb[inst].genCfg.isSCellActDeactAlgoEnable = cfg->s.schInstCfg.genCfg.isSCellActDeactAlgoEnable;
160 schCb[inst].genCfg.startCellId = cfg->s.schInstCfg.genCfg.startCellId;
162 /* Initialzie the timer queue */
163 cmMemset((U8 *)&schCb[inst].tmrTq, 0, sizeof(CmTqType)*RGSCH_TQ_SIZE);
164 /* Initialize the timer control point */
165 cmMemset((U8 *)&schCb[inst].tmrTqCp, 0, sizeof(CmTqCp));
166 schCb[inst].tmrTqCp.tmrLen = RGSCH_TQ_SIZE;
168 /* SS_MT_TMR needs to be enabled as schActvTmr needs instance information */
169 /* Timer Registration request to SSI */
170 if (SRegTmrMt(schCb[inst].schInit.ent, dInst,
171 (S16)schCb[inst].genCfg.tmrRes, schActvTmr) != ROK)
173 RLOG_ARG0(L_ERROR,DBG_INSTID,inst, "SchInstCfg(): Failed to "
175 RETVALUE(LCM_REASON_MEM_NOAVAIL);
178 /* Set Config done in TskInit */
179 schCb[inst].schInit.cfgDone = TRUE;
180 printf("\nScheduler gen config done");
186 * @brief Layer Manager Configuration request handler.
190 * Function : HandleSchGenCfgReq
192 * This function handles the configuration
193 * request received at scheduler instance from the Layer Manager.
194 * -# Based on the cfg->hdr.elmId.elmnt value it invokes one of the
195 * functions rgHdlGenCfg() or rgHdlSapCfg().
196 * -# Invokes RgMiLrgSchCfgCfm() to send back the confirmation to the LM.
198 * @param[in] Pst *pst, the post structure
199 * @param[in] RgMngmt *cfg, the configuration parameter's structure
203 int HandleSchGenCfgReq
205 Pst *pst, /* post structure */
206 RgMngmt *cfg /* config structure */
209 uint16_t ret = LCM_PRIM_OK;
210 uint16_t reason = LCM_REASON_NOT_APPL;
214 if(pst->dstInst < SCH_INST_START)
216 DU_LOG("\nInvalid inst ID");
217 DU_LOG("\nHandleSchGenCfgReq(): "
218 "pst->dstInst=%d SCH_INST_START=%d", pst->dstInst,SCH_INST_START);
221 printf("\nReceived scheduler gen config");
222 /* Fill the post structure for sending the confirmation */
223 memset(&cfmPst, 0 , sizeof(Pst));
224 SchFillCfmPst(pst, &cfmPst, cfg);
226 cmMemset((U8 *)&cfm, 0, sizeof(RgMngmt));
233 cfm.hdr.elmId.elmnt = cfg->hdr.elmId.elmnt;
234 switch(cfg->hdr.elmId.elmnt)
237 reason = SchInstCfg(&cfg->t.cfg,pst->dstInst );
241 reason = LCM_REASON_INVALID_ELMNT;
242 DU_LOG("\nInvalid Elmnt=%d", cfg->hdr.elmId.elmnt);
246 if (reason != LCM_REASON_NOT_APPL)
251 cfm.cfm.status = ret;
252 cfm.cfm.reason = reason;
254 SchSendCfgCfm(&cfmPst, &cfm);
255 /* SPutSBuf(pst->region, pst->pool, (Data *)cfg, sizeof(RgMngmt)); */
258 }/*-- HandleSchGenCfgReq --*/
261 * @brief slot indication from MAC to SCH.
265 * Function : macSchSlotInd
267 * This API is invoked by PHY to indicate slot indication to Scheduler for
270 * @param[in] Pst *pst
271 * @param[in] SlotIndInfo *slotInd
282 Inst inst = pst->dstInst-SCH_INST_START;
284 /* Now call the TOM (Tfu ownership module) primitive to process further */
285 schProcessSlotInd(slotInd, inst);
288 } /* macSchSlotInd */
290 /*******************************************************************
292 * @brief Processes Rach indication from MAC
296 * Function : macSchRachInd
299 * Processes Rach indication from MAC
302 * @return ROK - success
305 * ****************************************************************/
306 int macSchRachInd(Pst *pst, RachIndInfo *rachInd)
308 Inst inst = pst->dstInst-SCH_INST_START;
309 DU_LOG("\nSCH : Received Rach indication");
310 schProcessRachInd(rachInd, inst);
314 /*******************************************************************
316 * @brief Processes CRC indication from MAC
320 * Function : macSchCrcInd
323 * Processes CRC indication from MAC
325 * @params[in] Post structure
327 * @return ROK - success
330 * ****************************************************************/
331 int macSchCrcInd(Pst *pst, CrcIndInfo *crcInd)
333 switch(crcInd->crcInd[0])
336 DU_LOG("\nSCH : Received CRC indication. CRC Status [FAILURE]");
339 DU_LOG("\nSCH : Received CRC indication. CRC Status [PASS]");
342 DU_LOG("\nSCH : Invalid CRC state %d", crcInd->crcInd[0]);
350 * @brief inti cellCb based on cellCfg
354 * Function : InitSchCellCb
356 * This API is invoked after receiving schCellCfg
358 * @param[in] schCellCb *cell
359 * @param[in] SchCellCfg *schCellCfg
364 int InitSchCellCb(Inst inst, SchCellCfg *schCellCfg)
367 SCH_ALLOC(cell, sizeof(SchCellCb));
370 DU_LOG("\nMemory allocation failed in InitSchCellCb");
374 cell->cellId = schCellCfg->cellId;
375 cell->instIdx = inst;
376 switch(schCellCfg->ssbSchCfg.scsCommon)
380 cell->numSlots = SCH_NUM_SLOTS;
384 DU_LOG("\nSCS %d not supported", schCellCfg->ssbSchCfg.scsCommon);
387 for(uint8_t idx=0; idx<SCH_NUM_SLOTS; idx++)
389 SchDlSlotInfo *schDlSlotInfo;
390 SchUlSlotInfo *schUlSlotInfo;
393 SCH_ALLOC(schDlSlotInfo, sizeof(SchDlSlotInfo));
396 DU_LOG("\nMemory allocation failed in InitSchCellCb");
401 SCH_ALLOC(schUlSlotInfo, sizeof(SchUlSlotInfo));
404 DU_LOG("\nMemory allocation failed in InitSchCellCb");
408 memset(schDlSlotInfo, 0, sizeof(SchDlSlotInfo));
409 memset(schUlSlotInfo, 0, sizeof(SchUlSlotInfo));
411 schDlSlotInfo->totalPrb = schUlSlotInfo->totalPrb = MAX_NUM_RB;
413 for(uint8_t itr=0; itr<SCH_SYMBOL_PER_SLOT; itr++)
415 schDlSlotInfo->assignedPrb[itr] = 0;
416 schUlSlotInfo->assignedPrb[itr] = 0;
418 schUlSlotInfo->schPuschInfo = NULLP;
420 for(uint8_t itr=0; itr<MAX_SSB_IDX; itr++)
422 memset(&schDlSlotInfo->ssbInfo[itr], 0, sizeof(SsbInfo));
425 cell->schDlSlotInfo[idx] = schDlSlotInfo;
426 cell->schUlSlotInfo[idx] = schUlSlotInfo;
429 schCb[inst].cells[inst] = cell;
431 DU_LOG("\nCell init completed for cellId:%d", cell->cellId);
438 SchSib1Cfg *sib1SchCfg,
443 uint8_t coreset0Idx = 0;
444 uint8_t searchSpace0Idx = 0;
445 //uint8_t ssbMuxPattern = 0;
447 uint8_t numSymbols = 0;
450 //uint8_t numSearchSpacePerSlot = 0;
452 uint8_t firstSymbol = 0; /* need to calculate using formula mentioned in 38.213 */
453 uint8_t slotIndex = 0;
454 uint8_t FreqDomainResource[6] = {0};
456 uint8_t numPdschSymbols = 12; /* considering pdsch region from 2 to 13 */
458 PdcchCfg *pdcch = &(sib1SchCfg->sib1PdcchCfg);
459 PdschCfg *pdsch = &(sib1SchCfg->sib1PdschCfg);
460 BwpCfg *bwp = &(sib1SchCfg->bwp);
462 coreset0Idx = sib1SchCfg->coresetZeroIndex;
463 searchSpace0Idx = sib1SchCfg->searchSpaceZeroIndex;
465 /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
466 //ssbMuxPattern = coresetIdxTable[coreset0Idx][0];
467 numRbs = coresetIdxTable[coreset0Idx][1];
468 numSymbols = coresetIdxTable[coreset0Idx][2];
469 offset = coresetIdxTable[coreset0Idx][3];
471 /* derive the search space params from table 13-11 spec 38.213 */
472 oValue = searchSpaceIdxTable[searchSpace0Idx][0];
473 //numSearchSpacePerSlot = searchSpaceIdxTable[searchSpace0Idx][1];
474 mValue = searchSpaceIdxTable[searchSpace0Idx][2];
475 firstSymbol = searchSpaceIdxTable[searchSpace0Idx][3];
477 /* calculate the n0, need to add the formulae, as of now the value is 0
478 * Need to add the even and odd values of i during configuration
479 * [(O . 2^u + i . M ) ] mod numSlotsPerSubframe
480 * assuming u = 0, i = 0, numSlotsPerSubframe = 10
481 * Also, from this configuration, coreset0 is only on even subframe */
482 slotIndex = ((oValue * 1) + (0 * mValue)) % 10;
483 sib1SchCfg->n0 = slotIndex;
485 /* calculate the PRBs */
486 schAllocFreqDomRscType0(((offsetPointA-offset)/6), (numRbs/6), FreqDomainResource);
489 bwp->freqAlloc.numPrb = MAX_NUM_RB; /* whole of BW */
490 bwp->freqAlloc.startPrb = 0;
491 bwp->subcarrierSpacing = 0; /* 15Khz */
492 bwp->cyclicPrefix = 0; /* normal */
494 /* fill the PDCCH PDU */
495 pdcch->coreset0Cfg.coreSet0Size = numRbs;
496 pdcch->coreset0Cfg.startSymbolIndex = firstSymbol;
497 pdcch->coreset0Cfg.durationSymbols = numSymbols;
498 memcpy(pdcch->coreset0Cfg.freqDomainResource,FreqDomainResource,6);
499 pdcch->coreset0Cfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
500 pdcch->coreset0Cfg.regBundleSize = 6; /* spec-38.211 sec 7.3.2.2 */
501 pdcch->coreset0Cfg.interleaverSize = 2; /* spec-38.211 sec 7.3.2.2 */
502 pdcch->coreset0Cfg.coreSetType = 0;
503 pdcch->coreset0Cfg.shiftIndex = pci;
504 pdcch->coreset0Cfg.precoderGranularity = 0; /* sameAsRegBundle */
506 pdcch->dci.rnti = SI_RNTI;
507 pdcch->dci.scramblingId = pci;
508 pdcch->dci.scramblingRnti = 0;
509 pdcch->dci.cceIndex = 0;
510 pdcch->dci.aggregLevel = 4;
511 pdcch->dci.beamPdcchInfo.numPrgs = 1;
512 pdcch->dci.beamPdcchInfo.prgSize = 1;
513 pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
514 pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
515 pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
516 pdcch->dci.txPdcchPower.powerValue = 0;
517 pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
518 /* Storing pdschCfg pointer here. Required to access pdsch config while
519 fillig up pdcch pdu */
520 pdcch->dci.pdschCfg = pdsch;
522 /* fill the PDSCH PDU */
524 pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
525 pdsch->rnti = 0xFFFF; /* SI-RNTI */
527 pdsch->numCodewords = 1;
528 for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
530 pdsch->codeword[cwCount].targetCodeRate = 308;
531 pdsch->codeword[cwCount].qamModOrder = 2;
532 pdsch->codeword[cwCount].mcsIndex = sib1SchCfg->sib1Mcs;
533 pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
534 pdsch->codeword[cwCount].rvIndex = 0;
535 tbSize = schCalcTbSize(sib1SchCfg->sib1PduLen);
536 pdsch->codeword[cwCount].tbSize = tbSize;
538 pdsch->dataScramblingId = pci;
539 pdsch->numLayers = 1;
540 pdsch->transmissionScheme = 0;
542 pdsch->dmrs.dlDmrsSymbPos = 2;
543 pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
544 pdsch->dmrs.dlDmrsScramblingId = pci;
545 pdsch->dmrs.scid = 0;
546 pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
547 pdsch->dmrs.dmrsPorts = 0;
548 pdsch->dmrs.mappingType = DMRS_MAP_TYPE_A; /* Type-A */
549 pdsch->dmrs.nrOfDmrsSymbols = NUM_DMRS_SYMBOLS;
550 pdsch->dmrs.dmrsAddPos = DMRS_ADDITIONAL_POS;
552 pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
553 pdsch->pdschFreqAlloc.freqAlloc.startPrb = offset + SCH_SSB_NUM_PRB; /* the RB numbering starts from coreset0,
554 and PDSCH is always above SSB */
555 pdsch->pdschFreqAlloc.freqAlloc.numPrb = schCalcNumPrb(tbSize,sib1SchCfg->sib1Mcs,numPdschSymbols);
556 pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
557 pdsch->pdschTimeAlloc.rowIndex = 1;
558 pdsch->pdschTimeAlloc.timeAlloc.startSymb = 2; /* spec-38.214, Table 5.1.2.1-1 */
559 pdsch->pdschTimeAlloc.timeAlloc.numSymb = numPdschSymbols;
560 pdsch->beamPdschInfo.numPrgs = 1;
561 pdsch->beamPdschInfo.prgSize = 1;
562 pdsch->beamPdschInfo.digBfInterfaces = 0;
563 pdsch->beamPdschInfo.prg[0].pmIdx = 0;
564 pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
565 pdsch->txPdschPower.powerControlOffset = 0;
566 pdsch->txPdschPower.powerControlOffsetSS = 0;
571 * @brief Fill SSB start symbol
575 * Function : fillSsbStartSymb
577 * This API stores SSB start index per beam
579 * @param[in] SchCellCb *cellCb
584 void fillSsbStartSymb(SchCellCb *cellCb)
588 scs = cellCb->cellCfg.ssbSchCfg.scsCommon;
589 uint8_t ssbStartSymbArr[SCH_MAX_SSB_BEAM];
591 memset(ssbStartSymbArr, 0, sizeof(SCH_MAX_SSB_BEAM));
592 /* Determine value of "n" based on Section 4.1 of 3GPP TS 38.213 */
598 cnt = 2;/* n = 0, 1 for SCS = 15KHz */
599 for(uint8_t idx=0; idx<cnt; idx++)
601 /* start symbol determined using {2, 8} + 14n */
602 ssbStartSymbArr[symbIdx++] = 2 + SCH_SYMBOL_PER_SLOT*idx;
603 ssbStartSymbArr[symbIdx++] = 8 + SCH_SYMBOL_PER_SLOT*idx;
608 DU_LOG("\nSCS %d is currently not supported", scs);
610 memset(cellCb->ssbStartSymbArr, 0, sizeof(SCH_MAX_SSB_BEAM));
611 memcpy(cellCb->ssbStartSymbArr, ssbStartSymbArr, SCH_MAX_SSB_BEAM);
616 * @brief cell config from MAC to SCH.
620 * Function : macSchCellCfgReq
622 * This API is invoked by MAC to send cell config to SCH
624 * @param[in] Pst *pst
625 * @param[in] SchCellCfg *schCellCfg
633 SchCellCfg *schCellCfg
638 SchCellCfgCfm schCellCfgCfm;
640 Inst inst = pst->dstInst-1;
642 InitSchCellCb(inst, schCellCfg);
643 cellCb = schCb[inst].cells[inst]; //cells is of MAX_CELLS, why inst
644 cellCb->macInst = pst->srcInst;
646 /* derive the SIB1 config parameters */
649 &(schCellCfg->sib1SchCfg),
650 schCellCfg->phyCellId,
651 schCellCfg->ssbSchCfg.ssbOffsetPointA);
652 memcpy(&cellCb->cellCfg, schCellCfg, sizeof(SchCellCfg));
654 memset(&rspPst, 0, sizeof(Pst));
655 SCH_FILL_RSP_PST(rspPst, inst);
656 rspPst.event = EVENT_SCH_CELL_CFG_CFM;
657 schCellCfgCfm.rsp = RSP_OK;
659 ret = (*SchCellCfgCfmOpts[rspPst.selector])(&rspPst, &schCellCfgCfm);
665 /*******************************************************************
667 * @brief Processes DL RLC BO info from MAC
671 * Function : macSchDlRlcBoInfo
674 * Processes DL RLC BO info from MAC
677 * @return ROK - success
680 * ****************************************************************/
681 uint8_t macSchDlRlcBoInfo(Pst *pst, DlRlcBOInfo *dlBoInfo)
684 Inst inst = pst->dstInst-SCH_INST_START;
685 DU_LOG("\nSCH : Received RLC BO Status indication");
687 SchCellCb *cell = schCb[inst].cells[inst];
688 SchDlSlotInfo *schDlSlotInfo = \
689 cell->schDlSlotInfo[(cell->slotInfo.slot + SCHED_DELTA + PHY_DELTA + MSG4_DELAY) % SCH_NUM_SLOTS];
691 for(lcIdx = 0; lcIdx < dlBoInfo->numLc; lcIdx++)
693 if(dlBoInfo->boInfo[lcIdx].lcId == CCCH_LCID)
695 SCH_ALLOC(schDlSlotInfo->msg4Info, sizeof(Msg4Info));
696 if(!schDlSlotInfo->msg4Info)
698 DU_LOG("\nSCH : Memory allocation failed for msg4Info");
699 schDlSlotInfo = NULL;
702 schDlSlotInfo->msg4Info->crnti = dlBoInfo->crnti;
703 schDlSlotInfo->msg4Info->ndi = 1;
704 schDlSlotInfo->msg4Info->harqProcNum = 0;
705 schDlSlotInfo->msg4Info->dlAssignIdx = 0;
706 schDlSlotInfo->msg4Info->pucchTpc = 0;
707 schDlSlotInfo->msg4Info->pucchResInd = 0;
708 schDlSlotInfo->msg4Info->harqFeedbackInd = 0;
709 schDlSlotInfo->msg4Info->dciFormatId = 1;
716 /**********************************************************************
718 **********************************************************************/