net/bnxt: fix multicast filter programming
[dpdk.git] / drivers / net / bnxt / bnxt_hwrm.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2018 Broadcom
3  * All rights reserved.
4  */
5
6 #include <unistd.h>
7
8 #include <rte_byteorder.h>
9 #include <rte_common.h>
10 #include <rte_cycles.h>
11 #include <rte_malloc.h>
12 #include <rte_memzone.h>
13 #include <rte_version.h>
14
15 #include "bnxt.h"
16 #include "bnxt_cpr.h"
17 #include "bnxt_filter.h"
18 #include "bnxt_hwrm.h"
19 #include "bnxt_rxq.h"
20 #include "bnxt_rxr.h"
21 #include "bnxt_ring.h"
22 #include "bnxt_txq.h"
23 #include "bnxt_txr.h"
24 #include "bnxt_vnic.h"
25 #include "hsi_struct_def_dpdk.h"
26
27 #include <rte_io.h>
28
29 #define HWRM_CMD_TIMEOUT                6000000
30 #define HWRM_SHORT_CMD_TIMEOUT          50000
31 #define HWRM_SPEC_CODE_1_8_3            0x10803
32 #define HWRM_VERSION_1_9_1              0x10901
33 #define HWRM_VERSION_1_9_2              0x10903
34
35 struct bnxt_plcmodes_cfg {
36         uint32_t        flags;
37         uint16_t        jumbo_thresh;
38         uint16_t        hds_offset;
39         uint16_t        hds_threshold;
40 };
41
42 static int page_getenum(size_t size)
43 {
44         if (size <= 1 << 4)
45                 return 4;
46         if (size <= 1 << 12)
47                 return 12;
48         if (size <= 1 << 13)
49                 return 13;
50         if (size <= 1 << 16)
51                 return 16;
52         if (size <= 1 << 21)
53                 return 21;
54         if (size <= 1 << 22)
55                 return 22;
56         if (size <= 1 << 30)
57                 return 30;
58         PMD_DRV_LOG(ERR, "Page size %zu out of range\n", size);
59         return sizeof(void *) * 8 - 1;
60 }
61
62 static int page_roundup(size_t size)
63 {
64         return 1 << page_getenum(size);
65 }
66
67 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem,
68                                   uint8_t *pg_attr,
69                                   uint64_t *pg_dir)
70 {
71         if (rmem->nr_pages > 1) {
72                 *pg_attr = 1;
73                 *pg_dir = rte_cpu_to_le_64(rmem->pg_tbl_map);
74         } else {
75                 *pg_dir = rte_cpu_to_le_64(rmem->dma_arr[0]);
76         }
77 }
78
79 /*
80  * HWRM Functions (sent to HWRM)
81  * These are named bnxt_hwrm_*() and return -1 if bnxt_hwrm_send_message()
82  * fails (ie: a timeout), and a positive non-zero HWRM error code if the HWRM
83  * command was failed by the ChiMP.
84  */
85
86 static int bnxt_hwrm_send_message(struct bnxt *bp, void *msg,
87                                   uint32_t msg_len, bool use_kong_mb)
88 {
89         unsigned int i;
90         struct input *req = msg;
91         struct output *resp = bp->hwrm_cmd_resp_addr;
92         uint32_t *data = msg;
93         uint8_t *bar;
94         uint8_t *valid;
95         uint16_t max_req_len = bp->max_req_len;
96         struct hwrm_short_input short_input = { 0 };
97         uint16_t bar_offset = use_kong_mb ?
98                 GRCPF_REG_KONG_CHANNEL_OFFSET : GRCPF_REG_CHIMP_CHANNEL_OFFSET;
99         uint16_t mb_trigger_offset = use_kong_mb ?
100                 GRCPF_REG_KONG_COMM_TRIGGER : GRCPF_REG_CHIMP_COMM_TRIGGER;
101         uint32_t timeout;
102
103         /* Do not send HWRM commands to firmware in error state */
104         if (bp->flags & BNXT_FLAG_FATAL_ERROR)
105                 return 0;
106
107         /* For VER_GET command, set timeout as 50ms */
108         if (rte_cpu_to_le_16(req->req_type) == HWRM_VER_GET)
109                 timeout = HWRM_SHORT_CMD_TIMEOUT;
110         else
111                 timeout = HWRM_CMD_TIMEOUT;
112
113         if (bp->flags & BNXT_FLAG_SHORT_CMD ||
114             msg_len > bp->max_req_len) {
115                 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
116
117                 memset(short_cmd_req, 0, bp->hwrm_max_ext_req_len);
118                 memcpy(short_cmd_req, req, msg_len);
119
120                 short_input.req_type = rte_cpu_to_le_16(req->req_type);
121                 short_input.signature = rte_cpu_to_le_16(
122                                         HWRM_SHORT_INPUT_SIGNATURE_SHORT_CMD);
123                 short_input.size = rte_cpu_to_le_16(msg_len);
124                 short_input.req_addr =
125                         rte_cpu_to_le_64(bp->hwrm_short_cmd_req_dma_addr);
126
127                 data = (uint32_t *)&short_input;
128                 msg_len = sizeof(short_input);
129
130                 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
131         }
132
133         /* Write request msg to hwrm channel */
134         for (i = 0; i < msg_len; i += 4) {
135                 bar = (uint8_t *)bp->bar0 + bar_offset + i;
136                 rte_write32(*data, bar);
137                 data++;
138         }
139
140         /* Zero the rest of the request space */
141         for (; i < max_req_len; i += 4) {
142                 bar = (uint8_t *)bp->bar0 + bar_offset + i;
143                 rte_write32(0, bar);
144         }
145
146         /* Ring channel doorbell */
147         bar = (uint8_t *)bp->bar0 + mb_trigger_offset;
148         rte_write32(1, bar);
149         /*
150          * Make sure the channel doorbell ring command complete before
151          * reading the response to avoid getting stale or invalid
152          * responses.
153          */
154         rte_io_mb();
155
156         /* Poll for the valid bit */
157         for (i = 0; i < timeout; i++) {
158                 /* Sanity check on the resp->resp_len */
159                 rte_cio_rmb();
160                 if (resp->resp_len && resp->resp_len <= bp->max_resp_len) {
161                         /* Last byte of resp contains the valid key */
162                         valid = (uint8_t *)resp + resp->resp_len - 1;
163                         if (*valid == HWRM_RESP_VALID_KEY)
164                                 break;
165                 }
166                 rte_delay_us(1);
167         }
168
169         if (i >= timeout) {
170                 /* Suppress VER_GET timeout messages during reset recovery */
171                 if (bp->flags & BNXT_FLAG_FW_RESET &&
172                     rte_cpu_to_le_16(req->req_type) == HWRM_VER_GET)
173                         return -ETIMEDOUT;
174
175                 PMD_DRV_LOG(ERR, "Error(timeout) sending msg 0x%04x\n",
176                             req->req_type);
177                 return -ETIMEDOUT;
178         }
179         return 0;
180 }
181
182 /*
183  * HWRM_PREP() should be used to prepare *ALL* HWRM commands.  It grabs the
184  * spinlock, and does initial processing.
185  *
186  * HWRM_CHECK_RESULT() returns errors on failure and may not be used.  It
187  * releases the spinlock only if it returns.  If the regular int return codes
188  * are not used by the function, HWRM_CHECK_RESULT() should not be used
189  * directly, rather it should be copied and modified to suit the function.
190  *
191  * HWRM_UNLOCK() must be called after all response processing is completed.
192  */
193 #define HWRM_PREP(req, type, kong) do { \
194         rte_spinlock_lock(&bp->hwrm_lock); \
195         memset(bp->hwrm_cmd_resp_addr, 0, bp->max_resp_len); \
196         req.req_type = rte_cpu_to_le_16(HWRM_##type); \
197         req.cmpl_ring = rte_cpu_to_le_16(-1); \
198         req.seq_id = kong ? rte_cpu_to_le_16(bp->kong_cmd_seq++) :\
199                 rte_cpu_to_le_16(bp->hwrm_cmd_seq++); \
200         req.target_id = rte_cpu_to_le_16(0xffff); \
201         req.resp_addr = rte_cpu_to_le_64(bp->hwrm_cmd_resp_dma_addr); \
202 } while (0)
203
204 #define HWRM_CHECK_RESULT_SILENT() do {\
205         if (rc) { \
206                 rte_spinlock_unlock(&bp->hwrm_lock); \
207                 return rc; \
208         } \
209         if (resp->error_code) { \
210                 rc = rte_le_to_cpu_16(resp->error_code); \
211                 rte_spinlock_unlock(&bp->hwrm_lock); \
212                 return rc; \
213         } \
214 } while (0)
215
216 #define HWRM_CHECK_RESULT() do {\
217         if (rc) { \
218                 PMD_DRV_LOG(ERR, "failed rc:%d\n", rc); \
219                 rte_spinlock_unlock(&bp->hwrm_lock); \
220                 if (rc == HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED) \
221                         rc = -EACCES; \
222                 else if (rc == HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR) \
223                         rc = -ENOSPC; \
224                 else if (rc == HWRM_ERR_CODE_INVALID_PARAMS) \
225                         rc = -EINVAL; \
226                 else if (rc == HWRM_ERR_CODE_CMD_NOT_SUPPORTED) \
227                         rc = -ENOTSUP; \
228                 else if (rc > 0) \
229                         rc = -EIO; \
230                 return rc; \
231         } \
232         if (resp->error_code) { \
233                 rc = rte_le_to_cpu_16(resp->error_code); \
234                 if (resp->resp_len >= 16) { \
235                         struct hwrm_err_output *tmp_hwrm_err_op = \
236                                                 (void *)resp; \
237                         PMD_DRV_LOG(ERR, \
238                                 "error %d:%d:%08x:%04x\n", \
239                                 rc, tmp_hwrm_err_op->cmd_err, \
240                                 rte_le_to_cpu_32(\
241                                         tmp_hwrm_err_op->opaque_0), \
242                                 rte_le_to_cpu_16(\
243                                         tmp_hwrm_err_op->opaque_1)); \
244                 } else { \
245                         PMD_DRV_LOG(ERR, "error %d\n", rc); \
246                 } \
247                 rte_spinlock_unlock(&bp->hwrm_lock); \
248                 if (rc == HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED) \
249                         rc = -EACCES; \
250                 else if (rc == HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR) \
251                         rc = -ENOSPC; \
252                 else if (rc == HWRM_ERR_CODE_INVALID_PARAMS) \
253                         rc = -EINVAL; \
254                 else if (rc == HWRM_ERR_CODE_CMD_NOT_SUPPORTED) \
255                         rc = -ENOTSUP; \
256                 else if (rc > 0) \
257                         rc = -EIO; \
258                 return rc; \
259         } \
260 } while (0)
261
262 #define HWRM_UNLOCK()           rte_spinlock_unlock(&bp->hwrm_lock)
263
264 int bnxt_hwrm_cfa_l2_clear_rx_mask(struct bnxt *bp, struct bnxt_vnic_info *vnic)
265 {
266         int rc = 0;
267         struct hwrm_cfa_l2_set_rx_mask_input req = {.req_type = 0 };
268         struct hwrm_cfa_l2_set_rx_mask_output *resp = bp->hwrm_cmd_resp_addr;
269
270         HWRM_PREP(req, CFA_L2_SET_RX_MASK, BNXT_USE_CHIMP_MB);
271         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
272         req.mask = 0;
273
274         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
275
276         HWRM_CHECK_RESULT();
277         HWRM_UNLOCK();
278
279         return rc;
280 }
281
282 int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp,
283                                  struct bnxt_vnic_info *vnic,
284                                  uint16_t vlan_count,
285                                  struct bnxt_vlan_table_entry *vlan_table)
286 {
287         int rc = 0;
288         struct hwrm_cfa_l2_set_rx_mask_input req = {.req_type = 0 };
289         struct hwrm_cfa_l2_set_rx_mask_output *resp = bp->hwrm_cmd_resp_addr;
290         uint32_t mask = 0;
291
292         if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
293                 return rc;
294
295         HWRM_PREP(req, CFA_L2_SET_RX_MASK, BNXT_USE_CHIMP_MB);
296         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
297
298         if (vnic->flags & BNXT_VNIC_INFO_BCAST)
299                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_BCAST;
300         if (vnic->flags & BNXT_VNIC_INFO_UNTAGGED)
301                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLAN_NONVLAN;
302
303         if (vnic->flags & BNXT_VNIC_INFO_PROMISC)
304                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_PROMISCUOUS;
305
306         if (vnic->flags & BNXT_VNIC_INFO_ALLMULTI) {
307                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ALL_MCAST;
308         } else if (vnic->flags & BNXT_VNIC_INFO_MCAST) {
309                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_MCAST;
310                 req.num_mc_entries = rte_cpu_to_le_32(vnic->mc_addr_cnt);
311                 req.mc_tbl_addr = rte_cpu_to_le_64(vnic->mc_list_dma_addr);
312         }
313         if (vlan_table) {
314                 if (!(mask & HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLAN_NONVLAN))
315                         mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLANONLY;
316                 req.vlan_tag_tbl_addr = rte_cpu_to_le_64(
317                          rte_mem_virt2iova(vlan_table));
318                 req.num_vlan_tags = rte_cpu_to_le_32((uint32_t)vlan_count);
319         }
320         req.mask = rte_cpu_to_le_32(mask);
321
322         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
323
324         HWRM_CHECK_RESULT();
325         HWRM_UNLOCK();
326
327         return rc;
328 }
329
330 int bnxt_hwrm_cfa_vlan_antispoof_cfg(struct bnxt *bp, uint16_t fid,
331                         uint16_t vlan_count,
332                         struct bnxt_vlan_antispoof_table_entry *vlan_table)
333 {
334         int rc = 0;
335         struct hwrm_cfa_vlan_antispoof_cfg_input req = {.req_type = 0 };
336         struct hwrm_cfa_vlan_antispoof_cfg_output *resp =
337                                                 bp->hwrm_cmd_resp_addr;
338
339         /*
340          * Older HWRM versions did not support this command, and the set_rx_mask
341          * list was used for anti-spoof. In 1.8.0, the TX path configuration was
342          * removed from set_rx_mask call, and this command was added.
343          *
344          * This command is also present from 1.7.8.11 and higher,
345          * as well as 1.7.8.0
346          */
347         if (bp->fw_ver < ((1 << 24) | (8 << 16))) {
348                 if (bp->fw_ver != ((1 << 24) | (7 << 16) | (8 << 8))) {
349                         if (bp->fw_ver < ((1 << 24) | (7 << 16) | (8 << 8) |
350                                         (11)))
351                                 return 0;
352                 }
353         }
354         HWRM_PREP(req, CFA_VLAN_ANTISPOOF_CFG, BNXT_USE_CHIMP_MB);
355         req.fid = rte_cpu_to_le_16(fid);
356
357         req.vlan_tag_mask_tbl_addr =
358                 rte_cpu_to_le_64(rte_mem_virt2iova(vlan_table));
359         req.num_vlan_entries = rte_cpu_to_le_32((uint32_t)vlan_count);
360
361         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
362
363         HWRM_CHECK_RESULT();
364         HWRM_UNLOCK();
365
366         return rc;
367 }
368
369 int bnxt_hwrm_clear_l2_filter(struct bnxt *bp,
370                            struct bnxt_filter_info *filter)
371 {
372         int rc = 0;
373         struct bnxt_filter_info *l2_filter = filter;
374         struct hwrm_cfa_l2_filter_free_input req = {.req_type = 0 };
375         struct hwrm_cfa_l2_filter_free_output *resp = bp->hwrm_cmd_resp_addr;
376
377         if (filter->fw_l2_filter_id == UINT64_MAX)
378                 return 0;
379
380         if (filter->matching_l2_fltr_ptr)
381                 l2_filter = filter->matching_l2_fltr_ptr;
382
383         PMD_DRV_LOG(DEBUG, "filter: %p l2_filter: %p ref_cnt: %d\n",
384                     filter, l2_filter, l2_filter->l2_ref_cnt);
385
386         if (l2_filter->l2_ref_cnt > 0)
387                 l2_filter->l2_ref_cnt--;
388
389         if (l2_filter->l2_ref_cnt > 0)
390                 return 0;
391
392         HWRM_PREP(req, CFA_L2_FILTER_FREE, BNXT_USE_CHIMP_MB);
393
394         req.l2_filter_id = rte_cpu_to_le_64(filter->fw_l2_filter_id);
395
396         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
397
398         HWRM_CHECK_RESULT();
399         HWRM_UNLOCK();
400
401         filter->fw_l2_filter_id = UINT64_MAX;
402
403         return 0;
404 }
405
406 int bnxt_hwrm_set_l2_filter(struct bnxt *bp,
407                          uint16_t dst_id,
408                          struct bnxt_filter_info *filter)
409 {
410         int rc = 0;
411         struct hwrm_cfa_l2_filter_alloc_input req = {.req_type = 0 };
412         struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
413         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
414         const struct rte_eth_vmdq_rx_conf *conf =
415                     &dev_conf->rx_adv_conf.vmdq_rx_conf;
416         uint32_t enables = 0;
417         uint16_t j = dst_id - 1;
418
419         //TODO: Is there a better way to add VLANs to each VNIC in case of VMDQ
420         if ((dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_FLAG) &&
421             conf->pool_map[j].pools & (1UL << j)) {
422                 PMD_DRV_LOG(DEBUG,
423                         "Add vlan %u to vmdq pool %u\n",
424                         conf->pool_map[j].vlan_id, j);
425
426                 filter->l2_ivlan = conf->pool_map[j].vlan_id;
427                 filter->enables |=
428                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN |
429                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN_MASK;
430         }
431
432         if (filter->fw_l2_filter_id != UINT64_MAX)
433                 bnxt_hwrm_clear_l2_filter(bp, filter);
434
435         HWRM_PREP(req, CFA_L2_FILTER_ALLOC, BNXT_USE_CHIMP_MB);
436
437         req.flags = rte_cpu_to_le_32(filter->flags);
438
439         enables = filter->enables |
440               HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_DST_ID;
441         req.dst_id = rte_cpu_to_le_16(dst_id);
442
443         if (enables &
444             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR)
445                 memcpy(req.l2_addr, filter->l2_addr,
446                        RTE_ETHER_ADDR_LEN);
447         if (enables &
448             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR_MASK)
449                 memcpy(req.l2_addr_mask, filter->l2_addr_mask,
450                        RTE_ETHER_ADDR_LEN);
451         if (enables &
452             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_OVLAN)
453                 req.l2_ovlan = filter->l2_ovlan;
454         if (enables &
455             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN)
456                 req.l2_ivlan = filter->l2_ivlan;
457         if (enables &
458             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_OVLAN_MASK)
459                 req.l2_ovlan_mask = filter->l2_ovlan_mask;
460         if (enables &
461             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN_MASK)
462                 req.l2_ivlan_mask = filter->l2_ivlan_mask;
463         if (enables & HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_SRC_ID)
464                 req.src_id = rte_cpu_to_le_32(filter->src_id);
465         if (enables & HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_SRC_TYPE)
466                 req.src_type = filter->src_type;
467         if (filter->pri_hint) {
468                 req.pri_hint = filter->pri_hint;
469                 req.l2_filter_id_hint =
470                         rte_cpu_to_le_64(filter->l2_filter_id_hint);
471         }
472
473         req.enables = rte_cpu_to_le_32(enables);
474
475         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
476
477         HWRM_CHECK_RESULT();
478
479         filter->fw_l2_filter_id = rte_le_to_cpu_64(resp->l2_filter_id);
480         HWRM_UNLOCK();
481
482         return rc;
483 }
484
485 int bnxt_hwrm_ptp_cfg(struct bnxt *bp)
486 {
487         struct hwrm_port_mac_cfg_input req = {.req_type = 0};
488         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
489         uint32_t flags = 0;
490         int rc;
491
492         if (!ptp)
493                 return 0;
494
495         HWRM_PREP(req, PORT_MAC_CFG, BNXT_USE_CHIMP_MB);
496
497         if (ptp->rx_filter)
498                 flags |= HWRM_PORT_MAC_CFG_INPUT_FLAGS_PTP_RX_TS_CAPTURE_ENABLE;
499         else
500                 flags |=
501                         HWRM_PORT_MAC_CFG_INPUT_FLAGS_PTP_RX_TS_CAPTURE_DISABLE;
502         if (ptp->tx_tstamp_en)
503                 flags |= HWRM_PORT_MAC_CFG_INPUT_FLAGS_PTP_TX_TS_CAPTURE_ENABLE;
504         else
505                 flags |=
506                         HWRM_PORT_MAC_CFG_INPUT_FLAGS_PTP_TX_TS_CAPTURE_DISABLE;
507         req.flags = rte_cpu_to_le_32(flags);
508         req.enables = rte_cpu_to_le_32
509                 (HWRM_PORT_MAC_CFG_INPUT_ENABLES_RX_TS_CAPTURE_PTP_MSG_TYPE);
510         req.rx_ts_capture_ptp_msg_type = rte_cpu_to_le_16(ptp->rxctl);
511
512         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
513         HWRM_UNLOCK();
514
515         return rc;
516 }
517
518 static int bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
519 {
520         int rc = 0;
521         struct hwrm_port_mac_ptp_qcfg_input req = {.req_type = 0};
522         struct hwrm_port_mac_ptp_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
523         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
524
525 /*      if (bp->hwrm_spec_code < 0x10801 || ptp)  TBD  */
526         if (ptp)
527                 return 0;
528
529         HWRM_PREP(req, PORT_MAC_PTP_QCFG, BNXT_USE_CHIMP_MB);
530
531         req.port_id = rte_cpu_to_le_16(bp->pf.port_id);
532
533         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
534
535         HWRM_CHECK_RESULT();
536
537         if (!BNXT_CHIP_THOR(bp) &&
538             !(resp->flags & HWRM_PORT_MAC_PTP_QCFG_OUTPUT_FLAGS_DIRECT_ACCESS))
539                 return 0;
540
541         if (resp->flags & HWRM_PORT_MAC_PTP_QCFG_OUTPUT_FLAGS_ONE_STEP_TX_TS)
542                 bp->flags |= BNXT_FLAG_FW_CAP_ONE_STEP_TX_TS;
543
544         ptp = rte_zmalloc("ptp_cfg", sizeof(*ptp), 0);
545         if (!ptp)
546                 return -ENOMEM;
547
548         if (!BNXT_CHIP_THOR(bp)) {
549                 ptp->rx_regs[BNXT_PTP_RX_TS_L] =
550                         rte_le_to_cpu_32(resp->rx_ts_reg_off_lower);
551                 ptp->rx_regs[BNXT_PTP_RX_TS_H] =
552                         rte_le_to_cpu_32(resp->rx_ts_reg_off_upper);
553                 ptp->rx_regs[BNXT_PTP_RX_SEQ] =
554                         rte_le_to_cpu_32(resp->rx_ts_reg_off_seq_id);
555                 ptp->rx_regs[BNXT_PTP_RX_FIFO] =
556                         rte_le_to_cpu_32(resp->rx_ts_reg_off_fifo);
557                 ptp->rx_regs[BNXT_PTP_RX_FIFO_ADV] =
558                         rte_le_to_cpu_32(resp->rx_ts_reg_off_fifo_adv);
559                 ptp->tx_regs[BNXT_PTP_TX_TS_L] =
560                         rte_le_to_cpu_32(resp->tx_ts_reg_off_lower);
561                 ptp->tx_regs[BNXT_PTP_TX_TS_H] =
562                         rte_le_to_cpu_32(resp->tx_ts_reg_off_upper);
563                 ptp->tx_regs[BNXT_PTP_TX_SEQ] =
564                         rte_le_to_cpu_32(resp->tx_ts_reg_off_seq_id);
565                 ptp->tx_regs[BNXT_PTP_TX_FIFO] =
566                         rte_le_to_cpu_32(resp->tx_ts_reg_off_fifo);
567         }
568
569         ptp->bp = bp;
570         bp->ptp_cfg = ptp;
571
572         return 0;
573 }
574
575 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
576 {
577         int rc = 0;
578         struct hwrm_func_qcaps_input req = {.req_type = 0 };
579         struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
580         uint16_t new_max_vfs;
581         uint32_t flags;
582         int i;
583
584         HWRM_PREP(req, FUNC_QCAPS, BNXT_USE_CHIMP_MB);
585
586         req.fid = rte_cpu_to_le_16(0xffff);
587
588         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
589
590         HWRM_CHECK_RESULT();
591
592         bp->max_ring_grps = rte_le_to_cpu_32(resp->max_hw_ring_grps);
593         flags = rte_le_to_cpu_32(resp->flags);
594         if (BNXT_PF(bp)) {
595                 bp->pf.port_id = resp->port_id;
596                 bp->pf.first_vf_id = rte_le_to_cpu_16(resp->first_vf_id);
597                 bp->pf.total_vfs = rte_le_to_cpu_16(resp->max_vfs);
598                 new_max_vfs = bp->pdev->max_vfs;
599                 if (new_max_vfs != bp->pf.max_vfs) {
600                         if (bp->pf.vf_info)
601                                 rte_free(bp->pf.vf_info);
602                         bp->pf.vf_info = rte_malloc("bnxt_vf_info",
603                             sizeof(bp->pf.vf_info[0]) * new_max_vfs, 0);
604                         bp->pf.max_vfs = new_max_vfs;
605                         for (i = 0; i < new_max_vfs; i++) {
606                                 bp->pf.vf_info[i].fid = bp->pf.first_vf_id + i;
607                                 bp->pf.vf_info[i].vlan_table =
608                                         rte_zmalloc("VF VLAN table",
609                                                     getpagesize(),
610                                                     getpagesize());
611                                 if (bp->pf.vf_info[i].vlan_table == NULL)
612                                         PMD_DRV_LOG(ERR,
613                                         "Fail to alloc VLAN table for VF %d\n",
614                                         i);
615                                 else
616                                         rte_mem_lock_page(
617                                                 bp->pf.vf_info[i].vlan_table);
618                                 bp->pf.vf_info[i].vlan_as_table =
619                                         rte_zmalloc("VF VLAN AS table",
620                                                     getpagesize(),
621                                                     getpagesize());
622                                 if (bp->pf.vf_info[i].vlan_as_table == NULL)
623                                         PMD_DRV_LOG(ERR,
624                                         "Alloc VLAN AS table for VF %d fail\n",
625                                         i);
626                                 else
627                                         rte_mem_lock_page(
628                                                bp->pf.vf_info[i].vlan_as_table);
629                                 STAILQ_INIT(&bp->pf.vf_info[i].filter);
630                         }
631                 }
632         }
633
634         bp->fw_fid = rte_le_to_cpu_32(resp->fid);
635         memcpy(bp->dflt_mac_addr, &resp->mac_address, RTE_ETHER_ADDR_LEN);
636         bp->max_rsscos_ctx = rte_le_to_cpu_16(resp->max_rsscos_ctx);
637         bp->max_cp_rings = rte_le_to_cpu_16(resp->max_cmpl_rings);
638         bp->max_tx_rings = rte_le_to_cpu_16(resp->max_tx_rings);
639         bp->max_rx_rings = rte_le_to_cpu_16(resp->max_rx_rings);
640         bp->first_vf_id = rte_le_to_cpu_16(resp->first_vf_id);
641         bp->max_rx_em_flows = rte_le_to_cpu_16(resp->max_rx_em_flows);
642         bp->max_l2_ctx = rte_le_to_cpu_16(resp->max_l2_ctxs);
643         if (!BNXT_CHIP_THOR(bp))
644                 bp->max_l2_ctx += bp->max_rx_em_flows;
645         /* TODO: For now, do not support VMDq/RFS on VFs. */
646         if (BNXT_PF(bp)) {
647                 if (bp->pf.max_vfs)
648                         bp->max_vnics = 1;
649                 else
650                         bp->max_vnics = rte_le_to_cpu_16(resp->max_vnics);
651         } else {
652                 bp->max_vnics = 1;
653         }
654         bp->max_stat_ctx = rte_le_to_cpu_16(resp->max_stat_ctx);
655         if (BNXT_PF(bp)) {
656                 bp->pf.total_vnics = rte_le_to_cpu_16(resp->max_vnics);
657                 if (flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_PTP_SUPPORTED) {
658                         bp->flags |= BNXT_FLAG_PTP_SUPPORTED;
659                         PMD_DRV_LOG(DEBUG, "PTP SUPPORTED\n");
660                         HWRM_UNLOCK();
661                         bnxt_hwrm_ptp_qcfg(bp);
662                 }
663         }
664
665         if (flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_EXT_STATS_SUPPORTED)
666                 bp->flags |= BNXT_FLAG_EXT_STATS_SUPPORTED;
667
668         if (flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_ERROR_RECOVERY_CAPABLE) {
669                 bp->flags |= BNXT_FLAG_FW_CAP_ERROR_RECOVERY;
670                 PMD_DRV_LOG(DEBUG, "Adapter Error recovery SUPPORTED\n");
671         } else {
672                 bp->flags &= ~BNXT_FLAG_FW_CAP_ERROR_RECOVERY;
673         }
674
675         if (flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_ERR_RECOVER_RELOAD)
676                 bp->flags |= BNXT_FLAG_FW_CAP_ERR_RECOVER_RELOAD;
677         else
678                 bp->flags &= ~BNXT_FLAG_FW_CAP_ERR_RECOVER_RELOAD;
679
680         HWRM_UNLOCK();
681
682         return rc;
683 }
684
685 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
686 {
687         int rc;
688
689         rc = __bnxt_hwrm_func_qcaps(bp);
690         if (!rc && bp->hwrm_spec_code >= HWRM_SPEC_CODE_1_8_3) {
691                 rc = bnxt_alloc_ctx_mem(bp);
692                 if (rc)
693                         return rc;
694
695                 rc = bnxt_hwrm_func_resc_qcaps(bp);
696                 if (!rc)
697                         bp->flags |= BNXT_FLAG_NEW_RM;
698         }
699
700         return rc;
701 }
702
703 int bnxt_hwrm_func_reset(struct bnxt *bp)
704 {
705         int rc = 0;
706         struct hwrm_func_reset_input req = {.req_type = 0 };
707         struct hwrm_func_reset_output *resp = bp->hwrm_cmd_resp_addr;
708
709         HWRM_PREP(req, FUNC_RESET, BNXT_USE_CHIMP_MB);
710
711         req.enables = rte_cpu_to_le_32(0);
712
713         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
714
715         HWRM_CHECK_RESULT();
716         HWRM_UNLOCK();
717
718         return rc;
719 }
720
721 int bnxt_hwrm_func_driver_register(struct bnxt *bp)
722 {
723         int rc;
724         uint32_t flags = 0;
725         struct hwrm_func_drv_rgtr_input req = {.req_type = 0 };
726         struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
727
728         if (bp->flags & BNXT_FLAG_REGISTERED)
729                 return 0;
730
731         flags = HWRM_FUNC_DRV_RGTR_INPUT_FLAGS_HOT_RESET_SUPPORT;
732         if (bp->flags & BNXT_FLAG_FW_CAP_ERROR_RECOVERY)
733                 flags |= HWRM_FUNC_DRV_RGTR_INPUT_FLAGS_ERROR_RECOVERY_SUPPORT;
734
735         /* PFs and trusted VFs should indicate the support of the
736          * Master capability on non Stingray platform
737          */
738         if ((BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp)) && !BNXT_STINGRAY(bp))
739                 flags |= HWRM_FUNC_DRV_RGTR_INPUT_FLAGS_MASTER_SUPPORT;
740
741         HWRM_PREP(req, FUNC_DRV_RGTR, BNXT_USE_CHIMP_MB);
742         req.enables = rte_cpu_to_le_32(HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_VER |
743                         HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_ASYNC_EVENT_FWD);
744         req.ver_maj = RTE_VER_YEAR;
745         req.ver_min = RTE_VER_MONTH;
746         req.ver_upd = RTE_VER_MINOR;
747
748         if (BNXT_PF(bp)) {
749                 req.enables |= rte_cpu_to_le_32(
750                         HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_VF_REQ_FWD);
751                 memcpy(req.vf_req_fwd, bp->pf.vf_req_fwd,
752                        RTE_MIN(sizeof(req.vf_req_fwd),
753                                sizeof(bp->pf.vf_req_fwd)));
754
755                 /*
756                  * PF can sniff HWRM API issued by VF. This can be set up by
757                  * linux driver and inherited by the DPDK PF driver. Clear
758                  * this HWRM sniffer list in FW because DPDK PF driver does
759                  * not support this.
760                  */
761                 flags |= HWRM_FUNC_DRV_RGTR_INPUT_FLAGS_FWD_NONE_MODE;
762         }
763
764         req.flags = rte_cpu_to_le_32(flags);
765
766         req.async_event_fwd[0] |=
767                 rte_cpu_to_le_32(ASYNC_CMPL_EVENT_ID_LINK_STATUS_CHANGE |
768                                  ASYNC_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED |
769                                  ASYNC_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE |
770                                  ASYNC_CMPL_EVENT_ID_LINK_SPEED_CHANGE |
771                                  ASYNC_CMPL_EVENT_ID_RESET_NOTIFY);
772         if (bp->flags & BNXT_FLAG_FW_CAP_ERROR_RECOVERY)
773                 req.async_event_fwd[0] |=
774                         rte_cpu_to_le_32(ASYNC_CMPL_EVENT_ID_ERROR_RECOVERY);
775         req.async_event_fwd[1] |=
776                 rte_cpu_to_le_32(ASYNC_CMPL_EVENT_ID_PF_DRVR_UNLOAD |
777                                  ASYNC_CMPL_EVENT_ID_VF_CFG_CHANGE);
778
779         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
780
781         HWRM_CHECK_RESULT();
782
783         flags = rte_le_to_cpu_32(resp->flags);
784         if (flags & HWRM_FUNC_DRV_RGTR_OUTPUT_FLAGS_IF_CHANGE_SUPPORTED)
785                 bp->flags |= BNXT_FLAG_FW_CAP_IF_CHANGE;
786
787         HWRM_UNLOCK();
788
789         bp->flags |= BNXT_FLAG_REGISTERED;
790
791         return rc;
792 }
793
794 int bnxt_hwrm_check_vf_rings(struct bnxt *bp)
795 {
796         if (!(BNXT_VF(bp) && (bp->flags & BNXT_FLAG_NEW_RM)))
797                 return 0;
798
799         return bnxt_hwrm_func_reserve_vf_resc(bp, true);
800 }
801
802 int bnxt_hwrm_func_reserve_vf_resc(struct bnxt *bp, bool test)
803 {
804         int rc;
805         uint32_t flags = 0;
806         uint32_t enables;
807         struct hwrm_func_vf_cfg_output *resp = bp->hwrm_cmd_resp_addr;
808         struct hwrm_func_vf_cfg_input req = {0};
809
810         HWRM_PREP(req, FUNC_VF_CFG, BNXT_USE_CHIMP_MB);
811
812         enables = HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_RX_RINGS  |
813                   HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_TX_RINGS   |
814                   HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_STAT_CTXS  |
815                   HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_CMPL_RINGS |
816                   HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_VNICS;
817
818         if (BNXT_HAS_RING_GRPS(bp)) {
819                 enables |= HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_HW_RING_GRPS;
820                 req.num_hw_ring_grps = rte_cpu_to_le_16(bp->rx_nr_rings);
821         }
822
823         req.num_tx_rings = rte_cpu_to_le_16(bp->tx_nr_rings);
824         req.num_rx_rings = rte_cpu_to_le_16(bp->rx_nr_rings *
825                                             AGG_RING_MULTIPLIER);
826         req.num_stat_ctxs = rte_cpu_to_le_16(bp->rx_nr_rings +
827                                              bp->tx_nr_rings +
828                                              BNXT_NUM_ASYNC_CPR(bp));
829         req.num_cmpl_rings = rte_cpu_to_le_16(bp->rx_nr_rings +
830                                               bp->tx_nr_rings +
831                                               BNXT_NUM_ASYNC_CPR(bp));
832         req.num_vnics = rte_cpu_to_le_16(bp->rx_nr_rings);
833         if (bp->vf_resv_strategy ==
834             HWRM_FUNC_RESOURCE_QCAPS_OUTPUT_VF_RESV_STRATEGY_MINIMAL_STATIC) {
835                 enables |= HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_VNICS |
836                            HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_L2_CTXS |
837                            HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_RSSCOS_CTXS;
838                 req.num_rsscos_ctxs = rte_cpu_to_le_16(BNXT_VF_RSV_NUM_RSS_CTX);
839                 req.num_l2_ctxs = rte_cpu_to_le_16(BNXT_VF_RSV_NUM_L2_CTX);
840                 req.num_vnics = rte_cpu_to_le_16(BNXT_VF_RSV_NUM_VNIC);
841         }
842
843         if (test)
844                 flags = HWRM_FUNC_VF_CFG_INPUT_FLAGS_TX_ASSETS_TEST |
845                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_RX_ASSETS_TEST |
846                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_CMPL_ASSETS_TEST |
847                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_RING_GRP_ASSETS_TEST |
848                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_STAT_CTX_ASSETS_TEST |
849                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_VNIC_ASSETS_TEST;
850
851         if (test && BNXT_HAS_RING_GRPS(bp))
852                 flags |= HWRM_FUNC_VF_CFG_INPUT_FLAGS_RING_GRP_ASSETS_TEST;
853
854         req.flags = rte_cpu_to_le_32(flags);
855         req.enables |= rte_cpu_to_le_32(enables);
856
857         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
858
859         if (test)
860                 HWRM_CHECK_RESULT_SILENT();
861         else
862                 HWRM_CHECK_RESULT();
863
864         HWRM_UNLOCK();
865         return rc;
866 }
867
868 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp)
869 {
870         int rc;
871         struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
872         struct hwrm_func_resource_qcaps_input req = {0};
873
874         HWRM_PREP(req, FUNC_RESOURCE_QCAPS, BNXT_USE_CHIMP_MB);
875         req.fid = rte_cpu_to_le_16(0xffff);
876
877         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
878
879         HWRM_CHECK_RESULT();
880
881         if (BNXT_VF(bp)) {
882                 bp->max_rsscos_ctx = rte_le_to_cpu_16(resp->max_rsscos_ctx);
883                 bp->max_cp_rings = rte_le_to_cpu_16(resp->max_cmpl_rings);
884                 bp->max_tx_rings = rte_le_to_cpu_16(resp->max_tx_rings);
885                 bp->max_rx_rings = rte_le_to_cpu_16(resp->max_rx_rings);
886                 bp->max_ring_grps = rte_le_to_cpu_32(resp->max_hw_ring_grps);
887                 /* func_resource_qcaps does not return max_rx_em_flows.
888                  * So use the value provided by func_qcaps.
889                  */
890                 bp->max_l2_ctx = rte_le_to_cpu_16(resp->max_l2_ctxs);
891                 if (!BNXT_CHIP_THOR(bp))
892                         bp->max_l2_ctx += bp->max_rx_em_flows;
893                 bp->max_vnics = rte_le_to_cpu_16(resp->max_vnics);
894                 bp->max_stat_ctx = rte_le_to_cpu_16(resp->max_stat_ctx);
895         }
896         bp->max_nq_rings = rte_le_to_cpu_16(resp->max_msix);
897         bp->vf_resv_strategy = rte_le_to_cpu_16(resp->vf_reservation_strategy);
898         if (bp->vf_resv_strategy >
899             HWRM_FUNC_RESOURCE_QCAPS_OUTPUT_VF_RESV_STRATEGY_MINIMAL_STATIC)
900                 bp->vf_resv_strategy =
901                 HWRM_FUNC_RESOURCE_QCAPS_OUTPUT_VF_RESERVATION_STRATEGY_MAXIMAL;
902
903         HWRM_UNLOCK();
904         return rc;
905 }
906
907 int bnxt_hwrm_ver_get(struct bnxt *bp)
908 {
909         int rc = 0;
910         struct hwrm_ver_get_input req = {.req_type = 0 };
911         struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
912         uint32_t fw_version;
913         uint16_t max_resp_len;
914         char type[RTE_MEMZONE_NAMESIZE];
915         uint32_t dev_caps_cfg;
916
917         bp->max_req_len = HWRM_MAX_REQ_LEN;
918         HWRM_PREP(req, VER_GET, BNXT_USE_CHIMP_MB);
919
920         req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
921         req.hwrm_intf_min = HWRM_VERSION_MINOR;
922         req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
923
924         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
925
926         if (bp->flags & BNXT_FLAG_FW_RESET)
927                 HWRM_CHECK_RESULT_SILENT();
928         else
929                 HWRM_CHECK_RESULT();
930
931         PMD_DRV_LOG(INFO, "%d.%d.%d:%d.%d.%d\n",
932                 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
933                 resp->hwrm_intf_upd_8b, resp->hwrm_fw_maj_8b,
934                 resp->hwrm_fw_min_8b, resp->hwrm_fw_bld_8b);
935         bp->fw_ver = (resp->hwrm_fw_maj_8b << 24) |
936                      (resp->hwrm_fw_min_8b << 16) |
937                      (resp->hwrm_fw_bld_8b << 8) |
938                      resp->hwrm_fw_rsvd_8b;
939         PMD_DRV_LOG(INFO, "Driver HWRM version: %d.%d.%d\n",
940                 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, HWRM_VERSION_UPDATE);
941
942         fw_version = resp->hwrm_intf_maj_8b << 16;
943         fw_version |= resp->hwrm_intf_min_8b << 8;
944         fw_version |= resp->hwrm_intf_upd_8b;
945         bp->hwrm_spec_code = fw_version;
946
947         if (resp->hwrm_intf_maj_8b != HWRM_VERSION_MAJOR) {
948                 PMD_DRV_LOG(ERR, "Unsupported firmware API version\n");
949                 rc = -EINVAL;
950                 goto error;
951         }
952
953         if (bp->max_req_len > resp->max_req_win_len) {
954                 PMD_DRV_LOG(ERR, "Unsupported request length\n");
955                 rc = -EINVAL;
956         }
957         bp->max_req_len = rte_le_to_cpu_16(resp->max_req_win_len);
958         bp->hwrm_max_ext_req_len = rte_le_to_cpu_16(resp->max_ext_req_len);
959         if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
960                 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
961
962         max_resp_len = rte_le_to_cpu_16(resp->max_resp_len);
963         dev_caps_cfg = rte_le_to_cpu_32(resp->dev_caps_cfg);
964
965         if (bp->max_resp_len != max_resp_len) {
966                 sprintf(type, "bnxt_hwrm_%04x:%02x:%02x:%02x",
967                         bp->pdev->addr.domain, bp->pdev->addr.bus,
968                         bp->pdev->addr.devid, bp->pdev->addr.function);
969
970                 rte_free(bp->hwrm_cmd_resp_addr);
971
972                 bp->hwrm_cmd_resp_addr = rte_malloc(type, max_resp_len, 0);
973                 if (bp->hwrm_cmd_resp_addr == NULL) {
974                         rc = -ENOMEM;
975                         goto error;
976                 }
977                 rte_mem_lock_page(bp->hwrm_cmd_resp_addr);
978                 bp->hwrm_cmd_resp_dma_addr =
979                         rte_mem_virt2iova(bp->hwrm_cmd_resp_addr);
980                 if (bp->hwrm_cmd_resp_dma_addr == RTE_BAD_IOVA) {
981                         PMD_DRV_LOG(ERR,
982                         "Unable to map response buffer to physical memory.\n");
983                         rc = -ENOMEM;
984                         goto error;
985                 }
986                 bp->max_resp_len = max_resp_len;
987         }
988
989         if ((dev_caps_cfg &
990                 HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
991             (dev_caps_cfg &
992              HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) {
993                 PMD_DRV_LOG(DEBUG, "Short command supported\n");
994                 bp->flags |= BNXT_FLAG_SHORT_CMD;
995         }
996
997         if (((dev_caps_cfg &
998               HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
999              (dev_caps_cfg &
1000               HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) ||
1001             bp->hwrm_max_ext_req_len > HWRM_MAX_REQ_LEN) {
1002                 sprintf(type, "bnxt_hwrm_short_%04x:%02x:%02x:%02x",
1003                         bp->pdev->addr.domain, bp->pdev->addr.bus,
1004                         bp->pdev->addr.devid, bp->pdev->addr.function);
1005
1006                 rte_free(bp->hwrm_short_cmd_req_addr);
1007
1008                 bp->hwrm_short_cmd_req_addr =
1009                                 rte_malloc(type, bp->hwrm_max_ext_req_len, 0);
1010                 if (bp->hwrm_short_cmd_req_addr == NULL) {
1011                         rc = -ENOMEM;
1012                         goto error;
1013                 }
1014                 rte_mem_lock_page(bp->hwrm_short_cmd_req_addr);
1015                 bp->hwrm_short_cmd_req_dma_addr =
1016                         rte_mem_virt2iova(bp->hwrm_short_cmd_req_addr);
1017                 if (bp->hwrm_short_cmd_req_dma_addr == RTE_BAD_IOVA) {
1018                         rte_free(bp->hwrm_short_cmd_req_addr);
1019                         PMD_DRV_LOG(ERR,
1020                                 "Unable to map buffer to physical memory.\n");
1021                         rc = -ENOMEM;
1022                         goto error;
1023                 }
1024         }
1025         if (dev_caps_cfg &
1026             HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED) {
1027                 bp->flags |= BNXT_FLAG_KONG_MB_EN;
1028                 PMD_DRV_LOG(DEBUG, "Kong mailbox channel enabled\n");
1029         }
1030         if (dev_caps_cfg &
1031             HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
1032                 PMD_DRV_LOG(DEBUG, "FW supports Trusted VFs\n");
1033         if (dev_caps_cfg &
1034             HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED) {
1035                 bp->flags |= BNXT_FLAG_ADV_FLOW_MGMT;
1036                 PMD_DRV_LOG(DEBUG, "FW supports advanced flow management\n");
1037         }
1038
1039 error:
1040         HWRM_UNLOCK();
1041         return rc;
1042 }
1043
1044 int bnxt_hwrm_func_driver_unregister(struct bnxt *bp, uint32_t flags)
1045 {
1046         int rc;
1047         struct hwrm_func_drv_unrgtr_input req = {.req_type = 0 };
1048         struct hwrm_func_drv_unrgtr_output *resp = bp->hwrm_cmd_resp_addr;
1049
1050         if (!(bp->flags & BNXT_FLAG_REGISTERED))
1051                 return 0;
1052
1053         HWRM_PREP(req, FUNC_DRV_UNRGTR, BNXT_USE_CHIMP_MB);
1054         req.flags = flags;
1055
1056         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1057
1058         HWRM_CHECK_RESULT();
1059         HWRM_UNLOCK();
1060
1061         return rc;
1062 }
1063
1064 static int bnxt_hwrm_port_phy_cfg(struct bnxt *bp, struct bnxt_link_info *conf)
1065 {
1066         int rc = 0;
1067         struct hwrm_port_phy_cfg_input req = {0};
1068         struct hwrm_port_phy_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1069         uint32_t enables = 0;
1070
1071         HWRM_PREP(req, PORT_PHY_CFG, BNXT_USE_CHIMP_MB);
1072
1073         if (conf->link_up) {
1074                 /* Setting Fixed Speed. But AutoNeg is ON, So disable it */
1075                 if (bp->link_info.auto_mode && conf->link_speed) {
1076                         req.auto_mode = HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_NONE;
1077                         PMD_DRV_LOG(DEBUG, "Disabling AutoNeg\n");
1078                 }
1079
1080                 req.flags = rte_cpu_to_le_32(conf->phy_flags);
1081                 req.force_link_speed = rte_cpu_to_le_16(conf->link_speed);
1082                 enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_MODE;
1083                 /*
1084                  * Note, ChiMP FW 20.2.1 and 20.2.2 return an error when we set
1085                  * any auto mode, even "none".
1086                  */
1087                 if (!conf->link_speed) {
1088                         /* No speeds specified. Enable AutoNeg - all speeds */
1089                         req.auto_mode =
1090                                 HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_ALL_SPEEDS;
1091                 }
1092                 /* AutoNeg - Advertise speeds specified. */
1093                 if (conf->auto_link_speed_mask &&
1094                     !(conf->phy_flags & HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE)) {
1095                         req.auto_mode =
1096                                 HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_SPEED_MASK;
1097                         req.auto_link_speed_mask =
1098                                 conf->auto_link_speed_mask;
1099                         enables |=
1100                         HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_LINK_SPEED_MASK;
1101                 }
1102
1103                 req.auto_duplex = conf->duplex;
1104                 enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_DUPLEX;
1105                 req.auto_pause = conf->auto_pause;
1106                 req.force_pause = conf->force_pause;
1107                 /* Set force_pause if there is no auto or if there is a force */
1108                 if (req.auto_pause && !req.force_pause)
1109                         enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_PAUSE;
1110                 else
1111                         enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_FORCE_PAUSE;
1112
1113                 req.enables = rte_cpu_to_le_32(enables);
1114         } else {
1115                 req.flags =
1116                 rte_cpu_to_le_32(HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE_LINK_DWN);
1117                 PMD_DRV_LOG(INFO, "Force Link Down\n");
1118         }
1119
1120         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1121
1122         HWRM_CHECK_RESULT();
1123         HWRM_UNLOCK();
1124
1125         return rc;
1126 }
1127
1128 static int bnxt_hwrm_port_phy_qcfg(struct bnxt *bp,
1129                                    struct bnxt_link_info *link_info)
1130 {
1131         int rc = 0;
1132         struct hwrm_port_phy_qcfg_input req = {0};
1133         struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
1134
1135         HWRM_PREP(req, PORT_PHY_QCFG, BNXT_USE_CHIMP_MB);
1136
1137         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1138
1139         HWRM_CHECK_RESULT();
1140
1141         link_info->phy_link_status = resp->link;
1142         link_info->link_up =
1143                 (link_info->phy_link_status ==
1144                  HWRM_PORT_PHY_QCFG_OUTPUT_LINK_LINK) ? 1 : 0;
1145         link_info->link_speed = rte_le_to_cpu_16(resp->link_speed);
1146         link_info->duplex = resp->duplex_cfg;
1147         link_info->pause = resp->pause;
1148         link_info->auto_pause = resp->auto_pause;
1149         link_info->force_pause = resp->force_pause;
1150         link_info->auto_mode = resp->auto_mode;
1151         link_info->phy_type = resp->phy_type;
1152         link_info->media_type = resp->media_type;
1153
1154         link_info->support_speeds = rte_le_to_cpu_16(resp->support_speeds);
1155         link_info->auto_link_speed = rte_le_to_cpu_16(resp->auto_link_speed);
1156         link_info->preemphasis = rte_le_to_cpu_32(resp->preemphasis);
1157         link_info->force_link_speed = rte_le_to_cpu_16(resp->force_link_speed);
1158         link_info->phy_ver[0] = resp->phy_maj;
1159         link_info->phy_ver[1] = resp->phy_min;
1160         link_info->phy_ver[2] = resp->phy_bld;
1161
1162         HWRM_UNLOCK();
1163
1164         PMD_DRV_LOG(DEBUG, "Link Speed %d\n", link_info->link_speed);
1165         PMD_DRV_LOG(DEBUG, "Auto Mode %d\n", link_info->auto_mode);
1166         PMD_DRV_LOG(DEBUG, "Support Speeds %x\n", link_info->support_speeds);
1167         PMD_DRV_LOG(DEBUG, "Auto Link Speed %x\n", link_info->auto_link_speed);
1168         PMD_DRV_LOG(DEBUG, "Auto Link Speed Mask %x\n",
1169                     link_info->auto_link_speed_mask);
1170         PMD_DRV_LOG(DEBUG, "Forced Link Speed %x\n",
1171                     link_info->force_link_speed);
1172
1173         return rc;
1174 }
1175
1176 int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
1177 {
1178         int rc = 0;
1179         struct hwrm_queue_qportcfg_input req = {.req_type = 0 };
1180         struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
1181         int i;
1182
1183         HWRM_PREP(req, QUEUE_QPORTCFG, BNXT_USE_CHIMP_MB);
1184
1185         req.flags = HWRM_QUEUE_QPORTCFG_INPUT_FLAGS_PATH_TX;
1186         /* HWRM Version >= 1.9.1 */
1187         if (bp->hwrm_spec_code >= HWRM_VERSION_1_9_1)
1188                 req.drv_qmap_cap =
1189                         HWRM_QUEUE_QPORTCFG_INPUT_DRV_QMAP_CAP_ENABLED;
1190         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1191
1192         HWRM_CHECK_RESULT();
1193
1194 #define GET_QUEUE_INFO(x) \
1195         bp->cos_queue[x].id = resp->queue_id##x; \
1196         bp->cos_queue[x].profile = resp->queue_id##x##_service_profile
1197
1198         GET_QUEUE_INFO(0);
1199         GET_QUEUE_INFO(1);
1200         GET_QUEUE_INFO(2);
1201         GET_QUEUE_INFO(3);
1202         GET_QUEUE_INFO(4);
1203         GET_QUEUE_INFO(5);
1204         GET_QUEUE_INFO(6);
1205         GET_QUEUE_INFO(7);
1206
1207         HWRM_UNLOCK();
1208
1209         if (bp->hwrm_spec_code < HWRM_VERSION_1_9_1) {
1210                 bp->tx_cosq_id = bp->cos_queue[0].id;
1211         } else {
1212                 /* iterate and find the COSq profile to use for Tx */
1213                 for (i = 0; i < BNXT_COS_QUEUE_COUNT; i++) {
1214                         if (bp->cos_queue[i].profile ==
1215                                 HWRM_QUEUE_SERVICE_PROFILE_LOSSY) {
1216                                 bp->tx_cosq_id = bp->cos_queue[i].id;
1217                                 break;
1218                         }
1219                 }
1220         }
1221
1222         bp->max_tc = resp->max_configurable_queues;
1223         bp->max_lltc = resp->max_configurable_lossless_queues;
1224         if (bp->max_tc > BNXT_MAX_QUEUE)
1225                 bp->max_tc = BNXT_MAX_QUEUE;
1226         bp->max_q = bp->max_tc;
1227
1228         PMD_DRV_LOG(DEBUG, "Tx Cos Queue to use: %d\n", bp->tx_cosq_id);
1229
1230         return rc;
1231 }
1232
1233 int bnxt_hwrm_ring_alloc(struct bnxt *bp,
1234                          struct bnxt_ring *ring,
1235                          uint32_t ring_type, uint32_t map_index,
1236                          uint32_t stats_ctx_id, uint32_t cmpl_ring_id)
1237 {
1238         int rc = 0;
1239         uint32_t enables = 0;
1240         struct hwrm_ring_alloc_input req = {.req_type = 0 };
1241         struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
1242         struct rte_mempool *mb_pool;
1243         uint16_t rx_buf_size;
1244
1245         HWRM_PREP(req, RING_ALLOC, BNXT_USE_CHIMP_MB);
1246
1247         req.page_tbl_addr = rte_cpu_to_le_64(ring->bd_dma);
1248         req.fbo = rte_cpu_to_le_32(0);
1249         /* Association of ring index with doorbell index */
1250         req.logical_id = rte_cpu_to_le_16(map_index);
1251         req.length = rte_cpu_to_le_32(ring->ring_size);
1252
1253         switch (ring_type) {
1254         case HWRM_RING_ALLOC_INPUT_RING_TYPE_TX:
1255                 req.ring_type = ring_type;
1256                 req.cmpl_ring_id = rte_cpu_to_le_16(cmpl_ring_id);
1257                 req.stat_ctx_id = rte_cpu_to_le_32(stats_ctx_id);
1258                 req.queue_id = rte_cpu_to_le_16(bp->tx_cosq_id);
1259                 if (stats_ctx_id != INVALID_STATS_CTX_ID)
1260                         enables |=
1261                         HWRM_RING_ALLOC_INPUT_ENABLES_STAT_CTX_ID_VALID;
1262                 break;
1263         case HWRM_RING_ALLOC_INPUT_RING_TYPE_RX:
1264                 req.ring_type = ring_type;
1265                 req.cmpl_ring_id = rte_cpu_to_le_16(cmpl_ring_id);
1266                 req.stat_ctx_id = rte_cpu_to_le_32(stats_ctx_id);
1267                 if (BNXT_CHIP_THOR(bp)) {
1268                         mb_pool = bp->rx_queues[0]->mb_pool;
1269                         rx_buf_size = rte_pktmbuf_data_room_size(mb_pool) -
1270                                       RTE_PKTMBUF_HEADROOM;
1271                         rx_buf_size = RTE_MIN(BNXT_MAX_PKT_LEN, rx_buf_size);
1272                         req.rx_buf_size = rte_cpu_to_le_16(rx_buf_size);
1273                         enables |=
1274                                 HWRM_RING_ALLOC_INPUT_ENABLES_RX_BUF_SIZE_VALID;
1275                 }
1276                 if (stats_ctx_id != INVALID_STATS_CTX_ID)
1277                         enables |=
1278                                 HWRM_RING_ALLOC_INPUT_ENABLES_STAT_CTX_ID_VALID;
1279                 break;
1280         case HWRM_RING_ALLOC_INPUT_RING_TYPE_L2_CMPL:
1281                 req.ring_type = ring_type;
1282                 if (BNXT_HAS_NQ(bp)) {
1283                         /* Association of cp ring with nq */
1284                         req.nq_ring_id = rte_cpu_to_le_16(cmpl_ring_id);
1285                         enables |=
1286                                 HWRM_RING_ALLOC_INPUT_ENABLES_NQ_RING_ID_VALID;
1287                 }
1288                 req.int_mode = HWRM_RING_ALLOC_INPUT_INT_MODE_MSIX;
1289                 break;
1290         case HWRM_RING_ALLOC_INPUT_RING_TYPE_NQ:
1291                 req.ring_type = ring_type;
1292                 req.page_size = BNXT_PAGE_SHFT;
1293                 req.int_mode = HWRM_RING_ALLOC_INPUT_INT_MODE_MSIX;
1294                 break;
1295         case HWRM_RING_ALLOC_INPUT_RING_TYPE_RX_AGG:
1296                 req.ring_type = ring_type;
1297                 req.rx_ring_id = rte_cpu_to_le_16(ring->fw_rx_ring_id);
1298
1299                 mb_pool = bp->rx_queues[0]->mb_pool;
1300                 rx_buf_size = rte_pktmbuf_data_room_size(mb_pool) -
1301                               RTE_PKTMBUF_HEADROOM;
1302                 rx_buf_size = RTE_MIN(BNXT_MAX_PKT_LEN, rx_buf_size);
1303                 req.rx_buf_size = rte_cpu_to_le_16(rx_buf_size);
1304
1305                 req.stat_ctx_id = rte_cpu_to_le_32(stats_ctx_id);
1306                 enables |= HWRM_RING_ALLOC_INPUT_ENABLES_RX_RING_ID_VALID |
1307                            HWRM_RING_ALLOC_INPUT_ENABLES_RX_BUF_SIZE_VALID |
1308                            HWRM_RING_ALLOC_INPUT_ENABLES_STAT_CTX_ID_VALID;
1309                 break;
1310         default:
1311                 PMD_DRV_LOG(ERR, "hwrm alloc invalid ring type %d\n",
1312                         ring_type);
1313                 HWRM_UNLOCK();
1314                 return -EINVAL;
1315         }
1316         req.enables = rte_cpu_to_le_32(enables);
1317
1318         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1319
1320         if (rc || resp->error_code) {
1321                 if (rc == 0 && resp->error_code)
1322                         rc = rte_le_to_cpu_16(resp->error_code);
1323                 switch (ring_type) {
1324                 case HWRM_RING_ALLOC_INPUT_RING_TYPE_L2_CMPL:
1325                         PMD_DRV_LOG(ERR,
1326                                 "hwrm_ring_alloc cp failed. rc:%d\n", rc);
1327                         HWRM_UNLOCK();
1328                         return rc;
1329                 case HWRM_RING_ALLOC_INPUT_RING_TYPE_RX:
1330                         PMD_DRV_LOG(ERR,
1331                                     "hwrm_ring_alloc rx failed. rc:%d\n", rc);
1332                         HWRM_UNLOCK();
1333                         return rc;
1334                 case HWRM_RING_ALLOC_INPUT_RING_TYPE_RX_AGG:
1335                         PMD_DRV_LOG(ERR,
1336                                     "hwrm_ring_alloc rx agg failed. rc:%d\n",
1337                                     rc);
1338                         HWRM_UNLOCK();
1339                         return rc;
1340                 case HWRM_RING_ALLOC_INPUT_RING_TYPE_TX:
1341                         PMD_DRV_LOG(ERR,
1342                                     "hwrm_ring_alloc tx failed. rc:%d\n", rc);
1343                         HWRM_UNLOCK();
1344                         return rc;
1345                 case HWRM_RING_ALLOC_INPUT_RING_TYPE_NQ:
1346                         PMD_DRV_LOG(ERR,
1347                                     "hwrm_ring_alloc nq failed. rc:%d\n", rc);
1348                         HWRM_UNLOCK();
1349                         return rc;
1350                 default:
1351                         PMD_DRV_LOG(ERR, "Invalid ring. rc:%d\n", rc);
1352                         HWRM_UNLOCK();
1353                         return rc;
1354                 }
1355         }
1356
1357         ring->fw_ring_id = rte_le_to_cpu_16(resp->ring_id);
1358         HWRM_UNLOCK();
1359         return rc;
1360 }
1361
1362 int bnxt_hwrm_ring_free(struct bnxt *bp,
1363                         struct bnxt_ring *ring, uint32_t ring_type)
1364 {
1365         int rc;
1366         struct hwrm_ring_free_input req = {.req_type = 0 };
1367         struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
1368
1369         HWRM_PREP(req, RING_FREE, BNXT_USE_CHIMP_MB);
1370
1371         req.ring_type = ring_type;
1372         req.ring_id = rte_cpu_to_le_16(ring->fw_ring_id);
1373
1374         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1375
1376         if (rc || resp->error_code) {
1377                 if (rc == 0 && resp->error_code)
1378                         rc = rte_le_to_cpu_16(resp->error_code);
1379                 HWRM_UNLOCK();
1380
1381                 switch (ring_type) {
1382                 case HWRM_RING_FREE_INPUT_RING_TYPE_L2_CMPL:
1383                         PMD_DRV_LOG(ERR, "hwrm_ring_free cp failed. rc:%d\n",
1384                                 rc);
1385                         return rc;
1386                 case HWRM_RING_FREE_INPUT_RING_TYPE_RX:
1387                         PMD_DRV_LOG(ERR, "hwrm_ring_free rx failed. rc:%d\n",
1388                                 rc);
1389                         return rc;
1390                 case HWRM_RING_FREE_INPUT_RING_TYPE_TX:
1391                         PMD_DRV_LOG(ERR, "hwrm_ring_free tx failed. rc:%d\n",
1392                                 rc);
1393                         return rc;
1394                 case HWRM_RING_FREE_INPUT_RING_TYPE_NQ:
1395                         PMD_DRV_LOG(ERR,
1396                                     "hwrm_ring_free nq failed. rc:%d\n", rc);
1397                         return rc;
1398                 case HWRM_RING_FREE_INPUT_RING_TYPE_RX_AGG:
1399                         PMD_DRV_LOG(ERR,
1400                                     "hwrm_ring_free agg failed. rc:%d\n", rc);
1401                         return rc;
1402                 default:
1403                         PMD_DRV_LOG(ERR, "Invalid ring, rc:%d\n", rc);
1404                         return rc;
1405                 }
1406         }
1407         HWRM_UNLOCK();
1408         return 0;
1409 }
1410
1411 int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp, unsigned int idx)
1412 {
1413         int rc = 0;
1414         struct hwrm_ring_grp_alloc_input req = {.req_type = 0 };
1415         struct hwrm_ring_grp_alloc_output *resp = bp->hwrm_cmd_resp_addr;
1416
1417         HWRM_PREP(req, RING_GRP_ALLOC, BNXT_USE_CHIMP_MB);
1418
1419         req.cr = rte_cpu_to_le_16(bp->grp_info[idx].cp_fw_ring_id);
1420         req.rr = rte_cpu_to_le_16(bp->grp_info[idx].rx_fw_ring_id);
1421         req.ar = rte_cpu_to_le_16(bp->grp_info[idx].ag_fw_ring_id);
1422         req.sc = rte_cpu_to_le_16(bp->grp_info[idx].fw_stats_ctx);
1423
1424         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1425
1426         HWRM_CHECK_RESULT();
1427
1428         bp->grp_info[idx].fw_grp_id =
1429             rte_le_to_cpu_16(resp->ring_group_id);
1430
1431         HWRM_UNLOCK();
1432
1433         return rc;
1434 }
1435
1436 int bnxt_hwrm_ring_grp_free(struct bnxt *bp, unsigned int idx)
1437 {
1438         int rc;
1439         struct hwrm_ring_grp_free_input req = {.req_type = 0 };
1440         struct hwrm_ring_grp_free_output *resp = bp->hwrm_cmd_resp_addr;
1441
1442         HWRM_PREP(req, RING_GRP_FREE, BNXT_USE_CHIMP_MB);
1443
1444         req.ring_group_id = rte_cpu_to_le_16(bp->grp_info[idx].fw_grp_id);
1445
1446         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1447
1448         HWRM_CHECK_RESULT();
1449         HWRM_UNLOCK();
1450
1451         bp->grp_info[idx].fw_grp_id = INVALID_HW_RING_ID;
1452         return rc;
1453 }
1454
1455 int bnxt_hwrm_stat_clear(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1456 {
1457         int rc = 0;
1458         struct hwrm_stat_ctx_clr_stats_input req = {.req_type = 0 };
1459         struct hwrm_stat_ctx_clr_stats_output *resp = bp->hwrm_cmd_resp_addr;
1460
1461         if (cpr->hw_stats_ctx_id == (uint32_t)HWRM_NA_SIGNATURE)
1462                 return rc;
1463
1464         HWRM_PREP(req, STAT_CTX_CLR_STATS, BNXT_USE_CHIMP_MB);
1465
1466         req.stat_ctx_id = rte_cpu_to_le_32(cpr->hw_stats_ctx_id);
1467
1468         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1469
1470         HWRM_CHECK_RESULT();
1471         HWRM_UNLOCK();
1472
1473         return rc;
1474 }
1475
1476 int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1477                                 unsigned int idx __rte_unused)
1478 {
1479         int rc;
1480         struct hwrm_stat_ctx_alloc_input req = {.req_type = 0 };
1481         struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
1482
1483         HWRM_PREP(req, STAT_CTX_ALLOC, BNXT_USE_CHIMP_MB);
1484
1485         req.update_period_ms = rte_cpu_to_le_32(0);
1486
1487         req.stats_dma_addr =
1488             rte_cpu_to_le_64(cpr->hw_stats_map);
1489
1490         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1491
1492         HWRM_CHECK_RESULT();
1493
1494         cpr->hw_stats_ctx_id = rte_le_to_cpu_32(resp->stat_ctx_id);
1495
1496         HWRM_UNLOCK();
1497
1498         return rc;
1499 }
1500
1501 int bnxt_hwrm_stat_ctx_free(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1502                                 unsigned int idx __rte_unused)
1503 {
1504         int rc;
1505         struct hwrm_stat_ctx_free_input req = {.req_type = 0 };
1506         struct hwrm_stat_ctx_free_output *resp = bp->hwrm_cmd_resp_addr;
1507
1508         HWRM_PREP(req, STAT_CTX_FREE, BNXT_USE_CHIMP_MB);
1509
1510         req.stat_ctx_id = rte_cpu_to_le_32(cpr->hw_stats_ctx_id);
1511
1512         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1513
1514         HWRM_CHECK_RESULT();
1515         HWRM_UNLOCK();
1516
1517         return rc;
1518 }
1519
1520 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1521 {
1522         int rc = 0, i, j;
1523         struct hwrm_vnic_alloc_input req = { 0 };
1524         struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
1525
1526         if (!BNXT_HAS_RING_GRPS(bp))
1527                 goto skip_ring_grps;
1528
1529         /* map ring groups to this vnic */
1530         PMD_DRV_LOG(DEBUG, "Alloc VNIC. Start %x, End %x\n",
1531                 vnic->start_grp_id, vnic->end_grp_id);
1532         for (i = vnic->start_grp_id, j = 0; i < vnic->end_grp_id; i++, j++)
1533                 vnic->fw_grp_ids[j] = bp->grp_info[i].fw_grp_id;
1534
1535         vnic->dflt_ring_grp = bp->grp_info[vnic->start_grp_id].fw_grp_id;
1536         vnic->rss_rule = (uint16_t)HWRM_NA_SIGNATURE;
1537         vnic->cos_rule = (uint16_t)HWRM_NA_SIGNATURE;
1538         vnic->lb_rule = (uint16_t)HWRM_NA_SIGNATURE;
1539
1540 skip_ring_grps:
1541         vnic->mru = bp->eth_dev->data->mtu + RTE_ETHER_HDR_LEN +
1542                                 RTE_ETHER_CRC_LEN + VLAN_TAG_SIZE;
1543         HWRM_PREP(req, VNIC_ALLOC, BNXT_USE_CHIMP_MB);
1544
1545         if (vnic->func_default)
1546                 req.flags =
1547                         rte_cpu_to_le_32(HWRM_VNIC_ALLOC_INPUT_FLAGS_DEFAULT);
1548         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1549
1550         HWRM_CHECK_RESULT();
1551
1552         vnic->fw_vnic_id = rte_le_to_cpu_16(resp->vnic_id);
1553         HWRM_UNLOCK();
1554         PMD_DRV_LOG(DEBUG, "VNIC ID %x\n", vnic->fw_vnic_id);
1555         return rc;
1556 }
1557
1558 static int bnxt_hwrm_vnic_plcmodes_qcfg(struct bnxt *bp,
1559                                         struct bnxt_vnic_info *vnic,
1560                                         struct bnxt_plcmodes_cfg *pmode)
1561 {
1562         int rc = 0;
1563         struct hwrm_vnic_plcmodes_qcfg_input req = {.req_type = 0 };
1564         struct hwrm_vnic_plcmodes_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
1565
1566         HWRM_PREP(req, VNIC_PLCMODES_QCFG, BNXT_USE_CHIMP_MB);
1567
1568         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1569
1570         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1571
1572         HWRM_CHECK_RESULT();
1573
1574         pmode->flags = rte_le_to_cpu_32(resp->flags);
1575         /* dflt_vnic bit doesn't exist in the _cfg command */
1576         pmode->flags &= ~(HWRM_VNIC_PLCMODES_QCFG_OUTPUT_FLAGS_DFLT_VNIC);
1577         pmode->jumbo_thresh = rte_le_to_cpu_16(resp->jumbo_thresh);
1578         pmode->hds_offset = rte_le_to_cpu_16(resp->hds_offset);
1579         pmode->hds_threshold = rte_le_to_cpu_16(resp->hds_threshold);
1580
1581         HWRM_UNLOCK();
1582
1583         return rc;
1584 }
1585
1586 static int bnxt_hwrm_vnic_plcmodes_cfg(struct bnxt *bp,
1587                                        struct bnxt_vnic_info *vnic,
1588                                        struct bnxt_plcmodes_cfg *pmode)
1589 {
1590         int rc = 0;
1591         struct hwrm_vnic_plcmodes_cfg_input req = {.req_type = 0 };
1592         struct hwrm_vnic_plcmodes_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1593
1594         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
1595                 PMD_DRV_LOG(DEBUG, "VNIC ID %x\n", vnic->fw_vnic_id);
1596                 return rc;
1597         }
1598
1599         HWRM_PREP(req, VNIC_PLCMODES_CFG, BNXT_USE_CHIMP_MB);
1600
1601         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1602         req.flags = rte_cpu_to_le_32(pmode->flags);
1603         req.jumbo_thresh = rte_cpu_to_le_16(pmode->jumbo_thresh);
1604         req.hds_offset = rte_cpu_to_le_16(pmode->hds_offset);
1605         req.hds_threshold = rte_cpu_to_le_16(pmode->hds_threshold);
1606         req.enables = rte_cpu_to_le_32(
1607             HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_HDS_THRESHOLD_VALID |
1608             HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_HDS_OFFSET_VALID |
1609             HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_JUMBO_THRESH_VALID
1610         );
1611
1612         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1613
1614         HWRM_CHECK_RESULT();
1615         HWRM_UNLOCK();
1616
1617         return rc;
1618 }
1619
1620 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1621 {
1622         int rc = 0;
1623         struct hwrm_vnic_cfg_input req = {.req_type = 0 };
1624         struct hwrm_vnic_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1625         struct bnxt_plcmodes_cfg pmodes = { 0 };
1626         uint32_t ctx_enable_flag = 0;
1627         uint32_t enables = 0;
1628
1629         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
1630                 PMD_DRV_LOG(DEBUG, "VNIC ID %x\n", vnic->fw_vnic_id);
1631                 return rc;
1632         }
1633
1634         rc = bnxt_hwrm_vnic_plcmodes_qcfg(bp, vnic, &pmodes);
1635         if (rc)
1636                 return rc;
1637
1638         HWRM_PREP(req, VNIC_CFG, BNXT_USE_CHIMP_MB);
1639
1640         if (BNXT_CHIP_THOR(bp)) {
1641                 struct bnxt_rx_queue *rxq = bp->eth_dev->data->rx_queues[0];
1642                 struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
1643                 struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
1644
1645                 req.default_rx_ring_id =
1646                         rte_cpu_to_le_16(rxr->rx_ring_struct->fw_ring_id);
1647                 req.default_cmpl_ring_id =
1648                         rte_cpu_to_le_16(cpr->cp_ring_struct->fw_ring_id);
1649                 enables = HWRM_VNIC_CFG_INPUT_ENABLES_DEFAULT_RX_RING_ID |
1650                           HWRM_VNIC_CFG_INPUT_ENABLES_DEFAULT_CMPL_RING_ID;
1651                 goto config_mru;
1652         }
1653
1654         /* Only RSS support for now TBD: COS & LB */
1655         enables = HWRM_VNIC_CFG_INPUT_ENABLES_DFLT_RING_GRP;
1656         if (vnic->lb_rule != 0xffff)
1657                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_LB_RULE;
1658         if (vnic->cos_rule != 0xffff)
1659                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_COS_RULE;
1660         if (vnic->rss_rule != (uint16_t)HWRM_NA_SIGNATURE) {
1661                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_MRU;
1662                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_RSS_RULE;
1663         }
1664         enables |= ctx_enable_flag;
1665         req.dflt_ring_grp = rte_cpu_to_le_16(vnic->dflt_ring_grp);
1666         req.rss_rule = rte_cpu_to_le_16(vnic->rss_rule);
1667         req.cos_rule = rte_cpu_to_le_16(vnic->cos_rule);
1668         req.lb_rule = rte_cpu_to_le_16(vnic->lb_rule);
1669
1670 config_mru:
1671         req.enables = rte_cpu_to_le_32(enables);
1672         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1673         req.mru = rte_cpu_to_le_16(vnic->mru);
1674         /* Configure default VNIC only once. */
1675         if (vnic->func_default && !(bp->flags & BNXT_FLAG_DFLT_VNIC_SET)) {
1676                 req.flags |=
1677                     rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_FLAGS_DEFAULT);
1678                 bp->flags |= BNXT_FLAG_DFLT_VNIC_SET;
1679         }
1680         if (vnic->vlan_strip)
1681                 req.flags |=
1682                     rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_FLAGS_VLAN_STRIP_MODE);
1683         if (vnic->bd_stall)
1684                 req.flags |=
1685                     rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_FLAGS_BD_STALL_MODE);
1686         if (vnic->roce_dual)
1687                 req.flags |= rte_cpu_to_le_32(
1688                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_ROCE_DUAL_VNIC_MODE);
1689         if (vnic->roce_only)
1690                 req.flags |= rte_cpu_to_le_32(
1691                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_ROCE_ONLY_VNIC_MODE);
1692         if (vnic->rss_dflt_cr)
1693                 req.flags |= rte_cpu_to_le_32(
1694                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_RSS_DFLT_CR_MODE);
1695
1696         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1697
1698         HWRM_CHECK_RESULT();
1699         HWRM_UNLOCK();
1700
1701         rc = bnxt_hwrm_vnic_plcmodes_cfg(bp, vnic, &pmodes);
1702
1703         return rc;
1704 }
1705
1706 int bnxt_hwrm_vnic_qcfg(struct bnxt *bp, struct bnxt_vnic_info *vnic,
1707                 int16_t fw_vf_id)
1708 {
1709         int rc = 0;
1710         struct hwrm_vnic_qcfg_input req = {.req_type = 0 };
1711         struct hwrm_vnic_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
1712
1713         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
1714                 PMD_DRV_LOG(DEBUG, "VNIC QCFG ID %d\n", vnic->fw_vnic_id);
1715                 return rc;
1716         }
1717         HWRM_PREP(req, VNIC_QCFG, BNXT_USE_CHIMP_MB);
1718
1719         req.enables =
1720                 rte_cpu_to_le_32(HWRM_VNIC_QCFG_INPUT_ENABLES_VF_ID_VALID);
1721         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1722         req.vf_id = rte_cpu_to_le_16(fw_vf_id);
1723
1724         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1725
1726         HWRM_CHECK_RESULT();
1727
1728         vnic->dflt_ring_grp = rte_le_to_cpu_16(resp->dflt_ring_grp);
1729         vnic->rss_rule = rte_le_to_cpu_16(resp->rss_rule);
1730         vnic->cos_rule = rte_le_to_cpu_16(resp->cos_rule);
1731         vnic->lb_rule = rte_le_to_cpu_16(resp->lb_rule);
1732         vnic->mru = rte_le_to_cpu_16(resp->mru);
1733         vnic->func_default = rte_le_to_cpu_32(
1734                         resp->flags) & HWRM_VNIC_QCFG_OUTPUT_FLAGS_DEFAULT;
1735         vnic->vlan_strip = rte_le_to_cpu_32(resp->flags) &
1736                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_VLAN_STRIP_MODE;
1737         vnic->bd_stall = rte_le_to_cpu_32(resp->flags) &
1738                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_BD_STALL_MODE;
1739         vnic->roce_dual = rte_le_to_cpu_32(resp->flags) &
1740                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_ROCE_DUAL_VNIC_MODE;
1741         vnic->roce_only = rte_le_to_cpu_32(resp->flags) &
1742                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_ROCE_ONLY_VNIC_MODE;
1743         vnic->rss_dflt_cr = rte_le_to_cpu_32(resp->flags) &
1744                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_RSS_DFLT_CR_MODE;
1745
1746         HWRM_UNLOCK();
1747
1748         return rc;
1749 }
1750
1751 int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
1752                              struct bnxt_vnic_info *vnic, uint16_t ctx_idx)
1753 {
1754         int rc = 0;
1755         uint16_t ctx_id;
1756         struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {.req_type = 0 };
1757         struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
1758                                                 bp->hwrm_cmd_resp_addr;
1759
1760         HWRM_PREP(req, VNIC_RSS_COS_LB_CTX_ALLOC, BNXT_USE_CHIMP_MB);
1761
1762         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1763         HWRM_CHECK_RESULT();
1764
1765         ctx_id = rte_le_to_cpu_16(resp->rss_cos_lb_ctx_id);
1766         if (!BNXT_HAS_RING_GRPS(bp))
1767                 vnic->fw_grp_ids[ctx_idx] = ctx_id;
1768         else if (ctx_idx == 0)
1769                 vnic->rss_rule = ctx_id;
1770
1771         HWRM_UNLOCK();
1772
1773         return rc;
1774 }
1775
1776 static
1777 int _bnxt_hwrm_vnic_ctx_free(struct bnxt *bp,
1778                              struct bnxt_vnic_info *vnic, uint16_t ctx_idx)
1779 {
1780         int rc = 0;
1781         struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {.req_type = 0 };
1782         struct hwrm_vnic_rss_cos_lb_ctx_free_output *resp =
1783                                                 bp->hwrm_cmd_resp_addr;
1784
1785         if (ctx_idx == (uint16_t)HWRM_NA_SIGNATURE) {
1786                 PMD_DRV_LOG(DEBUG, "VNIC RSS Rule %x\n", vnic->rss_rule);
1787                 return rc;
1788         }
1789         HWRM_PREP(req, VNIC_RSS_COS_LB_CTX_FREE, BNXT_USE_CHIMP_MB);
1790
1791         req.rss_cos_lb_ctx_id = rte_cpu_to_le_16(ctx_idx);
1792
1793         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1794
1795         HWRM_CHECK_RESULT();
1796         HWRM_UNLOCK();
1797
1798         return rc;
1799 }
1800
1801 int bnxt_hwrm_vnic_ctx_free(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1802 {
1803         int rc = 0;
1804
1805         if (BNXT_CHIP_THOR(bp)) {
1806                 int j;
1807
1808                 for (j = 0; j < vnic->num_lb_ctxts; j++) {
1809                         rc = _bnxt_hwrm_vnic_ctx_free(bp,
1810                                                       vnic,
1811                                                       vnic->fw_grp_ids[j]);
1812                         vnic->fw_grp_ids[j] = INVALID_HW_RING_ID;
1813                 }
1814                 vnic->num_lb_ctxts = 0;
1815         } else {
1816                 rc = _bnxt_hwrm_vnic_ctx_free(bp, vnic, vnic->rss_rule);
1817                 vnic->rss_rule = INVALID_HW_RING_ID;
1818         }
1819
1820         return rc;
1821 }
1822
1823 int bnxt_hwrm_vnic_free(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1824 {
1825         int rc = 0;
1826         struct hwrm_vnic_free_input req = {.req_type = 0 };
1827         struct hwrm_vnic_free_output *resp = bp->hwrm_cmd_resp_addr;
1828
1829         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
1830                 PMD_DRV_LOG(DEBUG, "VNIC FREE ID %x\n", vnic->fw_vnic_id);
1831                 return rc;
1832         }
1833
1834         HWRM_PREP(req, VNIC_FREE, BNXT_USE_CHIMP_MB);
1835
1836         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1837
1838         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1839
1840         HWRM_CHECK_RESULT();
1841         HWRM_UNLOCK();
1842
1843         vnic->fw_vnic_id = INVALID_HW_RING_ID;
1844         /* Configure default VNIC again if necessary. */
1845         if (vnic->func_default && (bp->flags & BNXT_FLAG_DFLT_VNIC_SET))
1846                 bp->flags &= ~BNXT_FLAG_DFLT_VNIC_SET;
1847
1848         return rc;
1849 }
1850
1851 static int
1852 bnxt_hwrm_vnic_rss_cfg_thor(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1853 {
1854         int i;
1855         int rc = 0;
1856         int nr_ctxs = vnic->num_lb_ctxts;
1857         struct hwrm_vnic_rss_cfg_input req = {.req_type = 0 };
1858         struct hwrm_vnic_rss_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1859
1860         for (i = 0; i < nr_ctxs; i++) {
1861                 HWRM_PREP(req, VNIC_RSS_CFG, BNXT_USE_CHIMP_MB);
1862
1863                 req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1864                 req.hash_type = rte_cpu_to_le_32(vnic->hash_type);
1865                 req.hash_mode_flags = vnic->hash_mode;
1866
1867                 req.hash_key_tbl_addr =
1868                         rte_cpu_to_le_64(vnic->rss_hash_key_dma_addr);
1869
1870                 req.ring_grp_tbl_addr =
1871                         rte_cpu_to_le_64(vnic->rss_table_dma_addr +
1872                                          i * HW_HASH_INDEX_SIZE);
1873                 req.ring_table_pair_index = i;
1874                 req.rss_ctx_idx = rte_cpu_to_le_16(vnic->fw_grp_ids[i]);
1875
1876                 rc = bnxt_hwrm_send_message(bp, &req, sizeof(req),
1877                                             BNXT_USE_CHIMP_MB);
1878
1879                 HWRM_CHECK_RESULT();
1880                 HWRM_UNLOCK();
1881         }
1882
1883         return rc;
1884 }
1885
1886 int bnxt_hwrm_vnic_rss_cfg(struct bnxt *bp,
1887                            struct bnxt_vnic_info *vnic)
1888 {
1889         int rc = 0;
1890         struct hwrm_vnic_rss_cfg_input req = {.req_type = 0 };
1891         struct hwrm_vnic_rss_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1892
1893         if (!vnic->rss_table)
1894                 return 0;
1895
1896         if (BNXT_CHIP_THOR(bp))
1897                 return bnxt_hwrm_vnic_rss_cfg_thor(bp, vnic);
1898
1899         HWRM_PREP(req, VNIC_RSS_CFG, BNXT_USE_CHIMP_MB);
1900
1901         req.hash_type = rte_cpu_to_le_32(vnic->hash_type);
1902         req.hash_mode_flags = vnic->hash_mode;
1903
1904         req.ring_grp_tbl_addr =
1905             rte_cpu_to_le_64(vnic->rss_table_dma_addr);
1906         req.hash_key_tbl_addr =
1907             rte_cpu_to_le_64(vnic->rss_hash_key_dma_addr);
1908         req.rss_ctx_idx = rte_cpu_to_le_16(vnic->rss_rule);
1909         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1910
1911         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1912
1913         HWRM_CHECK_RESULT();
1914         HWRM_UNLOCK();
1915
1916         return rc;
1917 }
1918
1919 int bnxt_hwrm_vnic_plcmode_cfg(struct bnxt *bp,
1920                         struct bnxt_vnic_info *vnic)
1921 {
1922         int rc = 0;
1923         struct hwrm_vnic_plcmodes_cfg_input req = {.req_type = 0 };
1924         struct hwrm_vnic_plcmodes_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1925         uint16_t size;
1926
1927         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
1928                 PMD_DRV_LOG(DEBUG, "VNIC ID %x\n", vnic->fw_vnic_id);
1929                 return rc;
1930         }
1931
1932         HWRM_PREP(req, VNIC_PLCMODES_CFG, BNXT_USE_CHIMP_MB);
1933
1934         req.flags = rte_cpu_to_le_32(
1935                         HWRM_VNIC_PLCMODES_CFG_INPUT_FLAGS_JUMBO_PLACEMENT);
1936
1937         req.enables = rte_cpu_to_le_32(
1938                 HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_JUMBO_THRESH_VALID);
1939
1940         size = rte_pktmbuf_data_room_size(bp->rx_queues[0]->mb_pool);
1941         size -= RTE_PKTMBUF_HEADROOM;
1942         size = RTE_MIN(BNXT_MAX_PKT_LEN, size);
1943
1944         req.jumbo_thresh = rte_cpu_to_le_16(size);
1945         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1946
1947         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1948
1949         HWRM_CHECK_RESULT();
1950         HWRM_UNLOCK();
1951
1952         return rc;
1953 }
1954
1955 int bnxt_hwrm_vnic_tpa_cfg(struct bnxt *bp,
1956                         struct bnxt_vnic_info *vnic, bool enable)
1957 {
1958         int rc = 0;
1959         struct hwrm_vnic_tpa_cfg_input req = {.req_type = 0 };
1960         struct hwrm_vnic_tpa_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1961
1962         if (BNXT_CHIP_THOR(bp))
1963                 return 0;
1964
1965         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
1966                 PMD_DRV_LOG(DEBUG, "Invalid vNIC ID\n");
1967                 return 0;
1968         }
1969
1970         HWRM_PREP(req, VNIC_TPA_CFG, BNXT_USE_CHIMP_MB);
1971
1972         if (enable) {
1973                 req.enables = rte_cpu_to_le_32(
1974                                 HWRM_VNIC_TPA_CFG_INPUT_ENABLES_MAX_AGG_SEGS |
1975                                 HWRM_VNIC_TPA_CFG_INPUT_ENABLES_MAX_AGGS |
1976                                 HWRM_VNIC_TPA_CFG_INPUT_ENABLES_MIN_AGG_LEN);
1977                 req.flags = rte_cpu_to_le_32(
1978                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_TPA |
1979                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_ENCAP_TPA |
1980                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_RSC_WND_UPDATE |
1981                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_GRO |
1982                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_AGG_WITH_ECN |
1983                         HWRM_VNIC_TPA_CFG_INPUT_FLAGS_AGG_WITH_SAME_GRE_SEQ);
1984                 req.max_agg_segs = rte_cpu_to_le_16(5);
1985                 req.max_aggs =
1986                         rte_cpu_to_le_16(HWRM_VNIC_TPA_CFG_INPUT_MAX_AGGS_MAX);
1987                 req.min_agg_len = rte_cpu_to_le_32(512);
1988         }
1989         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1990
1991         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1992
1993         HWRM_CHECK_RESULT();
1994         HWRM_UNLOCK();
1995
1996         return rc;
1997 }
1998
1999 int bnxt_hwrm_func_vf_mac(struct bnxt *bp, uint16_t vf, const uint8_t *mac_addr)
2000 {
2001         struct hwrm_func_cfg_input req = {0};
2002         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2003         int rc;
2004
2005         req.flags = rte_cpu_to_le_32(bp->pf.vf_info[vf].func_cfg_flags);
2006         req.enables = rte_cpu_to_le_32(
2007                         HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
2008         memcpy(req.dflt_mac_addr, mac_addr, sizeof(req.dflt_mac_addr));
2009         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
2010
2011         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
2012
2013         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2014         HWRM_CHECK_RESULT();
2015         HWRM_UNLOCK();
2016
2017         bp->pf.vf_info[vf].random_mac = false;
2018
2019         return rc;
2020 }
2021
2022 int bnxt_hwrm_func_qstats_tx_drop(struct bnxt *bp, uint16_t fid,
2023                                   uint64_t *dropped)
2024 {
2025         int rc = 0;
2026         struct hwrm_func_qstats_input req = {.req_type = 0};
2027         struct hwrm_func_qstats_output *resp = bp->hwrm_cmd_resp_addr;
2028
2029         HWRM_PREP(req, FUNC_QSTATS, BNXT_USE_CHIMP_MB);
2030
2031         req.fid = rte_cpu_to_le_16(fid);
2032
2033         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2034
2035         HWRM_CHECK_RESULT();
2036
2037         if (dropped)
2038                 *dropped = rte_le_to_cpu_64(resp->tx_drop_pkts);
2039
2040         HWRM_UNLOCK();
2041
2042         return rc;
2043 }
2044
2045 int bnxt_hwrm_func_qstats(struct bnxt *bp, uint16_t fid,
2046                           struct rte_eth_stats *stats)
2047 {
2048         int rc = 0;
2049         struct hwrm_func_qstats_input req = {.req_type = 0};
2050         struct hwrm_func_qstats_output *resp = bp->hwrm_cmd_resp_addr;
2051
2052         HWRM_PREP(req, FUNC_QSTATS, BNXT_USE_CHIMP_MB);
2053
2054         req.fid = rte_cpu_to_le_16(fid);
2055
2056         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2057
2058         HWRM_CHECK_RESULT();
2059
2060         stats->ipackets = rte_le_to_cpu_64(resp->rx_ucast_pkts);
2061         stats->ipackets += rte_le_to_cpu_64(resp->rx_mcast_pkts);
2062         stats->ipackets += rte_le_to_cpu_64(resp->rx_bcast_pkts);
2063         stats->ibytes = rte_le_to_cpu_64(resp->rx_ucast_bytes);
2064         stats->ibytes += rte_le_to_cpu_64(resp->rx_mcast_bytes);
2065         stats->ibytes += rte_le_to_cpu_64(resp->rx_bcast_bytes);
2066
2067         stats->opackets = rte_le_to_cpu_64(resp->tx_ucast_pkts);
2068         stats->opackets += rte_le_to_cpu_64(resp->tx_mcast_pkts);
2069         stats->opackets += rte_le_to_cpu_64(resp->tx_bcast_pkts);
2070         stats->obytes = rte_le_to_cpu_64(resp->tx_ucast_bytes);
2071         stats->obytes += rte_le_to_cpu_64(resp->tx_mcast_bytes);
2072         stats->obytes += rte_le_to_cpu_64(resp->tx_bcast_bytes);
2073
2074         stats->imissed = rte_le_to_cpu_64(resp->rx_discard_pkts);
2075         stats->ierrors = rte_le_to_cpu_64(resp->rx_drop_pkts);
2076         stats->oerrors = rte_le_to_cpu_64(resp->tx_discard_pkts);
2077
2078         HWRM_UNLOCK();
2079
2080         return rc;
2081 }
2082
2083 int bnxt_hwrm_func_clr_stats(struct bnxt *bp, uint16_t fid)
2084 {
2085         int rc = 0;
2086         struct hwrm_func_clr_stats_input req = {.req_type = 0};
2087         struct hwrm_func_clr_stats_output *resp = bp->hwrm_cmd_resp_addr;
2088
2089         HWRM_PREP(req, FUNC_CLR_STATS, BNXT_USE_CHIMP_MB);
2090
2091         req.fid = rte_cpu_to_le_16(fid);
2092
2093         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2094
2095         HWRM_CHECK_RESULT();
2096         HWRM_UNLOCK();
2097
2098         return rc;
2099 }
2100
2101 /*
2102  * HWRM utility functions
2103  */
2104
2105 int bnxt_clear_all_hwrm_stat_ctxs(struct bnxt *bp)
2106 {
2107         unsigned int i;
2108         int rc = 0;
2109
2110         for (i = 0; i < bp->rx_cp_nr_rings + bp->tx_cp_nr_rings; i++) {
2111                 struct bnxt_tx_queue *txq;
2112                 struct bnxt_rx_queue *rxq;
2113                 struct bnxt_cp_ring_info *cpr;
2114
2115                 if (i >= bp->rx_cp_nr_rings) {
2116                         txq = bp->tx_queues[i - bp->rx_cp_nr_rings];
2117                         cpr = txq->cp_ring;
2118                 } else {
2119                         rxq = bp->rx_queues[i];
2120                         cpr = rxq->cp_ring;
2121                 }
2122
2123                 rc = bnxt_hwrm_stat_clear(bp, cpr);
2124                 if (rc)
2125                         return rc;
2126         }
2127         return 0;
2128 }
2129
2130 int bnxt_free_all_hwrm_stat_ctxs(struct bnxt *bp)
2131 {
2132         int rc;
2133         unsigned int i;
2134         struct bnxt_cp_ring_info *cpr;
2135
2136         for (i = 0; i < bp->rx_cp_nr_rings + bp->tx_cp_nr_rings; i++) {
2137
2138                 if (i >= bp->rx_cp_nr_rings) {
2139                         cpr = bp->tx_queues[i - bp->rx_cp_nr_rings]->cp_ring;
2140                 } else {
2141                         cpr = bp->rx_queues[i]->cp_ring;
2142                         if (BNXT_HAS_RING_GRPS(bp))
2143                                 bp->grp_info[i].fw_stats_ctx = -1;
2144                 }
2145                 if (cpr->hw_stats_ctx_id != HWRM_NA_SIGNATURE) {
2146                         rc = bnxt_hwrm_stat_ctx_free(bp, cpr, i);
2147                         cpr->hw_stats_ctx_id = HWRM_NA_SIGNATURE;
2148                         if (rc)
2149                                 return rc;
2150                 }
2151         }
2152         return 0;
2153 }
2154
2155 int bnxt_alloc_all_hwrm_stat_ctxs(struct bnxt *bp)
2156 {
2157         unsigned int i;
2158         int rc = 0;
2159
2160         for (i = 0; i < bp->rx_cp_nr_rings + bp->tx_cp_nr_rings; i++) {
2161                 struct bnxt_tx_queue *txq;
2162                 struct bnxt_rx_queue *rxq;
2163                 struct bnxt_cp_ring_info *cpr;
2164
2165                 if (i >= bp->rx_cp_nr_rings) {
2166                         txq = bp->tx_queues[i - bp->rx_cp_nr_rings];
2167                         cpr = txq->cp_ring;
2168                 } else {
2169                         rxq = bp->rx_queues[i];
2170                         cpr = rxq->cp_ring;
2171                 }
2172
2173                 rc = bnxt_hwrm_stat_ctx_alloc(bp, cpr, i);
2174
2175                 if (rc)
2176                         return rc;
2177         }
2178         return rc;
2179 }
2180
2181 int bnxt_free_all_hwrm_ring_grps(struct bnxt *bp)
2182 {
2183         uint16_t idx;
2184         uint32_t rc = 0;
2185
2186         if (!BNXT_HAS_RING_GRPS(bp))
2187                 return 0;
2188
2189         for (idx = 0; idx < bp->rx_cp_nr_rings; idx++) {
2190
2191                 if (bp->grp_info[idx].fw_grp_id == INVALID_HW_RING_ID)
2192                         continue;
2193
2194                 rc = bnxt_hwrm_ring_grp_free(bp, idx);
2195
2196                 if (rc)
2197                         return rc;
2198         }
2199         return rc;
2200 }
2201
2202 void bnxt_free_nq_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2203 {
2204         struct bnxt_ring *cp_ring = cpr->cp_ring_struct;
2205
2206         bnxt_hwrm_ring_free(bp, cp_ring,
2207                             HWRM_RING_FREE_INPUT_RING_TYPE_NQ);
2208         cp_ring->fw_ring_id = INVALID_HW_RING_ID;
2209         memset(cpr->cp_desc_ring, 0, cpr->cp_ring_struct->ring_size *
2210                                      sizeof(*cpr->cp_desc_ring));
2211         cpr->cp_raw_cons = 0;
2212         cpr->valid = 0;
2213 }
2214
2215 void bnxt_free_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2216 {
2217         struct bnxt_ring *cp_ring = cpr->cp_ring_struct;
2218
2219         bnxt_hwrm_ring_free(bp, cp_ring,
2220                         HWRM_RING_FREE_INPUT_RING_TYPE_L2_CMPL);
2221         cp_ring->fw_ring_id = INVALID_HW_RING_ID;
2222         memset(cpr->cp_desc_ring, 0, cpr->cp_ring_struct->ring_size *
2223                         sizeof(*cpr->cp_desc_ring));
2224         cpr->cp_raw_cons = 0;
2225         cpr->valid = 0;
2226 }
2227
2228 void bnxt_free_hwrm_rx_ring(struct bnxt *bp, int queue_index)
2229 {
2230         struct bnxt_rx_queue *rxq = bp->rx_queues[queue_index];
2231         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
2232         struct bnxt_ring *ring = rxr->rx_ring_struct;
2233         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
2234
2235         if (ring->fw_ring_id != INVALID_HW_RING_ID) {
2236                 bnxt_hwrm_ring_free(bp, ring,
2237                                     HWRM_RING_FREE_INPUT_RING_TYPE_RX);
2238                 ring->fw_ring_id = INVALID_HW_RING_ID;
2239                 if (BNXT_HAS_RING_GRPS(bp))
2240                         bp->grp_info[queue_index].rx_fw_ring_id =
2241                                                         INVALID_HW_RING_ID;
2242                 memset(rxr->rx_desc_ring, 0,
2243                        rxr->rx_ring_struct->ring_size *
2244                        sizeof(*rxr->rx_desc_ring));
2245                 memset(rxr->rx_buf_ring, 0,
2246                        rxr->rx_ring_struct->ring_size *
2247                        sizeof(*rxr->rx_buf_ring));
2248                 rxr->rx_prod = 0;
2249         }
2250         ring = rxr->ag_ring_struct;
2251         if (ring->fw_ring_id != INVALID_HW_RING_ID) {
2252                 bnxt_hwrm_ring_free(bp, ring,
2253                                     BNXT_CHIP_THOR(bp) ?
2254                                     HWRM_RING_FREE_INPUT_RING_TYPE_RX_AGG :
2255                                     HWRM_RING_FREE_INPUT_RING_TYPE_RX);
2256                 ring->fw_ring_id = INVALID_HW_RING_ID;
2257                 memset(rxr->ag_buf_ring, 0,
2258                        rxr->ag_ring_struct->ring_size *
2259                        sizeof(*rxr->ag_buf_ring));
2260                 rxr->ag_prod = 0;
2261                 if (BNXT_HAS_RING_GRPS(bp))
2262                         bp->grp_info[queue_index].ag_fw_ring_id =
2263                                                         INVALID_HW_RING_ID;
2264         }
2265         if (cpr->cp_ring_struct->fw_ring_id != INVALID_HW_RING_ID) {
2266                 bnxt_free_cp_ring(bp, cpr);
2267                 if (rxq->nq_ring)
2268                         bnxt_free_nq_ring(bp, rxq->nq_ring);
2269         }
2270
2271         if (BNXT_HAS_RING_GRPS(bp))
2272                 bp->grp_info[queue_index].cp_fw_ring_id = INVALID_HW_RING_ID;
2273 }
2274
2275 int bnxt_free_all_hwrm_rings(struct bnxt *bp)
2276 {
2277         unsigned int i;
2278
2279         for (i = 0; i < bp->tx_cp_nr_rings; i++) {
2280                 struct bnxt_tx_queue *txq = bp->tx_queues[i];
2281                 struct bnxt_tx_ring_info *txr = txq->tx_ring;
2282                 struct bnxt_ring *ring = txr->tx_ring_struct;
2283                 struct bnxt_cp_ring_info *cpr = txq->cp_ring;
2284
2285                 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
2286                         bnxt_hwrm_ring_free(bp, ring,
2287                                         HWRM_RING_FREE_INPUT_RING_TYPE_TX);
2288                         ring->fw_ring_id = INVALID_HW_RING_ID;
2289                         memset(txr->tx_desc_ring, 0,
2290                                         txr->tx_ring_struct->ring_size *
2291                                         sizeof(*txr->tx_desc_ring));
2292                         memset(txr->tx_buf_ring, 0,
2293                                         txr->tx_ring_struct->ring_size *
2294                                         sizeof(*txr->tx_buf_ring));
2295                         txr->tx_prod = 0;
2296                         txr->tx_cons = 0;
2297                 }
2298                 if (cpr->cp_ring_struct->fw_ring_id != INVALID_HW_RING_ID) {
2299                         bnxt_free_cp_ring(bp, cpr);
2300                         cpr->cp_ring_struct->fw_ring_id = INVALID_HW_RING_ID;
2301                         if (txq->nq_ring)
2302                                 bnxt_free_nq_ring(bp, txq->nq_ring);
2303                 }
2304         }
2305
2306         for (i = 0; i < bp->rx_cp_nr_rings; i++)
2307                 bnxt_free_hwrm_rx_ring(bp, i);
2308
2309         return 0;
2310 }
2311
2312 int bnxt_alloc_all_hwrm_ring_grps(struct bnxt *bp)
2313 {
2314         uint16_t i;
2315         uint32_t rc = 0;
2316
2317         if (!BNXT_HAS_RING_GRPS(bp))
2318                 return 0;
2319
2320         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
2321                 rc = bnxt_hwrm_ring_grp_alloc(bp, i);
2322                 if (rc)
2323                         return rc;
2324         }
2325         return rc;
2326 }
2327
2328 void bnxt_free_hwrm_resources(struct bnxt *bp)
2329 {
2330         /* Release memzone */
2331         rte_free(bp->hwrm_cmd_resp_addr);
2332         rte_free(bp->hwrm_short_cmd_req_addr);
2333         bp->hwrm_cmd_resp_addr = NULL;
2334         bp->hwrm_short_cmd_req_addr = NULL;
2335         bp->hwrm_cmd_resp_dma_addr = 0;
2336         bp->hwrm_short_cmd_req_dma_addr = 0;
2337 }
2338
2339 int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2340 {
2341         struct rte_pci_device *pdev = bp->pdev;
2342         char type[RTE_MEMZONE_NAMESIZE];
2343
2344         sprintf(type, "bnxt_hwrm_%04x:%02x:%02x:%02x", pdev->addr.domain,
2345                 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
2346         bp->max_resp_len = HWRM_MAX_RESP_LEN;
2347         bp->hwrm_cmd_resp_addr = rte_malloc(type, bp->max_resp_len, 0);
2348         rte_mem_lock_page(bp->hwrm_cmd_resp_addr);
2349         if (bp->hwrm_cmd_resp_addr == NULL)
2350                 return -ENOMEM;
2351         bp->hwrm_cmd_resp_dma_addr =
2352                 rte_mem_virt2iova(bp->hwrm_cmd_resp_addr);
2353         if (bp->hwrm_cmd_resp_dma_addr == RTE_BAD_IOVA) {
2354                 PMD_DRV_LOG(ERR,
2355                         "unable to map response address to physical memory\n");
2356                 return -ENOMEM;
2357         }
2358         rte_spinlock_init(&bp->hwrm_lock);
2359
2360         return 0;
2361 }
2362
2363 int bnxt_clear_hwrm_vnic_filters(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2364 {
2365         struct bnxt_filter_info *filter;
2366         int rc = 0;
2367
2368         STAILQ_FOREACH(filter, &vnic->filter, next) {
2369                 if (filter->filter_type == HWRM_CFA_EM_FILTER)
2370                         rc = bnxt_hwrm_clear_em_filter(bp, filter);
2371                 else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER)
2372                         rc = bnxt_hwrm_clear_ntuple_filter(bp, filter);
2373                 else
2374                         rc = bnxt_hwrm_clear_l2_filter(bp, filter);
2375                 STAILQ_REMOVE(&vnic->filter, filter, bnxt_filter_info, next);
2376                 bnxt_free_filter(bp, filter);
2377                 //if (rc)
2378                         //break;
2379         }
2380         return rc;
2381 }
2382
2383 static int
2384 bnxt_clear_hwrm_vnic_flows(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2385 {
2386         struct bnxt_filter_info *filter;
2387         struct rte_flow *flow;
2388         int rc = 0;
2389
2390         while (!STAILQ_EMPTY(&vnic->flow_list)) {
2391                 flow = STAILQ_FIRST(&vnic->flow_list);
2392                 filter = flow->filter;
2393                 PMD_DRV_LOG(DEBUG, "filter type %d\n", filter->filter_type);
2394                 if (filter->filter_type == HWRM_CFA_EM_FILTER)
2395                         rc = bnxt_hwrm_clear_em_filter(bp, filter);
2396                 else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER)
2397                         rc = bnxt_hwrm_clear_ntuple_filter(bp, filter);
2398                 else
2399                         rc = bnxt_hwrm_clear_l2_filter(bp, filter);
2400
2401                 STAILQ_REMOVE(&vnic->flow_list, flow, rte_flow, next);
2402                 rte_free(flow);
2403                 //if (rc)
2404                         //break;
2405         }
2406         return rc;
2407 }
2408
2409 int bnxt_set_hwrm_vnic_filters(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2410 {
2411         struct bnxt_filter_info *filter;
2412         int rc = 0;
2413
2414         STAILQ_FOREACH(filter, &vnic->filter, next) {
2415                 if (filter->filter_type == HWRM_CFA_EM_FILTER) {
2416                         rc = bnxt_hwrm_set_em_filter(bp, filter->dst_id,
2417                                                      filter);
2418                 } else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER) {
2419                         rc = bnxt_hwrm_set_ntuple_filter(bp, filter->dst_id,
2420                                                          filter);
2421                 } else {
2422                         rc = bnxt_hwrm_set_l2_filter(bp, vnic->fw_vnic_id,
2423                                                      filter);
2424                         if (!rc)
2425                                 filter->dflt = 1;
2426                 }
2427                 if (rc)
2428                         break;
2429         }
2430         return rc;
2431 }
2432
2433 void bnxt_free_tunnel_ports(struct bnxt *bp)
2434 {
2435         if (bp->vxlan_port_cnt)
2436                 bnxt_hwrm_tunnel_dst_port_free(bp, bp->vxlan_fw_dst_port_id,
2437                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_VXLAN);
2438         bp->vxlan_port = 0;
2439         if (bp->geneve_port_cnt)
2440                 bnxt_hwrm_tunnel_dst_port_free(bp, bp->geneve_fw_dst_port_id,
2441                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_GENEVE);
2442         bp->geneve_port = 0;
2443 }
2444
2445 void bnxt_free_all_hwrm_resources(struct bnxt *bp)
2446 {
2447         int i;
2448
2449         if (bp->vnic_info == NULL)
2450                 return;
2451
2452         /*
2453          * Cleanup VNICs in reverse order, to make sure the L2 filter
2454          * from vnic0 is last to be cleaned up.
2455          */
2456         for (i = bp->max_vnics - 1; i >= 0; i--) {
2457                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2458
2459                 // If the VNIC ID is invalid we are not currently using the VNIC
2460                 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
2461                         continue;
2462
2463                 bnxt_clear_hwrm_vnic_flows(bp, vnic);
2464
2465                 bnxt_clear_hwrm_vnic_filters(bp, vnic);
2466
2467                 bnxt_hwrm_vnic_ctx_free(bp, vnic);
2468
2469                 bnxt_hwrm_vnic_tpa_cfg(bp, vnic, false);
2470
2471                 bnxt_hwrm_vnic_free(bp, vnic);
2472
2473                 rte_free(vnic->fw_grp_ids);
2474         }
2475         /* Ring resources */
2476         bnxt_free_all_hwrm_rings(bp);
2477         bnxt_free_all_hwrm_ring_grps(bp);
2478         bnxt_free_all_hwrm_stat_ctxs(bp);
2479         bnxt_free_tunnel_ports(bp);
2480 }
2481
2482 static uint16_t bnxt_parse_eth_link_duplex(uint32_t conf_link_speed)
2483 {
2484         uint8_t hw_link_duplex = HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH;
2485
2486         if ((conf_link_speed & ETH_LINK_SPEED_FIXED) == ETH_LINK_SPEED_AUTONEG)
2487                 return HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH;
2488
2489         switch (conf_link_speed) {
2490         case ETH_LINK_SPEED_10M_HD:
2491         case ETH_LINK_SPEED_100M_HD:
2492                 /* FALLTHROUGH */
2493                 return HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_HALF;
2494         }
2495         return hw_link_duplex;
2496 }
2497
2498 static uint16_t bnxt_check_eth_link_autoneg(uint32_t conf_link)
2499 {
2500         return (conf_link & ETH_LINK_SPEED_FIXED) ? 0 : 1;
2501 }
2502
2503 static uint16_t bnxt_parse_eth_link_speed(uint32_t conf_link_speed)
2504 {
2505         uint16_t eth_link_speed = 0;
2506
2507         if (conf_link_speed == ETH_LINK_SPEED_AUTONEG)
2508                 return ETH_LINK_SPEED_AUTONEG;
2509
2510         switch (conf_link_speed & ~ETH_LINK_SPEED_FIXED) {
2511         case ETH_LINK_SPEED_100M:
2512         case ETH_LINK_SPEED_100M_HD:
2513                 /* FALLTHROUGH */
2514                 eth_link_speed =
2515                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_100MB;
2516                 break;
2517         case ETH_LINK_SPEED_1G:
2518                 eth_link_speed =
2519                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_1GB;
2520                 break;
2521         case ETH_LINK_SPEED_2_5G:
2522                 eth_link_speed =
2523                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_2_5GB;
2524                 break;
2525         case ETH_LINK_SPEED_10G:
2526                 eth_link_speed =
2527                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_10GB;
2528                 break;
2529         case ETH_LINK_SPEED_20G:
2530                 eth_link_speed =
2531                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_20GB;
2532                 break;
2533         case ETH_LINK_SPEED_25G:
2534                 eth_link_speed =
2535                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_25GB;
2536                 break;
2537         case ETH_LINK_SPEED_40G:
2538                 eth_link_speed =
2539                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_40GB;
2540                 break;
2541         case ETH_LINK_SPEED_50G:
2542                 eth_link_speed =
2543                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_50GB;
2544                 break;
2545         case ETH_LINK_SPEED_100G:
2546                 eth_link_speed =
2547                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_100GB;
2548                 break;
2549         default:
2550                 PMD_DRV_LOG(ERR,
2551                         "Unsupported link speed %d; default to AUTO\n",
2552                         conf_link_speed);
2553                 break;
2554         }
2555         return eth_link_speed;
2556 }
2557
2558 #define BNXT_SUPPORTED_SPEEDS (ETH_LINK_SPEED_100M | ETH_LINK_SPEED_100M_HD | \
2559                 ETH_LINK_SPEED_1G | ETH_LINK_SPEED_2_5G | \
2560                 ETH_LINK_SPEED_10G | ETH_LINK_SPEED_20G | ETH_LINK_SPEED_25G | \
2561                 ETH_LINK_SPEED_40G | ETH_LINK_SPEED_50G | ETH_LINK_SPEED_100G)
2562
2563 static int bnxt_valid_link_speed(uint32_t link_speed, uint16_t port_id)
2564 {
2565         uint32_t one_speed;
2566
2567         if (link_speed == ETH_LINK_SPEED_AUTONEG)
2568                 return 0;
2569
2570         if (link_speed & ETH_LINK_SPEED_FIXED) {
2571                 one_speed = link_speed & ~ETH_LINK_SPEED_FIXED;
2572
2573                 if (one_speed & (one_speed - 1)) {
2574                         PMD_DRV_LOG(ERR,
2575                                 "Invalid advertised speeds (%u) for port %u\n",
2576                                 link_speed, port_id);
2577                         return -EINVAL;
2578                 }
2579                 if ((one_speed & BNXT_SUPPORTED_SPEEDS) != one_speed) {
2580                         PMD_DRV_LOG(ERR,
2581                                 "Unsupported advertised speed (%u) for port %u\n",
2582                                 link_speed, port_id);
2583                         return -EINVAL;
2584                 }
2585         } else {
2586                 if (!(link_speed & BNXT_SUPPORTED_SPEEDS)) {
2587                         PMD_DRV_LOG(ERR,
2588                                 "Unsupported advertised speeds (%u) for port %u\n",
2589                                 link_speed, port_id);
2590                         return -EINVAL;
2591                 }
2592         }
2593         return 0;
2594 }
2595
2596 static uint16_t
2597 bnxt_parse_eth_link_speed_mask(struct bnxt *bp, uint32_t link_speed)
2598 {
2599         uint16_t ret = 0;
2600
2601         if (link_speed == ETH_LINK_SPEED_AUTONEG) {
2602                 if (bp->link_info.support_speeds)
2603                         return bp->link_info.support_speeds;
2604                 link_speed = BNXT_SUPPORTED_SPEEDS;
2605         }
2606
2607         if (link_speed & ETH_LINK_SPEED_100M)
2608                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100MB;
2609         if (link_speed & ETH_LINK_SPEED_100M_HD)
2610                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100MB;
2611         if (link_speed & ETH_LINK_SPEED_1G)
2612                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_1GB;
2613         if (link_speed & ETH_LINK_SPEED_2_5G)
2614                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_2_5GB;
2615         if (link_speed & ETH_LINK_SPEED_10G)
2616                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_10GB;
2617         if (link_speed & ETH_LINK_SPEED_20G)
2618                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_20GB;
2619         if (link_speed & ETH_LINK_SPEED_25G)
2620                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_25GB;
2621         if (link_speed & ETH_LINK_SPEED_40G)
2622                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_40GB;
2623         if (link_speed & ETH_LINK_SPEED_50G)
2624                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_50GB;
2625         if (link_speed & ETH_LINK_SPEED_100G)
2626                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100GB;
2627         return ret;
2628 }
2629
2630 static uint32_t bnxt_parse_hw_link_speed(uint16_t hw_link_speed)
2631 {
2632         uint32_t eth_link_speed = ETH_SPEED_NUM_NONE;
2633
2634         switch (hw_link_speed) {
2635         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100MB:
2636                 eth_link_speed = ETH_SPEED_NUM_100M;
2637                 break;
2638         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_1GB:
2639                 eth_link_speed = ETH_SPEED_NUM_1G;
2640                 break;
2641         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2_5GB:
2642                 eth_link_speed = ETH_SPEED_NUM_2_5G;
2643                 break;
2644         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_10GB:
2645                 eth_link_speed = ETH_SPEED_NUM_10G;
2646                 break;
2647         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_20GB:
2648                 eth_link_speed = ETH_SPEED_NUM_20G;
2649                 break;
2650         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_25GB:
2651                 eth_link_speed = ETH_SPEED_NUM_25G;
2652                 break;
2653         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_40GB:
2654                 eth_link_speed = ETH_SPEED_NUM_40G;
2655                 break;
2656         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_50GB:
2657                 eth_link_speed = ETH_SPEED_NUM_50G;
2658                 break;
2659         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100GB:
2660                 eth_link_speed = ETH_SPEED_NUM_100G;
2661                 break;
2662         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2GB:
2663         default:
2664                 PMD_DRV_LOG(ERR, "HWRM link speed %d not defined\n",
2665                         hw_link_speed);
2666                 break;
2667         }
2668         return eth_link_speed;
2669 }
2670
2671 static uint16_t bnxt_parse_hw_link_duplex(uint16_t hw_link_duplex)
2672 {
2673         uint16_t eth_link_duplex = ETH_LINK_FULL_DUPLEX;
2674
2675         switch (hw_link_duplex) {
2676         case HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH:
2677         case HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_FULL:
2678                 /* FALLTHROUGH */
2679                 eth_link_duplex = ETH_LINK_FULL_DUPLEX;
2680                 break;
2681         case HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_HALF:
2682                 eth_link_duplex = ETH_LINK_HALF_DUPLEX;
2683                 break;
2684         default:
2685                 PMD_DRV_LOG(ERR, "HWRM link duplex %d not defined\n",
2686                         hw_link_duplex);
2687                 break;
2688         }
2689         return eth_link_duplex;
2690 }
2691
2692 int bnxt_get_hwrm_link_config(struct bnxt *bp, struct rte_eth_link *link)
2693 {
2694         int rc = 0;
2695         struct bnxt_link_info *link_info = &bp->link_info;
2696
2697         rc = bnxt_hwrm_port_phy_qcfg(bp, link_info);
2698         if (rc) {
2699                 PMD_DRV_LOG(ERR,
2700                         "Get link config failed with rc %d\n", rc);
2701                 goto exit;
2702         }
2703         if (link_info->link_speed)
2704                 link->link_speed =
2705                         bnxt_parse_hw_link_speed(link_info->link_speed);
2706         else
2707                 link->link_speed = ETH_SPEED_NUM_NONE;
2708         link->link_duplex = bnxt_parse_hw_link_duplex(link_info->duplex);
2709         link->link_status = link_info->link_up;
2710         link->link_autoneg = link_info->auto_mode ==
2711                 HWRM_PORT_PHY_QCFG_OUTPUT_AUTO_MODE_NONE ?
2712                 ETH_LINK_FIXED : ETH_LINK_AUTONEG;
2713 exit:
2714         return rc;
2715 }
2716
2717 int bnxt_set_hwrm_link_config(struct bnxt *bp, bool link_up)
2718 {
2719         int rc = 0;
2720         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
2721         struct bnxt_link_info link_req;
2722         uint16_t speed, autoneg;
2723
2724         if (!BNXT_SINGLE_PF(bp) || BNXT_VF(bp))
2725                 return 0;
2726
2727         rc = bnxt_valid_link_speed(dev_conf->link_speeds,
2728                         bp->eth_dev->data->port_id);
2729         if (rc)
2730                 goto error;
2731
2732         memset(&link_req, 0, sizeof(link_req));
2733         link_req.link_up = link_up;
2734         if (!link_up)
2735                 goto port_phy_cfg;
2736
2737         autoneg = bnxt_check_eth_link_autoneg(dev_conf->link_speeds);
2738         if (BNXT_CHIP_THOR(bp) &&
2739             dev_conf->link_speeds == ETH_LINK_SPEED_40G) {
2740                 /* 40G is not supported as part of media auto detect.
2741                  * The speed should be forced and autoneg disabled
2742                  * to configure 40G speed.
2743                  */
2744                 PMD_DRV_LOG(INFO, "Disabling autoneg for 40G\n");
2745                 autoneg = 0;
2746         }
2747
2748         speed = bnxt_parse_eth_link_speed(dev_conf->link_speeds);
2749         link_req.phy_flags = HWRM_PORT_PHY_CFG_INPUT_FLAGS_RESET_PHY;
2750         /* Autoneg can be done only when the FW allows.
2751          * When user configures fixed speed of 40G and later changes to
2752          * any other speed, auto_link_speed/force_link_speed is still set
2753          * to 40G until link comes up at new speed.
2754          */
2755         if (autoneg == 1 &&
2756             !(!BNXT_CHIP_THOR(bp) &&
2757               (bp->link_info.auto_link_speed ||
2758                bp->link_info.force_link_speed))) {
2759                 link_req.phy_flags |=
2760                                 HWRM_PORT_PHY_CFG_INPUT_FLAGS_RESTART_AUTONEG;
2761                 link_req.auto_link_speed_mask =
2762                         bnxt_parse_eth_link_speed_mask(bp,
2763                                                        dev_conf->link_speeds);
2764         } else {
2765                 if (bp->link_info.phy_type ==
2766                     HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET ||
2767                     bp->link_info.phy_type ==
2768                     HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASETE ||
2769                     bp->link_info.media_type ==
2770                     HWRM_PORT_PHY_QCFG_OUTPUT_MEDIA_TYPE_TP) {
2771                         PMD_DRV_LOG(ERR, "10GBase-T devices must autoneg\n");
2772                         return -EINVAL;
2773                 }
2774
2775                 link_req.phy_flags |= HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE;
2776                 /* If user wants a particular speed try that first. */
2777                 if (speed)
2778                         link_req.link_speed = speed;
2779                 else if (bp->link_info.force_link_speed)
2780                         link_req.link_speed = bp->link_info.force_link_speed;
2781                 else
2782                         link_req.link_speed = bp->link_info.auto_link_speed;
2783         }
2784         link_req.duplex = bnxt_parse_eth_link_duplex(dev_conf->link_speeds);
2785         link_req.auto_pause = bp->link_info.auto_pause;
2786         link_req.force_pause = bp->link_info.force_pause;
2787
2788 port_phy_cfg:
2789         rc = bnxt_hwrm_port_phy_cfg(bp, &link_req);
2790         if (rc) {
2791                 PMD_DRV_LOG(ERR,
2792                         "Set link config failed with rc %d\n", rc);
2793         }
2794
2795 error:
2796         return rc;
2797 }
2798
2799 /* JIRA 22088 */
2800 int bnxt_hwrm_func_qcfg(struct bnxt *bp, uint16_t *mtu)
2801 {
2802         struct hwrm_func_qcfg_input req = {0};
2803         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
2804         uint16_t flags;
2805         int rc = 0;
2806
2807         HWRM_PREP(req, FUNC_QCFG, BNXT_USE_CHIMP_MB);
2808         req.fid = rte_cpu_to_le_16(0xffff);
2809
2810         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2811
2812         HWRM_CHECK_RESULT();
2813
2814         /* Hard Coded.. 0xfff VLAN ID mask */
2815         bp->vlan = rte_le_to_cpu_16(resp->vlan) & 0xfff;
2816         flags = rte_le_to_cpu_16(resp->flags);
2817         if (BNXT_PF(bp) && (flags & HWRM_FUNC_QCFG_OUTPUT_FLAGS_MULTI_HOST))
2818                 bp->flags |= BNXT_FLAG_MULTI_HOST;
2819
2820         if (BNXT_VF(bp) && (flags & HWRM_FUNC_QCFG_OUTPUT_FLAGS_TRUSTED_VF)) {
2821                 bp->flags |= BNXT_FLAG_TRUSTED_VF_EN;
2822                 PMD_DRV_LOG(INFO, "Trusted VF cap enabled\n");
2823         } else if (BNXT_VF(bp) &&
2824                    !(flags & HWRM_FUNC_QCFG_OUTPUT_FLAGS_TRUSTED_VF)) {
2825                 bp->flags &= ~BNXT_FLAG_TRUSTED_VF_EN;
2826                 PMD_DRV_LOG(INFO, "Trusted VF cap disabled\n");
2827         }
2828
2829         if (mtu)
2830                 *mtu = resp->mtu;
2831
2832         switch (resp->port_partition_type) {
2833         case HWRM_FUNC_QCFG_OUTPUT_PORT_PARTITION_TYPE_NPAR1_0:
2834         case HWRM_FUNC_QCFG_OUTPUT_PORT_PARTITION_TYPE_NPAR1_5:
2835         case HWRM_FUNC_QCFG_OUTPUT_PORT_PARTITION_TYPE_NPAR2_0:
2836                 /* FALLTHROUGH */
2837                 bp->port_partition_type = resp->port_partition_type;
2838                 break;
2839         default:
2840                 bp->port_partition_type = 0;
2841                 break;
2842         }
2843
2844         HWRM_UNLOCK();
2845
2846         return rc;
2847 }
2848
2849 static void copy_func_cfg_to_qcaps(struct hwrm_func_cfg_input *fcfg,
2850                                    struct hwrm_func_qcaps_output *qcaps)
2851 {
2852         qcaps->max_rsscos_ctx = fcfg->num_rsscos_ctxs;
2853         memcpy(qcaps->mac_address, fcfg->dflt_mac_addr,
2854                sizeof(qcaps->mac_address));
2855         qcaps->max_l2_ctxs = fcfg->num_l2_ctxs;
2856         qcaps->max_rx_rings = fcfg->num_rx_rings;
2857         qcaps->max_tx_rings = fcfg->num_tx_rings;
2858         qcaps->max_cmpl_rings = fcfg->num_cmpl_rings;
2859         qcaps->max_stat_ctx = fcfg->num_stat_ctxs;
2860         qcaps->max_vfs = 0;
2861         qcaps->first_vf_id = 0;
2862         qcaps->max_vnics = fcfg->num_vnics;
2863         qcaps->max_decap_records = 0;
2864         qcaps->max_encap_records = 0;
2865         qcaps->max_tx_wm_flows = 0;
2866         qcaps->max_tx_em_flows = 0;
2867         qcaps->max_rx_wm_flows = 0;
2868         qcaps->max_rx_em_flows = 0;
2869         qcaps->max_flow_id = 0;
2870         qcaps->max_mcast_filters = fcfg->num_mcast_filters;
2871         qcaps->max_sp_tx_rings = 0;
2872         qcaps->max_hw_ring_grps = fcfg->num_hw_ring_grps;
2873 }
2874
2875 static int bnxt_hwrm_pf_func_cfg(struct bnxt *bp, int tx_rings)
2876 {
2877         struct hwrm_func_cfg_input req = {0};
2878         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2879         uint32_t enables;
2880         int rc;
2881
2882         enables = HWRM_FUNC_CFG_INPUT_ENABLES_MTU |
2883                   HWRM_FUNC_CFG_INPUT_ENABLES_MRU |
2884                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RSSCOS_CTXS |
2885                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_STAT_CTXS |
2886                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_CMPL_RINGS |
2887                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_TX_RINGS |
2888                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RX_RINGS |
2889                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_L2_CTXS |
2890                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_VNICS;
2891
2892         if (BNXT_HAS_RING_GRPS(bp)) {
2893                 enables |= HWRM_FUNC_CFG_INPUT_ENABLES_NUM_HW_RING_GRPS;
2894                 req.num_hw_ring_grps = rte_cpu_to_le_16(bp->max_ring_grps);
2895         } else if (BNXT_HAS_NQ(bp)) {
2896                 enables |= HWRM_FUNC_CFG_INPUT_ENABLES_NUM_MSIX;
2897                 req.num_msix = rte_cpu_to_le_16(bp->max_nq_rings);
2898         }
2899
2900         req.flags = rte_cpu_to_le_32(bp->pf.func_cfg_flags);
2901         req.mtu = rte_cpu_to_le_16(BNXT_MAX_MTU);
2902         req.mru = rte_cpu_to_le_16(bp->eth_dev->data->mtu + RTE_ETHER_HDR_LEN +
2903                                    RTE_ETHER_CRC_LEN + VLAN_TAG_SIZE *
2904                                    BNXT_NUM_VLANS);
2905         req.num_rsscos_ctxs = rte_cpu_to_le_16(bp->max_rsscos_ctx);
2906         req.num_stat_ctxs = rte_cpu_to_le_16(bp->max_stat_ctx);
2907         req.num_cmpl_rings = rte_cpu_to_le_16(bp->max_cp_rings);
2908         req.num_tx_rings = rte_cpu_to_le_16(tx_rings);
2909         req.num_rx_rings = rte_cpu_to_le_16(bp->max_rx_rings);
2910         req.num_l2_ctxs = rte_cpu_to_le_16(bp->max_l2_ctx);
2911         req.num_vnics = rte_cpu_to_le_16(bp->max_vnics);
2912         req.fid = rte_cpu_to_le_16(0xffff);
2913         req.enables = rte_cpu_to_le_32(enables);
2914
2915         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
2916
2917         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2918
2919         HWRM_CHECK_RESULT();
2920         HWRM_UNLOCK();
2921
2922         return rc;
2923 }
2924
2925 static void populate_vf_func_cfg_req(struct bnxt *bp,
2926                                      struct hwrm_func_cfg_input *req,
2927                                      int num_vfs)
2928 {
2929         req->enables = rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_MTU |
2930                         HWRM_FUNC_CFG_INPUT_ENABLES_MRU |
2931                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RSSCOS_CTXS |
2932                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_STAT_CTXS |
2933                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_CMPL_RINGS |
2934                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_TX_RINGS |
2935                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RX_RINGS |
2936                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_L2_CTXS |
2937                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_VNICS |
2938                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_HW_RING_GRPS);
2939
2940         req->mtu = rte_cpu_to_le_16(bp->eth_dev->data->mtu + RTE_ETHER_HDR_LEN +
2941                                     RTE_ETHER_CRC_LEN + VLAN_TAG_SIZE *
2942                                     BNXT_NUM_VLANS);
2943         req->mru = rte_cpu_to_le_16(bp->eth_dev->data->mtu + RTE_ETHER_HDR_LEN +
2944                                     RTE_ETHER_CRC_LEN + VLAN_TAG_SIZE *
2945                                     BNXT_NUM_VLANS);
2946         req->num_rsscos_ctxs = rte_cpu_to_le_16(bp->max_rsscos_ctx /
2947                                                 (num_vfs + 1));
2948         req->num_stat_ctxs = rte_cpu_to_le_16(bp->max_stat_ctx / (num_vfs + 1));
2949         req->num_cmpl_rings = rte_cpu_to_le_16(bp->max_cp_rings /
2950                                                (num_vfs + 1));
2951         req->num_tx_rings = rte_cpu_to_le_16(bp->max_tx_rings / (num_vfs + 1));
2952         req->num_rx_rings = rte_cpu_to_le_16(bp->max_rx_rings / (num_vfs + 1));
2953         req->num_l2_ctxs = rte_cpu_to_le_16(bp->max_l2_ctx / (num_vfs + 1));
2954         /* TODO: For now, do not support VMDq/RFS on VFs. */
2955         req->num_vnics = rte_cpu_to_le_16(1);
2956         req->num_hw_ring_grps = rte_cpu_to_le_16(bp->max_ring_grps /
2957                                                  (num_vfs + 1));
2958 }
2959
2960 static void add_random_mac_if_needed(struct bnxt *bp,
2961                                      struct hwrm_func_cfg_input *cfg_req,
2962                                      int vf)
2963 {
2964         struct rte_ether_addr mac;
2965
2966         if (bnxt_hwrm_func_qcfg_vf_default_mac(bp, vf, &mac))
2967                 return;
2968
2969         if (memcmp(mac.addr_bytes, "\x00\x00\x00\x00\x00", 6) == 0) {
2970                 cfg_req->enables |=
2971                 rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
2972                 rte_eth_random_addr(cfg_req->dflt_mac_addr);
2973                 bp->pf.vf_info[vf].random_mac = true;
2974         } else {
2975                 memcpy(cfg_req->dflt_mac_addr, mac.addr_bytes,
2976                         RTE_ETHER_ADDR_LEN);
2977         }
2978 }
2979
2980 static void reserve_resources_from_vf(struct bnxt *bp,
2981                                       struct hwrm_func_cfg_input *cfg_req,
2982                                       int vf)
2983 {
2984         struct hwrm_func_qcaps_input req = {0};
2985         struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
2986         int rc;
2987
2988         /* Get the actual allocated values now */
2989         HWRM_PREP(req, FUNC_QCAPS, BNXT_USE_CHIMP_MB);
2990         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
2991         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2992
2993         if (rc) {
2994                 PMD_DRV_LOG(ERR, "hwrm_func_qcaps failed rc:%d\n", rc);
2995                 copy_func_cfg_to_qcaps(cfg_req, resp);
2996         } else if (resp->error_code) {
2997                 rc = rte_le_to_cpu_16(resp->error_code);
2998                 PMD_DRV_LOG(ERR, "hwrm_func_qcaps error %d\n", rc);
2999                 copy_func_cfg_to_qcaps(cfg_req, resp);
3000         }
3001
3002         bp->max_rsscos_ctx -= rte_le_to_cpu_16(resp->max_rsscos_ctx);
3003         bp->max_stat_ctx -= rte_le_to_cpu_16(resp->max_stat_ctx);
3004         bp->max_cp_rings -= rte_le_to_cpu_16(resp->max_cmpl_rings);
3005         bp->max_tx_rings -= rte_le_to_cpu_16(resp->max_tx_rings);
3006         bp->max_rx_rings -= rte_le_to_cpu_16(resp->max_rx_rings);
3007         bp->max_l2_ctx -= rte_le_to_cpu_16(resp->max_l2_ctxs);
3008         /*
3009          * TODO: While not supporting VMDq with VFs, max_vnics is always
3010          * forced to 1 in this case
3011          */
3012         //bp->max_vnics -= rte_le_to_cpu_16(esp->max_vnics);
3013         bp->max_ring_grps -= rte_le_to_cpu_16(resp->max_hw_ring_grps);
3014
3015         HWRM_UNLOCK();
3016 }
3017
3018 int bnxt_hwrm_func_qcfg_current_vf_vlan(struct bnxt *bp, int vf)
3019 {
3020         struct hwrm_func_qcfg_input req = {0};
3021         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3022         int rc;
3023
3024         /* Check for zero MAC address */
3025         HWRM_PREP(req, FUNC_QCFG, BNXT_USE_CHIMP_MB);
3026         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
3027         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3028         HWRM_CHECK_RESULT();
3029         rc = rte_le_to_cpu_16(resp->vlan);
3030
3031         HWRM_UNLOCK();
3032
3033         return rc;
3034 }
3035
3036 static int update_pf_resource_max(struct bnxt *bp)
3037 {
3038         struct hwrm_func_qcfg_input req = {0};
3039         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3040         int rc;
3041
3042         /* And copy the allocated numbers into the pf struct */
3043         HWRM_PREP(req, FUNC_QCFG, BNXT_USE_CHIMP_MB);
3044         req.fid = rte_cpu_to_le_16(0xffff);
3045         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3046         HWRM_CHECK_RESULT();
3047
3048         /* Only TX ring value reflects actual allocation? TODO */
3049         bp->max_tx_rings = rte_le_to_cpu_16(resp->alloc_tx_rings);
3050         bp->pf.evb_mode = resp->evb_mode;
3051
3052         HWRM_UNLOCK();
3053
3054         return rc;
3055 }
3056
3057 int bnxt_hwrm_allocate_pf_only(struct bnxt *bp)
3058 {
3059         int rc;
3060
3061         if (!BNXT_PF(bp)) {
3062                 PMD_DRV_LOG(ERR, "Attempt to allcoate VFs on a VF!\n");
3063                 return -EINVAL;
3064         }
3065
3066         rc = bnxt_hwrm_func_qcaps(bp);
3067         if (rc)
3068                 return rc;
3069
3070         bp->pf.func_cfg_flags &=
3071                 ~(HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_ENABLE |
3072                   HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_DISABLE);
3073         bp->pf.func_cfg_flags |=
3074                 HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_DISABLE;
3075         rc = bnxt_hwrm_pf_func_cfg(bp, bp->max_tx_rings);
3076         rc = __bnxt_hwrm_func_qcaps(bp);
3077         return rc;
3078 }
3079
3080 int bnxt_hwrm_allocate_vfs(struct bnxt *bp, int num_vfs)
3081 {
3082         struct hwrm_func_cfg_input req = {0};
3083         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3084         int i;
3085         size_t sz;
3086         int rc = 0;
3087         size_t req_buf_sz;
3088
3089         if (!BNXT_PF(bp)) {
3090                 PMD_DRV_LOG(ERR, "Attempt to allcoate VFs on a VF!\n");
3091                 return -EINVAL;
3092         }
3093
3094         rc = bnxt_hwrm_func_qcaps(bp);
3095
3096         if (rc)
3097                 return rc;
3098
3099         bp->pf.active_vfs = num_vfs;
3100
3101         /*
3102          * First, configure the PF to only use one TX ring.  This ensures that
3103          * there are enough rings for all VFs.
3104          *
3105          * If we don't do this, when we call func_alloc() later, we will lock
3106          * extra rings to the PF that won't be available during func_cfg() of
3107          * the VFs.
3108          *
3109          * This has been fixed with firmware versions above 20.6.54
3110          */
3111         bp->pf.func_cfg_flags &=
3112                 ~(HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_ENABLE |
3113                   HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_DISABLE);
3114         bp->pf.func_cfg_flags |=
3115                 HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_ENABLE;
3116         rc = bnxt_hwrm_pf_func_cfg(bp, 1);
3117         if (rc)
3118                 return rc;
3119
3120         /*
3121          * Now, create and register a buffer to hold forwarded VF requests
3122          */
3123         req_buf_sz = num_vfs * HWRM_MAX_REQ_LEN;
3124         bp->pf.vf_req_buf = rte_malloc("bnxt_vf_fwd", req_buf_sz,
3125                 page_roundup(num_vfs * HWRM_MAX_REQ_LEN));
3126         if (bp->pf.vf_req_buf == NULL) {
3127                 rc = -ENOMEM;
3128                 goto error_free;
3129         }
3130         for (sz = 0; sz < req_buf_sz; sz += getpagesize())
3131                 rte_mem_lock_page(((char *)bp->pf.vf_req_buf) + sz);
3132         for (i = 0; i < num_vfs; i++)
3133                 bp->pf.vf_info[i].req_buf = ((char *)bp->pf.vf_req_buf) +
3134                                         (i * HWRM_MAX_REQ_LEN);
3135
3136         rc = bnxt_hwrm_func_buf_rgtr(bp);
3137         if (rc)
3138                 goto error_free;
3139
3140         populate_vf_func_cfg_req(bp, &req, num_vfs);
3141
3142         bp->pf.active_vfs = 0;
3143         for (i = 0; i < num_vfs; i++) {
3144                 add_random_mac_if_needed(bp, &req, i);
3145
3146                 HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
3147                 req.flags = rte_cpu_to_le_32(bp->pf.vf_info[i].func_cfg_flags);
3148                 req.fid = rte_cpu_to_le_16(bp->pf.vf_info[i].fid);
3149                 rc = bnxt_hwrm_send_message(bp,
3150                                             &req,
3151                                             sizeof(req),
3152                                             BNXT_USE_CHIMP_MB);
3153
3154                 /* Clear enable flag for next pass */
3155                 req.enables &= ~rte_cpu_to_le_32(
3156                                 HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
3157
3158                 if (rc || resp->error_code) {
3159                         PMD_DRV_LOG(ERR,
3160                                 "Failed to initizlie VF %d\n", i);
3161                         PMD_DRV_LOG(ERR,
3162                                 "Not all VFs available. (%d, %d)\n",
3163                                 rc, resp->error_code);
3164                         HWRM_UNLOCK();
3165                         break;
3166                 }
3167
3168                 HWRM_UNLOCK();
3169
3170                 reserve_resources_from_vf(bp, &req, i);
3171                 bp->pf.active_vfs++;
3172                 bnxt_hwrm_func_clr_stats(bp, bp->pf.vf_info[i].fid);
3173         }
3174
3175         /*
3176          * Now configure the PF to use "the rest" of the resources
3177          * We're using STD_TX_RING_MODE here though which will limit the TX
3178          * rings.  This will allow QoS to function properly.  Not setting this
3179          * will cause PF rings to break bandwidth settings.
3180          */
3181         rc = bnxt_hwrm_pf_func_cfg(bp, bp->max_tx_rings);
3182         if (rc)
3183                 goto error_free;
3184
3185         rc = update_pf_resource_max(bp);
3186         if (rc)
3187                 goto error_free;
3188
3189         return rc;
3190
3191 error_free:
3192         bnxt_hwrm_func_buf_unrgtr(bp);
3193         return rc;
3194 }
3195
3196 int bnxt_hwrm_pf_evb_mode(struct bnxt *bp)
3197 {
3198         struct hwrm_func_cfg_input req = {0};
3199         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3200         int rc;
3201
3202         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
3203
3204         req.fid = rte_cpu_to_le_16(0xffff);
3205         req.enables = rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_EVB_MODE);
3206         req.evb_mode = bp->pf.evb_mode;
3207
3208         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3209         HWRM_CHECK_RESULT();
3210         HWRM_UNLOCK();
3211
3212         return rc;
3213 }
3214
3215 int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, uint16_t port,
3216                                 uint8_t tunnel_type)
3217 {
3218         struct hwrm_tunnel_dst_port_alloc_input req = {0};
3219         struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3220         int rc = 0;
3221
3222         HWRM_PREP(req, TUNNEL_DST_PORT_ALLOC, BNXT_USE_CHIMP_MB);
3223         req.tunnel_type = tunnel_type;
3224         req.tunnel_dst_port_val = port;
3225         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3226         HWRM_CHECK_RESULT();
3227
3228         switch (tunnel_type) {
3229         case HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_VXLAN:
3230                 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
3231                 bp->vxlan_port = port;
3232                 break;
3233         case HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_GENEVE:
3234                 bp->geneve_fw_dst_port_id = resp->tunnel_dst_port_id;
3235                 bp->geneve_port = port;
3236                 break;
3237         default:
3238                 break;
3239         }
3240
3241         HWRM_UNLOCK();
3242
3243         return rc;
3244 }
3245
3246 int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, uint16_t port,
3247                                 uint8_t tunnel_type)
3248 {
3249         struct hwrm_tunnel_dst_port_free_input req = {0};
3250         struct hwrm_tunnel_dst_port_free_output *resp = bp->hwrm_cmd_resp_addr;
3251         int rc = 0;
3252
3253         HWRM_PREP(req, TUNNEL_DST_PORT_FREE, BNXT_USE_CHIMP_MB);
3254
3255         req.tunnel_type = tunnel_type;
3256         req.tunnel_dst_port_id = rte_cpu_to_be_16(port);
3257         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3258
3259         HWRM_CHECK_RESULT();
3260         HWRM_UNLOCK();
3261
3262         return rc;
3263 }
3264
3265 int bnxt_hwrm_func_cfg_vf_set_flags(struct bnxt *bp, uint16_t vf,
3266                                         uint32_t flags)
3267 {
3268         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3269         struct hwrm_func_cfg_input req = {0};
3270         int rc;
3271
3272         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
3273
3274         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
3275         req.flags = rte_cpu_to_le_32(flags);
3276         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3277
3278         HWRM_CHECK_RESULT();
3279         HWRM_UNLOCK();
3280
3281         return rc;
3282 }
3283
3284 void vf_vnic_set_rxmask_cb(struct bnxt_vnic_info *vnic, void *flagp)
3285 {
3286         uint32_t *flag = flagp;
3287
3288         vnic->flags = *flag;
3289 }
3290
3291 int bnxt_set_rx_mask_no_vlan(struct bnxt *bp, struct bnxt_vnic_info *vnic)
3292 {
3293         return bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
3294 }
3295
3296 int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
3297 {
3298         int rc = 0;
3299         struct hwrm_func_buf_rgtr_input req = {.req_type = 0 };
3300         struct hwrm_func_buf_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
3301
3302         HWRM_PREP(req, FUNC_BUF_RGTR, BNXT_USE_CHIMP_MB);
3303
3304         req.req_buf_num_pages = rte_cpu_to_le_16(1);
3305         req.req_buf_page_size = rte_cpu_to_le_16(
3306                          page_getenum(bp->pf.active_vfs * HWRM_MAX_REQ_LEN));
3307         req.req_buf_len = rte_cpu_to_le_16(HWRM_MAX_REQ_LEN);
3308         req.req_buf_page_addr0 =
3309                 rte_cpu_to_le_64(rte_mem_virt2iova(bp->pf.vf_req_buf));
3310         if (req.req_buf_page_addr0 == RTE_BAD_IOVA) {
3311                 PMD_DRV_LOG(ERR,
3312                         "unable to map buffer address to physical memory\n");
3313                 return -ENOMEM;
3314         }
3315
3316         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3317
3318         HWRM_CHECK_RESULT();
3319         HWRM_UNLOCK();
3320
3321         return rc;
3322 }
3323
3324 int bnxt_hwrm_func_buf_unrgtr(struct bnxt *bp)
3325 {
3326         int rc = 0;
3327         struct hwrm_func_buf_unrgtr_input req = {.req_type = 0 };
3328         struct hwrm_func_buf_unrgtr_output *resp = bp->hwrm_cmd_resp_addr;
3329
3330         if (!(BNXT_PF(bp) && bp->pdev->max_vfs))
3331                 return 0;
3332
3333         HWRM_PREP(req, FUNC_BUF_UNRGTR, BNXT_USE_CHIMP_MB);
3334
3335         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3336
3337         HWRM_CHECK_RESULT();
3338         HWRM_UNLOCK();
3339
3340         return rc;
3341 }
3342
3343 int bnxt_hwrm_func_cfg_def_cp(struct bnxt *bp)
3344 {
3345         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3346         struct hwrm_func_cfg_input req = {0};
3347         int rc;
3348
3349         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
3350
3351         req.fid = rte_cpu_to_le_16(0xffff);
3352         req.flags = rte_cpu_to_le_32(bp->pf.func_cfg_flags);
3353         req.enables = rte_cpu_to_le_32(
3354                         HWRM_FUNC_CFG_INPUT_ENABLES_ASYNC_EVENT_CR);
3355         req.async_event_cr = rte_cpu_to_le_16(
3356                         bp->async_cp_ring->cp_ring_struct->fw_ring_id);
3357         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3358
3359         HWRM_CHECK_RESULT();
3360         HWRM_UNLOCK();
3361
3362         return rc;
3363 }
3364
3365 int bnxt_hwrm_vf_func_cfg_def_cp(struct bnxt *bp)
3366 {
3367         struct hwrm_func_vf_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3368         struct hwrm_func_vf_cfg_input req = {0};
3369         int rc;
3370
3371         HWRM_PREP(req, FUNC_VF_CFG, BNXT_USE_CHIMP_MB);
3372
3373         req.enables = rte_cpu_to_le_32(
3374                         HWRM_FUNC_VF_CFG_INPUT_ENABLES_ASYNC_EVENT_CR);
3375         req.async_event_cr = rte_cpu_to_le_16(
3376                         bp->async_cp_ring->cp_ring_struct->fw_ring_id);
3377         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3378
3379         HWRM_CHECK_RESULT();
3380         HWRM_UNLOCK();
3381
3382         return rc;
3383 }
3384
3385 int bnxt_hwrm_set_default_vlan(struct bnxt *bp, int vf, uint8_t is_vf)
3386 {
3387         struct hwrm_func_cfg_input req = {0};
3388         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3389         uint16_t dflt_vlan, fid;
3390         uint32_t func_cfg_flags;
3391         int rc = 0;
3392
3393         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
3394
3395         if (is_vf) {
3396                 dflt_vlan = bp->pf.vf_info[vf].dflt_vlan;
3397                 fid = bp->pf.vf_info[vf].fid;
3398                 func_cfg_flags = bp->pf.vf_info[vf].func_cfg_flags;
3399         } else {
3400                 fid = rte_cpu_to_le_16(0xffff);
3401                 func_cfg_flags = bp->pf.func_cfg_flags;
3402                 dflt_vlan = bp->vlan;
3403         }
3404
3405         req.flags = rte_cpu_to_le_32(func_cfg_flags);
3406         req.fid = rte_cpu_to_le_16(fid);
3407         req.enables |= rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_VLAN);
3408         req.dflt_vlan = rte_cpu_to_le_16(dflt_vlan);
3409
3410         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3411
3412         HWRM_CHECK_RESULT();
3413         HWRM_UNLOCK();
3414
3415         return rc;
3416 }
3417
3418 int bnxt_hwrm_func_bw_cfg(struct bnxt *bp, uint16_t vf,
3419                         uint16_t max_bw, uint16_t enables)
3420 {
3421         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3422         struct hwrm_func_cfg_input req = {0};
3423         int rc;
3424
3425         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
3426
3427         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
3428         req.enables |= rte_cpu_to_le_32(enables);
3429         req.flags = rte_cpu_to_le_32(bp->pf.vf_info[vf].func_cfg_flags);
3430         req.max_bw = rte_cpu_to_le_32(max_bw);
3431         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3432
3433         HWRM_CHECK_RESULT();
3434         HWRM_UNLOCK();
3435
3436         return rc;
3437 }
3438
3439 int bnxt_hwrm_set_vf_vlan(struct bnxt *bp, int vf)
3440 {
3441         struct hwrm_func_cfg_input req = {0};
3442         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3443         int rc = 0;
3444
3445         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
3446
3447         req.flags = rte_cpu_to_le_32(bp->pf.vf_info[vf].func_cfg_flags);
3448         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
3449         req.enables |= rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_VLAN);
3450         req.dflt_vlan = rte_cpu_to_le_16(bp->pf.vf_info[vf].dflt_vlan);
3451
3452         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3453
3454         HWRM_CHECK_RESULT();
3455         HWRM_UNLOCK();
3456
3457         return rc;
3458 }
3459
3460 int bnxt_hwrm_set_async_event_cr(struct bnxt *bp)
3461 {
3462         int rc;
3463
3464         if (BNXT_PF(bp))
3465                 rc = bnxt_hwrm_func_cfg_def_cp(bp);
3466         else
3467                 rc = bnxt_hwrm_vf_func_cfg_def_cp(bp);
3468
3469         return rc;
3470 }
3471
3472 int bnxt_hwrm_reject_fwd_resp(struct bnxt *bp, uint16_t target_id,
3473                               void *encaped, size_t ec_size)
3474 {
3475         int rc = 0;
3476         struct hwrm_reject_fwd_resp_input req = {.req_type = 0};
3477         struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
3478
3479         if (ec_size > sizeof(req.encap_request))
3480                 return -1;
3481
3482         HWRM_PREP(req, REJECT_FWD_RESP, BNXT_USE_CHIMP_MB);
3483
3484         req.encap_resp_target_id = rte_cpu_to_le_16(target_id);
3485         memcpy(req.encap_request, encaped, ec_size);
3486
3487         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3488
3489         HWRM_CHECK_RESULT();
3490         HWRM_UNLOCK();
3491
3492         return rc;
3493 }
3494
3495 int bnxt_hwrm_func_qcfg_vf_default_mac(struct bnxt *bp, uint16_t vf,
3496                                        struct rte_ether_addr *mac)
3497 {
3498         struct hwrm_func_qcfg_input req = {0};
3499         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3500         int rc;
3501
3502         HWRM_PREP(req, FUNC_QCFG, BNXT_USE_CHIMP_MB);
3503
3504         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
3505         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3506
3507         HWRM_CHECK_RESULT();
3508
3509         memcpy(mac->addr_bytes, resp->mac_address, RTE_ETHER_ADDR_LEN);
3510
3511         HWRM_UNLOCK();
3512
3513         return rc;
3514 }
3515
3516 int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, uint16_t target_id,
3517                             void *encaped, size_t ec_size)
3518 {
3519         int rc = 0;
3520         struct hwrm_exec_fwd_resp_input req = {.req_type = 0};
3521         struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
3522
3523         if (ec_size > sizeof(req.encap_request))
3524                 return -1;
3525
3526         HWRM_PREP(req, EXEC_FWD_RESP, BNXT_USE_CHIMP_MB);
3527
3528         req.encap_resp_target_id = rte_cpu_to_le_16(target_id);
3529         memcpy(req.encap_request, encaped, ec_size);
3530
3531         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3532
3533         HWRM_CHECK_RESULT();
3534         HWRM_UNLOCK();
3535
3536         return rc;
3537 }
3538
3539 int bnxt_hwrm_ctx_qstats(struct bnxt *bp, uint32_t cid, int idx,
3540                          struct rte_eth_stats *stats, uint8_t rx)
3541 {
3542         int rc = 0;
3543         struct hwrm_stat_ctx_query_input req = {.req_type = 0};
3544         struct hwrm_stat_ctx_query_output *resp = bp->hwrm_cmd_resp_addr;
3545
3546         HWRM_PREP(req, STAT_CTX_QUERY, BNXT_USE_CHIMP_MB);
3547
3548         req.stat_ctx_id = rte_cpu_to_le_32(cid);
3549
3550         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3551
3552         HWRM_CHECK_RESULT();
3553
3554         if (rx) {
3555                 stats->q_ipackets[idx] = rte_le_to_cpu_64(resp->rx_ucast_pkts);
3556                 stats->q_ipackets[idx] += rte_le_to_cpu_64(resp->rx_mcast_pkts);
3557                 stats->q_ipackets[idx] += rte_le_to_cpu_64(resp->rx_bcast_pkts);
3558                 stats->q_ibytes[idx] = rte_le_to_cpu_64(resp->rx_ucast_bytes);
3559                 stats->q_ibytes[idx] += rte_le_to_cpu_64(resp->rx_mcast_bytes);
3560                 stats->q_ibytes[idx] += rte_le_to_cpu_64(resp->rx_bcast_bytes);
3561                 stats->q_errors[idx] = rte_le_to_cpu_64(resp->rx_err_pkts);
3562                 stats->q_errors[idx] += rte_le_to_cpu_64(resp->rx_drop_pkts);
3563         } else {
3564                 stats->q_opackets[idx] = rte_le_to_cpu_64(resp->tx_ucast_pkts);
3565                 stats->q_opackets[idx] += rte_le_to_cpu_64(resp->tx_mcast_pkts);
3566                 stats->q_opackets[idx] += rte_le_to_cpu_64(resp->tx_bcast_pkts);
3567                 stats->q_obytes[idx] = rte_le_to_cpu_64(resp->tx_ucast_bytes);
3568                 stats->q_obytes[idx] += rte_le_to_cpu_64(resp->tx_mcast_bytes);
3569                 stats->q_obytes[idx] += rte_le_to_cpu_64(resp->tx_bcast_bytes);
3570         }
3571
3572
3573         HWRM_UNLOCK();
3574
3575         return rc;
3576 }
3577
3578 int bnxt_hwrm_port_qstats(struct bnxt *bp)
3579 {
3580         struct hwrm_port_qstats_input req = {0};
3581         struct hwrm_port_qstats_output *resp = bp->hwrm_cmd_resp_addr;
3582         struct bnxt_pf_info *pf = &bp->pf;
3583         int rc;
3584
3585         HWRM_PREP(req, PORT_QSTATS, BNXT_USE_CHIMP_MB);
3586
3587         req.port_id = rte_cpu_to_le_16(pf->port_id);
3588         req.tx_stat_host_addr = rte_cpu_to_le_64(bp->hw_tx_port_stats_map);
3589         req.rx_stat_host_addr = rte_cpu_to_le_64(bp->hw_rx_port_stats_map);
3590         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3591
3592         HWRM_CHECK_RESULT();
3593         HWRM_UNLOCK();
3594
3595         return rc;
3596 }
3597
3598 int bnxt_hwrm_port_clr_stats(struct bnxt *bp)
3599 {
3600         struct hwrm_port_clr_stats_input req = {0};
3601         struct hwrm_port_clr_stats_output *resp = bp->hwrm_cmd_resp_addr;
3602         struct bnxt_pf_info *pf = &bp->pf;
3603         int rc;
3604
3605         /* Not allowed on NS2 device, NPAR, MultiHost, VF */
3606         if (!(bp->flags & BNXT_FLAG_PORT_STATS) || BNXT_VF(bp) ||
3607             BNXT_NPAR(bp) || BNXT_MH(bp) || BNXT_TOTAL_VFS(bp))
3608                 return 0;
3609
3610         HWRM_PREP(req, PORT_CLR_STATS, BNXT_USE_CHIMP_MB);
3611
3612         req.port_id = rte_cpu_to_le_16(pf->port_id);
3613         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3614
3615         HWRM_CHECK_RESULT();
3616         HWRM_UNLOCK();
3617
3618         return rc;
3619 }
3620
3621 int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
3622 {
3623         struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
3624         struct hwrm_port_led_qcaps_input req = {0};
3625         int rc;
3626
3627         if (BNXT_VF(bp))
3628                 return 0;
3629
3630         HWRM_PREP(req, PORT_LED_QCAPS, BNXT_USE_CHIMP_MB);
3631         req.port_id = bp->pf.port_id;
3632         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3633
3634         HWRM_CHECK_RESULT();
3635
3636         if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
3637                 unsigned int i;
3638
3639                 bp->num_leds = resp->num_leds;
3640                 memcpy(bp->leds, &resp->led0_id,
3641                         sizeof(bp->leds[0]) * bp->num_leds);
3642                 for (i = 0; i < bp->num_leds; i++) {
3643                         struct bnxt_led_info *led = &bp->leds[i];
3644
3645                         uint16_t caps = led->led_state_caps;
3646
3647                         if (!led->led_group_id ||
3648                                 !BNXT_LED_ALT_BLINK_CAP(caps)) {
3649                                 bp->num_leds = 0;
3650                                 break;
3651                         }
3652                 }
3653         }
3654
3655         HWRM_UNLOCK();
3656
3657         return rc;
3658 }
3659
3660 int bnxt_hwrm_port_led_cfg(struct bnxt *bp, bool led_on)
3661 {
3662         struct hwrm_port_led_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3663         struct hwrm_port_led_cfg_input req = {0};
3664         struct bnxt_led_cfg *led_cfg;
3665         uint8_t led_state = HWRM_PORT_LED_QCFG_OUTPUT_LED0_STATE_DEFAULT;
3666         uint16_t duration = 0;
3667         int rc, i;
3668
3669         if (!bp->num_leds || BNXT_VF(bp))
3670                 return -EOPNOTSUPP;
3671
3672         HWRM_PREP(req, PORT_LED_CFG, BNXT_USE_CHIMP_MB);
3673
3674         if (led_on) {
3675                 led_state = HWRM_PORT_LED_CFG_INPUT_LED0_STATE_BLINKALT;
3676                 duration = rte_cpu_to_le_16(500);
3677         }
3678         req.port_id = bp->pf.port_id;
3679         req.num_leds = bp->num_leds;
3680         led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
3681         for (i = 0; i < bp->num_leds; i++, led_cfg++) {
3682                 req.enables |= BNXT_LED_DFLT_ENABLES(i);
3683                 led_cfg->led_id = bp->leds[i].led_id;
3684                 led_cfg->led_state = led_state;
3685                 led_cfg->led_blink_on = duration;
3686                 led_cfg->led_blink_off = duration;
3687                 led_cfg->led_group_id = bp->leds[i].led_group_id;
3688         }
3689
3690         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3691
3692         HWRM_CHECK_RESULT();
3693         HWRM_UNLOCK();
3694
3695         return rc;
3696 }
3697
3698 int bnxt_hwrm_nvm_get_dir_info(struct bnxt *bp, uint32_t *entries,
3699                                uint32_t *length)
3700 {
3701         int rc;
3702         struct hwrm_nvm_get_dir_info_input req = {0};
3703         struct hwrm_nvm_get_dir_info_output *resp = bp->hwrm_cmd_resp_addr;
3704
3705         HWRM_PREP(req, NVM_GET_DIR_INFO, BNXT_USE_CHIMP_MB);
3706
3707         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3708
3709         HWRM_CHECK_RESULT();
3710
3711         *entries = rte_le_to_cpu_32(resp->entries);
3712         *length = rte_le_to_cpu_32(resp->entry_length);
3713
3714         HWRM_UNLOCK();
3715         return rc;
3716 }
3717
3718 int bnxt_get_nvram_directory(struct bnxt *bp, uint32_t len, uint8_t *data)
3719 {
3720         int rc;
3721         uint32_t dir_entries;
3722         uint32_t entry_length;
3723         uint8_t *buf;
3724         size_t buflen;
3725         rte_iova_t dma_handle;
3726         struct hwrm_nvm_get_dir_entries_input req = {0};
3727         struct hwrm_nvm_get_dir_entries_output *resp = bp->hwrm_cmd_resp_addr;
3728
3729         rc = bnxt_hwrm_nvm_get_dir_info(bp, &dir_entries, &entry_length);
3730         if (rc != 0)
3731                 return rc;
3732
3733         *data++ = dir_entries;
3734         *data++ = entry_length;
3735         len -= 2;
3736         memset(data, 0xff, len);
3737
3738         buflen = dir_entries * entry_length;
3739         buf = rte_malloc("nvm_dir", buflen, 0);
3740         rte_mem_lock_page(buf);
3741         if (buf == NULL)
3742                 return -ENOMEM;
3743         dma_handle = rte_mem_virt2iova(buf);
3744         if (dma_handle == RTE_BAD_IOVA) {
3745                 PMD_DRV_LOG(ERR,
3746                         "unable to map response address to physical memory\n");
3747                 return -ENOMEM;
3748         }
3749         HWRM_PREP(req, NVM_GET_DIR_ENTRIES, BNXT_USE_CHIMP_MB);
3750         req.host_dest_addr = rte_cpu_to_le_64(dma_handle);
3751         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3752
3753         if (rc == 0)
3754                 memcpy(data, buf, len > buflen ? buflen : len);
3755
3756         rte_free(buf);
3757         HWRM_CHECK_RESULT();
3758         HWRM_UNLOCK();
3759
3760         return rc;
3761 }
3762
3763 int bnxt_hwrm_get_nvram_item(struct bnxt *bp, uint32_t index,
3764                              uint32_t offset, uint32_t length,
3765                              uint8_t *data)
3766 {
3767         int rc;
3768         uint8_t *buf;
3769         rte_iova_t dma_handle;
3770         struct hwrm_nvm_read_input req = {0};
3771         struct hwrm_nvm_read_output *resp = bp->hwrm_cmd_resp_addr;
3772
3773         buf = rte_malloc("nvm_item", length, 0);
3774         rte_mem_lock_page(buf);
3775         if (!buf)
3776                 return -ENOMEM;
3777
3778         dma_handle = rte_mem_virt2iova(buf);
3779         if (dma_handle == RTE_BAD_IOVA) {
3780                 PMD_DRV_LOG(ERR,
3781                         "unable to map response address to physical memory\n");
3782                 return -ENOMEM;
3783         }
3784         HWRM_PREP(req, NVM_READ, BNXT_USE_CHIMP_MB);
3785         req.host_dest_addr = rte_cpu_to_le_64(dma_handle);
3786         req.dir_idx = rte_cpu_to_le_16(index);
3787         req.offset = rte_cpu_to_le_32(offset);
3788         req.len = rte_cpu_to_le_32(length);
3789         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3790         if (rc == 0)
3791                 memcpy(data, buf, length);
3792
3793         rte_free(buf);
3794         HWRM_CHECK_RESULT();
3795         HWRM_UNLOCK();
3796
3797         return rc;
3798 }
3799
3800 int bnxt_hwrm_erase_nvram_directory(struct bnxt *bp, uint8_t index)
3801 {
3802         int rc;
3803         struct hwrm_nvm_erase_dir_entry_input req = {0};
3804         struct hwrm_nvm_erase_dir_entry_output *resp = bp->hwrm_cmd_resp_addr;
3805
3806         HWRM_PREP(req, NVM_ERASE_DIR_ENTRY, BNXT_USE_CHIMP_MB);
3807         req.dir_idx = rte_cpu_to_le_16(index);
3808         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3809         HWRM_CHECK_RESULT();
3810         HWRM_UNLOCK();
3811
3812         return rc;
3813 }
3814
3815
3816 int bnxt_hwrm_flash_nvram(struct bnxt *bp, uint16_t dir_type,
3817                           uint16_t dir_ordinal, uint16_t dir_ext,
3818                           uint16_t dir_attr, const uint8_t *data,
3819                           size_t data_len)
3820 {
3821         int rc;
3822         struct hwrm_nvm_write_input req = {0};
3823         struct hwrm_nvm_write_output *resp = bp->hwrm_cmd_resp_addr;
3824         rte_iova_t dma_handle;
3825         uint8_t *buf;
3826
3827         buf = rte_malloc("nvm_write", data_len, 0);
3828         rte_mem_lock_page(buf);
3829         if (!buf)
3830                 return -ENOMEM;
3831
3832         dma_handle = rte_mem_virt2iova(buf);
3833         if (dma_handle == RTE_BAD_IOVA) {
3834                 PMD_DRV_LOG(ERR,
3835                         "unable to map response address to physical memory\n");
3836                 return -ENOMEM;
3837         }
3838         memcpy(buf, data, data_len);
3839
3840         HWRM_PREP(req, NVM_WRITE, BNXT_USE_CHIMP_MB);
3841
3842         req.dir_type = rte_cpu_to_le_16(dir_type);
3843         req.dir_ordinal = rte_cpu_to_le_16(dir_ordinal);
3844         req.dir_ext = rte_cpu_to_le_16(dir_ext);
3845         req.dir_attr = rte_cpu_to_le_16(dir_attr);
3846         req.dir_data_length = rte_cpu_to_le_32(data_len);
3847         req.host_src_addr = rte_cpu_to_le_64(dma_handle);
3848
3849         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3850
3851         rte_free(buf);
3852         HWRM_CHECK_RESULT();
3853         HWRM_UNLOCK();
3854
3855         return rc;
3856 }
3857
3858 static void
3859 bnxt_vnic_count(struct bnxt_vnic_info *vnic __rte_unused, void *cbdata)
3860 {
3861         uint32_t *count = cbdata;
3862
3863         *count = *count + 1;
3864 }
3865
3866 static int bnxt_vnic_count_hwrm_stub(struct bnxt *bp __rte_unused,
3867                                      struct bnxt_vnic_info *vnic __rte_unused)
3868 {
3869         return 0;
3870 }
3871
3872 int bnxt_vf_vnic_count(struct bnxt *bp, uint16_t vf)
3873 {
3874         uint32_t count = 0;
3875
3876         bnxt_hwrm_func_vf_vnic_query_and_config(bp, vf, bnxt_vnic_count,
3877             &count, bnxt_vnic_count_hwrm_stub);
3878
3879         return count;
3880 }
3881
3882 static int bnxt_hwrm_func_vf_vnic_query(struct bnxt *bp, uint16_t vf,
3883                                         uint16_t *vnic_ids)
3884 {
3885         struct hwrm_func_vf_vnic_ids_query_input req = {0};
3886         struct hwrm_func_vf_vnic_ids_query_output *resp =
3887                                                 bp->hwrm_cmd_resp_addr;
3888         int rc;
3889
3890         /* First query all VNIC ids */
3891         HWRM_PREP(req, FUNC_VF_VNIC_IDS_QUERY, BNXT_USE_CHIMP_MB);
3892
3893         req.vf_id = rte_cpu_to_le_16(bp->pf.first_vf_id + vf);
3894         req.max_vnic_id_cnt = rte_cpu_to_le_32(bp->pf.total_vnics);
3895         req.vnic_id_tbl_addr = rte_cpu_to_le_64(rte_mem_virt2iova(vnic_ids));
3896
3897         if (req.vnic_id_tbl_addr == RTE_BAD_IOVA) {
3898                 HWRM_UNLOCK();
3899                 PMD_DRV_LOG(ERR,
3900                 "unable to map VNIC ID table address to physical memory\n");
3901                 return -ENOMEM;
3902         }
3903         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3904         HWRM_CHECK_RESULT();
3905         rc = rte_le_to_cpu_32(resp->vnic_id_cnt);
3906
3907         HWRM_UNLOCK();
3908
3909         return rc;
3910 }
3911
3912 /*
3913  * This function queries the VNIC IDs  for a specified VF. It then calls
3914  * the vnic_cb to update the necessary field in vnic_info with cbdata.
3915  * Then it calls the hwrm_cb function to program this new vnic configuration.
3916  */
3917 int bnxt_hwrm_func_vf_vnic_query_and_config(struct bnxt *bp, uint16_t vf,
3918         void (*vnic_cb)(struct bnxt_vnic_info *, void *), void *cbdata,
3919         int (*hwrm_cb)(struct bnxt *bp, struct bnxt_vnic_info *vnic))
3920 {
3921         struct bnxt_vnic_info vnic;
3922         int rc = 0;
3923         int i, num_vnic_ids;
3924         uint16_t *vnic_ids;
3925         size_t vnic_id_sz;
3926         size_t sz;
3927
3928         /* First query all VNIC ids */
3929         vnic_id_sz = bp->pf.total_vnics * sizeof(*vnic_ids);
3930         vnic_ids = rte_malloc("bnxt_hwrm_vf_vnic_ids_query", vnic_id_sz,
3931                         RTE_CACHE_LINE_SIZE);
3932         if (vnic_ids == NULL)
3933                 return -ENOMEM;
3934
3935         for (sz = 0; sz < vnic_id_sz; sz += getpagesize())
3936                 rte_mem_lock_page(((char *)vnic_ids) + sz);
3937
3938         num_vnic_ids = bnxt_hwrm_func_vf_vnic_query(bp, vf, vnic_ids);
3939
3940         if (num_vnic_ids < 0)
3941                 return num_vnic_ids;
3942
3943         /* Retrieve VNIC, update bd_stall then update */
3944
3945         for (i = 0; i < num_vnic_ids; i++) {
3946                 memset(&vnic, 0, sizeof(struct bnxt_vnic_info));
3947                 vnic.fw_vnic_id = rte_le_to_cpu_16(vnic_ids[i]);
3948                 rc = bnxt_hwrm_vnic_qcfg(bp, &vnic, bp->pf.first_vf_id + vf);
3949                 if (rc)
3950                         break;
3951                 if (vnic.mru <= 4)      /* Indicates unallocated */
3952                         continue;
3953
3954                 vnic_cb(&vnic, cbdata);
3955
3956                 rc = hwrm_cb(bp, &vnic);
3957                 if (rc)
3958                         break;
3959         }
3960
3961         rte_free(vnic_ids);
3962
3963         return rc;
3964 }
3965
3966 int bnxt_hwrm_func_cfg_vf_set_vlan_anti_spoof(struct bnxt *bp, uint16_t vf,
3967                                               bool on)
3968 {
3969         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3970         struct hwrm_func_cfg_input req = {0};
3971         int rc;
3972
3973         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
3974
3975         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
3976         req.enables |= rte_cpu_to_le_32(
3977                         HWRM_FUNC_CFG_INPUT_ENABLES_VLAN_ANTISPOOF_MODE);
3978         req.vlan_antispoof_mode = on ?
3979                 HWRM_FUNC_CFG_INPUT_VLAN_ANTISPOOF_MODE_VALIDATE_VLAN :
3980                 HWRM_FUNC_CFG_INPUT_VLAN_ANTISPOOF_MODE_NOCHECK;
3981         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3982
3983         HWRM_CHECK_RESULT();
3984         HWRM_UNLOCK();
3985
3986         return rc;
3987 }
3988
3989 int bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(struct bnxt *bp, int vf)
3990 {
3991         struct bnxt_vnic_info vnic;
3992         uint16_t *vnic_ids;
3993         size_t vnic_id_sz;
3994         int num_vnic_ids, i;
3995         size_t sz;
3996         int rc;
3997
3998         vnic_id_sz = bp->pf.total_vnics * sizeof(*vnic_ids);
3999         vnic_ids = rte_malloc("bnxt_hwrm_vf_vnic_ids_query", vnic_id_sz,
4000                         RTE_CACHE_LINE_SIZE);
4001         if (vnic_ids == NULL)
4002                 return -ENOMEM;
4003
4004         for (sz = 0; sz < vnic_id_sz; sz += getpagesize())
4005                 rte_mem_lock_page(((char *)vnic_ids) + sz);
4006
4007         rc = bnxt_hwrm_func_vf_vnic_query(bp, vf, vnic_ids);
4008         if (rc <= 0)
4009                 goto exit;
4010         num_vnic_ids = rc;
4011
4012         /*
4013          * Loop through to find the default VNIC ID.
4014          * TODO: The easier way would be to obtain the resp->dflt_vnic_id
4015          * by sending the hwrm_func_qcfg command to the firmware.
4016          */
4017         for (i = 0; i < num_vnic_ids; i++) {
4018                 memset(&vnic, 0, sizeof(struct bnxt_vnic_info));
4019                 vnic.fw_vnic_id = rte_le_to_cpu_16(vnic_ids[i]);
4020                 rc = bnxt_hwrm_vnic_qcfg(bp, &vnic,
4021                                         bp->pf.first_vf_id + vf);
4022                 if (rc)
4023                         goto exit;
4024                 if (vnic.func_default) {
4025                         rte_free(vnic_ids);
4026                         return vnic.fw_vnic_id;
4027                 }
4028         }
4029         /* Could not find a default VNIC. */
4030         PMD_DRV_LOG(ERR, "No default VNIC\n");
4031 exit:
4032         rte_free(vnic_ids);
4033         return rc;
4034 }
4035
4036 int bnxt_hwrm_set_em_filter(struct bnxt *bp,
4037                          uint16_t dst_id,
4038                          struct bnxt_filter_info *filter)
4039 {
4040         int rc = 0;
4041         struct hwrm_cfa_em_flow_alloc_input req = {.req_type = 0 };
4042         struct hwrm_cfa_em_flow_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4043         uint32_t enables = 0;
4044
4045         if (filter->fw_em_filter_id != UINT64_MAX)
4046                 bnxt_hwrm_clear_em_filter(bp, filter);
4047
4048         HWRM_PREP(req, CFA_EM_FLOW_ALLOC, BNXT_USE_KONG(bp));
4049
4050         req.flags = rte_cpu_to_le_32(filter->flags);
4051
4052         enables = filter->enables |
4053               HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_DST_ID;
4054         req.dst_id = rte_cpu_to_le_16(dst_id);
4055
4056         if (filter->ip_addr_type) {
4057                 req.ip_addr_type = filter->ip_addr_type;
4058                 enables |= HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_IPADDR_TYPE;
4059         }
4060         if (enables &
4061             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_L2_FILTER_ID)
4062                 req.l2_filter_id = rte_cpu_to_le_64(filter->fw_l2_filter_id);
4063         if (enables &
4064             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_SRC_MACADDR)
4065                 memcpy(req.src_macaddr, filter->src_macaddr,
4066                        RTE_ETHER_ADDR_LEN);
4067         if (enables &
4068             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_DST_MACADDR)
4069                 memcpy(req.dst_macaddr, filter->dst_macaddr,
4070                        RTE_ETHER_ADDR_LEN);
4071         if (enables &
4072             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_OVLAN_VID)
4073                 req.ovlan_vid = filter->l2_ovlan;
4074         if (enables &
4075             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_IVLAN_VID)
4076                 req.ivlan_vid = filter->l2_ivlan;
4077         if (enables &
4078             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_ETHERTYPE)
4079                 req.ethertype = rte_cpu_to_be_16(filter->ethertype);
4080         if (enables &
4081             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_IP_PROTOCOL)
4082                 req.ip_protocol = filter->ip_protocol;
4083         if (enables &
4084             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_SRC_IPADDR)
4085                 req.src_ipaddr[0] = rte_cpu_to_be_32(filter->src_ipaddr[0]);
4086         if (enables &
4087             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_DST_IPADDR)
4088                 req.dst_ipaddr[0] = rte_cpu_to_be_32(filter->dst_ipaddr[0]);
4089         if (enables &
4090             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_SRC_PORT)
4091                 req.src_port = rte_cpu_to_be_16(filter->src_port);
4092         if (enables &
4093             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_DST_PORT)
4094                 req.dst_port = rte_cpu_to_be_16(filter->dst_port);
4095         if (enables &
4096             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_MIRROR_VNIC_ID)
4097                 req.mirror_vnic_id = filter->mirror_vnic_id;
4098
4099         req.enables = rte_cpu_to_le_32(enables);
4100
4101         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_KONG(bp));
4102
4103         HWRM_CHECK_RESULT();
4104
4105         filter->fw_em_filter_id = rte_le_to_cpu_64(resp->em_filter_id);
4106         HWRM_UNLOCK();
4107
4108         return rc;
4109 }
4110
4111 int bnxt_hwrm_clear_em_filter(struct bnxt *bp, struct bnxt_filter_info *filter)
4112 {
4113         int rc = 0;
4114         struct hwrm_cfa_em_flow_free_input req = {.req_type = 0 };
4115         struct hwrm_cfa_em_flow_free_output *resp = bp->hwrm_cmd_resp_addr;
4116
4117         if (filter->fw_em_filter_id == UINT64_MAX)
4118                 return 0;
4119
4120         PMD_DRV_LOG(ERR, "Clear EM filter\n");
4121         HWRM_PREP(req, CFA_EM_FLOW_FREE, BNXT_USE_KONG(bp));
4122
4123         req.em_filter_id = rte_cpu_to_le_64(filter->fw_em_filter_id);
4124
4125         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_KONG(bp));
4126
4127         HWRM_CHECK_RESULT();
4128         HWRM_UNLOCK();
4129
4130         filter->fw_em_filter_id = UINT64_MAX;
4131         filter->fw_l2_filter_id = UINT64_MAX;
4132
4133         return 0;
4134 }
4135
4136 int bnxt_hwrm_set_ntuple_filter(struct bnxt *bp,
4137                          uint16_t dst_id,
4138                          struct bnxt_filter_info *filter)
4139 {
4140         int rc = 0;
4141         struct hwrm_cfa_ntuple_filter_alloc_input req = {.req_type = 0 };
4142         struct hwrm_cfa_ntuple_filter_alloc_output *resp =
4143                                                 bp->hwrm_cmd_resp_addr;
4144         uint32_t enables = 0;
4145
4146         if (filter->fw_ntuple_filter_id != UINT64_MAX)
4147                 bnxt_hwrm_clear_ntuple_filter(bp, filter);
4148
4149         HWRM_PREP(req, CFA_NTUPLE_FILTER_ALLOC, BNXT_USE_CHIMP_MB);
4150
4151         req.flags = rte_cpu_to_le_32(filter->flags);
4152
4153         enables = filter->enables |
4154               HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_ID;
4155         req.dst_id = rte_cpu_to_le_16(dst_id);
4156
4157
4158         if (filter->ip_addr_type) {
4159                 req.ip_addr_type = filter->ip_addr_type;
4160                 enables |=
4161                         HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_IPADDR_TYPE;
4162         }
4163         if (enables &
4164             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_L2_FILTER_ID)
4165                 req.l2_filter_id = rte_cpu_to_le_64(filter->fw_l2_filter_id);
4166         if (enables &
4167             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_MACADDR)
4168                 memcpy(req.src_macaddr, filter->src_macaddr,
4169                        RTE_ETHER_ADDR_LEN);
4170         //if (enables &
4171             //HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_MACADDR)
4172                 //memcpy(req.dst_macaddr, filter->dst_macaddr,
4173                        //RTE_ETHER_ADDR_LEN);
4174         if (enables &
4175             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_ETHERTYPE)
4176                 req.ethertype = rte_cpu_to_be_16(filter->ethertype);
4177         if (enables &
4178             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_IP_PROTOCOL)
4179                 req.ip_protocol = filter->ip_protocol;
4180         if (enables &
4181             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_IPADDR)
4182                 req.src_ipaddr[0] = rte_cpu_to_le_32(filter->src_ipaddr[0]);
4183         if (enables &
4184             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_IPADDR_MASK)
4185                 req.src_ipaddr_mask[0] =
4186                         rte_cpu_to_le_32(filter->src_ipaddr_mask[0]);
4187         if (enables &
4188             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_IPADDR)
4189                 req.dst_ipaddr[0] = rte_cpu_to_le_32(filter->dst_ipaddr[0]);
4190         if (enables &
4191             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_IPADDR_MASK)
4192                 req.dst_ipaddr_mask[0] =
4193                         rte_cpu_to_be_32(filter->dst_ipaddr_mask[0]);
4194         if (enables &
4195             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_PORT)
4196                 req.src_port = rte_cpu_to_le_16(filter->src_port);
4197         if (enables &
4198             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_PORT_MASK)
4199                 req.src_port_mask = rte_cpu_to_le_16(filter->src_port_mask);
4200         if (enables &
4201             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_PORT)
4202                 req.dst_port = rte_cpu_to_le_16(filter->dst_port);
4203         if (enables &
4204             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_PORT_MASK)
4205                 req.dst_port_mask = rte_cpu_to_le_16(filter->dst_port_mask);
4206         if (enables &
4207             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_MIRROR_VNIC_ID)
4208                 req.mirror_vnic_id = filter->mirror_vnic_id;
4209
4210         req.enables = rte_cpu_to_le_32(enables);
4211
4212         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4213
4214         HWRM_CHECK_RESULT();
4215
4216         filter->fw_ntuple_filter_id = rte_le_to_cpu_64(resp->ntuple_filter_id);
4217         HWRM_UNLOCK();
4218
4219         return rc;
4220 }
4221
4222 int bnxt_hwrm_clear_ntuple_filter(struct bnxt *bp,
4223                                 struct bnxt_filter_info *filter)
4224 {
4225         int rc = 0;
4226         struct hwrm_cfa_ntuple_filter_free_input req = {.req_type = 0 };
4227         struct hwrm_cfa_ntuple_filter_free_output *resp =
4228                                                 bp->hwrm_cmd_resp_addr;
4229
4230         if (filter->fw_ntuple_filter_id == UINT64_MAX)
4231                 return 0;
4232
4233         HWRM_PREP(req, CFA_NTUPLE_FILTER_FREE, BNXT_USE_CHIMP_MB);
4234
4235         req.ntuple_filter_id = rte_cpu_to_le_64(filter->fw_ntuple_filter_id);
4236
4237         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4238
4239         HWRM_CHECK_RESULT();
4240         HWRM_UNLOCK();
4241
4242         filter->fw_ntuple_filter_id = UINT64_MAX;
4243
4244         return 0;
4245 }
4246
4247 static int
4248 bnxt_vnic_rss_configure_thor(struct bnxt *bp, struct bnxt_vnic_info *vnic)
4249 {
4250         struct hwrm_vnic_rss_cfg_output *resp = bp->hwrm_cmd_resp_addr;
4251         uint8_t *rx_queue_state = bp->eth_dev->data->rx_queue_state;
4252         struct hwrm_vnic_rss_cfg_input req = {.req_type = 0 };
4253         struct bnxt_rx_queue **rxqs = bp->rx_queues;
4254         uint16_t *ring_tbl = vnic->rss_table;
4255         int nr_ctxs = vnic->num_lb_ctxts;
4256         int max_rings = bp->rx_nr_rings;
4257         int i, j, k, cnt;
4258         int rc = 0;
4259
4260         for (i = 0, k = 0; i < nr_ctxs; i++) {
4261                 struct bnxt_rx_ring_info *rxr;
4262                 struct bnxt_cp_ring_info *cpr;
4263
4264                 HWRM_PREP(req, VNIC_RSS_CFG, BNXT_USE_CHIMP_MB);
4265
4266                 req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
4267                 req.hash_type = rte_cpu_to_le_32(vnic->hash_type);
4268                 req.hash_mode_flags = vnic->hash_mode;
4269
4270                 req.ring_grp_tbl_addr =
4271                     rte_cpu_to_le_64(vnic->rss_table_dma_addr +
4272                                      i * BNXT_RSS_ENTRIES_PER_CTX_THOR *
4273                                      2 * sizeof(*ring_tbl));
4274                 req.hash_key_tbl_addr =
4275                     rte_cpu_to_le_64(vnic->rss_hash_key_dma_addr);
4276
4277                 req.ring_table_pair_index = i;
4278                 req.rss_ctx_idx = rte_cpu_to_le_16(vnic->fw_grp_ids[i]);
4279
4280                 for (j = 0; j < 64; j++) {
4281                         uint16_t ring_id;
4282
4283                         /* Find next active ring. */
4284                         for (cnt = 0; cnt < max_rings; cnt++) {
4285                                 if (rx_queue_state[k] !=
4286                                                 RTE_ETH_QUEUE_STATE_STOPPED)
4287                                         break;
4288                                 if (++k == max_rings)
4289                                         k = 0;
4290                         }
4291
4292                         /* Return if no rings are active. */
4293                         if (cnt == max_rings)
4294                                 return 0;
4295
4296                         /* Add rx/cp ring pair to RSS table. */
4297                         rxr = rxqs[k]->rx_ring;
4298                         cpr = rxqs[k]->cp_ring;
4299
4300                         ring_id = rxr->rx_ring_struct->fw_ring_id;
4301                         *ring_tbl++ = rte_cpu_to_le_16(ring_id);
4302                         ring_id = cpr->cp_ring_struct->fw_ring_id;
4303                         *ring_tbl++ = rte_cpu_to_le_16(ring_id);
4304
4305                         if (++k == max_rings)
4306                                 k = 0;
4307                 }
4308                 rc = bnxt_hwrm_send_message(bp, &req, sizeof(req),
4309                                             BNXT_USE_CHIMP_MB);
4310
4311                 HWRM_CHECK_RESULT();
4312                 HWRM_UNLOCK();
4313         }
4314
4315         return rc;
4316 }
4317
4318 int bnxt_vnic_rss_configure(struct bnxt *bp, struct bnxt_vnic_info *vnic)
4319 {
4320         unsigned int rss_idx, fw_idx, i;
4321
4322         if (!(vnic->rss_table && vnic->hash_type))
4323                 return 0;
4324
4325         if (BNXT_CHIP_THOR(bp))
4326                 return bnxt_vnic_rss_configure_thor(bp, vnic);
4327
4328         if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
4329                 return 0;
4330
4331         if (vnic->rss_table && vnic->hash_type) {
4332                 /*
4333                  * Fill the RSS hash & redirection table with
4334                  * ring group ids for all VNICs
4335                  */
4336                 for (rss_idx = 0, fw_idx = 0; rss_idx < HW_HASH_INDEX_SIZE;
4337                         rss_idx++, fw_idx++) {
4338                         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
4339                                 fw_idx %= bp->rx_cp_nr_rings;
4340                                 if (vnic->fw_grp_ids[fw_idx] !=
4341                                     INVALID_HW_RING_ID)
4342                                         break;
4343                                 fw_idx++;
4344                         }
4345                         if (i == bp->rx_cp_nr_rings)
4346                                 return 0;
4347                         vnic->rss_table[rss_idx] = vnic->fw_grp_ids[fw_idx];
4348                 }
4349                 return bnxt_hwrm_vnic_rss_cfg(bp, vnic);
4350         }
4351
4352         return 0;
4353 }
4354
4355 static void bnxt_hwrm_set_coal_params(struct bnxt_coal *hw_coal,
4356         struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4357 {
4358         uint16_t flags;
4359
4360         req->num_cmpl_aggr_int = rte_cpu_to_le_16(hw_coal->num_cmpl_aggr_int);
4361
4362         /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
4363         req->num_cmpl_dma_aggr = rte_cpu_to_le_16(hw_coal->num_cmpl_dma_aggr);
4364
4365         /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
4366         req->num_cmpl_dma_aggr_during_int =
4367                 rte_cpu_to_le_16(hw_coal->num_cmpl_dma_aggr_during_int);
4368
4369         req->int_lat_tmr_max = rte_cpu_to_le_16(hw_coal->int_lat_tmr_max);
4370
4371         /* min timer set to 1/2 of interrupt timer */
4372         req->int_lat_tmr_min = rte_cpu_to_le_16(hw_coal->int_lat_tmr_min);
4373
4374         /* buf timer set to 1/4 of interrupt timer */
4375         req->cmpl_aggr_dma_tmr = rte_cpu_to_le_16(hw_coal->cmpl_aggr_dma_tmr);
4376
4377         req->cmpl_aggr_dma_tmr_during_int =
4378                 rte_cpu_to_le_16(hw_coal->cmpl_aggr_dma_tmr_during_int);
4379
4380         flags = HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_FLAGS_TIMER_RESET |
4381                 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_FLAGS_RING_IDLE;
4382         req->flags = rte_cpu_to_le_16(flags);
4383 }
4384
4385 static int bnxt_hwrm_set_coal_params_thor(struct bnxt *bp,
4386                 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *agg_req)
4387 {
4388         struct hwrm_ring_aggint_qcaps_input req = {0};
4389         struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4390         uint32_t enables;
4391         uint16_t flags;
4392         int rc;
4393
4394         HWRM_PREP(req, RING_AGGINT_QCAPS, BNXT_USE_CHIMP_MB);
4395         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4396         HWRM_CHECK_RESULT();
4397
4398         agg_req->num_cmpl_dma_aggr = resp->num_cmpl_dma_aggr_max;
4399         agg_req->cmpl_aggr_dma_tmr = resp->cmpl_aggr_dma_tmr_min;
4400
4401         flags = HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_FLAGS_TIMER_RESET |
4402                 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_FLAGS_RING_IDLE;
4403         agg_req->flags = rte_cpu_to_le_16(flags);
4404         enables =
4405          HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_ENABLES_CMPL_AGGR_DMA_TMR |
4406          HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_ENABLES_NUM_CMPL_DMA_AGGR;
4407         agg_req->enables = rte_cpu_to_le_32(enables);
4408
4409         HWRM_UNLOCK();
4410         return rc;
4411 }
4412
4413 int bnxt_hwrm_set_ring_coal(struct bnxt *bp,
4414                         struct bnxt_coal *coal, uint16_t ring_id)
4415 {
4416         struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
4417         struct hwrm_ring_cmpl_ring_cfg_aggint_params_output *resp =
4418                                                 bp->hwrm_cmd_resp_addr;
4419         int rc;
4420
4421         /* Set ring coalesce parameters only for 100G NICs */
4422         if (BNXT_CHIP_THOR(bp)) {
4423                 if (bnxt_hwrm_set_coal_params_thor(bp, &req))
4424                         return -1;
4425         } else if (bnxt_stratus_device(bp)) {
4426                 bnxt_hwrm_set_coal_params(coal, &req);
4427         } else {
4428                 return 0;
4429         }
4430
4431         HWRM_PREP(req, RING_CMPL_RING_CFG_AGGINT_PARAMS, BNXT_USE_CHIMP_MB);
4432         req.ring_id = rte_cpu_to_le_16(ring_id);
4433         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4434         HWRM_CHECK_RESULT();
4435         HWRM_UNLOCK();
4436         return 0;
4437 }
4438
4439 #define BNXT_RTE_MEMZONE_FLAG  (RTE_MEMZONE_1GB | RTE_MEMZONE_IOVA_CONTIG)
4440 int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
4441 {
4442         struct hwrm_func_backing_store_qcaps_input req = {0};
4443         struct hwrm_func_backing_store_qcaps_output *resp =
4444                 bp->hwrm_cmd_resp_addr;
4445         struct bnxt_ctx_pg_info *ctx_pg;
4446         struct bnxt_ctx_mem_info *ctx;
4447         int total_alloc_len;
4448         int rc, i;
4449
4450         if (!BNXT_CHIP_THOR(bp) ||
4451             bp->hwrm_spec_code < HWRM_VERSION_1_9_2 ||
4452             BNXT_VF(bp) ||
4453             bp->ctx)
4454                 return 0;
4455
4456         HWRM_PREP(req, FUNC_BACKING_STORE_QCAPS, BNXT_USE_CHIMP_MB);
4457         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4458         HWRM_CHECK_RESULT_SILENT();
4459
4460         total_alloc_len = sizeof(*ctx);
4461         ctx = rte_zmalloc("bnxt_ctx_mem", total_alloc_len,
4462                           RTE_CACHE_LINE_SIZE);
4463         if (!ctx) {
4464                 rc = -ENOMEM;
4465                 goto ctx_err;
4466         }
4467
4468         ctx_pg = rte_malloc("bnxt_ctx_pg_mem",
4469                             sizeof(*ctx_pg) * BNXT_MAX_Q,
4470                             RTE_CACHE_LINE_SIZE);
4471         if (!ctx_pg) {
4472                 rc = -ENOMEM;
4473                 goto ctx_err;
4474         }
4475         for (i = 0; i < BNXT_MAX_Q; i++, ctx_pg++)
4476                 ctx->tqm_mem[i] = ctx_pg;
4477
4478         bp->ctx = ctx;
4479         ctx->qp_max_entries = rte_le_to_cpu_32(resp->qp_max_entries);
4480         ctx->qp_min_qp1_entries =
4481                 rte_le_to_cpu_16(resp->qp_min_qp1_entries);
4482         ctx->qp_max_l2_entries =
4483                 rte_le_to_cpu_16(resp->qp_max_l2_entries);
4484         ctx->qp_entry_size = rte_le_to_cpu_16(resp->qp_entry_size);
4485         ctx->srq_max_l2_entries =
4486                 rte_le_to_cpu_16(resp->srq_max_l2_entries);
4487         ctx->srq_max_entries = rte_le_to_cpu_32(resp->srq_max_entries);
4488         ctx->srq_entry_size = rte_le_to_cpu_16(resp->srq_entry_size);
4489         ctx->cq_max_l2_entries =
4490                 rte_le_to_cpu_16(resp->cq_max_l2_entries);
4491         ctx->cq_max_entries = rte_le_to_cpu_32(resp->cq_max_entries);
4492         ctx->cq_entry_size = rte_le_to_cpu_16(resp->cq_entry_size);
4493         ctx->vnic_max_vnic_entries =
4494                 rte_le_to_cpu_16(resp->vnic_max_vnic_entries);
4495         ctx->vnic_max_ring_table_entries =
4496                 rte_le_to_cpu_16(resp->vnic_max_ring_table_entries);
4497         ctx->vnic_entry_size = rte_le_to_cpu_16(resp->vnic_entry_size);
4498         ctx->stat_max_entries =
4499                 rte_le_to_cpu_32(resp->stat_max_entries);
4500         ctx->stat_entry_size = rte_le_to_cpu_16(resp->stat_entry_size);
4501         ctx->tqm_entry_size = rte_le_to_cpu_16(resp->tqm_entry_size);
4502         ctx->tqm_min_entries_per_ring =
4503                 rte_le_to_cpu_32(resp->tqm_min_entries_per_ring);
4504         ctx->tqm_max_entries_per_ring =
4505                 rte_le_to_cpu_32(resp->tqm_max_entries_per_ring);
4506         ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
4507         if (!ctx->tqm_entries_multiple)
4508                 ctx->tqm_entries_multiple = 1;
4509         ctx->mrav_max_entries =
4510                 rte_le_to_cpu_32(resp->mrav_max_entries);
4511         ctx->mrav_entry_size = rte_le_to_cpu_16(resp->mrav_entry_size);
4512         ctx->tim_entry_size = rte_le_to_cpu_16(resp->tim_entry_size);
4513         ctx->tim_max_entries = rte_le_to_cpu_32(resp->tim_max_entries);
4514 ctx_err:
4515         HWRM_UNLOCK();
4516         return rc;
4517 }
4518
4519 int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, uint32_t enables)
4520 {
4521         struct hwrm_func_backing_store_cfg_input req = {0};
4522         struct hwrm_func_backing_store_cfg_output *resp =
4523                 bp->hwrm_cmd_resp_addr;
4524         struct bnxt_ctx_mem_info *ctx = bp->ctx;
4525         struct bnxt_ctx_pg_info *ctx_pg;
4526         uint32_t *num_entries;
4527         uint64_t *pg_dir;
4528         uint8_t *pg_attr;
4529         uint32_t ena;
4530         int i, rc;
4531
4532         if (!ctx)
4533                 return 0;
4534
4535         HWRM_PREP(req, FUNC_BACKING_STORE_CFG, BNXT_USE_CHIMP_MB);
4536         req.enables = rte_cpu_to_le_32(enables);
4537
4538         if (enables & HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_QP) {
4539                 ctx_pg = &ctx->qp_mem;
4540                 req.qp_num_entries = rte_cpu_to_le_32(ctx_pg->entries);
4541                 req.qp_num_qp1_entries =
4542                         rte_cpu_to_le_16(ctx->qp_min_qp1_entries);
4543                 req.qp_num_l2_entries =
4544                         rte_cpu_to_le_16(ctx->qp_max_l2_entries);
4545                 req.qp_entry_size = rte_cpu_to_le_16(ctx->qp_entry_size);
4546                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
4547                                       &req.qpc_pg_size_qpc_lvl,
4548                                       &req.qpc_page_dir);
4549         }
4550
4551         if (enables & HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_SRQ) {
4552                 ctx_pg = &ctx->srq_mem;
4553                 req.srq_num_entries = rte_cpu_to_le_32(ctx_pg->entries);
4554                 req.srq_num_l2_entries =
4555                                  rte_cpu_to_le_16(ctx->srq_max_l2_entries);
4556                 req.srq_entry_size = rte_cpu_to_le_16(ctx->srq_entry_size);
4557                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
4558                                       &req.srq_pg_size_srq_lvl,
4559                                       &req.srq_page_dir);
4560         }
4561
4562         if (enables & HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_CQ) {
4563                 ctx_pg = &ctx->cq_mem;
4564                 req.cq_num_entries = rte_cpu_to_le_32(ctx_pg->entries);
4565                 req.cq_num_l2_entries =
4566                                 rte_cpu_to_le_16(ctx->cq_max_l2_entries);
4567                 req.cq_entry_size = rte_cpu_to_le_16(ctx->cq_entry_size);
4568                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
4569                                       &req.cq_pg_size_cq_lvl,
4570                                       &req.cq_page_dir);
4571         }
4572
4573         if (enables & HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_VNIC) {
4574                 ctx_pg = &ctx->vnic_mem;
4575                 req.vnic_num_vnic_entries =
4576                         rte_cpu_to_le_16(ctx->vnic_max_vnic_entries);
4577                 req.vnic_num_ring_table_entries =
4578                         rte_cpu_to_le_16(ctx->vnic_max_ring_table_entries);
4579                 req.vnic_entry_size = rte_cpu_to_le_16(ctx->vnic_entry_size);
4580                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
4581                                       &req.vnic_pg_size_vnic_lvl,
4582                                       &req.vnic_page_dir);
4583         }
4584
4585         if (enables & HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_STAT) {
4586                 ctx_pg = &ctx->stat_mem;
4587                 req.stat_num_entries = rte_cpu_to_le_16(ctx->stat_max_entries);
4588                 req.stat_entry_size = rte_cpu_to_le_16(ctx->stat_entry_size);
4589                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
4590                                       &req.stat_pg_size_stat_lvl,
4591                                       &req.stat_page_dir);
4592         }
4593
4594         req.tqm_entry_size = rte_cpu_to_le_16(ctx->tqm_entry_size);
4595         num_entries = &req.tqm_sp_num_entries;
4596         pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl;
4597         pg_dir = &req.tqm_sp_page_dir;
4598         ena = HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_TQM_SP;
4599         for (i = 0; i < 9; i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
4600                 if (!(enables & ena))
4601                         continue;
4602
4603                 req.tqm_entry_size = rte_cpu_to_le_16(ctx->tqm_entry_size);
4604
4605                 ctx_pg = ctx->tqm_mem[i];
4606                 *num_entries = rte_cpu_to_le_16(ctx_pg->entries);
4607                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
4608         }
4609
4610         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4611         HWRM_CHECK_RESULT();
4612         HWRM_UNLOCK();
4613
4614         return rc;
4615 }
4616
4617 int bnxt_hwrm_ext_port_qstats(struct bnxt *bp)
4618 {
4619         struct hwrm_port_qstats_ext_input req = {0};
4620         struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
4621         struct bnxt_pf_info *pf = &bp->pf;
4622         int rc;
4623
4624         if (!(bp->flags & BNXT_FLAG_EXT_RX_PORT_STATS ||
4625               bp->flags & BNXT_FLAG_EXT_TX_PORT_STATS))
4626                 return 0;
4627
4628         HWRM_PREP(req, PORT_QSTATS_EXT, BNXT_USE_CHIMP_MB);
4629
4630         req.port_id = rte_cpu_to_le_16(pf->port_id);
4631         if (bp->flags & BNXT_FLAG_EXT_TX_PORT_STATS) {
4632                 req.tx_stat_host_addr =
4633                         rte_cpu_to_le_64(bp->hw_tx_port_stats_ext_map);
4634                 req.tx_stat_size =
4635                         rte_cpu_to_le_16(sizeof(struct tx_port_stats_ext));
4636         }
4637         if (bp->flags & BNXT_FLAG_EXT_RX_PORT_STATS) {
4638                 req.rx_stat_host_addr =
4639                         rte_cpu_to_le_64(bp->hw_rx_port_stats_ext_map);
4640                 req.rx_stat_size =
4641                         rte_cpu_to_le_16(sizeof(struct rx_port_stats_ext));
4642         }
4643         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4644
4645         if (rc) {
4646                 bp->fw_rx_port_stats_ext_size = 0;
4647                 bp->fw_tx_port_stats_ext_size = 0;
4648         } else {
4649                 bp->fw_rx_port_stats_ext_size =
4650                         rte_le_to_cpu_16(resp->rx_stat_size);
4651                 bp->fw_tx_port_stats_ext_size =
4652                         rte_le_to_cpu_16(resp->tx_stat_size);
4653         }
4654
4655         HWRM_CHECK_RESULT();
4656         HWRM_UNLOCK();
4657
4658         return rc;
4659 }
4660
4661 int
4662 bnxt_hwrm_tunnel_redirect(struct bnxt *bp, uint8_t type)
4663 {
4664         struct hwrm_cfa_redirect_tunnel_type_alloc_input req = {0};
4665         struct hwrm_cfa_redirect_tunnel_type_alloc_output *resp =
4666                 bp->hwrm_cmd_resp_addr;
4667         int rc = 0;
4668
4669         HWRM_PREP(req, CFA_REDIRECT_TUNNEL_TYPE_ALLOC, BNXT_USE_CHIMP_MB);
4670         req.tunnel_type = type;
4671         req.dest_fid = bp->fw_fid;
4672         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4673         HWRM_CHECK_RESULT();
4674
4675         HWRM_UNLOCK();
4676
4677         return rc;
4678 }
4679
4680 int
4681 bnxt_hwrm_tunnel_redirect_free(struct bnxt *bp, uint8_t type)
4682 {
4683         struct hwrm_cfa_redirect_tunnel_type_free_input req = {0};
4684         struct hwrm_cfa_redirect_tunnel_type_free_output *resp =
4685                 bp->hwrm_cmd_resp_addr;
4686         int rc = 0;
4687
4688         HWRM_PREP(req, CFA_REDIRECT_TUNNEL_TYPE_FREE, BNXT_USE_CHIMP_MB);
4689         req.tunnel_type = type;
4690         req.dest_fid = bp->fw_fid;
4691         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4692         HWRM_CHECK_RESULT();
4693
4694         HWRM_UNLOCK();
4695
4696         return rc;
4697 }
4698
4699 int bnxt_hwrm_tunnel_redirect_query(struct bnxt *bp, uint32_t *type)
4700 {
4701         struct hwrm_cfa_redirect_query_tunnel_type_input req = {0};
4702         struct hwrm_cfa_redirect_query_tunnel_type_output *resp =
4703                 bp->hwrm_cmd_resp_addr;
4704         int rc = 0;
4705
4706         HWRM_PREP(req, CFA_REDIRECT_QUERY_TUNNEL_TYPE, BNXT_USE_CHIMP_MB);
4707         req.src_fid = bp->fw_fid;
4708         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4709         HWRM_CHECK_RESULT();
4710
4711         if (type)
4712                 *type = rte_le_to_cpu_32(resp->tunnel_mask);
4713
4714         HWRM_UNLOCK();
4715
4716         return rc;
4717 }
4718
4719 int bnxt_hwrm_tunnel_redirect_info(struct bnxt *bp, uint8_t tun_type,
4720                                    uint16_t *dst_fid)
4721 {
4722         struct hwrm_cfa_redirect_tunnel_type_info_input req = {0};
4723         struct hwrm_cfa_redirect_tunnel_type_info_output *resp =
4724                 bp->hwrm_cmd_resp_addr;
4725         int rc = 0;
4726
4727         HWRM_PREP(req, CFA_REDIRECT_TUNNEL_TYPE_INFO, BNXT_USE_CHIMP_MB);
4728         req.src_fid = bp->fw_fid;
4729         req.tunnel_type = tun_type;
4730         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4731         HWRM_CHECK_RESULT();
4732
4733         if (dst_fid)
4734                 *dst_fid = rte_le_to_cpu_16(resp->dest_fid);
4735
4736         PMD_DRV_LOG(DEBUG, "dst_fid: %x\n", resp->dest_fid);
4737
4738         HWRM_UNLOCK();
4739
4740         return rc;
4741 }
4742
4743 int bnxt_hwrm_set_mac(struct bnxt *bp)
4744 {
4745         struct hwrm_func_vf_cfg_output *resp = bp->hwrm_cmd_resp_addr;
4746         struct hwrm_func_vf_cfg_input req = {0};
4747         int rc = 0;
4748
4749         if (!BNXT_VF(bp))
4750                 return 0;
4751
4752         HWRM_PREP(req, FUNC_VF_CFG, BNXT_USE_CHIMP_MB);
4753
4754         req.enables =
4755                 rte_cpu_to_le_32(HWRM_FUNC_VF_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
4756         memcpy(req.dflt_mac_addr, bp->mac_addr, RTE_ETHER_ADDR_LEN);
4757
4758         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4759
4760         HWRM_CHECK_RESULT();
4761
4762         memcpy(bp->dflt_mac_addr, bp->mac_addr, RTE_ETHER_ADDR_LEN);
4763         HWRM_UNLOCK();
4764
4765         return rc;
4766 }
4767
4768 int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
4769 {
4770         struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
4771         struct hwrm_func_drv_if_change_input req = {0};
4772         uint32_t flags;
4773         int rc;
4774
4775         if (!(bp->flags & BNXT_FLAG_FW_CAP_IF_CHANGE))
4776                 return 0;
4777
4778         /* Do not issue FUNC_DRV_IF_CHANGE during reset recovery.
4779          * If we issue FUNC_DRV_IF_CHANGE with flags down before
4780          * FUNC_DRV_UNRGTR, FW resets before FUNC_DRV_UNRGTR
4781          */
4782         if (!up && (bp->flags & BNXT_FLAG_FW_RESET))
4783                 return 0;
4784
4785         HWRM_PREP(req, FUNC_DRV_IF_CHANGE, BNXT_USE_CHIMP_MB);
4786
4787         if (up)
4788                 req.flags =
4789                 rte_cpu_to_le_32(HWRM_FUNC_DRV_IF_CHANGE_INPUT_FLAGS_UP);
4790
4791         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4792
4793         HWRM_CHECK_RESULT();
4794         flags = rte_le_to_cpu_32(resp->flags);
4795         HWRM_UNLOCK();
4796
4797         if (flags & HWRM_FUNC_DRV_IF_CHANGE_OUTPUT_FLAGS_HOT_FW_RESET_DONE) {
4798                 PMD_DRV_LOG(INFO, "FW reset happened while port was down\n");
4799                 bp->flags |= BNXT_FLAG_IF_CHANGE_HOT_FW_RESET_DONE;
4800         }
4801
4802         return 0;
4803 }
4804
4805 int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
4806 {
4807         struct hwrm_error_recovery_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4808         struct bnxt_error_recovery_info *info = bp->recovery_info;
4809         struct hwrm_error_recovery_qcfg_input req = {0};
4810         uint32_t flags = 0;
4811         unsigned int i;
4812         int rc;
4813
4814         /* Older FW does not have error recovery support */
4815         if (!(bp->flags & BNXT_FLAG_FW_CAP_ERROR_RECOVERY))
4816                 return 0;
4817
4818         if (!info) {
4819                 info = rte_zmalloc("bnxt_hwrm_error_recovery_qcfg",
4820                                    sizeof(*info), 0);
4821                 bp->recovery_info = info;
4822                 if (info == NULL)
4823                         return -ENOMEM;
4824         } else {
4825                 memset(info, 0, sizeof(*info));
4826         }
4827
4828         HWRM_PREP(req, ERROR_RECOVERY_QCFG, BNXT_USE_CHIMP_MB);
4829
4830         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4831
4832         HWRM_CHECK_RESULT();
4833
4834         flags = rte_le_to_cpu_32(resp->flags);
4835         if (flags & HWRM_ERROR_RECOVERY_QCFG_OUTPUT_FLAGS_HOST)
4836                 info->flags |= BNXT_FLAG_ERROR_RECOVERY_HOST;
4837         else if (flags & HWRM_ERROR_RECOVERY_QCFG_OUTPUT_FLAGS_CO_CPU)
4838                 info->flags |= BNXT_FLAG_ERROR_RECOVERY_CO_CPU;
4839
4840         if ((info->flags & BNXT_FLAG_ERROR_RECOVERY_CO_CPU) &&
4841             !(bp->flags & BNXT_FLAG_KONG_MB_EN)) {
4842                 rc = -EINVAL;
4843                 goto err;
4844         }
4845
4846         /* FW returned values are in units of 100msec */
4847         info->driver_polling_freq =
4848                 rte_le_to_cpu_32(resp->driver_polling_freq) * 100;
4849         info->master_func_wait_period =
4850                 rte_le_to_cpu_32(resp->master_func_wait_period) * 100;
4851         info->normal_func_wait_period =
4852                 rte_le_to_cpu_32(resp->normal_func_wait_period) * 100;
4853         info->master_func_wait_period_after_reset =
4854                 rte_le_to_cpu_32(resp->master_func_wait_period_after_reset) * 100;
4855         info->max_bailout_time_after_reset =
4856                 rte_le_to_cpu_32(resp->max_bailout_time_after_reset) * 100;
4857         info->status_regs[BNXT_FW_STATUS_REG] =
4858                 rte_le_to_cpu_32(resp->fw_health_status_reg);
4859         info->status_regs[BNXT_FW_HEARTBEAT_CNT_REG] =
4860                 rte_le_to_cpu_32(resp->fw_heartbeat_reg);
4861         info->status_regs[BNXT_FW_RECOVERY_CNT_REG] =
4862                 rte_le_to_cpu_32(resp->fw_reset_cnt_reg);
4863         info->status_regs[BNXT_FW_RESET_INPROG_REG] =
4864                 rte_le_to_cpu_32(resp->reset_inprogress_reg);
4865         info->reg_array_cnt =
4866                 rte_le_to_cpu_32(resp->reg_array_cnt);
4867
4868         if (info->reg_array_cnt >= BNXT_NUM_RESET_REG) {
4869                 rc = -EINVAL;
4870                 goto err;
4871         }
4872
4873         for (i = 0; i < info->reg_array_cnt; i++) {
4874                 info->reset_reg[i] =
4875                         rte_le_to_cpu_32(resp->reset_reg[i]);
4876                 info->reset_reg_val[i] =
4877                         rte_le_to_cpu_32(resp->reset_reg_val[i]);
4878                 info->delay_after_reset[i] =
4879                         resp->delay_after_reset[i];
4880         }
4881 err:
4882         HWRM_UNLOCK();
4883
4884         /* Map the FW status registers */
4885         if (!rc)
4886                 rc = bnxt_map_fw_health_status_regs(bp);
4887
4888         if (rc) {
4889                 rte_free(bp->recovery_info);
4890                 bp->recovery_info = NULL;
4891         }
4892         return rc;
4893 }
4894
4895 int bnxt_hwrm_fw_reset(struct bnxt *bp)
4896 {
4897         struct hwrm_fw_reset_output *resp = bp->hwrm_cmd_resp_addr;
4898         struct hwrm_fw_reset_input req = {0};
4899         int rc;
4900
4901         if (!BNXT_PF(bp))
4902                 return -EOPNOTSUPP;
4903
4904         HWRM_PREP(req, FW_RESET, BNXT_USE_KONG(bp));
4905
4906         req.embedded_proc_type =
4907                 HWRM_FW_RESET_INPUT_EMBEDDED_PROC_TYPE_CHIP;
4908         req.selfrst_status =
4909                 HWRM_FW_RESET_INPUT_SELFRST_STATUS_SELFRSTASAP;
4910         req.flags = HWRM_FW_RESET_INPUT_FLAGS_RESET_GRACEFUL;
4911
4912         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req),
4913                                     BNXT_USE_KONG(bp));
4914
4915         HWRM_CHECK_RESULT();
4916         HWRM_UNLOCK();
4917
4918         return rc;
4919 }
4920
4921 int bnxt_hwrm_port_ts_query(struct bnxt *bp, uint8_t path, uint64_t *timestamp)
4922 {
4923         struct hwrm_port_ts_query_output *resp = bp->hwrm_cmd_resp_addr;
4924         struct hwrm_port_ts_query_input req = {0};
4925         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
4926         uint32_t flags = 0;
4927         int rc;
4928
4929         if (!ptp)
4930                 return 0;
4931
4932         HWRM_PREP(req, PORT_TS_QUERY, BNXT_USE_CHIMP_MB);
4933
4934         switch (path) {
4935         case BNXT_PTP_FLAGS_PATH_TX:
4936                 flags |= HWRM_PORT_TS_QUERY_INPUT_FLAGS_PATH_TX;
4937                 break;
4938         case BNXT_PTP_FLAGS_PATH_RX:
4939                 flags |= HWRM_PORT_TS_QUERY_INPUT_FLAGS_PATH_RX;
4940                 break;
4941         case BNXT_PTP_FLAGS_CURRENT_TIME:
4942                 flags |= HWRM_PORT_TS_QUERY_INPUT_FLAGS_CURRENT_TIME;
4943                 break;
4944         }
4945
4946         req.flags = rte_cpu_to_le_32(flags);
4947         req.port_id = rte_cpu_to_le_16(bp->pf.port_id);
4948
4949         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4950
4951         HWRM_CHECK_RESULT();
4952
4953         if (timestamp) {
4954                 *timestamp = rte_le_to_cpu_32(resp->ptp_msg_ts[0]);
4955                 *timestamp |=
4956                         (uint64_t)(rte_le_to_cpu_32(resp->ptp_msg_ts[1])) << 32;
4957         }
4958         HWRM_UNLOCK();
4959
4960         return rc;
4961 }
4962
4963 int bnxt_hwrm_cfa_adv_flow_mgmt_qcaps(struct bnxt *bp)
4964 {
4965         struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp =
4966                                         bp->hwrm_cmd_resp_addr;
4967         struct hwrm_cfa_adv_flow_mgnt_qcaps_input req = {0};
4968         uint32_t flags = 0;
4969         int rc = 0;
4970
4971         if (!(bp->flags & BNXT_FLAG_ADV_FLOW_MGMT))
4972                 return rc;
4973
4974         if (!(BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp))) {
4975                 PMD_DRV_LOG(DEBUG,
4976                             "Not a PF or trusted VF. Command not supported\n");
4977                 return 0;
4978         }
4979
4980         HWRM_PREP(req, CFA_ADV_FLOW_MGNT_QCAPS, BNXT_USE_KONG(bp));
4981         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_KONG(bp));
4982
4983         HWRM_CHECK_RESULT();
4984         flags = rte_le_to_cpu_32(resp->flags);
4985         HWRM_UNLOCK();
4986
4987         if (flags & HWRM_CFA_ADV_FLOW_MGNT_QCAPS_L2_HDR_SRC_FILTER_EN) {
4988                 bp->flow_flags |= BNXT_FLOW_FLAG_L2_HDR_SRC_FILTER_EN;
4989                 PMD_DRV_LOG(INFO, "Source L2 header filtering enabled\n");
4990         }
4991
4992         return rc;
4993 }