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