net/bnxt: support periodic FW health monitoring
[dpdk.git] / drivers / net / bnxt / bnxt_cpr.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2018 Broadcom
3  * All rights reserved.
4  */
5
6 #include <rte_malloc.h>
7 #include <rte_alarm.h>
8
9 #include "bnxt.h"
10 #include "bnxt_cpr.h"
11 #include "bnxt_hwrm.h"
12 #include "bnxt_ring.h"
13 #include "hsi_struct_def_dpdk.h"
14
15 /*
16  * Async event handling
17  */
18 void bnxt_handle_async_event(struct bnxt *bp,
19                              struct cmpl_base *cmp)
20 {
21         struct hwrm_async_event_cmpl *async_cmp =
22                                 (struct hwrm_async_event_cmpl *)cmp;
23         uint16_t event_id = rte_le_to_cpu_16(async_cmp->event_id);
24         struct bnxt_error_recovery_info *info;
25         uint32_t event_data;
26
27         /* TODO: HWRM async events are not defined yet */
28         /* Needs to handle: link events, error events, etc. */
29         switch (event_id) {
30         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
31         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
32         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE:
33                 /* FALLTHROUGH */
34                 bnxt_link_update_op(bp->eth_dev, 1);
35                 break;
36         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
37                 PMD_DRV_LOG(INFO, "Async event: PF driver unloaded\n");
38                 break;
39         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
40                 PMD_DRV_LOG(INFO, "Async event: VF config changed\n");
41                 bnxt_hwrm_func_qcfg(bp, NULL);
42                 break;
43         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED:
44                 PMD_DRV_LOG(INFO, "Port conn async event\n");
45                 break;
46         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY:
47                 event_data = rte_le_to_cpu_32(async_cmp->event_data1);
48                 /* timestamp_lo/hi values are in units of 100ms */
49                 bp->fw_reset_max_msecs = async_cmp->timestamp_hi ?
50                         rte_le_to_cpu_16(async_cmp->timestamp_hi) * 100 :
51                         BNXT_MAX_FW_RESET_TIMEOUT;
52                 bp->fw_reset_min_msecs = async_cmp->timestamp_lo ?
53                         async_cmp->timestamp_lo * 100 :
54                         BNXT_MIN_FW_READY_TIMEOUT;
55                 if ((event_data & EVENT_DATA1_REASON_CODE_MASK) ==
56                     EVENT_DATA1_REASON_CODE_FW_EXCEPTION_FATAL) {
57                         PMD_DRV_LOG(INFO,
58                                     "Firmware fatal reset event received\n");
59                         bp->flags |= BNXT_FLAG_FATAL_ERROR;
60                 } else {
61                         PMD_DRV_LOG(INFO,
62                                     "Firmware non-fatal reset event received\n");
63                 }
64
65                 bp->flags |= BNXT_FLAG_FW_RESET;
66                 rte_eal_alarm_set(US_PER_MS, bnxt_dev_reset_and_resume,
67                                   (void *)bp);
68                 break;
69         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY:
70                 info = bp->recovery_info;
71
72                 if (!info)
73                         return;
74
75                 PMD_DRV_LOG(INFO, "Error recovery async event received\n");
76
77                 event_data = rte_le_to_cpu_32(async_cmp->event_data1) &
78                                 EVENT_DATA1_FLAGS_MASK;
79
80                 if (event_data & EVENT_DATA1_FLAGS_MASTER_FUNC)
81                         info->flags |= BNXT_FLAG_MASTER_FUNC;
82                 else
83                         info->flags &= ~BNXT_FLAG_MASTER_FUNC;
84
85                 if (event_data & EVENT_DATA1_FLAGS_RECOVERY_ENABLED)
86                         info->flags |= BNXT_FLAG_RECOVERY_ENABLED;
87                 else
88                         info->flags &= ~BNXT_FLAG_RECOVERY_ENABLED;
89
90                 PMD_DRV_LOG(INFO, "recovery enabled(%d), master function(%d)\n",
91                             bnxt_is_recovery_enabled(bp),
92                             bnxt_is_master_func(bp));
93
94                 if (bp->flags & BNXT_FLAG_FW_HEALTH_CHECK_SCHEDULED)
95                         return;
96
97                 info->last_heart_beat =
98                         bnxt_read_fw_status_reg(bp, BNXT_FW_HEARTBEAT_CNT_REG);
99                 info->last_reset_counter =
100                         bnxt_read_fw_status_reg(bp, BNXT_FW_RECOVERY_CNT_REG);
101
102                 bnxt_schedule_fw_health_check(bp);
103                 break;
104         default:
105                 PMD_DRV_LOG(INFO, "handle_async_event id = 0x%x\n", event_id);
106                 break;
107         }
108 }
109
110 void bnxt_handle_fwd_req(struct bnxt *bp, struct cmpl_base *cmpl)
111 {
112         struct hwrm_exec_fwd_resp_input *fwreq;
113         struct hwrm_fwd_req_cmpl *fwd_cmpl = (struct hwrm_fwd_req_cmpl *)cmpl;
114         struct input *fwd_cmd;
115         uint16_t fw_vf_id;
116         uint16_t vf_id;
117         uint16_t req_len;
118         int rc;
119
120         if (bp->pf.active_vfs <= 0) {
121                 PMD_DRV_LOG(ERR, "Forwarded VF with no active VFs\n");
122                 return;
123         }
124
125         /* Qualify the fwd request */
126         fw_vf_id = rte_le_to_cpu_16(fwd_cmpl->source_id);
127         vf_id = fw_vf_id - bp->pf.first_vf_id;
128
129         req_len = (rte_le_to_cpu_16(fwd_cmpl->req_len_type) &
130                    HWRM_FWD_REQ_CMPL_REQ_LEN_MASK) >>
131                 HWRM_FWD_REQ_CMPL_REQ_LEN_SFT;
132         if (req_len > sizeof(fwreq->encap_request))
133                 req_len = sizeof(fwreq->encap_request);
134
135         /* Locate VF's forwarded command */
136         fwd_cmd = (struct input *)bp->pf.vf_info[vf_id].req_buf;
137
138         if (fw_vf_id < bp->pf.first_vf_id ||
139             fw_vf_id >= (bp->pf.first_vf_id) + bp->pf.active_vfs) {
140                 PMD_DRV_LOG(ERR,
141                 "FWD req's source_id 0x%x out of range 0x%x - 0x%x (%d %d)\n",
142                         fw_vf_id, bp->pf.first_vf_id,
143                         (bp->pf.first_vf_id) + bp->pf.active_vfs - 1,
144                         bp->pf.first_vf_id, bp->pf.active_vfs);
145                 goto reject;
146         }
147
148         if (bnxt_rcv_msg_from_vf(bp, vf_id, fwd_cmd) == true) {
149                 /*
150                  * In older firmware versions, the MAC had to be all zeros for
151                  * the VF to set it's MAC via hwrm_func_vf_cfg. Set to all
152                  * zeros if it's being configured and has been ok'd by caller.
153                  */
154                 if (fwd_cmd->req_type == HWRM_FUNC_VF_CFG) {
155                         struct hwrm_func_vf_cfg_input *vfc = (void *)fwd_cmd;
156
157                         if (vfc->enables &
158                             HWRM_FUNC_VF_CFG_INPUT_ENABLES_DFLT_MAC_ADDR) {
159                                 bnxt_hwrm_func_vf_mac(bp, vf_id,
160                                 (const uint8_t *)"\x00\x00\x00\x00\x00");
161                         }
162                 }
163                 if (fwd_cmd->req_type == HWRM_CFA_L2_SET_RX_MASK) {
164                         struct hwrm_cfa_l2_set_rx_mask_input *srm =
165                                                         (void *)fwd_cmd;
166
167                         srm->vlan_tag_tbl_addr = rte_cpu_to_le_64(0);
168                         srm->num_vlan_tags = rte_cpu_to_le_32(0);
169                         srm->mask &= ~rte_cpu_to_le_32(
170                                 HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLANONLY |
171                             HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLAN_NONVLAN |
172                             HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ANYVLAN_NONVLAN);
173                 }
174                 /* Forward */
175                 rc = bnxt_hwrm_exec_fwd_resp(bp, fw_vf_id, fwd_cmd, req_len);
176                 if (rc) {
177                         PMD_DRV_LOG(ERR,
178                                 "Failed to send FWD req VF 0x%x, type 0x%x.\n",
179                                 fw_vf_id - bp->pf.first_vf_id,
180                                 rte_le_to_cpu_16(fwd_cmd->req_type));
181                 }
182                 return;
183         }
184
185 reject:
186         rc = bnxt_hwrm_reject_fwd_resp(bp, fw_vf_id, fwd_cmd, req_len);
187         if (rc) {
188                 PMD_DRV_LOG(ERR,
189                         "Failed to send REJECT req VF 0x%x, type 0x%x.\n",
190                         fw_vf_id - bp->pf.first_vf_id,
191                         rte_le_to_cpu_16(fwd_cmd->req_type));
192         }
193
194         return;
195 }
196
197 int bnxt_event_hwrm_resp_handler(struct bnxt *bp, struct cmpl_base *cmp)
198 {
199         bool evt = 0;
200
201         if (bp == NULL || cmp == NULL) {
202                 PMD_DRV_LOG(ERR, "invalid NULL argument\n");
203                 return evt;
204         }
205
206         if (unlikely(is_bnxt_in_error(bp)))
207                 return 0;
208
209         switch (CMP_TYPE(cmp)) {
210         case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
211                 /* Handle any async event */
212                 bnxt_handle_async_event(bp, cmp);
213                 evt = 1;
214                 break;
215         case CMPL_BASE_TYPE_HWRM_FWD_RESP:
216                 /* Handle HWRM forwarded responses */
217                 bnxt_handle_fwd_req(bp, cmp);
218                 evt = 1;
219                 break;
220         default:
221                 /* Ignore any other events */
222                 PMD_DRV_LOG(INFO, "Ignoring %02x completion\n", CMP_TYPE(cmp));
223                 break;
224         }
225
226         return evt;
227 }
228
229 bool bnxt_is_master_func(struct bnxt *bp)
230 {
231         if (bp->recovery_info->flags & BNXT_FLAG_MASTER_FUNC)
232                 return true;
233
234         return false;
235 }
236
237 bool bnxt_is_recovery_enabled(struct bnxt *bp)
238 {
239         struct bnxt_error_recovery_info *info;
240
241         info = bp->recovery_info;
242         if (info && (info->flags & BNXT_FLAG_RECOVERY_ENABLED))
243                 return true;
244
245         return false;
246 }