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