Moving all common header file into common_def.h file
[o-du/l2.git] / src / 5gnrmac / lwr_mac_handle_phy.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 /* header include files (.h) */
20 #include "common_def.h"
21 #include "tfu.h"           /* RGU Interface defines */
22 #ifdef INTEL_FAPI
23 #include "fapi.h"
24 #endif
25
26 /* header/extern include files (.x) */
27 #include "tfu.x"           /* RGU Interface includes */
28 #include "du_log.h"
29 #include "lwr_mac_fsm.h"
30 #include "lwr_mac_phy.h"
31 #include "lwr_mac_upr_inf.h"
32 #include "rg.h"
33
34 #ifdef INTEL_FAPI
35 /* Function pointer for slot indication from lower mac to mac */
36 packSlotIndMsg packSlotIndOpts[] =
37 {
38    packLcSlotInd,  /* packing for loosely coupled */
39    fapiMacSlotInd, /* packing for tightly coupled */
40    packLwlcSlotInd /* packing for light weight loosly coupled */
41 };
42
43 /* Function pointer for rach indication from lower mac to mac */ 
44 packRachIndMsg sendRachIndOpts[] =
45 {
46    packRachInd,
47    fapiMacRachInd,
48    packRachInd
49 };
50
51 /* Function pointer for crc indication from lower mac to mac */
52 packCrcIndMsg sendCrcIndOpts[] =
53 {
54    packCrcInd,
55    fapiMacCrcInd,
56    packCrcInd
57 };
58
59 /* Function pointer for Rx Data indication from lower mac to mac */
60 packRxDataIndMsg sendRxDataIndOpts[] =
61 {
62    packRxDataInd,
63    fapiMacRxDataInd,
64    packRxDataInd
65 };
66  
67 /* Function pointer for stop indication from lower mac to mac */ 
68 packStopIndMsg sendStopIndOpts[] =
69 {
70    packStopInd,
71    fapiMacStopInd,
72    packStopInd
73 };
74 /*******************************************************************
75  *
76  * @brief Fills post structure
77  *
78  * @details
79  *
80  *    Function : fillLwrMacToMacPst
81  *
82  *    Functionality:
83  *     Fills post structure used to send message from lower MAC
84  *     to MAC
85  *
86  * @params[in] Pst pointer 
87  * @return ROK     - success
88  *         RFAILED - failure
89  *
90  * ****************************************************************/
91 void fillLwrMacToMacPst(Pst *pst)
92 {
93    pst->srcProcId = 0;
94    pst->dstProcId = 0;
95    pst->srcEnt = ENTTF;
96    pst->dstEnt = ENTRG;
97    pst->srcInst = 0;
98    pst->dstInst = 0;
99    pst->region = 0;
100    pst->pool =  0; 
101    pst->selector = ODU_SELECTOR_TC;
102 }
103
104 /*******************************************************************
105  *
106  * @brief Processes Slot Indication from PHY and sends to MAC
107  *
108  * @details
109  *
110  *    Function : handleSlotInd
111  *
112  *    Functionality:
113  *     Processes Slot Indication from PHY and sends to MAC
114  *
115  * @params[in] fapi_slot_ind_t pointer
116  * @return ROK     - success
117  *         RFAILED - failure
118  *
119  * ****************************************************************/
120 U16 handleSlotInd(fapi_slot_ind_t *fapiSlotInd)
121 {
122    /* fill Pst structure to send to lwr_mac to MAC */
123    Pst pst;
124    uint16_t ret;
125    SlotIndInfo slotInd;
126
127    fillLwrMacToMacPst(&pst);
128    pst.event = EVENT_SLOT_IND_TO_MAC;
129
130    slotInd.sfn = fapiSlotInd->sfn;
131    slotInd.slot = fapiSlotInd->slot;
132
133    ret = (*packSlotIndOpts[pst.selector])(&pst, &slotInd);
134
135 #ifdef INTEL_WLS
136    slotIndIdx++;
137    if(slotIndIdx > WLS_MEM_FREE_PRD)
138    {
139       slotIndIdx = 1;
140    }
141    freeWlsBlockList(slotIndIdx - 1);
142 #endif
143
144    return ret;
145 }
146
147 /*******************************************************************
148  *
149  * @brief Handles stop indication recived from PHY
150  *
151  * @details
152  *
153  *    Function : handleStopInd
154  *
155  *    Functionality:
156  *         Handles Stop Indication received from PHY
157  *
158  * @return ROK     - success
159  *         RFAILED - failure
160  *
161  * ****************************************************************/
162 uint8_t handleStopInd()
163 {
164    uint8_t ret;
165    Pst pst;
166
167    clGlobalCp.phyState = PHY_STATE_CONFIGURED;
168    DU_LOG("\nLWR_MAC: PHY has moved to configured state");
169
170    fillLwrMacToMacPst(&pst);
171    pst.event = EVENT_STOP_IND_TO_MAC;
172
173    ret = (*sendStopIndOpts[pst.selector])(&pst);
174    return ret;
175 }
176 /*******************************************************************
177  *
178  * @brief Processes Rach Indication from PHY and sends to MAC
179  *
180  * @details
181  *
182  *    Function : handleRachInd
183  *
184  *    Functionality:
185  *         Processes Rach Indication from PHY and sends to MAC
186  *
187  * @params[in] fapi_rach_indication_t pointer
188  * @return ROK     - success
189  *         RFAILED - failure
190  *
191  * ****************************************************************/
192 uint8_t handleRachInd(fapi_rach_indication_t  *fapiRachInd)
193 {
194    Pst          pst;
195    uint8_t      pduIdx;
196    uint8_t      prmbleIdx;
197    RachPduInfo  *rachPdu;
198    RachInd      rachInd;
199
200    rachInd.timingInfo.sfn = fapiRachInd->sfn;
201    rachInd.timingInfo.slot = fapiRachInd->slot;
202    rachInd.numPdu = fapiRachInd->numPdus;
203    for(pduIdx=0; pduIdx < rachInd.numPdu; pduIdx++)
204    {
205       rachPdu = &rachInd.rachPdu[pduIdx];
206       rachPdu->pci = fapiRachInd->rachPdu[pduIdx].physCellId;
207       rachPdu->symbolIdx = fapiRachInd->rachPdu[pduIdx].symbolIndex;
208       rachPdu->slotIdx = fapiRachInd->rachPdu[pduIdx].slotIndex;
209       rachPdu->freqIdx = fapiRachInd->rachPdu[pduIdx].freqIndex;
210       rachPdu->numPream = fapiRachInd->rachPdu[pduIdx].numPreamble; 
211       for(prmbleIdx=0; prmbleIdx<rachPdu->numPream; prmbleIdx++)
212       {
213          rachPdu->preamInfo[prmbleIdx].preamIdx = \
214             fapiRachInd->rachPdu[pduIdx].preambleInfo[prmbleIdx].preambleIndex;
215          rachPdu->preamInfo[prmbleIdx].timingAdv = \
216             fapiRachInd->rachPdu[pduIdx].preambleInfo[prmbleIdx].timingAdvance;
217       }
218    }
219    fillLwrMacToMacPst(&pst);
220    pst.event = EVENT_RACH_IND_TO_MAC;
221
222    (*sendRachIndOpts[pst.selector])(&pst, &rachInd);
223         return ROK;
224
225 }/* handleRachInd */
226
227 /*******************************************************************
228  *
229  * @brief Handles CRC indication from PHY and sends to MAC
230  *
231  * @details
232  *
233  *    Function : handleCrcInd
234  *
235  *    Functionality:
236  *      Handles CRC indication from PHY and sends to MAC
237  *
238  * @params[in] fapi_crc_ind_t message pointer
239  * @return ROK     - success
240  *         RFAILED - failure
241  *
242  * ****************************************************************/
243
244 uint8_t handleCrcInd(fapi_crc_ind_t  *fapiCrcInd)
245 {
246    Pst          pst;
247    uint8_t      crcInfoIdx;
248    uint8_t      crcStatusIdx;
249    CrcInfo      *crcIndInfo;
250    CrcInd       crcInd;
251
252    crcInd.timingInfo.sfn = fapiCrcInd->sfn;
253    crcInd.timingInfo.slot = fapiCrcInd->slot;
254    crcInd.numCrc = fapiCrcInd->numCrcs;
255
256    for(crcInfoIdx = 0; crcInfoIdx < crcInd.numCrc; crcInfoIdx++)
257    {
258       crcIndInfo = &crcInd.crcInfo[crcInfoIdx];
259       crcIndInfo->handle      = fapiCrcInd->crc[crcInfoIdx].handle;
260       crcIndInfo->rnti        = fapiCrcInd->crc[crcInfoIdx].rnti;
261       crcIndInfo->harqId      = fapiCrcInd->crc[crcInfoIdx].harqId;
262       crcIndInfo->tbCrcStatus = fapiCrcInd->crc[crcInfoIdx].tbCrcStatus;
263       crcIndInfo->numCb       = fapiCrcInd->crc[crcInfoIdx].numCb;
264       for(crcStatusIdx = 0; crcStatusIdx < crcIndInfo->numCb; crcStatusIdx++)
265       {
266          crcIndInfo->cbCrcStatus[crcStatusIdx] = \
267             fapiCrcInd->crc[crcInfoIdx].cbCrcStatus[crcStatusIdx];
268       }
269       crcIndInfo->ul_cqi  = fapiCrcInd->crc[crcInfoIdx].ul_cqi;
270       crcIndInfo->timingAdvance = fapiCrcInd->crc[crcInfoIdx].timingAdvance;
271       crcIndInfo->rssi = fapiCrcInd->crc[crcInfoIdx].rssi;
272    }
273
274    fillLwrMacToMacPst(&pst);
275    pst.event = EVENT_CRC_IND_TO_MAC;
276    
277    (*sendCrcIndOpts[pst.selector])(&pst, &crcInd);
278    return ROK;
279
280 } /* handleCrcInd */
281
282 /*******************************************************************
283  *
284  * @brief Handles Rx Data indication from PHY and sends to MAC
285  *
286  * @details
287  *
288  *    Function : handleRxDataInd
289  *
290  *    Functionality:
291  *      Handles Rx Data indication from PHY and sends to MAC
292  *
293  * @params[in] fapi_rx_data_indication_t message pointer
294  * @return ROK     - success
295  *         RFAILED - failure
296  *
297  * ****************************************************************/
298  
299 uint8_t handleRxDataInd(fapi_rx_data_indication_t  *fapiRxDataInd)
300 {
301    Pst           pst;
302    uint8_t       pduIdx;
303    RxDataInd     rxDataInd;
304    RxDataIndPdu  *pdu;   
305
306    rxDataInd.timingInfo.sfn = fapiRxDataInd->sfn; 
307    rxDataInd.timingInfo.slot = fapiRxDataInd->slot;
308    rxDataInd.numPdus = fapiRxDataInd->numPdus;
309
310    for(pduIdx = 0; pduIdx < rxDataInd.numPdus; pduIdx++)
311    {
312       pdu = &rxDataInd.pdus[pduIdx];
313       pdu->handle = fapiRxDataInd->pdus[pduIdx].handle;
314       pdu->rnti = fapiRxDataInd->pdus[pduIdx].rnti;
315       pdu->harqId = fapiRxDataInd->pdus[pduIdx].harqId;
316       pdu->pduLength = fapiRxDataInd->pdus[pduIdx].pduLength;
317       pdu->ul_cqi = fapiRxDataInd->pdus[pduIdx].ul_cqi;
318       pdu->timingAdvance = fapiRxDataInd->pdus[pduIdx].timingAdvance;
319       pdu->rssi = fapiRxDataInd->pdus[pduIdx].rssi;
320
321       MAC_ALLOC(pdu->pduData, pdu->pduLength);
322       memcpy(pdu->pduData, fapiRxDataInd->pdus[pduIdx].pduData, pdu->pduLength);
323    }
324
325    fillLwrMacToMacPst(&pst);
326    pst.event = EVENT_RX_DATA_IND_TO_MAC;
327  
328    (*sendRxDataIndOpts[pst.selector])(&pst, &rxDataInd);
329    return ROK;
330 }
331
332 #endif /* FAPI */
333
334 void handlePhyMessages(uint16_t msgType, uint32_t msgSize, void *msg)
335 {
336 #ifdef INTEL_FAPI
337    /* extract the header */
338    fapi_msg_t *header;
339    header = (fapi_msg_t *)msg;
340
341    switch(header->message_type_id)
342    {
343       case FAPI_PARAM_RESPONSE:
344       case FAPI_CONFIG_RESPONSE:
345       {
346          sendToLowerMac(msgType, msgSize, msg);
347          break;
348       }
349       case FAPI_SLOT_INDICATION:
350       {
351          if(clGlobalCp.phyState == PHY_STATE_CONFIGURED)
352          {
353             DU_LOG("\nLWR_MAC: PHY has moved to running state");
354             clGlobalCp.phyState = PHY_STATE_RUNNING;
355          }
356
357          fapi_slot_ind_t *slotInd;
358          slotInd  = (fapi_slot_ind_t *)msg;
359          handleSlotInd(slotInd);
360          break;
361       }
362       case FAPI_ERROR_INDICATION:
363       {
364          break;
365       }
366       case FAPI_RX_DATA_INDICATION:
367       {
368          fapi_rx_data_indication_t *rxDataInd;
369          rxDataInd = (fapi_rx_data_indication_t *)msg;
370          handleRxDataInd(rxDataInd);
371          break;
372       }  
373       case FAPI_CRC_INDICATION:
374       {
375          fapi_crc_ind_t  *crcInd;
376          crcInd = (fapi_crc_ind_t *)msg;
377          handleCrcInd(crcInd);
378          break;
379       }  
380       case FAPI_UCI_INDICATION:
381       {
382          break;
383       }
384       case FAPI_SRS_INDICATION:
385       {
386          break;
387       }  
388       case FAPI_RACH_INDICATION:
389       {
390          fapi_rach_indication_t  *rachInd;
391          rachInd = (fapi_rach_indication_t *)msg;
392          handleRachInd(rachInd);
393          break;
394       }
395       case FAPI_STOP_INDICATION:
396       {
397          DU_LOG("\nLWR_MAC: Handling Stop Indication");
398          handleStopInd();
399          break;
400       }  
401    }
402 #ifdef INTEL_WLS
403    WLS_MEM_FREE(msg, LWR_MAC_WLS_BUF_SIZE); 
404 #endif
405 #endif
406 }
407
408 /**********************************************************************
409          End of file
410 **********************************************************************/