5850e7e48ba09e9ddb3bf95e353848ed3d62e9b9
[dpdk.git] / drivers / net / bnxt / bnxt_hwrm.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2021 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 #include <rte_io.h>
15
16 #include "bnxt.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 #define HWRM_SPEC_CODE_1_8_3            0x10803
28 #define HWRM_VERSION_1_9_1              0x10901
29 #define HWRM_VERSION_1_9_2              0x10903
30 #define HWRM_VERSION_1_10_2_13          0x10a020d
31 struct bnxt_plcmodes_cfg {
32         uint32_t        flags;
33         uint16_t        jumbo_thresh;
34         uint16_t        hds_offset;
35         uint16_t        hds_threshold;
36 };
37
38 static int page_getenum(size_t size)
39 {
40         if (size <= 1 << 4)
41                 return 4;
42         if (size <= 1 << 12)
43                 return 12;
44         if (size <= 1 << 13)
45                 return 13;
46         if (size <= 1 << 16)
47                 return 16;
48         if (size <= 1 << 21)
49                 return 21;
50         if (size <= 1 << 22)
51                 return 22;
52         if (size <= 1 << 30)
53                 return 30;
54         PMD_DRV_LOG(ERR, "Page size %zu out of range\n", size);
55         return sizeof(int) * 8 - 1;
56 }
57
58 static int page_roundup(size_t size)
59 {
60         return 1 << page_getenum(size);
61 }
62
63 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem,
64                                   uint8_t *pg_attr,
65                                   uint64_t *pg_dir)
66 {
67         if (rmem->nr_pages == 0)
68                 return;
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 static struct bnxt_cp_ring_info*
79 bnxt_get_ring_info_by_id(struct bnxt *bp, uint16_t rid, uint16_t type)
80 {
81         struct bnxt_cp_ring_info *cp_ring = NULL;
82         uint16_t i;
83
84         switch (type) {
85         case HWRM_RING_FREE_INPUT_RING_TYPE_RX:
86         case HWRM_RING_FREE_INPUT_RING_TYPE_RX_AGG:
87                 /* FALLTHROUGH */
88                 for (i = 0; i < bp->rx_cp_nr_rings; i++) {
89                         struct bnxt_rx_queue *rxq = bp->rx_queues[i];
90
91                         if (rxq->cp_ring->cp_ring_struct->fw_ring_id ==
92                             rte_cpu_to_le_16(rid)) {
93                                 return rxq->cp_ring;
94                         }
95                 }
96                 break;
97         case HWRM_RING_FREE_INPUT_RING_TYPE_TX:
98                 for (i = 0; i < bp->tx_cp_nr_rings; i++) {
99                         struct bnxt_tx_queue *txq = bp->tx_queues[i];
100
101                         if (txq->cp_ring->cp_ring_struct->fw_ring_id ==
102                             rte_cpu_to_le_16(rid)) {
103                                 return txq->cp_ring;
104                         }
105                 }
106                 break;
107         default:
108                 return cp_ring;
109         }
110         return cp_ring;
111 }
112
113 /* Complete a sweep of the CQ ring for the corresponding Tx/Rx/AGG ring.
114  * If the CMPL_BASE_TYPE_HWRM_DONE is not encountered by the last pass,
115  * before timeout, we force the done bit for the cleanup to proceed.
116  * Also if cpr is null, do nothing.. The HWRM command is  not for a
117  * Tx/Rx/AGG ring cleanup.
118  */
119 static int
120 bnxt_check_cq_hwrm_done(struct bnxt_cp_ring_info *cpr,
121                         bool tx, bool rx, bool timeout)
122 {
123         int done = 0;
124
125         if (cpr != NULL) {
126                 if (tx)
127                         done = bnxt_flush_tx_cmp(cpr);
128
129                 if (rx)
130                         done = bnxt_flush_rx_cmp(cpr);
131
132                 if (done)
133                         PMD_DRV_LOG(DEBUG, "HWRM DONE for %s ring\n",
134                                     rx ? "Rx" : "Tx");
135
136                 /* We are about to timeout and still haven't seen the
137                  * HWRM done for the Ring free. Force the cleanup.
138                  */
139                 if (!done && timeout) {
140                         done = 1;
141                         PMD_DRV_LOG(DEBUG, "Timing out for %s ring\n",
142                                     rx ? "Rx" : "Tx");
143                 }
144         } else {
145                 /* This HWRM command is not for a Tx/Rx/AGG ring cleanup.
146                  * Otherwise the cpr would have been valid. So do nothing.
147                  */
148                 done = 1;
149         }
150
151         return done;
152 }
153
154 /*
155  * HWRM Functions (sent to HWRM)
156  * These are named bnxt_hwrm_*() and return 0 on success or -110 if the
157  * HWRM command times out, or a negative error code if the HWRM
158  * command was failed by the FW.
159  */
160
161 static int bnxt_hwrm_send_message(struct bnxt *bp, void *msg,
162                                   uint32_t msg_len, bool use_kong_mb)
163 {
164         unsigned int i;
165         struct input *req = msg;
166         struct output *resp = bp->hwrm_cmd_resp_addr;
167         uint32_t *data = msg;
168         uint8_t *bar;
169         uint8_t *valid;
170         uint16_t max_req_len = bp->max_req_len;
171         struct hwrm_short_input short_input = { 0 };
172         uint16_t bar_offset = use_kong_mb ?
173                 GRCPF_REG_KONG_CHANNEL_OFFSET : GRCPF_REG_CHIMP_CHANNEL_OFFSET;
174         uint16_t mb_trigger_offset = use_kong_mb ?
175                 GRCPF_REG_KONG_COMM_TRIGGER : GRCPF_REG_CHIMP_COMM_TRIGGER;
176         struct bnxt_cp_ring_info *cpr = NULL;
177         bool is_rx = false;
178         bool is_tx = false;
179         uint32_t timeout;
180
181         /* Do not send HWRM commands to firmware in error state */
182         if (bp->flags & BNXT_FLAG_FATAL_ERROR)
183                 return 0;
184
185         timeout = bp->hwrm_cmd_timeout;
186
187         /* Update the message length for backing store config for new FW. */
188         if (bp->fw_ver >= HWRM_VERSION_1_10_2_13 &&
189             rte_cpu_to_le_16(req->req_type) == HWRM_FUNC_BACKING_STORE_CFG)
190                 msg_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
191
192         if (bp->flags & BNXT_FLAG_SHORT_CMD ||
193             msg_len > bp->max_req_len) {
194                 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
195
196                 memset(short_cmd_req, 0, bp->hwrm_max_ext_req_len);
197                 memcpy(short_cmd_req, req, msg_len);
198
199                 short_input.req_type = rte_cpu_to_le_16(req->req_type);
200                 short_input.signature = rte_cpu_to_le_16(
201                                         HWRM_SHORT_INPUT_SIGNATURE_SHORT_CMD);
202                 short_input.size = rte_cpu_to_le_16(msg_len);
203                 short_input.req_addr =
204                         rte_cpu_to_le_64(bp->hwrm_short_cmd_req_dma_addr);
205
206                 data = (uint32_t *)&short_input;
207                 msg_len = sizeof(short_input);
208
209                 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
210         }
211
212         /* Write request msg to hwrm channel */
213         for (i = 0; i < msg_len; i += 4) {
214                 bar = (uint8_t *)bp->bar0 + bar_offset + i;
215                 rte_write32(*data, bar);
216                 data++;
217         }
218
219         /* Zero the rest of the request space */
220         for (; i < max_req_len; i += 4) {
221                 bar = (uint8_t *)bp->bar0 + bar_offset + i;
222                 rte_write32(0, bar);
223         }
224
225         /* Ring channel doorbell */
226         bar = (uint8_t *)bp->bar0 + mb_trigger_offset;
227         rte_write32(1, bar);
228         /*
229          * Make sure the channel doorbell ring command complete before
230          * reading the response to avoid getting stale or invalid
231          * responses.
232          */
233         rte_io_mb();
234
235         /* Check ring flush is done.
236          * This is valid only for Tx and Rx rings (including AGG rings).
237          * The Tx and Rx rings should be freed once the HW confirms all
238          * the internal buffers and BDs associated with the rings are
239          * consumed and the corresponding DMA is handled.
240          */
241         if (rte_cpu_to_le_16(req->cmpl_ring) != INVALID_HW_RING_ID) {
242                 /* Check if the TxCQ matches. If that fails check if RxCQ
243                  * matches. And if neither match, is_rx = false, is_tx = false.
244                  */
245                 cpr = bnxt_get_ring_info_by_id(bp, req->cmpl_ring,
246                                                HWRM_RING_FREE_INPUT_RING_TYPE_TX);
247                 if (cpr == NULL) {
248                         /* Not a TxCQ. Check if the RxCQ matches. */
249                         cpr =
250                         bnxt_get_ring_info_by_id(bp, req->cmpl_ring,
251                                                  HWRM_RING_FREE_INPUT_RING_TYPE_RX);
252                         if (cpr != NULL)
253                                 is_rx = true;
254                 } else {
255                         is_tx = true;
256                 }
257         }
258
259         /* Poll for the valid bit */
260         for (i = 0; i < timeout; i++) {
261                 int done;
262
263                 done = bnxt_check_cq_hwrm_done(cpr, is_tx, is_rx,
264                                                i == timeout - 1);
265                 /* Sanity check on the resp->resp_len */
266                 rte_io_rmb();
267                 if (resp->resp_len && resp->resp_len <= bp->max_resp_len) {
268                         /* Last byte of resp contains the valid key */
269                         valid = (uint8_t *)resp + resp->resp_len - 1;
270                         if (*valid == HWRM_RESP_VALID_KEY && done)
271                                 break;
272                 }
273                 rte_delay_us(1);
274         }
275
276         if (i >= timeout) {
277                 /* Suppress VER_GET timeout messages during reset recovery */
278                 if (bp->flags & BNXT_FLAG_FW_RESET &&
279                     rte_cpu_to_le_16(req->req_type) == HWRM_VER_GET)
280                         return -ETIMEDOUT;
281
282                 PMD_DRV_LOG(ERR,
283                             "Error(timeout) sending msg 0x%04x, seq_id %d\n",
284                             req->req_type, req->seq_id);
285                 return -ETIMEDOUT;
286         }
287         return 0;
288 }
289
290 /*
291  * HWRM_PREP() should be used to prepare *ALL* HWRM commands. It grabs the
292  * spinlock, and does initial processing.
293  *
294  * HWRM_CHECK_RESULT() returns errors on failure and may not be used.  It
295  * releases the spinlock only if it returns. If the regular int return codes
296  * are not used by the function, HWRM_CHECK_RESULT() should not be used
297  * directly, rather it should be copied and modified to suit the function.
298  *
299  * HWRM_UNLOCK() must be called after all response processing is completed.
300  */
301 #define HWRM_PREP(req, type, kong) do { \
302         rte_spinlock_lock(&bp->hwrm_lock); \
303         if (bp->hwrm_cmd_resp_addr == NULL) { \
304                 rte_spinlock_unlock(&bp->hwrm_lock); \
305                 return -EACCES; \
306         } \
307         memset(bp->hwrm_cmd_resp_addr, 0, bp->max_resp_len); \
308         (req)->req_type = rte_cpu_to_le_16(type); \
309         (req)->cmpl_ring = rte_cpu_to_le_16(-1); \
310         (req)->seq_id = kong ? rte_cpu_to_le_16(bp->kong_cmd_seq++) :\
311                 rte_cpu_to_le_16(bp->chimp_cmd_seq++); \
312         (req)->target_id = rte_cpu_to_le_16(0xffff); \
313         (req)->resp_addr = rte_cpu_to_le_64(bp->hwrm_cmd_resp_dma_addr); \
314 } while (0)
315
316 #define HWRM_CHECK_RESULT_SILENT() do {\
317         if (rc) { \
318                 rte_spinlock_unlock(&bp->hwrm_lock); \
319                 return rc; \
320         } \
321         if (resp->error_code) { \
322                 rc = rte_le_to_cpu_16(resp->error_code); \
323                 rte_spinlock_unlock(&bp->hwrm_lock); \
324                 return rc; \
325         } \
326 } while (0)
327
328 #define HWRM_CHECK_RESULT() do {\
329         if (rc) { \
330                 PMD_DRV_LOG(ERR, "failed rc:%d\n", rc); \
331                 rte_spinlock_unlock(&bp->hwrm_lock); \
332                 if (rc == HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED) \
333                         rc = -EACCES; \
334                 else if (rc == HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR) \
335                         rc = -ENOSPC; \
336                 else if (rc == HWRM_ERR_CODE_INVALID_PARAMS) \
337                         rc = -EINVAL; \
338                 else if (rc == HWRM_ERR_CODE_CMD_NOT_SUPPORTED) \
339                         rc = -ENOTSUP; \
340                 else if (rc == HWRM_ERR_CODE_HOT_RESET_PROGRESS) \
341                         rc = -EAGAIN; \
342                 else if (rc > 0) \
343                         rc = -EIO; \
344                 return rc; \
345         } \
346         if (resp->error_code) { \
347                 rc = rte_le_to_cpu_16(resp->error_code); \
348                 if (resp->resp_len >= 16) { \
349                         struct hwrm_err_output *tmp_hwrm_err_op = \
350                                                 (void *)resp; \
351                         PMD_DRV_LOG(ERR, \
352                                 "error %d:%d:%08x:%04x\n", \
353                                 rc, tmp_hwrm_err_op->cmd_err, \
354                                 rte_le_to_cpu_32(\
355                                         tmp_hwrm_err_op->opaque_0), \
356                                 rte_le_to_cpu_16(\
357                                         tmp_hwrm_err_op->opaque_1)); \
358                 } else { \
359                         PMD_DRV_LOG(ERR, "error %d\n", rc); \
360                 } \
361                 rte_spinlock_unlock(&bp->hwrm_lock); \
362                 if (rc == HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED) \
363                         rc = -EACCES; \
364                 else if (rc == HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR) \
365                         rc = -ENOSPC; \
366                 else if (rc == HWRM_ERR_CODE_INVALID_PARAMS) \
367                         rc = -EINVAL; \
368                 else if (rc == HWRM_ERR_CODE_CMD_NOT_SUPPORTED) \
369                         rc = -ENOTSUP; \
370                 else if (rc == HWRM_ERR_CODE_HOT_RESET_PROGRESS) \
371                         rc = -EAGAIN; \
372                 else if (rc > 0) \
373                         rc = -EIO; \
374                 return rc; \
375         } \
376 } while (0)
377
378 #define HWRM_UNLOCK()           rte_spinlock_unlock(&bp->hwrm_lock)
379
380 int bnxt_hwrm_tf_message_direct(struct bnxt *bp,
381                                 bool use_kong_mb,
382                                 uint16_t msg_type,
383                                 void *msg,
384                                 uint32_t msg_len,
385                                 void *resp_msg,
386                                 uint32_t resp_len)
387 {
388         int rc = 0;
389         bool mailbox = BNXT_USE_CHIMP_MB;
390         struct input *req = msg;
391         struct output *resp = bp->hwrm_cmd_resp_addr;
392
393         if (use_kong_mb)
394                 mailbox = BNXT_USE_KONG(bp);
395
396         HWRM_PREP(req, msg_type, mailbox);
397
398         rc = bnxt_hwrm_send_message(bp, req, msg_len, mailbox);
399
400         HWRM_CHECK_RESULT();
401
402         if (resp_msg)
403                 memcpy(resp_msg, resp, resp_len);
404
405         HWRM_UNLOCK();
406
407         return rc;
408 }
409
410 int bnxt_hwrm_tf_message_tunneled(struct bnxt *bp,
411                                   bool use_kong_mb,
412                                   uint16_t tf_type,
413                                   uint16_t tf_subtype,
414                                   uint32_t *tf_response_code,
415                                   void *msg,
416                                   uint32_t msg_len,
417                                   void *response,
418                                   uint32_t response_len)
419 {
420         int rc = 0;
421         struct hwrm_cfa_tflib_input req = { .req_type = 0 };
422         struct hwrm_cfa_tflib_output *resp = bp->hwrm_cmd_resp_addr;
423         bool mailbox = BNXT_USE_CHIMP_MB;
424
425         if (msg_len > sizeof(req.tf_req))
426                 return -ENOMEM;
427
428         if (use_kong_mb)
429                 mailbox = BNXT_USE_KONG(bp);
430
431         HWRM_PREP(&req, HWRM_TF, mailbox);
432         /* Build request using the user supplied request payload.
433          * TLV request size is checked at build time against HWRM
434          * request max size, thus no checking required.
435          */
436         req.tf_type = tf_type;
437         req.tf_subtype = tf_subtype;
438         memcpy(req.tf_req, msg, msg_len);
439
440         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), mailbox);
441         HWRM_CHECK_RESULT();
442
443         /* Copy the resp to user provided response buffer */
444         if (response != NULL)
445                 /* Post process response data. We need to copy only
446                  * the 'payload' as the HWRM data structure really is
447                  * HWRM header + msg header + payload and the TFLIB
448                  * only provided a payload place holder.
449                  */
450                 if (response_len != 0) {
451                         memcpy(response,
452                                resp->tf_resp,
453                                response_len);
454                 }
455
456         /* Extract the internal tflib response code */
457         *tf_response_code = resp->tf_resp_code;
458         HWRM_UNLOCK();
459
460         return rc;
461 }
462
463 int bnxt_hwrm_cfa_l2_clear_rx_mask(struct bnxt *bp, struct bnxt_vnic_info *vnic)
464 {
465         int rc = 0;
466         struct hwrm_cfa_l2_set_rx_mask_input req = {.req_type = 0 };
467         struct hwrm_cfa_l2_set_rx_mask_output *resp = bp->hwrm_cmd_resp_addr;
468
469         HWRM_PREP(&req, HWRM_CFA_L2_SET_RX_MASK, BNXT_USE_CHIMP_MB);
470         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
471         req.mask = 0;
472
473         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
474
475         HWRM_CHECK_RESULT();
476         HWRM_UNLOCK();
477
478         return rc;
479 }
480
481 int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp,
482                                  struct bnxt_vnic_info *vnic,
483                                  uint16_t vlan_count,
484                                  struct bnxt_vlan_table_entry *vlan_table)
485 {
486         int rc = 0;
487         struct hwrm_cfa_l2_set_rx_mask_input req = {.req_type = 0 };
488         struct hwrm_cfa_l2_set_rx_mask_output *resp = bp->hwrm_cmd_resp_addr;
489         uint32_t mask = 0;
490
491         if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
492                 return rc;
493
494         HWRM_PREP(&req, HWRM_CFA_L2_SET_RX_MASK, BNXT_USE_CHIMP_MB);
495         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
496
497         if (vnic->flags & BNXT_VNIC_INFO_BCAST)
498                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_BCAST;
499         if (vnic->flags & BNXT_VNIC_INFO_UNTAGGED)
500                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLAN_NONVLAN;
501
502         if (vnic->flags & BNXT_VNIC_INFO_PROMISC)
503                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_PROMISCUOUS;
504
505         if (vnic->flags & BNXT_VNIC_INFO_ALLMULTI) {
506                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ALL_MCAST;
507         } else if (vnic->flags & BNXT_VNIC_INFO_MCAST) {
508                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_MCAST;
509                 req.num_mc_entries = rte_cpu_to_le_32(bp->nb_mc_addr);
510                 req.mc_tbl_addr = rte_cpu_to_le_64(bp->mc_list_dma_addr);
511         }
512         if (vlan_table) {
513                 if (!(mask & HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLAN_NONVLAN))
514                         mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLANONLY;
515                 req.vlan_tag_tbl_addr =
516                         rte_cpu_to_le_64(rte_malloc_virt2iova(vlan_table));
517                 req.num_vlan_tags = rte_cpu_to_le_32((uint32_t)vlan_count);
518         }
519         req.mask = rte_cpu_to_le_32(mask);
520
521         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
522
523         HWRM_CHECK_RESULT();
524         HWRM_UNLOCK();
525
526         return rc;
527 }
528
529 int bnxt_hwrm_cfa_vlan_antispoof_cfg(struct bnxt *bp, uint16_t fid,
530                         uint16_t vlan_count,
531                         struct bnxt_vlan_antispoof_table_entry *vlan_table)
532 {
533         int rc = 0;
534         struct hwrm_cfa_vlan_antispoof_cfg_input req = {.req_type = 0 };
535         struct hwrm_cfa_vlan_antispoof_cfg_output *resp =
536                                                 bp->hwrm_cmd_resp_addr;
537
538         /*
539          * Older HWRM versions did not support this command, and the set_rx_mask
540          * list was used for anti-spoof. In 1.8.0, the TX path configuration was
541          * removed from set_rx_mask call, and this command was added.
542          *
543          * This command is also present from 1.7.8.11 and higher,
544          * as well as 1.7.8.0
545          */
546         if (bp->fw_ver < ((1 << 24) | (8 << 16))) {
547                 if (bp->fw_ver != ((1 << 24) | (7 << 16) | (8 << 8))) {
548                         if (bp->fw_ver < ((1 << 24) | (7 << 16) | (8 << 8) |
549                                         (11)))
550                                 return 0;
551                 }
552         }
553         HWRM_PREP(&req, HWRM_CFA_VLAN_ANTISPOOF_CFG, BNXT_USE_CHIMP_MB);
554         req.fid = rte_cpu_to_le_16(fid);
555
556         req.vlan_tag_mask_tbl_addr =
557                 rte_cpu_to_le_64(rte_malloc_virt2iova(vlan_table));
558         req.num_vlan_entries = rte_cpu_to_le_32((uint32_t)vlan_count);
559
560         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
561
562         HWRM_CHECK_RESULT();
563         HWRM_UNLOCK();
564
565         return rc;
566 }
567
568 int bnxt_hwrm_clear_l2_filter(struct bnxt *bp,
569                              struct bnxt_filter_info *filter)
570 {
571         int rc = 0;
572         struct bnxt_filter_info *l2_filter = filter;
573         struct bnxt_vnic_info *vnic = NULL;
574         struct hwrm_cfa_l2_filter_free_input req = {.req_type = 0 };
575         struct hwrm_cfa_l2_filter_free_output *resp = bp->hwrm_cmd_resp_addr;
576
577         if (filter->fw_l2_filter_id == UINT64_MAX)
578                 return 0;
579
580         if (filter->matching_l2_fltr_ptr)
581                 l2_filter = filter->matching_l2_fltr_ptr;
582
583         PMD_DRV_LOG(DEBUG, "filter: %p l2_filter: %p ref_cnt: %d\n",
584                     filter, l2_filter, l2_filter->l2_ref_cnt);
585
586         if (l2_filter->l2_ref_cnt == 0)
587                 return 0;
588
589         if (l2_filter->l2_ref_cnt > 0)
590                 l2_filter->l2_ref_cnt--;
591
592         if (l2_filter->l2_ref_cnt > 0)
593                 return 0;
594
595         HWRM_PREP(&req, HWRM_CFA_L2_FILTER_FREE, BNXT_USE_CHIMP_MB);
596
597         req.l2_filter_id = rte_cpu_to_le_64(filter->fw_l2_filter_id);
598
599         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
600
601         HWRM_CHECK_RESULT();
602         HWRM_UNLOCK();
603
604         filter->fw_l2_filter_id = UINT64_MAX;
605         if (l2_filter->l2_ref_cnt == 0) {
606                 vnic = l2_filter->vnic;
607                 if (vnic) {
608                         STAILQ_REMOVE(&vnic->filter, l2_filter,
609                                       bnxt_filter_info, next);
610                         bnxt_free_filter(bp, l2_filter);
611                 }
612         }
613
614         return 0;
615 }
616
617 int bnxt_hwrm_set_l2_filter(struct bnxt *bp,
618                          uint16_t dst_id,
619                          struct bnxt_filter_info *filter)
620 {
621         int rc = 0;
622         struct hwrm_cfa_l2_filter_alloc_input req = {.req_type = 0 };
623         struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
624         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
625         const struct rte_eth_vmdq_rx_conf *conf =
626                     &dev_conf->rx_adv_conf.vmdq_rx_conf;
627         uint32_t enables = 0;
628         uint16_t j = dst_id - 1;
629
630         //TODO: Is there a better way to add VLANs to each VNIC in case of VMDQ
631         if ((dev_conf->rxmode.mq_mode & RTE_ETH_MQ_RX_VMDQ_FLAG) &&
632             conf->pool_map[j].pools & (1UL << j)) {
633                 PMD_DRV_LOG(DEBUG,
634                         "Add vlan %u to vmdq pool %u\n",
635                         conf->pool_map[j].vlan_id, j);
636
637                 filter->l2_ivlan = conf->pool_map[j].vlan_id;
638                 filter->enables |=
639                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN |
640                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN_MASK;
641         }
642
643         if (filter->fw_l2_filter_id != UINT64_MAX)
644                 bnxt_hwrm_clear_l2_filter(bp, filter);
645
646         HWRM_PREP(&req, HWRM_CFA_L2_FILTER_ALLOC, BNXT_USE_CHIMP_MB);
647
648         /* PMD does not support XDP and RoCE */
649         filter->flags |= HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_XDP_DISABLE |
650                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_TRAFFIC_L2;
651         req.flags = rte_cpu_to_le_32(filter->flags);
652
653         enables = filter->enables |
654               HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_DST_ID;
655         req.dst_id = rte_cpu_to_le_16(dst_id);
656
657         if (enables &
658             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR)
659                 memcpy(req.l2_addr, filter->l2_addr,
660                        RTE_ETHER_ADDR_LEN);
661         if (enables &
662             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR_MASK)
663                 memcpy(req.l2_addr_mask, filter->l2_addr_mask,
664                        RTE_ETHER_ADDR_LEN);
665         if (enables &
666             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_OVLAN)
667                 req.l2_ovlan = filter->l2_ovlan;
668         if (enables &
669             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN)
670                 req.l2_ivlan = filter->l2_ivlan;
671         if (enables &
672             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_OVLAN_MASK)
673                 req.l2_ovlan_mask = filter->l2_ovlan_mask;
674         if (enables &
675             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN_MASK)
676                 req.l2_ivlan_mask = filter->l2_ivlan_mask;
677         if (enables & HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_SRC_ID)
678                 req.src_id = rte_cpu_to_le_32(filter->src_id);
679         if (enables & HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_SRC_TYPE)
680                 req.src_type = filter->src_type;
681         if (filter->pri_hint) {
682                 req.pri_hint = filter->pri_hint;
683                 req.l2_filter_id_hint =
684                         rte_cpu_to_le_64(filter->l2_filter_id_hint);
685         }
686
687         req.enables = rte_cpu_to_le_32(enables);
688
689         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
690
691         HWRM_CHECK_RESULT();
692
693         filter->fw_l2_filter_id = rte_le_to_cpu_64(resp->l2_filter_id);
694         filter->flow_id = rte_le_to_cpu_32(resp->flow_id);
695         HWRM_UNLOCK();
696
697         filter->l2_ref_cnt++;
698
699         return rc;
700 }
701
702 int bnxt_hwrm_ptp_cfg(struct bnxt *bp)
703 {
704         struct hwrm_port_mac_cfg_input req = {.req_type = 0};
705         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
706         uint32_t flags = 0;
707         int rc;
708
709         if (!ptp)
710                 return 0;
711
712         HWRM_PREP(&req, HWRM_PORT_MAC_CFG, BNXT_USE_CHIMP_MB);
713
714         if (ptp->rx_filter)
715                 flags |= HWRM_PORT_MAC_CFG_INPUT_FLAGS_PTP_RX_TS_CAPTURE_ENABLE;
716         else
717                 flags |=
718                         HWRM_PORT_MAC_CFG_INPUT_FLAGS_PTP_RX_TS_CAPTURE_DISABLE;
719         if (ptp->tx_tstamp_en)
720                 flags |= HWRM_PORT_MAC_CFG_INPUT_FLAGS_PTP_TX_TS_CAPTURE_ENABLE;
721         else
722                 flags |=
723                         HWRM_PORT_MAC_CFG_INPUT_FLAGS_PTP_TX_TS_CAPTURE_DISABLE;
724         req.flags = rte_cpu_to_le_32(flags);
725         req.enables = rte_cpu_to_le_32
726                 (HWRM_PORT_MAC_CFG_INPUT_ENABLES_RX_TS_CAPTURE_PTP_MSG_TYPE);
727         req.rx_ts_capture_ptp_msg_type = rte_cpu_to_le_16(ptp->rxctl);
728
729         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
730         HWRM_UNLOCK();
731
732         return rc;
733 }
734
735 static int bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
736 {
737         int rc = 0;
738         struct hwrm_port_mac_ptp_qcfg_input req = {.req_type = 0};
739         struct hwrm_port_mac_ptp_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
740         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
741
742         if (ptp)
743                 return 0;
744
745         HWRM_PREP(&req, HWRM_PORT_MAC_PTP_QCFG, BNXT_USE_CHIMP_MB);
746
747         req.port_id = rte_cpu_to_le_16(bp->pf->port_id);
748
749         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
750
751         HWRM_CHECK_RESULT();
752
753         if (BNXT_CHIP_P5(bp)) {
754                 if (!(resp->flags & HWRM_PORT_MAC_PTP_QCFG_OUTPUT_FLAGS_HWRM_ACCESS))
755                         return 0;
756         } else {
757                 if (!(resp->flags & HWRM_PORT_MAC_PTP_QCFG_OUTPUT_FLAGS_DIRECT_ACCESS))
758                         return 0;
759         }
760
761         if (resp->flags & HWRM_PORT_MAC_PTP_QCFG_OUTPUT_FLAGS_ONE_STEP_TX_TS)
762                 bp->flags |= BNXT_FLAG_FW_CAP_ONE_STEP_TX_TS;
763
764         ptp = rte_zmalloc("ptp_cfg", sizeof(*ptp), 0);
765         if (!ptp)
766                 return -ENOMEM;
767
768         if (!BNXT_CHIP_P5(bp)) {
769                 ptp->rx_regs[BNXT_PTP_RX_TS_L] =
770                         rte_le_to_cpu_32(resp->rx_ts_reg_off_lower);
771                 ptp->rx_regs[BNXT_PTP_RX_TS_H] =
772                         rte_le_to_cpu_32(resp->rx_ts_reg_off_upper);
773                 ptp->rx_regs[BNXT_PTP_RX_SEQ] =
774                         rte_le_to_cpu_32(resp->rx_ts_reg_off_seq_id);
775                 ptp->rx_regs[BNXT_PTP_RX_FIFO] =
776                         rte_le_to_cpu_32(resp->rx_ts_reg_off_fifo);
777                 ptp->rx_regs[BNXT_PTP_RX_FIFO_ADV] =
778                         rte_le_to_cpu_32(resp->rx_ts_reg_off_fifo_adv);
779                 ptp->tx_regs[BNXT_PTP_TX_TS_L] =
780                         rte_le_to_cpu_32(resp->tx_ts_reg_off_lower);
781                 ptp->tx_regs[BNXT_PTP_TX_TS_H] =
782                         rte_le_to_cpu_32(resp->tx_ts_reg_off_upper);
783                 ptp->tx_regs[BNXT_PTP_TX_SEQ] =
784                         rte_le_to_cpu_32(resp->tx_ts_reg_off_seq_id);
785                 ptp->tx_regs[BNXT_PTP_TX_FIFO] =
786                         rte_le_to_cpu_32(resp->tx_ts_reg_off_fifo);
787         }
788
789         ptp->bp = bp;
790         bp->ptp_cfg = ptp;
791
792         return 0;
793 }
794
795 void bnxt_free_vf_info(struct bnxt *bp)
796 {
797         int i;
798
799         if (bp->pf == NULL)
800                 return;
801
802         if (bp->pf->vf_info == NULL)
803                 return;
804
805         for (i = 0; i < bp->pf->max_vfs; i++) {
806                 rte_free(bp->pf->vf_info[i].vlan_table);
807                 bp->pf->vf_info[i].vlan_table = NULL;
808                 rte_free(bp->pf->vf_info[i].vlan_as_table);
809                 bp->pf->vf_info[i].vlan_as_table = NULL;
810         }
811         rte_free(bp->pf->vf_info);
812         bp->pf->vf_info = NULL;
813 }
814
815 static int bnxt_alloc_vf_info(struct bnxt *bp, uint16_t max_vfs)
816 {
817         struct bnxt_child_vf_info *vf_info = bp->pf->vf_info;
818         int i;
819
820         if (vf_info)
821                 bnxt_free_vf_info(bp);
822
823         vf_info = rte_zmalloc("bnxt_vf_info", sizeof(*vf_info) * max_vfs, 0);
824         if (vf_info == NULL) {
825                 PMD_DRV_LOG(ERR, "Failed to alloc vf info\n");
826                 return -ENOMEM;
827         }
828
829         bp->pf->max_vfs = max_vfs;
830         for (i = 0; i < max_vfs; i++) {
831                 vf_info[i].fid = bp->pf->first_vf_id + i;
832                 vf_info[i].vlan_table = rte_zmalloc("VF VLAN table",
833                                                     getpagesize(), getpagesize());
834                 if (vf_info[i].vlan_table == NULL) {
835                         PMD_DRV_LOG(ERR, "Failed to alloc VLAN table for VF %d\n", i);
836                         goto err;
837                 }
838                 rte_mem_lock_page(vf_info[i].vlan_table);
839
840                 vf_info[i].vlan_as_table = rte_zmalloc("VF VLAN AS table",
841                                                        getpagesize(), getpagesize());
842                 if (vf_info[i].vlan_as_table == NULL) {
843                         PMD_DRV_LOG(ERR, "Failed to alloc VLAN AS table for VF %d\n", i);
844                         goto err;
845                 }
846                 rte_mem_lock_page(vf_info[i].vlan_as_table);
847
848                 STAILQ_INIT(&vf_info[i].filter);
849         }
850
851         bp->pf->vf_info = vf_info;
852
853         return 0;
854 err:
855         bnxt_free_vf_info(bp);
856         return -ENOMEM;
857 }
858
859 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
860 {
861         int rc = 0;
862         struct hwrm_func_qcaps_input req = {.req_type = 0 };
863         struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
864         uint16_t new_max_vfs;
865         uint32_t flags;
866
867         HWRM_PREP(&req, HWRM_FUNC_QCAPS, BNXT_USE_CHIMP_MB);
868
869         req.fid = rte_cpu_to_le_16(0xffff);
870
871         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
872
873         HWRM_CHECK_RESULT();
874
875         bp->max_ring_grps = rte_le_to_cpu_32(resp->max_hw_ring_grps);
876         flags = rte_le_to_cpu_32(resp->flags);
877         if (BNXT_PF(bp)) {
878                 bp->pf->port_id = resp->port_id;
879                 bp->pf->first_vf_id = rte_le_to_cpu_16(resp->first_vf_id);
880                 bp->pf->total_vfs = rte_le_to_cpu_16(resp->max_vfs);
881                 new_max_vfs = bp->pdev->max_vfs;
882                 if (new_max_vfs != bp->pf->max_vfs) {
883                         rc = bnxt_alloc_vf_info(bp, new_max_vfs);
884                         if (rc)
885                                 goto unlock;
886                 }
887         }
888
889         bp->fw_fid = rte_le_to_cpu_32(resp->fid);
890         if (!bnxt_check_zero_bytes(resp->mac_address, RTE_ETHER_ADDR_LEN)) {
891                 bp->flags |= BNXT_FLAG_DFLT_MAC_SET;
892                 memcpy(bp->mac_addr, &resp->mac_address, RTE_ETHER_ADDR_LEN);
893         } else {
894                 bp->flags &= ~BNXT_FLAG_DFLT_MAC_SET;
895         }
896         bp->max_rsscos_ctx = rte_le_to_cpu_16(resp->max_rsscos_ctx);
897         bp->max_cp_rings = rte_le_to_cpu_16(resp->max_cmpl_rings);
898         bp->max_tx_rings = rte_le_to_cpu_16(resp->max_tx_rings);
899         bp->max_rx_rings = rte_le_to_cpu_16(resp->max_rx_rings);
900         bp->first_vf_id = rte_le_to_cpu_16(resp->first_vf_id);
901         bp->max_rx_em_flows = rte_le_to_cpu_16(resp->max_rx_em_flows);
902         bp->max_l2_ctx = rte_le_to_cpu_16(resp->max_l2_ctxs);
903         if (!BNXT_CHIP_P5(bp) && !bp->pdev->max_vfs)
904                 bp->max_l2_ctx += bp->max_rx_em_flows;
905         /* TODO: For now, do not support VMDq/RFS on VFs. */
906         if (BNXT_PF(bp)) {
907                 if (bp->pf->max_vfs)
908                         bp->max_vnics = 1;
909                 else
910                         bp->max_vnics = rte_le_to_cpu_16(resp->max_vnics);
911         } else {
912                 bp->max_vnics = 1;
913         }
914         PMD_DRV_LOG(DEBUG, "Max l2_cntxts is %d vnics is %d\n",
915                     bp->max_l2_ctx, bp->max_vnics);
916         bp->max_stat_ctx = rte_le_to_cpu_16(resp->max_stat_ctx);
917         bp->max_mcast_addr = rte_le_to_cpu_32(resp->max_mcast_filters);
918
919         if (BNXT_PF(bp)) {
920                 bp->pf->total_vnics = rte_le_to_cpu_16(resp->max_vnics);
921                 if (flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_PTP_SUPPORTED) {
922                         bp->flags |= BNXT_FLAG_PTP_SUPPORTED;
923                         PMD_DRV_LOG(DEBUG, "PTP SUPPORTED\n");
924                         HWRM_UNLOCK();
925                         bnxt_hwrm_ptp_qcfg(bp);
926                 }
927         }
928
929         if (flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_EXT_STATS_SUPPORTED)
930                 bp->flags |= BNXT_FLAG_EXT_STATS_SUPPORTED;
931
932         if (flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_ERROR_RECOVERY_CAPABLE) {
933                 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
934                 PMD_DRV_LOG(DEBUG, "Adapter Error recovery SUPPORTED\n");
935         }
936
937         if (flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_ERR_RECOVER_RELOAD)
938                 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
939
940         if (flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_HOT_RESET_CAPABLE)
941                 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
942
943         if (flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
944                 bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
945
946         if (!(flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_VLAN_ACCELERATION_TX_DISABLED)) {
947                 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
948                 PMD_DRV_LOG(DEBUG, "VLAN acceleration for TX is enabled\n");
949         }
950 unlock:
951         HWRM_UNLOCK();
952
953         return rc;
954 }
955
956 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
957 {
958         int rc;
959
960         rc = __bnxt_hwrm_func_qcaps(bp);
961         if (rc == -ENOMEM)
962                 return rc;
963
964         if (!rc && bp->hwrm_spec_code >= HWRM_SPEC_CODE_1_8_3) {
965                 rc = bnxt_alloc_ctx_mem(bp);
966                 if (rc)
967                         return rc;
968
969                 /* On older FW,
970                  * bnxt_hwrm_func_resc_qcaps can fail and cause init failure.
971                  * But the error can be ignored. Return success.
972                  */
973                 rc = bnxt_hwrm_func_resc_qcaps(bp);
974                 if (!rc)
975                         bp->flags |= BNXT_FLAG_NEW_RM;
976         }
977
978         return 0;
979 }
980
981 /* VNIC cap covers capability of all VNICs. So no need to pass vnic_id */
982 int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
983 {
984         int rc = 0;
985         uint32_t flags;
986         struct hwrm_vnic_qcaps_input req = {.req_type = 0 };
987         struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
988
989         HWRM_PREP(&req, HWRM_VNIC_QCAPS, BNXT_USE_CHIMP_MB);
990
991         req.target_id = rte_cpu_to_le_16(0xffff);
992
993         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
994
995         HWRM_CHECK_RESULT();
996
997         flags = rte_le_to_cpu_32(resp->flags);
998
999         if (flags & HWRM_VNIC_QCAPS_OUTPUT_FLAGS_COS_ASSIGNMENT_CAP) {
1000                 bp->vnic_cap_flags |= BNXT_VNIC_CAP_COS_CLASSIFY;
1001                 PMD_DRV_LOG(INFO, "CoS assignment capability enabled\n");
1002         }
1003
1004         if (flags & HWRM_VNIC_QCAPS_OUTPUT_FLAGS_OUTERMOST_RSS_CAP)
1005                 bp->vnic_cap_flags |= BNXT_VNIC_CAP_OUTER_RSS;
1006
1007         if (flags & HWRM_VNIC_QCAPS_OUTPUT_FLAGS_RX_CMPL_V2_CAP)
1008                 bp->vnic_cap_flags |= BNXT_VNIC_CAP_RX_CMPL_V2;
1009
1010         if (flags & HWRM_VNIC_QCAPS_OUTPUT_FLAGS_VLAN_STRIP_CAP) {
1011                 bp->vnic_cap_flags |= BNXT_VNIC_CAP_VLAN_RX_STRIP;
1012                 PMD_DRV_LOG(DEBUG, "Rx VLAN strip capability enabled\n");
1013         }
1014
1015         bp->max_tpa_v2 = rte_le_to_cpu_16(resp->max_aggs_supported);
1016
1017         HWRM_UNLOCK();
1018
1019         return rc;
1020 }
1021
1022 int bnxt_hwrm_func_reset(struct bnxt *bp)
1023 {
1024         int rc = 0;
1025         struct hwrm_func_reset_input req = {.req_type = 0 };
1026         struct hwrm_func_reset_output *resp = bp->hwrm_cmd_resp_addr;
1027
1028         HWRM_PREP(&req, HWRM_FUNC_RESET, BNXT_USE_CHIMP_MB);
1029
1030         req.enables = rte_cpu_to_le_32(0);
1031
1032         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1033
1034         HWRM_CHECK_RESULT();
1035         HWRM_UNLOCK();
1036
1037         return rc;
1038 }
1039
1040 int bnxt_hwrm_func_driver_register(struct bnxt *bp)
1041 {
1042         int rc;
1043         uint32_t flags = 0;
1044         struct hwrm_func_drv_rgtr_input req = {.req_type = 0 };
1045         struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
1046
1047         if (bp->flags & BNXT_FLAG_REGISTERED)
1048                 return 0;
1049
1050         if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
1051                 flags = HWRM_FUNC_DRV_RGTR_INPUT_FLAGS_HOT_RESET_SUPPORT;
1052         if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
1053                 flags |= HWRM_FUNC_DRV_RGTR_INPUT_FLAGS_ERROR_RECOVERY_SUPPORT;
1054
1055         /* PFs and trusted VFs should indicate the support of the
1056          * Master capability on non Stingray platform
1057          */
1058         if ((BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp)) && !BNXT_STINGRAY(bp))
1059                 flags |= HWRM_FUNC_DRV_RGTR_INPUT_FLAGS_MASTER_SUPPORT;
1060
1061         HWRM_PREP(&req, HWRM_FUNC_DRV_RGTR, BNXT_USE_CHIMP_MB);
1062         req.enables = rte_cpu_to_le_32(HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_VER |
1063                         HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_ASYNC_EVENT_FWD);
1064         req.ver_maj_8b = RTE_VER_YEAR;
1065         req.ver_min_8b = RTE_VER_MONTH;
1066         req.ver_upd_8b = RTE_VER_MINOR;
1067
1068         if (BNXT_PF(bp)) {
1069                 req.enables |= rte_cpu_to_le_32(
1070                         HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_VF_REQ_FWD);
1071                 memcpy(req.vf_req_fwd, bp->pf->vf_req_fwd,
1072                        RTE_MIN(sizeof(req.vf_req_fwd),
1073                                sizeof(bp->pf->vf_req_fwd)));
1074         }
1075
1076         req.flags = rte_cpu_to_le_32(flags);
1077
1078         req.async_event_fwd[0] |=
1079                 rte_cpu_to_le_32(ASYNC_CMPL_EVENT_ID_LINK_STATUS_CHANGE |
1080                                  ASYNC_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED |
1081                                  ASYNC_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE |
1082                                  ASYNC_CMPL_EVENT_ID_LINK_SPEED_CHANGE |
1083                                  ASYNC_CMPL_EVENT_ID_RESET_NOTIFY);
1084         if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
1085                 req.async_event_fwd[0] |=
1086                         rte_cpu_to_le_32(ASYNC_CMPL_EVENT_ID_ERROR_RECOVERY);
1087         req.async_event_fwd[1] |=
1088                 rte_cpu_to_le_32(ASYNC_CMPL_EVENT_ID_PF_DRVR_UNLOAD |
1089                                  ASYNC_CMPL_EVENT_ID_VF_CFG_CHANGE);
1090         if (BNXT_PF(bp))
1091                 req.async_event_fwd[1] |=
1092                         rte_cpu_to_le_32(ASYNC_CMPL_EVENT_ID_DBG_NOTIFICATION);
1093
1094         if (BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp))
1095                 req.async_event_fwd[1] |=
1096                 rte_cpu_to_le_32(ASYNC_CMPL_EVENT_ID_DEFAULT_VNIC_CHANGE);
1097
1098         req.async_event_fwd[2] |=
1099                 rte_cpu_to_le_32(ASYNC_CMPL_EVENT_ID_ECHO_REQUEST |
1100                                  ASYNC_CMPL_EVENT_ID_ERROR_REPORT);
1101
1102         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1103
1104         HWRM_CHECK_RESULT();
1105
1106         flags = rte_le_to_cpu_32(resp->flags);
1107         if (flags & HWRM_FUNC_DRV_RGTR_OUTPUT_FLAGS_IF_CHANGE_SUPPORTED)
1108                 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
1109
1110         HWRM_UNLOCK();
1111
1112         bp->flags |= BNXT_FLAG_REGISTERED;
1113
1114         return rc;
1115 }
1116
1117 int bnxt_hwrm_check_vf_rings(struct bnxt *bp)
1118 {
1119         if (!(BNXT_VF(bp) && (bp->flags & BNXT_FLAG_NEW_RM)))
1120                 return 0;
1121
1122         return bnxt_hwrm_func_reserve_vf_resc(bp, true);
1123 }
1124
1125 int bnxt_hwrm_func_reserve_vf_resc(struct bnxt *bp, bool test)
1126 {
1127         int rc;
1128         uint32_t flags = 0;
1129         uint32_t enables;
1130         struct hwrm_func_vf_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1131         struct hwrm_func_vf_cfg_input req = {0};
1132
1133         HWRM_PREP(&req, HWRM_FUNC_VF_CFG, BNXT_USE_CHIMP_MB);
1134
1135         enables = HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_RX_RINGS  |
1136                   HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_TX_RINGS   |
1137                   HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_STAT_CTXS  |
1138                   HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_CMPL_RINGS |
1139                   HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_VNICS;
1140
1141         if (BNXT_HAS_RING_GRPS(bp)) {
1142                 enables |= HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_HW_RING_GRPS;
1143                 req.num_hw_ring_grps = rte_cpu_to_le_16(bp->rx_nr_rings);
1144         }
1145
1146         req.num_tx_rings = rte_cpu_to_le_16(bp->tx_nr_rings);
1147         req.num_rx_rings = rte_cpu_to_le_16(bp->rx_nr_rings *
1148                                             AGG_RING_MULTIPLIER);
1149         req.num_stat_ctxs = rte_cpu_to_le_16(bp->rx_nr_rings + bp->tx_nr_rings);
1150         req.num_cmpl_rings = rte_cpu_to_le_16(bp->rx_nr_rings +
1151                                               bp->tx_nr_rings +
1152                                               BNXT_NUM_ASYNC_CPR(bp));
1153         req.num_vnics = rte_cpu_to_le_16(bp->rx_nr_rings);
1154         if (bp->vf_resv_strategy ==
1155             HWRM_FUNC_RESOURCE_QCAPS_OUTPUT_VF_RESV_STRATEGY_MINIMAL_STATIC) {
1156                 enables |= HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_VNICS |
1157                            HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_L2_CTXS |
1158                            HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_RSSCOS_CTXS;
1159                 req.num_rsscos_ctxs = rte_cpu_to_le_16(BNXT_VF_RSV_NUM_RSS_CTX);
1160                 req.num_l2_ctxs = rte_cpu_to_le_16(BNXT_VF_RSV_NUM_L2_CTX);
1161                 req.num_vnics = rte_cpu_to_le_16(BNXT_VF_RSV_NUM_VNIC);
1162         } else if (bp->vf_resv_strategy ==
1163                    HWRM_FUNC_RESOURCE_QCAPS_OUTPUT_VF_RESV_STRATEGY_MAXIMAL) {
1164                 enables |= HWRM_FUNC_VF_CFG_INPUT_ENABLES_NUM_RSSCOS_CTXS;
1165                 req.num_rsscos_ctxs = rte_cpu_to_le_16(bp->max_rsscos_ctx);
1166         }
1167
1168         if (test)
1169                 flags = HWRM_FUNC_VF_CFG_INPUT_FLAGS_TX_ASSETS_TEST |
1170                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_RX_ASSETS_TEST |
1171                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_CMPL_ASSETS_TEST |
1172                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_RING_GRP_ASSETS_TEST |
1173                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_STAT_CTX_ASSETS_TEST |
1174                         HWRM_FUNC_VF_CFG_INPUT_FLAGS_VNIC_ASSETS_TEST;
1175
1176         if (test && BNXT_HAS_RING_GRPS(bp))
1177                 flags |= HWRM_FUNC_VF_CFG_INPUT_FLAGS_RING_GRP_ASSETS_TEST;
1178
1179         req.flags = rte_cpu_to_le_32(flags);
1180         req.enables |= rte_cpu_to_le_32(enables);
1181
1182         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1183
1184         if (test)
1185                 HWRM_CHECK_RESULT_SILENT();
1186         else
1187                 HWRM_CHECK_RESULT();
1188
1189         HWRM_UNLOCK();
1190         return rc;
1191 }
1192
1193 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp)
1194 {
1195         int rc;
1196         struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
1197         struct hwrm_func_resource_qcaps_input req = {0};
1198
1199         HWRM_PREP(&req, HWRM_FUNC_RESOURCE_QCAPS, BNXT_USE_CHIMP_MB);
1200         req.fid = rte_cpu_to_le_16(0xffff);
1201
1202         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1203
1204         HWRM_CHECK_RESULT_SILENT();
1205
1206         bp->max_rsscos_ctx = rte_le_to_cpu_16(resp->max_rsscos_ctx);
1207         bp->max_cp_rings = rte_le_to_cpu_16(resp->max_cmpl_rings);
1208         bp->max_tx_rings = rte_le_to_cpu_16(resp->max_tx_rings);
1209         bp->max_rx_rings = rte_le_to_cpu_16(resp->max_rx_rings);
1210         bp->max_ring_grps = rte_le_to_cpu_32(resp->max_hw_ring_grps);
1211         /* func_resource_qcaps does not return max_rx_em_flows.
1212          * So use the value provided by func_qcaps.
1213          */
1214         bp->max_l2_ctx = rte_le_to_cpu_16(resp->max_l2_ctxs);
1215         if (!BNXT_CHIP_P5(bp) && !bp->pdev->max_vfs)
1216                 bp->max_l2_ctx += bp->max_rx_em_flows;
1217         bp->max_vnics = rte_le_to_cpu_16(resp->max_vnics);
1218         bp->max_stat_ctx = rte_le_to_cpu_16(resp->max_stat_ctx);
1219         bp->max_nq_rings = rte_le_to_cpu_16(resp->max_msix);
1220         bp->vf_resv_strategy = rte_le_to_cpu_16(resp->vf_reservation_strategy);
1221         if (bp->vf_resv_strategy >
1222             HWRM_FUNC_RESOURCE_QCAPS_OUTPUT_VF_RESV_STRATEGY_MINIMAL_STATIC)
1223                 bp->vf_resv_strategy =
1224                 HWRM_FUNC_RESOURCE_QCAPS_OUTPUT_VF_RESERVATION_STRATEGY_MAXIMAL;
1225
1226         HWRM_UNLOCK();
1227         return rc;
1228 }
1229
1230 int bnxt_hwrm_ver_get(struct bnxt *bp, uint32_t timeout)
1231 {
1232         int rc = 0;
1233         struct hwrm_ver_get_input req = {.req_type = 0 };
1234         struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
1235         uint32_t fw_version;
1236         uint16_t max_resp_len;
1237         char type[RTE_MEMZONE_NAMESIZE];
1238         uint32_t dev_caps_cfg;
1239
1240         bp->max_req_len = HWRM_MAX_REQ_LEN;
1241         bp->hwrm_cmd_timeout = timeout;
1242         HWRM_PREP(&req, HWRM_VER_GET, BNXT_USE_CHIMP_MB);
1243
1244         req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
1245         req.hwrm_intf_min = HWRM_VERSION_MINOR;
1246         req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
1247
1248         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1249
1250         if (bp->flags & BNXT_FLAG_FW_RESET)
1251                 HWRM_CHECK_RESULT_SILENT();
1252         else
1253                 HWRM_CHECK_RESULT();
1254
1255         if (resp->flags & HWRM_VER_GET_OUTPUT_FLAGS_DEV_NOT_RDY) {
1256                 rc = -EAGAIN;
1257                 goto error;
1258         }
1259
1260         PMD_DRV_LOG(INFO, "%d.%d.%d:%d.%d.%d.%d\n",
1261                 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
1262                 resp->hwrm_intf_upd_8b, resp->hwrm_fw_maj_8b,
1263                 resp->hwrm_fw_min_8b, resp->hwrm_fw_bld_8b,
1264                 resp->hwrm_fw_rsvd_8b);
1265         bp->fw_ver = ((uint32_t)resp->hwrm_fw_maj_8b << 24) |
1266                      ((uint32_t)resp->hwrm_fw_min_8b << 16) |
1267                      ((uint32_t)resp->hwrm_fw_bld_8b << 8) |
1268                      resp->hwrm_fw_rsvd_8b;
1269         PMD_DRV_LOG(INFO, "Driver HWRM version: %d.%d.%d\n",
1270                 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, HWRM_VERSION_UPDATE);
1271
1272         fw_version = resp->hwrm_intf_maj_8b << 16;
1273         fw_version |= resp->hwrm_intf_min_8b << 8;
1274         fw_version |= resp->hwrm_intf_upd_8b;
1275         bp->hwrm_spec_code = fw_version;
1276
1277         /* def_req_timeout value is in milliseconds */
1278         bp->hwrm_cmd_timeout = rte_le_to_cpu_16(resp->def_req_timeout);
1279         /* convert timeout to usec */
1280         bp->hwrm_cmd_timeout *= 1000;
1281         if (!bp->hwrm_cmd_timeout)
1282                 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
1283
1284         if (resp->hwrm_intf_maj_8b != HWRM_VERSION_MAJOR) {
1285                 PMD_DRV_LOG(ERR, "Unsupported firmware API version\n");
1286                 rc = -EINVAL;
1287                 goto error;
1288         }
1289
1290         if (bp->max_req_len > resp->max_req_win_len) {
1291                 PMD_DRV_LOG(ERR, "Unsupported request length\n");
1292                 rc = -EINVAL;
1293                 goto error;
1294         }
1295
1296         bp->chip_num = rte_le_to_cpu_16(resp->chip_num);
1297
1298         bp->max_req_len = rte_le_to_cpu_16(resp->max_req_win_len);
1299         bp->hwrm_max_ext_req_len = rte_le_to_cpu_16(resp->max_ext_req_len);
1300         if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
1301                 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
1302
1303         max_resp_len = rte_le_to_cpu_16(resp->max_resp_len);
1304         dev_caps_cfg = rte_le_to_cpu_32(resp->dev_caps_cfg);
1305
1306         RTE_VERIFY(max_resp_len <= bp->max_resp_len);
1307         bp->max_resp_len = max_resp_len;
1308
1309         if ((dev_caps_cfg &
1310                 HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
1311             (dev_caps_cfg &
1312              HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) {
1313                 PMD_DRV_LOG(DEBUG, "Short command supported\n");
1314                 bp->flags |= BNXT_FLAG_SHORT_CMD;
1315         }
1316
1317         if (((dev_caps_cfg &
1318               HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
1319              (dev_caps_cfg &
1320               HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) ||
1321             bp->hwrm_max_ext_req_len > HWRM_MAX_REQ_LEN) {
1322                 sprintf(type, "bnxt_hwrm_short_" PCI_PRI_FMT,
1323                         bp->pdev->addr.domain, bp->pdev->addr.bus,
1324                         bp->pdev->addr.devid, bp->pdev->addr.function);
1325
1326                 rte_free(bp->hwrm_short_cmd_req_addr);
1327
1328                 bp->hwrm_short_cmd_req_addr =
1329                                 rte_malloc(type, bp->hwrm_max_ext_req_len, 0);
1330                 if (bp->hwrm_short_cmd_req_addr == NULL) {
1331                         rc = -ENOMEM;
1332                         goto error;
1333                 }
1334                 bp->hwrm_short_cmd_req_dma_addr =
1335                         rte_malloc_virt2iova(bp->hwrm_short_cmd_req_addr);
1336                 if (bp->hwrm_short_cmd_req_dma_addr == RTE_BAD_IOVA) {
1337                         rte_free(bp->hwrm_short_cmd_req_addr);
1338                         PMD_DRV_LOG(ERR,
1339                                 "Unable to map buffer to physical memory.\n");
1340                         rc = -ENOMEM;
1341                         goto error;
1342                 }
1343         }
1344         if (dev_caps_cfg &
1345             HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED) {
1346                 bp->flags |= BNXT_FLAG_KONG_MB_EN;
1347                 PMD_DRV_LOG(DEBUG, "Kong mailbox channel enabled\n");
1348         }
1349         if (dev_caps_cfg &
1350             HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
1351                 PMD_DRV_LOG(DEBUG, "FW supports Trusted VFs\n");
1352         if (dev_caps_cfg &
1353             HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED) {
1354                 bp->fw_cap |= BNXT_FW_CAP_ADV_FLOW_MGMT;
1355                 PMD_DRV_LOG(DEBUG, "FW supports advanced flow management\n");
1356         }
1357
1358         if (dev_caps_cfg &
1359             HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_ADV_FLOW_COUNTERS_SUPPORTED) {
1360                 PMD_DRV_LOG(DEBUG, "FW supports advanced flow counters\n");
1361                 bp->fw_cap |= BNXT_FW_CAP_ADV_FLOW_COUNTERS;
1362         }
1363
1364         if (dev_caps_cfg &
1365             HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_CFA_TRUFLOW_SUPPORTED) {
1366                 PMD_DRV_LOG(DEBUG, "Host-based truflow feature enabled.\n");
1367                 bp->fw_cap |= BNXT_FW_CAP_TRUFLOW_EN;
1368         }
1369
1370 error:
1371         HWRM_UNLOCK();
1372         return rc;
1373 }
1374
1375 int bnxt_hwrm_func_driver_unregister(struct bnxt *bp)
1376 {
1377         int rc;
1378         struct hwrm_func_drv_unrgtr_input req = {.req_type = 0 };
1379         struct hwrm_func_drv_unrgtr_output *resp = bp->hwrm_cmd_resp_addr;
1380
1381         if (!(bp->flags & BNXT_FLAG_REGISTERED))
1382                 return 0;
1383
1384         HWRM_PREP(&req, HWRM_FUNC_DRV_UNRGTR, BNXT_USE_CHIMP_MB);
1385
1386         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1387
1388         HWRM_CHECK_RESULT();
1389         HWRM_UNLOCK();
1390
1391         PMD_DRV_LOG(DEBUG, "Port %u: Unregistered with fw\n",
1392                     bp->eth_dev->data->port_id);
1393
1394         return rc;
1395 }
1396
1397 static int bnxt_hwrm_port_phy_cfg(struct bnxt *bp, struct bnxt_link_info *conf)
1398 {
1399         int rc = 0;
1400         struct hwrm_port_phy_cfg_input req = {0};
1401         struct hwrm_port_phy_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1402         uint32_t enables = 0;
1403
1404         HWRM_PREP(&req, HWRM_PORT_PHY_CFG, BNXT_USE_CHIMP_MB);
1405
1406         if (conf->link_up) {
1407                 /* Setting Fixed Speed. But AutoNeg is ON, So disable it */
1408                 if (bp->link_info->auto_mode && conf->link_speed) {
1409                         req.auto_mode = HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_NONE;
1410                         PMD_DRV_LOG(DEBUG, "Disabling AutoNeg\n");
1411                 }
1412
1413                 req.flags = rte_cpu_to_le_32(conf->phy_flags);
1414                 /*
1415                  * Note, ChiMP FW 20.2.1 and 20.2.2 return an error when we set
1416                  * any auto mode, even "none".
1417                  */
1418                 if (!conf->link_speed) {
1419                         /* No speeds specified. Enable AutoNeg - all speeds */
1420                         enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_MODE;
1421                         req.auto_mode =
1422                                 HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_ALL_SPEEDS;
1423                 } else {
1424                         if (bp->link_info->link_signal_mode) {
1425                                 enables |=
1426                                 HWRM_PORT_PHY_CFG_IN_EN_FORCE_PAM4_LINK_SPEED;
1427                                 req.force_pam4_link_speed =
1428                                         rte_cpu_to_le_16(conf->link_speed);
1429                         } else {
1430                                 req.force_link_speed =
1431                                         rte_cpu_to_le_16(conf->link_speed);
1432                         }
1433                 }
1434                 /* AutoNeg - Advertise speeds specified. */
1435                 if (conf->auto_link_speed_mask &&
1436                     !(conf->phy_flags & HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE)) {
1437                         req.auto_mode =
1438                                 HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_SPEED_MASK;
1439                         if (conf->auto_pam4_link_speed_mask &&
1440                             bp->link_info->link_signal_mode) {
1441                                 enables |=
1442                                 HWRM_PORT_PHY_CFG_IN_EN_AUTO_PAM4_LINK_SPD_MASK;
1443                                 req.auto_link_pam4_speed_mask =
1444                                 rte_cpu_to_le_16(conf->auto_pam4_link_speed_mask);
1445                         } else {
1446                                 enables |=
1447                                 HWRM_PORT_PHY_CFG_IN_EN_AUTO_LINK_SPEED_MASK;
1448                                 req.auto_link_speed_mask =
1449                                 rte_cpu_to_le_16(conf->auto_link_speed_mask);
1450                         }
1451                 }
1452                 if (conf->auto_link_speed &&
1453                 !(conf->phy_flags & HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE))
1454                         enables |=
1455                                 HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_LINK_SPEED;
1456
1457                 req.auto_duplex = conf->duplex;
1458                 enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_DUPLEX;
1459                 req.auto_pause = conf->auto_pause;
1460                 req.force_pause = conf->force_pause;
1461                 /* Set force_pause if there is no auto or if there is a force */
1462                 if (req.auto_pause && !req.force_pause)
1463                         enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_PAUSE;
1464                 else
1465                         enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_FORCE_PAUSE;
1466
1467                 req.enables = rte_cpu_to_le_32(enables);
1468         } else {
1469                 req.flags =
1470                 rte_cpu_to_le_32(HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE_LINK_DWN);
1471                 PMD_DRV_LOG(INFO, "Force Link Down\n");
1472         }
1473
1474         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1475
1476         HWRM_CHECK_RESULT();
1477         HWRM_UNLOCK();
1478
1479         return rc;
1480 }
1481
1482 static int bnxt_hwrm_port_phy_qcfg(struct bnxt *bp,
1483                                    struct bnxt_link_info *link_info)
1484 {
1485         int rc = 0;
1486         struct hwrm_port_phy_qcfg_input req = {0};
1487         struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
1488
1489         HWRM_PREP(&req, HWRM_PORT_PHY_QCFG, BNXT_USE_CHIMP_MB);
1490
1491         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1492
1493         HWRM_CHECK_RESULT();
1494
1495         link_info->phy_link_status = resp->link;
1496         link_info->link_up =
1497                 (link_info->phy_link_status ==
1498                  HWRM_PORT_PHY_QCFG_OUTPUT_LINK_LINK) ? 1 : 0;
1499         link_info->link_speed = rte_le_to_cpu_16(resp->link_speed);
1500         link_info->duplex = resp->duplex_cfg;
1501         link_info->pause = resp->pause;
1502         link_info->auto_pause = resp->auto_pause;
1503         link_info->force_pause = resp->force_pause;
1504         link_info->auto_mode = resp->auto_mode;
1505         link_info->phy_type = resp->phy_type;
1506         link_info->media_type = resp->media_type;
1507
1508         link_info->support_speeds = rte_le_to_cpu_16(resp->support_speeds);
1509         link_info->auto_link_speed = rte_le_to_cpu_16(resp->auto_link_speed);
1510         link_info->auto_link_speed_mask = rte_le_to_cpu_16(resp->auto_link_speed_mask);
1511         link_info->preemphasis = rte_le_to_cpu_32(resp->preemphasis);
1512         link_info->force_link_speed = rte_le_to_cpu_16(resp->force_link_speed);
1513         link_info->phy_ver[0] = resp->phy_maj;
1514         link_info->phy_ver[1] = resp->phy_min;
1515         link_info->phy_ver[2] = resp->phy_bld;
1516         link_info->link_signal_mode =
1517                 rte_le_to_cpu_16(resp->active_fec_signal_mode);
1518         link_info->force_pam4_link_speed =
1519                         rte_le_to_cpu_16(resp->force_pam4_link_speed);
1520         link_info->support_pam4_speeds =
1521                         rte_le_to_cpu_16(resp->support_pam4_speeds);
1522         link_info->auto_pam4_link_speed_mask =
1523                         rte_le_to_cpu_16(resp->auto_pam4_link_speed_mask);
1524         link_info->module_status = resp->module_status;
1525         HWRM_UNLOCK();
1526
1527         PMD_DRV_LOG(DEBUG, "Link Speed:%d,Auto:%d:%x:%x,Support:%x,Force:%x\n",
1528                     link_info->link_speed, link_info->auto_mode,
1529                     link_info->auto_link_speed, link_info->auto_link_speed_mask,
1530                     link_info->support_speeds, link_info->force_link_speed);
1531         PMD_DRV_LOG(DEBUG, "Link Signal:%d,PAM::Auto:%x,Support:%x,Force:%x\n",
1532                     link_info->link_signal_mode,
1533                     link_info->auto_pam4_link_speed_mask,
1534                     link_info->support_pam4_speeds,
1535                     link_info->force_pam4_link_speed);
1536         return rc;
1537 }
1538
1539 int bnxt_hwrm_port_phy_qcaps(struct bnxt *bp)
1540 {
1541         int rc = 0;
1542         struct hwrm_port_phy_qcaps_input req = {0};
1543         struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
1544         struct bnxt_link_info *link_info = bp->link_info;
1545
1546         if (BNXT_VF(bp) && !BNXT_VF_IS_TRUSTED(bp))
1547                 return 0;
1548
1549         HWRM_PREP(&req, HWRM_PORT_PHY_QCAPS, BNXT_USE_CHIMP_MB);
1550
1551         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1552
1553         HWRM_CHECK_RESULT_SILENT();
1554
1555         bp->port_cnt = resp->port_cnt;
1556         if (resp->supported_speeds_auto_mode)
1557                 link_info->support_auto_speeds =
1558                         rte_le_to_cpu_16(resp->supported_speeds_auto_mode);
1559         if (resp->supported_pam4_speeds_auto_mode)
1560                 link_info->support_pam4_auto_speeds =
1561                         rte_le_to_cpu_16(resp->supported_pam4_speeds_auto_mode);
1562
1563         HWRM_UNLOCK();
1564
1565         /* Older firmware does not have supported_auto_speeds, so assume
1566          * that all supported speeds can be autonegotiated.
1567          */
1568         if (link_info->auto_link_speed_mask && !link_info->support_auto_speeds)
1569                 link_info->support_auto_speeds = link_info->support_speeds;
1570
1571         return 0;
1572 }
1573
1574 static bool bnxt_find_lossy_profile(struct bnxt *bp)
1575 {
1576         int i = 0;
1577
1578         for (i = BNXT_COS_QUEUE_COUNT - 1; i >= 0; i--) {
1579                 if (bp->tx_cos_queue[i].profile ==
1580                     HWRM_QUEUE_SERVICE_PROFILE_LOSSY) {
1581                         bp->tx_cosq_id[0] = bp->tx_cos_queue[i].id;
1582                         return true;
1583                 }
1584         }
1585         return false;
1586 }
1587
1588 static void bnxt_find_first_valid_profile(struct bnxt *bp)
1589 {
1590         int i = 0;
1591
1592         for (i = BNXT_COS_QUEUE_COUNT - 1; i >= 0; i--) {
1593                 if (bp->tx_cos_queue[i].profile !=
1594                     HWRM_QUEUE_SERVICE_PROFILE_UNKNOWN &&
1595                     bp->tx_cos_queue[i].id !=
1596                     HWRM_QUEUE_SERVICE_PROFILE_UNKNOWN) {
1597                         bp->tx_cosq_id[0] = bp->tx_cos_queue[i].id;
1598                         break;
1599                 }
1600         }
1601 }
1602
1603 int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
1604 {
1605         int rc = 0;
1606         struct hwrm_queue_qportcfg_input req = {.req_type = 0 };
1607         struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
1608         uint32_t dir = HWRM_QUEUE_QPORTCFG_INPUT_FLAGS_PATH_TX;
1609         int i;
1610
1611 get_rx_info:
1612         HWRM_PREP(&req, HWRM_QUEUE_QPORTCFG, BNXT_USE_CHIMP_MB);
1613
1614         req.flags = rte_cpu_to_le_32(dir);
1615         /* HWRM Version >= 1.9.1 only if COS Classification is not required. */
1616         if (bp->hwrm_spec_code >= HWRM_VERSION_1_9_1 &&
1617             !(bp->vnic_cap_flags & BNXT_VNIC_CAP_COS_CLASSIFY))
1618                 req.drv_qmap_cap =
1619                         HWRM_QUEUE_QPORTCFG_INPUT_DRV_QMAP_CAP_ENABLED;
1620         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1621
1622         HWRM_CHECK_RESULT();
1623
1624         if (dir == HWRM_QUEUE_QPORTCFG_INPUT_FLAGS_PATH_TX) {
1625                 GET_TX_QUEUE_INFO(0);
1626                 GET_TX_QUEUE_INFO(1);
1627                 GET_TX_QUEUE_INFO(2);
1628                 GET_TX_QUEUE_INFO(3);
1629                 GET_TX_QUEUE_INFO(4);
1630                 GET_TX_QUEUE_INFO(5);
1631                 GET_TX_QUEUE_INFO(6);
1632                 GET_TX_QUEUE_INFO(7);
1633         } else  {
1634                 GET_RX_QUEUE_INFO(0);
1635                 GET_RX_QUEUE_INFO(1);
1636                 GET_RX_QUEUE_INFO(2);
1637                 GET_RX_QUEUE_INFO(3);
1638                 GET_RX_QUEUE_INFO(4);
1639                 GET_RX_QUEUE_INFO(5);
1640                 GET_RX_QUEUE_INFO(6);
1641                 GET_RX_QUEUE_INFO(7);
1642         }
1643
1644         HWRM_UNLOCK();
1645
1646         if (dir == HWRM_QUEUE_QPORTCFG_INPUT_FLAGS_PATH_RX)
1647                 goto done;
1648
1649         if (bp->hwrm_spec_code < HWRM_VERSION_1_9_1) {
1650                 bp->tx_cosq_id[0] = bp->tx_cos_queue[0].id;
1651         } else {
1652                 int j;
1653
1654                 /* iterate and find the COSq profile to use for Tx */
1655                 if (bp->vnic_cap_flags & BNXT_VNIC_CAP_COS_CLASSIFY) {
1656                         for (j = 0, i = 0; i < BNXT_COS_QUEUE_COUNT; i++) {
1657                                 if (bp->tx_cos_queue[i].id != 0xff)
1658                                         bp->tx_cosq_id[j++] =
1659                                                 bp->tx_cos_queue[i].id;
1660                         }
1661                 } else {
1662                         /* When CoS classification is disabled, for normal NIC
1663                          * operations, ideally we should look to use LOSSY.
1664                          * If not found, fallback to the first valid profile
1665                          */
1666                         if (!bnxt_find_lossy_profile(bp))
1667                                 bnxt_find_first_valid_profile(bp);
1668
1669                 }
1670         }
1671
1672         bp->max_tc = resp->max_configurable_queues;
1673         bp->max_lltc = resp->max_configurable_lossless_queues;
1674         if (bp->max_tc > BNXT_MAX_QUEUE)
1675                 bp->max_tc = BNXT_MAX_QUEUE;
1676         bp->max_q = bp->max_tc;
1677
1678         if (dir == HWRM_QUEUE_QPORTCFG_INPUT_FLAGS_PATH_TX) {
1679                 dir = HWRM_QUEUE_QPORTCFG_INPUT_FLAGS_PATH_RX;
1680                 goto get_rx_info;
1681         }
1682
1683 done:
1684         return rc;
1685 }
1686
1687 int bnxt_hwrm_ring_alloc(struct bnxt *bp,
1688                          struct bnxt_ring *ring,
1689                          uint32_t ring_type, uint32_t map_index,
1690                          uint32_t stats_ctx_id, uint32_t cmpl_ring_id,
1691                          uint16_t tx_cosq_id)
1692 {
1693         int rc = 0;
1694         uint32_t enables = 0;
1695         struct hwrm_ring_alloc_input req = {.req_type = 0 };
1696         struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
1697         struct rte_mempool *mb_pool;
1698         uint16_t rx_buf_size;
1699
1700         HWRM_PREP(&req, HWRM_RING_ALLOC, BNXT_USE_CHIMP_MB);
1701
1702         req.page_tbl_addr = rte_cpu_to_le_64(ring->bd_dma);
1703         req.fbo = rte_cpu_to_le_32(0);
1704         /* Association of ring index with doorbell index */
1705         req.logical_id = rte_cpu_to_le_16(map_index);
1706         req.length = rte_cpu_to_le_32(ring->ring_size);
1707
1708         switch (ring_type) {
1709         case HWRM_RING_ALLOC_INPUT_RING_TYPE_TX:
1710                 req.ring_type = ring_type;
1711                 req.cmpl_ring_id = rte_cpu_to_le_16(cmpl_ring_id);
1712                 req.stat_ctx_id = rte_cpu_to_le_32(stats_ctx_id);
1713                 req.queue_id = rte_cpu_to_le_16(tx_cosq_id);
1714                 if (stats_ctx_id != INVALID_STATS_CTX_ID)
1715                         enables |=
1716                         HWRM_RING_ALLOC_INPUT_ENABLES_STAT_CTX_ID_VALID;
1717                 break;
1718         case HWRM_RING_ALLOC_INPUT_RING_TYPE_RX:
1719                 req.ring_type = ring_type;
1720                 req.cmpl_ring_id = rte_cpu_to_le_16(cmpl_ring_id);
1721                 req.stat_ctx_id = rte_cpu_to_le_32(stats_ctx_id);
1722                 if (BNXT_CHIP_P5(bp)) {
1723                         mb_pool = bp->rx_queues[0]->mb_pool;
1724                         rx_buf_size = rte_pktmbuf_data_room_size(mb_pool) -
1725                                       RTE_PKTMBUF_HEADROOM;
1726                         rx_buf_size = RTE_MIN(BNXT_MAX_PKT_LEN, rx_buf_size);
1727                         req.rx_buf_size = rte_cpu_to_le_16(rx_buf_size);
1728                         enables |=
1729                                 HWRM_RING_ALLOC_INPUT_ENABLES_RX_BUF_SIZE_VALID;
1730                 }
1731                 if (stats_ctx_id != INVALID_STATS_CTX_ID)
1732                         enables |=
1733                                 HWRM_RING_ALLOC_INPUT_ENABLES_STAT_CTX_ID_VALID;
1734                 break;
1735         case HWRM_RING_ALLOC_INPUT_RING_TYPE_L2_CMPL:
1736                 req.ring_type = ring_type;
1737                 if (BNXT_HAS_NQ(bp)) {
1738                         /* Association of cp ring with nq */
1739                         req.nq_ring_id = rte_cpu_to_le_16(cmpl_ring_id);
1740                         enables |=
1741                                 HWRM_RING_ALLOC_INPUT_ENABLES_NQ_RING_ID_VALID;
1742                 }
1743                 req.int_mode = HWRM_RING_ALLOC_INPUT_INT_MODE_MSIX;
1744                 break;
1745         case HWRM_RING_ALLOC_INPUT_RING_TYPE_NQ:
1746                 req.ring_type = ring_type;
1747                 req.page_size = BNXT_PAGE_SHFT;
1748                 req.int_mode = HWRM_RING_ALLOC_INPUT_INT_MODE_MSIX;
1749                 break;
1750         case HWRM_RING_ALLOC_INPUT_RING_TYPE_RX_AGG:
1751                 req.ring_type = ring_type;
1752                 req.rx_ring_id = rte_cpu_to_le_16(ring->fw_rx_ring_id);
1753
1754                 mb_pool = bp->rx_queues[0]->mb_pool;
1755                 rx_buf_size = rte_pktmbuf_data_room_size(mb_pool) -
1756                               RTE_PKTMBUF_HEADROOM;
1757                 rx_buf_size = RTE_MIN(BNXT_MAX_PKT_LEN, rx_buf_size);
1758                 req.rx_buf_size = rte_cpu_to_le_16(rx_buf_size);
1759
1760                 req.stat_ctx_id = rte_cpu_to_le_32(stats_ctx_id);
1761                 enables |= HWRM_RING_ALLOC_INPUT_ENABLES_RX_RING_ID_VALID |
1762                            HWRM_RING_ALLOC_INPUT_ENABLES_RX_BUF_SIZE_VALID |
1763                            HWRM_RING_ALLOC_INPUT_ENABLES_STAT_CTX_ID_VALID;
1764                 break;
1765         default:
1766                 PMD_DRV_LOG(ERR, "hwrm alloc invalid ring type %d\n",
1767                         ring_type);
1768                 HWRM_UNLOCK();
1769                 return -EINVAL;
1770         }
1771         req.enables = rte_cpu_to_le_32(enables);
1772
1773         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1774
1775         if (rc || resp->error_code) {
1776                 if (rc == 0 && resp->error_code)
1777                         rc = rte_le_to_cpu_16(resp->error_code);
1778                 switch (ring_type) {
1779                 case HWRM_RING_ALLOC_INPUT_RING_TYPE_L2_CMPL:
1780                         PMD_DRV_LOG(ERR,
1781                                 "hwrm_ring_alloc cp failed. rc:%d\n", rc);
1782                         HWRM_UNLOCK();
1783                         return rc;
1784                 case HWRM_RING_ALLOC_INPUT_RING_TYPE_RX:
1785                         PMD_DRV_LOG(ERR,
1786                                     "hwrm_ring_alloc rx failed. rc:%d\n", rc);
1787                         HWRM_UNLOCK();
1788                         return rc;
1789                 case HWRM_RING_ALLOC_INPUT_RING_TYPE_RX_AGG:
1790                         PMD_DRV_LOG(ERR,
1791                                     "hwrm_ring_alloc rx agg failed. rc:%d\n",
1792                                     rc);
1793                         HWRM_UNLOCK();
1794                         return rc;
1795                 case HWRM_RING_ALLOC_INPUT_RING_TYPE_TX:
1796                         PMD_DRV_LOG(ERR,
1797                                     "hwrm_ring_alloc tx failed. rc:%d\n", rc);
1798                         HWRM_UNLOCK();
1799                         return rc;
1800                 case HWRM_RING_ALLOC_INPUT_RING_TYPE_NQ:
1801                         PMD_DRV_LOG(ERR,
1802                                     "hwrm_ring_alloc nq failed. rc:%d\n", rc);
1803                         HWRM_UNLOCK();
1804                         return rc;
1805                 default:
1806                         PMD_DRV_LOG(ERR, "Invalid ring. rc:%d\n", rc);
1807                         HWRM_UNLOCK();
1808                         return rc;
1809                 }
1810         }
1811
1812         ring->fw_ring_id = rte_le_to_cpu_16(resp->ring_id);
1813         HWRM_UNLOCK();
1814         return rc;
1815 }
1816
1817 int bnxt_hwrm_ring_free(struct bnxt *bp,
1818                         struct bnxt_ring *ring, uint32_t ring_type,
1819                         uint16_t cp_ring_id)
1820 {
1821         int rc;
1822         struct hwrm_ring_free_input req = {.req_type = 0 };
1823         struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
1824
1825         if (ring->fw_ring_id == INVALID_HW_RING_ID)
1826                 return -EINVAL;
1827
1828         HWRM_PREP(&req, HWRM_RING_FREE, BNXT_USE_CHIMP_MB);
1829
1830         req.ring_type = ring_type;
1831         req.ring_id = rte_cpu_to_le_16(ring->fw_ring_id);
1832         req.cmpl_ring = rte_cpu_to_le_16(cp_ring_id);
1833
1834         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1835         ring->fw_ring_id = INVALID_HW_RING_ID;
1836
1837         if (rc || resp->error_code) {
1838                 if (rc == 0 && resp->error_code)
1839                         rc = rte_le_to_cpu_16(resp->error_code);
1840                 HWRM_UNLOCK();
1841
1842                 switch (ring_type) {
1843                 case HWRM_RING_FREE_INPUT_RING_TYPE_L2_CMPL:
1844                         PMD_DRV_LOG(ERR, "hwrm_ring_free cp failed. rc:%d\n",
1845                                 rc);
1846                         return rc;
1847                 case HWRM_RING_FREE_INPUT_RING_TYPE_RX:
1848                         PMD_DRV_LOG(ERR, "hwrm_ring_free rx failed. rc:%d\n",
1849                                 rc);
1850                         return rc;
1851                 case HWRM_RING_FREE_INPUT_RING_TYPE_TX:
1852                         PMD_DRV_LOG(ERR, "hwrm_ring_free tx failed. rc:%d\n",
1853                                 rc);
1854                         return rc;
1855                 case HWRM_RING_FREE_INPUT_RING_TYPE_NQ:
1856                         PMD_DRV_LOG(ERR,
1857                                     "hwrm_ring_free nq failed. rc:%d\n", rc);
1858                         return rc;
1859                 case HWRM_RING_FREE_INPUT_RING_TYPE_RX_AGG:
1860                         PMD_DRV_LOG(ERR,
1861                                     "hwrm_ring_free agg failed. rc:%d\n", rc);
1862                         return rc;
1863                 default:
1864                         PMD_DRV_LOG(ERR, "Invalid ring, rc:%d\n", rc);
1865                         return rc;
1866                 }
1867         }
1868         HWRM_UNLOCK();
1869         return 0;
1870 }
1871
1872 int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp, unsigned int idx)
1873 {
1874         int rc = 0;
1875         struct hwrm_ring_grp_alloc_input req = {.req_type = 0 };
1876         struct hwrm_ring_grp_alloc_output *resp = bp->hwrm_cmd_resp_addr;
1877
1878         /* Don't attempt to re-create the ring group if it is already created */
1879         if (bp->grp_info[idx].fw_grp_id != INVALID_HW_RING_ID)
1880                 return 0;
1881
1882         HWRM_PREP(&req, HWRM_RING_GRP_ALLOC, BNXT_USE_CHIMP_MB);
1883
1884         req.cr = rte_cpu_to_le_16(bp->grp_info[idx].cp_fw_ring_id);
1885         req.rr = rte_cpu_to_le_16(bp->grp_info[idx].rx_fw_ring_id);
1886         req.ar = rte_cpu_to_le_16(bp->grp_info[idx].ag_fw_ring_id);
1887         req.sc = rte_cpu_to_le_16(bp->grp_info[idx].fw_stats_ctx);
1888
1889         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1890
1891         HWRM_CHECK_RESULT();
1892
1893         bp->grp_info[idx].fw_grp_id = rte_le_to_cpu_16(resp->ring_group_id);
1894
1895         HWRM_UNLOCK();
1896
1897         return rc;
1898 }
1899
1900 int bnxt_hwrm_ring_grp_free(struct bnxt *bp, unsigned int idx)
1901 {
1902         int rc;
1903         struct hwrm_ring_grp_free_input req = {.req_type = 0 };
1904         struct hwrm_ring_grp_free_output *resp = bp->hwrm_cmd_resp_addr;
1905
1906         if (bp->grp_info[idx].fw_grp_id == INVALID_HW_RING_ID)
1907                 return 0;
1908
1909         HWRM_PREP(&req, HWRM_RING_GRP_FREE, BNXT_USE_CHIMP_MB);
1910
1911         req.ring_group_id = rte_cpu_to_le_16(bp->grp_info[idx].fw_grp_id);
1912
1913         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1914
1915         HWRM_CHECK_RESULT();
1916         HWRM_UNLOCK();
1917
1918         bp->grp_info[idx].fw_grp_id = INVALID_HW_RING_ID;
1919         return rc;
1920 }
1921
1922 int bnxt_hwrm_stat_clear(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1923 {
1924         int rc = 0;
1925         struct hwrm_stat_ctx_clr_stats_input req = {.req_type = 0 };
1926         struct hwrm_stat_ctx_clr_stats_output *resp = bp->hwrm_cmd_resp_addr;
1927
1928         if (cpr->hw_stats_ctx_id == HWRM_NA_SIGNATURE)
1929                 return rc;
1930
1931         HWRM_PREP(&req, HWRM_STAT_CTX_CLR_STATS, BNXT_USE_CHIMP_MB);
1932
1933         req.stat_ctx_id = rte_cpu_to_le_32(cpr->hw_stats_ctx_id);
1934
1935         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1936
1937         HWRM_CHECK_RESULT();
1938         HWRM_UNLOCK();
1939
1940         return rc;
1941 }
1942
1943 int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1944 {
1945         int rc;
1946         struct hwrm_stat_ctx_alloc_input req = {.req_type = 0 };
1947         struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
1948
1949         if (cpr->hw_stats_ctx_id != HWRM_NA_SIGNATURE)
1950                 return 0;
1951
1952         HWRM_PREP(&req, HWRM_STAT_CTX_ALLOC, BNXT_USE_CHIMP_MB);
1953
1954         req.update_period_ms = rte_cpu_to_le_32(0);
1955
1956         req.stats_dma_addr = rte_cpu_to_le_64(cpr->hw_stats_map);
1957
1958         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1959
1960         HWRM_CHECK_RESULT();
1961
1962         cpr->hw_stats_ctx_id = rte_le_to_cpu_32(resp->stat_ctx_id);
1963
1964         HWRM_UNLOCK();
1965
1966         return rc;
1967 }
1968
1969 static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1970 {
1971         int rc;
1972         struct hwrm_stat_ctx_free_input req = {.req_type = 0 };
1973         struct hwrm_stat_ctx_free_output *resp = bp->hwrm_cmd_resp_addr;
1974
1975         if (cpr->hw_stats_ctx_id == HWRM_NA_SIGNATURE)
1976                 return 0;
1977
1978         HWRM_PREP(&req, HWRM_STAT_CTX_FREE, BNXT_USE_CHIMP_MB);
1979
1980         req.stat_ctx_id = rte_cpu_to_le_32(cpr->hw_stats_ctx_id);
1981
1982         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
1983
1984         HWRM_CHECK_RESULT();
1985         HWRM_UNLOCK();
1986
1987         cpr->hw_stats_ctx_id = HWRM_NA_SIGNATURE;
1988
1989         return rc;
1990 }
1991
1992 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1993 {
1994         int rc = 0, i, j;
1995         struct hwrm_vnic_alloc_input req = { 0 };
1996         struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
1997
1998         if (!BNXT_HAS_RING_GRPS(bp))
1999                 goto skip_ring_grps;
2000
2001         /* map ring groups to this vnic */
2002         PMD_DRV_LOG(DEBUG, "Alloc VNIC. Start %x, End %x\n",
2003                 vnic->start_grp_id, vnic->end_grp_id);
2004         for (i = vnic->start_grp_id, j = 0; i < vnic->end_grp_id; i++, j++)
2005                 vnic->fw_grp_ids[j] = bp->grp_info[i].fw_grp_id;
2006
2007         vnic->dflt_ring_grp = bp->grp_info[vnic->start_grp_id].fw_grp_id;
2008         vnic->rss_rule = (uint16_t)HWRM_NA_SIGNATURE;
2009         vnic->cos_rule = (uint16_t)HWRM_NA_SIGNATURE;
2010         vnic->lb_rule = (uint16_t)HWRM_NA_SIGNATURE;
2011
2012 skip_ring_grps:
2013         vnic->mru = BNXT_VNIC_MRU(bp->eth_dev->data->mtu);
2014         HWRM_PREP(&req, HWRM_VNIC_ALLOC, BNXT_USE_CHIMP_MB);
2015
2016         if (vnic->func_default)
2017                 req.flags =
2018                         rte_cpu_to_le_32(HWRM_VNIC_ALLOC_INPUT_FLAGS_DEFAULT);
2019         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2020
2021         HWRM_CHECK_RESULT();
2022
2023         vnic->fw_vnic_id = rte_le_to_cpu_16(resp->vnic_id);
2024         HWRM_UNLOCK();
2025         PMD_DRV_LOG(DEBUG, "VNIC ID %x\n", vnic->fw_vnic_id);
2026         return rc;
2027 }
2028
2029 static int bnxt_hwrm_vnic_plcmodes_qcfg(struct bnxt *bp,
2030                                         struct bnxt_vnic_info *vnic,
2031                                         struct bnxt_plcmodes_cfg *pmode)
2032 {
2033         int rc = 0;
2034         struct hwrm_vnic_plcmodes_qcfg_input req = {.req_type = 0 };
2035         struct hwrm_vnic_plcmodes_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
2036
2037         HWRM_PREP(&req, HWRM_VNIC_PLCMODES_QCFG, BNXT_USE_CHIMP_MB);
2038
2039         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
2040
2041         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2042
2043         HWRM_CHECK_RESULT();
2044
2045         pmode->flags = rte_le_to_cpu_32(resp->flags);
2046         /* dflt_vnic bit doesn't exist in the _cfg command */
2047         pmode->flags &= ~(HWRM_VNIC_PLCMODES_QCFG_OUTPUT_FLAGS_DFLT_VNIC);
2048         pmode->jumbo_thresh = rte_le_to_cpu_16(resp->jumbo_thresh);
2049         pmode->hds_offset = rte_le_to_cpu_16(resp->hds_offset);
2050         pmode->hds_threshold = rte_le_to_cpu_16(resp->hds_threshold);
2051
2052         HWRM_UNLOCK();
2053
2054         return rc;
2055 }
2056
2057 static int bnxt_hwrm_vnic_plcmodes_cfg(struct bnxt *bp,
2058                                        struct bnxt_vnic_info *vnic,
2059                                        struct bnxt_plcmodes_cfg *pmode)
2060 {
2061         int rc = 0;
2062         struct hwrm_vnic_plcmodes_cfg_input req = {.req_type = 0 };
2063         struct hwrm_vnic_plcmodes_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2064
2065         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
2066                 PMD_DRV_LOG(DEBUG, "VNIC ID %x\n", vnic->fw_vnic_id);
2067                 return rc;
2068         }
2069
2070         HWRM_PREP(&req, HWRM_VNIC_PLCMODES_CFG, BNXT_USE_CHIMP_MB);
2071
2072         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
2073         req.flags = rte_cpu_to_le_32(pmode->flags);
2074         req.jumbo_thresh = rte_cpu_to_le_16(pmode->jumbo_thresh);
2075         req.hds_offset = rte_cpu_to_le_16(pmode->hds_offset);
2076         req.hds_threshold = rte_cpu_to_le_16(pmode->hds_threshold);
2077         req.enables = rte_cpu_to_le_32(
2078             HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_HDS_THRESHOLD_VALID |
2079             HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_HDS_OFFSET_VALID |
2080             HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_JUMBO_THRESH_VALID
2081         );
2082
2083         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2084
2085         HWRM_CHECK_RESULT();
2086         HWRM_UNLOCK();
2087
2088         return rc;
2089 }
2090
2091 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2092 {
2093         int rc = 0;
2094         struct hwrm_vnic_cfg_input req = {.req_type = 0 };
2095         struct hwrm_vnic_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2096         struct bnxt_plcmodes_cfg pmodes = { 0 };
2097         uint32_t ctx_enable_flag = 0;
2098         uint32_t enables = 0;
2099
2100         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
2101                 PMD_DRV_LOG(DEBUG, "VNIC ID %x\n", vnic->fw_vnic_id);
2102                 return rc;
2103         }
2104
2105         rc = bnxt_hwrm_vnic_plcmodes_qcfg(bp, vnic, &pmodes);
2106         if (rc)
2107                 return rc;
2108
2109         HWRM_PREP(&req, HWRM_VNIC_CFG, BNXT_USE_CHIMP_MB);
2110
2111         if (BNXT_CHIP_P5(bp)) {
2112                 int dflt_rxq = vnic->start_grp_id;
2113                 struct bnxt_rx_ring_info *rxr;
2114                 struct bnxt_cp_ring_info *cpr;
2115                 struct bnxt_rx_queue *rxq;
2116                 int i;
2117
2118                 /*
2119                  * The first active receive ring is used as the VNIC
2120                  * default receive ring. If there are no active receive
2121                  * rings (all corresponding receive queues are stopped),
2122                  * the first receive ring is used.
2123                  */
2124                 for (i = vnic->start_grp_id; i < vnic->end_grp_id; i++) {
2125                         rxq = bp->eth_dev->data->rx_queues[i];
2126                         if (rxq->rx_started) {
2127                                 dflt_rxq = i;
2128                                 break;
2129                         }
2130                 }
2131
2132                 rxq = bp->eth_dev->data->rx_queues[dflt_rxq];
2133                 rxr = rxq->rx_ring;
2134                 cpr = rxq->cp_ring;
2135
2136                 req.default_rx_ring_id =
2137                         rte_cpu_to_le_16(rxr->rx_ring_struct->fw_ring_id);
2138                 req.default_cmpl_ring_id =
2139                         rte_cpu_to_le_16(cpr->cp_ring_struct->fw_ring_id);
2140                 enables = HWRM_VNIC_CFG_INPUT_ENABLES_DEFAULT_RX_RING_ID |
2141                           HWRM_VNIC_CFG_INPUT_ENABLES_DEFAULT_CMPL_RING_ID;
2142                 if (bp->vnic_cap_flags & BNXT_VNIC_CAP_RX_CMPL_V2) {
2143                         enables |= HWRM_VNIC_CFG_INPUT_ENABLES_RX_CSUM_V2_MODE;
2144                         req.rx_csum_v2_mode =
2145                                 HWRM_VNIC_CFG_INPUT_RX_CSUM_V2_MODE_ALL_OK;
2146                 }
2147                 goto config_mru;
2148         }
2149
2150         /* Only RSS support for now TBD: COS & LB */
2151         enables = HWRM_VNIC_CFG_INPUT_ENABLES_DFLT_RING_GRP;
2152         if (vnic->lb_rule != 0xffff)
2153                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_LB_RULE;
2154         if (vnic->cos_rule != 0xffff)
2155                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_COS_RULE;
2156         if (vnic->rss_rule != (uint16_t)HWRM_NA_SIGNATURE) {
2157                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_MRU;
2158                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_RSS_RULE;
2159         }
2160         if (bp->vnic_cap_flags & BNXT_VNIC_CAP_COS_CLASSIFY) {
2161                 ctx_enable_flag |= HWRM_VNIC_CFG_INPUT_ENABLES_QUEUE_ID;
2162                 req.queue_id = rte_cpu_to_le_16(vnic->cos_queue_id);
2163         }
2164
2165         enables |= ctx_enable_flag;
2166         req.dflt_ring_grp = rte_cpu_to_le_16(vnic->dflt_ring_grp);
2167         req.rss_rule = rte_cpu_to_le_16(vnic->rss_rule);
2168         req.cos_rule = rte_cpu_to_le_16(vnic->cos_rule);
2169         req.lb_rule = rte_cpu_to_le_16(vnic->lb_rule);
2170
2171 config_mru:
2172         req.enables = rte_cpu_to_le_32(enables);
2173         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
2174         req.mru = rte_cpu_to_le_16(vnic->mru);
2175         /* Configure default VNIC only once. */
2176         if (vnic->func_default && !(bp->flags & BNXT_FLAG_DFLT_VNIC_SET)) {
2177                 req.flags |=
2178                     rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_FLAGS_DEFAULT);
2179                 bp->flags |= BNXT_FLAG_DFLT_VNIC_SET;
2180         }
2181         if (vnic->vlan_strip)
2182                 req.flags |=
2183                     rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_FLAGS_VLAN_STRIP_MODE);
2184         if (vnic->bd_stall)
2185                 req.flags |=
2186                     rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_FLAGS_BD_STALL_MODE);
2187         if (vnic->rss_dflt_cr)
2188                 req.flags |= rte_cpu_to_le_32(
2189                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_RSS_DFLT_CR_MODE);
2190
2191         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2192
2193         HWRM_CHECK_RESULT();
2194         HWRM_UNLOCK();
2195
2196         rc = bnxt_hwrm_vnic_plcmodes_cfg(bp, vnic, &pmodes);
2197
2198         return rc;
2199 }
2200
2201 int bnxt_hwrm_vnic_qcfg(struct bnxt *bp, struct bnxt_vnic_info *vnic,
2202                 int16_t fw_vf_id)
2203 {
2204         int rc = 0;
2205         struct hwrm_vnic_qcfg_input req = {.req_type = 0 };
2206         struct hwrm_vnic_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
2207
2208         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
2209                 PMD_DRV_LOG(DEBUG, "VNIC QCFG ID %d\n", vnic->fw_vnic_id);
2210                 return rc;
2211         }
2212         HWRM_PREP(&req, HWRM_VNIC_QCFG, BNXT_USE_CHIMP_MB);
2213
2214         req.enables =
2215                 rte_cpu_to_le_32(HWRM_VNIC_QCFG_INPUT_ENABLES_VF_ID_VALID);
2216         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
2217         req.vf_id = rte_cpu_to_le_16(fw_vf_id);
2218
2219         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2220
2221         HWRM_CHECK_RESULT();
2222
2223         vnic->dflt_ring_grp = rte_le_to_cpu_16(resp->dflt_ring_grp);
2224         vnic->rss_rule = rte_le_to_cpu_16(resp->rss_rule);
2225         vnic->cos_rule = rte_le_to_cpu_16(resp->cos_rule);
2226         vnic->lb_rule = rte_le_to_cpu_16(resp->lb_rule);
2227         vnic->mru = rte_le_to_cpu_16(resp->mru);
2228         vnic->func_default = rte_le_to_cpu_32(
2229                         resp->flags) & HWRM_VNIC_QCFG_OUTPUT_FLAGS_DEFAULT;
2230         vnic->vlan_strip = rte_le_to_cpu_32(resp->flags) &
2231                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_VLAN_STRIP_MODE;
2232         vnic->bd_stall = rte_le_to_cpu_32(resp->flags) &
2233                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_BD_STALL_MODE;
2234         vnic->rss_dflt_cr = rte_le_to_cpu_32(resp->flags) &
2235                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_RSS_DFLT_CR_MODE;
2236
2237         HWRM_UNLOCK();
2238
2239         return rc;
2240 }
2241
2242 int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
2243                              struct bnxt_vnic_info *vnic, uint16_t ctx_idx)
2244 {
2245         int rc = 0;
2246         uint16_t ctx_id;
2247         struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {.req_type = 0 };
2248         struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
2249                                                 bp->hwrm_cmd_resp_addr;
2250
2251         HWRM_PREP(&req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, BNXT_USE_CHIMP_MB);
2252
2253         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2254         HWRM_CHECK_RESULT();
2255
2256         ctx_id = rte_le_to_cpu_16(resp->rss_cos_lb_ctx_id);
2257         if (!BNXT_HAS_RING_GRPS(bp))
2258                 vnic->fw_grp_ids[ctx_idx] = ctx_id;
2259         else if (ctx_idx == 0)
2260                 vnic->rss_rule = ctx_id;
2261
2262         HWRM_UNLOCK();
2263
2264         return rc;
2265 }
2266
2267 static
2268 int _bnxt_hwrm_vnic_ctx_free(struct bnxt *bp,
2269                              struct bnxt_vnic_info *vnic, uint16_t ctx_idx)
2270 {
2271         int rc = 0;
2272         struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {.req_type = 0 };
2273         struct hwrm_vnic_rss_cos_lb_ctx_free_output *resp =
2274                                                 bp->hwrm_cmd_resp_addr;
2275
2276         if (ctx_idx == (uint16_t)HWRM_NA_SIGNATURE) {
2277                 PMD_DRV_LOG(DEBUG, "VNIC RSS Rule %x\n", vnic->rss_rule);
2278                 return rc;
2279         }
2280         HWRM_PREP(&req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, BNXT_USE_CHIMP_MB);
2281
2282         req.rss_cos_lb_ctx_id = rte_cpu_to_le_16(ctx_idx);
2283
2284         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2285
2286         HWRM_CHECK_RESULT();
2287         HWRM_UNLOCK();
2288
2289         return rc;
2290 }
2291
2292 int bnxt_hwrm_vnic_ctx_free(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2293 {
2294         int rc = 0;
2295
2296         if (BNXT_CHIP_P5(bp)) {
2297                 int j;
2298
2299                 for (j = 0; j < vnic->num_lb_ctxts; j++) {
2300                         rc = _bnxt_hwrm_vnic_ctx_free(bp,
2301                                                       vnic,
2302                                                       vnic->fw_grp_ids[j]);
2303                         vnic->fw_grp_ids[j] = INVALID_HW_RING_ID;
2304                 }
2305                 vnic->num_lb_ctxts = 0;
2306         } else {
2307                 rc = _bnxt_hwrm_vnic_ctx_free(bp, vnic, vnic->rss_rule);
2308                 vnic->rss_rule = INVALID_HW_RING_ID;
2309         }
2310
2311         return rc;
2312 }
2313
2314 int bnxt_hwrm_vnic_free(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2315 {
2316         int rc = 0;
2317         struct hwrm_vnic_free_input req = {.req_type = 0 };
2318         struct hwrm_vnic_free_output *resp = bp->hwrm_cmd_resp_addr;
2319
2320         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
2321                 PMD_DRV_LOG(DEBUG, "VNIC FREE ID %x\n", vnic->fw_vnic_id);
2322                 return rc;
2323         }
2324
2325         HWRM_PREP(&req, HWRM_VNIC_FREE, BNXT_USE_CHIMP_MB);
2326
2327         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
2328
2329         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2330
2331         HWRM_CHECK_RESULT();
2332         HWRM_UNLOCK();
2333
2334         vnic->fw_vnic_id = INVALID_HW_RING_ID;
2335         /* Configure default VNIC again if necessary. */
2336         if (vnic->func_default && (bp->flags & BNXT_FLAG_DFLT_VNIC_SET))
2337                 bp->flags &= ~BNXT_FLAG_DFLT_VNIC_SET;
2338
2339         return rc;
2340 }
2341
2342 static int
2343 bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2344 {
2345         int i;
2346         int rc = 0;
2347         int nr_ctxs = vnic->num_lb_ctxts;
2348         struct hwrm_vnic_rss_cfg_input req = {.req_type = 0 };
2349         struct hwrm_vnic_rss_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2350
2351         for (i = 0; i < nr_ctxs; i++) {
2352                 HWRM_PREP(&req, HWRM_VNIC_RSS_CFG, BNXT_USE_CHIMP_MB);
2353
2354                 req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
2355                 req.hash_type = rte_cpu_to_le_32(vnic->hash_type);
2356                 req.hash_mode_flags = vnic->hash_mode;
2357
2358                 req.hash_key_tbl_addr =
2359                         rte_cpu_to_le_64(vnic->rss_hash_key_dma_addr);
2360
2361                 req.ring_grp_tbl_addr =
2362                         rte_cpu_to_le_64(vnic->rss_table_dma_addr +
2363                                          i * HW_HASH_INDEX_SIZE);
2364                 req.ring_table_pair_index = i;
2365                 req.rss_ctx_idx = rte_cpu_to_le_16(vnic->fw_grp_ids[i]);
2366
2367                 rc = bnxt_hwrm_send_message(bp, &req, sizeof(req),
2368                                             BNXT_USE_CHIMP_MB);
2369
2370                 HWRM_CHECK_RESULT();
2371                 HWRM_UNLOCK();
2372         }
2373
2374         return rc;
2375 }
2376
2377 int bnxt_hwrm_vnic_rss_cfg(struct bnxt *bp,
2378                            struct bnxt_vnic_info *vnic)
2379 {
2380         int rc = 0;
2381         struct hwrm_vnic_rss_cfg_input req = {.req_type = 0 };
2382         struct hwrm_vnic_rss_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2383
2384         if (!vnic->rss_table)
2385                 return 0;
2386
2387         if (BNXT_CHIP_P5(bp))
2388                 return bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
2389
2390         HWRM_PREP(&req, HWRM_VNIC_RSS_CFG, BNXT_USE_CHIMP_MB);
2391
2392         req.hash_type = rte_cpu_to_le_32(vnic->hash_type);
2393         req.hash_mode_flags = vnic->hash_mode;
2394
2395         req.ring_grp_tbl_addr =
2396             rte_cpu_to_le_64(vnic->rss_table_dma_addr);
2397         req.hash_key_tbl_addr =
2398             rte_cpu_to_le_64(vnic->rss_hash_key_dma_addr);
2399         req.rss_ctx_idx = rte_cpu_to_le_16(vnic->rss_rule);
2400         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
2401
2402         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2403
2404         HWRM_CHECK_RESULT();
2405         HWRM_UNLOCK();
2406
2407         return rc;
2408 }
2409
2410 int bnxt_hwrm_vnic_plcmode_cfg(struct bnxt *bp,
2411                         struct bnxt_vnic_info *vnic)
2412 {
2413         int rc = 0;
2414         struct hwrm_vnic_plcmodes_cfg_input req = {.req_type = 0 };
2415         struct hwrm_vnic_plcmodes_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2416         uint16_t size;
2417
2418         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
2419                 PMD_DRV_LOG(DEBUG, "VNIC ID %x\n", vnic->fw_vnic_id);
2420                 return rc;
2421         }
2422
2423         HWRM_PREP(&req, HWRM_VNIC_PLCMODES_CFG, BNXT_USE_CHIMP_MB);
2424
2425         req.flags = rte_cpu_to_le_32(
2426                         HWRM_VNIC_PLCMODES_CFG_INPUT_FLAGS_JUMBO_PLACEMENT);
2427
2428         req.enables = rte_cpu_to_le_32(
2429                 HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_JUMBO_THRESH_VALID);
2430
2431         size = rte_pktmbuf_data_room_size(bp->rx_queues[0]->mb_pool);
2432         size -= RTE_PKTMBUF_HEADROOM;
2433         size = RTE_MIN(BNXT_MAX_PKT_LEN, size);
2434
2435         req.jumbo_thresh = rte_cpu_to_le_16(size);
2436         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
2437
2438         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2439
2440         HWRM_CHECK_RESULT();
2441         HWRM_UNLOCK();
2442
2443         return rc;
2444 }
2445
2446 int bnxt_hwrm_vnic_tpa_cfg(struct bnxt *bp,
2447                         struct bnxt_vnic_info *vnic, bool enable)
2448 {
2449         int rc = 0;
2450         struct hwrm_vnic_tpa_cfg_input req = {.req_type = 0 };
2451         struct hwrm_vnic_tpa_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2452
2453         if (BNXT_CHIP_P5(bp) && !bp->max_tpa_v2) {
2454                 if (enable)
2455                         PMD_DRV_LOG(ERR, "No HW support for LRO\n");
2456                 return -ENOTSUP;
2457         }
2458
2459         if (vnic->fw_vnic_id == INVALID_HW_RING_ID) {
2460                 PMD_DRV_LOG(DEBUG, "Invalid vNIC ID\n");
2461                 return 0;
2462         }
2463
2464         HWRM_PREP(&req, HWRM_VNIC_TPA_CFG, BNXT_USE_CHIMP_MB);
2465
2466         if (enable) {
2467                 req.enables = rte_cpu_to_le_32(
2468                                 HWRM_VNIC_TPA_CFG_INPUT_ENABLES_MAX_AGG_SEGS |
2469                                 HWRM_VNIC_TPA_CFG_INPUT_ENABLES_MAX_AGGS |
2470                                 HWRM_VNIC_TPA_CFG_INPUT_ENABLES_MIN_AGG_LEN);
2471                 req.flags = rte_cpu_to_le_32(
2472                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_TPA |
2473                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_ENCAP_TPA |
2474                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_RSC_WND_UPDATE |
2475                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_GRO |
2476                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_AGG_WITH_ECN |
2477                         HWRM_VNIC_TPA_CFG_INPUT_FLAGS_AGG_WITH_SAME_GRE_SEQ);
2478                 req.max_aggs = rte_cpu_to_le_16(BNXT_TPA_MAX_AGGS(bp));
2479                 req.max_agg_segs = rte_cpu_to_le_16(BNXT_TPA_MAX_SEGS(bp));
2480                 req.min_agg_len = rte_cpu_to_le_32(512);
2481         }
2482         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
2483
2484         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2485
2486         HWRM_CHECK_RESULT();
2487         HWRM_UNLOCK();
2488
2489         return rc;
2490 }
2491
2492 int bnxt_hwrm_func_vf_mac(struct bnxt *bp, uint16_t vf, const uint8_t *mac_addr)
2493 {
2494         struct hwrm_func_cfg_input req = {0};
2495         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2496         int rc;
2497
2498         req.flags = rte_cpu_to_le_32(bp->pf->vf_info[vf].func_cfg_flags);
2499         req.enables = rte_cpu_to_le_32(
2500                         HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
2501         memcpy(req.dflt_mac_addr, mac_addr, sizeof(req.dflt_mac_addr));
2502         req.fid = rte_cpu_to_le_16(bp->pf->vf_info[vf].fid);
2503
2504         HWRM_PREP(&req, HWRM_FUNC_CFG, BNXT_USE_CHIMP_MB);
2505
2506         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2507         HWRM_CHECK_RESULT();
2508         HWRM_UNLOCK();
2509
2510         bp->pf->vf_info[vf].random_mac = false;
2511
2512         return rc;
2513 }
2514
2515 int bnxt_hwrm_func_qstats_tx_drop(struct bnxt *bp, uint16_t fid,
2516                                   uint64_t *dropped)
2517 {
2518         int rc = 0;
2519         struct hwrm_func_qstats_input req = {.req_type = 0};
2520         struct hwrm_func_qstats_output *resp = bp->hwrm_cmd_resp_addr;
2521
2522         HWRM_PREP(&req, HWRM_FUNC_QSTATS, BNXT_USE_CHIMP_MB);
2523
2524         req.fid = rte_cpu_to_le_16(fid);
2525
2526         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2527
2528         HWRM_CHECK_RESULT();
2529
2530         if (dropped)
2531                 *dropped = rte_le_to_cpu_64(resp->tx_drop_pkts);
2532
2533         HWRM_UNLOCK();
2534
2535         return rc;
2536 }
2537
2538 int bnxt_hwrm_func_qstats(struct bnxt *bp, uint16_t fid,
2539                           struct rte_eth_stats *stats,
2540                           struct hwrm_func_qstats_output *func_qstats)
2541 {
2542         int rc = 0;
2543         struct hwrm_func_qstats_input req = {.req_type = 0};
2544         struct hwrm_func_qstats_output *resp = bp->hwrm_cmd_resp_addr;
2545
2546         HWRM_PREP(&req, HWRM_FUNC_QSTATS, BNXT_USE_CHIMP_MB);
2547
2548         req.fid = rte_cpu_to_le_16(fid);
2549
2550         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2551
2552         HWRM_CHECK_RESULT();
2553         if (func_qstats)
2554                 memcpy(func_qstats, resp,
2555                        sizeof(struct hwrm_func_qstats_output));
2556
2557         if (!stats)
2558                 goto exit;
2559
2560         stats->ipackets = rte_le_to_cpu_64(resp->rx_ucast_pkts);
2561         stats->ipackets += rte_le_to_cpu_64(resp->rx_mcast_pkts);
2562         stats->ipackets += rte_le_to_cpu_64(resp->rx_bcast_pkts);
2563         stats->ibytes = rte_le_to_cpu_64(resp->rx_ucast_bytes);
2564         stats->ibytes += rte_le_to_cpu_64(resp->rx_mcast_bytes);
2565         stats->ibytes += rte_le_to_cpu_64(resp->rx_bcast_bytes);
2566
2567         stats->opackets = rte_le_to_cpu_64(resp->tx_ucast_pkts);
2568         stats->opackets += rte_le_to_cpu_64(resp->tx_mcast_pkts);
2569         stats->opackets += rte_le_to_cpu_64(resp->tx_bcast_pkts);
2570         stats->obytes = rte_le_to_cpu_64(resp->tx_ucast_bytes);
2571         stats->obytes += rte_le_to_cpu_64(resp->tx_mcast_bytes);
2572         stats->obytes += rte_le_to_cpu_64(resp->tx_bcast_bytes);
2573
2574         stats->imissed = rte_le_to_cpu_64(resp->rx_discard_pkts);
2575         stats->ierrors = rte_le_to_cpu_64(resp->rx_drop_pkts);
2576         stats->oerrors = rte_le_to_cpu_64(resp->tx_discard_pkts);
2577
2578 exit:
2579         HWRM_UNLOCK();
2580
2581         return rc;
2582 }
2583
2584 int bnxt_hwrm_func_clr_stats(struct bnxt *bp, uint16_t fid)
2585 {
2586         int rc = 0;
2587         struct hwrm_func_clr_stats_input req = {.req_type = 0};
2588         struct hwrm_func_clr_stats_output *resp = bp->hwrm_cmd_resp_addr;
2589
2590         HWRM_PREP(&req, HWRM_FUNC_CLR_STATS, BNXT_USE_CHIMP_MB);
2591
2592         req.fid = rte_cpu_to_le_16(fid);
2593
2594         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2595
2596         HWRM_CHECK_RESULT();
2597         HWRM_UNLOCK();
2598
2599         return rc;
2600 }
2601
2602 int bnxt_clear_all_hwrm_stat_ctxs(struct bnxt *bp)
2603 {
2604         unsigned int i;
2605         int rc = 0;
2606
2607         for (i = 0; i < bp->rx_cp_nr_rings + bp->tx_cp_nr_rings; i++) {
2608                 struct bnxt_tx_queue *txq;
2609                 struct bnxt_rx_queue *rxq;
2610                 struct bnxt_cp_ring_info *cpr;
2611
2612                 if (i >= bp->rx_cp_nr_rings) {
2613                         txq = bp->tx_queues[i - bp->rx_cp_nr_rings];
2614                         cpr = txq->cp_ring;
2615                 } else {
2616                         rxq = bp->rx_queues[i];
2617                         cpr = rxq->cp_ring;
2618                 }
2619
2620                 rc = bnxt_hwrm_stat_clear(bp, cpr);
2621                 if (rc)
2622                         return rc;
2623         }
2624         return 0;
2625 }
2626
2627 static int
2628 bnxt_free_all_hwrm_stat_ctxs(struct bnxt *bp)
2629 {
2630         int rc;
2631         unsigned int i;
2632         struct bnxt_cp_ring_info *cpr;
2633
2634         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
2635
2636                 cpr = bp->rx_queues[i]->cp_ring;
2637                 if (BNXT_HAS_RING_GRPS(bp))
2638                         bp->grp_info[i].fw_stats_ctx = -1;
2639                 if (cpr == NULL)
2640                         continue;
2641                 rc = bnxt_hwrm_stat_ctx_free(bp, cpr);
2642                 if (rc)
2643                         return rc;
2644         }
2645
2646         for (i = 0; i < bp->tx_cp_nr_rings; i++) {
2647                 cpr = bp->tx_queues[i]->cp_ring;
2648                 if (cpr == NULL)
2649                         continue;
2650                 rc = bnxt_hwrm_stat_ctx_free(bp, cpr);
2651                 if (rc)
2652                         return rc;
2653         }
2654
2655         return 0;
2656 }
2657
2658 static int
2659 bnxt_free_all_hwrm_ring_grps(struct bnxt *bp)
2660 {
2661         uint16_t idx;
2662         uint32_t rc = 0;
2663
2664         if (!BNXT_HAS_RING_GRPS(bp))
2665                 return 0;
2666
2667         for (idx = 0; idx < bp->rx_cp_nr_rings; idx++) {
2668
2669                 if (bp->grp_info[idx].fw_grp_id == INVALID_HW_RING_ID)
2670                         continue;
2671
2672                 rc = bnxt_hwrm_ring_grp_free(bp, idx);
2673
2674                 if (rc)
2675                         return rc;
2676         }
2677         return rc;
2678 }
2679
2680 void bnxt_free_nq_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2681 {
2682         struct bnxt_ring *cp_ring = cpr->cp_ring_struct;
2683
2684         bnxt_hwrm_ring_free(bp, cp_ring,
2685                             HWRM_RING_FREE_INPUT_RING_TYPE_NQ,
2686                             INVALID_HW_RING_ID);
2687         memset(cpr->cp_desc_ring, 0,
2688                cpr->cp_ring_struct->ring_size * sizeof(*cpr->cp_desc_ring));
2689         cpr->cp_raw_cons = 0;
2690 }
2691
2692 void bnxt_free_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2693 {
2694         struct bnxt_ring *cp_ring = cpr->cp_ring_struct;
2695
2696         bnxt_hwrm_ring_free(bp, cp_ring,
2697                             HWRM_RING_FREE_INPUT_RING_TYPE_L2_CMPL,
2698                             INVALID_HW_RING_ID);
2699         memset(cpr->cp_desc_ring, 0,
2700                cpr->cp_ring_struct->ring_size * sizeof(*cpr->cp_desc_ring));
2701         cpr->cp_raw_cons = 0;
2702 }
2703
2704 void bnxt_free_hwrm_rx_ring(struct bnxt *bp, int queue_index)
2705 {
2706         struct bnxt_rx_queue *rxq = bp->rx_queues[queue_index];
2707         struct bnxt_rx_ring_info *rxr = rxq ? rxq->rx_ring : NULL;
2708         struct bnxt_ring *ring = rxr ? rxr->rx_ring_struct : NULL;
2709         struct bnxt_cp_ring_info *cpr = rxq ? rxq->cp_ring : NULL;
2710
2711         if (BNXT_HAS_RING_GRPS(bp))
2712                 bnxt_hwrm_ring_grp_free(bp, queue_index);
2713
2714         if (ring != NULL && cpr != NULL)
2715                 bnxt_hwrm_ring_free(bp, ring,
2716                                     HWRM_RING_FREE_INPUT_RING_TYPE_RX,
2717                                     cpr->cp_ring_struct->fw_ring_id);
2718         if (BNXT_HAS_RING_GRPS(bp))
2719                 bp->grp_info[queue_index].rx_fw_ring_id = INVALID_HW_RING_ID;
2720
2721         /* Check agg ring struct explicitly.
2722          * bnxt_need_agg_ring() returns the current state of offload flags,
2723          * but we may have to deal with agg ring struct before the offload
2724          * flags are updated.
2725          */
2726         if (!bnxt_need_agg_ring(bp->eth_dev) ||
2727             (rxr && rxr->ag_ring_struct == NULL))
2728                 goto no_agg;
2729
2730         ring = rxr ? rxr->ag_ring_struct : NULL;
2731         if (ring != NULL && cpr != NULL) {
2732                 bnxt_hwrm_ring_free(bp, ring,
2733                                     BNXT_CHIP_P5(bp) ?
2734                                     HWRM_RING_FREE_INPUT_RING_TYPE_RX_AGG :
2735                                     HWRM_RING_FREE_INPUT_RING_TYPE_RX,
2736                                     cpr->cp_ring_struct->fw_ring_id);
2737         }
2738         if (BNXT_HAS_RING_GRPS(bp))
2739                 bp->grp_info[queue_index].ag_fw_ring_id = INVALID_HW_RING_ID;
2740
2741 no_agg:
2742         if (cpr != NULL) {
2743                 bnxt_hwrm_stat_ctx_free(bp, cpr);
2744                 bnxt_free_cp_ring(bp, cpr);
2745         }
2746
2747         if (BNXT_HAS_RING_GRPS(bp))
2748                 bp->grp_info[queue_index].cp_fw_ring_id = INVALID_HW_RING_ID;
2749 }
2750
2751 int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int queue_index)
2752 {
2753         int rc;
2754         struct hwrm_ring_reset_input req = {.req_type = 0 };
2755         struct hwrm_ring_reset_output *resp = bp->hwrm_cmd_resp_addr;
2756
2757         HWRM_PREP(&req, HWRM_RING_RESET, BNXT_USE_CHIMP_MB);
2758
2759         req.ring_type = HWRM_RING_RESET_INPUT_RING_TYPE_RX_RING_GRP;
2760         req.ring_id = rte_cpu_to_le_16(bp->grp_info[queue_index].fw_grp_id);
2761         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
2762
2763         HWRM_CHECK_RESULT();
2764
2765         HWRM_UNLOCK();
2766
2767         return rc;
2768 }
2769
2770 static int
2771 bnxt_free_all_hwrm_rings(struct bnxt *bp)
2772 {
2773         unsigned int i;
2774
2775         for (i = 0; i < bp->tx_cp_nr_rings; i++)
2776                 bnxt_free_hwrm_tx_ring(bp, i);
2777
2778         for (i = 0; i < bp->rx_cp_nr_rings; i++)
2779                 bnxt_free_hwrm_rx_ring(bp, i);
2780
2781         return 0;
2782 }
2783
2784 int bnxt_alloc_all_hwrm_ring_grps(struct bnxt *bp)
2785 {
2786         uint16_t i;
2787         uint32_t rc = 0;
2788
2789         if (!BNXT_HAS_RING_GRPS(bp))
2790                 return 0;
2791
2792         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
2793                 rc = bnxt_hwrm_ring_grp_alloc(bp, i);
2794                 if (rc)
2795                         return rc;
2796         }
2797         return rc;
2798 }
2799
2800 /*
2801  * HWRM utility functions
2802  */
2803
2804 void bnxt_free_hwrm_resources(struct bnxt *bp)
2805 {
2806         /* Release memzone */
2807         rte_free(bp->hwrm_cmd_resp_addr);
2808         rte_free(bp->hwrm_short_cmd_req_addr);
2809         bp->hwrm_cmd_resp_addr = NULL;
2810         bp->hwrm_short_cmd_req_addr = NULL;
2811         bp->hwrm_cmd_resp_dma_addr = 0;
2812         bp->hwrm_short_cmd_req_dma_addr = 0;
2813 }
2814
2815 int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2816 {
2817         struct rte_pci_device *pdev = bp->pdev;
2818         char type[RTE_MEMZONE_NAMESIZE];
2819
2820         sprintf(type, "bnxt_hwrm_" PCI_PRI_FMT, pdev->addr.domain,
2821                 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
2822         bp->max_resp_len = BNXT_PAGE_SIZE;
2823         bp->hwrm_cmd_resp_addr = rte_malloc(type, bp->max_resp_len, 0);
2824         if (bp->hwrm_cmd_resp_addr == NULL)
2825                 return -ENOMEM;
2826         bp->hwrm_cmd_resp_dma_addr =
2827                 rte_malloc_virt2iova(bp->hwrm_cmd_resp_addr);
2828         if (bp->hwrm_cmd_resp_dma_addr == RTE_BAD_IOVA) {
2829                 PMD_DRV_LOG(ERR,
2830                         "unable to map response address to physical memory\n");
2831                 return -ENOMEM;
2832         }
2833         rte_spinlock_init(&bp->hwrm_lock);
2834
2835         return 0;
2836 }
2837
2838 int
2839 bnxt_clear_one_vnic_filter(struct bnxt *bp, struct bnxt_filter_info *filter)
2840 {
2841         int rc = 0;
2842
2843         if (filter->filter_type == HWRM_CFA_EM_FILTER) {
2844                 rc = bnxt_hwrm_clear_em_filter(bp, filter);
2845                 if (rc)
2846                         return rc;
2847         } else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER) {
2848                 rc = bnxt_hwrm_clear_ntuple_filter(bp, filter);
2849                 if (rc)
2850                         return rc;
2851         }
2852
2853         rc = bnxt_hwrm_clear_l2_filter(bp, filter);
2854         return rc;
2855 }
2856
2857 static int
2858 bnxt_clear_hwrm_vnic_filters(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2859 {
2860         struct bnxt_filter_info *filter;
2861         int rc = 0;
2862
2863         STAILQ_FOREACH(filter, &vnic->filter, next) {
2864                 rc = bnxt_clear_one_vnic_filter(bp, filter);
2865                 STAILQ_REMOVE(&vnic->filter, filter, bnxt_filter_info, next);
2866                 bnxt_free_filter(bp, filter);
2867         }
2868         return rc;
2869 }
2870
2871 static int
2872 bnxt_clear_hwrm_vnic_flows(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2873 {
2874         struct bnxt_filter_info *filter;
2875         struct rte_flow *flow;
2876         int rc = 0;
2877
2878         while (!STAILQ_EMPTY(&vnic->flow_list)) {
2879                 flow = STAILQ_FIRST(&vnic->flow_list);
2880                 filter = flow->filter;
2881                 PMD_DRV_LOG(DEBUG, "filter type %d\n", filter->filter_type);
2882                 rc = bnxt_clear_one_vnic_filter(bp, filter);
2883
2884                 STAILQ_REMOVE(&vnic->flow_list, flow, rte_flow, next);
2885                 rte_free(flow);
2886         }
2887         return rc;
2888 }
2889
2890 int bnxt_set_hwrm_vnic_filters(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2891 {
2892         struct bnxt_filter_info *filter;
2893         int rc = 0;
2894
2895         STAILQ_FOREACH(filter, &vnic->filter, next) {
2896                 if (filter->filter_type == HWRM_CFA_EM_FILTER)
2897                         rc = bnxt_hwrm_set_em_filter(bp, filter->dst_id,
2898                                                      filter);
2899                 else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER)
2900                         rc = bnxt_hwrm_set_ntuple_filter(bp, filter->dst_id,
2901                                                          filter);
2902                 else
2903                         rc = bnxt_hwrm_set_l2_filter(bp, vnic->fw_vnic_id,
2904                                                      filter);
2905                 if (rc)
2906                         break;
2907         }
2908         return rc;
2909 }
2910
2911 static void
2912 bnxt_free_tunnel_ports(struct bnxt *bp)
2913 {
2914         if (bp->vxlan_port_cnt)
2915                 bnxt_hwrm_tunnel_dst_port_free(bp, bp->vxlan_fw_dst_port_id,
2916                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_VXLAN);
2917
2918         if (bp->geneve_port_cnt)
2919                 bnxt_hwrm_tunnel_dst_port_free(bp, bp->geneve_fw_dst_port_id,
2920                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_GENEVE);
2921 }
2922
2923 void bnxt_free_all_hwrm_resources(struct bnxt *bp)
2924 {
2925         int i;
2926
2927         if (bp->vnic_info == NULL)
2928                 return;
2929
2930         /*
2931          * Cleanup VNICs in reverse order, to make sure the L2 filter
2932          * from vnic0 is last to be cleaned up.
2933          */
2934         for (i = bp->max_vnics - 1; i >= 0; i--) {
2935                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2936
2937                 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
2938                         continue;
2939
2940                 bnxt_clear_hwrm_vnic_flows(bp, vnic);
2941
2942                 bnxt_clear_hwrm_vnic_filters(bp, vnic);
2943
2944                 bnxt_hwrm_vnic_ctx_free(bp, vnic);
2945
2946                 bnxt_hwrm_vnic_tpa_cfg(bp, vnic, false);
2947
2948                 bnxt_hwrm_vnic_free(bp, vnic);
2949
2950                 rte_free(vnic->fw_grp_ids);
2951         }
2952         /* Ring resources */
2953         bnxt_free_all_hwrm_rings(bp);
2954         bnxt_free_all_hwrm_ring_grps(bp);
2955         bnxt_free_all_hwrm_stat_ctxs(bp);
2956         bnxt_free_tunnel_ports(bp);
2957 }
2958
2959 static uint16_t bnxt_parse_eth_link_duplex(uint32_t conf_link_speed)
2960 {
2961         uint8_t hw_link_duplex = HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH;
2962
2963         if ((conf_link_speed & RTE_ETH_LINK_SPEED_FIXED) == RTE_ETH_LINK_SPEED_AUTONEG)
2964                 return HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH;
2965
2966         switch (conf_link_speed) {
2967         case RTE_ETH_LINK_SPEED_10M_HD:
2968         case RTE_ETH_LINK_SPEED_100M_HD:
2969                 /* FALLTHROUGH */
2970                 return HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_HALF;
2971         }
2972         return hw_link_duplex;
2973 }
2974
2975 static uint16_t bnxt_check_eth_link_autoneg(uint32_t conf_link)
2976 {
2977         return !conf_link;
2978 }
2979
2980 static uint16_t bnxt_parse_eth_link_speed(uint32_t conf_link_speed,
2981                                           uint16_t pam4_link)
2982 {
2983         uint16_t eth_link_speed = 0;
2984
2985         if (conf_link_speed == RTE_ETH_LINK_SPEED_AUTONEG)
2986                 return RTE_ETH_LINK_SPEED_AUTONEG;
2987
2988         switch (conf_link_speed & ~RTE_ETH_LINK_SPEED_FIXED) {
2989         case RTE_ETH_LINK_SPEED_100M:
2990         case RTE_ETH_LINK_SPEED_100M_HD:
2991                 /* FALLTHROUGH */
2992                 eth_link_speed =
2993                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_100MB;
2994                 break;
2995         case RTE_ETH_LINK_SPEED_1G:
2996                 eth_link_speed =
2997                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_1GB;
2998                 break;
2999         case RTE_ETH_LINK_SPEED_2_5G:
3000                 eth_link_speed =
3001                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_2_5GB;
3002                 break;
3003         case RTE_ETH_LINK_SPEED_10G:
3004                 eth_link_speed =
3005                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_10GB;
3006                 break;
3007         case RTE_ETH_LINK_SPEED_20G:
3008                 eth_link_speed =
3009                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_20GB;
3010                 break;
3011         case RTE_ETH_LINK_SPEED_25G:
3012                 eth_link_speed =
3013                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_25GB;
3014                 break;
3015         case RTE_ETH_LINK_SPEED_40G:
3016                 eth_link_speed =
3017                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_40GB;
3018                 break;
3019         case RTE_ETH_LINK_SPEED_50G:
3020                 eth_link_speed = pam4_link ?
3021                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAM4_LINK_SPEED_50GB :
3022                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_50GB;
3023                 break;
3024         case RTE_ETH_LINK_SPEED_100G:
3025                 eth_link_speed = pam4_link ?
3026                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAM4_LINK_SPEED_100GB :
3027                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_100GB;
3028                 break;
3029         case RTE_ETH_LINK_SPEED_200G:
3030                 eth_link_speed =
3031                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAM4_LINK_SPEED_200GB;
3032                 break;
3033         default:
3034                 PMD_DRV_LOG(ERR,
3035                         "Unsupported link speed %d; default to AUTO\n",
3036                         conf_link_speed);
3037                 break;
3038         }
3039         return eth_link_speed;
3040 }
3041
3042 #define BNXT_SUPPORTED_SPEEDS (RTE_ETH_LINK_SPEED_100M | RTE_ETH_LINK_SPEED_100M_HD | \
3043                 RTE_ETH_LINK_SPEED_1G | RTE_ETH_LINK_SPEED_2_5G | \
3044                 RTE_ETH_LINK_SPEED_10G | RTE_ETH_LINK_SPEED_20G | RTE_ETH_LINK_SPEED_25G | \
3045                 RTE_ETH_LINK_SPEED_40G | RTE_ETH_LINK_SPEED_50G | \
3046                 RTE_ETH_LINK_SPEED_100G | RTE_ETH_LINK_SPEED_200G)
3047
3048 static int bnxt_validate_link_speed(struct bnxt *bp)
3049 {
3050         uint32_t link_speed = bp->eth_dev->data->dev_conf.link_speeds;
3051         uint16_t port_id = bp->eth_dev->data->port_id;
3052         uint32_t link_speed_capa;
3053         uint32_t one_speed;
3054
3055         if (link_speed == RTE_ETH_LINK_SPEED_AUTONEG)
3056                 return 0;
3057
3058         link_speed_capa = bnxt_get_speed_capabilities(bp);
3059
3060         if (link_speed & RTE_ETH_LINK_SPEED_FIXED) {
3061                 one_speed = link_speed & ~RTE_ETH_LINK_SPEED_FIXED;
3062
3063                 if (one_speed & (one_speed - 1)) {
3064                         PMD_DRV_LOG(ERR,
3065                                 "Invalid advertised speeds (%u) for port %u\n",
3066                                 link_speed, port_id);
3067                         return -EINVAL;
3068                 }
3069                 if ((one_speed & link_speed_capa) != one_speed) {
3070                         PMD_DRV_LOG(ERR,
3071                                 "Unsupported advertised speed (%u) for port %u\n",
3072                                 link_speed, port_id);
3073                         return -EINVAL;
3074                 }
3075         } else {
3076                 if (!(link_speed & link_speed_capa)) {
3077                         PMD_DRV_LOG(ERR,
3078                                 "Unsupported advertised speeds (%u) for port %u\n",
3079                                 link_speed, port_id);
3080                         return -EINVAL;
3081                 }
3082         }
3083         return 0;
3084 }
3085
3086 static uint16_t
3087 bnxt_parse_eth_link_speed_mask(struct bnxt *bp, uint32_t link_speed)
3088 {
3089         uint16_t ret = 0;
3090
3091         if (link_speed == RTE_ETH_LINK_SPEED_AUTONEG) {
3092                 if (bp->link_info->support_speeds)
3093                         return bp->link_info->support_speeds;
3094                 link_speed = BNXT_SUPPORTED_SPEEDS;
3095         }
3096
3097         if (link_speed & RTE_ETH_LINK_SPEED_100M)
3098                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100MB;
3099         if (link_speed & RTE_ETH_LINK_SPEED_100M_HD)
3100                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100MB;
3101         if (link_speed & RTE_ETH_LINK_SPEED_1G)
3102                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_1GB;
3103         if (link_speed & RTE_ETH_LINK_SPEED_2_5G)
3104                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_2_5GB;
3105         if (link_speed & RTE_ETH_LINK_SPEED_10G)
3106                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_10GB;
3107         if (link_speed & RTE_ETH_LINK_SPEED_20G)
3108                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_20GB;
3109         if (link_speed & RTE_ETH_LINK_SPEED_25G)
3110                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_25GB;
3111         if (link_speed & RTE_ETH_LINK_SPEED_40G)
3112                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_40GB;
3113         if (link_speed & RTE_ETH_LINK_SPEED_50G)
3114                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_50GB;
3115         if (link_speed & RTE_ETH_LINK_SPEED_100G)
3116                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100GB;
3117         if (link_speed & RTE_ETH_LINK_SPEED_200G)
3118                 ret |= HWRM_PORT_PHY_CFG_INPUT_FORCE_PAM4_LINK_SPEED_200GB;
3119         return ret;
3120 }
3121
3122 static uint32_t bnxt_parse_hw_link_speed(uint16_t hw_link_speed)
3123 {
3124         uint32_t eth_link_speed = RTE_ETH_SPEED_NUM_NONE;
3125
3126         switch (hw_link_speed) {
3127         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100MB:
3128                 eth_link_speed = RTE_ETH_SPEED_NUM_100M;
3129                 break;
3130         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_1GB:
3131                 eth_link_speed = RTE_ETH_SPEED_NUM_1G;
3132                 break;
3133         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2_5GB:
3134                 eth_link_speed = RTE_ETH_SPEED_NUM_2_5G;
3135                 break;
3136         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_10GB:
3137                 eth_link_speed = RTE_ETH_SPEED_NUM_10G;
3138                 break;
3139         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_20GB:
3140                 eth_link_speed = RTE_ETH_SPEED_NUM_20G;
3141                 break;
3142         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_25GB:
3143                 eth_link_speed = RTE_ETH_SPEED_NUM_25G;
3144                 break;
3145         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_40GB:
3146                 eth_link_speed = RTE_ETH_SPEED_NUM_40G;
3147                 break;
3148         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_50GB:
3149                 eth_link_speed = RTE_ETH_SPEED_NUM_50G;
3150                 break;
3151         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100GB:
3152                 eth_link_speed = RTE_ETH_SPEED_NUM_100G;
3153                 break;
3154         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_200GB:
3155                 eth_link_speed = RTE_ETH_SPEED_NUM_200G;
3156                 break;
3157         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2GB:
3158         default:
3159                 PMD_DRV_LOG(ERR, "HWRM link speed %d not defined\n",
3160                         hw_link_speed);
3161                 break;
3162         }
3163         return eth_link_speed;
3164 }
3165
3166 static uint16_t bnxt_parse_hw_link_duplex(uint16_t hw_link_duplex)
3167 {
3168         uint16_t eth_link_duplex = RTE_ETH_LINK_FULL_DUPLEX;
3169
3170         switch (hw_link_duplex) {
3171         case HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH:
3172         case HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_FULL:
3173                 /* FALLTHROUGH */
3174                 eth_link_duplex = RTE_ETH_LINK_FULL_DUPLEX;
3175                 break;
3176         case HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_HALF:
3177                 eth_link_duplex = RTE_ETH_LINK_HALF_DUPLEX;
3178                 break;
3179         default:
3180                 PMD_DRV_LOG(ERR, "HWRM link duplex %d not defined\n",
3181                         hw_link_duplex);
3182                 break;
3183         }
3184         return eth_link_duplex;
3185 }
3186
3187 int bnxt_get_hwrm_link_config(struct bnxt *bp, struct rte_eth_link *link)
3188 {
3189         int rc = 0;
3190         struct bnxt_link_info *link_info = bp->link_info;
3191
3192         rc = bnxt_hwrm_port_phy_qcaps(bp);
3193         if (rc)
3194                 PMD_DRV_LOG(ERR, "Get link config failed with rc %d\n", rc);
3195
3196         rc = bnxt_hwrm_port_phy_qcfg(bp, link_info);
3197         if (rc) {
3198                 PMD_DRV_LOG(ERR, "Get link config failed with rc %d\n", rc);
3199                 goto exit;
3200         }
3201
3202         if (link_info->link_speed)
3203                 link->link_speed =
3204                         bnxt_parse_hw_link_speed(link_info->link_speed);
3205         else
3206                 link->link_speed = RTE_ETH_SPEED_NUM_NONE;
3207         link->link_duplex = bnxt_parse_hw_link_duplex(link_info->duplex);
3208         link->link_status = link_info->link_up;
3209         link->link_autoneg = link_info->auto_mode ==
3210                 HWRM_PORT_PHY_QCFG_OUTPUT_AUTO_MODE_NONE ?
3211                 RTE_ETH_LINK_FIXED : RTE_ETH_LINK_AUTONEG;
3212 exit:
3213         return rc;
3214 }
3215
3216 int bnxt_set_hwrm_link_config(struct bnxt *bp, bool link_up)
3217 {
3218         int rc = 0;
3219         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
3220         struct bnxt_link_info link_req;
3221         uint16_t speed, autoneg;
3222
3223         if (!BNXT_SINGLE_PF(bp) || BNXT_VF(bp))
3224                 return 0;
3225
3226         rc = bnxt_validate_link_speed(bp);
3227         if (rc)
3228                 goto error;
3229
3230         memset(&link_req, 0, sizeof(link_req));
3231         link_req.link_up = link_up;
3232         if (!link_up)
3233                 goto port_phy_cfg;
3234
3235         autoneg = bnxt_check_eth_link_autoneg(dev_conf->link_speeds);
3236         if (BNXT_CHIP_P5(bp) &&
3237             dev_conf->link_speeds == RTE_ETH_LINK_SPEED_40G) {
3238                 /* 40G is not supported as part of media auto detect.
3239                  * The speed should be forced and autoneg disabled
3240                  * to configure 40G speed.
3241                  */
3242                 PMD_DRV_LOG(INFO, "Disabling autoneg for 40G\n");
3243                 autoneg = 0;
3244         }
3245
3246         /* No auto speeds and no auto_pam4_link. Disable autoneg */
3247         if (bp->link_info->auto_link_speed == 0 &&
3248             bp->link_info->link_signal_mode &&
3249             bp->link_info->auto_pam4_link_speed_mask == 0)
3250                 autoneg = 0;
3251
3252         speed = bnxt_parse_eth_link_speed(dev_conf->link_speeds,
3253                                           bp->link_info->link_signal_mode);
3254         link_req.phy_flags = HWRM_PORT_PHY_CFG_INPUT_FLAGS_RESET_PHY;
3255         /* Autoneg can be done only when the FW allows. */
3256         if (autoneg == 1 && bp->link_info->support_auto_speeds) {
3257                 link_req.phy_flags |=
3258                                 HWRM_PORT_PHY_CFG_INPUT_FLAGS_RESTART_AUTONEG;
3259                 link_req.auto_link_speed_mask =
3260                         bnxt_parse_eth_link_speed_mask(bp,
3261                                                        dev_conf->link_speeds);
3262                 link_req.auto_pam4_link_speed_mask =
3263                         bp->link_info->auto_pam4_link_speed_mask;
3264         } else {
3265                 if (bp->link_info->phy_type ==
3266                     HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET ||
3267                     bp->link_info->phy_type ==
3268                     HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASETE ||
3269                     bp->link_info->media_type ==
3270                     HWRM_PORT_PHY_QCFG_OUTPUT_MEDIA_TYPE_TP) {
3271                         PMD_DRV_LOG(ERR, "10GBase-T devices must autoneg\n");
3272                         return -EINVAL;
3273                 }
3274
3275                 link_req.phy_flags |= HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE;
3276                 /* If user wants a particular speed try that first. */
3277                 if (speed)
3278                         link_req.link_speed = speed;
3279                 else if (bp->link_info->force_pam4_link_speed)
3280                         link_req.link_speed =
3281                                 bp->link_info->force_pam4_link_speed;
3282                 else if (bp->link_info->auto_pam4_link_speed_mask)
3283                         link_req.link_speed =
3284                                 bp->link_info->auto_pam4_link_speed_mask;
3285                 else if (bp->link_info->support_pam4_speeds)
3286                         link_req.link_speed =
3287                                 bp->link_info->support_pam4_speeds;
3288                 else if (bp->link_info->force_link_speed)
3289                         link_req.link_speed = bp->link_info->force_link_speed;
3290                 else
3291                         link_req.link_speed = bp->link_info->auto_link_speed;
3292                 /* Auto PAM4 link speed is zero, but auto_link_speed is not
3293                  * zero. Use the auto_link_speed.
3294                  */
3295                 if (bp->link_info->auto_link_speed != 0 &&
3296                     bp->link_info->auto_pam4_link_speed_mask == 0)
3297                         link_req.link_speed = bp->link_info->auto_link_speed;
3298         }
3299         link_req.duplex = bnxt_parse_eth_link_duplex(dev_conf->link_speeds);
3300         link_req.auto_pause = bp->link_info->auto_pause;
3301         link_req.force_pause = bp->link_info->force_pause;
3302
3303 port_phy_cfg:
3304         rc = bnxt_hwrm_port_phy_cfg(bp, &link_req);
3305         if (rc) {
3306                 PMD_DRV_LOG(ERR,
3307                         "Set link config failed with rc %d\n", rc);
3308         }
3309
3310 error:
3311         return rc;
3312 }
3313
3314 int bnxt_hwrm_func_qcfg(struct bnxt *bp, uint16_t *mtu)
3315 {
3316         struct hwrm_func_qcfg_input req = {0};
3317         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3318         uint16_t flags;
3319         int rc = 0;
3320         bp->func_svif = BNXT_SVIF_INVALID;
3321         uint16_t svif_info;
3322
3323         HWRM_PREP(&req, HWRM_FUNC_QCFG, BNXT_USE_CHIMP_MB);
3324         req.fid = rte_cpu_to_le_16(0xffff);
3325
3326         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3327
3328         HWRM_CHECK_RESULT();
3329
3330         bp->vlan = rte_le_to_cpu_16(resp->vlan) & RTE_ETH_VLAN_ID_MAX;
3331
3332         svif_info = rte_le_to_cpu_16(resp->svif_info);
3333         if (svif_info & HWRM_FUNC_QCFG_OUTPUT_SVIF_INFO_SVIF_VALID)
3334                 bp->func_svif = svif_info &
3335                                      HWRM_FUNC_QCFG_OUTPUT_SVIF_INFO_SVIF_MASK;
3336
3337         flags = rte_le_to_cpu_16(resp->flags);
3338         if (BNXT_PF(bp) && (flags & HWRM_FUNC_QCFG_OUTPUT_FLAGS_MULTI_HOST))
3339                 bp->flags |= BNXT_FLAG_MULTI_HOST;
3340
3341         if (BNXT_VF(bp) &&
3342             !BNXT_VF_IS_TRUSTED(bp) &&
3343             (flags & HWRM_FUNC_QCFG_OUTPUT_FLAGS_TRUSTED_VF)) {
3344                 bp->flags |= BNXT_FLAG_TRUSTED_VF_EN;
3345                 PMD_DRV_LOG(INFO, "Trusted VF cap enabled\n");
3346         } else if (BNXT_VF(bp) &&
3347                    BNXT_VF_IS_TRUSTED(bp) &&
3348                    !(flags & HWRM_FUNC_QCFG_OUTPUT_FLAGS_TRUSTED_VF)) {
3349                 bp->flags &= ~BNXT_FLAG_TRUSTED_VF_EN;
3350                 PMD_DRV_LOG(INFO, "Trusted VF cap disabled\n");
3351         }
3352
3353         if (mtu)
3354                 *mtu = rte_le_to_cpu_16(resp->admin_mtu);
3355
3356         switch (resp->port_partition_type) {
3357         case HWRM_FUNC_QCFG_OUTPUT_PORT_PARTITION_TYPE_NPAR1_0:
3358         case HWRM_FUNC_QCFG_OUTPUT_PORT_PARTITION_TYPE_NPAR1_5:
3359         case HWRM_FUNC_QCFG_OUTPUT_PORT_PARTITION_TYPE_NPAR2_0:
3360                 /* FALLTHROUGH */
3361                 bp->flags |= BNXT_FLAG_NPAR_PF;
3362                 break;
3363         default:
3364                 bp->flags &= ~BNXT_FLAG_NPAR_PF;
3365                 break;
3366         }
3367
3368         bp->legacy_db_size =
3369                 rte_le_to_cpu_16(resp->legacy_l2_db_size_kb) * 1024;
3370
3371         HWRM_UNLOCK();
3372
3373         return rc;
3374 }
3375
3376 int bnxt_hwrm_parent_pf_qcfg(struct bnxt *bp)
3377 {
3378         struct hwrm_func_qcfg_input req = {0};
3379         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3380         uint16_t flags;
3381         int rc;
3382
3383         if (!BNXT_VF_IS_TRUSTED(bp))
3384                 return 0;
3385
3386         if (!bp->parent)
3387                 return -EINVAL;
3388
3389         bp->parent->fid = BNXT_PF_FID_INVALID;
3390
3391         HWRM_PREP(&req, HWRM_FUNC_QCFG, BNXT_USE_CHIMP_MB);
3392
3393         req.fid = rte_cpu_to_le_16(0xfffe); /* Request parent PF information. */
3394
3395         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3396
3397         HWRM_CHECK_RESULT_SILENT();
3398
3399         memcpy(bp->parent->mac_addr, resp->mac_address, RTE_ETHER_ADDR_LEN);
3400         bp->parent->vnic = rte_le_to_cpu_16(resp->dflt_vnic_id);
3401         bp->parent->fid = rte_le_to_cpu_16(resp->fid);
3402         bp->parent->port_id = rte_le_to_cpu_16(resp->port_id);
3403
3404         flags = rte_le_to_cpu_16(resp->flags);
3405         /* check for the multi-root support */
3406         if (flags & HWRM_FUNC_QCFG_OUTPUT_FLAGS_MULTI_ROOT) {
3407                 bp->flags2 |= BNXT_FLAGS2_MULTIROOT_EN;
3408                 PMD_DRV_LOG(DEBUG, "PF enabled with multi root capability\n");
3409         }
3410
3411         HWRM_UNLOCK();
3412
3413         return 0;
3414 }
3415
3416 int bnxt_hwrm_get_dflt_vnic_svif(struct bnxt *bp, uint16_t fid,
3417                                  uint16_t *vnic_id, uint16_t *svif)
3418 {
3419         struct hwrm_func_qcfg_input req = {0};
3420         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3421         uint16_t svif_info;
3422         int rc = 0;
3423
3424         HWRM_PREP(&req, HWRM_FUNC_QCFG, BNXT_USE_CHIMP_MB);
3425         req.fid = rte_cpu_to_le_16(fid);
3426
3427         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3428
3429         HWRM_CHECK_RESULT();
3430
3431         if (vnic_id)
3432                 *vnic_id = rte_le_to_cpu_16(resp->dflt_vnic_id);
3433
3434         svif_info = rte_le_to_cpu_16(resp->svif_info);
3435         if (svif && (svif_info & HWRM_FUNC_QCFG_OUTPUT_SVIF_INFO_SVIF_VALID))
3436                 *svif = svif_info & HWRM_FUNC_QCFG_OUTPUT_SVIF_INFO_SVIF_MASK;
3437
3438         HWRM_UNLOCK();
3439
3440         return rc;
3441 }
3442
3443 int bnxt_hwrm_port_mac_qcfg(struct bnxt *bp)
3444 {
3445         struct hwrm_port_mac_qcfg_input req = {0};
3446         struct hwrm_port_mac_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3447         uint16_t port_svif_info;
3448         int rc;
3449
3450         bp->port_svif = BNXT_SVIF_INVALID;
3451
3452         if (BNXT_VF(bp) && !BNXT_VF_IS_TRUSTED(bp))
3453                 return 0;
3454
3455         HWRM_PREP(&req, HWRM_PORT_MAC_QCFG, BNXT_USE_CHIMP_MB);
3456
3457         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3458
3459         HWRM_CHECK_RESULT_SILENT();
3460
3461         port_svif_info = rte_le_to_cpu_16(resp->port_svif_info);
3462         if (port_svif_info &
3463             HWRM_PORT_MAC_QCFG_OUTPUT_PORT_SVIF_INFO_PORT_SVIF_VALID)
3464                 bp->port_svif = port_svif_info &
3465                         HWRM_PORT_MAC_QCFG_OUTPUT_PORT_SVIF_INFO_PORT_SVIF_MASK;
3466
3467         HWRM_UNLOCK();
3468
3469         return 0;
3470 }
3471
3472 static int bnxt_hwrm_pf_func_cfg(struct bnxt *bp,
3473                                  struct bnxt_pf_resource_info *pf_resc)
3474 {
3475         struct hwrm_func_cfg_input req = {0};
3476         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3477         uint32_t enables;
3478         int rc;
3479
3480         enables = HWRM_FUNC_CFG_INPUT_ENABLES_ADMIN_MTU |
3481                   HWRM_FUNC_CFG_INPUT_ENABLES_HOST_MTU |
3482                   HWRM_FUNC_CFG_INPUT_ENABLES_MRU |
3483                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RSSCOS_CTXS |
3484                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_STAT_CTXS |
3485                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_CMPL_RINGS |
3486                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_TX_RINGS |
3487                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RX_RINGS |
3488                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_L2_CTXS |
3489                   HWRM_FUNC_CFG_INPUT_ENABLES_NUM_VNICS;
3490
3491         if (BNXT_HAS_RING_GRPS(bp)) {
3492                 enables |= HWRM_FUNC_CFG_INPUT_ENABLES_NUM_HW_RING_GRPS;
3493                 req.num_hw_ring_grps =
3494                         rte_cpu_to_le_16(pf_resc->num_hw_ring_grps);
3495         } else if (BNXT_HAS_NQ(bp)) {
3496                 enables |= HWRM_FUNC_CFG_INPUT_ENABLES_NUM_MSIX;
3497                 req.num_msix = rte_cpu_to_le_16(bp->max_nq_rings);
3498         }
3499
3500         req.flags = rte_cpu_to_le_32(bp->pf->func_cfg_flags);
3501         req.admin_mtu = rte_cpu_to_le_16(BNXT_MAX_MTU);
3502         req.host_mtu = rte_cpu_to_le_16(bp->eth_dev->data->mtu);
3503         req.mru = rte_cpu_to_le_16(BNXT_VNIC_MRU(bp->eth_dev->data->mtu));
3504         req.num_rsscos_ctxs = rte_cpu_to_le_16(pf_resc->num_rsscos_ctxs);
3505         req.num_stat_ctxs = rte_cpu_to_le_16(pf_resc->num_stat_ctxs);
3506         req.num_cmpl_rings = rte_cpu_to_le_16(pf_resc->num_cp_rings);
3507         req.num_tx_rings = rte_cpu_to_le_16(pf_resc->num_tx_rings);
3508         req.num_rx_rings = rte_cpu_to_le_16(pf_resc->num_rx_rings);
3509         req.num_l2_ctxs = rte_cpu_to_le_16(pf_resc->num_l2_ctxs);
3510         req.num_vnics = rte_cpu_to_le_16(bp->max_vnics);
3511         req.fid = rte_cpu_to_le_16(0xffff);
3512         req.enables = rte_cpu_to_le_32(enables);
3513
3514         HWRM_PREP(&req, HWRM_FUNC_CFG, BNXT_USE_CHIMP_MB);
3515
3516         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3517
3518         HWRM_CHECK_RESULT();
3519         HWRM_UNLOCK();
3520
3521         return rc;
3522 }
3523
3524 /* min values are the guaranteed resources and max values are subject
3525  * to availability. The strategy for now is to keep both min & max
3526  * values the same.
3527  */
3528 static void
3529 bnxt_fill_vf_func_cfg_req_new(struct bnxt *bp,
3530                               struct hwrm_func_vf_resource_cfg_input *req,
3531                               int num_vfs)
3532 {
3533         req->max_rsscos_ctx = rte_cpu_to_le_16(bp->max_rsscos_ctx /
3534                                                (num_vfs + 1));
3535         req->min_rsscos_ctx = req->max_rsscos_ctx;
3536         req->max_stat_ctx = rte_cpu_to_le_16(bp->max_stat_ctx / (num_vfs + 1));
3537         req->min_stat_ctx = req->max_stat_ctx;
3538         req->max_cmpl_rings = rte_cpu_to_le_16(bp->max_cp_rings /
3539                                                (num_vfs + 1));
3540         req->min_cmpl_rings = req->max_cmpl_rings;
3541         req->max_tx_rings = rte_cpu_to_le_16(bp->max_tx_rings / (num_vfs + 1));
3542         req->min_tx_rings = req->max_tx_rings;
3543         req->max_rx_rings = rte_cpu_to_le_16(bp->max_rx_rings / (num_vfs + 1));
3544         req->min_rx_rings = req->max_rx_rings;
3545         req->max_l2_ctxs = rte_cpu_to_le_16(bp->max_l2_ctx / (num_vfs + 1));
3546         req->min_l2_ctxs = req->max_l2_ctxs;
3547         /* TODO: For now, do not support VMDq/RFS on VFs. */
3548         req->max_vnics = rte_cpu_to_le_16(1);
3549         req->min_vnics = req->max_vnics;
3550         req->max_hw_ring_grps = rte_cpu_to_le_16(bp->max_ring_grps /
3551                                                  (num_vfs + 1));
3552         req->min_hw_ring_grps = req->max_hw_ring_grps;
3553         req->flags =
3554          rte_cpu_to_le_16(HWRM_FUNC_VF_RESOURCE_CFG_INPUT_FLAGS_MIN_GUARANTEED);
3555 }
3556
3557 static void
3558 bnxt_fill_vf_func_cfg_req_old(struct bnxt *bp,
3559                               struct hwrm_func_cfg_input *req,
3560                               int num_vfs)
3561 {
3562         req->enables = rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_ADMIN_MTU |
3563                         HWRM_FUNC_CFG_INPUT_ENABLES_MRU |
3564                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RSSCOS_CTXS |
3565                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_STAT_CTXS |
3566                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_CMPL_RINGS |
3567                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_TX_RINGS |
3568                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RX_RINGS |
3569                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_L2_CTXS |
3570                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_VNICS |
3571                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_HW_RING_GRPS);
3572
3573         req->admin_mtu = rte_cpu_to_le_16(bp->eth_dev->data->mtu + RTE_ETHER_HDR_LEN +
3574                                           RTE_ETHER_CRC_LEN + RTE_VLAN_HLEN *
3575                                           BNXT_NUM_VLANS);
3576         req->mru = rte_cpu_to_le_16(BNXT_VNIC_MRU(bp->eth_dev->data->mtu));
3577         req->num_rsscos_ctxs = rte_cpu_to_le_16(bp->max_rsscos_ctx /
3578                                                 (num_vfs + 1));
3579         req->num_stat_ctxs = rte_cpu_to_le_16(bp->max_stat_ctx / (num_vfs + 1));
3580         req->num_cmpl_rings = rte_cpu_to_le_16(bp->max_cp_rings /
3581                                                (num_vfs + 1));
3582         req->num_tx_rings = rte_cpu_to_le_16(bp->max_tx_rings / (num_vfs + 1));
3583         req->num_rx_rings = rte_cpu_to_le_16(bp->max_rx_rings / (num_vfs + 1));
3584         req->num_l2_ctxs = rte_cpu_to_le_16(bp->max_l2_ctx / (num_vfs + 1));
3585         /* TODO: For now, do not support VMDq/RFS on VFs. */
3586         req->num_vnics = rte_cpu_to_le_16(1);
3587         req->num_hw_ring_grps = rte_cpu_to_le_16(bp->max_ring_grps /
3588                                                  (num_vfs + 1));
3589 }
3590
3591 /* Update the port wide resource values based on how many resources
3592  * got allocated to the VF.
3593  */
3594 static int bnxt_update_max_resources(struct bnxt *bp,
3595                                      int vf)
3596 {
3597         struct hwrm_func_qcfg_input req = {0};
3598         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3599         int rc;
3600
3601         /* Get the actual allocated values now */
3602         HWRM_PREP(&req, HWRM_FUNC_QCFG, BNXT_USE_CHIMP_MB);
3603         req.fid = rte_cpu_to_le_16(bp->pf->vf_info[vf].fid);
3604         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3605         HWRM_CHECK_RESULT();
3606
3607         bp->max_rsscos_ctx -= rte_le_to_cpu_16(resp->alloc_rsscos_ctx);
3608         bp->max_stat_ctx -= rte_le_to_cpu_16(resp->alloc_stat_ctx);
3609         bp->max_cp_rings -= rte_le_to_cpu_16(resp->alloc_cmpl_rings);
3610         bp->max_tx_rings -= rte_le_to_cpu_16(resp->alloc_tx_rings);
3611         bp->max_rx_rings -= rte_le_to_cpu_16(resp->alloc_rx_rings);
3612         bp->max_l2_ctx -= rte_le_to_cpu_16(resp->alloc_l2_ctx);
3613         bp->max_ring_grps -= rte_le_to_cpu_16(resp->alloc_hw_ring_grps);
3614
3615         HWRM_UNLOCK();
3616
3617         return 0;
3618 }
3619
3620 /* Update the PF resource values based on how many resources
3621  * got allocated to it.
3622  */
3623 static int bnxt_update_max_resources_pf_only(struct bnxt *bp)
3624 {
3625         struct hwrm_func_qcfg_input req = {0};
3626         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3627         int rc;
3628
3629         /* Get the actual allocated values now */
3630         HWRM_PREP(&req, HWRM_FUNC_QCFG, BNXT_USE_CHIMP_MB);
3631         req.fid = rte_cpu_to_le_16(0xffff);
3632         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3633         HWRM_CHECK_RESULT();
3634
3635         bp->max_rsscos_ctx = rte_le_to_cpu_16(resp->alloc_rsscos_ctx);
3636         bp->max_stat_ctx = rte_le_to_cpu_16(resp->alloc_stat_ctx);
3637         bp->max_cp_rings = rte_le_to_cpu_16(resp->alloc_cmpl_rings);
3638         bp->max_tx_rings = rte_le_to_cpu_16(resp->alloc_tx_rings);
3639         bp->max_rx_rings = rte_le_to_cpu_16(resp->alloc_rx_rings);
3640         bp->max_l2_ctx = rte_le_to_cpu_16(resp->alloc_l2_ctx);
3641         bp->max_ring_grps = rte_le_to_cpu_16(resp->alloc_hw_ring_grps);
3642         bp->max_vnics = rte_le_to_cpu_16(resp->alloc_vnics);
3643
3644         HWRM_UNLOCK();
3645
3646         return 0;
3647 }
3648
3649 int bnxt_hwrm_func_qcfg_current_vf_vlan(struct bnxt *bp, int vf)
3650 {
3651         struct hwrm_func_qcfg_input req = {0};
3652         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3653         int rc;
3654
3655         /* Check for zero MAC address */
3656         HWRM_PREP(&req, HWRM_FUNC_QCFG, BNXT_USE_CHIMP_MB);
3657         req.fid = rte_cpu_to_le_16(bp->pf->vf_info[vf].fid);
3658         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3659         HWRM_CHECK_RESULT();
3660         rc = rte_le_to_cpu_16(resp->vlan);
3661
3662         HWRM_UNLOCK();
3663
3664         return rc;
3665 }
3666
3667 static int bnxt_query_pf_resources(struct bnxt *bp,
3668                                    struct bnxt_pf_resource_info *pf_resc)
3669 {
3670         struct hwrm_func_qcfg_input req = {0};
3671         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3672         int rc;
3673
3674         /* And copy the allocated numbers into the pf struct */
3675         HWRM_PREP(&req, HWRM_FUNC_QCFG, BNXT_USE_CHIMP_MB);
3676         req.fid = rte_cpu_to_le_16(0xffff);
3677         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3678         HWRM_CHECK_RESULT();
3679
3680         pf_resc->num_tx_rings = rte_le_to_cpu_16(resp->alloc_tx_rings);
3681         pf_resc->num_rsscos_ctxs = rte_le_to_cpu_16(resp->alloc_rsscos_ctx);
3682         pf_resc->num_stat_ctxs = rte_le_to_cpu_16(resp->alloc_stat_ctx);
3683         pf_resc->num_cp_rings = rte_le_to_cpu_16(resp->alloc_cmpl_rings);
3684         pf_resc->num_rx_rings = rte_le_to_cpu_16(resp->alloc_rx_rings);
3685         pf_resc->num_l2_ctxs = rte_le_to_cpu_16(resp->alloc_l2_ctx);
3686         pf_resc->num_hw_ring_grps = rte_le_to_cpu_32(resp->alloc_hw_ring_grps);
3687         bp->pf->evb_mode = resp->evb_mode;
3688
3689         HWRM_UNLOCK();
3690
3691         return rc;
3692 }
3693
3694 static void
3695 bnxt_calculate_pf_resources(struct bnxt *bp,
3696                             struct bnxt_pf_resource_info *pf_resc,
3697                             int num_vfs)
3698 {
3699         if (!num_vfs) {
3700                 pf_resc->num_rsscos_ctxs = bp->max_rsscos_ctx;
3701                 pf_resc->num_stat_ctxs = bp->max_stat_ctx;
3702                 pf_resc->num_cp_rings = bp->max_cp_rings;
3703                 pf_resc->num_tx_rings = bp->max_tx_rings;
3704                 pf_resc->num_rx_rings = bp->max_rx_rings;
3705                 pf_resc->num_l2_ctxs = bp->max_l2_ctx;
3706                 pf_resc->num_hw_ring_grps = bp->max_ring_grps;
3707
3708                 return;
3709         }
3710
3711         pf_resc->num_rsscos_ctxs = bp->max_rsscos_ctx / (num_vfs + 1) +
3712                                    bp->max_rsscos_ctx % (num_vfs + 1);
3713         pf_resc->num_stat_ctxs = bp->max_stat_ctx / (num_vfs + 1) +
3714                                  bp->max_stat_ctx % (num_vfs + 1);
3715         pf_resc->num_cp_rings = bp->max_cp_rings / (num_vfs + 1) +
3716                                 bp->max_cp_rings % (num_vfs + 1);
3717         pf_resc->num_tx_rings = bp->max_tx_rings / (num_vfs + 1) +
3718                                 bp->max_tx_rings % (num_vfs + 1);
3719         pf_resc->num_rx_rings = bp->max_rx_rings / (num_vfs + 1) +
3720                                 bp->max_rx_rings % (num_vfs + 1);
3721         pf_resc->num_l2_ctxs = bp->max_l2_ctx / (num_vfs + 1) +
3722                                bp->max_l2_ctx % (num_vfs + 1);
3723         pf_resc->num_hw_ring_grps = bp->max_ring_grps / (num_vfs + 1) +
3724                                     bp->max_ring_grps % (num_vfs + 1);
3725 }
3726
3727 int bnxt_hwrm_allocate_pf_only(struct bnxt *bp)
3728 {
3729         struct bnxt_pf_resource_info pf_resc = { 0 };
3730         int rc;
3731
3732         if (!BNXT_PF(bp)) {
3733                 PMD_DRV_LOG(ERR, "Attempt to allocate VFs on a VF!\n");
3734                 return -EINVAL;
3735         }
3736
3737         rc = bnxt_hwrm_func_qcaps(bp);
3738         if (rc)
3739                 return rc;
3740
3741         bnxt_calculate_pf_resources(bp, &pf_resc, 0);
3742
3743         bp->pf->func_cfg_flags &=
3744                 ~(HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_ENABLE |
3745                   HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_DISABLE);
3746         bp->pf->func_cfg_flags |=
3747                 HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_DISABLE;
3748
3749         rc = bnxt_hwrm_pf_func_cfg(bp, &pf_resc);
3750         if (rc)
3751                 return rc;
3752
3753         rc = bnxt_update_max_resources_pf_only(bp);
3754
3755         return rc;
3756 }
3757
3758 static int
3759 bnxt_configure_vf_req_buf(struct bnxt *bp, int num_vfs)
3760 {
3761         size_t req_buf_sz, sz;
3762         int i, rc;
3763
3764         req_buf_sz = num_vfs * HWRM_MAX_REQ_LEN;
3765         bp->pf->vf_req_buf = rte_malloc("bnxt_vf_fwd", req_buf_sz,
3766                 page_roundup(num_vfs * HWRM_MAX_REQ_LEN));
3767         if (bp->pf->vf_req_buf == NULL) {
3768                 return -ENOMEM;
3769         }
3770
3771         for (sz = 0; sz < req_buf_sz; sz += getpagesize())
3772                 rte_mem_lock_page(((char *)bp->pf->vf_req_buf) + sz);
3773
3774         for (i = 0; i < num_vfs; i++)
3775                 bp->pf->vf_info[i].req_buf = ((char *)bp->pf->vf_req_buf) +
3776                                              (i * HWRM_MAX_REQ_LEN);
3777
3778         rc = bnxt_hwrm_func_buf_rgtr(bp, num_vfs);
3779         if (rc)
3780                 rte_free(bp->pf->vf_req_buf);
3781
3782         return rc;
3783 }
3784
3785 static int
3786 bnxt_process_vf_resc_config_new(struct bnxt *bp, int num_vfs)
3787 {
3788         struct hwrm_func_vf_resource_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3789         struct hwrm_func_vf_resource_cfg_input req = {0};
3790         int i, rc = 0;
3791
3792         bnxt_fill_vf_func_cfg_req_new(bp, &req, num_vfs);
3793         bp->pf->active_vfs = 0;
3794         for (i = 0; i < num_vfs; i++) {
3795                 HWRM_PREP(&req, HWRM_FUNC_VF_RESOURCE_CFG, BNXT_USE_CHIMP_MB);
3796                 req.vf_id = rte_cpu_to_le_16(bp->pf->vf_info[i].fid);
3797                 rc = bnxt_hwrm_send_message(bp,
3798                                             &req,
3799                                             sizeof(req),
3800                                             BNXT_USE_CHIMP_MB);
3801                 if (rc || resp->error_code) {
3802                         PMD_DRV_LOG(ERR,
3803                                 "Failed to initialize VF %d\n", i);
3804                         PMD_DRV_LOG(ERR,
3805                                 "Not all VFs available. (%d, %d)\n",
3806                                 rc, resp->error_code);
3807                         HWRM_UNLOCK();
3808
3809                         /* If the first VF configuration itself fails,
3810                          * unregister the vf_fwd_request buffer.
3811                          */
3812                         if (i == 0)
3813                                 bnxt_hwrm_func_buf_unrgtr(bp);
3814                         break;
3815                 }
3816                 HWRM_UNLOCK();
3817
3818                 /* Update the max resource values based on the resource values
3819                  * allocated to the VF.
3820                  */
3821                 bnxt_update_max_resources(bp, i);
3822                 bp->pf->active_vfs++;
3823                 bnxt_hwrm_func_clr_stats(bp, bp->pf->vf_info[i].fid);
3824         }
3825
3826         return 0;
3827 }
3828
3829 static int
3830 bnxt_process_vf_resc_config_old(struct bnxt *bp, int num_vfs)
3831 {
3832         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3833         struct hwrm_func_cfg_input req = {0};
3834         int i, rc;
3835
3836         bnxt_fill_vf_func_cfg_req_old(bp, &req, num_vfs);
3837
3838         bp->pf->active_vfs = 0;
3839         for (i = 0; i < num_vfs; i++) {
3840                 HWRM_PREP(&req, HWRM_FUNC_CFG, BNXT_USE_CHIMP_MB);
3841                 req.flags = rte_cpu_to_le_32(bp->pf->vf_info[i].func_cfg_flags);
3842                 req.fid = rte_cpu_to_le_16(bp->pf->vf_info[i].fid);
3843                 rc = bnxt_hwrm_send_message(bp,
3844                                             &req,
3845                                             sizeof(req),
3846                                             BNXT_USE_CHIMP_MB);
3847
3848                 /* Clear enable flag for next pass */
3849                 req.enables &= ~rte_cpu_to_le_32(
3850                                 HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
3851
3852                 if (rc || resp->error_code) {
3853                         PMD_DRV_LOG(ERR,
3854                                 "Failed to initialize VF %d\n", i);
3855                         PMD_DRV_LOG(ERR,
3856                                 "Not all VFs available. (%d, %d)\n",
3857                                 rc, resp->error_code);
3858                         HWRM_UNLOCK();
3859
3860                         /* If the first VF configuration itself fails,
3861                          * unregister the vf_fwd_request buffer.
3862                          */
3863                         if (i == 0)
3864                                 bnxt_hwrm_func_buf_unrgtr(bp);
3865                         break;
3866                 }
3867
3868                 HWRM_UNLOCK();
3869
3870                 /* Update the max resource values based on the resource values
3871                  * allocated to the VF.
3872                  */
3873                 bnxt_update_max_resources(bp, i);
3874                 bp->pf->active_vfs++;
3875                 bnxt_hwrm_func_clr_stats(bp, bp->pf->vf_info[i].fid);
3876         }
3877
3878         return 0;
3879 }
3880
3881 static void
3882 bnxt_configure_vf_resources(struct bnxt *bp, int num_vfs)
3883 {
3884         if (bp->flags & BNXT_FLAG_NEW_RM)
3885                 bnxt_process_vf_resc_config_new(bp, num_vfs);
3886         else
3887                 bnxt_process_vf_resc_config_old(bp, num_vfs);
3888 }
3889
3890 static void
3891 bnxt_update_pf_resources(struct bnxt *bp,
3892                          struct bnxt_pf_resource_info *pf_resc)
3893 {
3894         bp->max_rsscos_ctx = pf_resc->num_rsscos_ctxs;
3895         bp->max_stat_ctx = pf_resc->num_stat_ctxs;
3896         bp->max_cp_rings = pf_resc->num_cp_rings;
3897         bp->max_tx_rings = pf_resc->num_tx_rings;
3898         bp->max_rx_rings = pf_resc->num_rx_rings;
3899         bp->max_ring_grps = pf_resc->num_hw_ring_grps;
3900 }
3901
3902 static int32_t
3903 bnxt_configure_pf_resources(struct bnxt *bp,
3904                             struct bnxt_pf_resource_info *pf_resc)
3905 {
3906         /*
3907          * We're using STD_TX_RING_MODE here which will limit the TX
3908          * rings. This will allow QoS to function properly. Not setting this
3909          * will cause PF rings to break bandwidth settings.
3910          */
3911         bp->pf->func_cfg_flags &=
3912                 ~(HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_ENABLE |
3913                   HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_DISABLE);
3914         bp->pf->func_cfg_flags |=
3915                 HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_ENABLE;
3916         return bnxt_hwrm_pf_func_cfg(bp, pf_resc);
3917 }
3918
3919 int bnxt_hwrm_allocate_vfs(struct bnxt *bp, int num_vfs)
3920 {
3921         struct bnxt_pf_resource_info pf_resc = { 0 };
3922         int rc;
3923
3924         if (!BNXT_PF(bp)) {
3925                 PMD_DRV_LOG(ERR, "Attempt to allocate VFs on a VF!\n");
3926                 return -EINVAL;
3927         }
3928
3929         rc = bnxt_hwrm_func_qcaps(bp);
3930         if (rc)
3931                 return rc;
3932
3933         bnxt_calculate_pf_resources(bp, &pf_resc, num_vfs);
3934
3935         rc = bnxt_configure_pf_resources(bp, &pf_resc);
3936         if (rc)
3937                 return rc;
3938
3939         rc = bnxt_query_pf_resources(bp, &pf_resc);
3940         if (rc)
3941                 return rc;
3942
3943         /*
3944          * Now, create and register a buffer to hold forwarded VF requests
3945          */
3946         rc = bnxt_configure_vf_req_buf(bp, num_vfs);
3947         if (rc)
3948                 return rc;
3949
3950         bnxt_configure_vf_resources(bp, num_vfs);
3951
3952         bnxt_update_pf_resources(bp, &pf_resc);
3953
3954         return 0;
3955 }
3956
3957 int bnxt_hwrm_pf_evb_mode(struct bnxt *bp)
3958 {
3959         struct hwrm_func_cfg_input req = {0};
3960         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
3961         int rc;
3962
3963         HWRM_PREP(&req, HWRM_FUNC_CFG, BNXT_USE_CHIMP_MB);
3964
3965         req.fid = rte_cpu_to_le_16(0xffff);
3966         req.enables = rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_EVB_MODE);
3967         req.evb_mode = bp->pf->evb_mode;
3968
3969         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3970         HWRM_CHECK_RESULT();
3971         HWRM_UNLOCK();
3972
3973         return rc;
3974 }
3975
3976 int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, uint16_t port,
3977                                 uint8_t tunnel_type)
3978 {
3979         struct hwrm_tunnel_dst_port_alloc_input req = {0};
3980         struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3981         int rc = 0;
3982
3983         HWRM_PREP(&req, HWRM_TUNNEL_DST_PORT_ALLOC, BNXT_USE_CHIMP_MB);
3984         req.tunnel_type = tunnel_type;
3985         req.tunnel_dst_port_val = rte_cpu_to_be_16(port);
3986         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
3987         HWRM_CHECK_RESULT();
3988
3989         switch (tunnel_type) {
3990         case HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_VXLAN:
3991                 bp->vxlan_fw_dst_port_id =
3992                         rte_le_to_cpu_16(resp->tunnel_dst_port_id);
3993                 bp->vxlan_port = port;
3994                 break;
3995         case HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_GENEVE:
3996                 bp->geneve_fw_dst_port_id =
3997                         rte_le_to_cpu_16(resp->tunnel_dst_port_id);
3998                 bp->geneve_port = port;
3999                 break;
4000         default:
4001                 break;
4002         }
4003
4004         HWRM_UNLOCK();
4005
4006         return rc;
4007 }
4008
4009 int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, uint16_t port,
4010                                 uint8_t tunnel_type)
4011 {
4012         struct hwrm_tunnel_dst_port_free_input req = {0};
4013         struct hwrm_tunnel_dst_port_free_output *resp = bp->hwrm_cmd_resp_addr;
4014         int rc = 0;
4015
4016         HWRM_PREP(&req, HWRM_TUNNEL_DST_PORT_FREE, BNXT_USE_CHIMP_MB);
4017
4018         req.tunnel_type = tunnel_type;
4019         req.tunnel_dst_port_id = rte_cpu_to_be_16(port);
4020         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4021
4022         HWRM_CHECK_RESULT();
4023         HWRM_UNLOCK();
4024
4025         if (tunnel_type ==
4026             HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_VXLAN) {
4027                 bp->vxlan_port = 0;
4028                 bp->vxlan_port_cnt = 0;
4029         }
4030
4031         if (tunnel_type ==
4032             HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_GENEVE) {
4033                 bp->geneve_port = 0;
4034                 bp->geneve_port_cnt = 0;
4035         }
4036
4037         return rc;
4038 }
4039
4040 int bnxt_hwrm_func_cfg_vf_set_flags(struct bnxt *bp, uint16_t vf,
4041                                         uint32_t flags)
4042 {
4043         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
4044         struct hwrm_func_cfg_input req = {0};
4045         int rc;
4046
4047         HWRM_PREP(&req, HWRM_FUNC_CFG, BNXT_USE_CHIMP_MB);
4048
4049         req.fid = rte_cpu_to_le_16(bp->pf->vf_info[vf].fid);
4050         req.flags = rte_cpu_to_le_32(flags);
4051         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4052
4053         HWRM_CHECK_RESULT();
4054         HWRM_UNLOCK();
4055
4056         return rc;
4057 }
4058
4059 void vf_vnic_set_rxmask_cb(struct bnxt_vnic_info *vnic, void *flagp)
4060 {
4061         uint32_t *flag = flagp;
4062
4063         vnic->flags = *flag;
4064 }
4065
4066 int bnxt_set_rx_mask_no_vlan(struct bnxt *bp, struct bnxt_vnic_info *vnic)
4067 {
4068         return bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
4069 }
4070
4071 int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp, int num_vfs)
4072 {
4073         struct hwrm_func_buf_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
4074         struct hwrm_func_buf_rgtr_input req = {.req_type = 0 };
4075         int rc;
4076
4077         HWRM_PREP(&req, HWRM_FUNC_BUF_RGTR, BNXT_USE_CHIMP_MB);
4078
4079         req.req_buf_num_pages = rte_cpu_to_le_16(1);
4080         req.req_buf_page_size =
4081                 rte_cpu_to_le_16(page_getenum(num_vfs * HWRM_MAX_REQ_LEN));
4082         req.req_buf_len = rte_cpu_to_le_16(HWRM_MAX_REQ_LEN);
4083         req.req_buf_page_addr0 =
4084                 rte_cpu_to_le_64(rte_malloc_virt2iova(bp->pf->vf_req_buf));
4085         if (req.req_buf_page_addr0 == RTE_BAD_IOVA) {
4086                 PMD_DRV_LOG(ERR,
4087                         "unable to map buffer address to physical memory\n");
4088                 HWRM_UNLOCK();
4089                 return -ENOMEM;
4090         }
4091
4092         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4093
4094         HWRM_CHECK_RESULT();
4095         HWRM_UNLOCK();
4096
4097         return rc;
4098 }
4099
4100 int bnxt_hwrm_func_buf_unrgtr(struct bnxt *bp)
4101 {
4102         int rc = 0;
4103         struct hwrm_func_buf_unrgtr_input req = {.req_type = 0 };
4104         struct hwrm_func_buf_unrgtr_output *resp = bp->hwrm_cmd_resp_addr;
4105
4106         if (!(BNXT_PF(bp) && bp->pdev->max_vfs))
4107                 return 0;
4108
4109         HWRM_PREP(&req, HWRM_FUNC_BUF_UNRGTR, BNXT_USE_CHIMP_MB);
4110
4111         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4112
4113         HWRM_CHECK_RESULT();
4114         HWRM_UNLOCK();
4115
4116         return rc;
4117 }
4118
4119 int bnxt_hwrm_func_cfg_def_cp(struct bnxt *bp)
4120 {
4121         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
4122         struct hwrm_func_cfg_input req = {0};
4123         int rc;
4124
4125         HWRM_PREP(&req, HWRM_FUNC_CFG, BNXT_USE_CHIMP_MB);
4126
4127         req.fid = rte_cpu_to_le_16(0xffff);
4128         req.flags = rte_cpu_to_le_32(bp->pf->func_cfg_flags);
4129         req.enables = rte_cpu_to_le_32(
4130                         HWRM_FUNC_CFG_INPUT_ENABLES_ASYNC_EVENT_CR);
4131         req.async_event_cr = rte_cpu_to_le_16(
4132                         bp->async_cp_ring->cp_ring_struct->fw_ring_id);
4133         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4134
4135         HWRM_CHECK_RESULT();
4136         HWRM_UNLOCK();
4137
4138         return rc;
4139 }
4140
4141 int bnxt_hwrm_vf_func_cfg_def_cp(struct bnxt *bp)
4142 {
4143         struct hwrm_func_vf_cfg_output *resp = bp->hwrm_cmd_resp_addr;
4144         struct hwrm_func_vf_cfg_input req = {0};
4145         int rc;
4146
4147         HWRM_PREP(&req, HWRM_FUNC_VF_CFG, BNXT_USE_CHIMP_MB);
4148
4149         req.enables = rte_cpu_to_le_32(
4150                         HWRM_FUNC_VF_CFG_INPUT_ENABLES_ASYNC_EVENT_CR);
4151         req.async_event_cr = rte_cpu_to_le_16(
4152                         bp->async_cp_ring->cp_ring_struct->fw_ring_id);
4153         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4154
4155         HWRM_CHECK_RESULT();
4156         HWRM_UNLOCK();
4157
4158         return rc;
4159 }
4160
4161 int bnxt_hwrm_set_default_vlan(struct bnxt *bp, int vf, uint8_t is_vf)
4162 {
4163         struct hwrm_func_cfg_input req = {0};
4164         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
4165         uint16_t dflt_vlan, fid;
4166         uint32_t func_cfg_flags;
4167         int rc = 0;
4168
4169         HWRM_PREP(&req, HWRM_FUNC_CFG, BNXT_USE_CHIMP_MB);
4170
4171         if (is_vf) {
4172                 dflt_vlan = bp->pf->vf_info[vf].dflt_vlan;
4173                 fid = bp->pf->vf_info[vf].fid;
4174                 func_cfg_flags = bp->pf->vf_info[vf].func_cfg_flags;
4175         } else {
4176                 fid = rte_cpu_to_le_16(0xffff);
4177                 func_cfg_flags = bp->pf->func_cfg_flags;
4178                 dflt_vlan = bp->vlan;
4179         }
4180
4181         req.flags = rte_cpu_to_le_32(func_cfg_flags);
4182         req.fid = rte_cpu_to_le_16(fid);
4183         req.enables |= rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_VLAN);
4184         req.dflt_vlan = rte_cpu_to_le_16(dflt_vlan);
4185
4186         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4187
4188         HWRM_CHECK_RESULT();
4189         HWRM_UNLOCK();
4190
4191         return rc;
4192 }
4193
4194 int bnxt_hwrm_func_bw_cfg(struct bnxt *bp, uint16_t vf,
4195                         uint16_t max_bw, uint16_t enables)
4196 {
4197         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
4198         struct hwrm_func_cfg_input req = {0};
4199         int rc;
4200
4201         HWRM_PREP(&req, HWRM_FUNC_CFG, BNXT_USE_CHIMP_MB);
4202
4203         req.fid = rte_cpu_to_le_16(bp->pf->vf_info[vf].fid);
4204         req.enables |= rte_cpu_to_le_32(enables);
4205         req.flags = rte_cpu_to_le_32(bp->pf->vf_info[vf].func_cfg_flags);
4206         req.max_bw = rte_cpu_to_le_32(max_bw);
4207         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4208
4209         HWRM_CHECK_RESULT();
4210         HWRM_UNLOCK();
4211
4212         return rc;
4213 }
4214
4215 int bnxt_hwrm_set_vf_vlan(struct bnxt *bp, int vf)
4216 {
4217         struct hwrm_func_cfg_input req = {0};
4218         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
4219         int rc = 0;
4220
4221         HWRM_PREP(&req, HWRM_FUNC_CFG, BNXT_USE_CHIMP_MB);
4222
4223         req.flags = rte_cpu_to_le_32(bp->pf->vf_info[vf].func_cfg_flags);
4224         req.fid = rte_cpu_to_le_16(bp->pf->vf_info[vf].fid);
4225         req.enables |= rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_VLAN);
4226         req.dflt_vlan = rte_cpu_to_le_16(bp->pf->vf_info[vf].dflt_vlan);
4227
4228         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4229
4230         HWRM_CHECK_RESULT();
4231         HWRM_UNLOCK();
4232
4233         return rc;
4234 }
4235
4236 int bnxt_hwrm_set_async_event_cr(struct bnxt *bp)
4237 {
4238         int rc;
4239
4240         if (BNXT_PF(bp))
4241                 rc = bnxt_hwrm_func_cfg_def_cp(bp);
4242         else
4243                 rc = bnxt_hwrm_vf_func_cfg_def_cp(bp);
4244
4245         return rc;
4246 }
4247
4248 int bnxt_hwrm_reject_fwd_resp(struct bnxt *bp, uint16_t target_id,
4249                               void *encaped, size_t ec_size)
4250 {
4251         int rc = 0;
4252         struct hwrm_reject_fwd_resp_input req = {.req_type = 0};
4253         struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
4254
4255         if (ec_size > sizeof(req.encap_request))
4256                 return -1;
4257
4258         HWRM_PREP(&req, HWRM_REJECT_FWD_RESP, BNXT_USE_CHIMP_MB);
4259
4260         req.encap_resp_target_id = rte_cpu_to_le_16(target_id);
4261         memcpy(req.encap_request, encaped, ec_size);
4262
4263         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4264
4265         HWRM_CHECK_RESULT();
4266         HWRM_UNLOCK();
4267
4268         return rc;
4269 }
4270
4271 int bnxt_hwrm_func_qcfg_vf_default_mac(struct bnxt *bp, uint16_t vf,
4272                                        struct rte_ether_addr *mac)
4273 {
4274         struct hwrm_func_qcfg_input req = {0};
4275         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4276         int rc;
4277
4278         HWRM_PREP(&req, HWRM_FUNC_QCFG, BNXT_USE_CHIMP_MB);
4279
4280         req.fid = rte_cpu_to_le_16(bp->pf->vf_info[vf].fid);
4281         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4282
4283         HWRM_CHECK_RESULT();
4284
4285         memcpy(mac->addr_bytes, resp->mac_address, RTE_ETHER_ADDR_LEN);
4286
4287         HWRM_UNLOCK();
4288
4289         return rc;
4290 }
4291
4292 int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, uint16_t target_id,
4293                             void *encaped, size_t ec_size)
4294 {
4295         int rc = 0;
4296         struct hwrm_exec_fwd_resp_input req = {.req_type = 0};
4297         struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
4298
4299         if (ec_size > sizeof(req.encap_request))
4300                 return -1;
4301
4302         HWRM_PREP(&req, HWRM_EXEC_FWD_RESP, BNXT_USE_CHIMP_MB);
4303
4304         req.encap_resp_target_id = rte_cpu_to_le_16(target_id);
4305         memcpy(req.encap_request, encaped, ec_size);
4306
4307         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4308
4309         HWRM_CHECK_RESULT();
4310         HWRM_UNLOCK();
4311
4312         return rc;
4313 }
4314
4315 static void bnxt_update_prev_stat(uint64_t *cntr, uint64_t *prev_cntr)
4316 {
4317         /* One of the HW stat values that make up this counter was zero as
4318          * returned by HW in this iteration, so use the previous
4319          * iteration's counter value
4320          */
4321         if (*prev_cntr && *cntr == 0)
4322                 *cntr = *prev_cntr;
4323         else
4324                 *prev_cntr = *cntr;
4325 }
4326
4327 int bnxt_hwrm_ring_stats(struct bnxt *bp, uint32_t cid, int idx,
4328                          struct bnxt_ring_stats *ring_stats, bool rx)
4329 {
4330         int rc = 0;
4331         struct hwrm_stat_ctx_query_input req = {.req_type = 0};
4332         struct hwrm_stat_ctx_query_output *resp = bp->hwrm_cmd_resp_addr;
4333
4334         HWRM_PREP(&req, HWRM_STAT_CTX_QUERY, BNXT_USE_CHIMP_MB);
4335
4336         req.stat_ctx_id = rte_cpu_to_le_32(cid);
4337
4338         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4339
4340         HWRM_CHECK_RESULT();
4341
4342         if (rx) {
4343                 struct bnxt_ring_stats *prev_stats = &bp->prev_rx_ring_stats[idx];
4344
4345                 ring_stats->rx_ucast_pkts = rte_le_to_cpu_64(resp->rx_ucast_pkts);
4346                 bnxt_update_prev_stat(&ring_stats->rx_ucast_pkts,
4347                                       &prev_stats->rx_ucast_pkts);
4348
4349                 ring_stats->rx_mcast_pkts = rte_le_to_cpu_64(resp->rx_mcast_pkts);
4350                 bnxt_update_prev_stat(&ring_stats->rx_mcast_pkts,
4351                                       &prev_stats->rx_mcast_pkts);
4352
4353                 ring_stats->rx_bcast_pkts = rte_le_to_cpu_64(resp->rx_bcast_pkts);
4354                 bnxt_update_prev_stat(&ring_stats->rx_bcast_pkts,
4355                                       &prev_stats->rx_bcast_pkts);
4356
4357                 ring_stats->rx_ucast_bytes = rte_le_to_cpu_64(resp->rx_ucast_bytes);
4358                 bnxt_update_prev_stat(&ring_stats->rx_ucast_bytes,
4359                                       &prev_stats->rx_ucast_bytes);
4360
4361                 ring_stats->rx_mcast_bytes = rte_le_to_cpu_64(resp->rx_mcast_bytes);
4362                 bnxt_update_prev_stat(&ring_stats->rx_mcast_bytes,
4363                                       &prev_stats->rx_mcast_bytes);
4364
4365                 ring_stats->rx_bcast_bytes = rte_le_to_cpu_64(resp->rx_bcast_bytes);
4366                 bnxt_update_prev_stat(&ring_stats->rx_bcast_bytes,
4367                                       &prev_stats->rx_bcast_bytes);
4368
4369                 ring_stats->rx_discard_pkts = rte_le_to_cpu_64(resp->rx_discard_pkts);
4370                 bnxt_update_prev_stat(&ring_stats->rx_discard_pkts,
4371                                       &prev_stats->rx_discard_pkts);
4372
4373                 ring_stats->rx_error_pkts = rte_le_to_cpu_64(resp->rx_error_pkts);
4374                 bnxt_update_prev_stat(&ring_stats->rx_error_pkts,
4375                                       &prev_stats->rx_error_pkts);
4376
4377                 ring_stats->rx_agg_pkts = rte_le_to_cpu_64(resp->rx_agg_pkts);
4378                 bnxt_update_prev_stat(&ring_stats->rx_agg_pkts,
4379                                       &prev_stats->rx_agg_pkts);
4380
4381                 ring_stats->rx_agg_bytes = rte_le_to_cpu_64(resp->rx_agg_bytes);
4382                 bnxt_update_prev_stat(&ring_stats->rx_agg_bytes,
4383                                       &prev_stats->rx_agg_bytes);
4384
4385                 ring_stats->rx_agg_events = rte_le_to_cpu_64(resp->rx_agg_events);
4386                 bnxt_update_prev_stat(&ring_stats->rx_agg_events,
4387                                       &prev_stats->rx_agg_events);
4388
4389                 ring_stats->rx_agg_aborts = rte_le_to_cpu_64(resp->rx_agg_aborts);
4390                 bnxt_update_prev_stat(&ring_stats->rx_agg_aborts,
4391                                       &prev_stats->rx_agg_aborts);
4392         } else {
4393                 struct bnxt_ring_stats *prev_stats = &bp->prev_tx_ring_stats[idx];
4394
4395                 ring_stats->tx_ucast_pkts = rte_le_to_cpu_64(resp->tx_ucast_pkts);
4396                 bnxt_update_prev_stat(&ring_stats->tx_ucast_pkts,
4397                                       &prev_stats->tx_ucast_pkts);
4398
4399                 ring_stats->tx_mcast_pkts = rte_le_to_cpu_64(resp->tx_mcast_pkts);
4400                 bnxt_update_prev_stat(&ring_stats->tx_mcast_pkts,
4401                                       &prev_stats->tx_mcast_pkts);
4402
4403                 ring_stats->tx_bcast_pkts = rte_le_to_cpu_64(resp->tx_bcast_pkts);
4404                 bnxt_update_prev_stat(&ring_stats->tx_bcast_pkts,
4405                                       &prev_stats->tx_bcast_pkts);
4406
4407                 ring_stats->tx_ucast_bytes = rte_le_to_cpu_64(resp->tx_ucast_bytes);
4408                 bnxt_update_prev_stat(&ring_stats->tx_ucast_bytes,
4409                                       &prev_stats->tx_ucast_bytes);
4410
4411                 ring_stats->tx_mcast_bytes = rte_le_to_cpu_64(resp->tx_mcast_bytes);
4412                 bnxt_update_prev_stat(&ring_stats->tx_mcast_bytes,
4413                                       &prev_stats->tx_mcast_bytes);
4414
4415                 ring_stats->tx_bcast_bytes = rte_le_to_cpu_64(resp->tx_bcast_bytes);
4416                 bnxt_update_prev_stat(&ring_stats->tx_bcast_bytes,
4417                                       &prev_stats->tx_bcast_bytes);
4418
4419                 ring_stats->tx_discard_pkts = rte_le_to_cpu_64(resp->tx_discard_pkts);
4420                 bnxt_update_prev_stat(&ring_stats->tx_discard_pkts,
4421                                       &prev_stats->tx_discard_pkts);
4422         }
4423
4424         HWRM_UNLOCK();
4425
4426         return rc;
4427 }
4428
4429 int bnxt_hwrm_port_qstats(struct bnxt *bp)
4430 {
4431         struct hwrm_port_qstats_input req = {0};
4432         struct hwrm_port_qstats_output *resp = bp->hwrm_cmd_resp_addr;
4433         struct bnxt_pf_info *pf = bp->pf;
4434         int rc;
4435
4436         HWRM_PREP(&req, HWRM_PORT_QSTATS, BNXT_USE_CHIMP_MB);
4437
4438         req.port_id = rte_cpu_to_le_16(pf->port_id);
4439         req.tx_stat_host_addr = rte_cpu_to_le_64(bp->hw_tx_port_stats_map);
4440         req.rx_stat_host_addr = rte_cpu_to_le_64(bp->hw_rx_port_stats_map);
4441         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4442
4443         HWRM_CHECK_RESULT();
4444         HWRM_UNLOCK();
4445
4446         return rc;
4447 }
4448
4449 int bnxt_hwrm_port_clr_stats(struct bnxt *bp)
4450 {
4451         struct hwrm_port_clr_stats_input req = {0};
4452         struct hwrm_port_clr_stats_output *resp = bp->hwrm_cmd_resp_addr;
4453         struct bnxt_pf_info *pf = bp->pf;
4454         int rc;
4455
4456         /* Not allowed on NS2 device, NPAR, MultiHost, VF */
4457         if (!(bp->flags & BNXT_FLAG_PORT_STATS) || BNXT_VF(bp) ||
4458             BNXT_NPAR(bp) || BNXT_MH(bp) || BNXT_TOTAL_VFS(bp))
4459                 return 0;
4460
4461         HWRM_PREP(&req, HWRM_PORT_CLR_STATS, BNXT_USE_CHIMP_MB);
4462
4463         req.port_id = rte_cpu_to_le_16(pf->port_id);
4464         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4465
4466         HWRM_CHECK_RESULT();
4467         HWRM_UNLOCK();
4468
4469         return rc;
4470 }
4471
4472 int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
4473 {
4474         struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4475         struct hwrm_port_led_qcaps_input req = {0};
4476         int rc;
4477
4478         if (BNXT_VF(bp))
4479                 return 0;
4480
4481         HWRM_PREP(&req, HWRM_PORT_LED_QCAPS, BNXT_USE_CHIMP_MB);
4482         req.port_id = bp->pf->port_id;
4483         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4484
4485         HWRM_CHECK_RESULT_SILENT();
4486
4487         if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
4488                 unsigned int i;
4489
4490                 bp->leds->num_leds = resp->num_leds;
4491                 memcpy(bp->leds, &resp->led0_id,
4492                         sizeof(bp->leds[0]) * bp->leds->num_leds);
4493                 for (i = 0; i < bp->leds->num_leds; i++) {
4494                         struct bnxt_led_info *led = &bp->leds[i];
4495
4496                         uint16_t caps = led->led_state_caps;
4497
4498                         if (!led->led_group_id ||
4499                                 !BNXT_LED_ALT_BLINK_CAP(caps)) {
4500                                 bp->leds->num_leds = 0;
4501                                 break;
4502                         }
4503                 }
4504         }
4505
4506         HWRM_UNLOCK();
4507
4508         return rc;
4509 }
4510
4511 int bnxt_hwrm_port_led_cfg(struct bnxt *bp, bool led_on)
4512 {
4513         struct hwrm_port_led_cfg_output *resp = bp->hwrm_cmd_resp_addr;
4514         struct hwrm_port_led_cfg_input req = {0};
4515         struct bnxt_led_cfg *led_cfg;
4516         uint8_t led_state = HWRM_PORT_LED_QCFG_OUTPUT_LED0_STATE_DEFAULT;
4517         uint16_t duration = 0;
4518         int rc, i;
4519
4520         if (!bp->leds->num_leds || BNXT_VF(bp))
4521                 return -EOPNOTSUPP;
4522
4523         HWRM_PREP(&req, HWRM_PORT_LED_CFG, BNXT_USE_CHIMP_MB);
4524
4525         if (led_on) {
4526                 led_state = HWRM_PORT_LED_CFG_INPUT_LED0_STATE_BLINKALT;
4527                 duration = rte_cpu_to_le_16(500);
4528         }
4529         req.port_id = bp->pf->port_id;
4530         req.num_leds = bp->leds->num_leds;
4531         led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
4532         for (i = 0; i < bp->leds->num_leds; i++, led_cfg++) {
4533                 req.enables |= BNXT_LED_DFLT_ENABLES(i);
4534                 led_cfg->led_id = bp->leds[i].led_id;
4535                 led_cfg->led_state = led_state;
4536                 led_cfg->led_blink_on = duration;
4537                 led_cfg->led_blink_off = duration;
4538                 led_cfg->led_group_id = bp->leds[i].led_group_id;
4539         }
4540
4541         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4542
4543         HWRM_CHECK_RESULT();
4544         HWRM_UNLOCK();
4545
4546         return rc;
4547 }
4548
4549 int bnxt_hwrm_nvm_get_dir_info(struct bnxt *bp, uint32_t *entries,
4550                                uint32_t *length)
4551 {
4552         int rc;
4553         struct hwrm_nvm_get_dir_info_input req = {0};
4554         struct hwrm_nvm_get_dir_info_output *resp = bp->hwrm_cmd_resp_addr;
4555
4556         HWRM_PREP(&req, HWRM_NVM_GET_DIR_INFO, BNXT_USE_CHIMP_MB);
4557
4558         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4559
4560         HWRM_CHECK_RESULT();
4561
4562         *entries = rte_le_to_cpu_32(resp->entries);
4563         *length = rte_le_to_cpu_32(resp->entry_length);
4564
4565         HWRM_UNLOCK();
4566         return rc;
4567 }
4568
4569 int bnxt_get_nvram_directory(struct bnxt *bp, uint32_t len, uint8_t *data)
4570 {
4571         int rc;
4572         uint32_t dir_entries;
4573         uint32_t entry_length;
4574         uint8_t *buf;
4575         size_t buflen;
4576         rte_iova_t dma_handle;
4577         struct hwrm_nvm_get_dir_entries_input req = {0};
4578         struct hwrm_nvm_get_dir_entries_output *resp = bp->hwrm_cmd_resp_addr;
4579
4580         rc = bnxt_hwrm_nvm_get_dir_info(bp, &dir_entries, &entry_length);
4581         if (rc != 0)
4582                 return rc;
4583
4584         *data++ = dir_entries;
4585         *data++ = entry_length;
4586         len -= 2;
4587         memset(data, 0xff, len);
4588
4589         buflen = dir_entries * entry_length;
4590         buf = rte_malloc("nvm_dir", buflen, 0);
4591         if (buf == NULL)
4592                 return -ENOMEM;
4593         dma_handle = rte_malloc_virt2iova(buf);
4594         if (dma_handle == RTE_BAD_IOVA) {
4595                 rte_free(buf);
4596                 PMD_DRV_LOG(ERR,
4597                         "unable to map response address to physical memory\n");
4598                 return -ENOMEM;
4599         }
4600         HWRM_PREP(&req, HWRM_NVM_GET_DIR_ENTRIES, BNXT_USE_CHIMP_MB);
4601         req.host_dest_addr = rte_cpu_to_le_64(dma_handle);
4602         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4603
4604         if (rc == 0)
4605                 memcpy(data, buf, len > buflen ? buflen : len);
4606
4607         rte_free(buf);
4608         HWRM_CHECK_RESULT();
4609         HWRM_UNLOCK();
4610
4611         return rc;
4612 }
4613
4614 int bnxt_hwrm_get_nvram_item(struct bnxt *bp, uint32_t index,
4615                              uint32_t offset, uint32_t length,
4616                              uint8_t *data)
4617 {
4618         int rc;
4619         uint8_t *buf;
4620         rte_iova_t dma_handle;
4621         struct hwrm_nvm_read_input req = {0};
4622         struct hwrm_nvm_read_output *resp = bp->hwrm_cmd_resp_addr;
4623
4624         buf = rte_malloc("nvm_item", length, 0);
4625         if (!buf)
4626                 return -ENOMEM;
4627
4628         dma_handle = rte_malloc_virt2iova(buf);
4629         if (dma_handle == RTE_BAD_IOVA) {
4630                 rte_free(buf);
4631                 PMD_DRV_LOG(ERR,
4632                         "unable to map response address to physical memory\n");
4633                 return -ENOMEM;
4634         }
4635         HWRM_PREP(&req, HWRM_NVM_READ, BNXT_USE_CHIMP_MB);
4636         req.host_dest_addr = rte_cpu_to_le_64(dma_handle);
4637         req.dir_idx = rte_cpu_to_le_16(index);
4638         req.offset = rte_cpu_to_le_32(offset);
4639         req.len = rte_cpu_to_le_32(length);
4640         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4641         if (rc == 0)
4642                 memcpy(data, buf, length);
4643
4644         rte_free(buf);
4645         HWRM_CHECK_RESULT();
4646         HWRM_UNLOCK();
4647
4648         return rc;
4649 }
4650
4651 int bnxt_hwrm_erase_nvram_directory(struct bnxt *bp, uint8_t index)
4652 {
4653         int rc;
4654         struct hwrm_nvm_erase_dir_entry_input req = {0};
4655         struct hwrm_nvm_erase_dir_entry_output *resp = bp->hwrm_cmd_resp_addr;
4656
4657         HWRM_PREP(&req, HWRM_NVM_ERASE_DIR_ENTRY, BNXT_USE_CHIMP_MB);
4658         req.dir_idx = rte_cpu_to_le_16(index);
4659         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4660         HWRM_CHECK_RESULT();
4661         HWRM_UNLOCK();
4662
4663         return rc;
4664 }
4665
4666 int bnxt_hwrm_flash_nvram(struct bnxt *bp, uint16_t dir_type,
4667                           uint16_t dir_ordinal, uint16_t dir_ext,
4668                           uint16_t dir_attr, const uint8_t *data,
4669                           size_t data_len)
4670 {
4671         int rc;
4672         struct hwrm_nvm_write_input req = {0};
4673         struct hwrm_nvm_write_output *resp = bp->hwrm_cmd_resp_addr;
4674         rte_iova_t dma_handle;
4675         uint8_t *buf;
4676
4677         buf = rte_malloc("nvm_write", data_len, 0);
4678         if (!buf)
4679                 return -ENOMEM;
4680
4681         dma_handle = rte_malloc_virt2iova(buf);
4682         if (dma_handle == RTE_BAD_IOVA) {
4683                 rte_free(buf);
4684                 PMD_DRV_LOG(ERR,
4685                         "unable to map response address to physical memory\n");
4686                 return -ENOMEM;
4687         }
4688         memcpy(buf, data, data_len);
4689
4690         HWRM_PREP(&req, HWRM_NVM_WRITE, BNXT_USE_CHIMP_MB);
4691
4692         req.dir_type = rte_cpu_to_le_16(dir_type);
4693         req.dir_ordinal = rte_cpu_to_le_16(dir_ordinal);
4694         req.dir_ext = rte_cpu_to_le_16(dir_ext);
4695         req.dir_attr = rte_cpu_to_le_16(dir_attr);
4696         req.dir_data_length = rte_cpu_to_le_32(data_len);
4697         req.host_src_addr = rte_cpu_to_le_64(dma_handle);
4698
4699         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4700
4701         rte_free(buf);
4702         HWRM_CHECK_RESULT();
4703         HWRM_UNLOCK();
4704
4705         return rc;
4706 }
4707
4708 static void
4709 bnxt_vnic_count(struct bnxt_vnic_info *vnic __rte_unused, void *cbdata)
4710 {
4711         uint32_t *count = cbdata;
4712
4713         *count = *count + 1;
4714 }
4715
4716 static int bnxt_vnic_count_hwrm_stub(struct bnxt *bp __rte_unused,
4717                                      struct bnxt_vnic_info *vnic __rte_unused)
4718 {
4719         return 0;
4720 }
4721
4722 int bnxt_vf_vnic_count(struct bnxt *bp, uint16_t vf)
4723 {
4724         uint32_t count = 0;
4725
4726         bnxt_hwrm_func_vf_vnic_query_and_config(bp, vf, bnxt_vnic_count,
4727             &count, bnxt_vnic_count_hwrm_stub);
4728
4729         return count;
4730 }
4731
4732 static int bnxt_hwrm_func_vf_vnic_query(struct bnxt *bp, uint16_t vf,
4733                                         uint16_t *vnic_ids)
4734 {
4735         struct hwrm_func_vf_vnic_ids_query_input req = {0};
4736         struct hwrm_func_vf_vnic_ids_query_output *resp =
4737                                                 bp->hwrm_cmd_resp_addr;
4738         int rc;
4739
4740         /* First query all VNIC ids */
4741         HWRM_PREP(&req, HWRM_FUNC_VF_VNIC_IDS_QUERY, BNXT_USE_CHIMP_MB);
4742
4743         req.vf_id = rte_cpu_to_le_16(bp->pf->first_vf_id + vf);
4744         req.max_vnic_id_cnt = rte_cpu_to_le_32(bp->pf->total_vnics);
4745         req.vnic_id_tbl_addr = rte_cpu_to_le_64(rte_malloc_virt2iova(vnic_ids));
4746
4747         if (req.vnic_id_tbl_addr == RTE_BAD_IOVA) {
4748                 HWRM_UNLOCK();
4749                 PMD_DRV_LOG(ERR,
4750                 "unable to map VNIC ID table address to physical memory\n");
4751                 return -ENOMEM;
4752         }
4753         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4754         HWRM_CHECK_RESULT();
4755         rc = rte_le_to_cpu_32(resp->vnic_id_cnt);
4756
4757         HWRM_UNLOCK();
4758
4759         return rc;
4760 }
4761
4762 /*
4763  * This function queries the VNIC IDs  for a specified VF. It then calls
4764  * the vnic_cb to update the necessary field in vnic_info with cbdata.
4765  * Then it calls the hwrm_cb function to program this new vnic configuration.
4766  */
4767 int bnxt_hwrm_func_vf_vnic_query_and_config(struct bnxt *bp, uint16_t vf,
4768         void (*vnic_cb)(struct bnxt_vnic_info *, void *), void *cbdata,
4769         int (*hwrm_cb)(struct bnxt *bp, struct bnxt_vnic_info *vnic))
4770 {
4771         struct bnxt_vnic_info vnic;
4772         int rc = 0;
4773         int i, num_vnic_ids;
4774         uint16_t *vnic_ids;
4775         size_t vnic_id_sz;
4776         size_t sz;
4777
4778         /* First query all VNIC ids */
4779         vnic_id_sz = bp->pf->total_vnics * sizeof(*vnic_ids);
4780         vnic_ids = rte_malloc("bnxt_hwrm_vf_vnic_ids_query", vnic_id_sz,
4781                         RTE_CACHE_LINE_SIZE);
4782         if (vnic_ids == NULL)
4783                 return -ENOMEM;
4784
4785         for (sz = 0; sz < vnic_id_sz; sz += getpagesize())
4786                 rte_mem_lock_page(((char *)vnic_ids) + sz);
4787
4788         num_vnic_ids = bnxt_hwrm_func_vf_vnic_query(bp, vf, vnic_ids);
4789
4790         if (num_vnic_ids < 0)
4791                 return num_vnic_ids;
4792
4793         /* Retrieve VNIC, update bd_stall then update */
4794
4795         for (i = 0; i < num_vnic_ids; i++) {
4796                 memset(&vnic, 0, sizeof(struct bnxt_vnic_info));
4797                 vnic.fw_vnic_id = rte_le_to_cpu_16(vnic_ids[i]);
4798                 rc = bnxt_hwrm_vnic_qcfg(bp, &vnic, bp->pf->first_vf_id + vf);
4799                 if (rc)
4800                         break;
4801                 if (vnic.mru <= 4)      /* Indicates unallocated */
4802                         continue;
4803
4804                 vnic_cb(&vnic, cbdata);
4805
4806                 rc = hwrm_cb(bp, &vnic);
4807                 if (rc)
4808                         break;
4809         }
4810
4811         rte_free(vnic_ids);
4812
4813         return rc;
4814 }
4815
4816 int bnxt_hwrm_func_cfg_vf_set_vlan_anti_spoof(struct bnxt *bp, uint16_t vf,
4817                                               bool on)
4818 {
4819         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
4820         struct hwrm_func_cfg_input req = {0};
4821         int rc;
4822
4823         HWRM_PREP(&req, HWRM_FUNC_CFG, BNXT_USE_CHIMP_MB);
4824
4825         req.fid = rte_cpu_to_le_16(bp->pf->vf_info[vf].fid);
4826         req.enables |= rte_cpu_to_le_32(
4827                         HWRM_FUNC_CFG_INPUT_ENABLES_VLAN_ANTISPOOF_MODE);
4828         req.vlan_antispoof_mode = on ?
4829                 HWRM_FUNC_CFG_INPUT_VLAN_ANTISPOOF_MODE_VALIDATE_VLAN :
4830                 HWRM_FUNC_CFG_INPUT_VLAN_ANTISPOOF_MODE_NOCHECK;
4831         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
4832
4833         HWRM_CHECK_RESULT();
4834         HWRM_UNLOCK();
4835
4836         return rc;
4837 }
4838
4839 int bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(struct bnxt *bp, int vf)
4840 {
4841         struct bnxt_vnic_info vnic;
4842         uint16_t *vnic_ids;
4843         size_t vnic_id_sz;
4844         int num_vnic_ids, i;
4845         size_t sz;
4846         int rc;
4847
4848         vnic_id_sz = bp->pf->total_vnics * sizeof(*vnic_ids);
4849         vnic_ids = rte_malloc("bnxt_hwrm_vf_vnic_ids_query", vnic_id_sz,
4850                         RTE_CACHE_LINE_SIZE);
4851         if (vnic_ids == NULL)
4852                 return -ENOMEM;
4853
4854         for (sz = 0; sz < vnic_id_sz; sz += getpagesize())
4855                 rte_mem_lock_page(((char *)vnic_ids) + sz);
4856
4857         rc = bnxt_hwrm_func_vf_vnic_query(bp, vf, vnic_ids);
4858         if (rc <= 0)
4859                 goto exit;
4860         num_vnic_ids = rc;
4861
4862         /*
4863          * Loop through to find the default VNIC ID.
4864          * TODO: The easier way would be to obtain the resp->dflt_vnic_id
4865          * by sending the hwrm_func_qcfg command to the firmware.
4866          */
4867         for (i = 0; i < num_vnic_ids; i++) {
4868                 memset(&vnic, 0, sizeof(struct bnxt_vnic_info));
4869                 vnic.fw_vnic_id = rte_le_to_cpu_16(vnic_ids[i]);
4870                 rc = bnxt_hwrm_vnic_qcfg(bp, &vnic,
4871                                         bp->pf->first_vf_id + vf);
4872                 if (rc)
4873                         goto exit;
4874                 if (vnic.func_default) {
4875                         rte_free(vnic_ids);
4876                         return vnic.fw_vnic_id;
4877                 }
4878         }
4879         /* Could not find a default VNIC. */
4880         PMD_DRV_LOG(ERR, "No default VNIC\n");
4881 exit:
4882         rte_free(vnic_ids);
4883         return rc;
4884 }
4885
4886 int bnxt_hwrm_set_em_filter(struct bnxt *bp,
4887                          uint16_t dst_id,
4888                          struct bnxt_filter_info *filter)
4889 {
4890         int rc = 0;
4891         struct hwrm_cfa_em_flow_alloc_input req = {.req_type = 0 };
4892         struct hwrm_cfa_em_flow_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4893         uint32_t enables = 0;
4894
4895         if (filter->fw_em_filter_id != UINT64_MAX)
4896                 bnxt_hwrm_clear_em_filter(bp, filter);
4897
4898         HWRM_PREP(&req, HWRM_CFA_EM_FLOW_ALLOC, BNXT_USE_KONG(bp));
4899
4900         req.flags = rte_cpu_to_le_32(filter->flags);
4901
4902         enables = filter->enables |
4903               HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_DST_ID;
4904         req.dst_id = rte_cpu_to_le_16(dst_id);
4905
4906         if (filter->ip_addr_type) {
4907                 req.ip_addr_type = filter->ip_addr_type;
4908                 enables |= HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_IPADDR_TYPE;
4909         }
4910         if (enables &
4911             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_L2_FILTER_ID)
4912                 req.l2_filter_id = rte_cpu_to_le_64(filter->fw_l2_filter_id);
4913         if (enables &
4914             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_SRC_MACADDR)
4915                 memcpy(req.src_macaddr, filter->src_macaddr,
4916                        RTE_ETHER_ADDR_LEN);
4917         if (enables &
4918             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_DST_MACADDR)
4919                 memcpy(req.dst_macaddr, filter->dst_macaddr,
4920                        RTE_ETHER_ADDR_LEN);
4921         if (enables &
4922             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_OVLAN_VID)
4923                 req.ovlan_vid = filter->l2_ovlan;
4924         if (enables &
4925             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_IVLAN_VID)
4926                 req.ivlan_vid = filter->l2_ivlan;
4927         if (enables &
4928             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_ETHERTYPE)
4929                 req.ethertype = rte_cpu_to_be_16(filter->ethertype);
4930         if (enables &
4931             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_IP_PROTOCOL)
4932                 req.ip_protocol = filter->ip_protocol;
4933         if (enables &
4934             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_SRC_IPADDR)
4935                 req.src_ipaddr[0] = rte_cpu_to_be_32(filter->src_ipaddr[0]);
4936         if (enables &
4937             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_DST_IPADDR)
4938                 req.dst_ipaddr[0] = rte_cpu_to_be_32(filter->dst_ipaddr[0]);
4939         if (enables &
4940             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_SRC_PORT)
4941                 req.src_port = rte_cpu_to_be_16(filter->src_port);
4942         if (enables &
4943             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_DST_PORT)
4944                 req.dst_port = rte_cpu_to_be_16(filter->dst_port);
4945         if (enables &
4946             HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_MIRROR_VNIC_ID)
4947                 req.mirror_vnic_id = filter->mirror_vnic_id;
4948
4949         req.enables = rte_cpu_to_le_32(enables);
4950
4951         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_KONG(bp));
4952
4953         HWRM_CHECK_RESULT();
4954
4955         filter->fw_em_filter_id = rte_le_to_cpu_64(resp->em_filter_id);
4956         HWRM_UNLOCK();
4957
4958         return rc;
4959 }
4960
4961 int bnxt_hwrm_clear_em_filter(struct bnxt *bp, struct bnxt_filter_info *filter)
4962 {
4963         int rc = 0;
4964         struct hwrm_cfa_em_flow_free_input req = {.req_type = 0 };
4965         struct hwrm_cfa_em_flow_free_output *resp = bp->hwrm_cmd_resp_addr;
4966
4967         if (filter->fw_em_filter_id == UINT64_MAX)
4968                 return 0;
4969
4970         HWRM_PREP(&req, HWRM_CFA_EM_FLOW_FREE, BNXT_USE_KONG(bp));
4971
4972         req.em_filter_id = rte_cpu_to_le_64(filter->fw_em_filter_id);
4973
4974         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_KONG(bp));
4975
4976         HWRM_CHECK_RESULT();
4977         HWRM_UNLOCK();
4978
4979         filter->fw_em_filter_id = UINT64_MAX;
4980         filter->fw_l2_filter_id = UINT64_MAX;
4981
4982         return 0;
4983 }
4984
4985 int bnxt_hwrm_set_ntuple_filter(struct bnxt *bp,
4986                          uint16_t dst_id,
4987                          struct bnxt_filter_info *filter)
4988 {
4989         int rc = 0;
4990         struct hwrm_cfa_ntuple_filter_alloc_input req = {.req_type = 0 };
4991         struct hwrm_cfa_ntuple_filter_alloc_output *resp =
4992                                                 bp->hwrm_cmd_resp_addr;
4993         uint32_t enables = 0;
4994
4995         if (filter->fw_ntuple_filter_id != UINT64_MAX)
4996                 bnxt_hwrm_clear_ntuple_filter(bp, filter);
4997
4998         HWRM_PREP(&req, HWRM_CFA_NTUPLE_FILTER_ALLOC, BNXT_USE_CHIMP_MB);
4999
5000         req.flags = rte_cpu_to_le_32(filter->flags);
5001
5002         enables = filter->enables |
5003               HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_ID;
5004         req.dst_id = rte_cpu_to_le_16(dst_id);
5005
5006         if (filter->ip_addr_type) {
5007                 req.ip_addr_type = filter->ip_addr_type;
5008                 enables |=
5009                         HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_IPADDR_TYPE;
5010         }
5011         if (enables &
5012             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_L2_FILTER_ID)
5013                 req.l2_filter_id = rte_cpu_to_le_64(filter->fw_l2_filter_id);
5014         if (enables &
5015             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_MACADDR)
5016                 memcpy(req.src_macaddr, filter->src_macaddr,
5017                        RTE_ETHER_ADDR_LEN);
5018         if (enables &
5019             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_ETHERTYPE)
5020                 req.ethertype = rte_cpu_to_be_16(filter->ethertype);
5021         if (enables &
5022             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_IP_PROTOCOL)
5023                 req.ip_protocol = filter->ip_protocol;
5024         if (enables &
5025             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_IPADDR)
5026                 req.src_ipaddr[0] = rte_cpu_to_le_32(filter->src_ipaddr[0]);
5027         if (enables &
5028             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_IPADDR_MASK)
5029                 req.src_ipaddr_mask[0] =
5030                         rte_cpu_to_le_32(filter->src_ipaddr_mask[0]);
5031         if (enables &
5032             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_IPADDR)
5033                 req.dst_ipaddr[0] = rte_cpu_to_le_32(filter->dst_ipaddr[0]);
5034         if (enables &
5035             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_IPADDR_MASK)
5036                 req.dst_ipaddr_mask[0] =
5037                         rte_cpu_to_be_32(filter->dst_ipaddr_mask[0]);
5038         if (enables &
5039             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_PORT)
5040                 req.src_port = rte_cpu_to_le_16(filter->src_port);
5041         if (enables &
5042             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_SRC_PORT_MASK)
5043                 req.src_port_mask = rte_cpu_to_le_16(filter->src_port_mask);
5044         if (enables &
5045             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_PORT)
5046                 req.dst_port = rte_cpu_to_le_16(filter->dst_port);
5047         if (enables &
5048             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_DST_PORT_MASK)
5049                 req.dst_port_mask = rte_cpu_to_le_16(filter->dst_port_mask);
5050         if (enables &
5051             HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_MIRROR_VNIC_ID)
5052                 req.mirror_vnic_id = filter->mirror_vnic_id;
5053
5054         req.enables = rte_cpu_to_le_32(enables);
5055
5056         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5057
5058         HWRM_CHECK_RESULT();
5059
5060         filter->fw_ntuple_filter_id = rte_le_to_cpu_64(resp->ntuple_filter_id);
5061         filter->flow_id = rte_le_to_cpu_32(resp->flow_id);
5062         HWRM_UNLOCK();
5063
5064         return rc;
5065 }
5066
5067 int bnxt_hwrm_clear_ntuple_filter(struct bnxt *bp,
5068                                 struct bnxt_filter_info *filter)
5069 {
5070         int rc = 0;
5071         struct hwrm_cfa_ntuple_filter_free_input req = {.req_type = 0 };
5072         struct hwrm_cfa_ntuple_filter_free_output *resp =
5073                                                 bp->hwrm_cmd_resp_addr;
5074
5075         if (filter->fw_ntuple_filter_id == UINT64_MAX)
5076                 return 0;
5077
5078         HWRM_PREP(&req, HWRM_CFA_NTUPLE_FILTER_FREE, BNXT_USE_CHIMP_MB);
5079
5080         req.ntuple_filter_id = rte_cpu_to_le_64(filter->fw_ntuple_filter_id);
5081
5082         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5083
5084         HWRM_CHECK_RESULT();
5085         HWRM_UNLOCK();
5086
5087         filter->fw_ntuple_filter_id = UINT64_MAX;
5088
5089         return 0;
5090 }
5091
5092 static int
5093 bnxt_vnic_rss_configure_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5094 {
5095         struct hwrm_vnic_rss_cfg_output *resp = bp->hwrm_cmd_resp_addr;
5096         uint8_t *rxq_state = bp->eth_dev->data->rx_queue_state;
5097         struct hwrm_vnic_rss_cfg_input req = {.req_type = 0 };
5098         struct bnxt_rx_queue **rxqs = bp->rx_queues;
5099         uint16_t *ring_tbl = vnic->rss_table;
5100         int nr_ctxs = vnic->num_lb_ctxts;
5101         int max_rings = bp->rx_nr_rings;
5102         int i, j, k, cnt;
5103         int rc = 0;
5104
5105         for (i = 0, k = 0; i < nr_ctxs; i++) {
5106                 struct bnxt_rx_ring_info *rxr;
5107                 struct bnxt_cp_ring_info *cpr;
5108
5109                 HWRM_PREP(&req, HWRM_VNIC_RSS_CFG, BNXT_USE_CHIMP_MB);
5110
5111                 req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
5112                 req.hash_type = rte_cpu_to_le_32(vnic->hash_type);
5113                 req.hash_mode_flags = vnic->hash_mode;
5114
5115                 req.ring_grp_tbl_addr =
5116                     rte_cpu_to_le_64(vnic->rss_table_dma_addr +
5117                                      i * BNXT_RSS_ENTRIES_PER_CTX_P5 *
5118                                      2 * sizeof(*ring_tbl));
5119                 req.hash_key_tbl_addr =
5120                     rte_cpu_to_le_64(vnic->rss_hash_key_dma_addr);
5121
5122                 req.ring_table_pair_index = i;
5123                 req.rss_ctx_idx = rte_cpu_to_le_16(vnic->fw_grp_ids[i]);
5124
5125                 for (j = 0; j < 64; j++) {
5126                         uint16_t ring_id;
5127
5128                         /* Find next active ring. */
5129                         for (cnt = 0; cnt < max_rings; cnt++) {
5130                                 if (rxq_state[k] != RTE_ETH_QUEUE_STATE_STOPPED)
5131                                         break;
5132                                 if (++k == max_rings)
5133                                         k = 0;
5134                         }
5135
5136                         /* Return if no rings are active. */
5137                         if (cnt == max_rings) {
5138                                 HWRM_UNLOCK();
5139                                 return 0;
5140                         }
5141
5142                         /* Add rx/cp ring pair to RSS table. */
5143                         rxr = rxqs[k]->rx_ring;
5144                         cpr = rxqs[k]->cp_ring;
5145
5146                         ring_id = rxr->rx_ring_struct->fw_ring_id;
5147                         *ring_tbl++ = rte_cpu_to_le_16(ring_id);
5148                         ring_id = cpr->cp_ring_struct->fw_ring_id;
5149                         *ring_tbl++ = rte_cpu_to_le_16(ring_id);
5150
5151                         if (++k == max_rings)
5152                                 k = 0;
5153                 }
5154                 rc = bnxt_hwrm_send_message(bp, &req, sizeof(req),
5155                                             BNXT_USE_CHIMP_MB);
5156
5157                 HWRM_CHECK_RESULT();
5158                 HWRM_UNLOCK();
5159         }
5160
5161         return rc;
5162 }
5163
5164 int bnxt_vnic_rss_configure(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5165 {
5166         unsigned int rss_idx, fw_idx, i;
5167
5168         if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
5169                 return 0;
5170
5171         if (!(vnic->rss_table && vnic->hash_type))
5172                 return 0;
5173
5174         if (BNXT_CHIP_P5(bp))
5175                 return bnxt_vnic_rss_configure_p5(bp, vnic);
5176
5177         /*
5178          * Fill the RSS hash & redirection table with
5179          * ring group ids for all VNICs
5180          */
5181         for (rss_idx = 0, fw_idx = 0; rss_idx < HW_HASH_INDEX_SIZE;
5182              rss_idx++, fw_idx++) {
5183                 for (i = 0; i < bp->rx_cp_nr_rings; i++) {
5184                         fw_idx %= bp->rx_cp_nr_rings;
5185                         if (vnic->fw_grp_ids[fw_idx] != INVALID_HW_RING_ID)
5186                                 break;
5187                         fw_idx++;
5188                 }
5189
5190                 if (i == bp->rx_cp_nr_rings)
5191                         return 0;
5192
5193                 vnic->rss_table[rss_idx] = vnic->fw_grp_ids[fw_idx];
5194         }
5195
5196         return bnxt_hwrm_vnic_rss_cfg(bp, vnic);
5197 }
5198
5199 static void bnxt_hwrm_set_coal_params(struct bnxt_coal *hw_coal,
5200         struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
5201 {
5202         uint16_t flags;
5203
5204         req->num_cmpl_aggr_int = rte_cpu_to_le_16(hw_coal->num_cmpl_aggr_int);
5205
5206         /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
5207         req->num_cmpl_dma_aggr = rte_cpu_to_le_16(hw_coal->num_cmpl_dma_aggr);
5208
5209         /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
5210         req->num_cmpl_dma_aggr_during_int =
5211                 rte_cpu_to_le_16(hw_coal->num_cmpl_dma_aggr_during_int);
5212
5213         req->int_lat_tmr_max = rte_cpu_to_le_16(hw_coal->int_lat_tmr_max);
5214
5215         /* min timer set to 1/2 of interrupt timer */
5216         req->int_lat_tmr_min = rte_cpu_to_le_16(hw_coal->int_lat_tmr_min);
5217
5218         /* buf timer set to 1/4 of interrupt timer */
5219         req->cmpl_aggr_dma_tmr = rte_cpu_to_le_16(hw_coal->cmpl_aggr_dma_tmr);
5220
5221         req->cmpl_aggr_dma_tmr_during_int =
5222                 rte_cpu_to_le_16(hw_coal->cmpl_aggr_dma_tmr_during_int);
5223
5224         flags = HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_FLAGS_TIMER_RESET |
5225                 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_FLAGS_RING_IDLE;
5226         req->flags = rte_cpu_to_le_16(flags);
5227 }
5228
5229 static int bnxt_hwrm_set_coal_params_p5(struct bnxt *bp,
5230                 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *agg_req)
5231 {
5232         struct hwrm_ring_aggint_qcaps_input req = {0};
5233         struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5234         uint32_t enables;
5235         uint16_t flags;
5236         int rc;
5237
5238         HWRM_PREP(&req, HWRM_RING_AGGINT_QCAPS, BNXT_USE_CHIMP_MB);
5239         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5240         HWRM_CHECK_RESULT();
5241
5242         agg_req->num_cmpl_dma_aggr = resp->num_cmpl_dma_aggr_max;
5243         agg_req->cmpl_aggr_dma_tmr = resp->cmpl_aggr_dma_tmr_min;
5244
5245         flags = HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_FLAGS_TIMER_RESET |
5246                 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_FLAGS_RING_IDLE;
5247         agg_req->flags = rte_cpu_to_le_16(flags);
5248         enables =
5249          HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_ENABLES_CMPL_AGGR_DMA_TMR |
5250          HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS_INPUT_ENABLES_NUM_CMPL_DMA_AGGR;
5251         agg_req->enables = rte_cpu_to_le_32(enables);
5252
5253         HWRM_UNLOCK();
5254         return rc;
5255 }
5256
5257 int bnxt_hwrm_set_ring_coal(struct bnxt *bp,
5258                         struct bnxt_coal *coal, uint16_t ring_id)
5259 {
5260         struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
5261         struct hwrm_ring_cmpl_ring_cfg_aggint_params_output *resp =
5262                                                 bp->hwrm_cmd_resp_addr;
5263         int rc;
5264
5265         /* Set ring coalesce parameters only for 100G NICs */
5266         if (BNXT_CHIP_P5(bp)) {
5267                 if (bnxt_hwrm_set_coal_params_p5(bp, &req))
5268                         return -1;
5269         } else if (bnxt_stratus_device(bp)) {
5270                 bnxt_hwrm_set_coal_params(coal, &req);
5271         } else {
5272                 return 0;
5273         }
5274
5275         HWRM_PREP(&req,
5276                   HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS,
5277                   BNXT_USE_CHIMP_MB);
5278         req.ring_id = rte_cpu_to_le_16(ring_id);
5279         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5280         HWRM_CHECK_RESULT();
5281         HWRM_UNLOCK();
5282         return 0;
5283 }
5284
5285 #define BNXT_RTE_MEMZONE_FLAG  (RTE_MEMZONE_1GB | RTE_MEMZONE_IOVA_CONTIG)
5286 int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
5287 {
5288         struct hwrm_func_backing_store_qcaps_input req = {0};
5289         struct hwrm_func_backing_store_qcaps_output *resp =
5290                 bp->hwrm_cmd_resp_addr;
5291         struct bnxt_ctx_pg_info *ctx_pg;
5292         struct bnxt_ctx_mem_info *ctx;
5293         int total_alloc_len;
5294         int rc, i, tqm_rings;
5295
5296         if (!BNXT_CHIP_P5(bp) ||
5297             bp->hwrm_spec_code < HWRM_VERSION_1_9_2 ||
5298             BNXT_VF(bp) ||
5299             bp->ctx)
5300                 return 0;
5301
5302         HWRM_PREP(&req, HWRM_FUNC_BACKING_STORE_QCAPS, BNXT_USE_CHIMP_MB);
5303         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5304         HWRM_CHECK_RESULT_SILENT();
5305
5306         total_alloc_len = sizeof(*ctx);
5307         ctx = rte_zmalloc("bnxt_ctx_mem", total_alloc_len,
5308                           RTE_CACHE_LINE_SIZE);
5309         if (!ctx) {
5310                 rc = -ENOMEM;
5311                 goto ctx_err;
5312         }
5313
5314         ctx->qp_max_entries = rte_le_to_cpu_32(resp->qp_max_entries);
5315         ctx->qp_min_qp1_entries =
5316                 rte_le_to_cpu_16(resp->qp_min_qp1_entries);
5317         ctx->qp_max_l2_entries =
5318                 rte_le_to_cpu_16(resp->qp_max_l2_entries);
5319         ctx->qp_entry_size = rte_le_to_cpu_16(resp->qp_entry_size);
5320         ctx->srq_max_l2_entries =
5321                 rte_le_to_cpu_16(resp->srq_max_l2_entries);
5322         ctx->srq_max_entries = rte_le_to_cpu_32(resp->srq_max_entries);
5323         ctx->srq_entry_size = rte_le_to_cpu_16(resp->srq_entry_size);
5324         ctx->cq_max_l2_entries =
5325                 rte_le_to_cpu_16(resp->cq_max_l2_entries);
5326         ctx->cq_max_entries = rte_le_to_cpu_32(resp->cq_max_entries);
5327         ctx->cq_entry_size = rte_le_to_cpu_16(resp->cq_entry_size);
5328         ctx->vnic_max_vnic_entries =
5329                 rte_le_to_cpu_16(resp->vnic_max_vnic_entries);
5330         ctx->vnic_max_ring_table_entries =
5331                 rte_le_to_cpu_16(resp->vnic_max_ring_table_entries);
5332         ctx->vnic_entry_size = rte_le_to_cpu_16(resp->vnic_entry_size);
5333         ctx->stat_max_entries =
5334                 rte_le_to_cpu_32(resp->stat_max_entries);
5335         ctx->stat_entry_size = rte_le_to_cpu_16(resp->stat_entry_size);
5336         ctx->tqm_entry_size = rte_le_to_cpu_16(resp->tqm_entry_size);
5337         ctx->tqm_min_entries_per_ring =
5338                 rte_le_to_cpu_32(resp->tqm_min_entries_per_ring);
5339         ctx->tqm_max_entries_per_ring =
5340                 rte_le_to_cpu_32(resp->tqm_max_entries_per_ring);
5341         ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
5342         if (!ctx->tqm_entries_multiple)
5343                 ctx->tqm_entries_multiple = 1;
5344         ctx->mrav_max_entries =
5345                 rte_le_to_cpu_32(resp->mrav_max_entries);
5346         ctx->mrav_entry_size = rte_le_to_cpu_16(resp->mrav_entry_size);
5347         ctx->tim_entry_size = rte_le_to_cpu_16(resp->tim_entry_size);
5348         ctx->tim_max_entries = rte_le_to_cpu_32(resp->tim_max_entries);
5349         ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
5350
5351         ctx->tqm_fp_rings_count = ctx->tqm_fp_rings_count ?
5352                                   RTE_MIN(ctx->tqm_fp_rings_count,
5353                                           BNXT_MAX_TQM_FP_LEGACY_RINGS) :
5354                                   bp->max_q;
5355
5356         /* Check if the ext ring count needs to be counted.
5357          * Ext ring count is available only with new FW so we should not
5358          * look at the field on older FW.
5359          */
5360         if (ctx->tqm_fp_rings_count == BNXT_MAX_TQM_FP_LEGACY_RINGS &&
5361             bp->hwrm_max_ext_req_len >= BNXT_BACKING_STORE_CFG_LEN) {
5362                 ctx->tqm_fp_rings_count += resp->tqm_fp_rings_count_ext;
5363                 ctx->tqm_fp_rings_count = RTE_MIN(BNXT_MAX_TQM_FP_RINGS,
5364                                                   ctx->tqm_fp_rings_count);
5365         }
5366
5367         tqm_rings = ctx->tqm_fp_rings_count + 1;
5368
5369         ctx_pg = rte_malloc("bnxt_ctx_pg_mem",
5370                             sizeof(*ctx_pg) * tqm_rings,
5371                             RTE_CACHE_LINE_SIZE);
5372         if (!ctx_pg) {
5373                 rc = -ENOMEM;
5374                 goto ctx_err;
5375         }
5376         for (i = 0; i < tqm_rings; i++, ctx_pg++)
5377                 ctx->tqm_mem[i] = ctx_pg;
5378
5379         bp->ctx = ctx;
5380 ctx_err:
5381         HWRM_UNLOCK();
5382         return rc;
5383 }
5384
5385 int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, uint32_t enables)
5386 {
5387         struct hwrm_func_backing_store_cfg_input req = {0};
5388         struct hwrm_func_backing_store_cfg_output *resp =
5389                 bp->hwrm_cmd_resp_addr;
5390         struct bnxt_ctx_mem_info *ctx = bp->ctx;
5391         struct bnxt_ctx_pg_info *ctx_pg;
5392         uint32_t *num_entries;
5393         uint64_t *pg_dir;
5394         uint8_t *pg_attr;
5395         uint32_t ena;
5396         int i, rc;
5397
5398         if (!ctx)
5399                 return 0;
5400
5401         HWRM_PREP(&req, HWRM_FUNC_BACKING_STORE_CFG, BNXT_USE_CHIMP_MB);
5402         req.enables = rte_cpu_to_le_32(enables);
5403
5404         if (enables & HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_QP) {
5405                 ctx_pg = &ctx->qp_mem;
5406                 req.qp_num_entries = rte_cpu_to_le_32(ctx_pg->entries);
5407                 req.qp_num_qp1_entries =
5408                         rte_cpu_to_le_16(ctx->qp_min_qp1_entries);
5409                 req.qp_num_l2_entries =
5410                         rte_cpu_to_le_16(ctx->qp_max_l2_entries);
5411                 req.qp_entry_size = rte_cpu_to_le_16(ctx->qp_entry_size);
5412                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
5413                                       &req.qpc_pg_size_qpc_lvl,
5414                                       &req.qpc_page_dir);
5415         }
5416
5417         if (enables & HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_SRQ) {
5418                 ctx_pg = &ctx->srq_mem;
5419                 req.srq_num_entries = rte_cpu_to_le_32(ctx_pg->entries);
5420                 req.srq_num_l2_entries =
5421                                  rte_cpu_to_le_16(ctx->srq_max_l2_entries);
5422                 req.srq_entry_size = rte_cpu_to_le_16(ctx->srq_entry_size);
5423                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
5424                                       &req.srq_pg_size_srq_lvl,
5425                                       &req.srq_page_dir);
5426         }
5427
5428         if (enables & HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_CQ) {
5429                 ctx_pg = &ctx->cq_mem;
5430                 req.cq_num_entries = rte_cpu_to_le_32(ctx_pg->entries);
5431                 req.cq_num_l2_entries =
5432                                 rte_cpu_to_le_16(ctx->cq_max_l2_entries);
5433                 req.cq_entry_size = rte_cpu_to_le_16(ctx->cq_entry_size);
5434                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
5435                                       &req.cq_pg_size_cq_lvl,
5436                                       &req.cq_page_dir);
5437         }
5438
5439         if (enables & HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_VNIC) {
5440                 ctx_pg = &ctx->vnic_mem;
5441                 req.vnic_num_vnic_entries =
5442                         rte_cpu_to_le_16(ctx->vnic_max_vnic_entries);
5443                 req.vnic_num_ring_table_entries =
5444                         rte_cpu_to_le_16(ctx->vnic_max_ring_table_entries);
5445                 req.vnic_entry_size = rte_cpu_to_le_16(ctx->vnic_entry_size);
5446                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
5447                                       &req.vnic_pg_size_vnic_lvl,
5448                                       &req.vnic_page_dir);
5449         }
5450
5451         if (enables & HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_STAT) {
5452                 ctx_pg = &ctx->stat_mem;
5453                 req.stat_num_entries = rte_cpu_to_le_16(ctx->stat_max_entries);
5454                 req.stat_entry_size = rte_cpu_to_le_16(ctx->stat_entry_size);
5455                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
5456                                       &req.stat_pg_size_stat_lvl,
5457                                       &req.stat_page_dir);
5458         }
5459
5460         req.tqm_entry_size = rte_cpu_to_le_16(ctx->tqm_entry_size);
5461         num_entries = &req.tqm_sp_num_entries;
5462         pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl;
5463         pg_dir = &req.tqm_sp_page_dir;
5464         ena = HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_TQM_SP;
5465         for (i = 0; i < 9; i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
5466                 if (!(enables & ena))
5467                         continue;
5468
5469                 req.tqm_entry_size = rte_cpu_to_le_16(ctx->tqm_entry_size);
5470
5471                 ctx_pg = ctx->tqm_mem[i];
5472                 *num_entries = rte_cpu_to_le_16(ctx_pg->entries);
5473                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
5474         }
5475
5476         if (enables & HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_TQM_RING8) {
5477                 /* DPDK does not need to configure MRAV and TIM type.
5478                  * So we are skipping over MRAV and TIM. Skip to configure
5479                  * HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_TQM_RING8.
5480                  */
5481                 ctx_pg = ctx->tqm_mem[BNXT_MAX_TQM_LEGACY_RINGS];
5482                 req.tqm_ring8_num_entries = rte_cpu_to_le_16(ctx_pg->entries);
5483                 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
5484                                       &req.tqm_ring8_pg_size_tqm_ring_lvl,
5485                                       &req.tqm_ring8_page_dir);
5486         }
5487
5488         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5489         HWRM_CHECK_RESULT();
5490         HWRM_UNLOCK();
5491
5492         return rc;
5493 }
5494
5495 int bnxt_hwrm_ext_port_qstats(struct bnxt *bp)
5496 {
5497         struct hwrm_port_qstats_ext_input req = {0};
5498         struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
5499         struct bnxt_pf_info *pf = bp->pf;
5500         int rc;
5501
5502         if (!(bp->flags & BNXT_FLAG_EXT_RX_PORT_STATS ||
5503               bp->flags & BNXT_FLAG_EXT_TX_PORT_STATS))
5504                 return 0;
5505
5506         HWRM_PREP(&req, HWRM_PORT_QSTATS_EXT, BNXT_USE_CHIMP_MB);
5507
5508         req.port_id = rte_cpu_to_le_16(pf->port_id);
5509         if (bp->flags & BNXT_FLAG_EXT_TX_PORT_STATS) {
5510                 req.tx_stat_host_addr =
5511                         rte_cpu_to_le_64(bp->hw_tx_port_stats_ext_map);
5512                 req.tx_stat_size =
5513                         rte_cpu_to_le_16(sizeof(struct tx_port_stats_ext));
5514         }
5515         if (bp->flags & BNXT_FLAG_EXT_RX_PORT_STATS) {
5516                 req.rx_stat_host_addr =
5517                         rte_cpu_to_le_64(bp->hw_rx_port_stats_ext_map);
5518                 req.rx_stat_size =
5519                         rte_cpu_to_le_16(sizeof(struct rx_port_stats_ext));
5520         }
5521         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5522
5523         if (rc) {
5524                 bp->fw_rx_port_stats_ext_size = 0;
5525                 bp->fw_tx_port_stats_ext_size = 0;
5526         } else {
5527                 bp->fw_rx_port_stats_ext_size =
5528                         rte_le_to_cpu_16(resp->rx_stat_size);
5529                 bp->fw_tx_port_stats_ext_size =
5530                         rte_le_to_cpu_16(resp->tx_stat_size);
5531         }
5532
5533         HWRM_CHECK_RESULT();
5534         HWRM_UNLOCK();
5535
5536         return rc;
5537 }
5538
5539 int
5540 bnxt_hwrm_tunnel_redirect(struct bnxt *bp, uint8_t type)
5541 {
5542         struct hwrm_cfa_redirect_tunnel_type_alloc_input req = {0};
5543         struct hwrm_cfa_redirect_tunnel_type_alloc_output *resp =
5544                 bp->hwrm_cmd_resp_addr;
5545         int rc = 0;
5546
5547         HWRM_PREP(&req, HWRM_CFA_REDIRECT_TUNNEL_TYPE_ALLOC, BNXT_USE_CHIMP_MB);
5548         req.tunnel_type = type;
5549         req.dest_fid = bp->fw_fid;
5550         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5551         HWRM_CHECK_RESULT();
5552
5553         HWRM_UNLOCK();
5554
5555         return rc;
5556 }
5557
5558 int
5559 bnxt_hwrm_tunnel_redirect_free(struct bnxt *bp, uint8_t type)
5560 {
5561         struct hwrm_cfa_redirect_tunnel_type_free_input req = {0};
5562         struct hwrm_cfa_redirect_tunnel_type_free_output *resp =
5563                 bp->hwrm_cmd_resp_addr;
5564         int rc = 0;
5565
5566         HWRM_PREP(&req, HWRM_CFA_REDIRECT_TUNNEL_TYPE_FREE, BNXT_USE_CHIMP_MB);
5567         req.tunnel_type = type;
5568         req.dest_fid = bp->fw_fid;
5569         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5570         HWRM_CHECK_RESULT();
5571
5572         HWRM_UNLOCK();
5573
5574         return rc;
5575 }
5576
5577 int bnxt_hwrm_tunnel_redirect_query(struct bnxt *bp, uint32_t *type)
5578 {
5579         struct hwrm_cfa_redirect_query_tunnel_type_input req = {0};
5580         struct hwrm_cfa_redirect_query_tunnel_type_output *resp =
5581                 bp->hwrm_cmd_resp_addr;
5582         int rc = 0;
5583
5584         HWRM_PREP(&req, HWRM_CFA_REDIRECT_QUERY_TUNNEL_TYPE, BNXT_USE_CHIMP_MB);
5585         req.src_fid = bp->fw_fid;
5586         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5587         HWRM_CHECK_RESULT();
5588
5589         if (type)
5590                 *type = rte_le_to_cpu_32(resp->tunnel_mask);
5591
5592         HWRM_UNLOCK();
5593
5594         return rc;
5595 }
5596
5597 int bnxt_hwrm_tunnel_redirect_info(struct bnxt *bp, uint8_t tun_type,
5598                                    uint16_t *dst_fid)
5599 {
5600         struct hwrm_cfa_redirect_tunnel_type_info_input req = {0};
5601         struct hwrm_cfa_redirect_tunnel_type_info_output *resp =
5602                 bp->hwrm_cmd_resp_addr;
5603         int rc = 0;
5604
5605         HWRM_PREP(&req, HWRM_CFA_REDIRECT_TUNNEL_TYPE_INFO, BNXT_USE_CHIMP_MB);
5606         req.src_fid = bp->fw_fid;
5607         req.tunnel_type = tun_type;
5608         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5609         HWRM_CHECK_RESULT();
5610
5611         if (dst_fid)
5612                 *dst_fid = rte_le_to_cpu_16(resp->dest_fid);
5613
5614         PMD_DRV_LOG(DEBUG, "dst_fid: %x\n", resp->dest_fid);
5615
5616         HWRM_UNLOCK();
5617
5618         return rc;
5619 }
5620
5621 int bnxt_hwrm_set_mac(struct bnxt *bp)
5622 {
5623         struct hwrm_func_vf_cfg_output *resp = bp->hwrm_cmd_resp_addr;
5624         struct hwrm_func_vf_cfg_input req = {0};
5625         int rc = 0;
5626
5627         if (!BNXT_VF(bp))
5628                 return 0;
5629
5630         HWRM_PREP(&req, HWRM_FUNC_VF_CFG, BNXT_USE_CHIMP_MB);
5631
5632         req.enables =
5633                 rte_cpu_to_le_32(HWRM_FUNC_VF_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
5634         memcpy(req.dflt_mac_addr, bp->mac_addr, RTE_ETHER_ADDR_LEN);
5635
5636         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5637
5638         HWRM_CHECK_RESULT();
5639
5640         HWRM_UNLOCK();
5641
5642         return rc;
5643 }
5644
5645 int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
5646 {
5647         struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
5648         struct hwrm_func_drv_if_change_input req = {0};
5649         uint32_t flags;
5650         int rc;
5651
5652         if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
5653                 return 0;
5654
5655         /* Do not issue FUNC_DRV_IF_CHANGE during reset recovery.
5656          * If we issue FUNC_DRV_IF_CHANGE with flags down before
5657          * FUNC_DRV_UNRGTR, FW resets before FUNC_DRV_UNRGTR
5658          */
5659         if (!up && (bp->flags & BNXT_FLAG_FW_RESET))
5660                 return 0;
5661
5662         HWRM_PREP(&req, HWRM_FUNC_DRV_IF_CHANGE, BNXT_USE_CHIMP_MB);
5663
5664         if (up)
5665                 req.flags =
5666                 rte_cpu_to_le_32(HWRM_FUNC_DRV_IF_CHANGE_INPUT_FLAGS_UP);
5667
5668         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5669
5670         HWRM_CHECK_RESULT();
5671         flags = rte_le_to_cpu_32(resp->flags);
5672         HWRM_UNLOCK();
5673
5674         if (!up)
5675                 return 0;
5676
5677         if (flags & HWRM_FUNC_DRV_IF_CHANGE_OUTPUT_FLAGS_HOT_FW_RESET_DONE) {
5678                 PMD_DRV_LOG(INFO, "FW reset happened while port was down\n");
5679                 bp->flags |= BNXT_FLAG_IF_CHANGE_HOT_FW_RESET_DONE;
5680         }
5681
5682         return 0;
5683 }
5684
5685 int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
5686 {
5687         struct hwrm_error_recovery_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5688         struct bnxt_error_recovery_info *info = bp->recovery_info;
5689         struct hwrm_error_recovery_qcfg_input req = {0};
5690         uint32_t flags = 0;
5691         unsigned int i;
5692         int rc;
5693
5694         /* Older FW does not have error recovery support */
5695         if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5696                 return 0;
5697
5698         HWRM_PREP(&req, HWRM_ERROR_RECOVERY_QCFG, BNXT_USE_CHIMP_MB);
5699
5700         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5701
5702         HWRM_CHECK_RESULT();
5703
5704         flags = rte_le_to_cpu_32(resp->flags);
5705         if (flags & HWRM_ERROR_RECOVERY_QCFG_OUTPUT_FLAGS_HOST)
5706                 info->flags |= BNXT_FLAG_ERROR_RECOVERY_HOST;
5707         else if (flags & HWRM_ERROR_RECOVERY_QCFG_OUTPUT_FLAGS_CO_CPU)
5708                 info->flags |= BNXT_FLAG_ERROR_RECOVERY_CO_CPU;
5709
5710         if ((info->flags & BNXT_FLAG_ERROR_RECOVERY_CO_CPU) &&
5711             !(bp->flags & BNXT_FLAG_KONG_MB_EN)) {
5712                 rc = -EINVAL;
5713                 goto err;
5714         }
5715
5716         /* FW returned values are in units of 100msec */
5717         info->driver_polling_freq =
5718                 rte_le_to_cpu_32(resp->driver_polling_freq) * 100;
5719         info->primary_func_wait_period =
5720                 rte_le_to_cpu_32(resp->master_func_wait_period) * 100;
5721         info->normal_func_wait_period =
5722                 rte_le_to_cpu_32(resp->normal_func_wait_period) * 100;
5723         info->primary_func_wait_period_after_reset =
5724                 rte_le_to_cpu_32(resp->master_func_wait_period_after_reset) * 100;
5725         info->max_bailout_time_after_reset =
5726                 rte_le_to_cpu_32(resp->max_bailout_time_after_reset) * 100;
5727         info->status_regs[BNXT_FW_STATUS_REG] =
5728                 rte_le_to_cpu_32(resp->fw_health_status_reg);
5729         info->status_regs[BNXT_FW_HEARTBEAT_CNT_REG] =
5730                 rte_le_to_cpu_32(resp->fw_heartbeat_reg);
5731         info->status_regs[BNXT_FW_RECOVERY_CNT_REG] =
5732                 rte_le_to_cpu_32(resp->fw_reset_cnt_reg);
5733         info->status_regs[BNXT_FW_RESET_INPROG_REG] =
5734                 rte_le_to_cpu_32(resp->reset_inprogress_reg);
5735         info->reg_array_cnt =
5736                 rte_le_to_cpu_32(resp->reg_array_cnt);
5737
5738         if (info->reg_array_cnt >= BNXT_NUM_RESET_REG) {
5739                 rc = -EINVAL;
5740                 goto err;
5741         }
5742
5743         for (i = 0; i < info->reg_array_cnt; i++) {
5744                 info->reset_reg[i] =
5745                         rte_le_to_cpu_32(resp->reset_reg[i]);
5746                 info->reset_reg_val[i] =
5747                         rte_le_to_cpu_32(resp->reset_reg_val[i]);
5748                 info->delay_after_reset[i] =
5749                         resp->delay_after_reset[i];
5750         }
5751 err:
5752         HWRM_UNLOCK();
5753
5754         /* Map the FW status registers */
5755         if (!rc)
5756                 rc = bnxt_map_fw_health_status_regs(bp);
5757
5758         if (rc) {
5759                 rte_free(bp->recovery_info);
5760                 bp->recovery_info = NULL;
5761         }
5762         return rc;
5763 }
5764
5765 int bnxt_hwrm_fw_reset(struct bnxt *bp)
5766 {
5767         struct hwrm_fw_reset_output *resp = bp->hwrm_cmd_resp_addr;
5768         struct hwrm_fw_reset_input req = {0};
5769         int rc;
5770
5771         if (!BNXT_PF(bp))
5772                 return -EOPNOTSUPP;
5773
5774         HWRM_PREP(&req, HWRM_FW_RESET, BNXT_USE_KONG(bp));
5775
5776         req.embedded_proc_type =
5777                 HWRM_FW_RESET_INPUT_EMBEDDED_PROC_TYPE_CHIP;
5778         req.selfrst_status =
5779                 HWRM_FW_RESET_INPUT_SELFRST_STATUS_SELFRSTASAP;
5780         req.flags = HWRM_FW_RESET_INPUT_FLAGS_RESET_GRACEFUL;
5781
5782         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req),
5783                                     BNXT_USE_KONG(bp));
5784
5785         HWRM_CHECK_RESULT();
5786         HWRM_UNLOCK();
5787
5788         return rc;
5789 }
5790
5791 int bnxt_hwrm_port_ts_query(struct bnxt *bp, uint8_t path, uint64_t *timestamp)
5792 {
5793         struct hwrm_port_ts_query_output *resp = bp->hwrm_cmd_resp_addr;
5794         struct hwrm_port_ts_query_input req = {0};
5795         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
5796         uint32_t flags = 0;
5797         int rc;
5798
5799         if (!ptp)
5800                 return 0;
5801
5802         HWRM_PREP(&req, HWRM_PORT_TS_QUERY, BNXT_USE_CHIMP_MB);
5803
5804         switch (path) {
5805         case BNXT_PTP_FLAGS_PATH_TX:
5806                 flags |= HWRM_PORT_TS_QUERY_INPUT_FLAGS_PATH_TX;
5807                 break;
5808         case BNXT_PTP_FLAGS_PATH_RX:
5809                 flags |= HWRM_PORT_TS_QUERY_INPUT_FLAGS_PATH_RX;
5810                 break;
5811         case BNXT_PTP_FLAGS_CURRENT_TIME:
5812                 flags |= HWRM_PORT_TS_QUERY_INPUT_FLAGS_CURRENT_TIME;
5813                 break;
5814         }
5815
5816         req.flags = rte_cpu_to_le_32(flags);
5817         req.port_id = rte_cpu_to_le_16(bp->pf->port_id);
5818
5819         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
5820
5821         HWRM_CHECK_RESULT();
5822
5823         if (timestamp) {
5824                 *timestamp = rte_le_to_cpu_32(resp->ptp_msg_ts[0]);
5825                 *timestamp |=
5826                         (uint64_t)(rte_le_to_cpu_32(resp->ptp_msg_ts[1])) << 32;
5827         }
5828         HWRM_UNLOCK();
5829
5830         return rc;
5831 }
5832
5833 int bnxt_hwrm_cfa_counter_qcaps(struct bnxt *bp, uint16_t *max_fc)
5834 {
5835         int rc = 0;
5836
5837         struct hwrm_cfa_counter_qcaps_input req = {0};
5838         struct hwrm_cfa_counter_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5839
5840         if (!(BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp))) {
5841                 PMD_DRV_LOG(DEBUG,
5842                             "Not a PF or trusted VF. Command not supported\n");
5843                 return 0;
5844         }
5845
5846         HWRM_PREP(&req, HWRM_CFA_COUNTER_QCAPS, BNXT_USE_KONG(bp));
5847         req.target_id = rte_cpu_to_le_16(bp->fw_fid);
5848         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_KONG(bp));
5849
5850         HWRM_CHECK_RESULT();
5851         if (max_fc)
5852                 *max_fc = rte_le_to_cpu_16(resp->max_rx_fc);
5853         HWRM_UNLOCK();
5854
5855         return 0;
5856 }
5857
5858 int bnxt_hwrm_ctx_rgtr(struct bnxt *bp, rte_iova_t dma_addr, uint16_t *ctx_id)
5859 {
5860         int rc = 0;
5861         struct hwrm_cfa_ctx_mem_rgtr_input req = {.req_type = 0 };
5862         struct hwrm_cfa_ctx_mem_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
5863
5864         if (!(BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp))) {
5865                 PMD_DRV_LOG(DEBUG,
5866                             "Not a PF or trusted VF. Command not supported\n");
5867                 return 0;
5868         }
5869
5870         HWRM_PREP(&req, HWRM_CFA_CTX_MEM_RGTR, BNXT_USE_KONG(bp));
5871
5872         req.page_level = HWRM_CFA_CTX_MEM_RGTR_INPUT_PAGE_LEVEL_LVL_0;
5873         req.page_size = HWRM_CFA_CTX_MEM_RGTR_INPUT_PAGE_SIZE_2M;
5874         req.page_dir = rte_cpu_to_le_64(dma_addr);
5875
5876         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_KONG(bp));
5877
5878         HWRM_CHECK_RESULT();
5879         if (ctx_id) {
5880                 *ctx_id  = rte_le_to_cpu_16(resp->ctx_id);
5881                 PMD_DRV_LOG(DEBUG, "ctx_id = %d\n", *ctx_id);
5882         }
5883         HWRM_UNLOCK();
5884
5885         return 0;
5886 }
5887
5888 int bnxt_hwrm_ctx_unrgtr(struct bnxt *bp, uint16_t ctx_id)
5889 {
5890         int rc = 0;
5891         struct hwrm_cfa_ctx_mem_unrgtr_input req = {.req_type = 0 };
5892         struct hwrm_cfa_ctx_mem_unrgtr_output *resp = bp->hwrm_cmd_resp_addr;
5893
5894         if (!(BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp))) {
5895                 PMD_DRV_LOG(DEBUG,
5896                             "Not a PF or trusted VF. Command not supported\n");
5897                 return 0;
5898         }
5899
5900         HWRM_PREP(&req, HWRM_CFA_CTX_MEM_UNRGTR, BNXT_USE_KONG(bp));
5901
5902         req.ctx_id = rte_cpu_to_le_16(ctx_id);
5903
5904         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_KONG(bp));
5905
5906         HWRM_CHECK_RESULT();
5907         HWRM_UNLOCK();
5908
5909         return rc;
5910 }
5911
5912 int bnxt_hwrm_cfa_counter_cfg(struct bnxt *bp, enum bnxt_flow_dir dir,
5913                               uint16_t cntr, uint16_t ctx_id,
5914                               uint32_t num_entries, bool enable)
5915 {
5916         struct hwrm_cfa_counter_cfg_input req = {0};
5917         struct hwrm_cfa_counter_cfg_output *resp = bp->hwrm_cmd_resp_addr;
5918         uint16_t flags = 0;
5919         int rc;
5920
5921         if (!(BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp))) {
5922                 PMD_DRV_LOG(DEBUG,
5923                             "Not a PF or trusted VF. Command not supported\n");
5924                 return 0;
5925         }
5926
5927         HWRM_PREP(&req, HWRM_CFA_COUNTER_CFG, BNXT_USE_KONG(bp));
5928
5929         req.target_id = rte_cpu_to_le_16(bp->fw_fid);
5930         req.counter_type = rte_cpu_to_le_16(cntr);
5931         flags = enable ? HWRM_CFA_COUNTER_CFG_INPUT_FLAGS_CFG_MODE_ENABLE :
5932                 HWRM_CFA_COUNTER_CFG_INPUT_FLAGS_CFG_MODE_DISABLE;
5933         flags |= HWRM_CFA_COUNTER_CFG_INPUT_FLAGS_DATA_TRANSFER_MODE_PULL;
5934         if (dir == BNXT_DIR_RX)
5935                 flags |=  HWRM_CFA_COUNTER_CFG_INPUT_FLAGS_PATH_RX;
5936         else if (dir == BNXT_DIR_TX)
5937                 flags |=  HWRM_CFA_COUNTER_CFG_INPUT_FLAGS_PATH_TX;
5938         req.flags = rte_cpu_to_le_16(flags);
5939         req.ctx_id =  rte_cpu_to_le_16(ctx_id);
5940         req.num_entries = rte_cpu_to_le_32(num_entries);
5941
5942         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_KONG(bp));
5943         HWRM_CHECK_RESULT();
5944         HWRM_UNLOCK();
5945
5946         return 0;
5947 }
5948
5949 int bnxt_hwrm_cfa_counter_qstats(struct bnxt *bp,
5950                                  enum bnxt_flow_dir dir,
5951                                  uint16_t cntr,
5952                                  uint16_t num_entries)
5953 {
5954         struct hwrm_cfa_counter_qstats_output *resp = bp->hwrm_cmd_resp_addr;
5955         struct hwrm_cfa_counter_qstats_input req = {0};
5956         uint16_t flow_ctx_id = 0;
5957         uint16_t flags = 0;
5958         int rc = 0;
5959
5960         if (!(BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp))) {
5961                 PMD_DRV_LOG(DEBUG,
5962                             "Not a PF or trusted VF. Command not supported\n");
5963                 return 0;
5964         }
5965
5966         if (dir == BNXT_DIR_RX) {
5967                 flow_ctx_id = bp->flow_stat->rx_fc_in_tbl.ctx_id;
5968                 flags = HWRM_CFA_COUNTER_QSTATS_INPUT_FLAGS_PATH_RX;
5969         } else if (dir == BNXT_DIR_TX) {
5970                 flow_ctx_id = bp->flow_stat->tx_fc_in_tbl.ctx_id;
5971                 flags = HWRM_CFA_COUNTER_QSTATS_INPUT_FLAGS_PATH_TX;
5972         }
5973
5974         HWRM_PREP(&req, HWRM_CFA_COUNTER_QSTATS, BNXT_USE_KONG(bp));
5975         req.target_id = rte_cpu_to_le_16(bp->fw_fid);
5976         req.counter_type = rte_cpu_to_le_16(cntr);
5977         req.input_flow_ctx_id = rte_cpu_to_le_16(flow_ctx_id);
5978         req.num_entries = rte_cpu_to_le_16(num_entries);
5979         req.flags = rte_cpu_to_le_16(flags);
5980         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_KONG(bp));
5981
5982         HWRM_CHECK_RESULT();
5983         HWRM_UNLOCK();
5984
5985         return 0;
5986 }
5987
5988 int bnxt_hwrm_first_vf_id_query(struct bnxt *bp, uint16_t fid,
5989                                 uint16_t *first_vf_id)
5990 {
5991         int rc = 0;
5992         struct hwrm_func_qcaps_input req = {.req_type = 0 };
5993         struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5994
5995         HWRM_PREP(&req, HWRM_FUNC_QCAPS, BNXT_USE_CHIMP_MB);
5996
5997         req.fid = rte_cpu_to_le_16(fid);
5998
5999         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
6000
6001         HWRM_CHECK_RESULT();
6002
6003         if (first_vf_id)
6004                 *first_vf_id = rte_le_to_cpu_16(resp->first_vf_id);
6005
6006         HWRM_UNLOCK();
6007
6008         return rc;
6009 }
6010
6011 int bnxt_hwrm_cfa_pair_exists(struct bnxt *bp, struct bnxt_representor *rep_bp)
6012 {
6013         struct hwrm_cfa_pair_info_output *resp = bp->hwrm_cmd_resp_addr;
6014         struct hwrm_cfa_pair_info_input req = {0};
6015         int rc = 0;
6016
6017         if (!(BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp))) {
6018                 PMD_DRV_LOG(DEBUG,
6019                             "Not a PF or trusted VF. Command not supported\n");
6020                 return 0;
6021         }
6022
6023         HWRM_PREP(&req, HWRM_CFA_PAIR_INFO, BNXT_USE_CHIMP_MB);
6024         snprintf(req.pair_name, sizeof(req.pair_name), "%svfr%d",
6025                  bp->eth_dev->data->name, rep_bp->vf_id);
6026         req.flags =
6027                 rte_cpu_to_le_32(HWRM_CFA_PAIR_INFO_INPUT_FLAGS_LOOKUP_TYPE);
6028
6029         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
6030         HWRM_CHECK_RESULT();
6031         if (rc == HWRM_ERR_CODE_SUCCESS && strlen(resp->pair_name)) {
6032                 HWRM_UNLOCK();
6033                 return !rc;
6034         }
6035         HWRM_UNLOCK();
6036         return rc;
6037 }
6038
6039 int bnxt_hwrm_cfa_pair_alloc(struct bnxt *bp, struct bnxt_representor *rep_bp)
6040 {
6041         struct hwrm_cfa_pair_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6042         struct hwrm_cfa_pair_alloc_input req = {0};
6043         int rc;
6044
6045         if (!(BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp))) {
6046                 PMD_DRV_LOG(DEBUG,
6047                             "Not a PF or trusted VF. Command not supported\n");
6048                 return 0;
6049         }
6050
6051         HWRM_PREP(&req, HWRM_CFA_PAIR_ALLOC, BNXT_USE_CHIMP_MB);
6052         req.pair_mode = HWRM_CFA_PAIR_FREE_INPUT_PAIR_MODE_REP2FN_TRUFLOW;
6053         snprintf(req.pair_name, sizeof(req.pair_name), "%svfr%d",
6054                  bp->eth_dev->data->name, rep_bp->vf_id);
6055
6056         req.pf_b_id = rep_bp->parent_pf_idx;
6057         req.vf_b_id = BNXT_REP_PF(rep_bp) ? rte_cpu_to_le_16(((uint16_t)-1)) :
6058                                                 rte_cpu_to_le_16(rep_bp->vf_id);
6059         req.vf_a_id = rte_cpu_to_le_16(bp->fw_fid);
6060         req.host_b_id = 1; /* TBD - Confirm if this is OK */
6061
6062         req.enables |= rep_bp->flags & BNXT_REP_Q_R2F_VALID ?
6063                         HWRM_CFA_PAIR_ALLOC_INPUT_ENABLES_Q_AB_VALID : 0;
6064         req.enables |= rep_bp->flags & BNXT_REP_Q_F2R_VALID ?
6065                         HWRM_CFA_PAIR_ALLOC_INPUT_ENABLES_Q_BA_VALID : 0;
6066         req.enables |= rep_bp->flags & BNXT_REP_FC_R2F_VALID ?
6067                         HWRM_CFA_PAIR_ALLOC_INPUT_ENABLES_FC_AB_VALID : 0;
6068         req.enables |= rep_bp->flags & BNXT_REP_FC_F2R_VALID ?
6069                         HWRM_CFA_PAIR_ALLOC_INPUT_ENABLES_FC_BA_VALID : 0;
6070
6071         req.q_ab = rep_bp->rep_q_r2f;
6072         req.q_ba = rep_bp->rep_q_f2r;
6073         req.fc_ab = rep_bp->rep_fc_r2f;
6074         req.fc_ba = rep_bp->rep_fc_f2r;
6075
6076         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
6077         HWRM_CHECK_RESULT();
6078
6079         HWRM_UNLOCK();
6080         PMD_DRV_LOG(DEBUG, "%s %d allocated\n",
6081                     BNXT_REP_PF(rep_bp) ? "PFR" : "VFR", rep_bp->vf_id);
6082         return rc;
6083 }
6084
6085 int bnxt_hwrm_cfa_pair_free(struct bnxt *bp, struct bnxt_representor *rep_bp)
6086 {
6087         struct hwrm_cfa_pair_free_output *resp = bp->hwrm_cmd_resp_addr;
6088         struct hwrm_cfa_pair_free_input req = {0};
6089         int rc;
6090
6091         if (!(BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp))) {
6092                 PMD_DRV_LOG(DEBUG,
6093                             "Not a PF or trusted VF. Command not supported\n");
6094                 return 0;
6095         }
6096
6097         HWRM_PREP(&req, HWRM_CFA_PAIR_FREE, BNXT_USE_CHIMP_MB);
6098         snprintf(req.pair_name, sizeof(req.pair_name), "%svfr%d",
6099                  bp->eth_dev->data->name, rep_bp->vf_id);
6100         req.pf_b_id = rep_bp->parent_pf_idx;
6101         req.pair_mode = HWRM_CFA_PAIR_FREE_INPUT_PAIR_MODE_REP2FN_TRUFLOW;
6102         req.vf_id = BNXT_REP_PF(rep_bp) ? rte_cpu_to_le_16(((uint16_t)-1)) :
6103                                                 rte_cpu_to_le_16(rep_bp->vf_id);
6104         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
6105         HWRM_CHECK_RESULT();
6106         HWRM_UNLOCK();
6107         PMD_DRV_LOG(DEBUG, "%s %d freed\n", BNXT_REP_PF(rep_bp) ? "PFR" : "VFR",
6108                     rep_bp->vf_id);
6109         return rc;
6110 }
6111
6112 int bnxt_hwrm_fw_echo_reply(struct bnxt *bp, uint32_t echo_req_data1,
6113                             uint32_t echo_req_data2)
6114 {
6115         struct hwrm_func_echo_response_input req = {0};
6116         struct hwrm_func_echo_response_output *resp = bp->hwrm_cmd_resp_addr;
6117         int rc;
6118
6119         HWRM_PREP(&req, HWRM_FUNC_ECHO_RESPONSE, BNXT_USE_CHIMP_MB);
6120         req.event_data1 = rte_cpu_to_le_32(echo_req_data1);
6121         req.event_data2 = rte_cpu_to_le_32(echo_req_data2);
6122
6123         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
6124
6125         HWRM_CHECK_RESULT();
6126         HWRM_UNLOCK();
6127
6128         return rc;
6129 }
6130
6131 int bnxt_hwrm_poll_ver_get(struct bnxt *bp)
6132 {
6133         struct hwrm_ver_get_input req = {.req_type = 0 };
6134         struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
6135         int rc = 0;
6136
6137         bp->max_req_len = HWRM_MAX_REQ_LEN;
6138         bp->max_resp_len = BNXT_PAGE_SIZE;
6139         bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
6140
6141         HWRM_PREP(&req, HWRM_VER_GET, BNXT_USE_CHIMP_MB);
6142         req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
6143         req.hwrm_intf_min = HWRM_VERSION_MINOR;
6144         req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
6145
6146         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
6147
6148         HWRM_CHECK_RESULT_SILENT();
6149
6150         if (resp->flags & HWRM_VER_GET_OUTPUT_FLAGS_DEV_NOT_RDY)
6151                 rc = -EAGAIN;
6152
6153         HWRM_UNLOCK();
6154
6155         return rc;
6156 }
6157
6158 int bnxt_hwrm_read_sfp_module_eeprom_info(struct bnxt *bp, uint16_t i2c_addr,
6159                                           uint16_t page_number, uint16_t start_addr,
6160                                           uint16_t data_length, uint8_t *buf)
6161 {
6162         struct hwrm_port_phy_i2c_read_output *resp = bp->hwrm_cmd_resp_addr;
6163         struct hwrm_port_phy_i2c_read_input req = {0};
6164         uint32_t enables = HWRM_PORT_PHY_I2C_READ_INPUT_ENABLES_PAGE_OFFSET;
6165         int rc, byte_offset = 0;
6166
6167         do {
6168                 uint16_t xfer_size;
6169
6170                 HWRM_PREP(&req, HWRM_PORT_PHY_I2C_READ, BNXT_USE_CHIMP_MB);
6171                 req.i2c_slave_addr = i2c_addr;
6172                 req.page_number = rte_cpu_to_le_16(page_number);
6173                 req.port_id = rte_cpu_to_le_16(bp->pf->port_id);
6174
6175                 xfer_size = RTE_MIN(data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
6176                 req.page_offset = rte_cpu_to_le_16(start_addr + byte_offset);
6177                 req.data_length = xfer_size;
6178                 req.enables = rte_cpu_to_le_32(start_addr + byte_offset ? enables : 0);
6179                 rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
6180                 HWRM_CHECK_RESULT();
6181
6182                 memcpy(buf + byte_offset, resp->data, xfer_size);
6183
6184                 data_length -= xfer_size;
6185                 byte_offset += xfer_size;
6186
6187                 HWRM_UNLOCK();
6188         } while (data_length > 0);
6189
6190         return rc;
6191 }
6192
6193 void bnxt_free_hwrm_tx_ring(struct bnxt *bp, int queue_index)
6194 {
6195         struct bnxt_tx_queue *txq = bp->tx_queues[queue_index];
6196         struct bnxt_tx_ring_info *txr = txq->tx_ring;
6197         struct bnxt_ring *ring = txr->tx_ring_struct;
6198         struct bnxt_cp_ring_info *cpr = txq->cp_ring;
6199
6200         bnxt_hwrm_ring_free(bp, ring,
6201                             HWRM_RING_FREE_INPUT_RING_TYPE_TX,
6202                             cpr->cp_ring_struct->fw_ring_id);
6203         txr->tx_raw_prod = 0;
6204         txr->tx_raw_cons = 0;
6205         memset(txr->tx_desc_ring, 0,
6206                 txr->tx_ring_struct->ring_size * sizeof(*txr->tx_desc_ring));
6207         memset(txr->tx_buf_ring, 0,
6208                 txr->tx_ring_struct->ring_size * sizeof(*txr->tx_buf_ring));
6209
6210         bnxt_hwrm_stat_ctx_free(bp, cpr);
6211
6212         bnxt_free_cp_ring(bp, cpr);
6213 }
6214
6215 int bnxt_hwrm_config_host_mtu(struct bnxt *bp)
6216 {
6217         struct hwrm_func_cfg_input req = {0};
6218         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
6219         int rc;
6220
6221         if (!BNXT_PF(bp))
6222                 return 0;
6223
6224         HWRM_PREP(&req, HWRM_FUNC_CFG, BNXT_USE_CHIMP_MB);
6225
6226         req.fid = rte_cpu_to_le_16(0xffff);
6227         req.enables = rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_HOST_MTU);
6228         req.host_mtu = rte_cpu_to_le_16(bp->eth_dev->data->mtu);
6229
6230         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
6231         HWRM_CHECK_RESULT();
6232         HWRM_UNLOCK();
6233
6234         return rc;
6235 }
6236
6237 int
6238 bnxt_vnic_rss_clear_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6239 {
6240         struct hwrm_vnic_rss_cfg_output *resp = bp->hwrm_cmd_resp_addr;
6241         struct hwrm_vnic_rss_cfg_input req = {0};
6242         int nr_ctxs = vnic->num_lb_ctxts;
6243         int i, rc = 0;
6244
6245         for (i = 0; i < nr_ctxs; i++) {
6246                 HWRM_PREP(&req, HWRM_VNIC_RSS_CFG, BNXT_USE_CHIMP_MB);
6247
6248                 req.rss_ctx_idx = rte_cpu_to_le_16(vnic->fw_grp_ids[i]);
6249                 req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
6250
6251                 rc = bnxt_hwrm_send_message(bp, &req, sizeof(req), BNXT_USE_CHIMP_MB);
6252
6253                 HWRM_CHECK_RESULT();
6254                 HWRM_UNLOCK();
6255         }
6256
6257         return rc;
6258 }