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