net: add rte prefix to ether defines
[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_SPEC_CODE_1_8_3            0x10803
31 #define HWRM_VERSION_1_9_1              0x10901
32
33 struct bnxt_plcmodes_cfg {
34         uint32_t        flags;
35         uint16_t        jumbo_thresh;
36         uint16_t        hds_offset;
37         uint16_t        hds_threshold;
38 };
39
40 static int page_getenum(size_t size)
41 {
42         if (size <= 1 << 4)
43                 return 4;
44         if (size <= 1 << 12)
45                 return 12;
46         if (size <= 1 << 13)
47                 return 13;
48         if (size <= 1 << 16)
49                 return 16;
50         if (size <= 1 << 21)
51                 return 21;
52         if (size <= 1 << 22)
53                 return 22;
54         if (size <= 1 << 30)
55                 return 30;
56         PMD_DRV_LOG(ERR, "Page size %zu out of range\n", size);
57         return sizeof(void *) * 8 - 1;
58 }
59
60 static int page_roundup(size_t size)
61 {
62         return 1 << page_getenum(size);
63 }
64
65 /*
66  * HWRM Functions (sent to HWRM)
67  * These are named bnxt_hwrm_*() and return -1 if bnxt_hwrm_send_message()
68  * fails (ie: a timeout), and a positive non-zero HWRM error code if the HWRM
69  * command was failed by the ChiMP.
70  */
71
72 static int bnxt_hwrm_send_message(struct bnxt *bp, void *msg,
73                                   uint32_t msg_len, bool use_kong_mb)
74 {
75         unsigned int i;
76         struct input *req = msg;
77         struct output *resp = bp->hwrm_cmd_resp_addr;
78         uint32_t *data = msg;
79         uint8_t *bar;
80         uint8_t *valid;
81         uint16_t max_req_len = bp->max_req_len;
82         struct hwrm_short_input short_input = { 0 };
83         uint16_t bar_offset = use_kong_mb ?
84                 GRCPF_REG_KONG_CHANNEL_OFFSET : GRCPF_REG_CHIMP_CHANNEL_OFFSET;
85         uint16_t mb_trigger_offset = use_kong_mb ?
86                 GRCPF_REG_KONG_COMM_TRIGGER : GRCPF_REG_CHIMP_COMM_TRIGGER;
87
88         if (bp->flags & BNXT_FLAG_SHORT_CMD) {
89                 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
90
91                 memset(short_cmd_req, 0, bp->max_req_len);
92                 memcpy(short_cmd_req, req, msg_len);
93
94                 short_input.req_type = rte_cpu_to_le_16(req->req_type);
95                 short_input.signature = rte_cpu_to_le_16(
96                                         HWRM_SHORT_INPUT_SIGNATURE_SHORT_CMD);
97                 short_input.size = rte_cpu_to_le_16(msg_len);
98                 short_input.req_addr =
99                         rte_cpu_to_le_64(bp->hwrm_short_cmd_req_dma_addr);
100
101                 data = (uint32_t *)&short_input;
102                 msg_len = sizeof(short_input);
103
104                 /* Sync memory write before updating doorbell */
105                 rte_wmb();
106
107                 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
108         }
109
110         /* Write request msg to hwrm channel */
111         for (i = 0; i < msg_len; i += 4) {
112                 bar = (uint8_t *)bp->bar0 + bar_offset + i;
113                 rte_write32(*data, bar);
114                 data++;
115         }
116
117         /* Zero the rest of the request space */
118         for (; i < max_req_len; i += 4) {
119                 bar = (uint8_t *)bp->bar0 + bar_offset + i;
120                 rte_write32(0, bar);
121         }
122
123         /* Ring channel doorbell */
124         bar = (uint8_t *)bp->bar0 + mb_trigger_offset;
125         rte_write32(1, bar);
126
127         /* Poll for the valid bit */
128         for (i = 0; i < HWRM_CMD_TIMEOUT; i++) {
129                 /* Sanity check on the resp->resp_len */
130                 rte_rmb();
131                 if (resp->resp_len && resp->resp_len <=
132                                 bp->max_resp_len) {
133                         /* Last byte of resp contains the valid key */
134                         valid = (uint8_t *)resp + resp->resp_len - 1;
135                         if (*valid == HWRM_RESP_VALID_KEY)
136                                 break;
137                 }
138                 rte_delay_us(1);
139         }
140
141         if (i >= HWRM_CMD_TIMEOUT) {
142                 PMD_DRV_LOG(ERR, "Error sending msg 0x%04x\n",
143                         req->req_type);
144                 goto err_ret;
145         }
146         return 0;
147
148 err_ret:
149         return -1;
150 }
151
152 /*
153  * HWRM_PREP() should be used to prepare *ALL* HWRM commands.  It grabs the
154  * spinlock, and does initial processing.
155  *
156  * HWRM_CHECK_RESULT() returns errors on failure and may not be used.  It
157  * releases the spinlock only if it returns.  If the regular int return codes
158  * are not used by the function, HWRM_CHECK_RESULT() should not be used
159  * directly, rather it should be copied and modified to suit the function.
160  *
161  * HWRM_UNLOCK() must be called after all response processing is completed.
162  */
163 #define HWRM_PREP(req, type, kong) do { \
164         rte_spinlock_lock(&bp->hwrm_lock); \
165         memset(bp->hwrm_cmd_resp_addr, 0, bp->max_resp_len); \
166         req.req_type = rte_cpu_to_le_16(HWRM_##type); \
167         req.cmpl_ring = rte_cpu_to_le_16(-1); \
168         req.seq_id = kong ? rte_cpu_to_le_16(bp->kong_cmd_seq++) :\
169                 rte_cpu_to_le_16(bp->hwrm_cmd_seq++); \
170         req.target_id = rte_cpu_to_le_16(0xffff); \
171         req.resp_addr = rte_cpu_to_le_64(bp->hwrm_cmd_resp_dma_addr); \
172 } while (0)
173
174 #define HWRM_CHECK_RESULT_SILENT() do {\
175         if (rc) { \
176                 rte_spinlock_unlock(&bp->hwrm_lock); \
177                 return rc; \
178         } \
179         if (resp->error_code) { \
180                 rc = rte_le_to_cpu_16(resp->error_code); \
181                 rte_spinlock_unlock(&bp->hwrm_lock); \
182                 return rc; \
183         } \
184 } while (0)
185
186 #define HWRM_CHECK_RESULT() do {\
187         if (rc) { \
188                 PMD_DRV_LOG(ERR, "failed rc:%d\n", rc); \
189                 rte_spinlock_unlock(&bp->hwrm_lock); \
190                 if (rc == HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED) \
191                         rc = -EACCES; \
192                 else if (rc > 0) \
193                         rc = -EINVAL; \
194                 return rc; \
195         } \
196         if (resp->error_code) { \
197                 rc = rte_le_to_cpu_16(resp->error_code); \
198                 if (resp->resp_len >= 16) { \
199                         struct hwrm_err_output *tmp_hwrm_err_op = \
200                                                 (void *)resp; \
201                         PMD_DRV_LOG(ERR, \
202                                 "error %d:%d:%08x:%04x\n", \
203                                 rc, tmp_hwrm_err_op->cmd_err, \
204                                 rte_le_to_cpu_32(\
205                                         tmp_hwrm_err_op->opaque_0), \
206                                 rte_le_to_cpu_16(\
207                                         tmp_hwrm_err_op->opaque_1)); \
208                 } else { \
209                         PMD_DRV_LOG(ERR, "error %d\n", rc); \
210                 } \
211                 rte_spinlock_unlock(&bp->hwrm_lock); \
212                 if (rc == HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED) \
213                         rc = -EACCES; \
214                 else if (rc > 0) \
215                         rc = -EINVAL; \
216                 return rc; \
217         } \
218 } while (0)
219
220 #define HWRM_UNLOCK()           rte_spinlock_unlock(&bp->hwrm_lock)
221
222 int bnxt_hwrm_cfa_l2_clear_rx_mask(struct bnxt *bp, struct bnxt_vnic_info *vnic)
223 {
224         int rc = 0;
225         struct hwrm_cfa_l2_set_rx_mask_input req = {.req_type = 0 };
226         struct hwrm_cfa_l2_set_rx_mask_output *resp = bp->hwrm_cmd_resp_addr;
227
228         HWRM_PREP(req, CFA_L2_SET_RX_MASK, BNXT_USE_CHIMP_MB);
229         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
230         req.mask = 0;
231
232         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
233
234         HWRM_CHECK_RESULT();
235         HWRM_UNLOCK();
236
237         return rc;
238 }
239
240 int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp,
241                                  struct bnxt_vnic_info *vnic,
242                                  uint16_t vlan_count,
243                                  struct bnxt_vlan_table_entry *vlan_table)
244 {
245         int rc = 0;
246         struct hwrm_cfa_l2_set_rx_mask_input req = {.req_type = 0 };
247         struct hwrm_cfa_l2_set_rx_mask_output *resp = bp->hwrm_cmd_resp_addr;
248         uint32_t mask = 0;
249
250         if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
251                 return rc;
252
253         HWRM_PREP(req, CFA_L2_SET_RX_MASK, BNXT_USE_CHIMP_MB);
254         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
255
256         /* FIXME add multicast flag, when multicast adding options is supported
257          * by ethtool.
258          */
259         if (vnic->flags & BNXT_VNIC_INFO_BCAST)
260                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_BCAST;
261         if (vnic->flags & BNXT_VNIC_INFO_UNTAGGED)
262                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLAN_NONVLAN;
263         if (vnic->flags & BNXT_VNIC_INFO_PROMISC)
264                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_PROMISCUOUS;
265         if (vnic->flags & BNXT_VNIC_INFO_ALLMULTI)
266                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ALL_MCAST;
267         if (vnic->flags & BNXT_VNIC_INFO_MCAST)
268                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_MCAST;
269         if (vnic->mc_addr_cnt) {
270                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_MCAST;
271                 req.num_mc_entries = rte_cpu_to_le_32(vnic->mc_addr_cnt);
272                 req.mc_tbl_addr = rte_cpu_to_le_64(vnic->mc_list_dma_addr);
273         }
274         if (vlan_table) {
275                 if (!(mask & HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLAN_NONVLAN))
276                         mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLANONLY;
277                 req.vlan_tag_tbl_addr = rte_cpu_to_le_64(
278                          rte_mem_virt2iova(vlan_table));
279                 req.num_vlan_tags = rte_cpu_to_le_32((uint32_t)vlan_count);
280         }
281         req.mask = rte_cpu_to_le_32(mask);
282
283         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
284
285         HWRM_CHECK_RESULT();
286         HWRM_UNLOCK();
287
288         return rc;
289 }
290
291 int bnxt_hwrm_cfa_vlan_antispoof_cfg(struct bnxt *bp, uint16_t fid,
292                         uint16_t vlan_count,
293                         struct bnxt_vlan_antispoof_table_entry *vlan_table)
294 {
295         int rc = 0;
296         struct hwrm_cfa_vlan_antispoof_cfg_input req = {.req_type = 0 };
297         struct hwrm_cfa_vlan_antispoof_cfg_output *resp =
298                                                 bp->hwrm_cmd_resp_addr;
299
300         /*
301          * Older HWRM versions did not support this command, and the set_rx_mask
302          * list was used for anti-spoof. In 1.8.0, the TX path configuration was
303          * removed from set_rx_mask call, and this command was added.
304          *
305          * This command is also present from 1.7.8.11 and higher,
306          * as well as 1.7.8.0
307          */
308         if (bp->fw_ver < ((1 << 24) | (8 << 16))) {
309                 if (bp->fw_ver != ((1 << 24) | (7 << 16) | (8 << 8))) {
310                         if (bp->fw_ver < ((1 << 24) | (7 << 16) | (8 << 8) |
311                                         (11)))
312                                 return 0;
313                 }
314         }
315         HWRM_PREP(req, CFA_VLAN_ANTISPOOF_CFG, BNXT_USE_CHIMP_MB);
316         req.fid = rte_cpu_to_le_16(fid);
317
318         req.vlan_tag_mask_tbl_addr =
319                 rte_cpu_to_le_64(rte_mem_virt2iova(vlan_table));
320         req.num_vlan_entries = rte_cpu_to_le_32((uint32_t)vlan_count);
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_clear_l2_filter(struct bnxt *bp,
331                            struct bnxt_filter_info *filter)
332 {
333         int rc = 0;
334         struct hwrm_cfa_l2_filter_free_input req = {.req_type = 0 };
335         struct hwrm_cfa_l2_filter_free_output *resp = bp->hwrm_cmd_resp_addr;
336
337         if (filter->fw_l2_filter_id == UINT64_MAX)
338                 return 0;
339
340         HWRM_PREP(req, CFA_L2_FILTER_FREE, BNXT_USE_CHIMP_MB);
341
342         req.l2_filter_id = rte_cpu_to_le_64(filter->fw_l2_filter_id);
343
344         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
345
346         HWRM_CHECK_RESULT();
347         HWRM_UNLOCK();
348
349         filter->fw_l2_filter_id = UINT64_MAX;
350
351         return 0;
352 }
353
354 int bnxt_hwrm_set_l2_filter(struct bnxt *bp,
355                          uint16_t dst_id,
356                          struct bnxt_filter_info *filter)
357 {
358         int rc = 0;
359         struct hwrm_cfa_l2_filter_alloc_input req = {.req_type = 0 };
360         struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
361         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
362         const struct rte_eth_vmdq_rx_conf *conf =
363                     &dev_conf->rx_adv_conf.vmdq_rx_conf;
364         uint32_t enables = 0;
365         uint16_t j = dst_id - 1;
366
367         //TODO: Is there a better way to add VLANs to each VNIC in case of VMDQ
368         if ((dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_FLAG) &&
369             conf->pool_map[j].pools & (1UL << j)) {
370                 PMD_DRV_LOG(DEBUG,
371                         "Add vlan %u to vmdq pool %u\n",
372                         conf->pool_map[j].vlan_id, j);
373
374                 filter->l2_ivlan = conf->pool_map[j].vlan_id;
375                 filter->enables |=
376                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN |
377                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN_MASK;
378         }
379
380         if (filter->fw_l2_filter_id != UINT64_MAX)
381                 bnxt_hwrm_clear_l2_filter(bp, filter);
382
383         HWRM_PREP(req, CFA_L2_FILTER_ALLOC, BNXT_USE_CHIMP_MB);
384
385         req.flags = rte_cpu_to_le_32(filter->flags);
386         req.flags |=
387         rte_cpu_to_le_32(HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST);
388
389         enables = filter->enables |
390               HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_DST_ID;
391         req.dst_id = rte_cpu_to_le_16(dst_id);
392
393         if (enables &
394             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR)
395                 memcpy(req.l2_addr, filter->l2_addr,
396                        RTE_ETHER_ADDR_LEN);
397         if (enables &
398             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR_MASK)
399                 memcpy(req.l2_addr_mask, filter->l2_addr_mask,
400                        RTE_ETHER_ADDR_LEN);
401         if (enables &
402             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_OVLAN)
403                 req.l2_ovlan = filter->l2_ovlan;
404         if (enables &
405             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN)
406                 req.l2_ivlan = filter->l2_ivlan;
407         if (enables &
408             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_OVLAN_MASK)
409                 req.l2_ovlan_mask = filter->l2_ovlan_mask;
410         if (enables &
411             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN_MASK)
412                 req.l2_ivlan_mask = filter->l2_ivlan_mask;
413         if (enables & HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_SRC_ID)
414                 req.src_id = rte_cpu_to_le_32(filter->src_id);
415         if (enables & HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_SRC_TYPE)
416                 req.src_type = filter->src_type;
417
418         req.enables = rte_cpu_to_le_32(enables);
419
420         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
421
422         HWRM_CHECK_RESULT();
423
424         filter->fw_l2_filter_id = rte_le_to_cpu_64(resp->l2_filter_id);
425         HWRM_UNLOCK();
426
427         return rc;
428 }
429
430 int bnxt_hwrm_ptp_cfg(struct bnxt *bp)
431 {
432         struct hwrm_port_mac_cfg_input req = {.req_type = 0};
433         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
434         uint32_t flags = 0;
435         int rc;
436
437         if (!ptp)
438                 return 0;
439
440         HWRM_PREP(req, PORT_MAC_CFG, BNXT_USE_CHIMP_MB);
441
442         if (ptp->rx_filter)
443                 flags |= HWRM_PORT_MAC_CFG_INPUT_FLAGS_PTP_RX_TS_CAPTURE_ENABLE;
444         else
445                 flags |=
446                         HWRM_PORT_MAC_CFG_INPUT_FLAGS_PTP_RX_TS_CAPTURE_DISABLE;
447         if (ptp->tx_tstamp_en)
448                 flags |= HWRM_PORT_MAC_CFG_INPUT_FLAGS_PTP_TX_TS_CAPTURE_ENABLE;
449         else
450                 flags |=
451                         HWRM_PORT_MAC_CFG_INPUT_FLAGS_PTP_TX_TS_CAPTURE_DISABLE;
452         req.flags = rte_cpu_to_le_32(flags);
453         req.enables = rte_cpu_to_le_32
454                 (HWRM_PORT_MAC_CFG_INPUT_ENABLES_RX_TS_CAPTURE_PTP_MSG_TYPE);
455         req.rx_ts_capture_ptp_msg_type = rte_cpu_to_le_16(ptp->rxctl);
456
457         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
458         HWRM_UNLOCK();
459
460         return rc;
461 }
462
463 static int bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
464 {
465         int rc = 0;
466         struct hwrm_port_mac_ptp_qcfg_input req = {.req_type = 0};
467         struct hwrm_port_mac_ptp_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
468         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
469
470 /*      if (bp->hwrm_spec_code < 0x10801 || ptp)  TBD  */
471         if (ptp)
472                 return 0;
473
474         HWRM_PREP(req, PORT_MAC_PTP_QCFG, BNXT_USE_CHIMP_MB);
475
476         req.port_id = rte_cpu_to_le_16(bp->pf.port_id);
477
478         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
479
480         HWRM_CHECK_RESULT();
481
482         if (!(resp->flags & HWRM_PORT_MAC_PTP_QCFG_OUTPUT_FLAGS_DIRECT_ACCESS))
483                 return 0;
484
485         ptp = rte_zmalloc("ptp_cfg", sizeof(*ptp), 0);
486         if (!ptp)
487                 return -ENOMEM;
488
489         ptp->rx_regs[BNXT_PTP_RX_TS_L] =
490                 rte_le_to_cpu_32(resp->rx_ts_reg_off_lower);
491         ptp->rx_regs[BNXT_PTP_RX_TS_H] =
492                 rte_le_to_cpu_32(resp->rx_ts_reg_off_upper);
493         ptp->rx_regs[BNXT_PTP_RX_SEQ] =
494                 rte_le_to_cpu_32(resp->rx_ts_reg_off_seq_id);
495         ptp->rx_regs[BNXT_PTP_RX_FIFO] =
496                 rte_le_to_cpu_32(resp->rx_ts_reg_off_fifo);
497         ptp->rx_regs[BNXT_PTP_RX_FIFO_ADV] =
498                 rte_le_to_cpu_32(resp->rx_ts_reg_off_fifo_adv);
499         ptp->tx_regs[BNXT_PTP_TX_TS_L] =
500                 rte_le_to_cpu_32(resp->tx_ts_reg_off_lower);
501         ptp->tx_regs[BNXT_PTP_TX_TS_H] =
502                 rte_le_to_cpu_32(resp->tx_ts_reg_off_upper);
503         ptp->tx_regs[BNXT_PTP_TX_SEQ] =
504                 rte_le_to_cpu_32(resp->tx_ts_reg_off_seq_id);
505         ptp->tx_regs[BNXT_PTP_TX_FIFO] =
506                 rte_le_to_cpu_32(resp->tx_ts_reg_off_fifo);
507
508         ptp->bp = bp;
509         bp->ptp_cfg = ptp;
510
511         return 0;
512 }
513
514 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
515 {
516         int rc = 0;
517         struct hwrm_func_qcaps_input req = {.req_type = 0 };
518         struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
519         uint16_t new_max_vfs;
520         uint32_t flags;
521         int i;
522
523         HWRM_PREP(req, FUNC_QCAPS, BNXT_USE_CHIMP_MB);
524
525         req.fid = rte_cpu_to_le_16(0xffff);
526
527         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
528
529         HWRM_CHECK_RESULT();
530
531         bp->max_ring_grps = rte_le_to_cpu_32(resp->max_hw_ring_grps);
532         flags = rte_le_to_cpu_32(resp->flags);
533         if (BNXT_PF(bp)) {
534                 bp->pf.port_id = resp->port_id;
535                 bp->pf.first_vf_id = rte_le_to_cpu_16(resp->first_vf_id);
536                 bp->pf.total_vfs = rte_le_to_cpu_16(resp->max_vfs);
537                 new_max_vfs = bp->pdev->max_vfs;
538                 if (new_max_vfs != bp->pf.max_vfs) {
539                         if (bp->pf.vf_info)
540                                 rte_free(bp->pf.vf_info);
541                         bp->pf.vf_info = rte_malloc("bnxt_vf_info",
542                             sizeof(bp->pf.vf_info[0]) * new_max_vfs, 0);
543                         bp->pf.max_vfs = new_max_vfs;
544                         for (i = 0; i < new_max_vfs; i++) {
545                                 bp->pf.vf_info[i].fid = bp->pf.first_vf_id + i;
546                                 bp->pf.vf_info[i].vlan_table =
547                                         rte_zmalloc("VF VLAN table",
548                                                     getpagesize(),
549                                                     getpagesize());
550                                 if (bp->pf.vf_info[i].vlan_table == NULL)
551                                         PMD_DRV_LOG(ERR,
552                                         "Fail to alloc VLAN table for VF %d\n",
553                                         i);
554                                 else
555                                         rte_mem_lock_page(
556                                                 bp->pf.vf_info[i].vlan_table);
557                                 bp->pf.vf_info[i].vlan_as_table =
558                                         rte_zmalloc("VF VLAN AS table",
559                                                     getpagesize(),
560                                                     getpagesize());
561                                 if (bp->pf.vf_info[i].vlan_as_table == NULL)
562                                         PMD_DRV_LOG(ERR,
563                                         "Alloc VLAN AS table for VF %d fail\n",
564                                         i);
565                                 else
566                                         rte_mem_lock_page(
567                                                bp->pf.vf_info[i].vlan_as_table);
568                                 STAILQ_INIT(&bp->pf.vf_info[i].filter);
569                         }
570                 }
571         }
572
573         bp->fw_fid = rte_le_to_cpu_32(resp->fid);
574         memcpy(bp->dflt_mac_addr, &resp->mac_address, RTE_ETHER_ADDR_LEN);
575         bp->max_rsscos_ctx = rte_le_to_cpu_16(resp->max_rsscos_ctx);
576         bp->max_cp_rings = rte_le_to_cpu_16(resp->max_cmpl_rings);
577         bp->max_tx_rings = rte_le_to_cpu_16(resp->max_tx_rings);
578         bp->max_rx_rings = rte_le_to_cpu_16(resp->max_rx_rings);
579         bp->max_l2_ctx = rte_le_to_cpu_16(resp->max_l2_ctxs);
580         /* TODO: For now, do not support VMDq/RFS on VFs. */
581         if (BNXT_PF(bp)) {
582                 if (bp->pf.max_vfs)
583                         bp->max_vnics = 1;
584                 else
585                         bp->max_vnics = rte_le_to_cpu_16(resp->max_vnics);
586         } else {
587                 bp->max_vnics = 1;
588         }
589         bp->max_stat_ctx = rte_le_to_cpu_16(resp->max_stat_ctx);
590         if (BNXT_PF(bp)) {
591                 bp->pf.total_vnics = rte_le_to_cpu_16(resp->max_vnics);
592                 if (flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_PTP_SUPPORTED) {
593                         bp->flags |= BNXT_FLAG_PTP_SUPPORTED;
594                         PMD_DRV_LOG(DEBUG, "PTP SUPPORTED\n");
595                         HWRM_UNLOCK();
596                         bnxt_hwrm_ptp_qcfg(bp);
597                 }
598         }
599
600         HWRM_UNLOCK();
601
602         return rc;
603 }
604
605 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
606 {
607         int rc;
608
609         rc = __bnxt_hwrm_func_qcaps(bp);
610         if (!rc && bp->hwrm_spec_code >= HWRM_SPEC_CODE_1_8_3) {
611                 rc = bnxt_hwrm_func_resc_qcaps(bp);
612                 if (!rc)
613                         bp->flags |= BNXT_FLAG_NEW_RM;
614         }
615
616         return rc;
617 }
618
619 int bnxt_hwrm_func_reset(struct bnxt *bp)
620 {
621         int rc = 0;
622         struct hwrm_func_reset_input req = {.req_type = 0 };
623         struct hwrm_func_reset_output *resp = bp->hwrm_cmd_resp_addr;
624
625         HWRM_PREP(req, FUNC_RESET, BNXT_USE_CHIMP_MB);
626
627         req.enables = rte_cpu_to_le_32(0);
628
629         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
630
631         HWRM_CHECK_RESULT();
632         HWRM_UNLOCK();
633
634         return rc;
635 }
636
637 int bnxt_hwrm_func_driver_register(struct bnxt *bp)
638 {
639         int rc;
640         struct hwrm_func_drv_rgtr_input req = {.req_type = 0 };
641         struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
642
643         if (bp->flags & BNXT_FLAG_REGISTERED)
644                 return 0;
645
646         HWRM_PREP(req, FUNC_DRV_RGTR, BNXT_USE_CHIMP_MB);
647         req.enables = rte_cpu_to_le_32(HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_VER |
648                         HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_ASYNC_EVENT_FWD);
649         req.ver_maj = RTE_VER_YEAR;
650         req.ver_min = RTE_VER_MONTH;
651         req.ver_upd = RTE_VER_MINOR;
652
653         if (BNXT_PF(bp)) {
654                 req.enables |= rte_cpu_to_le_32(
655                         HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_VF_REQ_FWD);
656                 memcpy(req.vf_req_fwd, bp->pf.vf_req_fwd,
657                        RTE_MIN(sizeof(req.vf_req_fwd),
658                                sizeof(bp->pf.vf_req_fwd)));
659
660                 /*
661                  * PF can sniff HWRM API issued by VF. This can be set up by
662                  * linux driver and inherited by the DPDK PF driver. Clear
663                  * this HWRM sniffer list in FW because DPDK PF driver does
664                  * not support this.
665                  */
666                 req.flags =
667                 rte_cpu_to_le_32(HWRM_FUNC_DRV_RGTR_INPUT_FLAGS_FWD_NONE_MODE);
668         }
669
670         req.async_event_fwd[0] |=
671                 rte_cpu_to_le_32(ASYNC_CMPL_EVENT_ID_LINK_STATUS_CHANGE |
672                                  ASYNC_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED |
673                                  ASYNC_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE);
674         req.async_event_fwd[1] |=
675                 rte_cpu_to_le_32(ASYNC_CMPL_EVENT_ID_PF_DRVR_UNLOAD |
676                                  ASYNC_CMPL_EVENT_ID_VF_CFG_CHANGE);
677
678         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
679
680         HWRM_CHECK_RESULT();
681         HWRM_UNLOCK();
682
683         bp->flags |= BNXT_FLAG_REGISTERED;
684
685         return rc;
686 }
687
688 int bnxt_hwrm_check_vf_rings(struct bnxt *bp)
689 {
690         if (!(BNXT_VF(bp) && (bp->flags & BNXT_FLAG_NEW_RM)))
691                 return 0;
692
693         return bnxt_hwrm_func_reserve_vf_resc(bp, true);
694 }
695
696 int bnxt_hwrm_func_reserve_vf_resc(struct bnxt *bp, bool test)
697 {
698         int rc;
699         uint32_t flags = 0;
700         uint32_t enables;
701         struct hwrm_func_vf_cfg_output *resp = bp->hwrm_cmd_resp_addr;
702         struct hwrm_func_vf_cfg_input req = {0};
703
704         HWRM_PREP(req, FUNC_VF_CFG, BNXT_USE_CHIMP_MB);
705
706         req.enables = rte_cpu_to_le_32
707                         (HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_RX_RINGS  |
708                         HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_TX_RINGS   |
709                         HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_STAT_CTXS  |
710                         HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_CMPL_RINGS |
711                         HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_HW_RING_GRPS |
712                         HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_VNICS);
713
714         req.num_tx_rings = rte_cpu_to_le_16(bp->tx_nr_rings);
715         req.num_rx_rings = rte_cpu_to_le_16(bp->rx_nr_rings *
716                                             AGG_RING_MULTIPLIER);
717         req.num_stat_ctxs = rte_cpu_to_le_16(bp->rx_nr_rings + bp->tx_nr_rings);
718         req.num_cmpl_rings = rte_cpu_to_le_16(bp->rx_nr_rings +
719                                               bp->tx_nr_rings);
720         req.num_hw_ring_grps = rte_cpu_to_le_16(bp->rx_nr_rings);
721         req.num_vnics = rte_cpu_to_le_16(bp->rx_nr_rings);
722         if (bp->vf_resv_strategy ==
723             HWRM_FUNC_RESOURCE_QCAPS_OUTPUT_VF_RESV_STRATEGY_MINIMAL_STATIC) {
724                 enables = HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_VNICS |
725                                 HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_L2_CTXS |
726                                 HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_RSSCOS_CTXS;
727                 req.enables |= rte_cpu_to_le_32(enables);
728                 req.num_rsscos_ctxs = rte_cpu_to_le_16(BNXT_VF_RSV_NUM_RSS_CTX);
729                 req.num_l2_ctxs = rte_cpu_to_le_16(BNXT_VF_RSV_NUM_L2_CTX);
730                 req.num_vnics = rte_cpu_to_le_16(BNXT_VF_RSV_NUM_VNIC);
731         }
732
733         if (test)
734                 flags = HWRM_FUNC_VF_CFG_INPUT_FLAGS_TX_ASSETS_TEST |
735                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_RX_ASSETS_TEST |
736                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_CMPL_ASSETS_TEST |
737                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_RING_GRP_ASSETS_TEST |
738                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_STAT_CTX_ASSETS_TEST |
739                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_VNIC_ASSETS_TEST;
740
741         req.flags = rte_cpu_to_le_32(flags);
742
743         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
744
745         if (test)
746                 HWRM_CHECK_RESULT_SILENT();
747         else
748                 HWRM_CHECK_RESULT();
749
750         HWRM_UNLOCK();
751         return rc;
752 }
753
754 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp)
755 {
756         int rc;
757         struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
758         struct hwrm_func_resource_qcaps_input req = {0};
759
760         HWRM_PREP(req, FUNC_RESOURCE_QCAPS, BNXT_USE_CHIMP_MB);
761         req.fid = rte_cpu_to_le_16(0xffff);
762
763         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
764
765         HWRM_CHECK_RESULT();
766
767         if (BNXT_VF(bp)) {
768                 bp->max_rsscos_ctx = rte_le_to_cpu_16(resp->max_rsscos_ctx);
769                 bp->max_cp_rings = rte_le_to_cpu_16(resp->max_cmpl_rings);
770                 bp->max_tx_rings = rte_le_to_cpu_16(resp->max_tx_rings);
771                 bp->max_rx_rings = rte_le_to_cpu_16(resp->max_rx_rings);
772                 bp->max_ring_grps = rte_le_to_cpu_32(resp->max_hw_ring_grps);
773                 bp->max_l2_ctx = rte_le_to_cpu_16(resp->max_l2_ctxs);
774                 bp->max_vnics = rte_le_to_cpu_16(resp->max_vnics);
775                 bp->max_stat_ctx = rte_le_to_cpu_16(resp->max_stat_ctx);
776         }
777         bp->vf_resv_strategy = rte_le_to_cpu_16(resp->vf_reservation_strategy);
778         if (bp->vf_resv_strategy >
779             HWRM_FUNC_RESOURCE_QCAPS_OUTPUT_VF_RESV_STRATEGY_MINIMAL_STATIC)
780                 bp->vf_resv_strategy =
781                 HWRM_FUNC_RESOURCE_QCAPS_OUTPUT_VF_RESERVATION_STRATEGY_MAXIMAL;
782
783         HWRM_UNLOCK();
784         return rc;
785 }
786
787 int bnxt_hwrm_ver_get(struct bnxt *bp)
788 {
789         int rc = 0;
790         struct hwrm_ver_get_input req = {.req_type = 0 };
791         struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
792         uint32_t fw_version;
793         uint16_t max_resp_len;
794         char type[RTE_MEMZONE_NAMESIZE];
795         uint32_t dev_caps_cfg;
796
797         bp->max_req_len = HWRM_MAX_REQ_LEN;
798         HWRM_PREP(req, VER_GET, BNXT_USE_CHIMP_MB);
799
800         req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
801         req.hwrm_intf_min = HWRM_VERSION_MINOR;
802         req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
803
804         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
805
806         HWRM_CHECK_RESULT();
807
808         PMD_DRV_LOG(INFO, "%d.%d.%d:%d.%d.%d\n",
809                 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
810                 resp->hwrm_intf_upd_8b, resp->hwrm_fw_maj_8b,
811                 resp->hwrm_fw_min_8b, resp->hwrm_fw_bld_8b);
812         bp->fw_ver = (resp->hwrm_fw_maj_8b << 24) |
813                      (resp->hwrm_fw_min_8b << 16) |
814                      (resp->hwrm_fw_bld_8b << 8) |
815                      resp->hwrm_fw_rsvd_8b;
816         PMD_DRV_LOG(INFO, "Driver HWRM version: %d.%d.%d\n",
817                 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, HWRM_VERSION_UPDATE);
818
819         fw_version = resp->hwrm_intf_maj_8b << 16;
820         fw_version |= resp->hwrm_intf_min_8b << 8;
821         fw_version |= resp->hwrm_intf_upd_8b;
822         bp->hwrm_spec_code = fw_version;
823
824         if (resp->hwrm_intf_maj_8b != HWRM_VERSION_MAJOR) {
825                 PMD_DRV_LOG(ERR, "Unsupported firmware API version\n");
826                 rc = -EINVAL;
827                 goto error;
828         }
829
830         if (bp->max_req_len > resp->max_req_win_len) {
831                 PMD_DRV_LOG(ERR, "Unsupported request length\n");
832                 rc = -EINVAL;
833         }
834         bp->max_req_len = rte_le_to_cpu_16(resp->max_req_win_len);
835         max_resp_len = resp->max_resp_len;
836         dev_caps_cfg = rte_le_to_cpu_32(resp->dev_caps_cfg);
837
838         if (bp->max_resp_len != max_resp_len) {
839                 sprintf(type, "bnxt_hwrm_%04x:%02x:%02x:%02x",
840                         bp->pdev->addr.domain, bp->pdev->addr.bus,
841                         bp->pdev->addr.devid, bp->pdev->addr.function);
842
843                 rte_free(bp->hwrm_cmd_resp_addr);
844
845                 bp->hwrm_cmd_resp_addr = rte_malloc(type, max_resp_len, 0);
846                 if (bp->hwrm_cmd_resp_addr == NULL) {
847                         rc = -ENOMEM;
848                         goto error;
849                 }
850                 rte_mem_lock_page(bp->hwrm_cmd_resp_addr);
851                 bp->hwrm_cmd_resp_dma_addr =
852                         rte_mem_virt2iova(bp->hwrm_cmd_resp_addr);
853                 if (bp->hwrm_cmd_resp_dma_addr == 0) {
854                         PMD_DRV_LOG(ERR,
855                         "Unable to map response buffer to physical memory.\n");
856                         rc = -ENOMEM;
857                         goto error;
858                 }
859                 bp->max_resp_len = max_resp_len;
860         }
861
862         if ((dev_caps_cfg &
863                 HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
864             (dev_caps_cfg &
865              HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) {
866                 PMD_DRV_LOG(DEBUG, "Short command supported\n");
867
868                 rte_free(bp->hwrm_short_cmd_req_addr);
869
870                 bp->hwrm_short_cmd_req_addr = rte_malloc(type,
871                                                         bp->max_req_len, 0);
872                 if (bp->hwrm_short_cmd_req_addr == NULL) {
873                         rc = -ENOMEM;
874                         goto error;
875                 }
876                 rte_mem_lock_page(bp->hwrm_short_cmd_req_addr);
877                 bp->hwrm_short_cmd_req_dma_addr =
878                         rte_mem_virt2iova(bp->hwrm_short_cmd_req_addr);
879                 if (bp->hwrm_short_cmd_req_dma_addr == 0) {
880                         rte_free(bp->hwrm_short_cmd_req_addr);
881                         PMD_DRV_LOG(ERR,
882                                 "Unable to map buffer to physical memory.\n");
883                         rc = -ENOMEM;
884                         goto error;
885                 }
886
887                 bp->flags |= BNXT_FLAG_SHORT_CMD;
888         }
889         if (dev_caps_cfg &
890             HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED) {
891                 bp->flags |= BNXT_FLAG_KONG_MB_EN;
892                 PMD_DRV_LOG(DEBUG, "Kong mailbox channel enabled\n");
893         }
894         if (dev_caps_cfg &
895             HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
896                 PMD_DRV_LOG(DEBUG, "FW supports Trusted VFs\n");
897
898 error:
899         HWRM_UNLOCK();
900         return rc;
901 }
902
903 int bnxt_hwrm_func_driver_unregister(struct bnxt *bp, uint32_t flags)
904 {
905         int rc;
906         struct hwrm_func_drv_unrgtr_input req = {.req_type = 0 };
907         struct hwrm_func_drv_unrgtr_output *resp = bp->hwrm_cmd_resp_addr;
908
909         if (!(bp->flags & BNXT_FLAG_REGISTERED))
910                 return 0;
911
912         HWRM_PREP(req, FUNC_DRV_UNRGTR, BNXT_USE_CHIMP_MB);
913         req.flags = flags;
914
915         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
916
917         HWRM_CHECK_RESULT();
918         HWRM_UNLOCK();
919
920         bp->flags &= ~BNXT_FLAG_REGISTERED;
921
922         return rc;
923 }
924
925 static int bnxt_hwrm_port_phy_cfg(struct bnxt *bp, struct bnxt_link_info *conf)
926 {
927         int rc = 0;
928         struct hwrm_port_phy_cfg_input req = {0};
929         struct hwrm_port_phy_cfg_output *resp = bp->hwrm_cmd_resp_addr;
930         uint32_t enables = 0;
931
932         HWRM_PREP(req, PORT_PHY_CFG, BNXT_USE_CHIMP_MB);
933
934         if (conf->link_up) {
935                 /* Setting Fixed Speed. But AutoNeg is ON, So disable it */
936                 if (bp->link_info.auto_mode && conf->link_speed) {
937                         req.auto_mode = HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_NONE;
938                         PMD_DRV_LOG(DEBUG, "Disabling AutoNeg\n");
939                 }
940
941                 req.flags = rte_cpu_to_le_32(conf->phy_flags);
942                 req.force_link_speed = rte_cpu_to_le_16(conf->link_speed);
943                 enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_MODE;
944                 /*
945                  * Note, ChiMP FW 20.2.1 and 20.2.2 return an error when we set
946                  * any auto mode, even "none".
947                  */
948                 if (!conf->link_speed) {
949                         /* No speeds specified. Enable AutoNeg - all speeds */
950                         req.auto_mode =
951                                 HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_ALL_SPEEDS;
952                 }
953                 /* AutoNeg - Advertise speeds specified. */
954                 if (conf->auto_link_speed_mask &&
955                     !(conf->phy_flags & HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE)) {
956                         req.auto_mode =
957                                 HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_SPEED_MASK;
958                         req.auto_link_speed_mask =
959                                 conf->auto_link_speed_mask;
960                         enables |=
961                         HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_LINK_SPEED_MASK;
962                 }
963
964                 req.auto_duplex = conf->duplex;
965                 enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_DUPLEX;
966                 req.auto_pause = conf->auto_pause;
967                 req.force_pause = conf->force_pause;
968                 /* Set force_pause if there is no auto or if there is a force */
969                 if (req.auto_pause && !req.force_pause)
970                         enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_PAUSE;
971                 else
972                         enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_FORCE_PAUSE;
973
974                 req.enables = rte_cpu_to_le_32(enables);
975         } else {
976                 req.flags =
977                 rte_cpu_to_le_32(HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE_LINK_DWN);
978                 PMD_DRV_LOG(INFO, "Force Link Down\n");
979         }
980
981         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
982
983         HWRM_CHECK_RESULT();
984         HWRM_UNLOCK();
985
986         return rc;
987 }
988
989 static int bnxt_hwrm_port_phy_qcfg(struct bnxt *bp,
990                                    struct bnxt_link_info *link_info)
991 {
992         int rc = 0;
993         struct hwrm_port_phy_qcfg_input req = {0};
994         struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
995
996         HWRM_PREP(req, PORT_PHY_QCFG, BNXT_USE_CHIMP_MB);
997
998         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
999
1000         HWRM_CHECK_RESULT();
1001
1002         link_info->phy_link_status = resp->link;
1003         link_info->link_up =
1004                 (link_info->phy_link_status ==
1005                  HWRM_PORT_PHY_QCFG_OUTPUT_LINK_LINK) ? 1 : 0;
1006         link_info->link_speed = rte_le_to_cpu_16(resp->link_speed);
1007         link_info->duplex = resp->duplex_cfg;
1008         link_info->pause = resp->pause;
1009         link_info->auto_pause = resp->auto_pause;
1010         link_info->force_pause = resp->force_pause;
1011         link_info->auto_mode = resp->auto_mode;
1012         link_info->phy_type = resp->phy_type;
1013         link_info->media_type = resp->media_type;
1014
1015         link_info->support_speeds = rte_le_to_cpu_16(resp->support_speeds);
1016         link_info->auto_link_speed = rte_le_to_cpu_16(resp->auto_link_speed);
1017         link_info->preemphasis = rte_le_to_cpu_32(resp->preemphasis);
1018         link_info->force_link_speed = rte_le_to_cpu_16(resp->force_link_speed);
1019         link_info->phy_ver[0] = resp->phy_maj;
1020         link_info->phy_ver[1] = resp->phy_min;
1021         link_info->phy_ver[2] = resp->phy_bld;
1022
1023         HWRM_UNLOCK();
1024
1025         PMD_DRV_LOG(DEBUG, "Link Speed %d\n", link_info->link_speed);
1026         PMD_DRV_LOG(DEBUG, "Auto Mode %d\n", link_info->auto_mode);
1027         PMD_DRV_LOG(DEBUG, "Support Speeds %x\n", link_info->support_speeds);
1028         PMD_DRV_LOG(DEBUG, "Auto Link Speed %x\n", link_info->auto_link_speed);
1029         PMD_DRV_LOG(DEBUG, "Auto Link Speed Mask %x\n",
1030                     link_info->auto_link_speed_mask);
1031         PMD_DRV_LOG(DEBUG, "Forced Link Speed %x\n",
1032                     link_info->force_link_speed);
1033
1034         return rc;
1035 }
1036
1037 int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
1038 {
1039         int rc = 0;
1040         struct hwrm_queue_qportcfg_input req = {.req_type = 0 };
1041         struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
1042         int i;
1043
1044         HWRM_PREP(req, QUEUE_QPORTCFG, BNXT_USE_CHIMP_MB);
1045
1046         req.flags = HWRM_QUEUE_QPORTCFG_INPUT_FLAGS_PATH_TX;
1047         /* HWRM Version >= 1.9.1 */
1048         if (bp->hwrm_spec_code >= HWRM_VERSION_1_9_1)
1049                 req.drv_qmap_cap =
1050                         HWRM_QUEUE_QPORTCFG_INPUT_DRV_QMAP_CAP_ENABLED;
1051         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1052
1053         HWRM_CHECK_RESULT();
1054
1055 #define GET_QUEUE_INFO(x) \
1056         bp->cos_queue[x].id = resp->queue_id##x; \
1057         bp->cos_queue[x].profile = resp->queue_id##x##_service_profile
1058
1059         GET_QUEUE_INFO(0);
1060         GET_QUEUE_INFO(1);
1061         GET_QUEUE_INFO(2);
1062         GET_QUEUE_INFO(3);
1063         GET_QUEUE_INFO(4);
1064         GET_QUEUE_INFO(5);
1065         GET_QUEUE_INFO(6);
1066         GET_QUEUE_INFO(7);
1067
1068         HWRM_UNLOCK();
1069
1070         if (bp->hwrm_spec_code < HWRM_VERSION_1_9_1) {
1071                 bp->tx_cosq_id = bp->cos_queue[0].id;
1072         } else {
1073                 /* iterate and find the COSq profile to use for Tx */
1074                 for (i = 0; i < BNXT_COS_QUEUE_COUNT; i++) {
1075                         if (bp->cos_queue[i].profile ==
1076                                 HWRM_QUEUE_SERVICE_PROFILE_LOSSY) {
1077                                 bp->tx_cosq_id = bp->cos_queue[i].id;
1078                                 break;
1079                         }
1080                 }
1081         }
1082         PMD_DRV_LOG(DEBUG, "Tx Cos Queue to use: %d\n", bp->tx_cosq_id);
1083
1084         return rc;
1085 }
1086
1087 int bnxt_hwrm_ring_alloc(struct bnxt *bp,
1088                          struct bnxt_ring *ring,
1089                          uint32_t ring_type, uint32_t map_index,
1090                          uint32_t stats_ctx_id, uint32_t cmpl_ring_id)
1091 {
1092         int rc = 0;
1093         uint32_t enables = 0;
1094         struct hwrm_ring_alloc_input req = {.req_type = 0 };
1095         struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
1096
1097         HWRM_PREP(req, RING_ALLOC, BNXT_USE_CHIMP_MB);
1098
1099         req.page_tbl_addr = rte_cpu_to_le_64(ring->bd_dma);
1100         req.fbo = rte_cpu_to_le_32(0);
1101         /* Association of ring index with doorbell index */
1102         req.logical_id = rte_cpu_to_le_16(map_index);
1103         req.length = rte_cpu_to_le_32(ring->ring_size);
1104
1105         switch (ring_type) {
1106         case HWRM_RING_ALLOC_INPUT_RING_TYPE_TX:
1107                 req.queue_id = rte_cpu_to_le_16(bp->tx_cosq_id);
1108                 /* FALLTHROUGH */
1109         case HWRM_RING_ALLOC_INPUT_RING_TYPE_RX:
1110                 req.ring_type = ring_type;
1111                 req.cmpl_ring_id = rte_cpu_to_le_16(cmpl_ring_id);
1112                 req.stat_ctx_id = rte_cpu_to_le_16(stats_ctx_id);
1113                 if (stats_ctx_id != INVALID_STATS_CTX_ID)
1114                         enables |=
1115                         HWRM_RING_ALLOC_INPUT_ENABLES_STAT_CTX_ID_VALID;
1116                 break;
1117         case HWRM_RING_ALLOC_INPUT_RING_TYPE_L2_CMPL:
1118                 req.ring_type = ring_type;
1119                 /*
1120                  * TODO: Some HWRM versions crash with
1121                  * HWRM_RING_ALLOC_INPUT_INT_MODE_POLL
1122                  */
1123                 req.int_mode = HWRM_RING_ALLOC_INPUT_INT_MODE_MSIX;
1124                 break;
1125         default:
1126                 PMD_DRV_LOG(ERR, "hwrm alloc invalid ring type %d\n",
1127                         ring_type);
1128                 HWRM_UNLOCK();
1129                 return -1;
1130         }
1131         req.enables = rte_cpu_to_le_32(enables);
1132
1133         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1134
1135         if (rc || resp->error_code) {
1136                 if (rc == 0 && resp->error_code)
1137                         rc = rte_le_to_cpu_16(resp->error_code);
1138                 switch (ring_type) {
1139                 case HWRM_RING_FREE_INPUT_RING_TYPE_L2_CMPL:
1140                         PMD_DRV_LOG(ERR,
1141                                 "hwrm_ring_alloc cp failed. rc:%d\n", rc);
1142                         HWRM_UNLOCK();
1143                         return rc;
1144                 case HWRM_RING_FREE_INPUT_RING_TYPE_RX:
1145                         PMD_DRV_LOG(ERR,
1146                                 "hwrm_ring_alloc rx failed. rc:%d\n", rc);
1147                         HWRM_UNLOCK();
1148                         return rc;
1149                 case HWRM_RING_FREE_INPUT_RING_TYPE_TX:
1150                         PMD_DRV_LOG(ERR,
1151                                 "hwrm_ring_alloc tx failed. rc:%d\n", rc);
1152                         HWRM_UNLOCK();
1153                         return rc;
1154                 default:
1155                         PMD_DRV_LOG(ERR, "Invalid ring. rc:%d\n", rc);
1156                         HWRM_UNLOCK();
1157                         return rc;
1158                 }
1159         }
1160
1161         ring->fw_ring_id = rte_le_to_cpu_16(resp->ring_id);
1162         HWRM_UNLOCK();
1163         return rc;
1164 }
1165
1166 int bnxt_hwrm_ring_free(struct bnxt *bp,
1167                         struct bnxt_ring *ring, uint32_t ring_type)
1168 {
1169         int rc;
1170         struct hwrm_ring_free_input req = {.req_type = 0 };
1171         struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
1172
1173         HWRM_PREP(req, RING_FREE, BNXT_USE_CHIMP_MB);
1174
1175         req.ring_type = ring_type;
1176         req.ring_id = rte_cpu_to_le_16(ring->fw_ring_id);
1177
1178         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1179
1180         if (rc || resp->error_code) {
1181                 if (rc == 0 && resp->error_code)
1182                         rc = rte_le_to_cpu_16(resp->error_code);
1183                 HWRM_UNLOCK();
1184
1185                 switch (ring_type) {
1186                 case HWRM_RING_FREE_INPUT_RING_TYPE_L2_CMPL:
1187                         PMD_DRV_LOG(ERR, "hwrm_ring_free cp failed. rc:%d\n",
1188                                 rc);
1189                         return rc;
1190                 case HWRM_RING_FREE_INPUT_RING_TYPE_RX:
1191                         PMD_DRV_LOG(ERR, "hwrm_ring_free rx failed. rc:%d\n",
1192                                 rc);
1193                         return rc;
1194                 case HWRM_RING_FREE_INPUT_RING_TYPE_TX:
1195                         PMD_DRV_LOG(ERR, "hwrm_ring_free tx failed. rc:%d\n",
1196                                 rc);
1197                         return rc;
1198                 default:
1199                         PMD_DRV_LOG(ERR, "Invalid ring, rc:%d\n", rc);
1200                         return rc;
1201                 }
1202         }
1203         HWRM_UNLOCK();
1204         return 0;
1205 }
1206
1207 int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp, unsigned int idx)
1208 {
1209         int rc = 0;
1210         struct hwrm_ring_grp_alloc_input req = {.req_type = 0 };
1211         struct hwrm_ring_grp_alloc_output *resp = bp->hwrm_cmd_resp_addr;
1212
1213         HWRM_PREP(req, RING_GRP_ALLOC, BNXT_USE_CHIMP_MB);
1214
1215         req.cr = rte_cpu_to_le_16(bp->grp_info[idx].cp_fw_ring_id);
1216         req.rr = rte_cpu_to_le_16(bp->grp_info[idx].rx_fw_ring_id);
1217         req.ar = rte_cpu_to_le_16(bp->grp_info[idx].ag_fw_ring_id);
1218         req.sc = rte_cpu_to_le_16(bp->grp_info[idx].fw_stats_ctx);
1219
1220         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1221
1222         HWRM_CHECK_RESULT();
1223
1224         bp->grp_info[idx].fw_grp_id =
1225             rte_le_to_cpu_16(resp->ring_group_id);
1226
1227         HWRM_UNLOCK();
1228
1229         return rc;
1230 }
1231
1232 int bnxt_hwrm_ring_grp_free(struct bnxt *bp, unsigned int idx)
1233 {
1234         int rc;
1235         struct hwrm_ring_grp_free_input req = {.req_type = 0 };
1236         struct hwrm_ring_grp_free_output *resp = bp->hwrm_cmd_resp_addr;
1237
1238         HWRM_PREP(req, RING_GRP_FREE, BNXT_USE_CHIMP_MB);
1239
1240         req.ring_group_id = rte_cpu_to_le_16(bp->grp_info[idx].fw_grp_id);
1241
1242         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1243
1244         HWRM_CHECK_RESULT();
1245         HWRM_UNLOCK();
1246
1247         bp->grp_info[idx].fw_grp_id = INVALID_HW_RING_ID;
1248         return rc;
1249 }
1250
1251 int bnxt_hwrm_stat_clear(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1252 {
1253         int rc = 0;
1254         struct hwrm_stat_ctx_clr_stats_input req = {.req_type = 0 };
1255         struct hwrm_stat_ctx_clr_stats_output *resp = bp->hwrm_cmd_resp_addr;
1256
1257         if (cpr->hw_stats_ctx_id == (uint32_t)HWRM_NA_SIGNATURE)
1258                 return rc;
1259
1260         HWRM_PREP(req, STAT_CTX_CLR_STATS, BNXT_USE_CHIMP_MB);
1261
1262         req.stat_ctx_id = rte_cpu_to_le_16(cpr->hw_stats_ctx_id);
1263
1264         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1265
1266         HWRM_CHECK_RESULT();
1267         HWRM_UNLOCK();
1268
1269         return rc;
1270 }
1271
1272 int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1273                                 unsigned int idx __rte_unused)
1274 {
1275         int rc;
1276         struct hwrm_stat_ctx_alloc_input req = {.req_type = 0 };
1277         struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
1278
1279         HWRM_PREP(req, STAT_CTX_ALLOC, BNXT_USE_CHIMP_MB);
1280
1281         req.update_period_ms = rte_cpu_to_le_32(0);
1282
1283         req.stats_dma_addr =
1284             rte_cpu_to_le_64(cpr->hw_stats_map);
1285
1286         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1287
1288         HWRM_CHECK_RESULT();
1289
1290         cpr->hw_stats_ctx_id = rte_le_to_cpu_16(resp->stat_ctx_id);
1291
1292         HWRM_UNLOCK();
1293
1294         return rc;
1295 }
1296
1297 int bnxt_hwrm_stat_ctx_free(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1298                                 unsigned int idx __rte_unused)
1299 {
1300         int rc;
1301         struct hwrm_stat_ctx_free_input req = {.req_type = 0 };
1302         struct hwrm_stat_ctx_free_output *resp = bp->hwrm_cmd_resp_addr;
1303
1304         HWRM_PREP(req, STAT_CTX_FREE, BNXT_USE_CHIMP_MB);
1305
1306         req.stat_ctx_id = rte_cpu_to_le_16(cpr->hw_stats_ctx_id);
1307
1308         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1309
1310         HWRM_CHECK_RESULT();
1311         HWRM_UNLOCK();
1312
1313         return rc;
1314 }
1315
1316 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1317 {
1318         int rc = 0, i, j;
1319         struct hwrm_vnic_alloc_input req = { 0 };
1320         struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
1321
1322         /* map ring groups to this vnic */
1323         PMD_DRV_LOG(DEBUG, "Alloc VNIC. Start %x, End %x\n",
1324                 vnic->start_grp_id, vnic->end_grp_id);
1325         for (i = vnic->start_grp_id, j = 0; i < vnic->end_grp_id; i++, j++)
1326                 vnic->fw_grp_ids[j] = bp->grp_info[i].fw_grp_id;
1327
1328         vnic->dflt_ring_grp = bp->grp_info[vnic->start_grp_id].fw_grp_id;
1329         vnic->rss_rule = (uint16_t)HWRM_NA_SIGNATURE;
1330         vnic->cos_rule = (uint16_t)HWRM_NA_SIGNATURE;
1331         vnic->lb_rule = (uint16_t)HWRM_NA_SIGNATURE;
1332         vnic->mru = bp->eth_dev->data->mtu + RTE_ETHER_HDR_LEN +
1333                                 RTE_ETHER_CRC_LEN + VLAN_TAG_SIZE;
1334         HWRM_PREP(req, VNIC_ALLOC, BNXT_USE_CHIMP_MB);
1335
1336         if (vnic->func_default)
1337                 req.flags =
1338                         rte_cpu_to_le_32(HWRM_VNIC_ALLOC_INPUT_FLAGS_DEFAULT);
1339         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1340
1341         HWRM_CHECK_RESULT();
1342
1343         vnic->fw_vnic_id = rte_le_to_cpu_16(resp->vnic_id);
1344         HWRM_UNLOCK();
1345         PMD_DRV_LOG(DEBUG, "VNIC ID %x\n", vnic->fw_vnic_id);
1346         return rc;
1347 }
1348
1349 static int bnxt_hwrm_vnic_plcmodes_qcfg(struct bnxt *bp,
1350                                         struct bnxt_vnic_info *vnic,
1351                                         struct bnxt_plcmodes_cfg *pmode)
1352 {
1353         int rc = 0;
1354         struct hwrm_vnic_plcmodes_qcfg_input req = {.req_type = 0 };
1355         struct hwrm_vnic_plcmodes_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
1356
1357         HWRM_PREP(req, VNIC_PLCMODES_QCFG, BNXT_USE_CHIMP_MB);
1358
1359         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1360
1361         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1362
1363         HWRM_CHECK_RESULT();
1364
1365         pmode->flags = rte_le_to_cpu_32(resp->flags);
1366         /* dflt_vnic bit doesn't exist in the _cfg command */
1367         pmode->flags &= ~(HWRM_VNIC_PLCMODES_QCFG_OUTPUT_FLAGS_DFLT_VNIC);
1368         pmode->jumbo_thresh = rte_le_to_cpu_16(resp->jumbo_thresh);
1369         pmode->hds_offset = rte_le_to_cpu_16(resp->hds_offset);
1370         pmode->hds_threshold = rte_le_to_cpu_16(resp->hds_threshold);
1371
1372         HWRM_UNLOCK();
1373
1374         return rc;
1375 }
1376
1377 static int bnxt_hwrm_vnic_plcmodes_cfg(struct bnxt *bp,
1378                                        struct bnxt_vnic_info *vnic,
1379                                        struct bnxt_plcmodes_cfg *pmode)
1380 {
1381         int rc = 0;
1382         struct hwrm_vnic_plcmodes_cfg_input req = {.req_type = 0 };
1383         struct hwrm_vnic_plcmodes_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1384
1385         HWRM_PREP(req, VNIC_PLCMODES_CFG, BNXT_USE_CHIMP_MB);
1386
1387         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1388         req.flags = rte_cpu_to_le_32(pmode->flags);
1389         req.jumbo_thresh = rte_cpu_to_le_16(pmode->jumbo_thresh);
1390         req.hds_offset = rte_cpu_to_le_16(pmode->hds_offset);
1391         req.hds_threshold = rte_cpu_to_le_16(pmode->hds_threshold);
1392         req.enables = rte_cpu_to_le_32(
1393             HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_HDS_THRESHOLD_VALID |
1394             HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_HDS_OFFSET_VALID |
1395             HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_JUMBO_THRESH_VALID
1396         );
1397
1398         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1399
1400         HWRM_CHECK_RESULT();
1401         HWRM_UNLOCK();
1402
1403         return rc;
1404 }
1405
1406 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1407 {
1408         int rc = 0;
1409         struct hwrm_vnic_cfg_input req = {.req_type = 0 };
1410         struct hwrm_vnic_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1411         uint32_t ctx_enable_flag = 0;
1412         struct bnxt_plcmodes_cfg pmodes;
1413
1414         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
1415                 PMD_DRV_LOG(DEBUG, "VNIC ID %x\n", vnic->fw_vnic_id);
1416                 return rc;
1417         }
1418
1419         rc = bnxt_hwrm_vnic_plcmodes_qcfg(bp, vnic, &pmodes);
1420         if (rc)
1421                 return rc;
1422
1423         HWRM_PREP(req, VNIC_CFG, BNXT_USE_CHIMP_MB);
1424
1425         /* Only RSS support for now TBD: COS & LB */
1426         req.enables =
1427             rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_ENABLES_DFLT_RING_GRP);
1428         if (vnic->lb_rule != 0xffff)
1429                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_LB_RULE;
1430         if (vnic->cos_rule != 0xffff)
1431                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_COS_RULE;
1432         if (vnic->rss_rule != 0xffff) {
1433                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_MRU;
1434                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_RSS_RULE;
1435         }
1436         req.enables |= rte_cpu_to_le_32(ctx_enable_flag);
1437         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1438         req.dflt_ring_grp = rte_cpu_to_le_16(vnic->dflt_ring_grp);
1439         req.rss_rule = rte_cpu_to_le_16(vnic->rss_rule);
1440         req.cos_rule = rte_cpu_to_le_16(vnic->cos_rule);
1441         req.lb_rule = rte_cpu_to_le_16(vnic->lb_rule);
1442         req.mru = rte_cpu_to_le_16(vnic->mru);
1443         /* Configure default VNIC only once. */
1444         if (vnic->func_default && !(bp->flags & BNXT_FLAG_DFLT_VNIC_SET)) {
1445                 req.flags |=
1446                     rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_FLAGS_DEFAULT);
1447                 bp->flags |= BNXT_FLAG_DFLT_VNIC_SET;
1448         }
1449         if (vnic->vlan_strip)
1450                 req.flags |=
1451                     rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_FLAGS_VLAN_STRIP_MODE);
1452         if (vnic->bd_stall)
1453                 req.flags |=
1454                     rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_FLAGS_BD_STALL_MODE);
1455         if (vnic->roce_dual)
1456                 req.flags |= rte_cpu_to_le_32(
1457                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_ROCE_DUAL_VNIC_MODE);
1458         if (vnic->roce_only)
1459                 req.flags |= rte_cpu_to_le_32(
1460                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_ROCE_ONLY_VNIC_MODE);
1461         if (vnic->rss_dflt_cr)
1462                 req.flags |= rte_cpu_to_le_32(
1463                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_RSS_DFLT_CR_MODE);
1464
1465         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1466
1467         HWRM_CHECK_RESULT();
1468         HWRM_UNLOCK();
1469
1470         rc = bnxt_hwrm_vnic_plcmodes_cfg(bp, vnic, &pmodes);
1471
1472         return rc;
1473 }
1474
1475 int bnxt_hwrm_vnic_qcfg(struct bnxt *bp, struct bnxt_vnic_info *vnic,
1476                 int16_t fw_vf_id)
1477 {
1478         int rc = 0;
1479         struct hwrm_vnic_qcfg_input req = {.req_type = 0 };
1480         struct hwrm_vnic_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
1481
1482         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
1483                 PMD_DRV_LOG(DEBUG, "VNIC QCFG ID %d\n", vnic->fw_vnic_id);
1484                 return rc;
1485         }
1486         HWRM_PREP(req, VNIC_QCFG, BNXT_USE_CHIMP_MB);
1487
1488         req.enables =
1489                 rte_cpu_to_le_32(HWRM_VNIC_QCFG_INPUT_ENABLES_VF_ID_VALID);
1490         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1491         req.vf_id = rte_cpu_to_le_16(fw_vf_id);
1492
1493         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1494
1495         HWRM_CHECK_RESULT();
1496
1497         vnic->dflt_ring_grp = rte_le_to_cpu_16(resp->dflt_ring_grp);
1498         vnic->rss_rule = rte_le_to_cpu_16(resp->rss_rule);
1499         vnic->cos_rule = rte_le_to_cpu_16(resp->cos_rule);
1500         vnic->lb_rule = rte_le_to_cpu_16(resp->lb_rule);
1501         vnic->mru = rte_le_to_cpu_16(resp->mru);
1502         vnic->func_default = rte_le_to_cpu_32(
1503                         resp->flags) & HWRM_VNIC_QCFG_OUTPUT_FLAGS_DEFAULT;
1504         vnic->vlan_strip = rte_le_to_cpu_32(resp->flags) &
1505                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_VLAN_STRIP_MODE;
1506         vnic->bd_stall = rte_le_to_cpu_32(resp->flags) &
1507                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_BD_STALL_MODE;
1508         vnic->roce_dual = rte_le_to_cpu_32(resp->flags) &
1509                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_ROCE_DUAL_VNIC_MODE;
1510         vnic->roce_only = rte_le_to_cpu_32(resp->flags) &
1511                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_ROCE_ONLY_VNIC_MODE;
1512         vnic->rss_dflt_cr = rte_le_to_cpu_32(resp->flags) &
1513                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_RSS_DFLT_CR_MODE;
1514
1515         HWRM_UNLOCK();
1516
1517         return rc;
1518 }
1519
1520 int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1521 {
1522         int rc = 0;
1523         struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {.req_type = 0 };
1524         struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
1525                                                 bp->hwrm_cmd_resp_addr;
1526
1527         HWRM_PREP(req, VNIC_RSS_COS_LB_CTX_ALLOC, BNXT_USE_CHIMP_MB);
1528
1529         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1530
1531         HWRM_CHECK_RESULT();
1532
1533         vnic->rss_rule = rte_le_to_cpu_16(resp->rss_cos_lb_ctx_id);
1534         HWRM_UNLOCK();
1535         PMD_DRV_LOG(DEBUG, "VNIC RSS Rule %x\n", vnic->rss_rule);
1536
1537         return rc;
1538 }
1539
1540 int bnxt_hwrm_vnic_ctx_free(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1541 {
1542         int rc = 0;
1543         struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {.req_type = 0 };
1544         struct hwrm_vnic_rss_cos_lb_ctx_free_output *resp =
1545                                                 bp->hwrm_cmd_resp_addr;
1546
1547         if (vnic->rss_rule == 0xffff) {
1548                 PMD_DRV_LOG(DEBUG, "VNIC RSS Rule %x\n", vnic->rss_rule);
1549                 return rc;
1550         }
1551         HWRM_PREP(req, VNIC_RSS_COS_LB_CTX_FREE, BNXT_USE_CHIMP_MB);
1552
1553         req.rss_cos_lb_ctx_id = rte_cpu_to_le_16(vnic->rss_rule);
1554
1555         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1556
1557         HWRM_CHECK_RESULT();
1558         HWRM_UNLOCK();
1559
1560         vnic->rss_rule = INVALID_HW_RING_ID;
1561
1562         return rc;
1563 }
1564
1565 int bnxt_hwrm_vnic_free(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1566 {
1567         int rc = 0;
1568         struct hwrm_vnic_free_input req = {.req_type = 0 };
1569         struct hwrm_vnic_free_output *resp = bp->hwrm_cmd_resp_addr;
1570
1571         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
1572                 PMD_DRV_LOG(DEBUG, "VNIC FREE ID %x\n", vnic->fw_vnic_id);
1573                 return rc;
1574         }
1575
1576         HWRM_PREP(req, VNIC_FREE, BNXT_USE_CHIMP_MB);
1577
1578         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1579
1580         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1581
1582         HWRM_CHECK_RESULT();
1583         HWRM_UNLOCK();
1584
1585         vnic->fw_vnic_id = INVALID_HW_RING_ID;
1586         /* Configure default VNIC again if necessary. */
1587         if (vnic->func_default && (bp->flags & BNXT_FLAG_DFLT_VNIC_SET))
1588                 bp->flags &= ~BNXT_FLAG_DFLT_VNIC_SET;
1589
1590         return rc;
1591 }
1592
1593 int bnxt_hwrm_vnic_rss_cfg(struct bnxt *bp,
1594                            struct bnxt_vnic_info *vnic)
1595 {
1596         int rc = 0;
1597         struct hwrm_vnic_rss_cfg_input req = {.req_type = 0 };
1598         struct hwrm_vnic_rss_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1599
1600         HWRM_PREP(req, VNIC_RSS_CFG, BNXT_USE_CHIMP_MB);
1601
1602         req.hash_type = rte_cpu_to_le_32(vnic->hash_type);
1603         req.hash_mode_flags = vnic->hash_mode;
1604
1605         req.ring_grp_tbl_addr =
1606             rte_cpu_to_le_64(vnic->rss_table_dma_addr);
1607         req.hash_key_tbl_addr =
1608             rte_cpu_to_le_64(vnic->rss_hash_key_dma_addr);
1609         req.rss_ctx_idx = rte_cpu_to_le_16(vnic->rss_rule);
1610
1611         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1612
1613         HWRM_CHECK_RESULT();
1614         HWRM_UNLOCK();
1615
1616         return rc;
1617 }
1618
1619 int bnxt_hwrm_vnic_plcmode_cfg(struct bnxt *bp,
1620                         struct bnxt_vnic_info *vnic)
1621 {
1622         int rc = 0;
1623         struct hwrm_vnic_plcmodes_cfg_input req = {.req_type = 0 };
1624         struct hwrm_vnic_plcmodes_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1625         uint16_t size;
1626
1627         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
1628                 PMD_DRV_LOG(DEBUG, "VNIC ID %x\n", vnic->fw_vnic_id);
1629                 return rc;
1630         }
1631
1632         HWRM_PREP(req, VNIC_PLCMODES_CFG, BNXT_USE_CHIMP_MB);
1633
1634         req.flags = rte_cpu_to_le_32(
1635                         HWRM_VNIC_PLCMODES_CFG_INPUT_FLAGS_JUMBO_PLACEMENT);
1636
1637         req.enables = rte_cpu_to_le_32(
1638                 HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_JUMBO_THRESH_VALID);
1639
1640         size = rte_pktmbuf_data_room_size(bp->rx_queues[0]->mb_pool);
1641         size -= RTE_PKTMBUF_HEADROOM;
1642
1643         req.jumbo_thresh = rte_cpu_to_le_16(size);
1644         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1645
1646         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1647
1648         HWRM_CHECK_RESULT();
1649         HWRM_UNLOCK();
1650
1651         return rc;
1652 }
1653
1654 int bnxt_hwrm_vnic_tpa_cfg(struct bnxt *bp,
1655                         struct bnxt_vnic_info *vnic, bool enable)
1656 {
1657         int rc = 0;
1658         struct hwrm_vnic_tpa_cfg_input req = {.req_type = 0 };
1659         struct hwrm_vnic_tpa_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1660
1661         HWRM_PREP(req, VNIC_TPA_CFG, BNXT_USE_CHIMP_MB);
1662
1663         if (enable) {
1664                 req.enables = rte_cpu_to_le_32(
1665                                 HWRM_VNIC_TPA_CFG_INPUT_ENABLES_MAX_AGG_SEGS |
1666                                 HWRM_VNIC_TPA_CFG_INPUT_ENABLES_MAX_AGGS |
1667                                 HWRM_VNIC_TPA_CFG_INPUT_ENABLES_MIN_AGG_LEN);
1668                 req.flags = rte_cpu_to_le_32(
1669                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_TPA |
1670                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_ENCAP_TPA |
1671                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_RSC_WND_UPDATE |
1672                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_GRO |
1673                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_AGG_WITH_ECN |
1674                         HWRM_VNIC_TPA_CFG_INPUT_FLAGS_AGG_WITH_SAME_GRE_SEQ);
1675                 req.max_agg_segs = rte_cpu_to_le_16(5);
1676                 req.max_aggs =
1677                         rte_cpu_to_le_16(HWRM_VNIC_TPA_CFG_INPUT_MAX_AGGS_MAX);
1678                 req.min_agg_len = rte_cpu_to_le_32(512);
1679         }
1680         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1681
1682         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1683
1684         HWRM_CHECK_RESULT();
1685         HWRM_UNLOCK();
1686
1687         return rc;
1688 }
1689
1690 int bnxt_hwrm_func_vf_mac(struct bnxt *bp, uint16_t vf, const uint8_t *mac_addr)
1691 {
1692         struct hwrm_func_cfg_input req = {0};
1693         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1694         int rc;
1695
1696         req.flags = rte_cpu_to_le_32(bp->pf.vf_info[vf].func_cfg_flags);
1697         req.enables = rte_cpu_to_le_32(
1698                         HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
1699         memcpy(req.dflt_mac_addr, mac_addr, sizeof(req.dflt_mac_addr));
1700         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
1701
1702         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
1703
1704         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1705         HWRM_CHECK_RESULT();
1706         HWRM_UNLOCK();
1707
1708         bp->pf.vf_info[vf].random_mac = false;
1709
1710         return rc;
1711 }
1712
1713 int bnxt_hwrm_func_qstats_tx_drop(struct bnxt *bp, uint16_t fid,
1714                                   uint64_t *dropped)
1715 {
1716         int rc = 0;
1717         struct hwrm_func_qstats_input req = {.req_type = 0};
1718         struct hwrm_func_qstats_output *resp = bp->hwrm_cmd_resp_addr;
1719
1720         HWRM_PREP(req, FUNC_QSTATS, BNXT_USE_CHIMP_MB);
1721
1722         req.fid = rte_cpu_to_le_16(fid);
1723
1724         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1725
1726         HWRM_CHECK_RESULT();
1727
1728         if (dropped)
1729                 *dropped = rte_le_to_cpu_64(resp->tx_drop_pkts);
1730
1731         HWRM_UNLOCK();
1732
1733         return rc;
1734 }
1735
1736 int bnxt_hwrm_func_qstats(struct bnxt *bp, uint16_t fid,
1737                           struct rte_eth_stats *stats)
1738 {
1739         int rc = 0;
1740         struct hwrm_func_qstats_input req = {.req_type = 0};
1741         struct hwrm_func_qstats_output *resp = bp->hwrm_cmd_resp_addr;
1742
1743         HWRM_PREP(req, FUNC_QSTATS, BNXT_USE_CHIMP_MB);
1744
1745         req.fid = rte_cpu_to_le_16(fid);
1746
1747         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1748
1749         HWRM_CHECK_RESULT();
1750
1751         stats->ipackets = rte_le_to_cpu_64(resp->rx_ucast_pkts);
1752         stats->ipackets += rte_le_to_cpu_64(resp->rx_mcast_pkts);
1753         stats->ipackets += rte_le_to_cpu_64(resp->rx_bcast_pkts);
1754         stats->ibytes = rte_le_to_cpu_64(resp->rx_ucast_bytes);
1755         stats->ibytes += rte_le_to_cpu_64(resp->rx_mcast_bytes);
1756         stats->ibytes += rte_le_to_cpu_64(resp->rx_bcast_bytes);
1757
1758         stats->opackets = rte_le_to_cpu_64(resp->tx_ucast_pkts);
1759         stats->opackets += rte_le_to_cpu_64(resp->tx_mcast_pkts);
1760         stats->opackets += rte_le_to_cpu_64(resp->tx_bcast_pkts);
1761         stats->obytes = rte_le_to_cpu_64(resp->tx_ucast_bytes);
1762         stats->obytes += rte_le_to_cpu_64(resp->tx_mcast_bytes);
1763         stats->obytes += rte_le_to_cpu_64(resp->tx_bcast_bytes);
1764
1765         stats->imissed = rte_le_to_cpu_64(resp->rx_discard_pkts);
1766         stats->ierrors = rte_le_to_cpu_64(resp->rx_drop_pkts);
1767         stats->oerrors = rte_le_to_cpu_64(resp->tx_discard_pkts);
1768
1769         HWRM_UNLOCK();
1770
1771         return rc;
1772 }
1773
1774 int bnxt_hwrm_func_clr_stats(struct bnxt *bp, uint16_t fid)
1775 {
1776         int rc = 0;
1777         struct hwrm_func_clr_stats_input req = {.req_type = 0};
1778         struct hwrm_func_clr_stats_output *resp = bp->hwrm_cmd_resp_addr;
1779
1780         HWRM_PREP(req, FUNC_CLR_STATS, BNXT_USE_CHIMP_MB);
1781
1782         req.fid = rte_cpu_to_le_16(fid);
1783
1784         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1785
1786         HWRM_CHECK_RESULT();
1787         HWRM_UNLOCK();
1788
1789         return rc;
1790 }
1791
1792 /*
1793  * HWRM utility functions
1794  */
1795
1796 int bnxt_clear_all_hwrm_stat_ctxs(struct bnxt *bp)
1797 {
1798         unsigned int i;
1799         int rc = 0;
1800
1801         for (i = 0; i < bp->rx_cp_nr_rings + bp->tx_cp_nr_rings; i++) {
1802                 struct bnxt_tx_queue *txq;
1803                 struct bnxt_rx_queue *rxq;
1804                 struct bnxt_cp_ring_info *cpr;
1805
1806                 if (i >= bp->rx_cp_nr_rings) {
1807                         txq = bp->tx_queues[i - bp->rx_cp_nr_rings];
1808                         cpr = txq->cp_ring;
1809                 } else {
1810                         rxq = bp->rx_queues[i];
1811                         cpr = rxq->cp_ring;
1812                 }
1813
1814                 rc = bnxt_hwrm_stat_clear(bp, cpr);
1815                 if (rc)
1816                         return rc;
1817         }
1818         return 0;
1819 }
1820
1821 int bnxt_free_all_hwrm_stat_ctxs(struct bnxt *bp)
1822 {
1823         int rc;
1824         unsigned int i;
1825         struct bnxt_cp_ring_info *cpr;
1826
1827         for (i = 0; i < bp->rx_cp_nr_rings + bp->tx_cp_nr_rings; i++) {
1828
1829                 if (i >= bp->rx_cp_nr_rings) {
1830                         cpr = bp->tx_queues[i - bp->rx_cp_nr_rings]->cp_ring;
1831                 } else {
1832                         cpr = bp->rx_queues[i]->cp_ring;
1833                         bp->grp_info[i].fw_stats_ctx = -1;
1834                 }
1835                 if (cpr->hw_stats_ctx_id != HWRM_NA_SIGNATURE) {
1836                         rc = bnxt_hwrm_stat_ctx_free(bp, cpr, i);
1837                         cpr->hw_stats_ctx_id = HWRM_NA_SIGNATURE;
1838                         if (rc)
1839                                 return rc;
1840                 }
1841         }
1842         return 0;
1843 }
1844
1845 int bnxt_alloc_all_hwrm_stat_ctxs(struct bnxt *bp)
1846 {
1847         unsigned int i;
1848         int rc = 0;
1849
1850         for (i = 0; i < bp->rx_cp_nr_rings + bp->tx_cp_nr_rings; i++) {
1851                 struct bnxt_tx_queue *txq;
1852                 struct bnxt_rx_queue *rxq;
1853                 struct bnxt_cp_ring_info *cpr;
1854
1855                 if (i >= bp->rx_cp_nr_rings) {
1856                         txq = bp->tx_queues[i - bp->rx_cp_nr_rings];
1857                         cpr = txq->cp_ring;
1858                 } else {
1859                         rxq = bp->rx_queues[i];
1860                         cpr = rxq->cp_ring;
1861                 }
1862
1863                 rc = bnxt_hwrm_stat_ctx_alloc(bp, cpr, i);
1864
1865                 if (rc)
1866                         return rc;
1867         }
1868         return rc;
1869 }
1870
1871 int bnxt_free_all_hwrm_ring_grps(struct bnxt *bp)
1872 {
1873         uint16_t idx;
1874         uint32_t rc = 0;
1875
1876         for (idx = 0; idx < bp->rx_cp_nr_rings; idx++) {
1877
1878                 if (bp->grp_info[idx].fw_grp_id == INVALID_HW_RING_ID)
1879                         continue;
1880
1881                 rc = bnxt_hwrm_ring_grp_free(bp, idx);
1882
1883                 if (rc)
1884                         return rc;
1885         }
1886         return rc;
1887 }
1888
1889 static void bnxt_free_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1890 {
1891         struct bnxt_ring *cp_ring = cpr->cp_ring_struct;
1892
1893         bnxt_hwrm_ring_free(bp, cp_ring,
1894                         HWRM_RING_FREE_INPUT_RING_TYPE_L2_CMPL);
1895         cp_ring->fw_ring_id = INVALID_HW_RING_ID;
1896         memset(cpr->cp_desc_ring, 0, cpr->cp_ring_struct->ring_size *
1897                         sizeof(*cpr->cp_desc_ring));
1898         cpr->cp_raw_cons = 0;
1899 }
1900
1901 void bnxt_free_hwrm_rx_ring(struct bnxt *bp, int queue_index)
1902 {
1903         struct bnxt_rx_queue *rxq = bp->rx_queues[queue_index];
1904         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
1905         struct bnxt_ring *ring = rxr->rx_ring_struct;
1906         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
1907
1908         if (ring->fw_ring_id != INVALID_HW_RING_ID) {
1909                 bnxt_hwrm_ring_free(bp, ring,
1910                                     HWRM_RING_FREE_INPUT_RING_TYPE_RX);
1911                 ring->fw_ring_id = INVALID_HW_RING_ID;
1912                 bp->grp_info[queue_index].rx_fw_ring_id = INVALID_HW_RING_ID;
1913                 memset(rxr->rx_desc_ring, 0,
1914                        rxr->rx_ring_struct->ring_size *
1915                        sizeof(*rxr->rx_desc_ring));
1916                 memset(rxr->rx_buf_ring, 0,
1917                        rxr->rx_ring_struct->ring_size *
1918                        sizeof(*rxr->rx_buf_ring));
1919                 rxr->rx_prod = 0;
1920         }
1921         ring = rxr->ag_ring_struct;
1922         if (ring->fw_ring_id != INVALID_HW_RING_ID) {
1923                 bnxt_hwrm_ring_free(bp, ring,
1924                                     HWRM_RING_FREE_INPUT_RING_TYPE_RX);
1925                 ring->fw_ring_id = INVALID_HW_RING_ID;
1926                 memset(rxr->ag_buf_ring, 0,
1927                        rxr->ag_ring_struct->ring_size *
1928                        sizeof(*rxr->ag_buf_ring));
1929                 rxr->ag_prod = 0;
1930                 bp->grp_info[queue_index].ag_fw_ring_id = INVALID_HW_RING_ID;
1931         }
1932         if (cpr->cp_ring_struct->fw_ring_id != INVALID_HW_RING_ID)
1933                 bnxt_free_cp_ring(bp, cpr);
1934
1935         bp->grp_info[queue_index].cp_fw_ring_id = INVALID_HW_RING_ID;
1936 }
1937
1938 int bnxt_free_all_hwrm_rings(struct bnxt *bp)
1939 {
1940         unsigned int i;
1941
1942         for (i = 0; i < bp->tx_cp_nr_rings; i++) {
1943                 struct bnxt_tx_queue *txq = bp->tx_queues[i];
1944                 struct bnxt_tx_ring_info *txr = txq->tx_ring;
1945                 struct bnxt_ring *ring = txr->tx_ring_struct;
1946                 struct bnxt_cp_ring_info *cpr = txq->cp_ring;
1947
1948                 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
1949                         bnxt_hwrm_ring_free(bp, ring,
1950                                         HWRM_RING_FREE_INPUT_RING_TYPE_TX);
1951                         ring->fw_ring_id = INVALID_HW_RING_ID;
1952                         memset(txr->tx_desc_ring, 0,
1953                                         txr->tx_ring_struct->ring_size *
1954                                         sizeof(*txr->tx_desc_ring));
1955                         memset(txr->tx_buf_ring, 0,
1956                                         txr->tx_ring_struct->ring_size *
1957                                         sizeof(*txr->tx_buf_ring));
1958                         txr->tx_prod = 0;
1959                         txr->tx_cons = 0;
1960                 }
1961                 if (cpr->cp_ring_struct->fw_ring_id != INVALID_HW_RING_ID) {
1962                         bnxt_free_cp_ring(bp, cpr);
1963                         cpr->cp_ring_struct->fw_ring_id = INVALID_HW_RING_ID;
1964                 }
1965         }
1966
1967         for (i = 0; i < bp->rx_cp_nr_rings; i++)
1968                 bnxt_free_hwrm_rx_ring(bp, i);
1969
1970         return 0;
1971 }
1972
1973 int bnxt_alloc_all_hwrm_ring_grps(struct bnxt *bp)
1974 {
1975         uint16_t i;
1976         uint32_t rc = 0;
1977
1978         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
1979                 rc = bnxt_hwrm_ring_grp_alloc(bp, i);
1980                 if (rc)
1981                         return rc;
1982         }
1983         return rc;
1984 }
1985
1986 void bnxt_free_hwrm_resources(struct bnxt *bp)
1987 {
1988         /* Release memzone */
1989         rte_free(bp->hwrm_cmd_resp_addr);
1990         rte_free(bp->hwrm_short_cmd_req_addr);
1991         bp->hwrm_cmd_resp_addr = NULL;
1992         bp->hwrm_short_cmd_req_addr = NULL;
1993         bp->hwrm_cmd_resp_dma_addr = 0;
1994         bp->hwrm_short_cmd_req_dma_addr = 0;
1995 }
1996
1997 int bnxt_alloc_hwrm_resources(struct bnxt *bp)
1998 {
1999         struct rte_pci_device *pdev = bp->pdev;
2000         char type[RTE_MEMZONE_NAMESIZE];
2001
2002         sprintf(type, "bnxt_hwrm_%04x:%02x:%02x:%02x", pdev->addr.domain,
2003                 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
2004         bp->max_resp_len = HWRM_MAX_RESP_LEN;
2005         bp->hwrm_cmd_resp_addr = rte_malloc(type, bp->max_resp_len, 0);
2006         rte_mem_lock_page(bp->hwrm_cmd_resp_addr);
2007         if (bp->hwrm_cmd_resp_addr == NULL)
2008                 return -ENOMEM;
2009         bp->hwrm_cmd_resp_dma_addr =
2010                 rte_mem_virt2iova(bp->hwrm_cmd_resp_addr);
2011         if (bp->hwrm_cmd_resp_dma_addr == 0) {
2012                 PMD_DRV_LOG(ERR,
2013                         "unable to map response address to physical memory\n");
2014                 return -ENOMEM;
2015         }
2016         rte_spinlock_init(&bp->hwrm_lock);
2017
2018         return 0;
2019 }
2020
2021 int bnxt_clear_hwrm_vnic_filters(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2022 {
2023         struct bnxt_filter_info *filter;
2024         int rc = 0;
2025
2026         STAILQ_FOREACH(filter, &vnic->filter, next) {
2027                 if (filter->filter_type == HWRM_CFA_EM_FILTER)
2028                         rc = bnxt_hwrm_clear_em_filter(bp, filter);
2029                 else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER)
2030                         rc = bnxt_hwrm_clear_ntuple_filter(bp, filter);
2031                 else
2032                         rc = bnxt_hwrm_clear_l2_filter(bp, filter);
2033                 STAILQ_REMOVE(&vnic->filter, filter, bnxt_filter_info, next);
2034                 //if (rc)
2035                         //break;
2036         }
2037         return rc;
2038 }
2039
2040 static int
2041 bnxt_clear_hwrm_vnic_flows(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2042 {
2043         struct bnxt_filter_info *filter;
2044         struct rte_flow *flow;
2045         int rc = 0;
2046
2047         STAILQ_FOREACH(flow, &vnic->flow_list, next) {
2048                 filter = flow->filter;
2049                 PMD_DRV_LOG(ERR, "filter type %d\n", filter->filter_type);
2050                 if (filter->filter_type == HWRM_CFA_EM_FILTER)
2051                         rc = bnxt_hwrm_clear_em_filter(bp, filter);
2052                 else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER)
2053                         rc = bnxt_hwrm_clear_ntuple_filter(bp, filter);
2054                 else
2055                         rc = bnxt_hwrm_clear_l2_filter(bp, filter);
2056
2057                 STAILQ_REMOVE(&vnic->flow_list, flow, rte_flow, next);
2058                 rte_free(flow);
2059                 //if (rc)
2060                         //break;
2061         }
2062         return rc;
2063 }
2064
2065 int bnxt_set_hwrm_vnic_filters(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2066 {
2067         struct bnxt_filter_info *filter;
2068         int rc = 0;
2069
2070         STAILQ_FOREACH(filter, &vnic->filter, next) {
2071                 if (filter->filter_type == HWRM_CFA_EM_FILTER)
2072                         rc = bnxt_hwrm_set_em_filter(bp, filter->dst_id,
2073                                                      filter);
2074                 else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER)
2075                         rc = bnxt_hwrm_set_ntuple_filter(bp, filter->dst_id,
2076                                                          filter);
2077                 else
2078                         rc = bnxt_hwrm_set_l2_filter(bp, vnic->fw_vnic_id,
2079                                                      filter);
2080                 if (rc)
2081                         break;
2082         }
2083         return rc;
2084 }
2085
2086 void bnxt_free_tunnel_ports(struct bnxt *bp)
2087 {
2088         if (bp->vxlan_port_cnt)
2089                 bnxt_hwrm_tunnel_dst_port_free(bp, bp->vxlan_fw_dst_port_id,
2090                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_VXLAN);
2091         bp->vxlan_port = 0;
2092         if (bp->geneve_port_cnt)
2093                 bnxt_hwrm_tunnel_dst_port_free(bp, bp->geneve_fw_dst_port_id,
2094                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_GENEVE);
2095         bp->geneve_port = 0;
2096 }
2097
2098 void bnxt_free_all_hwrm_resources(struct bnxt *bp)
2099 {
2100         int i;
2101
2102         if (bp->vnic_info == NULL)
2103                 return;
2104
2105         /*
2106          * Cleanup VNICs in reverse order, to make sure the L2 filter
2107          * from vnic0 is last to be cleaned up.
2108          */
2109         for (i = bp->nr_vnics - 1; i >= 0; i--) {
2110                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2111
2112                 bnxt_clear_hwrm_vnic_flows(bp, vnic);
2113
2114                 bnxt_clear_hwrm_vnic_filters(bp, vnic);
2115
2116                 bnxt_hwrm_vnic_ctx_free(bp, vnic);
2117
2118                 bnxt_hwrm_vnic_tpa_cfg(bp, vnic, false);
2119
2120                 bnxt_hwrm_vnic_free(bp, vnic);
2121
2122                 rte_free(vnic->fw_grp_ids);
2123         }
2124         /* Ring resources */
2125         bnxt_free_all_hwrm_rings(bp);
2126         bnxt_free_all_hwrm_ring_grps(bp);
2127         bnxt_free_all_hwrm_stat_ctxs(bp);
2128         bnxt_free_tunnel_ports(bp);
2129 }
2130
2131 static uint16_t bnxt_parse_eth_link_duplex(uint32_t conf_link_speed)
2132 {
2133         uint8_t hw_link_duplex = HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH;
2134
2135         if ((conf_link_speed & ETH_LINK_SPEED_FIXED) == ETH_LINK_SPEED_AUTONEG)
2136                 return HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH;
2137
2138         switch (conf_link_speed) {
2139         case ETH_LINK_SPEED_10M_HD:
2140         case ETH_LINK_SPEED_100M_HD:
2141                 /* FALLTHROUGH */
2142                 return HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_HALF;
2143         }
2144         return hw_link_duplex;
2145 }
2146
2147 static uint16_t bnxt_check_eth_link_autoneg(uint32_t conf_link)
2148 {
2149         return (conf_link & ETH_LINK_SPEED_FIXED) ? 0 : 1;
2150 }
2151
2152 static uint16_t bnxt_parse_eth_link_speed(uint32_t conf_link_speed)
2153 {
2154         uint16_t eth_link_speed = 0;
2155
2156         if (conf_link_speed == ETH_LINK_SPEED_AUTONEG)
2157                 return ETH_LINK_SPEED_AUTONEG;
2158
2159         switch (conf_link_speed & ~ETH_LINK_SPEED_FIXED) {
2160         case ETH_LINK_SPEED_100M:
2161         case ETH_LINK_SPEED_100M_HD:
2162                 /* FALLTHROUGH */
2163                 eth_link_speed =
2164                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_100MB;
2165                 break;
2166         case ETH_LINK_SPEED_1G:
2167                 eth_link_speed =
2168                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_1GB;
2169                 break;
2170         case ETH_LINK_SPEED_2_5G:
2171                 eth_link_speed =
2172                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_2_5GB;
2173                 break;
2174         case ETH_LINK_SPEED_10G:
2175                 eth_link_speed =
2176                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_10GB;
2177                 break;
2178         case ETH_LINK_SPEED_20G:
2179                 eth_link_speed =
2180                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_20GB;
2181                 break;
2182         case ETH_LINK_SPEED_25G:
2183                 eth_link_speed =
2184                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_25GB;
2185                 break;
2186         case ETH_LINK_SPEED_40G:
2187                 eth_link_speed =
2188                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_40GB;
2189                 break;
2190         case ETH_LINK_SPEED_50G:
2191                 eth_link_speed =
2192                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_50GB;
2193                 break;
2194         case ETH_LINK_SPEED_100G:
2195                 eth_link_speed =
2196                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_100GB;
2197                 break;
2198         default:
2199                 PMD_DRV_LOG(ERR,
2200                         "Unsupported link speed %d; default to AUTO\n",
2201                         conf_link_speed);
2202                 break;
2203         }
2204         return eth_link_speed;
2205 }
2206
2207 #define BNXT_SUPPORTED_SPEEDS (ETH_LINK_SPEED_100M | ETH_LINK_SPEED_100M_HD | \
2208                 ETH_LINK_SPEED_1G | ETH_LINK_SPEED_2_5G | \
2209                 ETH_LINK_SPEED_10G | ETH_LINK_SPEED_20G | ETH_LINK_SPEED_25G | \
2210                 ETH_LINK_SPEED_40G | ETH_LINK_SPEED_50G | ETH_LINK_SPEED_100G)
2211
2212 static int bnxt_valid_link_speed(uint32_t link_speed, uint16_t port_id)
2213 {
2214         uint32_t one_speed;
2215
2216         if (link_speed == ETH_LINK_SPEED_AUTONEG)
2217                 return 0;
2218
2219         if (link_speed & ETH_LINK_SPEED_FIXED) {
2220                 one_speed = link_speed & ~ETH_LINK_SPEED_FIXED;
2221
2222                 if (one_speed & (one_speed - 1)) {
2223                         PMD_DRV_LOG(ERR,
2224                                 "Invalid advertised speeds (%u) for port %u\n",
2225                                 link_speed, port_id);
2226                         return -EINVAL;
2227                 }
2228                 if ((one_speed & BNXT_SUPPORTED_SPEEDS) != one_speed) {
2229                         PMD_DRV_LOG(ERR,
2230                                 "Unsupported advertised speed (%u) for port %u\n",
2231                                 link_speed, port_id);
2232                         return -EINVAL;
2233                 }
2234         } else {
2235                 if (!(link_speed & BNXT_SUPPORTED_SPEEDS)) {
2236                         PMD_DRV_LOG(ERR,
2237                                 "Unsupported advertised speeds (%u) for port %u\n",
2238                                 link_speed, port_id);
2239                         return -EINVAL;
2240                 }
2241         }
2242         return 0;
2243 }
2244
2245 static uint16_t
2246 bnxt_parse_eth_link_speed_mask(struct bnxt *bp, uint32_t link_speed)
2247 {
2248         uint16_t ret = 0;
2249
2250         if (link_speed == ETH_LINK_SPEED_AUTONEG) {
2251                 if (bp->link_info.support_speeds)
2252                         return bp->link_info.support_speeds;
2253                 link_speed = BNXT_SUPPORTED_SPEEDS;
2254         }
2255
2256         if (link_speed & ETH_LINK_SPEED_100M)
2257                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100MB;
2258         if (link_speed & ETH_LINK_SPEED_100M_HD)
2259                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100MB;
2260         if (link_speed & ETH_LINK_SPEED_1G)
2261                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_1GB;
2262         if (link_speed & ETH_LINK_SPEED_2_5G)
2263                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_2_5GB;
2264         if (link_speed & ETH_LINK_SPEED_10G)
2265                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_10GB;
2266         if (link_speed & ETH_LINK_SPEED_20G)
2267                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_20GB;
2268         if (link_speed & ETH_LINK_SPEED_25G)
2269                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_25GB;
2270         if (link_speed & ETH_LINK_SPEED_40G)
2271                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_40GB;
2272         if (link_speed & ETH_LINK_SPEED_50G)
2273                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_50GB;
2274         if (link_speed & ETH_LINK_SPEED_100G)
2275                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100GB;
2276         return ret;
2277 }
2278
2279 static uint32_t bnxt_parse_hw_link_speed(uint16_t hw_link_speed)
2280 {
2281         uint32_t eth_link_speed = ETH_SPEED_NUM_NONE;
2282
2283         switch (hw_link_speed) {
2284         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100MB:
2285                 eth_link_speed = ETH_SPEED_NUM_100M;
2286                 break;
2287         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_1GB:
2288                 eth_link_speed = ETH_SPEED_NUM_1G;
2289                 break;
2290         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2_5GB:
2291                 eth_link_speed = ETH_SPEED_NUM_2_5G;
2292                 break;
2293         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_10GB:
2294                 eth_link_speed = ETH_SPEED_NUM_10G;
2295                 break;
2296         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_20GB:
2297                 eth_link_speed = ETH_SPEED_NUM_20G;
2298                 break;
2299         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_25GB:
2300                 eth_link_speed = ETH_SPEED_NUM_25G;
2301                 break;
2302         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_40GB:
2303                 eth_link_speed = ETH_SPEED_NUM_40G;
2304                 break;
2305         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_50GB:
2306                 eth_link_speed = ETH_SPEED_NUM_50G;
2307                 break;
2308         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100GB:
2309                 eth_link_speed = ETH_SPEED_NUM_100G;
2310                 break;
2311         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2GB:
2312         default:
2313                 PMD_DRV_LOG(ERR, "HWRM link speed %d not defined\n",
2314                         hw_link_speed);
2315                 break;
2316         }
2317         return eth_link_speed;
2318 }
2319
2320 static uint16_t bnxt_parse_hw_link_duplex(uint16_t hw_link_duplex)
2321 {
2322         uint16_t eth_link_duplex = ETH_LINK_FULL_DUPLEX;
2323
2324         switch (hw_link_duplex) {
2325         case HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH:
2326         case HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_FULL:
2327                 /* FALLTHROUGH */
2328                 eth_link_duplex = ETH_LINK_FULL_DUPLEX;
2329                 break;
2330         case HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_HALF:
2331                 eth_link_duplex = ETH_LINK_HALF_DUPLEX;
2332                 break;
2333         default:
2334                 PMD_DRV_LOG(ERR, "HWRM link duplex %d not defined\n",
2335                         hw_link_duplex);
2336                 break;
2337         }
2338         return eth_link_duplex;
2339 }
2340
2341 int bnxt_get_hwrm_link_config(struct bnxt *bp, struct rte_eth_link *link)
2342 {
2343         int rc = 0;
2344         struct bnxt_link_info *link_info = &bp->link_info;
2345
2346         rc = bnxt_hwrm_port_phy_qcfg(bp, link_info);
2347         if (rc) {
2348                 PMD_DRV_LOG(ERR,
2349                         "Get link config failed with rc %d\n", rc);
2350                 goto exit;
2351         }
2352         if (link_info->link_speed)
2353                 link->link_speed =
2354                         bnxt_parse_hw_link_speed(link_info->link_speed);
2355         else
2356                 link->link_speed = ETH_SPEED_NUM_NONE;
2357         link->link_duplex = bnxt_parse_hw_link_duplex(link_info->duplex);
2358         link->link_status = link_info->link_up;
2359         link->link_autoneg = link_info->auto_mode ==
2360                 HWRM_PORT_PHY_QCFG_OUTPUT_AUTO_MODE_NONE ?
2361                 ETH_LINK_FIXED : ETH_LINK_AUTONEG;
2362 exit:
2363         return rc;
2364 }
2365
2366 int bnxt_set_hwrm_link_config(struct bnxt *bp, bool link_up)
2367 {
2368         int rc = 0;
2369         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
2370         struct bnxt_link_info link_req;
2371         uint16_t speed, autoneg;
2372
2373         if (!BNXT_SINGLE_PF(bp) || BNXT_VF(bp))
2374                 return 0;
2375
2376         rc = bnxt_valid_link_speed(dev_conf->link_speeds,
2377                         bp->eth_dev->data->port_id);
2378         if (rc)
2379                 goto error;
2380
2381         memset(&link_req, 0, sizeof(link_req));
2382         link_req.link_up = link_up;
2383         if (!link_up)
2384                 goto port_phy_cfg;
2385
2386         autoneg = bnxt_check_eth_link_autoneg(dev_conf->link_speeds);
2387         speed = bnxt_parse_eth_link_speed(dev_conf->link_speeds);
2388         link_req.phy_flags = HWRM_PORT_PHY_CFG_INPUT_FLAGS_RESET_PHY;
2389         /* Autoneg can be done only when the FW allows */
2390         if (autoneg == 1 && !(bp->link_info.auto_link_speed ||
2391                                 bp->link_info.force_link_speed)) {
2392                 link_req.phy_flags |=
2393                                 HWRM_PORT_PHY_CFG_INPUT_FLAGS_RESTART_AUTONEG;
2394                 link_req.auto_link_speed_mask =
2395                         bnxt_parse_eth_link_speed_mask(bp,
2396                                                        dev_conf->link_speeds);
2397         } else {
2398                 if (bp->link_info.phy_type ==
2399                     HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET ||
2400                     bp->link_info.phy_type ==
2401                     HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASETE ||
2402                     bp->link_info.media_type ==
2403                     HWRM_PORT_PHY_QCFG_OUTPUT_MEDIA_TYPE_TP) {
2404                         PMD_DRV_LOG(ERR, "10GBase-T devices must autoneg\n");
2405                         return -EINVAL;
2406                 }
2407
2408                 link_req.phy_flags |= HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE;
2409                 /* If user wants a particular speed try that first. */
2410                 if (speed)
2411                         link_req.link_speed = speed;
2412                 else if (bp->link_info.force_link_speed)
2413                         link_req.link_speed = bp->link_info.force_link_speed;
2414                 else
2415                         link_req.link_speed = bp->link_info.auto_link_speed;
2416         }
2417         link_req.duplex = bnxt_parse_eth_link_duplex(dev_conf->link_speeds);
2418         link_req.auto_pause = bp->link_info.auto_pause;
2419         link_req.force_pause = bp->link_info.force_pause;
2420
2421 port_phy_cfg:
2422         rc = bnxt_hwrm_port_phy_cfg(bp, &link_req);
2423         if (rc) {
2424                 PMD_DRV_LOG(ERR,
2425                         "Set link config failed with rc %d\n", rc);
2426         }
2427
2428 error:
2429         return rc;
2430 }
2431
2432 /* JIRA 22088 */
2433 int bnxt_hwrm_func_qcfg(struct bnxt *bp)
2434 {
2435         struct hwrm_func_qcfg_input req = {0};
2436         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
2437         uint16_t flags;
2438         int rc = 0;
2439
2440         HWRM_PREP(req, FUNC_QCFG, BNXT_USE_CHIMP_MB);
2441         req.fid = rte_cpu_to_le_16(0xffff);
2442
2443         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2444
2445         HWRM_CHECK_RESULT();
2446
2447         /* Hard Coded.. 0xfff VLAN ID mask */
2448         bp->vlan = rte_le_to_cpu_16(resp->vlan) & 0xfff;
2449         flags = rte_le_to_cpu_16(resp->flags);
2450         if (BNXT_PF(bp) && (flags & HWRM_FUNC_QCFG_OUTPUT_FLAGS_MULTI_HOST))
2451                 bp->flags |= BNXT_FLAG_MULTI_HOST;
2452
2453         if (BNXT_VF(bp) && (flags & HWRM_FUNC_QCFG_OUTPUT_FLAGS_TRUSTED_VF)) {
2454                 bp->flags |= BNXT_FLAG_TRUSTED_VF_EN;
2455                 PMD_DRV_LOG(INFO, "Trusted VF cap enabled\n");
2456         }
2457
2458         switch (resp->port_partition_type) {
2459         case HWRM_FUNC_QCFG_OUTPUT_PORT_PARTITION_TYPE_NPAR1_0:
2460         case HWRM_FUNC_QCFG_OUTPUT_PORT_PARTITION_TYPE_NPAR1_5:
2461         case HWRM_FUNC_QCFG_OUTPUT_PORT_PARTITION_TYPE_NPAR2_0:
2462                 /* FALLTHROUGH */
2463                 bp->port_partition_type = resp->port_partition_type;
2464                 break;
2465         default:
2466                 bp->port_partition_type = 0;
2467                 break;
2468         }
2469
2470         HWRM_UNLOCK();
2471
2472         return rc;
2473 }
2474
2475 static void copy_func_cfg_to_qcaps(struct hwrm_func_cfg_input *fcfg,
2476                                    struct hwrm_func_qcaps_output *qcaps)
2477 {
2478         qcaps->max_rsscos_ctx = fcfg->num_rsscos_ctxs;
2479         memcpy(qcaps->mac_address, fcfg->dflt_mac_addr,
2480                sizeof(qcaps->mac_address));
2481         qcaps->max_l2_ctxs = fcfg->num_l2_ctxs;
2482         qcaps->max_rx_rings = fcfg->num_rx_rings;
2483         qcaps->max_tx_rings = fcfg->num_tx_rings;
2484         qcaps->max_cmpl_rings = fcfg->num_cmpl_rings;
2485         qcaps->max_stat_ctx = fcfg->num_stat_ctxs;
2486         qcaps->max_vfs = 0;
2487         qcaps->first_vf_id = 0;
2488         qcaps->max_vnics = fcfg->num_vnics;
2489         qcaps->max_decap_records = 0;
2490         qcaps->max_encap_records = 0;
2491         qcaps->max_tx_wm_flows = 0;
2492         qcaps->max_tx_em_flows = 0;
2493         qcaps->max_rx_wm_flows = 0;
2494         qcaps->max_rx_em_flows = 0;
2495         qcaps->max_flow_id = 0;
2496         qcaps->max_mcast_filters = fcfg->num_mcast_filters;
2497         qcaps->max_sp_tx_rings = 0;
2498         qcaps->max_hw_ring_grps = fcfg->num_hw_ring_grps;
2499 }
2500
2501 static int bnxt_hwrm_pf_func_cfg(struct bnxt *bp, int tx_rings)
2502 {
2503         struct hwrm_func_cfg_input req = {0};
2504         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2505         int rc;
2506
2507         req.enables = rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_MTU |
2508                         HWRM_FUNC_CFG_INPUT_ENABLES_MRU |
2509                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RSSCOS_CTXS |
2510                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_STAT_CTXS |
2511                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_CMPL_RINGS |
2512                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_TX_RINGS |
2513                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RX_RINGS |
2514                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_L2_CTXS |
2515                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_VNICS |
2516                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_HW_RING_GRPS);
2517         req.flags = rte_cpu_to_le_32(bp->pf.func_cfg_flags);
2518         req.mtu = rte_cpu_to_le_16(BNXT_MAX_MTU);
2519         req.mru = rte_cpu_to_le_16(bp->eth_dev->data->mtu + RTE_ETHER_HDR_LEN +
2520                                    RTE_ETHER_CRC_LEN + VLAN_TAG_SIZE *
2521                                    BNXT_NUM_VLANS);
2522         req.num_rsscos_ctxs = rte_cpu_to_le_16(bp->max_rsscos_ctx);
2523         req.num_stat_ctxs = rte_cpu_to_le_16(bp->max_stat_ctx);
2524         req.num_cmpl_rings = rte_cpu_to_le_16(bp->max_cp_rings);
2525         req.num_tx_rings = rte_cpu_to_le_16(tx_rings);
2526         req.num_rx_rings = rte_cpu_to_le_16(bp->max_rx_rings);
2527         req.num_l2_ctxs = rte_cpu_to_le_16(bp->max_l2_ctx);
2528         req.num_vnics = rte_cpu_to_le_16(bp->max_vnics);
2529         req.num_hw_ring_grps = rte_cpu_to_le_16(bp->max_ring_grps);
2530         req.fid = rte_cpu_to_le_16(0xffff);
2531
2532         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
2533
2534         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2535
2536         HWRM_CHECK_RESULT();
2537         HWRM_UNLOCK();
2538
2539         return rc;
2540 }
2541
2542 static void populate_vf_func_cfg_req(struct bnxt *bp,
2543                                      struct hwrm_func_cfg_input *req,
2544                                      int num_vfs)
2545 {
2546         req->enables = rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_MTU |
2547                         HWRM_FUNC_CFG_INPUT_ENABLES_MRU |
2548                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RSSCOS_CTXS |
2549                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_STAT_CTXS |
2550                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_CMPL_RINGS |
2551                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_TX_RINGS |
2552                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RX_RINGS |
2553                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_L2_CTXS |
2554                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_VNICS |
2555                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_HW_RING_GRPS);
2556
2557         req->mtu = rte_cpu_to_le_16(bp->eth_dev->data->mtu + RTE_ETHER_HDR_LEN +
2558                                     RTE_ETHER_CRC_LEN + VLAN_TAG_SIZE *
2559                                     BNXT_NUM_VLANS);
2560         req->mru = rte_cpu_to_le_16(bp->eth_dev->data->mtu + RTE_ETHER_HDR_LEN +
2561                                     RTE_ETHER_CRC_LEN + VLAN_TAG_SIZE *
2562                                     BNXT_NUM_VLANS);
2563         req->num_rsscos_ctxs = rte_cpu_to_le_16(bp->max_rsscos_ctx /
2564                                                 (num_vfs + 1));
2565         req->num_stat_ctxs = rte_cpu_to_le_16(bp->max_stat_ctx / (num_vfs + 1));
2566         req->num_cmpl_rings = rte_cpu_to_le_16(bp->max_cp_rings /
2567                                                (num_vfs + 1));
2568         req->num_tx_rings = rte_cpu_to_le_16(bp->max_tx_rings / (num_vfs + 1));
2569         req->num_rx_rings = rte_cpu_to_le_16(bp->max_rx_rings / (num_vfs + 1));
2570         req->num_l2_ctxs = rte_cpu_to_le_16(bp->max_l2_ctx / (num_vfs + 1));
2571         /* TODO: For now, do not support VMDq/RFS on VFs. */
2572         req->num_vnics = rte_cpu_to_le_16(1);
2573         req->num_hw_ring_grps = rte_cpu_to_le_16(bp->max_ring_grps /
2574                                                  (num_vfs + 1));
2575 }
2576
2577 static void add_random_mac_if_needed(struct bnxt *bp,
2578                                      struct hwrm_func_cfg_input *cfg_req,
2579                                      int vf)
2580 {
2581         struct rte_ether_addr mac;
2582
2583         if (bnxt_hwrm_func_qcfg_vf_default_mac(bp, vf, &mac))
2584                 return;
2585
2586         if (memcmp(mac.addr_bytes, "\x00\x00\x00\x00\x00", 6) == 0) {
2587                 cfg_req->enables |=
2588                 rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
2589                 rte_eth_random_addr(cfg_req->dflt_mac_addr);
2590                 bp->pf.vf_info[vf].random_mac = true;
2591         } else {
2592                 memcpy(cfg_req->dflt_mac_addr, mac.addr_bytes,
2593                         RTE_ETHER_ADDR_LEN);
2594         }
2595 }
2596
2597 static void reserve_resources_from_vf(struct bnxt *bp,
2598                                       struct hwrm_func_cfg_input *cfg_req,
2599                                       int vf)
2600 {
2601         struct hwrm_func_qcaps_input req = {0};
2602         struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
2603         int rc;
2604
2605         /* Get the actual allocated values now */
2606         HWRM_PREP(req, FUNC_QCAPS, BNXT_USE_CHIMP_MB);
2607         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
2608         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2609
2610         if (rc) {
2611                 PMD_DRV_LOG(ERR, "hwrm_func_qcaps failed rc:%d\n", rc);
2612                 copy_func_cfg_to_qcaps(cfg_req, resp);
2613         } else if (resp->error_code) {
2614                 rc = rte_le_to_cpu_16(resp->error_code);
2615                 PMD_DRV_LOG(ERR, "hwrm_func_qcaps error %d\n", rc);
2616                 copy_func_cfg_to_qcaps(cfg_req, resp);
2617         }
2618
2619         bp->max_rsscos_ctx -= rte_le_to_cpu_16(resp->max_rsscos_ctx);
2620         bp->max_stat_ctx -= rte_le_to_cpu_16(resp->max_stat_ctx);
2621         bp->max_cp_rings -= rte_le_to_cpu_16(resp->max_cmpl_rings);
2622         bp->max_tx_rings -= rte_le_to_cpu_16(resp->max_tx_rings);
2623         bp->max_rx_rings -= rte_le_to_cpu_16(resp->max_rx_rings);
2624         bp->max_l2_ctx -= rte_le_to_cpu_16(resp->max_l2_ctxs);
2625         /*
2626          * TODO: While not supporting VMDq with VFs, max_vnics is always
2627          * forced to 1 in this case
2628          */
2629         //bp->max_vnics -= rte_le_to_cpu_16(esp->max_vnics);
2630         bp->max_ring_grps -= rte_le_to_cpu_16(resp->max_hw_ring_grps);
2631
2632         HWRM_UNLOCK();
2633 }
2634
2635 int bnxt_hwrm_func_qcfg_current_vf_vlan(struct bnxt *bp, int vf)
2636 {
2637         struct hwrm_func_qcfg_input req = {0};
2638         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
2639         int rc;
2640
2641         /* Check for zero MAC address */
2642         HWRM_PREP(req, FUNC_QCFG, BNXT_USE_CHIMP_MB);
2643         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
2644         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2645         if (rc) {
2646                 PMD_DRV_LOG(ERR, "hwrm_func_qcfg failed rc:%d\n", rc);
2647                 return -1;
2648         } else if (resp->error_code) {
2649                 rc = rte_le_to_cpu_16(resp->error_code);
2650                 PMD_DRV_LOG(ERR, "hwrm_func_qcfg error %d\n", rc);
2651                 return -1;
2652         }
2653         rc = rte_le_to_cpu_16(resp->vlan);
2654
2655         HWRM_UNLOCK();
2656
2657         return rc;
2658 }
2659
2660 static int update_pf_resource_max(struct bnxt *bp)
2661 {
2662         struct hwrm_func_qcfg_input req = {0};
2663         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
2664         int rc;
2665
2666         /* And copy the allocated numbers into the pf struct */
2667         HWRM_PREP(req, FUNC_QCFG, BNXT_USE_CHIMP_MB);
2668         req.fid = rte_cpu_to_le_16(0xffff);
2669         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2670         HWRM_CHECK_RESULT();
2671
2672         /* Only TX ring value reflects actual allocation? TODO */
2673         bp->max_tx_rings = rte_le_to_cpu_16(resp->alloc_tx_rings);
2674         bp->pf.evb_mode = resp->evb_mode;
2675
2676         HWRM_UNLOCK();
2677
2678         return rc;
2679 }
2680
2681 int bnxt_hwrm_allocate_pf_only(struct bnxt *bp)
2682 {
2683         int rc;
2684
2685         if (!BNXT_PF(bp)) {
2686                 PMD_DRV_LOG(ERR, "Attempt to allcoate VFs on a VF!\n");
2687                 return -1;
2688         }
2689
2690         rc = bnxt_hwrm_func_qcaps(bp);
2691         if (rc)
2692                 return rc;
2693
2694         bp->pf.func_cfg_flags &=
2695                 ~(HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_ENABLE |
2696                   HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_DISABLE);
2697         bp->pf.func_cfg_flags |=
2698                 HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_DISABLE;
2699         rc = bnxt_hwrm_pf_func_cfg(bp, bp->max_tx_rings);
2700         return rc;
2701 }
2702
2703 int bnxt_hwrm_allocate_vfs(struct bnxt *bp, int num_vfs)
2704 {
2705         struct hwrm_func_cfg_input req = {0};
2706         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2707         int i;
2708         size_t sz;
2709         int rc = 0;
2710         size_t req_buf_sz;
2711
2712         if (!BNXT_PF(bp)) {
2713                 PMD_DRV_LOG(ERR, "Attempt to allcoate VFs on a VF!\n");
2714                 return -1;
2715         }
2716
2717         rc = bnxt_hwrm_func_qcaps(bp);
2718
2719         if (rc)
2720                 return rc;
2721
2722         bp->pf.active_vfs = num_vfs;
2723
2724         /*
2725          * First, configure the PF to only use one TX ring.  This ensures that
2726          * there are enough rings for all VFs.
2727          *
2728          * If we don't do this, when we call func_alloc() later, we will lock
2729          * extra rings to the PF that won't be available during func_cfg() of
2730          * the VFs.
2731          *
2732          * This has been fixed with firmware versions above 20.6.54
2733          */
2734         bp->pf.func_cfg_flags &=
2735                 ~(HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_ENABLE |
2736                   HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_DISABLE);
2737         bp->pf.func_cfg_flags |=
2738                 HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_ENABLE;
2739         rc = bnxt_hwrm_pf_func_cfg(bp, 1);
2740         if (rc)
2741                 return rc;
2742
2743         /*
2744          * Now, create and register a buffer to hold forwarded VF requests
2745          */
2746         req_buf_sz = num_vfs * HWRM_MAX_REQ_LEN;
2747         bp->pf.vf_req_buf = rte_malloc("bnxt_vf_fwd", req_buf_sz,
2748                 page_roundup(num_vfs * HWRM_MAX_REQ_LEN));
2749         if (bp->pf.vf_req_buf == NULL) {
2750                 rc = -ENOMEM;
2751                 goto error_free;
2752         }
2753         for (sz = 0; sz < req_buf_sz; sz += getpagesize())
2754                 rte_mem_lock_page(((char *)bp->pf.vf_req_buf) + sz);
2755         for (i = 0; i < num_vfs; i++)
2756                 bp->pf.vf_info[i].req_buf = ((char *)bp->pf.vf_req_buf) +
2757                                         (i * HWRM_MAX_REQ_LEN);
2758
2759         rc = bnxt_hwrm_func_buf_rgtr(bp);
2760         if (rc)
2761                 goto error_free;
2762
2763         populate_vf_func_cfg_req(bp, &req, num_vfs);
2764
2765         bp->pf.active_vfs = 0;
2766         for (i = 0; i < num_vfs; i++) {
2767                 add_random_mac_if_needed(bp, &req, i);
2768
2769                 HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
2770                 req.flags = rte_cpu_to_le_32(bp->pf.vf_info[i].func_cfg_flags);
2771                 req.fid = rte_cpu_to_le_16(bp->pf.vf_info[i].fid);
2772                 rc = bnxt_hwrm_send_message(bp,
2773                                             &req,
2774                                             sizeof(req),
2775                                             BNXT_USE_CHIMP_MB);
2776
2777                 /* Clear enable flag for next pass */
2778                 req.enables &= ~rte_cpu_to_le_32(
2779                                 HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
2780
2781                 if (rc || resp->error_code) {
2782                         PMD_DRV_LOG(ERR,
2783                                 "Failed to initizlie VF %d\n", i);
2784                         PMD_DRV_LOG(ERR,
2785                                 "Not all VFs available. (%d, %d)\n",
2786                                 rc, resp->error_code);
2787                         HWRM_UNLOCK();
2788                         break;
2789                 }
2790
2791                 HWRM_UNLOCK();
2792
2793                 reserve_resources_from_vf(bp, &req, i);
2794                 bp->pf.active_vfs++;
2795                 bnxt_hwrm_func_clr_stats(bp, bp->pf.vf_info[i].fid);
2796         }
2797
2798         /*
2799          * Now configure the PF to use "the rest" of the resources
2800          * We're using STD_TX_RING_MODE here though which will limit the TX
2801          * rings.  This will allow QoS to function properly.  Not setting this
2802          * will cause PF rings to break bandwidth settings.
2803          */
2804         rc = bnxt_hwrm_pf_func_cfg(bp, bp->max_tx_rings);
2805         if (rc)
2806                 goto error_free;
2807
2808         rc = update_pf_resource_max(bp);
2809         if (rc)
2810                 goto error_free;
2811
2812         return rc;
2813
2814 error_free:
2815         bnxt_hwrm_func_buf_unrgtr(bp);
2816         return rc;
2817 }
2818
2819 int bnxt_hwrm_pf_evb_mode(struct bnxt *bp)
2820 {
2821         struct hwrm_func_cfg_input req = {0};
2822         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2823         int rc;
2824
2825         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
2826
2827         req.fid = rte_cpu_to_le_16(0xffff);
2828         req.enables = rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_EVB_MODE);
2829         req.evb_mode = bp->pf.evb_mode;
2830
2831         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2832         HWRM_CHECK_RESULT();
2833         HWRM_UNLOCK();
2834
2835         return rc;
2836 }
2837
2838 int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, uint16_t port,
2839                                 uint8_t tunnel_type)
2840 {
2841         struct hwrm_tunnel_dst_port_alloc_input req = {0};
2842         struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
2843         int rc = 0;
2844
2845         HWRM_PREP(req, TUNNEL_DST_PORT_ALLOC, BNXT_USE_CHIMP_MB);
2846         req.tunnel_type = tunnel_type;
2847         req.tunnel_dst_port_val = port;
2848         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2849         HWRM_CHECK_RESULT();
2850
2851         switch (tunnel_type) {
2852         case HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_VXLAN:
2853                 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
2854                 bp->vxlan_port = port;
2855                 break;
2856         case HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_GENEVE:
2857                 bp->geneve_fw_dst_port_id = resp->tunnel_dst_port_id;
2858                 bp->geneve_port = port;
2859                 break;
2860         default:
2861                 break;
2862         }
2863
2864         HWRM_UNLOCK();
2865
2866         return rc;
2867 }
2868
2869 int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, uint16_t port,
2870                                 uint8_t tunnel_type)
2871 {
2872         struct hwrm_tunnel_dst_port_free_input req = {0};
2873         struct hwrm_tunnel_dst_port_free_output *resp = bp->hwrm_cmd_resp_addr;
2874         int rc = 0;
2875
2876         HWRM_PREP(req, TUNNEL_DST_PORT_FREE, BNXT_USE_CHIMP_MB);
2877
2878         req.tunnel_type = tunnel_type;
2879         req.tunnel_dst_port_id = rte_cpu_to_be_16(port);
2880         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2881
2882         HWRM_CHECK_RESULT();
2883         HWRM_UNLOCK();
2884
2885         return rc;
2886 }
2887
2888 int bnxt_hwrm_func_cfg_vf_set_flags(struct bnxt *bp, uint16_t vf,
2889                                         uint32_t flags)
2890 {
2891         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2892         struct hwrm_func_cfg_input req = {0};
2893         int rc;
2894
2895         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
2896
2897         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
2898         req.flags = rte_cpu_to_le_32(flags);
2899         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2900
2901         HWRM_CHECK_RESULT();
2902         HWRM_UNLOCK();
2903
2904         return rc;
2905 }
2906
2907 void vf_vnic_set_rxmask_cb(struct bnxt_vnic_info *vnic, void *flagp)
2908 {
2909         uint32_t *flag = flagp;
2910
2911         vnic->flags = *flag;
2912 }
2913
2914 int bnxt_set_rx_mask_no_vlan(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2915 {
2916         return bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
2917 }
2918
2919 int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
2920 {
2921         int rc = 0;
2922         struct hwrm_func_buf_rgtr_input req = {.req_type = 0 };
2923         struct hwrm_func_buf_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
2924
2925         HWRM_PREP(req, FUNC_BUF_RGTR, BNXT_USE_CHIMP_MB);
2926
2927         req.req_buf_num_pages = rte_cpu_to_le_16(1);
2928         req.req_buf_page_size = rte_cpu_to_le_16(
2929                          page_getenum(bp->pf.active_vfs * HWRM_MAX_REQ_LEN));
2930         req.req_buf_len = rte_cpu_to_le_16(HWRM_MAX_REQ_LEN);
2931         req.req_buf_page_addr0 =
2932                 rte_cpu_to_le_64(rte_mem_virt2iova(bp->pf.vf_req_buf));
2933         if (req.req_buf_page_addr0 == 0) {
2934                 PMD_DRV_LOG(ERR,
2935                         "unable to map buffer address to physical memory\n");
2936                 return -ENOMEM;
2937         }
2938
2939         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2940
2941         HWRM_CHECK_RESULT();
2942         HWRM_UNLOCK();
2943
2944         return rc;
2945 }
2946
2947 int bnxt_hwrm_func_buf_unrgtr(struct bnxt *bp)
2948 {
2949         int rc = 0;
2950         struct hwrm_func_buf_unrgtr_input req = {.req_type = 0 };
2951         struct hwrm_func_buf_unrgtr_output *resp = bp->hwrm_cmd_resp_addr;
2952
2953         HWRM_PREP(req, FUNC_BUF_UNRGTR, BNXT_USE_CHIMP_MB);
2954
2955         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2956
2957         HWRM_CHECK_RESULT();
2958         HWRM_UNLOCK();
2959
2960         return rc;
2961 }
2962
2963 int bnxt_hwrm_func_cfg_def_cp(struct bnxt *bp)
2964 {
2965         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2966         struct hwrm_func_cfg_input req = {0};
2967         int rc;
2968
2969         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
2970
2971         req.fid = rte_cpu_to_le_16(0xffff);
2972         req.flags = rte_cpu_to_le_32(bp->pf.func_cfg_flags);
2973         req.enables = rte_cpu_to_le_32(
2974                         HWRM_FUNC_CFG_INPUT_ENABLES_ASYNC_EVENT_CR);
2975         req.async_event_cr = rte_cpu_to_le_16(
2976                         bp->def_cp_ring->cp_ring_struct->fw_ring_id);
2977         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2978
2979         HWRM_CHECK_RESULT();
2980         HWRM_UNLOCK();
2981
2982         return rc;
2983 }
2984
2985 int bnxt_hwrm_vf_func_cfg_def_cp(struct bnxt *bp)
2986 {
2987         struct hwrm_func_vf_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2988         struct hwrm_func_vf_cfg_input req = {0};
2989         int rc;
2990
2991         HWRM_PREP(req, FUNC_VF_CFG, BNXT_USE_CHIMP_MB);
2992
2993         req.enables = rte_cpu_to_le_32(
2994                         HWRM_FUNC_VF_CFG_INPUT_ENABLES_ASYNC_EVENT_CR);
2995         req.async_event_cr = rte_cpu_to_le_16(
2996                         bp->def_cp_ring->cp_ring_struct->fw_ring_id);
2997         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2998
2999         HWRM_CHECK_RESULT();
3000         HWRM_UNLOCK();
3001
3002         return rc;
3003 }
3004
3005 int bnxt_hwrm_set_default_vlan(struct bnxt *bp, int vf, uint8_t is_vf)
3006 {
3007         struct hwrm_func_cfg_input req = {0};
3008         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3009         uint16_t dflt_vlan, fid;
3010         uint32_t func_cfg_flags;
3011         int rc = 0;
3012
3013         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
3014
3015         if (is_vf) {
3016                 dflt_vlan = bp->pf.vf_info[vf].dflt_vlan;
3017                 fid = bp->pf.vf_info[vf].fid;
3018                 func_cfg_flags = bp->pf.vf_info[vf].func_cfg_flags;
3019         } else {
3020                 fid = rte_cpu_to_le_16(0xffff);
3021                 func_cfg_flags = bp->pf.func_cfg_flags;
3022                 dflt_vlan = bp->vlan;
3023         }
3024
3025         req.flags = rte_cpu_to_le_32(func_cfg_flags);
3026         req.fid = rte_cpu_to_le_16(fid);
3027         req.enables |= rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_VLAN);
3028         req.dflt_vlan = rte_cpu_to_le_16(dflt_vlan);
3029
3030         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3031
3032         HWRM_CHECK_RESULT();
3033         HWRM_UNLOCK();
3034
3035         return rc;
3036 }
3037
3038 int bnxt_hwrm_func_bw_cfg(struct bnxt *bp, uint16_t vf,
3039                         uint16_t max_bw, uint16_t enables)
3040 {
3041         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3042         struct hwrm_func_cfg_input req = {0};
3043         int rc;
3044
3045         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
3046
3047         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
3048         req.enables |= rte_cpu_to_le_32(enables);
3049         req.flags = rte_cpu_to_le_32(bp->pf.vf_info[vf].func_cfg_flags);
3050         req.max_bw = rte_cpu_to_le_32(max_bw);
3051         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3052
3053         HWRM_CHECK_RESULT();
3054         HWRM_UNLOCK();
3055
3056         return rc;
3057 }
3058
3059 int bnxt_hwrm_set_vf_vlan(struct bnxt *bp, int vf)
3060 {
3061         struct hwrm_func_cfg_input req = {0};
3062         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3063         int rc = 0;
3064
3065         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
3066
3067         req.flags = rte_cpu_to_le_32(bp->pf.vf_info[vf].func_cfg_flags);
3068         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
3069         req.enables |= rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_VLAN);
3070         req.dflt_vlan = rte_cpu_to_le_16(bp->pf.vf_info[vf].dflt_vlan);
3071
3072         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3073
3074         HWRM_CHECK_RESULT();
3075         HWRM_UNLOCK();
3076
3077         return rc;
3078 }
3079
3080 int bnxt_hwrm_set_async_event_cr(struct bnxt *bp)
3081 {
3082         int rc;
3083
3084         if (BNXT_PF(bp))
3085                 rc = bnxt_hwrm_func_cfg_def_cp(bp);
3086         else
3087                 rc = bnxt_hwrm_vf_func_cfg_def_cp(bp);
3088
3089         return rc;
3090 }
3091
3092 int bnxt_hwrm_reject_fwd_resp(struct bnxt *bp, uint16_t target_id,
3093                               void *encaped, size_t ec_size)
3094 {
3095         int rc = 0;
3096         struct hwrm_reject_fwd_resp_input req = {.req_type = 0};
3097         struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
3098
3099         if (ec_size > sizeof(req.encap_request))
3100                 return -1;
3101
3102         HWRM_PREP(req, REJECT_FWD_RESP, BNXT_USE_CHIMP_MB);
3103
3104         req.encap_resp_target_id = rte_cpu_to_le_16(target_id);
3105         memcpy(req.encap_request, encaped, ec_size);
3106
3107         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3108
3109         HWRM_CHECK_RESULT();
3110         HWRM_UNLOCK();
3111
3112         return rc;
3113 }
3114
3115 int bnxt_hwrm_func_qcfg_vf_default_mac(struct bnxt *bp, uint16_t vf,
3116                                        struct rte_ether_addr *mac)
3117 {
3118         struct hwrm_func_qcfg_input req = {0};
3119         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3120         int rc;
3121
3122         HWRM_PREP(req, FUNC_QCFG, BNXT_USE_CHIMP_MB);
3123
3124         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
3125         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3126
3127         HWRM_CHECK_RESULT();
3128
3129         memcpy(mac->addr_bytes, resp->mac_address, RTE_ETHER_ADDR_LEN);
3130
3131         HWRM_UNLOCK();
3132
3133         return rc;
3134 }
3135
3136 int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, uint16_t target_id,
3137                             void *encaped, size_t ec_size)
3138 {
3139         int rc = 0;
3140         struct hwrm_exec_fwd_resp_input req = {.req_type = 0};
3141         struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
3142
3143         if (ec_size > sizeof(req.encap_request))
3144                 return -1;
3145
3146         HWRM_PREP(req, EXEC_FWD_RESP, BNXT_USE_CHIMP_MB);
3147
3148         req.encap_resp_target_id = rte_cpu_to_le_16(target_id);
3149         memcpy(req.encap_request, encaped, ec_size);
3150
3151         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3152
3153         HWRM_CHECK_RESULT();
3154         HWRM_UNLOCK();
3155
3156         return rc;
3157 }
3158
3159 int bnxt_hwrm_ctx_qstats(struct bnxt *bp, uint32_t cid, int idx,
3160                          struct rte_eth_stats *stats, uint8_t rx)
3161 {
3162         int rc = 0;
3163         struct hwrm_stat_ctx_query_input req = {.req_type = 0};
3164         struct hwrm_stat_ctx_query_output *resp = bp->hwrm_cmd_resp_addr;
3165
3166         HWRM_PREP(req, STAT_CTX_QUERY, BNXT_USE_CHIMP_MB);
3167
3168         req.stat_ctx_id = rte_cpu_to_le_32(cid);
3169
3170         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3171
3172         HWRM_CHECK_RESULT();
3173
3174         if (rx) {
3175                 stats->q_ipackets[idx] = rte_le_to_cpu_64(resp->rx_ucast_pkts);
3176                 stats->q_ipackets[idx] += rte_le_to_cpu_64(resp->rx_mcast_pkts);
3177                 stats->q_ipackets[idx] += rte_le_to_cpu_64(resp->rx_bcast_pkts);
3178                 stats->q_ibytes[idx] = rte_le_to_cpu_64(resp->rx_ucast_bytes);
3179                 stats->q_ibytes[idx] += rte_le_to_cpu_64(resp->rx_mcast_bytes);
3180                 stats->q_ibytes[idx] += rte_le_to_cpu_64(resp->rx_bcast_bytes);
3181                 stats->q_errors[idx] = rte_le_to_cpu_64(resp->rx_err_pkts);
3182                 stats->q_errors[idx] += rte_le_to_cpu_64(resp->rx_drop_pkts);
3183         } else {
3184                 stats->q_opackets[idx] = rte_le_to_cpu_64(resp->tx_ucast_pkts);
3185                 stats->q_opackets[idx] += rte_le_to_cpu_64(resp->tx_mcast_pkts);
3186                 stats->q_opackets[idx] += rte_le_to_cpu_64(resp->tx_bcast_pkts);
3187                 stats->q_obytes[idx] = rte_le_to_cpu_64(resp->tx_ucast_bytes);
3188                 stats->q_obytes[idx] += rte_le_to_cpu_64(resp->tx_mcast_bytes);
3189                 stats->q_obytes[idx] += rte_le_to_cpu_64(resp->tx_bcast_bytes);
3190                 stats->q_errors[idx] += rte_le_to_cpu_64(resp->tx_err_pkts);
3191         }
3192
3193
3194         HWRM_UNLOCK();
3195
3196         return rc;
3197 }
3198
3199 int bnxt_hwrm_port_qstats(struct bnxt *bp)
3200 {
3201         struct hwrm_port_qstats_input req = {0};
3202         struct hwrm_port_qstats_output *resp = bp->hwrm_cmd_resp_addr;
3203         struct bnxt_pf_info *pf = &bp->pf;
3204         int rc;
3205
3206         HWRM_PREP(req, PORT_QSTATS, BNXT_USE_CHIMP_MB);
3207
3208         req.port_id = rte_cpu_to_le_16(pf->port_id);
3209         req.tx_stat_host_addr = rte_cpu_to_le_64(bp->hw_tx_port_stats_map);
3210         req.rx_stat_host_addr = rte_cpu_to_le_64(bp->hw_rx_port_stats_map);
3211         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3212
3213         HWRM_CHECK_RESULT();
3214         HWRM_UNLOCK();
3215
3216         return rc;
3217 }
3218
3219 int bnxt_hwrm_port_clr_stats(struct bnxt *bp)
3220 {
3221         struct hwrm_port_clr_stats_input req = {0};
3222         struct hwrm_port_clr_stats_output *resp = bp->hwrm_cmd_resp_addr;
3223         struct bnxt_pf_info *pf = &bp->pf;
3224         int rc;
3225
3226         /* Not allowed on NS2 device, NPAR, MultiHost, VF */
3227         if (!(bp->flags & BNXT_FLAG_PORT_STATS) || BNXT_VF(bp) ||
3228             BNXT_NPAR(bp) || BNXT_MH(bp) || BNXT_TOTAL_VFS(bp))
3229                 return 0;
3230
3231         HWRM_PREP(req, PORT_CLR_STATS, BNXT_USE_CHIMP_MB);
3232
3233         req.port_id = rte_cpu_to_le_16(pf->port_id);
3234         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3235
3236         HWRM_CHECK_RESULT();
3237         HWRM_UNLOCK();
3238
3239         return rc;
3240 }
3241
3242 int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
3243 {
3244         struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
3245         struct hwrm_port_led_qcaps_input req = {0};
3246         int rc;
3247
3248         if (BNXT_VF(bp))
3249                 return 0;
3250
3251         HWRM_PREP(req, PORT_LED_QCAPS, BNXT_USE_CHIMP_MB);
3252         req.port_id = bp->pf.port_id;
3253         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3254
3255         HWRM_CHECK_RESULT();
3256
3257         if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
3258                 unsigned int i;
3259
3260                 bp->num_leds = resp->num_leds;
3261                 memcpy(bp->leds, &resp->led0_id,
3262                         sizeof(bp->leds[0]) * bp->num_leds);
3263                 for (i = 0; i < bp->num_leds; i++) {
3264                         struct bnxt_led_info *led = &bp->leds[i];
3265
3266                         uint16_t caps = led->led_state_caps;
3267
3268                         if (!led->led_group_id ||
3269                                 !BNXT_LED_ALT_BLINK_CAP(caps)) {
3270                                 bp->num_leds = 0;
3271                                 break;
3272                         }
3273                 }
3274         }
3275
3276         HWRM_UNLOCK();
3277
3278         return rc;
3279 }
3280
3281 int bnxt_hwrm_port_led_cfg(struct bnxt *bp, bool led_on)
3282 {
3283         struct hwrm_port_led_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3284         struct hwrm_port_led_cfg_input req = {0};
3285         struct bnxt_led_cfg *led_cfg;
3286         uint8_t led_state = HWRM_PORT_LED_QCFG_OUTPUT_LED0_STATE_DEFAULT;
3287         uint16_t duration = 0;
3288         int rc, i;
3289
3290         if (!bp->num_leds || BNXT_VF(bp))
3291                 return -EOPNOTSUPP;
3292
3293         HWRM_PREP(req, PORT_LED_CFG, BNXT_USE_CHIMP_MB);
3294
3295         if (led_on) {
3296                 led_state = HWRM_PORT_LED_CFG_INPUT_LED0_STATE_BLINKALT;
3297                 duration = rte_cpu_to_le_16(500);
3298         }
3299         req.port_id = bp->pf.port_id;
3300         req.num_leds = bp->num_leds;
3301         led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
3302         for (i = 0; i < bp->num_leds; i++, led_cfg++) {
3303                 req.enables |= BNXT_LED_DFLT_ENABLES(i);
3304                 led_cfg->led_id = bp->leds[i].led_id;
3305                 led_cfg->led_state = led_state;
3306                 led_cfg->led_blink_on = duration;
3307                 led_cfg->led_blink_off = duration;
3308                 led_cfg->led_group_id = bp->leds[i].led_group_id;
3309         }
3310
3311         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3312
3313         HWRM_CHECK_RESULT();
3314         HWRM_UNLOCK();
3315
3316         return rc;
3317 }
3318
3319 int bnxt_hwrm_nvm_get_dir_info(struct bnxt *bp, uint32_t *entries,
3320                                uint32_t *length)
3321 {
3322         int rc;
3323         struct hwrm_nvm_get_dir_info_input req = {0};
3324         struct hwrm_nvm_get_dir_info_output *resp = bp->hwrm_cmd_resp_addr;
3325
3326         HWRM_PREP(req, NVM_GET_DIR_INFO, BNXT_USE_CHIMP_MB);
3327
3328         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3329
3330         HWRM_CHECK_RESULT();
3331         HWRM_UNLOCK();
3332
3333         if (!rc) {
3334                 *entries = rte_le_to_cpu_32(resp->entries);
3335                 *length = rte_le_to_cpu_32(resp->entry_length);
3336         }
3337         return rc;
3338 }
3339
3340 int bnxt_get_nvram_directory(struct bnxt *bp, uint32_t len, uint8_t *data)
3341 {
3342         int rc;
3343         uint32_t dir_entries;
3344         uint32_t entry_length;
3345         uint8_t *buf;
3346         size_t buflen;
3347         rte_iova_t dma_handle;
3348         struct hwrm_nvm_get_dir_entries_input req = {0};
3349         struct hwrm_nvm_get_dir_entries_output *resp = bp->hwrm_cmd_resp_addr;
3350
3351         rc = bnxt_hwrm_nvm_get_dir_info(bp, &dir_entries, &entry_length);
3352         if (rc != 0)
3353                 return rc;
3354
3355         *data++ = dir_entries;
3356         *data++ = entry_length;
3357         len -= 2;
3358         memset(data, 0xff, len);
3359
3360         buflen = dir_entries * entry_length;
3361         buf = rte_malloc("nvm_dir", buflen, 0);
3362         rte_mem_lock_page(buf);
3363         if (buf == NULL)
3364                 return -ENOMEM;
3365         dma_handle = rte_mem_virt2iova(buf);
3366         if (dma_handle == 0) {
3367                 PMD_DRV_LOG(ERR,
3368                         "unable to map response address to physical memory\n");
3369                 return -ENOMEM;
3370         }
3371         HWRM_PREP(req, NVM_GET_DIR_ENTRIES, BNXT_USE_CHIMP_MB);
3372         req.host_dest_addr = rte_cpu_to_le_64(dma_handle);
3373         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3374
3375         if (rc == 0)
3376                 memcpy(data, buf, len > buflen ? buflen : len);
3377
3378         rte_free(buf);
3379         HWRM_CHECK_RESULT();
3380         HWRM_UNLOCK();
3381
3382         return rc;
3383 }
3384
3385 int bnxt_hwrm_get_nvram_item(struct bnxt *bp, uint32_t index,
3386                              uint32_t offset, uint32_t length,
3387                              uint8_t *data)
3388 {
3389         int rc;
3390         uint8_t *buf;
3391         rte_iova_t dma_handle;
3392         struct hwrm_nvm_read_input req = {0};
3393         struct hwrm_nvm_read_output *resp = bp->hwrm_cmd_resp_addr;
3394
3395         buf = rte_malloc("nvm_item", length, 0);
3396         rte_mem_lock_page(buf);
3397         if (!buf)
3398                 return -ENOMEM;
3399
3400         dma_handle = rte_mem_virt2iova(buf);
3401         if (dma_handle == 0) {
3402                 PMD_DRV_LOG(ERR,
3403                         "unable to map response address to physical memory\n");
3404                 return -ENOMEM;
3405         }
3406         HWRM_PREP(req, NVM_READ, BNXT_USE_CHIMP_MB);
3407         req.host_dest_addr = rte_cpu_to_le_64(dma_handle);
3408         req.dir_idx = rte_cpu_to_le_16(index);
3409         req.offset = rte_cpu_to_le_32(offset);
3410         req.len = rte_cpu_to_le_32(length);
3411         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3412         if (rc == 0)
3413                 memcpy(data, buf, length);
3414
3415         rte_free(buf);
3416         HWRM_CHECK_RESULT();
3417         HWRM_UNLOCK();
3418
3419         return rc;
3420 }
3421
3422 int bnxt_hwrm_erase_nvram_directory(struct bnxt *bp, uint8_t index)
3423 {
3424         int rc;
3425         struct hwrm_nvm_erase_dir_entry_input req = {0};
3426         struct hwrm_nvm_erase_dir_entry_output *resp = bp->hwrm_cmd_resp_addr;
3427
3428         HWRM_PREP(req, NVM_ERASE_DIR_ENTRY, BNXT_USE_CHIMP_MB);
3429         req.dir_idx = rte_cpu_to_le_16(index);
3430         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3431         HWRM_CHECK_RESULT();
3432         HWRM_UNLOCK();
3433
3434         return rc;
3435 }
3436
3437
3438 int bnxt_hwrm_flash_nvram(struct bnxt *bp, uint16_t dir_type,
3439                           uint16_t dir_ordinal, uint16_t dir_ext,
3440                           uint16_t dir_attr, const uint8_t *data,
3441                           size_t data_len)
3442 {
3443         int rc;
3444         struct hwrm_nvm_write_input req = {0};
3445         struct hwrm_nvm_write_output *resp = bp->hwrm_cmd_resp_addr;
3446         rte_iova_t dma_handle;
3447         uint8_t *buf;
3448
3449         buf = rte_malloc("nvm_write", data_len, 0);
3450         rte_mem_lock_page(buf);
3451         if (!buf)
3452                 return -ENOMEM;
3453
3454         dma_handle = rte_mem_virt2iova(buf);
3455         if (dma_handle == 0) {
3456                 PMD_DRV_LOG(ERR,
3457                         "unable to map response address to physical memory\n");
3458                 return -ENOMEM;
3459         }
3460         memcpy(buf, data, data_len);
3461
3462         HWRM_PREP(req, NVM_WRITE, BNXT_USE_CHIMP_MB);
3463
3464         req.dir_type = rte_cpu_to_le_16(dir_type);
3465         req.dir_ordinal = rte_cpu_to_le_16(dir_ordinal);
3466         req.dir_ext = rte_cpu_to_le_16(dir_ext);
3467         req.dir_attr = rte_cpu_to_le_16(dir_attr);
3468         req.dir_data_length = rte_cpu_to_le_32(data_len);
3469         req.host_src_addr = rte_cpu_to_le_64(dma_handle);
3470
3471         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3472
3473         rte_free(buf);
3474         HWRM_CHECK_RESULT();
3475         HWRM_UNLOCK();
3476
3477         return rc;
3478 }
3479
3480 static void
3481 bnxt_vnic_count(struct bnxt_vnic_info *vnic __rte_unused, void *cbdata)
3482 {
3483         uint32_t *count = cbdata;
3484
3485         *count = *count + 1;
3486 }
3487
3488 static int bnxt_vnic_count_hwrm_stub(struct bnxt *bp __rte_unused,
3489                                      struct bnxt_vnic_info *vnic __rte_unused)
3490 {
3491         return 0;
3492 }
3493
3494 int bnxt_vf_vnic_count(struct bnxt *bp, uint16_t vf)
3495 {
3496         uint32_t count = 0;
3497
3498         bnxt_hwrm_func_vf_vnic_query_and_config(bp, vf, bnxt_vnic_count,
3499             &count, bnxt_vnic_count_hwrm_stub);
3500
3501         return count;
3502 }
3503
3504 static int bnxt_hwrm_func_vf_vnic_query(struct bnxt *bp, uint16_t vf,
3505                                         uint16_t *vnic_ids)
3506 {
3507         struct hwrm_func_vf_vnic_ids_query_input req = {0};
3508         struct hwrm_func_vf_vnic_ids_query_output *resp =
3509                                                 bp->hwrm_cmd_resp_addr;
3510         int rc;
3511
3512         /* First query all VNIC ids */
3513         HWRM_PREP(req, FUNC_VF_VNIC_IDS_QUERY, BNXT_USE_CHIMP_MB);
3514
3515         req.vf_id = rte_cpu_to_le_16(bp->pf.first_vf_id + vf);
3516         req.max_vnic_id_cnt = rte_cpu_to_le_32(bp->pf.total_vnics);
3517         req.vnic_id_tbl_addr = rte_cpu_to_le_64(rte_mem_virt2iova(vnic_ids));
3518
3519         if (req.vnic_id_tbl_addr == 0) {
3520                 HWRM_UNLOCK();
3521                 PMD_DRV_LOG(ERR,
3522                 "unable to map VNIC ID table address to physical memory\n");
3523                 return -ENOMEM;
3524         }
3525         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3526         if (rc) {
3527                 HWRM_UNLOCK();
3528                 PMD_DRV_LOG(ERR, "hwrm_func_vf_vnic_query failed rc:%d\n", rc);
3529                 return -1;
3530         } else if (resp->error_code) {
3531                 rc = rte_le_to_cpu_16(resp->error_code);
3532                 HWRM_UNLOCK();
3533                 PMD_DRV_LOG(ERR, "hwrm_func_vf_vnic_query error %d\n", rc);
3534                 return -1;
3535         }
3536         rc = rte_le_to_cpu_32(resp->vnic_id_cnt);
3537
3538         HWRM_UNLOCK();
3539
3540         return rc;
3541 }
3542
3543 /*
3544  * This function queries the VNIC IDs  for a specified VF. It then calls
3545  * the vnic_cb to update the necessary field in vnic_info with cbdata.
3546  * Then it calls the hwrm_cb function to program this new vnic configuration.
3547  */
3548 int bnxt_hwrm_func_vf_vnic_query_and_config(struct bnxt *bp, uint16_t vf,
3549         void (*vnic_cb)(struct bnxt_vnic_info *, void *), void *cbdata,
3550         int (*hwrm_cb)(struct bnxt *bp, struct bnxt_vnic_info *vnic))
3551 {
3552         struct bnxt_vnic_info vnic;
3553         int rc = 0;
3554         int i, num_vnic_ids;
3555         uint16_t *vnic_ids;
3556         size_t vnic_id_sz;
3557         size_t sz;
3558
3559         /* First query all VNIC ids */
3560         vnic_id_sz = bp->pf.total_vnics * sizeof(*vnic_ids);
3561         vnic_ids = rte_malloc("bnxt_hwrm_vf_vnic_ids_query", vnic_id_sz,
3562                         RTE_CACHE_LINE_SIZE);
3563         if (vnic_ids == NULL) {
3564                 rc = -ENOMEM;
3565                 return rc;
3566         }
3567         for (sz = 0; sz < vnic_id_sz; sz += getpagesize())
3568                 rte_mem_lock_page(((char *)vnic_ids) + sz);
3569
3570         num_vnic_ids = bnxt_hwrm_func_vf_vnic_query(bp, vf, vnic_ids);
3571
3572         if (num_vnic_ids < 0)
3573                 return num_vnic_ids;
3574
3575         /* Retrieve VNIC, update bd_stall then update */
3576
3577         for (i = 0; i < num_vnic_ids; i++) {
3578                 memset(&vnic, 0, sizeof(struct bnxt_vnic_info));
3579                 vnic.fw_vnic_id = rte_le_to_cpu_16(vnic_ids[i]);
3580                 rc = bnxt_hwrm_vnic_qcfg(bp, &vnic, bp->pf.first_vf_id + vf);
3581                 if (rc)
3582                         break;
3583                 if (vnic.mru <= 4)      /* Indicates unallocated */
3584                         continue;
3585
3586                 vnic_cb(&vnic, cbdata);
3587
3588                 rc = hwrm_cb(bp, &vnic);
3589                 if (rc)
3590                         break;
3591         }
3592
3593         rte_free(vnic_ids);
3594
3595         return rc;
3596 }
3597
3598 int bnxt_hwrm_func_cfg_vf_set_vlan_anti_spoof(struct bnxt *bp, uint16_t vf,
3599                                               bool on)
3600 {
3601         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3602         struct hwrm_func_cfg_input req = {0};
3603         int rc;
3604
3605         HWRM_PREP(req, FUNC_CFG, BNXT_USE_CHIMP_MB);
3606
3607         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
3608         req.enables |= rte_cpu_to_le_32(
3609                         HWRM_FUNC_CFG_INPUT_ENABLES_VLAN_ANTISPOOF_MODE);
3610         req.vlan_antispoof_mode = on ?
3611                 HWRM_FUNC_CFG_INPUT_VLAN_ANTISPOOF_MODE_VALIDATE_VLAN :
3612                 HWRM_FUNC_CFG_INPUT_VLAN_ANTISPOOF_MODE_NOCHECK;
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_func_qcfg_vf_dflt_vnic_id(struct bnxt *bp, int vf)
3622 {
3623         struct bnxt_vnic_info vnic;
3624         uint16_t *vnic_ids;
3625         size_t vnic_id_sz;
3626         int num_vnic_ids, i;
3627         size_t sz;
3628         int rc;
3629
3630         vnic_id_sz = bp->pf.total_vnics * sizeof(*vnic_ids);
3631         vnic_ids = rte_malloc("bnxt_hwrm_vf_vnic_ids_query", vnic_id_sz,
3632                         RTE_CACHE_LINE_SIZE);
3633         if (vnic_ids == NULL) {
3634                 rc = -ENOMEM;
3635                 return rc;
3636         }
3637
3638         for (sz = 0; sz < vnic_id_sz; sz += getpagesize())
3639                 rte_mem_lock_page(((char *)vnic_ids) + sz);
3640
3641         rc = bnxt_hwrm_func_vf_vnic_query(bp, vf, vnic_ids);
3642         if (rc <= 0)
3643                 goto exit;
3644         num_vnic_ids = rc;
3645
3646         /*
3647          * Loop through to find the default VNIC ID.
3648          * TODO: The easier way would be to obtain the resp->dflt_vnic_id
3649          * by sending the hwrm_func_qcfg command to the firmware.
3650          */
3651         for (i = 0; i < num_vnic_ids; i++) {
3652                 memset(&vnic, 0, sizeof(struct bnxt_vnic_info));
3653                 vnic.fw_vnic_id = rte_le_to_cpu_16(vnic_ids[i]);
3654                 rc = bnxt_hwrm_vnic_qcfg(bp, &vnic,
3655                                         bp->pf.first_vf_id + vf);
3656                 if (rc)
3657                         goto exit;
3658                 if (vnic.func_default) {
3659                         rte_free(vnic_ids);
3660                         return vnic.fw_vnic_id;
3661                 }
3662         }
3663         /* Could not find a default VNIC. */
3664         PMD_DRV_LOG(ERR, "No default VNIC\n");
3665 exit:
3666         rte_free(vnic_ids);
3667         return -1;
3668 }
3669
3670 int bnxt_hwrm_set_em_filter(struct bnxt *bp,
3671                          uint16_t dst_id,
3672                          struct bnxt_filter_info *filter)
3673 {
3674         int rc = 0;
3675         struct hwrm_cfa_em_flow_alloc_input req = {.req_type = 0 };
3676         struct hwrm_cfa_em_flow_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3677         uint32_t enables = 0;
3678
3679         if (filter->fw_em_filter_id != UINT64_MAX)
3680                 bnxt_hwrm_clear_em_filter(bp, filter);
3681
3682         HWRM_PREP(req, CFA_EM_FLOW_ALLOC, BNXT_USE_KONG(bp));
3683
3684         req.flags = rte_cpu_to_le_32(filter->flags);
3685
3686         enables = filter->enables |
3687               HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_DST_ID;
3688         req.dst_id = rte_cpu_to_le_16(dst_id);
3689
3690         if (filter->ip_addr_type) {
3691                 req.ip_addr_type = filter->ip_addr_type;
3692                 enables |= HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_IPADDR_TYPE;
3693         }
3694         if (enables &
3695             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_L2_FILTER_ID)
3696                 req.l2_filter_id = rte_cpu_to_le_64(filter->fw_l2_filter_id);
3697         if (enables &
3698             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_SRC_MACADDR)
3699                 memcpy(req.src_macaddr, filter->src_macaddr,
3700                        RTE_ETHER_ADDR_LEN);
3701         if (enables &
3702             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_DST_MACADDR)
3703                 memcpy(req.dst_macaddr, filter->dst_macaddr,
3704                        RTE_ETHER_ADDR_LEN);
3705         if (enables &
3706             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_OVLAN_VID)
3707                 req.ovlan_vid = filter->l2_ovlan;
3708         if (enables &
3709             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_IVLAN_VID)
3710                 req.ivlan_vid = filter->l2_ivlan;
3711         if (enables &
3712             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_ETHERTYPE)
3713                 req.ethertype = rte_cpu_to_be_16(filter->ethertype);
3714         if (enables &
3715             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_IP_PROTOCOL)
3716                 req.ip_protocol = filter->ip_protocol;
3717         if (enables &
3718             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_SRC_IPADDR)
3719                 req.src_ipaddr[0] = rte_cpu_to_be_32(filter->src_ipaddr[0]);
3720         if (enables &
3721             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_DST_IPADDR)
3722                 req.dst_ipaddr[0] = rte_cpu_to_be_32(filter->dst_ipaddr[0]);
3723         if (enables &
3724             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_SRC_PORT)
3725                 req.src_port = rte_cpu_to_be_16(filter->src_port);
3726         if (enables &
3727             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_DST_PORT)
3728                 req.dst_port = rte_cpu_to_be_16(filter->dst_port);
3729         if (enables &
3730             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_MIRROR_VNIC_ID)
3731                 req.mirror_vnic_id = filter->mirror_vnic_id;
3732
3733         req.enables = rte_cpu_to_le_32(enables);
3734
3735         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_KONG(bp));
3736
3737         HWRM_CHECK_RESULT();
3738
3739         filter->fw_em_filter_id = rte_le_to_cpu_64(resp->em_filter_id);
3740         HWRM_UNLOCK();
3741
3742         return rc;
3743 }
3744
3745 int bnxt_hwrm_clear_em_filter(struct bnxt *bp, struct bnxt_filter_info *filter)
3746 {
3747         int rc = 0;
3748         struct hwrm_cfa_em_flow_free_input req = {.req_type = 0 };
3749         struct hwrm_cfa_em_flow_free_output *resp = bp->hwrm_cmd_resp_addr;
3750
3751         if (filter->fw_em_filter_id == UINT64_MAX)
3752                 return 0;
3753
3754         PMD_DRV_LOG(ERR, "Clear EM filter\n");
3755         HWRM_PREP(req, CFA_EM_FLOW_FREE, BNXT_USE_KONG(bp));
3756
3757         req.em_filter_id = rte_cpu_to_le_64(filter->fw_em_filter_id);
3758
3759         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_KONG(bp));
3760
3761         HWRM_CHECK_RESULT();
3762         HWRM_UNLOCK();
3763
3764         filter->fw_em_filter_id = UINT64_MAX;
3765         filter->fw_l2_filter_id = UINT64_MAX;
3766
3767         return 0;
3768 }
3769
3770 int bnxt_hwrm_set_ntuple_filter(struct bnxt *bp,
3771                          uint16_t dst_id,
3772                          struct bnxt_filter_info *filter)
3773 {
3774         int rc = 0;
3775         struct hwrm_cfa_ntuple_filter_alloc_input req = {.req_type = 0 };
3776         struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3777                                                 bp->hwrm_cmd_resp_addr;
3778         uint32_t enables = 0;
3779
3780         if (filter->fw_ntuple_filter_id != UINT64_MAX)
3781                 bnxt_hwrm_clear_ntuple_filter(bp, filter);
3782
3783         HWRM_PREP(req, CFA_NTUPLE_FILTER_ALLOC, BNXT_USE_CHIMP_MB);
3784
3785         req.flags = rte_cpu_to_le_32(filter->flags);
3786
3787         enables = filter->enables |
3788               HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_ID;
3789         req.dst_id = rte_cpu_to_le_16(dst_id);
3790
3791
3792         if (filter->ip_addr_type) {
3793                 req.ip_addr_type = filter->ip_addr_type;
3794                 enables |=
3795                         HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_IPADDR_TYPE;
3796         }
3797         if (enables &
3798             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_L2_FILTER_ID)
3799                 req.l2_filter_id = rte_cpu_to_le_64(filter->fw_l2_filter_id);
3800         if (enables &
3801             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_MACADDR)
3802                 memcpy(req.src_macaddr, filter->src_macaddr,
3803                        RTE_ETHER_ADDR_LEN);
3804         //if (enables &
3805             //HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_MACADDR)
3806                 //memcpy(req.dst_macaddr, filter->dst_macaddr,
3807                        //RTE_ETHER_ADDR_LEN);
3808         if (enables &
3809             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_ETHERTYPE)
3810                 req.ethertype = rte_cpu_to_be_16(filter->ethertype);
3811         if (enables &
3812             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_IP_PROTOCOL)
3813                 req.ip_protocol = filter->ip_protocol;
3814         if (enables &
3815             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_IPADDR)
3816                 req.src_ipaddr[0] = rte_cpu_to_le_32(filter->src_ipaddr[0]);
3817         if (enables &
3818             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_IPADDR_MASK)
3819                 req.src_ipaddr_mask[0] =
3820                         rte_cpu_to_le_32(filter->src_ipaddr_mask[0]);
3821         if (enables &
3822             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_IPADDR)
3823                 req.dst_ipaddr[0] = rte_cpu_to_le_32(filter->dst_ipaddr[0]);
3824         if (enables &
3825             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_IPADDR_MASK)
3826                 req.dst_ipaddr_mask[0] =
3827                         rte_cpu_to_be_32(filter->dst_ipaddr_mask[0]);
3828         if (enables &
3829             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_PORT)
3830                 req.src_port = rte_cpu_to_le_16(filter->src_port);
3831         if (enables &
3832             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_PORT_MASK)
3833                 req.src_port_mask = rte_cpu_to_le_16(filter->src_port_mask);
3834         if (enables &
3835             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_PORT)
3836                 req.dst_port = rte_cpu_to_le_16(filter->dst_port);
3837         if (enables &
3838             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_PORT_MASK)
3839                 req.dst_port_mask = rte_cpu_to_le_16(filter->dst_port_mask);
3840         if (enables &
3841             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_MIRROR_VNIC_ID)
3842                 req.mirror_vnic_id = filter->mirror_vnic_id;
3843
3844         req.enables = rte_cpu_to_le_32(enables);
3845
3846         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3847
3848         HWRM_CHECK_RESULT();
3849
3850         filter->fw_ntuple_filter_id = rte_le_to_cpu_64(resp->ntuple_filter_id);
3851         HWRM_UNLOCK();
3852
3853         return rc;
3854 }
3855
3856 int bnxt_hwrm_clear_ntuple_filter(struct bnxt *bp,
3857                                 struct bnxt_filter_info *filter)
3858 {
3859         int rc = 0;
3860         struct hwrm_cfa_ntuple_filter_free_input req = {.req_type = 0 };
3861         struct hwrm_cfa_ntuple_filter_free_output *resp =
3862                                                 bp->hwrm_cmd_resp_addr;
3863
3864         if (filter->fw_ntuple_filter_id == UINT64_MAX)
3865                 return 0;
3866
3867         HWRM_PREP(req, CFA_NTUPLE_FILTER_FREE, BNXT_USE_CHIMP_MB);
3868
3869         req.ntuple_filter_id = rte_cpu_to_le_64(filter->fw_ntuple_filter_id);
3870
3871         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3872
3873         HWRM_CHECK_RESULT();
3874         HWRM_UNLOCK();
3875
3876         filter->fw_ntuple_filter_id = UINT64_MAX;
3877
3878         return 0;
3879 }
3880
3881 int bnxt_vnic_rss_configure(struct bnxt *bp, struct bnxt_vnic_info *vnic)
3882 {
3883         unsigned int rss_idx, fw_idx, i;
3884
3885         if (vnic->rss_table && vnic->hash_type) {
3886                 /*
3887                  * Fill the RSS hash & redirection table with
3888                  * ring group ids for all VNICs
3889                  */
3890                 for (rss_idx = 0, fw_idx = 0; rss_idx < HW_HASH_INDEX_SIZE;
3891                         rss_idx++, fw_idx++) {
3892                         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
3893                                 fw_idx %= bp->rx_cp_nr_rings;
3894                                 if (vnic->fw_grp_ids[fw_idx] !=
3895                                     INVALID_HW_RING_ID)
3896                                         break;
3897                                 fw_idx++;
3898                         }
3899                         if (i == bp->rx_cp_nr_rings)
3900                                 return 0;
3901                         vnic->rss_table[rss_idx] =
3902                                 vnic->fw_grp_ids[fw_idx];
3903                 }
3904                 return bnxt_hwrm_vnic_rss_cfg(bp, vnic);
3905         }
3906         return 0;
3907 }
3908
3909 static void bnxt_hwrm_set_coal_params(struct bnxt_coal *hw_coal,
3910         struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
3911 {
3912         uint16_t flags;
3913
3914         req->num_cmpl_aggr_int = rte_cpu_to_le_16(hw_coal->num_cmpl_aggr_int);
3915
3916         /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
3917         req->num_cmpl_dma_aggr = rte_cpu_to_le_16(hw_coal->num_cmpl_dma_aggr);
3918
3919         /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
3920         req->num_cmpl_dma_aggr_during_int =
3921                 rte_cpu_to_le_16(hw_coal->num_cmpl_dma_aggr_during_int);
3922
3923         req->int_lat_tmr_max = rte_cpu_to_le_16(hw_coal->int_lat_tmr_max);
3924
3925         /* min timer set to 1/2 of interrupt timer */
3926         req->int_lat_tmr_min = rte_cpu_to_le_16(hw_coal->int_lat_tmr_min);
3927
3928         /* buf timer set to 1/4 of interrupt timer */
3929         req->cmpl_aggr_dma_tmr = rte_cpu_to_le_16(hw_coal->cmpl_aggr_dma_tmr);
3930
3931         req->cmpl_aggr_dma_tmr_during_int =
3932                 rte_cpu_to_le_16(hw_coal->cmpl_aggr_dma_tmr_during_int);
3933
3934         flags = HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_FLAGS_TIMER_RESET |
3935                 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_FLAGS_RING_IDLE;
3936         req->flags = rte_cpu_to_le_16(flags);
3937 }
3938
3939 int bnxt_hwrm_set_ring_coal(struct bnxt *bp,
3940                         struct bnxt_coal *coal, uint16_t ring_id)
3941 {
3942         struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
3943         struct hwrm_ring_cmpl_ring_cfg_aggint_params_output *resp =
3944                                                 bp->hwrm_cmd_resp_addr;
3945         int rc;
3946
3947         /* Set ring coalesce parameters only for Stratus 100G NIC */
3948         if (!bnxt_stratus_device(bp))
3949                 return 0;
3950
3951         HWRM_PREP(req, RING_CMPL_RING_CFG_AGGINT_PARAMS, BNXT_USE_CHIMP_MB);
3952         bnxt_hwrm_set_coal_params(coal, &req);
3953         req.ring_id = rte_cpu_to_le_16(ring_id);
3954         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3955         HWRM_CHECK_RESULT();
3956         HWRM_UNLOCK();
3957         return 0;
3958 }
3959
3960 int bnxt_hwrm_ext_port_qstats(struct bnxt *bp)
3961 {
3962         struct hwrm_port_qstats_ext_input req = {0};
3963         struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
3964         struct bnxt_pf_info *pf = &bp->pf;
3965         int rc;
3966
3967         if (!(bp->flags & BNXT_FLAG_EXT_RX_PORT_STATS ||
3968               bp->flags & BNXT_FLAG_EXT_TX_PORT_STATS))
3969                 return 0;
3970
3971         HWRM_PREP(req, PORT_QSTATS_EXT, BNXT_USE_CHIMP_MB);
3972
3973         req.port_id = rte_cpu_to_le_16(pf->port_id);
3974         if (bp->flags & BNXT_FLAG_EXT_TX_PORT_STATS) {
3975                 req.tx_stat_host_addr =
3976                         rte_cpu_to_le_64(bp->hw_tx_port_stats_map);
3977                 req.tx_stat_size =
3978                         rte_cpu_to_le_16(sizeof(struct tx_port_stats_ext));
3979         }
3980         if (bp->flags & BNXT_FLAG_EXT_RX_PORT_STATS) {
3981                 req.rx_stat_host_addr =
3982                         rte_cpu_to_le_64(bp->hw_rx_port_stats_map);
3983                 req.rx_stat_size =
3984                         rte_cpu_to_le_16(sizeof(struct rx_port_stats_ext));
3985         }
3986         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3987
3988         if (rc) {
3989                 bp->fw_rx_port_stats_ext_size = 0;
3990                 bp->fw_tx_port_stats_ext_size = 0;
3991         } else {
3992                 bp->fw_rx_port_stats_ext_size =
3993                         rte_le_to_cpu_16(resp->rx_stat_size);
3994                 bp->fw_tx_port_stats_ext_size =
3995                         rte_le_to_cpu_16(resp->tx_stat_size);
3996         }
3997
3998         HWRM_CHECK_RESULT();
3999         HWRM_UNLOCK();
4000
4001         return rc;
4002 }