* INTC Contribution to the O-RAN F Release for O-DU Low
[o-du/phy.git] / fapi_5g / source / api / fapi2mac / p7 / nr5g_fapi_proc_srs_ind.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2021 Intel.
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 /**
20  * @file
21  * This file consist of implementation of FAPI SRS.indication message.
22  *
23  **/
24
25 #include "nr5g_mac_phy_api.h"
26 #include "nr5g_fapi_framework.h"
27 #include "nr5g_fapi_fapi2mac_api.h"
28 #include "nr5g_fapi_fapi2mac_p7_proc.h"
29 #include "nr5g_fapi_fapi2mac_p7_pvt_proc.h"
30 #include "nr5g_fapi_memory.h"
31
32  /** @ingroup group_source_api_p7_fapi2mac_proc
33  *
34  *  @param[in]  p_phy_ctx Pointer to PHY context.
35  *  @param[in]  p_phy_srs_ind Pointer to FAPI SRS.indication message structure.
36  *  
37  *  @return     Returns ::SUCCESS and ::FAILURE.
38  *
39  *  @description
40  *  This message includes SRS PDU. 
41  *
42 **/
43 uint8_t nr5g_fapi_srs_indication(
44     bool is_urllc,
45     p_nr5g_fapi_phy_ctx_t p_phy_ctx,
46     p_fapi_api_stored_vendor_queue_elems vendor_extension_elems,
47     PRXSRSIndicationStruct p_phy_srs_ind)
48 {
49     uint8_t phy_id;
50     fapi_srs_indication_t *p_fapi_srs_ind;
51     p_fapi_api_queue_elem_t p_list_elem;
52     p_nr5g_fapi_phy_instance_t p_phy_instance = NULL;
53     nr5g_fapi_stats_t *p_stats;
54     fapi_vendor_p7_ind_msg_t *p_fapi_vend_p7;
55     fapi_vendor_ext_srs_ind_t *p_fapi_vend_srs_ind;
56
57     if (NULL == p_phy_ctx) {
58         NR5G_FAPI_LOG(ERROR_LOG, ("[SRS.indication] Invalid " "Phy Context"));
59         return FAILURE;
60     }
61
62     if (NULL == p_phy_srs_ind) {
63         NR5G_FAPI_LOG(ERROR_LOG, ("[SRS.indication] Invalid "
64                 "SRS Indication"));
65         return FAILURE;
66     }
67
68     phy_id = p_phy_srs_ind->sSFN_Slot.nCarrierIdx;
69     p_phy_instance = &p_phy_ctx->phy_instance[phy_id];
70     if (p_phy_instance->phy_id != phy_id) {
71         NR5G_FAPI_LOG(ERROR_LOG, ("[SRS.indication] Invalid " "Phy Instance"));
72         return FAILURE;
73     }
74
75     p_stats = &p_phy_instance->stats;
76     p_stats->iapi_stats.iapi_srs_ind++;
77
78     p_list_elem =
79         nr5g_fapi_fapi2mac_create_api_list_elem(FAPI_SRS_INDICATION, 1,
80         sizeof(fapi_srs_indication_t));
81     if (!p_list_elem) {
82         NR5G_FAPI_LOG(ERROR_LOG, ("[SRS.indication] Unable "
83                 "to create list element. Out of memory!!!"));
84         return FAILURE;
85     }
86
87     p_fapi_srs_ind = (fapi_srs_indication_t *) (p_list_elem + 1);
88     p_fapi_srs_ind->header.msg_id = FAPI_SRS_INDICATION;
89     p_fapi_srs_ind->header.length = (uint16_t) sizeof(fapi_srs_indication_t);
90
91     p_fapi_vend_p7 =
92         nr5g_fapi_proc_vendor_p7_msg_get(vendor_extension_elems, phy_id);
93     p_fapi_vend_srs_ind =
94         p_fapi_vend_p7 ? &p_fapi_vend_p7->srs_ind : NULL;
95     if (nr5g_fapi_srs_indication_to_fapi_translation(is_urllc, p_phy_instance,
96             p_phy_srs_ind, p_fapi_srs_ind, p_fapi_vend_srs_ind)) {
97         NR5G_FAPI_LOG(ERROR_LOG,
98             ("[SRS.indication] L1 to FAPI " "translation failed"));
99         return FAILURE;
100     }
101
102     /* Add element to send list */
103     nr5g_fapi_fapi2mac_add_api_to_list(phy_id, p_list_elem, is_urllc);
104
105     p_stats->fapi_stats.fapi_srs_ind++;
106     NR5G_FAPI_LOG(DEBUG_LOG, ("[SRS.indication][%u][%u,%u,%u] is_urllc %u",
107             p_phy_instance->phy_id,
108             p_phy_srs_ind->sSFN_Slot.nSFN, p_phy_srs_ind->sSFN_Slot.nSlot,
109             p_phy_srs_ind->sSFN_Slot.nSym, is_urllc));
110
111     return SUCCESS;
112 }
113
114  /** @ingroup group_source_api_p7_fapi2mac_proc
115  *
116  *  @param[in]   ue_id  Variable holding ue_id received SRS.Indication..
117  *  @param[in]   p_ul_slot_info Pointer to ul slot info structure that stores the 
118  *               UL_TTI.request PDU info.
119  *  
120  *  @return     Returns Pointer to srs info, if handle of p_ul_slot_info matches ue_id.
121                         NULL, if handle of p_ul_slot_info not matches ue_id
122  *
123  *  @description
124  *  This function retrieves the srs info stored during corresponding UL_TTI.request processing.  
125  *  based on ue_id.
126  *
127 **/
128 nr5g_fapi_srs_info_t *nr5g_fapi_get_srs_info(
129     uint16_t nUEId,
130     nr5g_fapi_ul_slot_info_t * p_ul_slot_info)
131 {
132     uint8_t i, num_srs;
133
134     nr5g_fapi_srs_info_t *p_srs_info;
135
136     num_srs = p_ul_slot_info->num_srs;
137     for (i = 0; i < num_srs; i++) {
138         p_srs_info = &p_ul_slot_info->srs_info[i];
139         if (p_srs_info->handle == nUEId) {
140             return p_srs_info;
141         }
142     }
143     return NULL;
144 }
145
146  /** @ingroup group_source_api_p7_fapi2mac_proc
147  *
148  *  @param[in]  p_phy_instance Pointer to PHY instance.
149  *  @param[in]   p_phy_srs_ind Pointer to IAPI SRS.indication structure.
150  *  @param[out]  p_fapi_srs_ind Pointer to FAPI SRS.indication structure.
151  *  
152  *  @return     Returns ::SUCCESS and ::FAILURE.
153  *
154  *  @description
155  *  This function converts IAPI SRS.indication to FAPI SRS.indication
156  *  structure.
157  *
158 **/
159 uint8_t nr5g_fapi_srs_indication_to_fapi_translation(
160     bool is_urllc,
161     p_nr5g_fapi_phy_instance_t p_phy_instance,
162     PRXSRSIndicationStruct p_phy_srs_ind,
163     fapi_srs_indication_t * p_fapi_srs_ind,
164     fapi_vendor_ext_srs_ind_t * p_fapi_vend_srs_ind)
165 {
166     uint8_t num_srs_pdus, i;
167     uint8_t symbol_no, num_rept_symbols, nr_of_symbols;
168     uint16_t slot_no, frame_no, num_rbs, j, k;
169     int8_t wideband_snr = 0, rb_snr;
170     int16_t temp_sum_wideband_snr;
171
172     nr5g_fapi_srs_info_t *p_srs_info;
173     fapi_srs_pdu_t *p_fapi_srs_pdu;
174     fapi_symb_snr_t *p_fapi_symb_snr;
175     nr5g_fapi_ul_slot_info_t *p_ul_slot_info;
176     nr5g_fapi_stats_t *p_stats;
177     ULSRSEstStruct *p_ul_srs_est_struct;
178     fapi_vendor_ext_srs_pdu_t *p_vend_srs_pdu;
179
180     p_stats = &p_phy_instance->stats;
181
182     frame_no = p_fapi_srs_ind->sfn = p_phy_srs_ind->sSFN_Slot.nSFN;
183     slot_no = p_fapi_srs_ind->slot = p_phy_srs_ind->sSFN_Slot.nSlot;
184     symbol_no = p_phy_srs_ind->sSFN_Slot.nSym;
185
186     p_ul_slot_info =
187         nr5g_fapi_get_ul_slot_info(is_urllc, frame_no, slot_no, symbol_no, p_phy_instance);
188
189     if (p_ul_slot_info == NULL) {
190         NR5G_FAPI_LOG(ERROR_LOG, ("[SRS.indication] No Valid data available "
191                 "for frame :%d and slot: %d", frame_no, slot_no));
192         return FAILURE;
193     }
194
195     num_srs_pdus = p_fapi_srs_ind->numPdus = p_phy_srs_ind->nNrOfSrs;
196     for (i = 0; i < num_srs_pdus; i++) {
197         temp_sum_wideband_snr = 0;
198         p_stats->iapi_stats.iapi_srs_ind_pdus++;
199         p_fapi_srs_pdu = &p_fapi_srs_ind->srsPdus[i];
200         p_ul_srs_est_struct = &p_phy_srs_ind->sULSRSEstStruct[i];
201         p_srs_info =
202             nr5g_fapi_get_srs_info(p_ul_srs_est_struct->nUEId, p_ul_slot_info);
203         if (p_srs_info == NULL) {
204             NR5G_FAPI_LOG(ERROR_LOG,
205                 ("[SRS.indication] No Valid data available "
206                     "for nUEId:%d with frameno:%d, slot_no:%d",
207                     p_ul_srs_est_struct->nUEId, frame_no, slot_no));
208             return FAILURE;
209         }
210
211         p_fapi_srs_pdu->handle = p_srs_info->handle;
212         p_fapi_srs_pdu->rnti = p_ul_srs_est_struct->nRNTI;
213         p_fapi_srs_pdu->timingAdvance = 0xFFFF;
214
215         nr_of_symbols = p_fapi_srs_pdu->numSymbols =
216             p_ul_srs_est_struct->nNrOfSymbols;
217         for (j = 0; j < nr_of_symbols; j++) {
218             temp_sum_wideband_snr += p_ul_srs_est_struct->nWideBandSNR[j];
219         }
220         wideband_snr = temp_sum_wideband_snr / nr_of_symbols;
221
222         p_fapi_srs_pdu->wideBandSnr = (wideband_snr + 64) * 2;
223         num_rept_symbols = p_fapi_srs_pdu->numReportedSymbols = 1;
224
225         for (j = 0; j < num_rept_symbols; j++) {
226             p_fapi_symb_snr = &p_fapi_srs_pdu->symbSnr[j];
227             num_rbs = p_fapi_symb_snr->numRbs =
228                 p_ul_srs_est_struct->nNrOfBlocks * 4;
229
230             for (k = 0; k < num_rbs; k++) {
231                 rb_snr = p_ul_srs_est_struct->nBlockSNR[k / 68][k % 68];
232                 p_fapi_symb_snr->rbSNR[k] = (rb_snr + 64) * 2;
233             }
234         }
235         p_stats->fapi_stats.fapi_srs_ind_pdus++;
236
237         if(p_fapi_vend_srs_ind) { // Fill vendor ext
238             p_fapi_vend_srs_ind->num_pdus = p_phy_srs_ind->nNrOfSrs;
239             p_vend_srs_pdu = &p_fapi_vend_srs_ind->srs_pdus[i];
240             p_vend_srs_pdu->nr_of_port = p_ul_srs_est_struct->nNrOfPort;
241             p_vend_srs_pdu->nr_of_rx_ant = p_ul_srs_est_struct->nNrOfRxAnt;
242             p_vend_srs_pdu->nr_of_rbs = p_ul_srs_est_struct->nNrOfRbs;
243             p_vend_srs_pdu->is_chan_est_pres =
244                 p_ul_srs_est_struct->nIsChanEstPres;
245             NR5G_FAPI_MEMCPY(p_vend_srs_pdu->p_srs_chan_est,
246                 sizeof(p_vend_srs_pdu->p_srs_chan_est),
247                 p_ul_srs_est_struct->pSrsChanEst,
248                 sizeof(p_ul_srs_est_struct->pSrsChanEst));
249         }
250     }
251
252     return SUCCESS;
253 }