[Epic-ID: ODUHIGH-405][Task-ID: ODUHIGH-423]Filled ReconfigurationWithSync IE,
[o-du/l2.git] / src / 5gnrmac / mac_rach.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 /* header include files (.h) */
19 #include "common_def.h"
20 #include "lrg.h"
21 #include "lrg.x"
22 #include "du_app_mac_inf.h"
23 #include "mac_sch_interface.h"
24 #include "lwr_mac_upr_inf.h"
25 #include "mac.h"
26 #include "mac_utils.h"
27
28 /* Function pointer for sending rach ind from MAC to SCH */
29 MacSchRachIndFunc macSchRachIndOpts[]=
30 {
31    packMacSchRachInd,   /* packing for loosely coupled */
32    MacSchRachInd,       /* packing for tightly coupled */ 
33    packMacSchRachInd    /* packing for light weight loosely coupled */
34 };
35
36 /* Function pointer for sending RACH resource request from MAC to SCH */
37 MacSchRachRsrcReqFunc macSchRachRsrcReqOpts[] = 
38 {
39    packMacSchRachRsrcReq,   /* packing for loosely coupled */
40    MacSchRachRsrcReq,       /* packing for tightly coupled */
41    packMacSchRachRsrcReq    /* packing for light weight loosely coupled */
42 };
43
44 /* Function pointer for sending RACH resource response from MAC to DU APP */
45 MacDuRachRsrcRspFunc macDuRachRsrcRspOpts[] =
46 {
47    packDuMacRachRsrcRsp,   /* packing for loosely coupled */
48    DuProcMacRachRsrcRsp,   /* packing for tightly coupled */
49    packDuMacRachRsrcRsp    /* packing for light weight loosly coupled */
50 };
51
52 /*******************************************************************
53  *
54  * @brief Sends RACH indication to SCH
55  *
56  * @details
57  *
58  *    Function : sendRachIndMacToSch
59  *
60  *    Functionality:
61  *     Sends RACH indication to SCH
62  *
63  * @params[in] RACH indication info
64  * @return ROK     - success
65  *         RFAILED - failure
66  *
67  * ****************************************************************/
68 uint8_t sendRachIndMacToSch(RachIndInfo *rachInd)
69 {
70    Pst pst;
71
72    FILL_PST_MAC_TO_SCH(pst, EVENT_RACH_IND_TO_SCH);
73    return(*macSchRachIndOpts[pst.selector])(&pst, rachInd); 
74 }
75
76 /*******************************************************************
77  *
78  * @brief Processes RACH indication from PHY
79  *
80  * @details
81  *
82  *    Function : fapiMacRachInd
83  *
84  *    Functionality:
85  *      Processes RACH indication from PHY
86  *
87  * @params[in] Post structure
88  *             Rach indication message
89  * @return ROK     - success
90  *         RFAILED - failure
91  *
92  * ****************************************************************/ 
93 uint8_t fapiMacRachInd(Pst *pst, RachInd *rachInd)
94 {
95    uint8_t      pduIdx;
96    uint8_t      preambleIdx;
97    RachIndInfo  *rachIndInfo;
98
99    DU_LOG("\nINFO  -->  MAC : Received RACH indication");
100    /* Considering one pdu and one preamble */
101    pduIdx = 0;
102    preambleIdx = 0;
103
104    MAC_ALLOC(rachIndInfo, sizeof(RachIndInfo));
105    if(!rachIndInfo)
106    {
107       DU_LOG("\nERROR  --> MAC : Memory allocation failure in fapiMacRachInd");
108       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rachInd, sizeof(RachInd));
109       return RFAILED;
110    }
111
112    rachIndInfo->cellId = rachInd->cellId;
113    rachIndInfo->timingInfo.sfn = rachInd->timingInfo.sfn;
114    rachIndInfo->timingInfo.slot = rachInd->timingInfo.slot;
115    rachIndInfo->slotIdx = rachInd->rachPdu[pduIdx].slotIdx;
116    rachIndInfo->symbolIdx = rachInd->rachPdu[pduIdx].symbolIdx;
117    rachIndInfo->freqIdx = rachInd->rachPdu[pduIdx].freqIdx;
118    rachIndInfo->preambleIdx = rachInd->rachPdu[pduIdx].preamInfo[preambleIdx].preamIdx;
119    rachIndInfo->timingAdv = rachInd->rachPdu[pduIdx].preamInfo[preambleIdx].timingAdv;
120
121    /* Store the value in macRaCb */
122    createMacRaCb(rachIndInfo);
123
124    /* Free sharable buffer used to send RACH Indication from lower MAC to MAC */
125    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rachInd, sizeof(RachInd));
126
127    /* Send RACH Indication to SCH */
128    return(sendRachIndMacToSch(rachIndInfo));
129 }
130
131 /*******************************************************************
132  *
133  * @brief Processes RACH Resource request from DU APP
134  *
135  * @details
136  *
137  *    Function : MacProcRachRsrcReq
138  *
139  *    Functionality: Processes RACH resource request from DU APP.
140  *      Fills and sends RACH resource request towards SCH.
141  *
142  * @params[in] Post structure
143  *             RACH resource request
144  * @return ROK     - success
145  *         RFAILED - failure
146  *
147  * ****************************************************************/
148 uint8_t MacProcRachRsrcReq(Pst *pst, MacRachRsrcReq *rachRsrcReq)
149 {
150    uint8_t   ret = RFAILED;
151    uint16_t  cellIdx = 0;
152    Pst       schPst;
153    MacCellCb *cellCb = NULLP;
154    MacUeCb   *ueCb = NULLP;
155    SchRachRsrcReq *schRachRsrcReq = NULLP;
156
157    DU_LOG("\nINFO  -->  MAC : Recieved RACH Resource Request for Cell ID [%d] UE ID [%d]",\
158          rachRsrcReq->cellId, rachRsrcReq->ueId);
159
160    /* Fetch Cell Cb */
161    GET_CELL_IDX(rachRsrcReq->cellId, cellIdx);
162    if(macCb.macCell[cellIdx] && (macCb.macCell[cellIdx]->cellId == rachRsrcReq->cellId))
163    {
164       cellCb = macCb.macCell[cellIdx];
165
166       /* Fetch UE Cb */
167       if(cellCb->ueCb[rachRsrcReq->ueId-1].ueId == rachRsrcReq->ueId)
168       {
169          ueCb = &cellCb->ueCb[rachRsrcReq->ueId-1];
170          /* Allocate memory to RACH resource request to be sent to SCH */
171          MAC_ALLOC(schRachRsrcReq, sizeof(SchRachRsrcReq));
172          if(schRachRsrcReq)
173          {
174             /* Fill SCH RACH resource request from information received from DU APP to MAC */
175             schRachRsrcReq->cellId = rachRsrcReq->cellId;
176             schRachRsrcReq->crnti = ueCb->crnti;
177             schRachRsrcReq->numSsb = rachRsrcReq->numSsb;
178             memcpy(schRachRsrcReq->ssbIdx, rachRsrcReq->ssbIdx, sizeof(schRachRsrcReq->ssbIdx));
179
180             /* Send RACH resource request from MAC to SCH */
181             FILL_PST_MAC_TO_SCH(schPst, EVENT_RACH_RESOURCE_REQUEST_TO_SCH);
182             ret = (*macSchRachRsrcReqOpts[schPst.selector])(&schPst, schRachRsrcReq);
183          }
184          else
185             DU_LOG("\nERROR  -->  MAC : Memory allocation failed for RACH resource request to SCH");
186       }
187       else
188          DU_LOG("\nERROR  -->  MAC : UE ID [%d] not found", rachRsrcReq->ueId);
189    }
190    else
191       DU_LOG("\nERROR  -->  MAC : Cell ID [%d] not found", rachRsrcReq->cellId);
192
193    /* Free sharable buffer used to send RACH reource request from DU APP to MAC */
194    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, rachRsrcReq, sizeof(MacRachRsrcReq));
195    return ret;
196 }
197
198 /*******************************************************************
199  *
200  * @brief Processes RACH Resource response from SCH
201  *
202  * @details
203  *
204  *    Function : MacProcSchRachRsrcRsp
205  *
206  *    Functionality: Processes RACH resource response from SCH
207  *      Fills and sends RACH resource response towards DU APP
208  *
209  * @params[in] Post structure
210  *             RACH resource response
211  * @return ROK     - success
212  *         RFAILED - failure
213  *
214  * ****************************************************************/
215 uint8_t MacProcSchRachRsrcRsp(Pst *pst, SchRachRsrcRsp *schRachRsrcRsp)
216 {
217    uint16_t cellIdx = 0;
218    Pst rspPst;
219    MacRachRsrcRsp *rachRsrcRsp = NULLP;
220    MacCellCb *cellCb = NULLP;
221    MacUeCb   *ueCb = NULLP;
222
223    DU_LOG("\nINFO  -->  MAC : Received RACH resource response from SCH : Cell ID [%d] CRNTI [%d]", \
224          schRachRsrcRsp->cellId, schRachRsrcRsp->crnti);
225
226    /* Fill RACH resource response to send to DU APP */
227    MAC_ALLOC_SHRABL_BUF(rachRsrcRsp, sizeof(MacRachRsrcRsp));
228    if(!rachRsrcRsp)
229    {
230       DU_LOG("\nERROR  -->  MAC : Memory allocation failed for RACH resource response");
231       MAC_FREE(schRachRsrcRsp, sizeof(SchRachRsrcRsp));
232       return RFAILED;
233    }
234    rachRsrcRsp->cellId = schRachRsrcRsp->cellId;
235    GET_UE_ID(schRachRsrcRsp->crnti, rachRsrcRsp->ueId);
236    rachRsrcRsp->result = MAC_DU_APP_RSP_OK;
237
238    /* Fill Pst structure to send RACH resource response from MAC to DU APP */
239    FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_RACH_RESOURCE_RSP);
240
241    /* If negative response is received from SCH, send it further to DU APP */ 
242    if(schRachRsrcRsp->result == RSP_NOK)
243    {
244       DU_LOG("\nINFO  -->  MAC : RACH Resource response from SCH : Result [FAILURE]");
245       rachRsrcRsp->result = MAC_DU_APP_RSP_NOK;
246    }
247    else
248    {
249       DU_LOG("\nINFO  -->  MAC : RACH Resource response from SCH : Result [SUCCESS]");
250       
251       /* Fetch Cell Cb */
252       GET_CELL_IDX(schRachRsrcRsp->cellId, cellIdx);
253       if(macCb.macCell[cellIdx] && (macCb.macCell[cellIdx]->cellId == schRachRsrcRsp->cellId))
254       {   
255          cellCb = macCb.macCell[cellIdx];
256
257          /* Fetch UE Cb */
258          if(cellCb->ueCb[rachRsrcRsp->ueId-1].crnti == schRachRsrcRsp->crnti)
259             ueCb = &cellCb->ueCb[rachRsrcRsp->ueId-1];
260          else
261          {
262             DU_LOG("\nERROR  -->  MAC : CRNTI [%d] not found", schRachRsrcRsp->crnti);
263             rachRsrcRsp->result = MAC_DU_APP_RSP_NOK;
264          }   
265       }   
266       else
267       {   
268          DU_LOG("\nERROR  -->  MAC : Cell ID [%d] not found", schRachRsrcRsp->cellId);
269          rachRsrcRsp->result = MAC_DU_APP_RSP_NOK;
270       }   
271    }
272
273    /* TODO : Check if ra-preamble index is to be stored in UE CB */
274
275    /* Fill SSB RACH resource info if SCH has sent a positive response and 
276     * processing of SCH RACH resource response at MAC has been successful so far */
277    if(rachRsrcRsp->result == MAC_DU_APP_RSP_OK)
278    {
279       rachRsrcRsp->newCrnti = ueCb->crnti; 
280       rachRsrcRsp->cfraResource.numSsb = schRachRsrcRsp->cfraResource.numSsb;
281       memcpy(rachRsrcRsp->cfraResource.ssbResource, schRachRsrcRsp->cfraResource.ssbResource, \
282          rachRsrcRsp->cfraResource.numSsb * sizeof(MacCfraSsbResource));
283    }
284
285    /* Free SCH RACH resource response */
286    MAC_FREE(schRachRsrcRsp, sizeof(SchRachRsrcRsp));
287
288    /* Send RACH resource response to DU APP */
289    FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_RACH_RESOURCE_RSP);
290    return (*macDuRachRsrcRspOpts[rspPst.selector])(&rspPst, rachRsrcRsp);
291 }
292
293 /* spec-38.211 Table 6.3.3.1-7 */
294 uint8_t UnrestrictedSetNcsTable[MAX_ZERO_CORR_CFG_IDX] = 
295 {0, 2, 4, 6, 8, 10, 12, 13, 15, 17, 19, 23, 27, 34, 46, 69};
296
297 /*******************************************************************
298  *
299  * @brief Processes UL scheduling info from SCH
300  *
301  * @details
302  *
303  *    Function : MacProcUlSchInfo
304  *
305  *    Functionality: Processes UL scheduling info from SCH
306  *
307  * @params[in] Post structure
308  *             UL scheduling info
309  * @return ROK     - success
310  *         RFAILED - failure
311  *
312  * ****************************************************************/
313 uint8_t MacProcUlSchInfo(Pst *pst, UlSchedInfo *ulSchedInfo)
314 {
315    uint16_t  cellIdx;
316
317 #ifdef CALL_FLOW_DEBUG_LOG
318    DU_LOG("\nCall Flow: ENTSCH -> ENTMAC : EVENT_UL_SCH_INFO\n");
319 #endif
320
321    GET_CELL_IDX(ulSchedInfo->cellId, cellIdx);
322    if(ulSchedInfo != NULLP)
323    {
324       MacUlSlot *currUlSlot = 
325          &macCb.macCell[cellIdx]->ulSlot[ulSchedInfo->slotIndInfo.slot % MAX_SLOTS];
326       memcpy(&currUlSlot->ulInfo, ulSchedInfo, sizeof(UlSchedInfo)); 
327    }
328    return ROK;
329 }
330
331 /**********************************************************************
332   End of file
333  **********************************************************************/
334