Fixes for SSB transmission in Radio mode integration [Issue-ID: ODUHIGH-267]
[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    return ret;
127 }
128
129 /*******************************************************************
130  *
131  * @brief Handles stop indication recived from PHY
132  *
133  * @details
134  *
135  *    Function : procStopInd
136  *
137  *    Functionality:
138  *         Handles Stop Indication received from PHY
139  *
140  * @return ROK     - success
141  *         RFAILED - failure
142  *
143  * ****************************************************************/
144 uint8_t procStopInd()
145 {
146    Pst pst;
147    uint16_t *cellId = NULLP;
148
149    MAC_ALLOC_SHRABL_BUF(cellId, sizeof(uint16_t));
150    if(!cellId)
151    {
152       DU_LOG("\nERROR  -->  LWR_MAC: Memory Allocation failed in procStopInd");
153       return RFAILED;
154    }
155
156    *cellId = lwrMacCb.cellCb[0].cellId;
157    lwrMacCb.phyState = PHY_STATE_CONFIGURED;
158    lwrMacCb.cellCb[0].state = PHY_STATE_CONFIGURED;
159    DU_LOG("\nINFO  -->  LWR_MAC: PHY has moved to configured state");
160
161    FILL_PST_LWR_MAC_TO_MAC(pst, EVENT_STOP_IND_TO_MAC);
162    return (*sendStopIndOpts[pst.selector])(&pst, cellId);
163 }
164
165 /*******************************************************************
166  *
167  * @brief Processes Rach Indication from PHY and sends to MAC
168  *
169  * @details
170  *
171  *    Function : procRachInd
172  *
173  *    Functionality:
174  *         Processes Rach Indication from PHY and sends to MAC
175  *
176  * @params[in] fapi_rach_indication_t pointer
177  * @return ROK     - success
178  *         RFAILED - failure
179  *
180  * ****************************************************************/
181 uint8_t procRachInd(fapi_rach_indication_t  *fapiRachInd)
182 {
183    Pst          pst;
184    uint8_t      pduIdx;
185    uint8_t      prmbleIdx;
186    RachPduInfo  *rachPdu = NULLP;
187    RachInd      *rachInd = NULLP;
188
189    MAC_ALLOC_SHRABL_BUF(rachInd, sizeof(RachInd));
190    if(!rachInd)
191    {
192       DU_LOG("\nERROR  -->  LWR_MAC: Memory Allocation failed in procRachInd");
193       return RFAILED;
194    }
195
196    rachInd->cellId = lwrMacCb.cellCb[0].cellId;
197    rachInd->timingInfo.sfn = fapiRachInd->sfn;
198    rachInd->timingInfo.slot = fapiRachInd->slot;
199    rachInd->numPdu = fapiRachInd->numPdus;
200    for(pduIdx=0; pduIdx < rachInd->numPdu; pduIdx++)
201    {
202       rachPdu = &rachInd->rachPdu[pduIdx];
203       rachPdu->pci = fapiRachInd->rachPdu[pduIdx].phyCellId;
204       rachPdu->symbolIdx = fapiRachInd->rachPdu[pduIdx].symbolIndex;
205       rachPdu->slotIdx = fapiRachInd->rachPdu[pduIdx].slotIndex;
206       rachPdu->freqIdx = fapiRachInd->rachPdu[pduIdx].freqIndex;
207       rachPdu->numPream = fapiRachInd->rachPdu[pduIdx].numPreamble; 
208       for(prmbleIdx=0; prmbleIdx<rachPdu->numPream; prmbleIdx++)
209       {
210          rachPdu->preamInfo[prmbleIdx].preamIdx = \
211             fapiRachInd->rachPdu[pduIdx].preambleInfo[prmbleIdx].preambleIndex;
212          rachPdu->preamInfo[prmbleIdx].timingAdv = \
213             fapiRachInd->rachPdu[pduIdx].preambleInfo[prmbleIdx].timingAdvance;
214       }
215    }
216
217    /* Fill post and sent to MAC */
218    FILL_PST_LWR_MAC_TO_MAC(pst, EVENT_RACH_IND_TO_MAC);
219    return (*sendRachIndOpts[pst.selector])(&pst, rachInd);
220
221 }/* handleRachInd */
222
223 /*******************************************************************
224  *
225  * @brief Handles CRC indication from PHY and sends to MAC
226  *
227  * @details
228  *
229  *    Function : procCrcInd
230  *
231  *    Functionality:
232  *      Handles CRC indication from PHY and sends to MAC
233  *
234  * @params[in] fapi_crc_ind_t message pointer
235  * @return ROK     - success
236  *         RFAILED - failure
237  *
238  * ****************************************************************/
239
240 uint8_t procCrcInd(fapi_crc_ind_t  *fapiCrcInd)
241 {
242    Pst          pst;
243    uint8_t      crcInfoIdx;
244    uint8_t      crcStatusIdx;
245    CrcInfo      *crcIndInfo = NULLP;
246    CrcInd       *crcInd = NULLP;
247
248    MAC_ALLOC_SHRABL_BUF(crcInd, sizeof(CrcInd));
249    if(!crcInd)
250    {
251       DU_LOG("\nERROR  -->  LWR_MAC: Memory Allocation failed in procCrcInd");
252       return RFAILED;
253    }
254
255    crcInd->cellId = lwrMacCb.cellCb[0].cellId;
256    crcInd->timingInfo.sfn = fapiCrcInd->sfn;
257    crcInd->timingInfo.slot = fapiCrcInd->slot;
258    crcInd->numCrc = fapiCrcInd->numCrcs;
259
260    for(crcInfoIdx = 0; crcInfoIdx < crcInd->numCrc; crcInfoIdx++)
261    {
262       crcIndInfo = &crcInd->crcInfo[crcInfoIdx];
263       crcIndInfo->handle      = fapiCrcInd->crc[crcInfoIdx].handle;
264       crcIndInfo->rnti        = fapiCrcInd->crc[crcInfoIdx].rnti;
265       crcIndInfo->harqId      = fapiCrcInd->crc[crcInfoIdx].harqId;
266       crcIndInfo->tbCrcStatus = fapiCrcInd->crc[crcInfoIdx].tbCrcStatus;
267       crcIndInfo->numCb       = fapiCrcInd->crc[crcInfoIdx].numCb;
268       for(crcStatusIdx = 0; crcStatusIdx < crcIndInfo->numCb; crcStatusIdx++)
269       {
270          crcIndInfo->cbCrcStatus[crcStatusIdx] = \
271             fapiCrcInd->crc[crcInfoIdx].cbCrcStatus[crcStatusIdx];
272       }
273       crcIndInfo->ul_cqi  = fapiCrcInd->crc[crcInfoIdx].ul_cqi;
274       crcIndInfo->timingAdvance = fapiCrcInd->crc[crcInfoIdx].timingAdvance;
275       crcIndInfo->rssi = fapiCrcInd->crc[crcInfoIdx].rssi;
276    }
277
278    /* Fill post and sent to MAC */
279    FILL_PST_LWR_MAC_TO_MAC(pst, EVENT_CRC_IND_TO_MAC);
280    return (*sendCrcIndOpts[pst.selector])(&pst, crcInd);
281 } /* handleCrcInd */
282
283 /*******************************************************************
284  *
285  * @brief Handles Rx Data indication from PHY and sends to MAC
286  *
287  * @details
288  *
289  *    Function : procRxDataInd
290  *
291  *    Functionality:
292  *      Handles Rx Data indication from PHY and sends to MAC
293  *
294  * @params[in] fapi_rx_data_indication_t message pointer
295  * @return ROK     - success
296  *         RFAILED - failure
297  *
298  * ****************************************************************/
299
300 uint8_t procRxDataInd(fapi_rx_data_indication_t  *fapiRxDataInd)
301 {
302    Pst           pst;
303    uint8_t       pduIdx =0;
304    RxDataInd     *rxDataInd = NULLP;
305    RxDataIndPdu  *pdu = NULLP;   
306
307    MAC_ALLOC_SHRABL_BUF(rxDataInd, sizeof(RxDataInd));
308    if(!rxDataInd)
309    {
310       DU_LOG("\nERROR  -->  LWR_MAC: Memory Allocation failed in procRxDataInd");
311       return RFAILED;
312    }
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_SHRABL_BUF(pdu->pduData, pdu->pduLength);
331       memcpy(pdu->pduData, fapiRxDataInd->pdus[pduIdx].pduData, pdu->pduLength);
332 #ifdef INTEL_WLS_MEM      
333       /* Free WLS memory allocated for Rx PDU */
334       WLS_MEM_FREE(fapiRxDataInd->pdus[pduIdx].pduData, LWR_MAC_WLS_BUF_SIZE);
335 #endif
336    }
337
338    /* Fill post and sent to MAC */
339    FILL_PST_LWR_MAC_TO_MAC(pst, EVENT_RX_DATA_IND_TO_MAC);
340    return (*sendRxDataIndOpts[pst.selector])(&pst, rxDataInd);
341 }
342
343 /*******************************************************************
344  *
345  * @brief Fills Uci Ind Pdu Info carried on Pucch Format 0/Format 1
346  *
347  * @details
348  *
349  *    Function : fillUciIndPucchF0F1
350  *
351  *    Functionality:
352  *       Fills Uci Ind Pdu Info carried on Pucch Format 0/Format 1
353  *
354  *@params[in] UciPucchF0F1 *
355  *            fapi_uci_o_pucch_f0f1_t *
356  * @return ROK     - success
357  *         RFAILED - failure
358  *
359  * ****************************************************************/
360 uint8_t fillUciIndPucchF0F1(UciPucchF0F1 *pduInfo, fapi_uci_o_pucch_f0f1_t *fapiPduInfo)
361 {
362
363    uint8_t harqIdx;
364    uint8_t ret = ROK;
365    
366    pduInfo->handle        = fapiPduInfo->handle;
367    pduInfo->pduBitmap     = fapiPduInfo->pduBitmap;
368    pduInfo->pucchFormat   = fapiPduInfo->pucchFormat;
369    pduInfo->ul_cqi        = fapiPduInfo->ul_cqi;
370    pduInfo->crnti         = fapiPduInfo->rnti;
371    pduInfo->timingAdvance = fapiPduInfo->timingAdvance;
372    pduInfo->rssi          = fapiPduInfo->rssi;   
373    if(fapiPduInfo->srInfo.srIndication)
374    {
375       pduInfo->srInfo.srIndPres = fapiPduInfo->srInfo.srIndication;
376       pduInfo->srInfo.srConfdcLevel = fapiPduInfo->srInfo.srConfidenceLevel;
377    }
378    if(fapiPduInfo->harqInfo.numHarq)
379    {
380       pduInfo->harqInfo.numHarq = fapiPduInfo->harqInfo.numHarq;
381       pduInfo->harqInfo.harqConfdcLevel = fapiPduInfo->harqInfo.harqConfidenceLevel;
382       for(harqIdx = 0; harqIdx < pduInfo->harqInfo.numHarq; harqIdx++)
383       {
384          pduInfo->harqInfo.harqValue[harqIdx] = fapiPduInfo->harqInfo.harqValue[harqIdx];
385       }
386    }
387    return ret;
388 }
389
390 /*******************************************************************
391  *
392  * @brief Handles Uci indication from PHY and sends to MAC
393  *
394  * @details
395  *
396  *    Function : procUciInd
397  *
398  *    Functionality:
399  *      Handles Uci indication from PHY and sends to MAC
400  *
401  * @params[in] fapi_uci_indication_t message pointer
402  * @return ROK     - success
403  *         RFAILED - failure
404  *
405  * ****************************************************************/
406
407 uint8_t procUciInd(fapi_uci_indication_t  *fapiUciInd)
408 {
409    uint8_t pduIdx;
410    uint8_t ret = ROK;
411    Pst     pst;
412    UciInd  *macUciInd = NULLP;
413
414    MAC_ALLOC_SHRABL_BUF(macUciInd, sizeof(UciInd));
415    if(!macUciInd)
416    {
417       DU_LOG("\nERROR  -->  LWR_MAC: Memory Allocation failed in procUciInd");
418       return RFAILED;
419    }
420
421    DU_LOG("\nDEBUG  -->  LWR_MAC: Processing UCI Indication");
422    memset(macUciInd, 0, sizeof(UciInd));
423    macUciInd->cellId = lwrMacCb.cellCb[0].cellId;
424    macUciInd->slotInd.sfn = fapiUciInd->sfn; 
425    macUciInd->slotInd.slot = fapiUciInd->slot;
426    macUciInd->numUcis = fapiUciInd->numUcis;
427
428    for(pduIdx = 0; pduIdx < macUciInd->numUcis; pduIdx++)
429    {
430       macUciInd->pdus[pduIdx].pduType = fapiUciInd->uciPdu[pduIdx].pduType;
431       switch(macUciInd->pdus[pduIdx].pduType)
432       {
433          case UCI_IND_PUSCH:
434          break;
435          case UCI_IND_PUCCH_F0F1:
436          {
437             UciPucchF0F1 *pduInfo = NULLP;
438             macUciInd->pdus[pduIdx].pduSize = fapiUciInd->uciPdu[pduIdx].pduSize;
439             pduInfo = &macUciInd->pdus[pduIdx].uci.uciPucchF0F1;
440             ret = fillUciIndPucchF0F1(pduInfo, &fapiUciInd->uciPdu[pduIdx].uci.uciPucchF0F1);
441          }
442          break;
443          case UCI_IND_PUCCH_F2F3F4:
444             break;
445          default:
446             DU_LOG("\nERROR  -->  LWR_MAC: Invalid Pdu Type %d at procmacUciInd()", macUciInd->pdus[pduIdx].pduType);
447             ret = RFAILED;
448             break;
449       }
450    }
451    if(!ret)
452    {
453       FILL_PST_LWR_MAC_TO_MAC(pst, EVENT_UCI_IND_TO_MAC);
454       ret = (*sendUciIndOpts[pst.selector])(&pst, macUciInd);
455    }
456    else
457    {
458       DU_LOG("\nERROR  -->  LWR_MAC: Failed sending UCI Ind to MAC");
459    }
460    return ret;
461 }
462 #endif /* FAPI */
463
464 /*******************************************************************
465  *
466  * @brief Processes message from PHY
467  *
468  * @details
469  *
470  *    Function : procPhyMessages
471  *
472  *    Functionality: Processes message from PHY
473  *
474  * @params[in] 
475  * @return ROK     - success
476  *         RFAILED - failure
477  *
478  * ****************************************************************/
479 void procPhyMessages(uint16_t msgType, uint32_t msgSize, void *msg)
480 {
481 #ifdef INTEL_FAPI
482    /* extract the header */
483    fapi_msg_t *header = NULLP;
484    header = (fapi_msg_t *)msg;
485
486    switch(header->msg_id)
487    {
488 #ifdef INTEL_TIMER_MODE
489       case FAPI_VENDOR_EXT_UL_IQ_SAMPLES:
490          {
491             DU_LOG("\nDEBUG  -->  LWR_MAC: Received FAPI_VENDOR_EXT_UL_IQ_SAMPLES");
492             //send config req
493             uint16_t cellId = 1;
494             sendToLowerMac(CONFIG_REQUEST, 0, (void *)&cellId);
495             break;
496          } 
497 #endif
498       case FAPI_PARAM_RESPONSE:
499          {
500             sendToLowerMac(PARAM_RESPONSE, msgSize, msg);
501             break;
502          }
503       case FAPI_CONFIG_RESPONSE:
504          {
505             sendToLowerMac(CONFIG_RESPONSE, msgSize, msg);
506             break;
507          }
508       case FAPI_SLOT_INDICATION:
509          {
510             if(lwrMacCb.phyState == PHY_STATE_CONFIGURED)
511             {
512                DU_LOG("\nINFO  -->  LWR_MAC: PHY has moved to running state");
513                lwrMacCb.phyState = PHY_STATE_RUNNING;
514                lwrMacCb.cellCb[0].state = PHY_STATE_RUNNING;
515             }
516
517             fapi_slot_ind_t *slotInd = NULLP;
518             slotInd  = (fapi_slot_ind_t *)msg;
519             procSlotInd(slotInd);
520             break;
521          }
522       case FAPI_ERROR_INDICATION:
523          {
524             break;
525          }
526       case FAPI_RX_DATA_INDICATION:
527          {
528             fapi_rx_data_indication_t *rxDataInd;
529             rxDataInd = (fapi_rx_data_indication_t *)msg;
530             procRxDataInd(rxDataInd);
531             break;
532          }  
533       case FAPI_CRC_INDICATION:
534          {
535             fapi_crc_ind_t  *crcInd;
536             crcInd = (fapi_crc_ind_t *)msg;
537             procCrcInd(crcInd);
538             break;
539          }  
540       case FAPI_UCI_INDICATION:
541          {
542             fapi_uci_indication_t *phyUciInd = NULLP;
543             phyUciInd = (fapi_uci_indication_t*)msg;
544             procUciInd(phyUciInd);
545             break;
546          }
547       case FAPI_SRS_INDICATION:
548          {
549             break;
550          }  
551       case FAPI_RACH_INDICATION:
552          {
553             fapi_rach_indication_t  *rachInd;
554             rachInd = (fapi_rach_indication_t *)msg;
555             procRachInd(rachInd);
556             break;
557          }
558       case FAPI_STOP_INDICATION:
559          {
560             DU_LOG("\nINFO  -->  LWR_MAC: Handling Stop Indication");
561             procStopInd();
562             break;
563          }  
564    }
565 #endif
566 }
567
568 /**********************************************************************
569   End of file
570  **********************************************************************/