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