ce7cfb14af829ed534cc345c8100a83b9888406f
[o-du/phy.git] / fapi_5g / source / api / fapi2mac / p5 / nr5g_fapi_proc_shutdown_resp.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2019 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 SHUTDOWN.response message.
22  *
23  **/
24 #include "nr5g_fapi_framework.h"
25 #include "gnb_l1_l2_api.h"
26 #include "nr5g_fapi_fapi2mac_api.h"
27 #include "nr5g_fapi_fapi2mac_p5_proc.h"
28 #include "nr5g_fapi_stats.h"
29 #include "nr5g_fapi_fapi2phy_p5_proc.h"
30 #include "nr5g_fapi_fapi2phy_p5_pvt_proc.h"
31 #include "nr5g_fapi_memory.h"
32
33  /** @ingroup group_source_api_p5_fapi2phy_proc
34  *
35  *  @param[in]  p_phy_ctx Pointer to PHY context.
36  *  @param[in]  p_fapi_resp Pointer to IAPI SHUTDOWN.response message structure.
37  *  @return     Returns ::SUCCESS and ::FAILURE.
38  *
39  *  @description
40  *  This message allows PHY to indicate whether it has successfully cleaned up 
41  *  the PHY resources alloted or not.
42  *
43 **/
44 uint8_t nr5g_fapi_shutdown_response(
45     p_nr5g_fapi_phy_ctx_t p_phy_ctx,
46     PSHUTDOWNRESPONSEStruct p_iapi_resp)
47 {
48     uint8_t phy_id;
49 #ifdef DEBUG_MODE
50     fapi_vendor_ext_shutdown_res_t *p_fapi_resp;
51 #else
52     fapi_stop_ind_t *p_stop_ind;
53 #endif
54     fapi_error_ind_t *p_fapi_error_ind;
55     p_fapi_api_queue_elem_t p_list_elem;
56     p_nr5g_fapi_phy_instance_t p_phy_instance = NULL;
57     nr5g_fapi_stats_t *p_stats;
58
59     if (NULL == p_phy_ctx) {
60         NR5G_FAPI_LOG(ERROR_LOG, ("[SHUTDOWN.response] Invalid "
61                 "phy instance"));
62         return FAILURE;
63     }
64
65     if (NULL == p_iapi_resp) {
66         NR5G_FAPI_LOG(ERROR_LOG, ("[SHUTDOWN.response] Invalid iapi "
67                 "shutdown response message"));
68         return FAILURE;
69     }
70
71     phy_id = p_iapi_resp->sSFN_Slot.nCarrierIdx;
72     p_phy_instance = &p_phy_ctx->phy_instance[phy_id];
73     if (p_phy_instance->phy_id != phy_id) {
74         NR5G_FAPI_LOG(ERROR_LOG, ("[SHUTDOWN.response] Invalid "
75                 "phy instance"));
76         return FAILURE;
77     }
78     p_stats = &p_phy_instance->stats;
79     p_stats->iapi_stats.iapi_shutdown_res++;
80
81     if (p_iapi_resp->nStatus == SUCCESS) {
82 #ifdef DEBUG_MODE
83         p_list_elem =
84             nr5g_fapi_fapi2mac_create_api_list_elem(
85                     FAPI_VENDOR_EXT_SHUTDOWN_RESPONSE, 1,
86                     sizeof(fapi_vendor_ext_shutdown_res_t));
87         if (!p_list_elem) {
88             NR5G_FAPI_LOG(ERROR_LOG, ("[SHUTDOWN.response] Unable to create "
89                     "list element. Out of memory!!!"));
90             return FAILURE;
91         }
92
93         p_fapi_resp = (fapi_vendor_ext_shutdown_res_t *) (p_list_elem + 1);
94         p_fapi_resp->header.msg_id = FAPI_VENDOR_EXT_SHUTDOWN_RESPONSE;
95         p_fapi_resp->header.length =
96             (uint16_t) sizeof(fapi_vendor_ext_shutdown_res_t);
97         p_fapi_resp->sfn = p_iapi_resp->sSFN_Slot.nSFN;
98         p_fapi_resp->slot = p_iapi_resp->sSFN_Slot.nSlot;
99         p_fapi_resp->nStatus = p_iapi_resp->nStatus;
100
101         /* Add element to send list */
102         nr5g_fapi_fapi2mac_add_api_to_list(phy_id, p_list_elem);
103
104         p_stats->fapi_stats.fapi_vext_shutdown_res++;
105         NR5G_FAPI_LOG(INFO_LOG, ("[SHUTDOWN.response][%d]", phy_id));
106         p_phy_instance->shutdown_retries = 0;
107 #else
108         p_list_elem =
109             nr5g_fapi_fapi2mac_create_api_list_elem(FAPI_STOP_INDICATION, 1,
110             sizeof(fapi_stop_ind_t));
111         if (!p_list_elem) {
112             NR5G_FAPI_LOG(ERROR_LOG,
113                 ("[SHUTDOWN.response][STOP.Indication] Unable to create "
114                     "list element. Out of memory!!!"));
115             return FAILURE;
116         }
117         p_stop_ind = (fapi_stop_ind_t *) (p_list_elem + 1);
118         p_stop_ind->header.msg_id = FAPI_STOP_INDICATION;
119         p_stop_ind->header.length = sizeof(fapi_stop_ind_t);
120
121         /* Add element to send list */
122         nr5g_fapi_fapi2mac_add_api_to_list(phy_id, p_list_elem);
123
124         p_stats->fapi_stats.fapi_stop_ind++;
125         NR5G_FAPI_LOG(INFO_LOG, ("[STOP.Indication][%d]", phy_id));
126         p_phy_instance->shutdown_retries = 0;
127 #endif
128     } else {
129         /* PHY SHUTDOWN  Failed. Retrigger Shutdown request for 3 tries before
130          * triggering error indication towards  L2 */
131         p_phy_instance->shutdown_retries++;
132         if (p_phy_instance->shutdown_retries <= 3) {
133             fapi_vendor_ext_shutdown_req_t fapi_req;
134             fapi_req.header.msg_id = FAPI_VENDOR_EXT_SHUTDOWN_REQUEST;
135             fapi_req.header.length = sizeof(fapi_vendor_ext_shutdown_req_t);
136             fapi_req.sfn = 0;
137             fapi_req.slot = 0;
138             fapi_req.test_type = p_phy_instance->shutdown_test_type;
139             nr5g_fapi_shutdown_request(p_phy_instance, &fapi_req);
140         } else {
141             NR5G_FAPI_LOG(ERROR_LOG, ("[SHUTDOWN.response] Invalid status "
142                     "from PHY, hence triggering Error Indication"));
143             p_list_elem =
144                 nr5g_fapi_fapi2mac_create_api_list_elem(FAPI_ERROR_INDICATION,
145                 1, sizeof(fapi_error_ind_t));
146
147             if (!p_list_elem) {
148                 NR5G_FAPI_LOG(ERROR_LOG,
149                     ("[SHUTDOWN.response][Error.Indication] Unable to create "
150                         "list element. Out of memory!!!"));
151                 return FAILURE;
152             }
153             p_fapi_error_ind = (fapi_error_ind_t *) (p_list_elem + 1);
154             p_fapi_error_ind->header.msg_id = FAPI_ERROR_INDICATION;
155             p_fapi_error_ind->header.length =
156                 (uint16_t) sizeof(fapi_error_ind_t);
157             p_fapi_error_ind->sfn = p_iapi_resp->sSFN_Slot.nSFN;
158             p_fapi_error_ind->slot = p_iapi_resp->sSFN_Slot.nSlot;
159             p_fapi_error_ind->message_id = FAPI_VENDOR_EXT_SHUTDOWN_REQUEST;
160             p_fapi_error_ind->error_code = p_iapi_resp->nStatus;
161
162             /* Add element to send list */
163             nr5g_fapi_fapi2mac_add_api_to_list(phy_id, p_list_elem);
164             p_stats->fapi_stats.fapi_error_ind++;
165             p_phy_instance->shutdown_retries = 0;
166             NR5G_FAPI_LOG(INFO_LOG, ("[Error.Indication][%d]", phy_id));
167         }
168     }
169
170     nr5g_fapi_print_phy_instance_stats(p_phy_instance);
171     NR5G_FAPI_MEMSET(&p_phy_instance->stats, sizeof(nr5g_fapi_stats_t), 0,
172         sizeof(nr5g_fapi_stats_t));
173     return SUCCESS;
174 }