Merge "Separation of logs Part - 2"
[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 #include "fapi_vendor_extension.h"
25 #endif
26
27 /* header/extern include files (.x) */
28 #include "lrg.x"
29 #include "du_app_mac_inf.h"
30 #include "mac_sch_interface.h"
31 #include "lwr_mac.h"
32 #include "lwr_mac_fsm.h"
33 #include "lwr_mac_phy.h"
34 #include "lwr_mac_upr_inf.h"
35 #include "mac.h"
36 #include "mac_utils.h"
37 #include "lwr_mac_utils.h"
38
39 #ifdef INTEL_FAPI
40 /* Function pointer for slot indication from lower mac to mac */
41 SlotIndFunc sendSlotIndOpts[] =
42 {
43    packSlotInd,  /* packing for loosely coupled */
44    fapiMacSlotInd, /* packing for tightly coupled */
45    packSlotInd /* packing for light weight loosly coupled */
46 };
47
48 /* Function pointer for rach indication from lower mac to mac */ 
49 RachIndFunc sendRachIndOpts[] =
50 {
51    packRachInd,
52    fapiMacRachInd,
53    packRachInd
54 };
55
56 /* Function pointer for crc indication from lower mac to mac */
57 CrcIndFunc sendCrcIndOpts[] =
58 {
59    packCrcInd,
60    fapiMacCrcInd,
61    packCrcInd
62 };
63
64 /* Function pointer for Rx Data indication from lower mac to mac */
65 RxDataIndFunc sendRxDataIndOpts[] =
66 {
67    packRxDataInd,
68    fapiMacRxDataInd,
69    packRxDataInd
70 };
71
72 /* Function pointer for stop indication from lower mac to mac */ 
73 StopIndFunc sendStopIndOpts[] =
74 {
75    packStopInd,
76    fapiMacStopInd,
77    packStopInd
78 };
79
80 /* Function pointer for Uci indication from lower mac to mac */
81 UciIndFunc sendUciIndOpts[] =
82 {
83    packUciInd,
84    FapiMacUciInd,
85    packUciInd
86 };
87
88 /*******************************************************************
89  *
90  * @brief Processes Slot Indication from PHY and sends to MAC
91  *
92  * @details
93  *
94  *    Function : procSlotInd
95  *
96  *    Functionality:
97  *     Processes Slot Indication from PHY and sends to MAC
98  *
99  * @params[in] fapi_slot_ind_t pointer
100  * @return ROK     - success
101  *         RFAILED - failure
102  *
103  * ****************************************************************/
104 uint8_t procSlotInd(fapi_slot_ind_t *fapiSlotInd)
105 {
106    /* fill Pst structure to send to lwr_mac to MAC */
107    Pst pst;
108    uint16_t ret = 0;
109    SlotIndInfo *slotInd = {0};
110
111    MAC_ALLOC_SHRABL_BUF(slotInd, sizeof(SlotIndInfo));
112    if(slotInd)
113    {
114       slotInd->cellId = lwrMacCb.cellCb[0].cellId; 
115       slotInd->sfn = fapiSlotInd->sfn;
116       slotInd->slot = fapiSlotInd->slot;
117
118       FILL_PST_LWR_MAC_TO_MAC(pst, EVENT_SLOT_IND_TO_MAC);
119       ret = (*sendSlotIndOpts[pst.selector])(&pst, slotInd);
120    }
121    else
122    {
123       DU_LOG("\nERROR  -->  LWR_MAC: Memory allocation failed in procSlotInd");
124       ret = RFAILED;
125    }
126
127 #ifdef INTEL_WLS_MEM
128    slotIndIdx++;
129    if(slotIndIdx > WLS_MEM_FREE_PRD)
130    {
131       slotIndIdx = 1;
132    }
133    freeWlsBlockList(slotIndIdx - 1);
134 #endif
135    return ret;
136 }
137
138 /*******************************************************************
139  *
140  * @brief Handles stop indication recived from PHY
141  *
142  * @details
143  *
144  *    Function : procStopInd
145  *
146  *    Functionality:
147  *         Handles Stop Indication received from PHY
148  *
149  * @return ROK     - success
150  *         RFAILED - failure
151  *
152  * ****************************************************************/
153 uint8_t procStopInd()
154 {
155    Pst pst;
156    uint16_t *cellId = NULLP;
157
158    MAC_ALLOC_SHRABL_BUF(cellId, sizeof(uint16_t));
159    if(!cellId)
160    {
161       DU_LOG("\nERROR  -->  LWR_MAC: Memory Allocation failed in procStopInd");
162       return RFAILED;
163    }
164
165    *cellId = lwrMacCb.cellCb[0].cellId;
166    lwrMacCb.phyState = PHY_STATE_CONFIGURED;
167    lwrMacCb.cellCb[0].state = PHY_STATE_CONFIGURED;
168    DU_LOG("\nINFO  -->  LWR_MAC: PHY has moved to configured state");
169
170    FILL_PST_LWR_MAC_TO_MAC(pst, EVENT_STOP_IND_TO_MAC);
171    return (*sendStopIndOpts[pst.selector])(&pst, cellId);
172 }
173
174 /*******************************************************************
175  *
176  * @brief Processes Rach Indication from PHY and sends to MAC
177  *
178  * @details
179  *
180  *    Function : procRachInd
181  *
182  *    Functionality:
183  *         Processes Rach Indication from PHY and sends to MAC
184  *
185  * @params[in] fapi_rach_indication_t pointer
186  * @return ROK     - success
187  *         RFAILED - failure
188  *
189  * ****************************************************************/
190 uint8_t procRachInd(fapi_rach_indication_t  *fapiRachInd)
191 {
192    Pst          pst;
193    uint8_t      pduIdx;
194    uint8_t      prmbleIdx;
195    RachPduInfo  *rachPdu = NULLP;
196    RachInd      *rachInd = NULLP;
197
198    MAC_ALLOC_SHRABL_BUF(rachInd, sizeof(RachInd));
199    if(!rachInd)
200    {
201       DU_LOG("\nERROR  -->  LWR_MAC: Memory Allocation failed in procRachInd");
202       return RFAILED;
203    }
204
205    rachInd->cellId = lwrMacCb.cellCb[0].cellId;
206    rachInd->timingInfo.sfn = fapiRachInd->sfn;
207    rachInd->timingInfo.slot = fapiRachInd->slot;
208    rachInd->numPdu = fapiRachInd->numPdus;
209    for(pduIdx=0; pduIdx < rachInd->numPdu; pduIdx++)
210    {
211       rachPdu = &rachInd->rachPdu[pduIdx];
212       rachPdu->pci = fapiRachInd->rachPdu[pduIdx].phyCellId;
213       rachPdu->symbolIdx = fapiRachInd->rachPdu[pduIdx].symbolIndex;
214       rachPdu->slotIdx = fapiRachInd->rachPdu[pduIdx].slotIndex;
215       rachPdu->freqIdx = fapiRachInd->rachPdu[pduIdx].freqIndex;
216       rachPdu->numPream = fapiRachInd->rachPdu[pduIdx].numPreamble; 
217       for(prmbleIdx=0; prmbleIdx<rachPdu->numPream; prmbleIdx++)
218       {
219          rachPdu->preamInfo[prmbleIdx].preamIdx = \
220             fapiRachInd->rachPdu[pduIdx].preambleInfo[prmbleIdx].preambleIndex;
221          rachPdu->preamInfo[prmbleIdx].timingAdv = \
222             fapiRachInd->rachPdu[pduIdx].preambleInfo[prmbleIdx].timingAdvance;
223       }
224    }
225
226    /* Fill post and sent to MAC */
227    FILL_PST_LWR_MAC_TO_MAC(pst, EVENT_RACH_IND_TO_MAC);
228    return (*sendRachIndOpts[pst.selector])(&pst, rachInd);
229
230 }/* handleRachInd */
231
232 /*******************************************************************
233  *
234  * @brief Handles CRC indication from PHY and sends to MAC
235  *
236  * @details
237  *
238  *    Function : procCrcInd
239  *
240  *    Functionality:
241  *      Handles CRC indication from PHY and sends to MAC
242  *
243  * @params[in] fapi_crc_ind_t message pointer
244  * @return ROK     - success
245  *         RFAILED - failure
246  *
247  * ****************************************************************/
248
249 uint8_t procCrcInd(fapi_crc_ind_t  *fapiCrcInd)
250 {
251    Pst          pst;
252    uint8_t      crcInfoIdx;
253    uint8_t      crcStatusIdx;
254    CrcInfo      *crcIndInfo = NULLP;
255    CrcInd       *crcInd = NULLP;
256
257    MAC_ALLOC_SHRABL_BUF(crcInd, sizeof(CrcInd));
258    if(!crcInd)
259    {
260       DU_LOG("\nERROR  -->  LWR_MAC: Memory Allocation failed in procCrcInd");
261       return RFAILED;
262    }
263
264    crcInd->cellId = lwrMacCb.cellCb[0].cellId;
265    crcInd->timingInfo.sfn = fapiCrcInd->sfn;
266    crcInd->timingInfo.slot = fapiCrcInd->slot;
267    crcInd->numCrc = fapiCrcInd->numCrcs;
268
269    for(crcInfoIdx = 0; crcInfoIdx < crcInd->numCrc; crcInfoIdx++)
270    {
271       crcIndInfo = &crcInd->crcInfo[crcInfoIdx];
272       crcIndInfo->handle      = fapiCrcInd->crc[crcInfoIdx].handle;
273       crcIndInfo->rnti        = fapiCrcInd->crc[crcInfoIdx].rnti;
274       crcIndInfo->harqId      = fapiCrcInd->crc[crcInfoIdx].harqId;
275       crcIndInfo->tbCrcStatus = fapiCrcInd->crc[crcInfoIdx].tbCrcStatus;
276       crcIndInfo->numCb       = fapiCrcInd->crc[crcInfoIdx].numCb;
277       for(crcStatusIdx = 0; crcStatusIdx < crcIndInfo->numCb; crcStatusIdx++)
278       {
279          crcIndInfo->cbCrcStatus[crcStatusIdx] = \
280             fapiCrcInd->crc[crcInfoIdx].cbCrcStatus[crcStatusIdx];
281       }
282       crcIndInfo->ul_cqi  = fapiCrcInd->crc[crcInfoIdx].ul_cqi;
283       crcIndInfo->timingAdvance = fapiCrcInd->crc[crcInfoIdx].timingAdvance;
284       crcIndInfo->rssi = fapiCrcInd->crc[crcInfoIdx].rssi;
285    }
286
287    /* Fill post and sent to MAC */
288    FILL_PST_LWR_MAC_TO_MAC(pst, EVENT_CRC_IND_TO_MAC);
289    return (*sendCrcIndOpts[pst.selector])(&pst, crcInd);
290 } /* handleCrcInd */
291
292 /*******************************************************************
293  *
294  * @brief Handles Rx Data indication from PHY and sends to MAC
295  *
296  * @details
297  *
298  *    Function : procRxDataInd
299  *
300  *    Functionality:
301  *      Handles Rx Data indication from PHY and sends to MAC
302  *
303  * @params[in] fapi_rx_data_indication_t message pointer
304  * @return ROK     - success
305  *         RFAILED - failure
306  *
307  * ****************************************************************/
308
309 uint8_t procRxDataInd(fapi_rx_data_indication_t  *fapiRxDataInd)
310 {
311    Pst           pst;
312    uint8_t       pduIdx =0;
313    RxDataInd     *rxDataInd = NULLP;
314    RxDataIndPdu  *pdu = NULLP;   
315
316    MAC_ALLOC_SHRABL_BUF(rxDataInd, sizeof(RxDataInd));
317    if(!rxDataInd)
318    {
319       DU_LOG("\nERROR  -->  LWR_MAC: Memory Allocation failed in procRxDataInd");
320       return RFAILED;
321    }
322
323    rxDataInd->cellId = lwrMacCb.cellCb[0].cellId;
324    rxDataInd->timingInfo.sfn = fapiRxDataInd->sfn; 
325    rxDataInd->timingInfo.slot = fapiRxDataInd->slot;
326    rxDataInd->numPdus = fapiRxDataInd->numPdus;
327
328    for(pduIdx = 0; pduIdx < rxDataInd->numPdus; pduIdx++)
329    {
330       pdu = &rxDataInd->pdus[pduIdx];
331       pdu->handle = fapiRxDataInd->pdus[pduIdx].handle;
332       pdu->rnti = fapiRxDataInd->pdus[pduIdx].rnti;
333       pdu->harqId = fapiRxDataInd->pdus[pduIdx].harqId;
334       pdu->pduLength = fapiRxDataInd->pdus[pduIdx].pdu_length;
335       pdu->ul_cqi = fapiRxDataInd->pdus[pduIdx].ul_cqi;
336       pdu->timingAdvance = fapiRxDataInd->pdus[pduIdx].timingAdvance;
337       pdu->rssi = fapiRxDataInd->pdus[pduIdx].rssi;
338
339       MAC_ALLOC_SHRABL_BUF(pdu->pduData, pdu->pduLength);
340       memcpy(pdu->pduData, fapiRxDataInd->pdus[pduIdx].pduData, pdu->pduLength);
341 #ifdef INTEL_WLS_MEM      
342       /* Free WLS memory allocated for Rx PDU */
343       WLS_MEM_FREE(fapiRxDataInd->pdus[pduIdx].pduData, LWR_MAC_WLS_BUF_SIZE);
344 #endif
345    }
346
347    /* Fill post and sent to MAC */
348    FILL_PST_LWR_MAC_TO_MAC(pst, EVENT_RX_DATA_IND_TO_MAC);
349    return (*sendRxDataIndOpts[pst.selector])(&pst, rxDataInd);
350 }
351
352 /*******************************************************************
353  *
354  * @brief Fills Uci Ind Pdu Info carried on Pucch Format 0/Format 1
355  *
356  * @details
357  *
358  *    Function : fillUciIndPucchF0F1
359  *
360  *    Functionality:
361  *       Fills Uci Ind Pdu Info carried on Pucch Format 0/Format 1
362  *
363  *@params[in] UciPucchF0F1 *
364  *            fapi_uci_o_pucch_f0f1_t *
365  * @return ROK     - success
366  *         RFAILED - failure
367  *
368  * ****************************************************************/
369 uint8_t fillUciIndPucchF0F1(UciPucchF0F1 *pduInfo, fapi_uci_o_pucch_f0f1_t *fapiPduInfo)
370 {
371
372    uint8_t harqIdx;
373    uint8_t ret = ROK;
374    
375    pduInfo->handle        = fapiPduInfo->handle;
376    pduInfo->pduBitmap     = fapiPduInfo->pduBitmap;
377    pduInfo->pucchFormat   = fapiPduInfo->pucchFormat;
378    pduInfo->ul_cqi        = fapiPduInfo->ul_cqi;
379    pduInfo->crnti         = fapiPduInfo->rnti;
380    pduInfo->timingAdvance = fapiPduInfo->timingAdvance;
381    pduInfo->rssi          = fapiPduInfo->rssi;   
382    if(fapiPduInfo->srInfo.srIndication)
383    {
384       pduInfo->srInfo.srIndPres = fapiPduInfo->srInfo.srIndication;
385       pduInfo->srInfo.srConfdcLevel = fapiPduInfo->srInfo.srConfidenceLevel;
386    }
387    if(fapiPduInfo->harqInfo.numHarq)
388    {
389       pduInfo->harqInfo.numHarq = fapiPduInfo->harqInfo.numHarq;
390       pduInfo->harqInfo.harqConfdcLevel = fapiPduInfo->harqInfo.harqConfidenceLevel;
391       for(harqIdx = 0; harqIdx < pduInfo->harqInfo.numHarq; harqIdx++)
392       {
393          pduInfo->harqInfo.harqValue[harqIdx] = fapiPduInfo->harqInfo.harqValue[harqIdx];
394       }
395    }
396    return ret;
397 }
398
399 /*******************************************************************
400  *
401  * @brief Handles Uci indication from PHY and sends to MAC
402  *
403  * @details
404  *
405  *    Function : procUciInd
406  *
407  *    Functionality:
408  *      Handles Uci indication from PHY and sends to MAC
409  *
410  * @params[in] fapi_uci_indication_t message pointer
411  * @return ROK     - success
412  *         RFAILED - failure
413  *
414  * ****************************************************************/
415
416 uint8_t procUciInd(fapi_uci_indication_t  *fapiUciInd)
417 {
418    uint8_t pduIdx;
419    uint8_t ret = ROK;
420    Pst     pst;
421    UciInd  *macUciInd = NULLP;
422
423    MAC_ALLOC_SHRABL_BUF(macUciInd, sizeof(UciInd));
424    if(!macUciInd)
425    {
426       DU_LOG("\nERROR  -->  LWR_MAC: Memory Allocation failed in procUciInd");
427       return RFAILED;
428    }
429
430    DU_LOG("\nDEBUG  -->  LWR_MAC: Processing UCI Indication");
431    memset(macUciInd, 0, sizeof(UciInd));
432    macUciInd->cellId = lwrMacCb.cellCb[0].cellId;
433    macUciInd->slotInd.sfn = fapiUciInd->sfn; 
434    macUciInd->slotInd.slot = fapiUciInd->slot;
435    macUciInd->numUcis = fapiUciInd->numUcis;
436
437    for(pduIdx = 0; pduIdx < macUciInd->numUcis; pduIdx++)
438    {
439       macUciInd->pdus[pduIdx].pduType = fapiUciInd->uciPdu[pduIdx].pduType;
440       switch(macUciInd->pdus[pduIdx].pduType)
441       {
442          case UCI_IND_PUSCH:
443          break;
444          case UCI_IND_PUCCH_F0F1:
445          {
446             UciPucchF0F1 *pduInfo = NULLP;
447             macUciInd->pdus[pduIdx].pduSize = fapiUciInd->uciPdu[pduIdx].pduSize;
448             pduInfo = &macUciInd->pdus[pduIdx].uci.uciPucchF0F1;
449             ret = fillUciIndPucchF0F1(pduInfo, &fapiUciInd->uciPdu[pduIdx].uci.uciPucchF0F1);
450          }
451          break;
452          case UCI_IND_PUCCH_F2F3F4:
453             break;
454          default:
455             DU_LOG("\nERROR  -->  LWR_MAC: Invalid Pdu Type %d at procmacUciInd()", macUciInd->pdus[pduIdx].pduType);
456             ret = RFAILED;
457             break;
458       }
459    }
460    if(!ret)
461    {
462       FILL_PST_LWR_MAC_TO_MAC(pst, EVENT_UCI_IND_TO_MAC);
463       ret = (*sendUciIndOpts[pst.selector])(&pst, macUciInd);
464    }
465    else
466    {
467       DU_LOG("\nERROR  -->  LWR_MAC: Failed sending UCI Ind to MAC");
468    }
469    return ret;
470 }
471 #endif /* FAPI */
472
473 /*******************************************************************
474  *
475  * @brief Processes message from PHY
476  *
477  * @details
478  *
479  *    Function : procPhyMessages
480  *
481  *    Functionality: Processes message from PHY
482  *
483  * @params[in] 
484  * @return ROK     - success
485  *         RFAILED - failure
486  *
487  * ****************************************************************/
488 void procPhyMessages(uint16_t msgType, uint32_t msgSize, void *msg)
489 {
490 #ifdef INTEL_FAPI
491    /* extract the header */
492    fapi_msg_t *header = NULLP;
493    header = (fapi_msg_t *)msg;
494
495    switch(header->msg_id)
496    {
497 #ifdef INTEL_TIMER_MODE
498       case FAPI_VENDOR_EXT_UL_IQ_SAMPLES:
499          {
500             DU_LOG("\nDEBUG  -->  LWR_MAC: Received FAPI_VENDOR_EXT_UL_IQ_SAMPLES");
501             //send config req
502             uint16_t cellId = 1;
503             sendToLowerMac(CONFIG_REQUEST, 0, (void *)&cellId);
504             break;
505          } 
506 #endif
507       case FAPI_PARAM_RESPONSE:
508          {
509             sendToLowerMac(PARAM_RESPONSE, msgSize, msg);
510             break;
511          }
512       case FAPI_CONFIG_RESPONSE:
513          {
514             sendToLowerMac(CONFIG_RESPONSE, msgSize, msg);
515             break;
516          }
517       case FAPI_SLOT_INDICATION:
518          {
519             if(lwrMacCb.phyState == PHY_STATE_CONFIGURED)
520             {
521                DU_LOG("\nINFO  -->  LWR_MAC: PHY has moved to running state");
522                lwrMacCb.phyState = PHY_STATE_RUNNING;
523                lwrMacCb.cellCb[0].state = PHY_STATE_RUNNING;
524             }
525
526             fapi_slot_ind_t *slotInd = NULLP;
527             slotInd  = (fapi_slot_ind_t *)msg;
528             procSlotInd(slotInd);
529             break;
530          }
531       case FAPI_ERROR_INDICATION:
532          {
533             break;
534          }
535       case FAPI_RX_DATA_INDICATION:
536          {
537             fapi_rx_data_indication_t *rxDataInd;
538             rxDataInd = (fapi_rx_data_indication_t *)msg;
539             procRxDataInd(rxDataInd);
540             break;
541          }  
542       case FAPI_CRC_INDICATION:
543          {
544             fapi_crc_ind_t  *crcInd;
545             crcInd = (fapi_crc_ind_t *)msg;
546             procCrcInd(crcInd);
547             break;
548          }  
549       case FAPI_UCI_INDICATION:
550          {
551             fapi_uci_indication_t *phyUciInd = NULLP;
552             phyUciInd = (fapi_uci_indication_t*)msg;
553             procUciInd(phyUciInd);
554             break;
555          }
556       case FAPI_SRS_INDICATION:
557          {
558             break;
559          }  
560       case FAPI_RACH_INDICATION:
561          {
562             fapi_rach_indication_t  *rachInd;
563             rachInd = (fapi_rach_indication_t *)msg;
564             procRachInd(rachInd);
565             break;
566          }
567       case FAPI_STOP_INDICATION:
568          {
569             DU_LOG("\nINFO  -->  LWR_MAC: Handling Stop Indication");
570             procStopInd();
571             break;
572          }  
573    }
574 #endif
575 }
576
577 /**********************************************************************
578   End of file
579  **********************************************************************/