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