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