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