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