net/bnxt: fix ring and context memory allocation
[dpdk.git] / drivers / net / bnxt / bnxt_cpr.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2021 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 static void
50 bnxt_process_default_vnic_change(struct bnxt *bp,
51                                  struct hwrm_async_event_cmpl *async_cmp)
52 {
53         uint16_t vnic_state, vf_fid, vf_id;
54         struct bnxt_representor *vf_rep_bp;
55         struct rte_eth_dev *eth_dev;
56         bool vfr_found = false;
57         uint32_t event_data;
58
59         if (!BNXT_TRUFLOW_EN(bp))
60                 return;
61
62         PMD_DRV_LOG(INFO, "Default vnic change async event received\n");
63         event_data = rte_le_to_cpu_32(async_cmp->event_data1);
64
65         vnic_state = (event_data & BNXT_DEFAULT_VNIC_STATE_MASK) >>
66                         BNXT_DEFAULT_VNIC_STATE_SFT;
67         if (vnic_state != BNXT_DEFAULT_VNIC_ALLOC)
68                 return;
69
70         if (!bp->rep_info)
71                 return;
72
73         vf_fid = (event_data & BNXT_DEFAULT_VNIC_CHANGE_VF_ID_MASK) >>
74                         BNXT_DEFAULT_VNIC_CHANGE_VF_ID_SFT;
75         PMD_DRV_LOG(INFO, "async event received vf_id 0x%x\n", vf_fid);
76
77         for (vf_id = 0; vf_id < BNXT_MAX_VF_REPS; vf_id++) {
78                 eth_dev = bp->rep_info[vf_id].vfr_eth_dev;
79                 if (!eth_dev)
80                         continue;
81                 vf_rep_bp = eth_dev->data->dev_private;
82                 if (vf_rep_bp &&
83                     vf_rep_bp->fw_fid == vf_fid) {
84                         vfr_found = true;
85                         break;
86                 }
87         }
88         if (!vfr_found)
89                 return;
90
91         bnxt_rep_dev_start_op(eth_dev);
92 }
93
94 /*
95  * Async event handling
96  */
97 void bnxt_handle_async_event(struct bnxt *bp,
98                              struct cmpl_base *cmp)
99 {
100         struct hwrm_async_event_cmpl *async_cmp =
101                                 (struct hwrm_async_event_cmpl *)cmp;
102         uint16_t event_id = rte_le_to_cpu_16(async_cmp->event_id);
103         uint16_t port_id = bp->eth_dev->data->port_id;
104         struct bnxt_error_recovery_info *info;
105         uint32_t event_data;
106         uint32_t echo_req_data1, echo_req_data2;
107
108         switch (event_id) {
109         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
110         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
111         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE:
112                 /* FALLTHROUGH */
113                 bnxt_link_update_op(bp->eth_dev, 0);
114                 break;
115         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
116                 PMD_DRV_LOG(INFO, "Async event: PF driver unloaded\n");
117                 break;
118         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
119                 PMD_DRV_LOG(INFO, "Async event: VF config changed\n");
120                 bnxt_hwrm_func_qcfg(bp, NULL);
121                 break;
122         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED:
123                 PMD_DRV_LOG(INFO, "Port conn async event\n");
124                 break;
125         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY:
126                 /*
127                  * Avoid any rx/tx packet processing during firmware reset
128                  * operation.
129                  */
130                 bnxt_stop_rxtx(bp);
131
132                 /* Ignore reset notify async events when stopping the port */
133                 if (!bp->eth_dev->data->dev_started) {
134                         bp->flags |= BNXT_FLAG_FATAL_ERROR;
135                         return;
136                 }
137
138                 pthread_mutex_lock(&bp->err_recovery_lock);
139                 event_data = rte_le_to_cpu_32(async_cmp->event_data1);
140                 /* timestamp_lo/hi values are in units of 100ms */
141                 bp->fw_reset_max_msecs = async_cmp->timestamp_hi ?
142                         rte_le_to_cpu_16(async_cmp->timestamp_hi) * 100 :
143                         BNXT_MAX_FW_RESET_TIMEOUT;
144                 bp->fw_reset_min_msecs = async_cmp->timestamp_lo ?
145                         async_cmp->timestamp_lo * 100 :
146                         BNXT_MIN_FW_READY_TIMEOUT;
147                 if ((event_data & EVENT_DATA1_REASON_CODE_MASK) ==
148                     EVENT_DATA1_REASON_CODE_FW_EXCEPTION_FATAL) {
149                         PMD_DRV_LOG(INFO,
150                                     "Port %u: Firmware fatal reset event received\n",
151                                     port_id);
152                         bp->flags |= BNXT_FLAG_FATAL_ERROR;
153                 } else {
154                         PMD_DRV_LOG(INFO,
155                                     "Port %u: Firmware non-fatal reset event received\n",
156                                     port_id);
157                 }
158
159                 bp->flags |= BNXT_FLAG_FW_RESET;
160                 pthread_mutex_unlock(&bp->err_recovery_lock);
161                 rte_eal_alarm_set(US_PER_MS, bnxt_dev_reset_and_resume,
162                                   (void *)bp);
163                 break;
164         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY:
165                 info = bp->recovery_info;
166
167                 if (!info)
168                         return;
169
170                 PMD_DRV_LOG(INFO, "Port %u: Error recovery async event received\n",
171                             port_id);
172
173                 event_data = rte_le_to_cpu_32(async_cmp->event_data1) &
174                                 EVENT_DATA1_FLAGS_MASK;
175
176                 if (event_data & EVENT_DATA1_FLAGS_MASTER_FUNC)
177                         info->flags |= BNXT_FLAG_MASTER_FUNC;
178                 else
179                         info->flags &= ~BNXT_FLAG_MASTER_FUNC;
180
181                 if (event_data & EVENT_DATA1_FLAGS_RECOVERY_ENABLED)
182                         info->flags |= BNXT_FLAG_RECOVERY_ENABLED;
183                 else
184                         info->flags &= ~BNXT_FLAG_RECOVERY_ENABLED;
185
186                 PMD_DRV_LOG(INFO, "Port %u: recovery enabled(%d), master function(%d)\n",
187                             port_id, bnxt_is_recovery_enabled(bp),
188                             bnxt_is_master_func(bp));
189
190                 if (bp->flags & BNXT_FLAG_FW_HEALTH_CHECK_SCHEDULED)
191                         return;
192
193                 info->last_heart_beat =
194                         bnxt_read_fw_status_reg(bp, BNXT_FW_HEARTBEAT_CNT_REG);
195                 info->last_reset_counter =
196                         bnxt_read_fw_status_reg(bp, BNXT_FW_RECOVERY_CNT_REG);
197
198                 bnxt_schedule_fw_health_check(bp);
199                 break;
200         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
201                 PMD_DRV_LOG(INFO, "DNC event: evt_data1 %#x evt_data2 %#x\n",
202                             rte_le_to_cpu_32(async_cmp->event_data1),
203                             rte_le_to_cpu_32(async_cmp->event_data2));
204                 break;
205         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_DEFAULT_VNIC_CHANGE:
206                 bnxt_process_default_vnic_change(bp, async_cmp);
207                 break;
208         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST:
209                 echo_req_data1 = rte_le_to_cpu_32(async_cmp->event_data1);
210                 echo_req_data2 = rte_le_to_cpu_32(async_cmp->event_data2);
211                 PMD_DRV_LOG(INFO,
212                             "Port %u: Received fw echo request: data1 %#x data2 %#x\n",
213                             port_id, echo_req_data1, echo_req_data2);
214                 if (bp->recovery_info)
215                         bnxt_hwrm_fw_echo_reply(bp, echo_req_data1,
216                                                 echo_req_data2);
217                 break;
218         default:
219                 PMD_DRV_LOG(DEBUG, "handle_async_event id = 0x%x\n", event_id);
220                 break;
221         }
222 }
223
224 void bnxt_handle_fwd_req(struct bnxt *bp, struct cmpl_base *cmpl)
225 {
226         struct hwrm_exec_fwd_resp_input *fwreq;
227         struct hwrm_fwd_req_cmpl *fwd_cmpl = (struct hwrm_fwd_req_cmpl *)cmpl;
228         struct input *fwd_cmd;
229         uint16_t fw_vf_id;
230         uint16_t vf_id;
231         uint16_t req_len;
232         int rc;
233
234         if (bp->pf->active_vfs <= 0) {
235                 PMD_DRV_LOG(ERR, "Forwarded VF with no active VFs\n");
236                 return;
237         }
238
239         /* Qualify the fwd request */
240         fw_vf_id = rte_le_to_cpu_16(fwd_cmpl->source_id);
241         vf_id = fw_vf_id - bp->pf->first_vf_id;
242
243         req_len = (rte_le_to_cpu_16(fwd_cmpl->req_len_type) &
244                    HWRM_FWD_REQ_CMPL_REQ_LEN_MASK) >>
245                 HWRM_FWD_REQ_CMPL_REQ_LEN_SFT;
246         if (req_len > sizeof(fwreq->encap_request))
247                 req_len = sizeof(fwreq->encap_request);
248
249         /* Locate VF's forwarded command */
250         fwd_cmd = (struct input *)bp->pf->vf_info[vf_id].req_buf;
251
252         if (fw_vf_id < bp->pf->first_vf_id ||
253             fw_vf_id >= bp->pf->first_vf_id + bp->pf->active_vfs) {
254                 PMD_DRV_LOG(ERR,
255                 "FWD req's source_id 0x%x out of range 0x%x - 0x%x (%d %d)\n",
256                         fw_vf_id, bp->pf->first_vf_id,
257                         (bp->pf->first_vf_id) + bp->pf->active_vfs - 1,
258                         bp->pf->first_vf_id, bp->pf->active_vfs);
259                 goto reject;
260         }
261
262         if (bnxt_rcv_msg_from_vf(bp, vf_id, fwd_cmd)) {
263                 /*
264                  * In older firmware versions, the MAC had to be all zeros for
265                  * the VF to set it's MAC via hwrm_func_vf_cfg. Set to all
266                  * zeros if it's being configured and has been ok'd by caller.
267                  */
268                 if (fwd_cmd->req_type == HWRM_FUNC_VF_CFG) {
269                         struct hwrm_func_vf_cfg_input *vfc = (void *)fwd_cmd;
270
271                         if (vfc->enables &
272                             HWRM_FUNC_VF_CFG_INPUT_ENABLES_DFLT_MAC_ADDR) {
273                                 bnxt_hwrm_func_vf_mac(bp, vf_id,
274                                 (const uint8_t *)"\x00\x00\x00\x00\x00");
275                         }
276                 }
277
278                 if (fwd_cmd->req_type == HWRM_CFA_L2_SET_RX_MASK) {
279                         struct hwrm_cfa_l2_set_rx_mask_input *srm =
280                                                         (void *)fwd_cmd;
281
282                         srm->vlan_tag_tbl_addr = rte_cpu_to_le_64(0);
283                         srm->num_vlan_tags = rte_cpu_to_le_32(0);
284                         srm->mask &= ~rte_cpu_to_le_32(
285                                 HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLANONLY |
286                             HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLAN_NONVLAN |
287                             HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ANYVLAN_NONVLAN);
288                 }
289
290                 /* Forward */
291                 rc = bnxt_hwrm_exec_fwd_resp(bp, fw_vf_id, fwd_cmd, req_len);
292                 if (rc) {
293                         PMD_DRV_LOG(ERR,
294                                 "Failed to send FWD req VF 0x%x, type 0x%x.\n",
295                                 fw_vf_id - bp->pf->first_vf_id,
296                                 rte_le_to_cpu_16(fwd_cmd->req_type));
297                 }
298                 return;
299         }
300
301 reject:
302         rc = bnxt_hwrm_reject_fwd_resp(bp, fw_vf_id, fwd_cmd, req_len);
303         if (rc) {
304                 PMD_DRV_LOG(ERR,
305                         "Failed to send REJECT req VF 0x%x, type 0x%x.\n",
306                         fw_vf_id - bp->pf->first_vf_id,
307                         rte_le_to_cpu_16(fwd_cmd->req_type));
308         }
309
310         return;
311 }
312
313 int bnxt_event_hwrm_resp_handler(struct bnxt *bp, struct cmpl_base *cmp)
314 {
315         bool evt = 0;
316
317         if (bp == NULL || cmp == NULL) {
318                 PMD_DRV_LOG(ERR, "invalid NULL argument\n");
319                 return evt;
320         }
321
322         if (unlikely(is_bnxt_in_error(bp)))
323                 return 0;
324
325         switch (CMP_TYPE(cmp)) {
326         case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
327                 /* Handle any async event */
328                 bnxt_handle_async_event(bp, cmp);
329                 evt = 1;
330                 break;
331         case CMPL_BASE_TYPE_HWRM_FWD_REQ:
332                 /* Handle HWRM forwarded responses */
333                 bnxt_handle_fwd_req(bp, cmp);
334                 evt = 1;
335                 break;
336         default:
337                 /* Ignore any other events */
338                 PMD_DRV_LOG(DEBUG, "Ignoring %02x completion\n", CMP_TYPE(cmp));
339                 break;
340         }
341
342         return evt;
343 }
344
345 bool bnxt_is_master_func(struct bnxt *bp)
346 {
347         if (bp->recovery_info->flags & BNXT_FLAG_MASTER_FUNC)
348                 return true;
349
350         return false;
351 }
352
353 bool bnxt_is_recovery_enabled(struct bnxt *bp)
354 {
355         struct bnxt_error_recovery_info *info;
356
357         info = bp->recovery_info;
358         if (info && (info->flags & BNXT_FLAG_RECOVERY_ENABLED))
359                 return true;
360
361         return false;
362 }
363
364 void bnxt_stop_rxtx(struct bnxt *bp)
365 {
366         bp->eth_dev->rx_pkt_burst = &bnxt_dummy_recv_pkts;
367         bp->eth_dev->tx_pkt_burst = &bnxt_dummy_xmit_pkts;
368 }