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