net/bnxt: support Stratus VF device
[dpdk.git] / drivers / net / bnxt / bnxt_hwrm.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) Broadcom Limited.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Broadcom Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <unistd.h>
35
36 #include <unistd.h>
37
38 #include <rte_byteorder.h>
39 #include <rte_common.h>
40 #include <rte_cycles.h>
41 #include <rte_malloc.h>
42 #include <rte_memzone.h>
43 #include <rte_version.h>
44
45 #include "bnxt.h"
46 #include "bnxt_cpr.h"
47 #include "bnxt_filter.h"
48 #include "bnxt_hwrm.h"
49 #include "bnxt_rxq.h"
50 #include "bnxt_rxr.h"
51 #include "bnxt_ring.h"
52 #include "bnxt_txq.h"
53 #include "bnxt_txr.h"
54 #include "bnxt_vnic.h"
55 #include "hsi_struct_def_dpdk.h"
56
57 #include <rte_io.h>
58
59 #define HWRM_CMD_TIMEOUT                2000
60
61 struct bnxt_plcmodes_cfg {
62         uint32_t        flags;
63         uint16_t        jumbo_thresh;
64         uint16_t        hds_offset;
65         uint16_t        hds_threshold;
66 };
67
68 static int page_getenum(size_t size)
69 {
70         if (size <= 1 << 4)
71                 return 4;
72         if (size <= 1 << 12)
73                 return 12;
74         if (size <= 1 << 13)
75                 return 13;
76         if (size <= 1 << 16)
77                 return 16;
78         if (size <= 1 << 21)
79                 return 21;
80         if (size <= 1 << 22)
81                 return 22;
82         if (size <= 1 << 30)
83                 return 30;
84         RTE_LOG(ERR, PMD, "Page size %zu out of range\n", size);
85         return sizeof(void *) * 8 - 1;
86 }
87
88 static int page_roundup(size_t size)
89 {
90         return 1 << page_getenum(size);
91 }
92
93 /*
94  * HWRM Functions (sent to HWRM)
95  * These are named bnxt_hwrm_*() and return -1 if bnxt_hwrm_send_message()
96  * fails (ie: a timeout), and a positive non-zero HWRM error code if the HWRM
97  * command was failed by the ChiMP.
98  */
99
100 static int bnxt_hwrm_send_message_locked(struct bnxt *bp, void *msg,
101                                         uint32_t msg_len)
102 {
103         unsigned int i;
104         struct input *req = msg;
105         struct output *resp = bp->hwrm_cmd_resp_addr;
106         uint32_t *data = msg;
107         uint8_t *bar;
108         uint8_t *valid;
109         uint16_t max_req_len = bp->max_req_len;
110         struct hwrm_short_input short_input = { 0 };
111
112         if (bp->flags & BNXT_FLAG_SHORT_CMD) {
113                 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
114
115                 memset(short_cmd_req, 0, bp->max_req_len);
116                 memcpy(short_cmd_req, req, msg_len);
117
118                 short_input.req_type = rte_cpu_to_le_16(req->req_type);
119                 short_input.signature = rte_cpu_to_le_16(
120                                         HWRM_SHORT_REQ_SIGNATURE_SHORT_CMD);
121                 short_input.size = rte_cpu_to_le_16(msg_len);
122                 short_input.req_addr =
123                         rte_cpu_to_le_64(bp->hwrm_short_cmd_req_dma_addr);
124
125                 data = (uint32_t *)&short_input;
126                 msg_len = sizeof(short_input);
127
128                 /* Sync memory write before updating doorbell */
129                 rte_wmb();
130
131                 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
132         }
133
134         /* Write request msg to hwrm channel */
135         for (i = 0; i < msg_len; i += 4) {
136                 bar = (uint8_t *)bp->bar0 + i;
137                 rte_write32(*data, bar);
138                 data++;
139         }
140
141         /* Zero the rest of the request space */
142         for (; i < max_req_len; i += 4) {
143                 bar = (uint8_t *)bp->bar0 + i;
144                 rte_write32(0, bar);
145         }
146
147         /* Ring channel doorbell */
148         bar = (uint8_t *)bp->bar0 + 0x100;
149         rte_write32(1, bar);
150
151         /* Poll for the valid bit */
152         for (i = 0; i < HWRM_CMD_TIMEOUT; i++) {
153                 /* Sanity check on the resp->resp_len */
154                 rte_rmb();
155                 if (resp->resp_len && resp->resp_len <=
156                                 bp->max_resp_len) {
157                         /* Last byte of resp contains the valid key */
158                         valid = (uint8_t *)resp + resp->resp_len - 1;
159                         if (*valid == HWRM_RESP_VALID_KEY)
160                                 break;
161                 }
162                 rte_delay_us(600);
163         }
164
165         if (i >= HWRM_CMD_TIMEOUT) {
166                 RTE_LOG(ERR, PMD, "Error sending msg 0x%04x\n",
167                         req->req_type);
168                 goto err_ret;
169         }
170         return 0;
171
172 err_ret:
173         return -1;
174 }
175
176 static int bnxt_hwrm_send_message(struct bnxt *bp, void *msg, uint32_t msg_len)
177 {
178         int rc;
179
180         rte_spinlock_lock(&bp->hwrm_lock);
181         rc = bnxt_hwrm_send_message_locked(bp, msg, msg_len);
182         rte_spinlock_unlock(&bp->hwrm_lock);
183         return rc;
184 }
185
186 #define HWRM_PREP(req, type, cr, resp) \
187         memset(bp->hwrm_cmd_resp_addr, 0, bp->max_resp_len); \
188         req.req_type = rte_cpu_to_le_16(HWRM_##type); \
189         req.cmpl_ring = rte_cpu_to_le_16(cr); \
190         req.seq_id = rte_cpu_to_le_16(bp->hwrm_cmd_seq++); \
191         req.target_id = rte_cpu_to_le_16(0xffff); \
192         req.resp_addr = rte_cpu_to_le_64(bp->hwrm_cmd_resp_dma_addr)
193
194 #define HWRM_CHECK_RESULT \
195         { \
196                 if (rc) { \
197                         RTE_LOG(ERR, PMD, "%s failed rc:%d\n", \
198                                 __func__, rc); \
199                         return rc; \
200                 } \
201                 if (resp->error_code) { \
202                         rc = rte_le_to_cpu_16(resp->error_code); \
203                         if (resp->resp_len >= 16) { \
204                                 struct hwrm_err_output *tmp_hwrm_err_op = \
205                                                         (void *)resp; \
206                                 RTE_LOG(ERR, PMD, \
207                                         "%s error %d:%d:%08x:%04x\n", \
208                                         __func__, \
209                                         rc, tmp_hwrm_err_op->cmd_err, \
210                                         rte_le_to_cpu_32(\
211                                                 tmp_hwrm_err_op->opaque_0), \
212                                         rte_le_to_cpu_16(\
213                                                 tmp_hwrm_err_op->opaque_1)); \
214                         } \
215                         else { \
216                                 RTE_LOG(ERR, PMD, \
217                                         "%s error %d\n", __func__, rc); \
218                         } \
219                         return rc; \
220                 } \
221         }
222
223 int bnxt_hwrm_cfa_l2_clear_rx_mask(struct bnxt *bp, struct bnxt_vnic_info *vnic)
224 {
225         int rc = 0;
226         struct hwrm_cfa_l2_set_rx_mask_input req = {.req_type = 0 };
227         struct hwrm_cfa_l2_set_rx_mask_output *resp = bp->hwrm_cmd_resp_addr;
228
229         HWRM_PREP(req, CFA_L2_SET_RX_MASK, -1, resp);
230         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
231         req.mask = 0;
232
233         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
234
235         HWRM_CHECK_RESULT;
236
237         return rc;
238 }
239
240 int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp,
241                                  struct bnxt_vnic_info *vnic,
242                                  uint16_t vlan_count,
243                                  struct bnxt_vlan_table_entry *vlan_table)
244 {
245         int rc = 0;
246         struct hwrm_cfa_l2_set_rx_mask_input req = {.req_type = 0 };
247         struct hwrm_cfa_l2_set_rx_mask_output *resp = bp->hwrm_cmd_resp_addr;
248         uint32_t mask = 0;
249
250         HWRM_PREP(req, CFA_L2_SET_RX_MASK, -1, resp);
251         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
252
253         /* FIXME add multicast flag, when multicast adding options is supported
254          * by ethtool.
255          */
256         if (vnic->flags & BNXT_VNIC_INFO_BCAST)
257                 mask = HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_BCAST;
258         if (vnic->flags & BNXT_VNIC_INFO_UNTAGGED)
259                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLAN_NONVLAN;
260         if (vnic->flags & BNXT_VNIC_INFO_PROMISC)
261                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_PROMISCUOUS;
262         if (vnic->flags & BNXT_VNIC_INFO_ALLMULTI)
263                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ALL_MCAST;
264         if (vnic->flags & BNXT_VNIC_INFO_MCAST)
265                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_MCAST;
266         if (vnic->mc_addr_cnt) {
267                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_MCAST;
268                 req.num_mc_entries = rte_cpu_to_le_32(vnic->mc_addr_cnt);
269                 req.mc_tbl_addr = rte_cpu_to_le_64(vnic->mc_list_dma_addr);
270         }
271         if (vlan_count && vlan_table) {
272                 mask |= HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_VLANONLY;
273                 req.vlan_tag_tbl_addr = rte_cpu_to_le_16(
274                          rte_mem_virt2phy(vlan_table));
275                 req.num_vlan_tags = rte_cpu_to_le_32((uint32_t)vlan_count);
276         }
277         req.mask = rte_cpu_to_le_32(HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_BCAST |
278                                     mask);
279
280         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
281
282         HWRM_CHECK_RESULT;
283
284         return rc;
285 }
286
287 int bnxt_hwrm_clear_filter(struct bnxt *bp,
288                            struct bnxt_filter_info *filter)
289 {
290         int rc = 0;
291         struct hwrm_cfa_l2_filter_free_input req = {.req_type = 0 };
292         struct hwrm_cfa_l2_filter_free_output *resp = bp->hwrm_cmd_resp_addr;
293
294         HWRM_PREP(req, CFA_L2_FILTER_FREE, -1, resp);
295
296         req.l2_filter_id = rte_cpu_to_le_64(filter->fw_l2_filter_id);
297
298         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
299
300         HWRM_CHECK_RESULT;
301
302         filter->fw_l2_filter_id = -1;
303
304         return 0;
305 }
306
307 int bnxt_hwrm_set_filter(struct bnxt *bp,
308                          uint16_t dst_id,
309                          struct bnxt_filter_info *filter)
310 {
311         int rc = 0;
312         struct hwrm_cfa_l2_filter_alloc_input req = {.req_type = 0 };
313         struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
314         uint32_t enables = 0;
315
316         HWRM_PREP(req, CFA_L2_FILTER_ALLOC, -1, resp);
317
318         req.flags = rte_cpu_to_le_32(filter->flags);
319
320         enables = filter->enables |
321               HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_DST_ID;
322         req.dst_id = rte_cpu_to_le_16(dst_id);
323
324         if (enables &
325             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR)
326                 memcpy(req.l2_addr, filter->l2_addr,
327                        ETHER_ADDR_LEN);
328         if (enables &
329             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR_MASK)
330                 memcpy(req.l2_addr_mask, filter->l2_addr_mask,
331                        ETHER_ADDR_LEN);
332         if (enables &
333             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_OVLAN)
334                 req.l2_ovlan = filter->l2_ovlan;
335         if (enables &
336             HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_OVLAN_MASK)
337                 req.l2_ovlan_mask = filter->l2_ovlan_mask;
338         if (enables & HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_SRC_ID)
339                 req.src_id = rte_cpu_to_le_32(filter->src_id);
340         if (enables & HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_SRC_TYPE)
341                 req.src_type = filter->src_type;
342
343         req.enables = rte_cpu_to_le_32(enables);
344
345         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
346
347         HWRM_CHECK_RESULT;
348
349         filter->fw_l2_filter_id = rte_le_to_cpu_64(resp->l2_filter_id);
350
351         return rc;
352 }
353
354 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
355 {
356         int rc = 0;
357         struct hwrm_func_qcaps_input req = {.req_type = 0 };
358         struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
359         uint16_t new_max_vfs;
360         int i;
361
362         HWRM_PREP(req, FUNC_QCAPS, -1, resp);
363
364         req.fid = rte_cpu_to_le_16(0xffff);
365
366         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
367
368         HWRM_CHECK_RESULT;
369
370         bp->max_ring_grps = rte_le_to_cpu_32(resp->max_hw_ring_grps);
371         if (BNXT_PF(bp)) {
372                 bp->pf.port_id = resp->port_id;
373                 bp->pf.first_vf_id = rte_le_to_cpu_16(resp->first_vf_id);
374                 new_max_vfs = bp->pdev->max_vfs;
375                 if (new_max_vfs != bp->pf.max_vfs) {
376                         if (bp->pf.vf_info)
377                                 rte_free(bp->pf.vf_info);
378                         bp->pf.vf_info = rte_malloc("bnxt_vf_info",
379                             sizeof(bp->pf.vf_info[0]) * new_max_vfs, 0);
380                         bp->pf.max_vfs = new_max_vfs;
381                         for (i = 0; i < new_max_vfs; i++) {
382                                 bp->pf.vf_info[i].fid = bp->pf.first_vf_id + i;
383                                 bp->pf.vf_info[i].vlan_table =
384                                         rte_zmalloc("VF VLAN table",
385                                                     getpagesize(),
386                                                     getpagesize());
387                                 if (bp->pf.vf_info[i].vlan_table == NULL)
388                                         RTE_LOG(ERR, PMD,
389                                         "Fail to alloc VLAN table for VF %d\n",
390                                         i);
391                                 else
392                                         rte_mem_lock_page(
393                                                 bp->pf.vf_info[i].vlan_table);
394                                 STAILQ_INIT(&bp->pf.vf_info[i].filter);
395                         }
396                 }
397         }
398
399         bp->fw_fid = rte_le_to_cpu_32(resp->fid);
400         memcpy(bp->dflt_mac_addr, &resp->mac_address, ETHER_ADDR_LEN);
401         bp->max_rsscos_ctx = rte_le_to_cpu_16(resp->max_rsscos_ctx);
402         bp->max_cp_rings = rte_le_to_cpu_16(resp->max_cmpl_rings);
403         bp->max_tx_rings = rte_le_to_cpu_16(resp->max_tx_rings);
404         bp->max_rx_rings = rte_le_to_cpu_16(resp->max_rx_rings);
405         bp->max_l2_ctx = rte_le_to_cpu_16(resp->max_l2_ctxs);
406         /* TODO: For now, do not support VMDq/RFS on VFs. */
407         if (BNXT_PF(bp)) {
408                 if (bp->pf.max_vfs)
409                         bp->max_vnics = 1;
410                 else
411                         bp->max_vnics = rte_le_to_cpu_16(resp->max_vnics);
412         } else {
413                 bp->max_vnics = 1;
414         }
415         bp->max_stat_ctx = rte_le_to_cpu_16(resp->max_stat_ctx);
416         if (BNXT_PF(bp))
417                 bp->pf.total_vnics = rte_le_to_cpu_16(resp->max_vnics);
418
419         return rc;
420 }
421
422 int bnxt_hwrm_func_reset(struct bnxt *bp)
423 {
424         int rc = 0;
425         struct hwrm_func_reset_input req = {.req_type = 0 };
426         struct hwrm_func_reset_output *resp = bp->hwrm_cmd_resp_addr;
427
428         HWRM_PREP(req, FUNC_RESET, -1, resp);
429
430         req.enables = rte_cpu_to_le_32(0);
431
432         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
433
434         HWRM_CHECK_RESULT;
435
436         return rc;
437 }
438
439 int bnxt_hwrm_func_driver_register(struct bnxt *bp)
440 {
441         int rc;
442         struct hwrm_func_drv_rgtr_input req = {.req_type = 0 };
443         struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
444
445         if (bp->flags & BNXT_FLAG_REGISTERED)
446                 return 0;
447
448         HWRM_PREP(req, FUNC_DRV_RGTR, -1, resp);
449         req.enables = rte_cpu_to_le_32(HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_VER |
450                         HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_ASYNC_EVENT_FWD);
451         req.ver_maj = RTE_VER_YEAR;
452         req.ver_min = RTE_VER_MONTH;
453         req.ver_upd = RTE_VER_MINOR;
454
455         if (BNXT_PF(bp)) {
456                 req.enables |= rte_cpu_to_le_32(
457                         HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_VF_INPUT_FWD);
458                 memcpy(req.vf_req_fwd, bp->pf.vf_req_fwd,
459                        RTE_MIN(sizeof(req.vf_req_fwd),
460                                sizeof(bp->pf.vf_req_fwd)));
461         }
462
463         req.async_event_fwd[0] |= rte_cpu_to_le_32(0x1);   /* TODO: Use MACRO */
464         memset(req.async_event_fwd, 0xff, sizeof(req.async_event_fwd));
465
466         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
467
468         HWRM_CHECK_RESULT;
469
470         bp->flags |= BNXT_FLAG_REGISTERED;
471
472         return rc;
473 }
474
475 int bnxt_hwrm_ver_get(struct bnxt *bp)
476 {
477         int rc = 0;
478         struct hwrm_ver_get_input req = {.req_type = 0 };
479         struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
480         uint32_t my_version;
481         uint32_t fw_version;
482         uint16_t max_resp_len;
483         char type[RTE_MEMZONE_NAMESIZE];
484         uint32_t dev_caps_cfg;
485
486         bp->max_req_len = HWRM_MAX_REQ_LEN;
487         HWRM_PREP(req, VER_GET, -1, resp);
488
489         req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
490         req.hwrm_intf_min = HWRM_VERSION_MINOR;
491         req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
492
493         /*
494          * Hold the lock since we may be adjusting the response pointers.
495          */
496         rte_spinlock_lock(&bp->hwrm_lock);
497         rc = bnxt_hwrm_send_message_locked(bp, &req, sizeof(req));
498
499         HWRM_CHECK_RESULT;
500
501         RTE_LOG(INFO, PMD, "%d.%d.%d:%d.%d.%d\n",
502                 resp->hwrm_intf_maj, resp->hwrm_intf_min,
503                 resp->hwrm_intf_upd,
504                 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld);
505         bp->fw_ver = (resp->hwrm_fw_maj << 24) | (resp->hwrm_fw_min << 16) |
506                         (resp->hwrm_fw_bld << 8) | resp->hwrm_fw_rsvd;
507         RTE_LOG(INFO, PMD, "Driver HWRM version: %d.%d.%d\n",
508                 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, HWRM_VERSION_UPDATE);
509
510         my_version = HWRM_VERSION_MAJOR << 16;
511         my_version |= HWRM_VERSION_MINOR << 8;
512         my_version |= HWRM_VERSION_UPDATE;
513
514         fw_version = resp->hwrm_intf_maj << 16;
515         fw_version |= resp->hwrm_intf_min << 8;
516         fw_version |= resp->hwrm_intf_upd;
517
518         if (resp->hwrm_intf_maj != HWRM_VERSION_MAJOR) {
519                 RTE_LOG(ERR, PMD, "Unsupported firmware API version\n");
520                 rc = -EINVAL;
521                 goto error;
522         }
523
524         if (my_version != fw_version) {
525                 RTE_LOG(INFO, PMD, "BNXT Driver/HWRM API mismatch.\n");
526                 if (my_version < fw_version) {
527                         RTE_LOG(INFO, PMD,
528                                 "Firmware API version is newer than driver.\n");
529                         RTE_LOG(INFO, PMD,
530                                 "The driver may be missing features.\n");
531                 } else {
532                         RTE_LOG(INFO, PMD,
533                                 "Firmware API version is older than driver.\n");
534                         RTE_LOG(INFO, PMD,
535                                 "Not all driver features may be functional.\n");
536                 }
537         }
538
539         if (bp->max_req_len > resp->max_req_win_len) {
540                 RTE_LOG(ERR, PMD, "Unsupported request length\n");
541                 rc = -EINVAL;
542         }
543         bp->max_req_len = rte_le_to_cpu_16(resp->max_req_win_len);
544         max_resp_len = resp->max_resp_len;
545         dev_caps_cfg = rte_le_to_cpu_32(resp->dev_caps_cfg);
546
547         if (bp->max_resp_len != max_resp_len) {
548                 sprintf(type, "bnxt_hwrm_%04x:%02x:%02x:%02x",
549                         bp->pdev->addr.domain, bp->pdev->addr.bus,
550                         bp->pdev->addr.devid, bp->pdev->addr.function);
551
552                 rte_free(bp->hwrm_cmd_resp_addr);
553
554                 bp->hwrm_cmd_resp_addr = rte_malloc(type, max_resp_len, 0);
555                 if (bp->hwrm_cmd_resp_addr == NULL) {
556                         rc = -ENOMEM;
557                         goto error;
558                 }
559                 rte_mem_lock_page(bp->hwrm_cmd_resp_addr);
560                 bp->hwrm_cmd_resp_dma_addr =
561                         rte_mem_virt2phy(bp->hwrm_cmd_resp_addr);
562                 if (bp->hwrm_cmd_resp_dma_addr == 0) {
563                         RTE_LOG(ERR, PMD,
564                         "Unable to map response buffer to physical memory.\n");
565                         rc = -ENOMEM;
566                         goto error;
567                 }
568                 bp->max_resp_len = max_resp_len;
569         }
570
571         if ((dev_caps_cfg &
572                 HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
573             (dev_caps_cfg &
574              HWRM_VER_GET_OUTPUT_DEV_CAPS_CFG_SHORT_CMD_INPUTUIRED)) {
575                 RTE_LOG(DEBUG, PMD, "Short command supported\n");
576
577                 rte_free(bp->hwrm_short_cmd_req_addr);
578
579                 bp->hwrm_short_cmd_req_addr = rte_malloc(type,
580                                                         bp->max_req_len, 0);
581                 if (bp->hwrm_short_cmd_req_addr == NULL) {
582                         rc = -ENOMEM;
583                         goto error;
584                 }
585                 rte_mem_lock_page(bp->hwrm_short_cmd_req_addr);
586                 bp->hwrm_short_cmd_req_dma_addr =
587                         rte_mem_virt2phy(bp->hwrm_short_cmd_req_addr);
588                 if (bp->hwrm_short_cmd_req_dma_addr == 0) {
589                         rte_free(bp->hwrm_short_cmd_req_addr);
590                         RTE_LOG(ERR, PMD,
591                                 "Unable to map buffer to physical memory.\n");
592                         rc = -ENOMEM;
593                         goto error;
594                 }
595
596                 bp->flags |= BNXT_FLAG_SHORT_CMD;
597         }
598
599 error:
600         rte_spinlock_unlock(&bp->hwrm_lock);
601         return rc;
602 }
603
604 int bnxt_hwrm_func_driver_unregister(struct bnxt *bp, uint32_t flags)
605 {
606         int rc;
607         struct hwrm_func_drv_unrgtr_input req = {.req_type = 0 };
608         struct hwrm_func_drv_unrgtr_output *resp = bp->hwrm_cmd_resp_addr;
609
610         if (!(bp->flags & BNXT_FLAG_REGISTERED))
611                 return 0;
612
613         HWRM_PREP(req, FUNC_DRV_UNRGTR, -1, resp);
614         req.flags = flags;
615
616         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
617
618         HWRM_CHECK_RESULT;
619
620         bp->flags &= ~BNXT_FLAG_REGISTERED;
621
622         return rc;
623 }
624
625 static int bnxt_hwrm_port_phy_cfg(struct bnxt *bp, struct bnxt_link_info *conf)
626 {
627         int rc = 0;
628         struct hwrm_port_phy_cfg_input req = {0};
629         struct hwrm_port_phy_cfg_output *resp = bp->hwrm_cmd_resp_addr;
630         uint32_t enables = 0;
631
632         HWRM_PREP(req, PORT_PHY_CFG, -1, resp);
633
634         if (conf->link_up) {
635                 req.flags = rte_cpu_to_le_32(conf->phy_flags);
636                 req.force_link_speed = rte_cpu_to_le_16(conf->link_speed);
637                 /*
638                  * Note, ChiMP FW 20.2.1 and 20.2.2 return an error when we set
639                  * any auto mode, even "none".
640                  */
641                 if (!conf->link_speed) {
642                         req.auto_mode |= conf->auto_mode;
643                         enables = HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_MODE;
644                         req.auto_link_speed_mask = conf->auto_link_speed_mask;
645                         enables |=
646                            HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_LINK_SPEED_MASK;
647                         req.auto_link_speed = bp->link_info.auto_link_speed;
648                         enables |=
649                                 HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_LINK_SPEED;
650                 }
651                 req.auto_duplex = conf->duplex;
652                 enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_DUPLEX;
653                 req.auto_pause = conf->auto_pause;
654                 req.force_pause = conf->force_pause;
655                 /* Set force_pause if there is no auto or if there is a force */
656                 if (req.auto_pause && !req.force_pause)
657                         enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_PAUSE;
658                 else
659                         enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_FORCE_PAUSE;
660
661                 req.enables = rte_cpu_to_le_32(enables);
662         } else {
663                 req.flags =
664                 rte_cpu_to_le_32(HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE_LINK_DWN);
665                 RTE_LOG(INFO, PMD, "Force Link Down\n");
666         }
667
668         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
669
670         HWRM_CHECK_RESULT;
671
672         return rc;
673 }
674
675 static int bnxt_hwrm_port_phy_qcfg(struct bnxt *bp,
676                                    struct bnxt_link_info *link_info)
677 {
678         int rc = 0;
679         struct hwrm_port_phy_qcfg_input req = {0};
680         struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
681
682         HWRM_PREP(req, PORT_PHY_QCFG, -1, resp);
683
684         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
685
686         HWRM_CHECK_RESULT;
687
688         link_info->phy_link_status = resp->link;
689         if (link_info->phy_link_status == HWRM_PORT_PHY_QCFG_OUTPUT_LINK_LINK) {
690                 link_info->link_up = 1;
691                 link_info->link_speed = rte_le_to_cpu_16(resp->link_speed);
692         } else {
693                 link_info->link_up = 0;
694                 link_info->link_speed = 0;
695         }
696         link_info->duplex = resp->duplex;
697         link_info->pause = resp->pause;
698         link_info->auto_pause = resp->auto_pause;
699         link_info->force_pause = resp->force_pause;
700         link_info->auto_mode = resp->auto_mode;
701
702         link_info->support_speeds = rte_le_to_cpu_16(resp->support_speeds);
703         link_info->auto_link_speed = rte_le_to_cpu_16(resp->auto_link_speed);
704         link_info->preemphasis = rte_le_to_cpu_32(resp->preemphasis);
705         link_info->phy_ver[0] = resp->phy_maj;
706         link_info->phy_ver[1] = resp->phy_min;
707         link_info->phy_ver[2] = resp->phy_bld;
708
709         return rc;
710 }
711
712 int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
713 {
714         int rc = 0;
715         struct hwrm_queue_qportcfg_input req = {.req_type = 0 };
716         struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
717
718         HWRM_PREP(req, QUEUE_QPORTCFG, -1, resp);
719
720         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
721
722         HWRM_CHECK_RESULT;
723
724 #define GET_QUEUE_INFO(x) \
725         bp->cos_queue[x].id = resp->queue_id##x; \
726         bp->cos_queue[x].profile = resp->queue_id##x##_service_profile
727
728         GET_QUEUE_INFO(0);
729         GET_QUEUE_INFO(1);
730         GET_QUEUE_INFO(2);
731         GET_QUEUE_INFO(3);
732         GET_QUEUE_INFO(4);
733         GET_QUEUE_INFO(5);
734         GET_QUEUE_INFO(6);
735         GET_QUEUE_INFO(7);
736
737         return rc;
738 }
739
740 int bnxt_hwrm_ring_alloc(struct bnxt *bp,
741                          struct bnxt_ring *ring,
742                          uint32_t ring_type, uint32_t map_index,
743                          uint32_t stats_ctx_id, uint32_t cmpl_ring_id)
744 {
745         int rc = 0;
746         uint32_t enables = 0;
747         struct hwrm_ring_alloc_input req = {.req_type = 0 };
748         struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
749
750         HWRM_PREP(req, RING_ALLOC, -1, resp);
751
752         req.page_tbl_addr = rte_cpu_to_le_64(ring->bd_dma);
753         req.fbo = rte_cpu_to_le_32(0);
754         /* Association of ring index with doorbell index */
755         req.logical_id = rte_cpu_to_le_16(map_index);
756         req.length = rte_cpu_to_le_32(ring->ring_size);
757
758         switch (ring_type) {
759         case HWRM_RING_ALLOC_INPUT_RING_TYPE_TX:
760                 req.queue_id = bp->cos_queue[0].id;
761                 /* FALLTHROUGH */
762         case HWRM_RING_ALLOC_INPUT_RING_TYPE_RX:
763                 req.ring_type = ring_type;
764                 req.cmpl_ring_id = rte_cpu_to_le_16(cmpl_ring_id);
765                 req.stat_ctx_id = rte_cpu_to_le_16(stats_ctx_id);
766                 if (stats_ctx_id != INVALID_STATS_CTX_ID)
767                         enables |=
768                         HWRM_RING_ALLOC_INPUT_ENABLES_STAT_CTX_ID_VALID;
769                 break;
770         case HWRM_RING_ALLOC_INPUT_RING_TYPE_L2_CMPL:
771                 req.ring_type = ring_type;
772                 /*
773                  * TODO: Some HWRM versions crash with
774                  * HWRM_RING_ALLOC_INPUT_INT_MODE_POLL
775                  */
776                 req.int_mode = HWRM_RING_ALLOC_INPUT_INT_MODE_MSIX;
777                 break;
778         default:
779                 RTE_LOG(ERR, PMD, "hwrm alloc invalid ring type %d\n",
780                         ring_type);
781                 return -1;
782         }
783         req.enables = rte_cpu_to_le_32(enables);
784
785         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
786
787         if (rc || resp->error_code) {
788                 if (rc == 0 && resp->error_code)
789                         rc = rte_le_to_cpu_16(resp->error_code);
790                 switch (ring_type) {
791                 case HWRM_RING_FREE_INPUT_RING_TYPE_L2_CMPL:
792                         RTE_LOG(ERR, PMD,
793                                 "hwrm_ring_alloc cp failed. rc:%d\n", rc);
794                         return rc;
795                 case HWRM_RING_FREE_INPUT_RING_TYPE_RX:
796                         RTE_LOG(ERR, PMD,
797                                 "hwrm_ring_alloc rx failed. rc:%d\n", rc);
798                         return rc;
799                 case HWRM_RING_FREE_INPUT_RING_TYPE_TX:
800                         RTE_LOG(ERR, PMD,
801                                 "hwrm_ring_alloc tx failed. rc:%d\n", rc);
802                         return rc;
803                 default:
804                         RTE_LOG(ERR, PMD, "Invalid ring. rc:%d\n", rc);
805                         return rc;
806                 }
807         }
808
809         ring->fw_ring_id = rte_le_to_cpu_16(resp->ring_id);
810         return rc;
811 }
812
813 int bnxt_hwrm_ring_free(struct bnxt *bp,
814                         struct bnxt_ring *ring, uint32_t ring_type)
815 {
816         int rc;
817         struct hwrm_ring_free_input req = {.req_type = 0 };
818         struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
819
820         HWRM_PREP(req, RING_FREE, -1, resp);
821
822         req.ring_type = ring_type;
823         req.ring_id = rte_cpu_to_le_16(ring->fw_ring_id);
824
825         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
826
827         if (rc || resp->error_code) {
828                 if (rc == 0 && resp->error_code)
829                         rc = rte_le_to_cpu_16(resp->error_code);
830
831                 switch (ring_type) {
832                 case HWRM_RING_FREE_INPUT_RING_TYPE_L2_CMPL:
833                         RTE_LOG(ERR, PMD, "hwrm_ring_free cp failed. rc:%d\n",
834                                 rc);
835                         return rc;
836                 case HWRM_RING_FREE_INPUT_RING_TYPE_RX:
837                         RTE_LOG(ERR, PMD, "hwrm_ring_free rx failed. rc:%d\n",
838                                 rc);
839                         return rc;
840                 case HWRM_RING_FREE_INPUT_RING_TYPE_TX:
841                         RTE_LOG(ERR, PMD, "hwrm_ring_free tx failed. rc:%d\n",
842                                 rc);
843                         return rc;
844                 default:
845                         RTE_LOG(ERR, PMD, "Invalid ring, rc:%d\n", rc);
846                         return rc;
847                 }
848         }
849         return 0;
850 }
851
852 int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp, unsigned int idx)
853 {
854         int rc = 0;
855         struct hwrm_ring_grp_alloc_input req = {.req_type = 0 };
856         struct hwrm_ring_grp_alloc_output *resp = bp->hwrm_cmd_resp_addr;
857
858         HWRM_PREP(req, RING_GRP_ALLOC, -1, resp);
859
860         req.cr = rte_cpu_to_le_16(bp->grp_info[idx].cp_fw_ring_id);
861         req.rr = rte_cpu_to_le_16(bp->grp_info[idx].rx_fw_ring_id);
862         req.ar = rte_cpu_to_le_16(bp->grp_info[idx].ag_fw_ring_id);
863         req.sc = rte_cpu_to_le_16(bp->grp_info[idx].fw_stats_ctx);
864
865         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
866
867         HWRM_CHECK_RESULT;
868
869         bp->grp_info[idx].fw_grp_id =
870             rte_le_to_cpu_16(resp->ring_group_id);
871
872         return rc;
873 }
874
875 int bnxt_hwrm_ring_grp_free(struct bnxt *bp, unsigned int idx)
876 {
877         int rc;
878         struct hwrm_ring_grp_free_input req = {.req_type = 0 };
879         struct hwrm_ring_grp_free_output *resp = bp->hwrm_cmd_resp_addr;
880
881         HWRM_PREP(req, RING_GRP_FREE, -1, resp);
882
883         req.ring_group_id = rte_cpu_to_le_16(bp->grp_info[idx].fw_grp_id);
884
885         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
886
887         HWRM_CHECK_RESULT;
888
889         bp->grp_info[idx].fw_grp_id = INVALID_HW_RING_ID;
890         return rc;
891 }
892
893 int bnxt_hwrm_stat_clear(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
894 {
895         int rc = 0;
896         struct hwrm_stat_ctx_clr_stats_input req = {.req_type = 0 };
897         struct hwrm_stat_ctx_clr_stats_output *resp = bp->hwrm_cmd_resp_addr;
898
899         if (cpr->hw_stats_ctx_id == (uint32_t)HWRM_NA_SIGNATURE)
900                 return rc;
901
902         HWRM_PREP(req, STAT_CTX_CLR_STATS, -1, resp);
903
904         req.stat_ctx_id = rte_cpu_to_le_16(cpr->hw_stats_ctx_id);
905
906         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
907
908         HWRM_CHECK_RESULT;
909
910         return rc;
911 }
912
913 int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
914                                 unsigned int idx __rte_unused)
915 {
916         int rc;
917         struct hwrm_stat_ctx_alloc_input req = {.req_type = 0 };
918         struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
919
920         HWRM_PREP(req, STAT_CTX_ALLOC, -1, resp);
921
922         req.update_period_ms = rte_cpu_to_le_32(0);
923
924         req.stats_dma_addr =
925             rte_cpu_to_le_64(cpr->hw_stats_map);
926
927         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
928
929         HWRM_CHECK_RESULT;
930
931         cpr->hw_stats_ctx_id = rte_le_to_cpu_16(resp->stat_ctx_id);
932
933         return rc;
934 }
935
936 int bnxt_hwrm_stat_ctx_free(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
937                                 unsigned int idx __rte_unused)
938 {
939         int rc;
940         struct hwrm_stat_ctx_free_input req = {.req_type = 0 };
941         struct hwrm_stat_ctx_free_output *resp = bp->hwrm_cmd_resp_addr;
942
943         HWRM_PREP(req, STAT_CTX_FREE, -1, resp);
944
945         req.stat_ctx_id = rte_cpu_to_le_16(cpr->hw_stats_ctx_id);
946
947         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
948
949         HWRM_CHECK_RESULT;
950
951         return rc;
952 }
953
954 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic)
955 {
956         int rc = 0, i, j;
957         struct hwrm_vnic_alloc_input req = { 0 };
958         struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
959
960         /* map ring groups to this vnic */
961         RTE_LOG(DEBUG, PMD, "Alloc VNIC. Start %x, End %x\n",
962                 vnic->start_grp_id, vnic->end_grp_id);
963         for (i = vnic->start_grp_id, j = 0; i <= vnic->end_grp_id; i++, j++)
964                 vnic->fw_grp_ids[j] = bp->grp_info[i].fw_grp_id;
965         vnic->dflt_ring_grp = bp->grp_info[vnic->start_grp_id].fw_grp_id;
966         vnic->rss_rule = (uint16_t)HWRM_NA_SIGNATURE;
967         vnic->cos_rule = (uint16_t)HWRM_NA_SIGNATURE;
968         vnic->lb_rule = (uint16_t)HWRM_NA_SIGNATURE;
969         vnic->mru = bp->eth_dev->data->mtu + ETHER_HDR_LEN +
970                                 ETHER_CRC_LEN + VLAN_TAG_SIZE;
971         HWRM_PREP(req, VNIC_ALLOC, -1, resp);
972
973         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
974
975         HWRM_CHECK_RESULT;
976
977         vnic->fw_vnic_id = rte_le_to_cpu_16(resp->vnic_id);
978         return rc;
979 }
980
981 static int bnxt_hwrm_vnic_plcmodes_qcfg(struct bnxt *bp,
982                                         struct bnxt_vnic_info *vnic,
983                                         struct bnxt_plcmodes_cfg *pmode)
984 {
985         int rc = 0;
986         struct hwrm_vnic_plcmodes_qcfg_input req = {.req_type = 0 };
987         struct hwrm_vnic_plcmodes_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
988
989         HWRM_PREP(req, VNIC_PLCMODES_QCFG, -1, resp);
990
991         req.vnic_id = rte_cpu_to_le_32(vnic->fw_vnic_id);
992
993         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
994
995         HWRM_CHECK_RESULT;
996
997         pmode->flags = rte_le_to_cpu_32(resp->flags);
998         /* dflt_vnic bit doesn't exist in the _cfg command */
999         pmode->flags &= ~(HWRM_VNIC_PLCMODES_QCFG_OUTPUT_FLAGS_DFLT_VNIC);
1000         pmode->jumbo_thresh = rte_le_to_cpu_16(resp->jumbo_thresh);
1001         pmode->hds_offset = rte_le_to_cpu_16(resp->hds_offset);
1002         pmode->hds_threshold = rte_le_to_cpu_16(resp->hds_threshold);
1003
1004         return rc;
1005 }
1006
1007 static int bnxt_hwrm_vnic_plcmodes_cfg(struct bnxt *bp,
1008                                        struct bnxt_vnic_info *vnic,
1009                                        struct bnxt_plcmodes_cfg *pmode)
1010 {
1011         int rc = 0;
1012         struct hwrm_vnic_plcmodes_cfg_input req = {.req_type = 0 };
1013         struct hwrm_vnic_plcmodes_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1014
1015         HWRM_PREP(req, VNIC_PLCMODES_CFG, -1, resp);
1016
1017         req.vnic_id = rte_cpu_to_le_32(vnic->fw_vnic_id);
1018         req.flags = rte_cpu_to_le_32(pmode->flags);
1019         req.jumbo_thresh = rte_cpu_to_le_16(pmode->jumbo_thresh);
1020         req.hds_offset = rte_cpu_to_le_16(pmode->hds_offset);
1021         req.hds_threshold = rte_cpu_to_le_16(pmode->hds_threshold);
1022         req.enables = rte_cpu_to_le_32(
1023             HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_HDS_THRESHOLD_VALID |
1024             HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_HDS_OFFSET_VALID |
1025             HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_JUMBO_THRESH_VALID
1026         );
1027
1028         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1029
1030         HWRM_CHECK_RESULT;
1031
1032         return rc;
1033 }
1034
1035 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1036 {
1037         int rc = 0;
1038         struct hwrm_vnic_cfg_input req = {.req_type = 0 };
1039         struct hwrm_vnic_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1040         uint32_t ctx_enable_flag = HWRM_VNIC_CFG_INPUT_ENABLES_RSS_RULE;
1041         struct bnxt_plcmodes_cfg pmodes;
1042
1043         rc = bnxt_hwrm_vnic_plcmodes_qcfg(bp, vnic, &pmodes);
1044         if (rc)
1045                 return rc;
1046
1047         HWRM_PREP(req, VNIC_CFG, -1, resp);
1048
1049         /* Only RSS support for now TBD: COS & LB */
1050         req.enables =
1051             rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_ENABLES_DFLT_RING_GRP |
1052                              HWRM_VNIC_CFG_INPUT_ENABLES_MRU);
1053         if (vnic->lb_rule != 0xffff)
1054                 ctx_enable_flag = HWRM_VNIC_CFG_INPUT_ENABLES_LB_RULE;
1055         if (vnic->cos_rule != 0xffff)
1056                 ctx_enable_flag = HWRM_VNIC_CFG_INPUT_ENABLES_COS_RULE;
1057         if (vnic->rss_rule != 0xffff)
1058                 ctx_enable_flag = HWRM_VNIC_CFG_INPUT_ENABLES_RSS_RULE;
1059         req.enables |= rte_cpu_to_le_32(ctx_enable_flag);
1060         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1061         req.dflt_ring_grp = rte_cpu_to_le_16(vnic->dflt_ring_grp);
1062         req.rss_rule = rte_cpu_to_le_16(vnic->rss_rule);
1063         req.cos_rule = rte_cpu_to_le_16(vnic->cos_rule);
1064         req.lb_rule = rte_cpu_to_le_16(vnic->lb_rule);
1065         req.mru = rte_cpu_to_le_16(vnic->mru);
1066         if (vnic->func_default)
1067                 req.flags |=
1068                     rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_FLAGS_DEFAULT);
1069         if (vnic->vlan_strip)
1070                 req.flags |=
1071                     rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_FLAGS_VLAN_STRIP_MODE);
1072         if (vnic->bd_stall)
1073                 req.flags |=
1074                     rte_cpu_to_le_32(HWRM_VNIC_CFG_INPUT_FLAGS_BD_STALL_MODE);
1075         if (vnic->roce_dual)
1076                 req.flags |= rte_cpu_to_le_32(
1077                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_ROCE_DUAL_VNIC_MODE);
1078         if (vnic->roce_only)
1079                 req.flags |= rte_cpu_to_le_32(
1080                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_ROCE_ONLY_VNIC_MODE);
1081         if (vnic->rss_dflt_cr)
1082                 req.flags |= rte_cpu_to_le_32(
1083                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_RSS_DFLT_CR_MODE);
1084
1085         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1086
1087         HWRM_CHECK_RESULT;
1088
1089         rc = bnxt_hwrm_vnic_plcmodes_cfg(bp, vnic, &pmodes);
1090
1091         return rc;
1092 }
1093
1094 int bnxt_hwrm_vnic_qcfg(struct bnxt *bp, struct bnxt_vnic_info *vnic,
1095                 int16_t fw_vf_id)
1096 {
1097         int rc = 0;
1098         struct hwrm_vnic_qcfg_input req = {.req_type = 0 };
1099         struct hwrm_vnic_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
1100
1101         HWRM_PREP(req, VNIC_QCFG, -1, resp);
1102
1103         req.enables =
1104                 rte_cpu_to_le_32(HWRM_VNIC_QCFG_INPUT_ENABLES_VF_ID_VALID);
1105         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1106         req.vf_id = rte_cpu_to_le_16(fw_vf_id);
1107
1108         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1109
1110         HWRM_CHECK_RESULT;
1111
1112         vnic->dflt_ring_grp = rte_le_to_cpu_16(resp->dflt_ring_grp);
1113         vnic->rss_rule = rte_le_to_cpu_16(resp->rss_rule);
1114         vnic->cos_rule = rte_le_to_cpu_16(resp->cos_rule);
1115         vnic->lb_rule = rte_le_to_cpu_16(resp->lb_rule);
1116         vnic->mru = rte_le_to_cpu_16(resp->mru);
1117         vnic->func_default = rte_le_to_cpu_32(
1118                         resp->flags) & HWRM_VNIC_QCFG_OUTPUT_FLAGS_DEFAULT;
1119         vnic->vlan_strip = rte_le_to_cpu_32(resp->flags) &
1120                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_VLAN_STRIP_MODE;
1121         vnic->bd_stall = rte_le_to_cpu_32(resp->flags) &
1122                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_BD_STALL_MODE;
1123         vnic->roce_dual = rte_le_to_cpu_32(resp->flags) &
1124                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_ROCE_DUAL_VNIC_MODE;
1125         vnic->roce_only = rte_le_to_cpu_32(resp->flags) &
1126                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_ROCE_ONLY_VNIC_MODE;
1127         vnic->rss_dflt_cr = rte_le_to_cpu_32(resp->flags) &
1128                         HWRM_VNIC_QCFG_OUTPUT_FLAGS_RSS_DFLT_CR_MODE;
1129
1130         return rc;
1131 }
1132
1133 int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1134 {
1135         int rc = 0;
1136         struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {.req_type = 0 };
1137         struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
1138                                                 bp->hwrm_cmd_resp_addr;
1139
1140         HWRM_PREP(req, VNIC_RSS_COS_LB_CTX_ALLOC, -1, resp);
1141
1142         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1143
1144         HWRM_CHECK_RESULT;
1145
1146         vnic->rss_rule = rte_le_to_cpu_16(resp->rss_cos_lb_ctx_id);
1147
1148         return rc;
1149 }
1150
1151 int bnxt_hwrm_vnic_ctx_free(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1152 {
1153         int rc = 0;
1154         struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {.req_type = 0 };
1155         struct hwrm_vnic_rss_cos_lb_ctx_free_output *resp =
1156                                                 bp->hwrm_cmd_resp_addr;
1157
1158         HWRM_PREP(req, VNIC_RSS_COS_LB_CTX_FREE, -1, resp);
1159
1160         req.rss_cos_lb_ctx_id = rte_cpu_to_le_16(vnic->rss_rule);
1161
1162         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1163
1164         HWRM_CHECK_RESULT;
1165
1166         vnic->rss_rule = INVALID_HW_RING_ID;
1167
1168         return rc;
1169 }
1170
1171 int bnxt_hwrm_vnic_free(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1172 {
1173         int rc = 0;
1174         struct hwrm_vnic_free_input req = {.req_type = 0 };
1175         struct hwrm_vnic_free_output *resp = bp->hwrm_cmd_resp_addr;
1176
1177         if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
1178                 return rc;
1179
1180         HWRM_PREP(req, VNIC_FREE, -1, resp);
1181
1182         req.vnic_id = rte_cpu_to_le_16(vnic->fw_vnic_id);
1183
1184         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1185
1186         HWRM_CHECK_RESULT;
1187
1188         vnic->fw_vnic_id = INVALID_HW_RING_ID;
1189         return rc;
1190 }
1191
1192 int bnxt_hwrm_vnic_rss_cfg(struct bnxt *bp,
1193                            struct bnxt_vnic_info *vnic)
1194 {
1195         int rc = 0;
1196         struct hwrm_vnic_rss_cfg_input req = {.req_type = 0 };
1197         struct hwrm_vnic_rss_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1198
1199         HWRM_PREP(req, VNIC_RSS_CFG, -1, resp);
1200
1201         req.hash_type = rte_cpu_to_le_32(vnic->hash_type);
1202
1203         req.ring_grp_tbl_addr =
1204             rte_cpu_to_le_64(vnic->rss_table_dma_addr);
1205         req.hash_key_tbl_addr =
1206             rte_cpu_to_le_64(vnic->rss_hash_key_dma_addr);
1207         req.rss_ctx_idx = rte_cpu_to_le_16(vnic->rss_rule);
1208
1209         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1210
1211         HWRM_CHECK_RESULT;
1212
1213         return rc;
1214 }
1215
1216 int bnxt_hwrm_vnic_plcmode_cfg(struct bnxt *bp,
1217                         struct bnxt_vnic_info *vnic)
1218 {
1219         int rc = 0;
1220         struct hwrm_vnic_plcmodes_cfg_input req = {.req_type = 0 };
1221         struct hwrm_vnic_plcmodes_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1222         uint16_t size;
1223
1224         HWRM_PREP(req, VNIC_PLCMODES_CFG, -1, resp);
1225
1226         req.flags = rte_cpu_to_le_32(
1227                         HWRM_VNIC_PLCMODES_CFG_INPUT_FLAGS_JUMBO_PLACEMENT);
1228
1229         req.enables = rte_cpu_to_le_32(
1230                 HWRM_VNIC_PLCMODES_CFG_INPUT_ENABLES_JUMBO_THRESH_VALID);
1231
1232         size = rte_pktmbuf_data_room_size(bp->rx_queues[0]->mb_pool);
1233         size -= RTE_PKTMBUF_HEADROOM;
1234
1235         req.jumbo_thresh = rte_cpu_to_le_16(size);
1236         req.vnic_id = rte_cpu_to_le_32(vnic->fw_vnic_id);
1237
1238         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1239
1240         HWRM_CHECK_RESULT;
1241
1242         return rc;
1243 }
1244
1245 int bnxt_hwrm_vnic_tpa_cfg(struct bnxt *bp,
1246                         struct bnxt_vnic_info *vnic, bool enable)
1247 {
1248         int rc = 0;
1249         struct hwrm_vnic_tpa_cfg_input req = {.req_type = 0 };
1250         struct hwrm_vnic_tpa_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1251
1252         HWRM_PREP(req, VNIC_TPA_CFG, -1, resp);
1253
1254         if (enable) {
1255                 req.enables = rte_cpu_to_le_32(
1256                                 HWRM_VNIC_TPA_CFG_INPUT_ENABLES_MAX_AGG_SEGS |
1257                                 HWRM_VNIC_TPA_CFG_INPUT_ENABLES_MAX_AGGS |
1258                                 HWRM_VNIC_TPA_CFG_INPUT_ENABLES_MIN_AGG_LEN);
1259                 req.flags = rte_cpu_to_le_32(
1260                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_TPA |
1261                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_ENCAP_TPA |
1262                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_RSC_WND_UPDATE |
1263                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_GRO |
1264                                 HWRM_VNIC_TPA_CFG_INPUT_FLAGS_AGG_WITH_ECN |
1265                         HWRM_VNIC_TPA_CFG_INPUT_FLAGS_AGG_WITH_SAME_GRE_SEQ);
1266                 req.vnic_id = rte_cpu_to_le_32(vnic->fw_vnic_id);
1267                 req.max_agg_segs = rte_cpu_to_le_16(5);
1268                 req.max_aggs =
1269                         rte_cpu_to_le_16(HWRM_VNIC_TPA_CFG_INPUT_MAX_AGGS_MAX);
1270                 req.min_agg_len = rte_cpu_to_le_32(512);
1271         }
1272
1273         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1274
1275         HWRM_CHECK_RESULT;
1276
1277         return rc;
1278 }
1279
1280 int bnxt_hwrm_func_vf_mac(struct bnxt *bp, uint16_t vf, const uint8_t *mac_addr)
1281 {
1282         struct hwrm_func_cfg_input req = {0};
1283         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
1284         int rc;
1285
1286         req.flags = rte_cpu_to_le_32(bp->pf.vf_info[vf].func_cfg_flags);
1287         req.enables = rte_cpu_to_le_32(
1288                         HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
1289         memcpy(req.dflt_mac_addr, mac_addr, sizeof(req.dflt_mac_addr));
1290         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
1291
1292         HWRM_PREP(req, FUNC_CFG, -1, resp);
1293
1294         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1295         HWRM_CHECK_RESULT;
1296
1297         bp->pf.vf_info[vf].random_mac = false;
1298
1299         return rc;
1300 }
1301
1302 int bnxt_hwrm_func_qstats_tx_drop(struct bnxt *bp, uint16_t fid,
1303                                   uint64_t *dropped)
1304 {
1305         int rc = 0;
1306         struct hwrm_func_qstats_input req = {.req_type = 0};
1307         struct hwrm_func_qstats_output *resp = bp->hwrm_cmd_resp_addr;
1308
1309         HWRM_PREP(req, FUNC_QSTATS, -1, resp);
1310
1311         req.fid = rte_cpu_to_le_16(fid);
1312
1313         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1314
1315         HWRM_CHECK_RESULT;
1316
1317         if (dropped)
1318                 *dropped = rte_le_to_cpu_64(resp->tx_drop_pkts);
1319
1320         return rc;
1321 }
1322
1323 int bnxt_hwrm_func_qstats(struct bnxt *bp, uint16_t fid,
1324                           struct rte_eth_stats *stats)
1325 {
1326         int rc = 0;
1327         struct hwrm_func_qstats_input req = {.req_type = 0};
1328         struct hwrm_func_qstats_output *resp = bp->hwrm_cmd_resp_addr;
1329
1330         HWRM_PREP(req, FUNC_QSTATS, -1, resp);
1331
1332         req.fid = rte_cpu_to_le_16(fid);
1333
1334         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1335
1336         HWRM_CHECK_RESULT;
1337
1338         stats->ipackets = rte_le_to_cpu_64(resp->rx_ucast_pkts);
1339         stats->ipackets += rte_le_to_cpu_64(resp->rx_mcast_pkts);
1340         stats->ipackets += rte_le_to_cpu_64(resp->rx_bcast_pkts);
1341         stats->ibytes = rte_le_to_cpu_64(resp->rx_ucast_bytes);
1342         stats->ibytes += rte_le_to_cpu_64(resp->rx_mcast_bytes);
1343         stats->ibytes += rte_le_to_cpu_64(resp->rx_bcast_bytes);
1344
1345         stats->opackets = rte_le_to_cpu_64(resp->tx_ucast_pkts);
1346         stats->opackets += rte_le_to_cpu_64(resp->tx_mcast_pkts);
1347         stats->opackets += rte_le_to_cpu_64(resp->tx_bcast_pkts);
1348         stats->obytes = rte_le_to_cpu_64(resp->tx_ucast_bytes);
1349         stats->obytes += rte_le_to_cpu_64(resp->tx_mcast_bytes);
1350         stats->obytes += rte_le_to_cpu_64(resp->tx_bcast_bytes);
1351
1352         stats->ierrors = rte_le_to_cpu_64(resp->rx_err_pkts);
1353         stats->oerrors = rte_le_to_cpu_64(resp->tx_err_pkts);
1354
1355         stats->imissed = rte_le_to_cpu_64(resp->rx_drop_pkts);
1356
1357         return rc;
1358 }
1359
1360 int bnxt_hwrm_func_clr_stats(struct bnxt *bp, uint16_t fid)
1361 {
1362         int rc = 0;
1363         struct hwrm_func_clr_stats_input req = {.req_type = 0};
1364         struct hwrm_func_clr_stats_output *resp = bp->hwrm_cmd_resp_addr;
1365
1366         HWRM_PREP(req, FUNC_CLR_STATS, -1, resp);
1367
1368         req.fid = rte_cpu_to_le_16(fid);
1369
1370         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1371
1372         HWRM_CHECK_RESULT;
1373
1374         return rc;
1375 }
1376
1377 /*
1378  * HWRM utility functions
1379  */
1380
1381 int bnxt_clear_all_hwrm_stat_ctxs(struct bnxt *bp)
1382 {
1383         unsigned int i;
1384         int rc = 0;
1385
1386         for (i = 0; i < bp->rx_cp_nr_rings + bp->tx_cp_nr_rings; i++) {
1387                 struct bnxt_tx_queue *txq;
1388                 struct bnxt_rx_queue *rxq;
1389                 struct bnxt_cp_ring_info *cpr;
1390
1391                 if (i >= bp->rx_cp_nr_rings) {
1392                         txq = bp->tx_queues[i - bp->rx_cp_nr_rings];
1393                         cpr = txq->cp_ring;
1394                 } else {
1395                         rxq = bp->rx_queues[i];
1396                         cpr = rxq->cp_ring;
1397                 }
1398
1399                 rc = bnxt_hwrm_stat_clear(bp, cpr);
1400                 if (rc)
1401                         return rc;
1402         }
1403         return 0;
1404 }
1405
1406 int bnxt_free_all_hwrm_stat_ctxs(struct bnxt *bp)
1407 {
1408         int rc;
1409         unsigned int i;
1410         struct bnxt_cp_ring_info *cpr;
1411
1412         for (i = 0; i < bp->rx_cp_nr_rings + bp->tx_cp_nr_rings; i++) {
1413
1414                 if (i >= bp->rx_cp_nr_rings)
1415                         cpr = bp->tx_queues[i - bp->rx_cp_nr_rings]->cp_ring;
1416                 else
1417                         cpr = bp->rx_queues[i]->cp_ring;
1418                 if (cpr->hw_stats_ctx_id != HWRM_NA_SIGNATURE) {
1419                         rc = bnxt_hwrm_stat_ctx_free(bp, cpr, i);
1420                         cpr->hw_stats_ctx_id = HWRM_NA_SIGNATURE;
1421                         /*
1422                          * TODO. Need a better way to reset grp_info.stats_ctx
1423                          * for Rx rings only. stats_ctx is not saved for Tx
1424                          * in grp_info.
1425                          */
1426                         bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
1427                         if (rc)
1428                                 return rc;
1429                 }
1430         }
1431         return 0;
1432 }
1433
1434 int bnxt_alloc_all_hwrm_stat_ctxs(struct bnxt *bp)
1435 {
1436         unsigned int i;
1437         int rc = 0;
1438
1439         for (i = 0; i < bp->rx_cp_nr_rings + bp->tx_cp_nr_rings; i++) {
1440                 struct bnxt_tx_queue *txq;
1441                 struct bnxt_rx_queue *rxq;
1442                 struct bnxt_cp_ring_info *cpr;
1443
1444                 if (i >= bp->rx_cp_nr_rings) {
1445                         txq = bp->tx_queues[i - bp->rx_cp_nr_rings];
1446                         cpr = txq->cp_ring;
1447                 } else {
1448                         rxq = bp->rx_queues[i];
1449                         cpr = rxq->cp_ring;
1450                 }
1451
1452                 rc = bnxt_hwrm_stat_ctx_alloc(bp, cpr, i);
1453
1454                 if (rc)
1455                         return rc;
1456         }
1457         return rc;
1458 }
1459
1460 int bnxt_free_all_hwrm_ring_grps(struct bnxt *bp)
1461 {
1462         uint16_t idx;
1463         uint32_t rc = 0;
1464
1465         for (idx = 0; idx < bp->rx_cp_nr_rings; idx++) {
1466
1467                 if (bp->grp_info[idx].fw_grp_id == INVALID_HW_RING_ID) {
1468                         RTE_LOG(ERR, PMD,
1469                                 "Attempt to free invalid ring group %d\n",
1470                                 idx);
1471                         continue;
1472                 }
1473
1474                 rc = bnxt_hwrm_ring_grp_free(bp, idx);
1475
1476                 if (rc)
1477                         return rc;
1478         }
1479         return rc;
1480 }
1481
1482 static void bnxt_free_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1483                                 unsigned int idx __rte_unused)
1484 {
1485         struct bnxt_ring *cp_ring = cpr->cp_ring_struct;
1486
1487         bnxt_hwrm_ring_free(bp, cp_ring,
1488                         HWRM_RING_FREE_INPUT_RING_TYPE_L2_CMPL);
1489         cp_ring->fw_ring_id = INVALID_HW_RING_ID;
1490         bp->grp_info[idx].cp_fw_ring_id = INVALID_HW_RING_ID;
1491         memset(cpr->cp_desc_ring, 0, cpr->cp_ring_struct->ring_size *
1492                         sizeof(*cpr->cp_desc_ring));
1493         cpr->cp_raw_cons = 0;
1494 }
1495
1496 int bnxt_free_all_hwrm_rings(struct bnxt *bp)
1497 {
1498         unsigned int i;
1499         int rc = 0;
1500
1501         for (i = 0; i < bp->tx_cp_nr_rings; i++) {
1502                 struct bnxt_tx_queue *txq = bp->tx_queues[i];
1503                 struct bnxt_tx_ring_info *txr = txq->tx_ring;
1504                 struct bnxt_ring *ring = txr->tx_ring_struct;
1505                 struct bnxt_cp_ring_info *cpr = txq->cp_ring;
1506                 unsigned int idx = bp->rx_cp_nr_rings + i + 1;
1507
1508                 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
1509                         bnxt_hwrm_ring_free(bp, ring,
1510                                         HWRM_RING_FREE_INPUT_RING_TYPE_TX);
1511                         ring->fw_ring_id = INVALID_HW_RING_ID;
1512                         memset(txr->tx_desc_ring, 0,
1513                                         txr->tx_ring_struct->ring_size *
1514                                         sizeof(*txr->tx_desc_ring));
1515                         memset(txr->tx_buf_ring, 0,
1516                                         txr->tx_ring_struct->ring_size *
1517                                         sizeof(*txr->tx_buf_ring));
1518                         txr->tx_prod = 0;
1519                         txr->tx_cons = 0;
1520                 }
1521                 if (cpr->cp_ring_struct->fw_ring_id != INVALID_HW_RING_ID) {
1522                         bnxt_free_cp_ring(bp, cpr, idx);
1523                         cpr->cp_ring_struct->fw_ring_id = INVALID_HW_RING_ID;
1524                 }
1525         }
1526
1527         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
1528                 struct bnxt_rx_queue *rxq = bp->rx_queues[i];
1529                 struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
1530                 struct bnxt_ring *ring = rxr->rx_ring_struct;
1531                 struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
1532                 unsigned int idx = i + 1;
1533
1534                 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
1535                         bnxt_hwrm_ring_free(bp, ring,
1536                                         HWRM_RING_FREE_INPUT_RING_TYPE_RX);
1537                         ring->fw_ring_id = INVALID_HW_RING_ID;
1538                         bp->grp_info[idx].rx_fw_ring_id = INVALID_HW_RING_ID;
1539                         memset(rxr->rx_desc_ring, 0,
1540                                         rxr->rx_ring_struct->ring_size *
1541                                         sizeof(*rxr->rx_desc_ring));
1542                         memset(rxr->rx_buf_ring, 0,
1543                                         rxr->rx_ring_struct->ring_size *
1544                                         sizeof(*rxr->rx_buf_ring));
1545                         rxr->rx_prod = 0;
1546                         memset(rxr->ag_buf_ring, 0,
1547                                         rxr->ag_ring_struct->ring_size *
1548                                         sizeof(*rxr->ag_buf_ring));
1549                         rxr->ag_prod = 0;
1550                 }
1551                 if (cpr->cp_ring_struct->fw_ring_id != INVALID_HW_RING_ID) {
1552                         bnxt_free_cp_ring(bp, cpr, idx);
1553                         bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
1554                         cpr->cp_ring_struct->fw_ring_id = INVALID_HW_RING_ID;
1555                 }
1556         }
1557
1558         /* Default completion ring */
1559         {
1560                 struct bnxt_cp_ring_info *cpr = bp->def_cp_ring;
1561
1562                 if (cpr->cp_ring_struct->fw_ring_id != INVALID_HW_RING_ID) {
1563                         bnxt_free_cp_ring(bp, cpr, 0);
1564                         cpr->cp_ring_struct->fw_ring_id = INVALID_HW_RING_ID;
1565                 }
1566         }
1567
1568         return rc;
1569 }
1570
1571 int bnxt_alloc_all_hwrm_ring_grps(struct bnxt *bp)
1572 {
1573         uint16_t i;
1574         uint32_t rc = 0;
1575
1576         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
1577                 rc = bnxt_hwrm_ring_grp_alloc(bp, i);
1578                 if (rc)
1579                         return rc;
1580         }
1581         return rc;
1582 }
1583
1584 void bnxt_free_hwrm_resources(struct bnxt *bp)
1585 {
1586         /* Release memzone */
1587         rte_free(bp->hwrm_cmd_resp_addr);
1588         rte_free(bp->hwrm_short_cmd_req_addr);
1589         bp->hwrm_cmd_resp_addr = NULL;
1590         bp->hwrm_short_cmd_req_addr = NULL;
1591         bp->hwrm_cmd_resp_dma_addr = 0;
1592         bp->hwrm_short_cmd_req_dma_addr = 0;
1593 }
1594
1595 int bnxt_alloc_hwrm_resources(struct bnxt *bp)
1596 {
1597         struct rte_pci_device *pdev = bp->pdev;
1598         char type[RTE_MEMZONE_NAMESIZE];
1599
1600         sprintf(type, "bnxt_hwrm_%04x:%02x:%02x:%02x", pdev->addr.domain,
1601                 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
1602         bp->max_resp_len = HWRM_MAX_RESP_LEN;
1603         bp->hwrm_cmd_resp_addr = rte_malloc(type, bp->max_resp_len, 0);
1604         rte_mem_lock_page(bp->hwrm_cmd_resp_addr);
1605         if (bp->hwrm_cmd_resp_addr == NULL)
1606                 return -ENOMEM;
1607         bp->hwrm_cmd_resp_dma_addr =
1608                 rte_mem_virt2phy(bp->hwrm_cmd_resp_addr);
1609         if (bp->hwrm_cmd_resp_dma_addr == 0) {
1610                 RTE_LOG(ERR, PMD,
1611                         "unable to map response address to physical memory\n");
1612                 return -ENOMEM;
1613         }
1614         rte_spinlock_init(&bp->hwrm_lock);
1615
1616         return 0;
1617 }
1618
1619 int bnxt_clear_hwrm_vnic_filters(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1620 {
1621         struct bnxt_filter_info *filter;
1622         int rc = 0;
1623
1624         STAILQ_FOREACH(filter, &vnic->filter, next) {
1625                 rc = bnxt_hwrm_clear_filter(bp, filter);
1626                 if (rc)
1627                         break;
1628         }
1629         return rc;
1630 }
1631
1632 int bnxt_set_hwrm_vnic_filters(struct bnxt *bp, struct bnxt_vnic_info *vnic)
1633 {
1634         struct bnxt_filter_info *filter;
1635         int rc = 0;
1636
1637         STAILQ_FOREACH(filter, &vnic->filter, next) {
1638                 rc = bnxt_hwrm_set_filter(bp, vnic->fw_vnic_id, filter);
1639                 if (rc)
1640                         break;
1641         }
1642         return rc;
1643 }
1644
1645 void bnxt_free_tunnel_ports(struct bnxt *bp)
1646 {
1647         if (bp->vxlan_port_cnt)
1648                 bnxt_hwrm_tunnel_dst_port_free(bp, bp->vxlan_fw_dst_port_id,
1649                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_VXLAN);
1650         bp->vxlan_port = 0;
1651         if (bp->geneve_port_cnt)
1652                 bnxt_hwrm_tunnel_dst_port_free(bp, bp->geneve_fw_dst_port_id,
1653                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_GENEVE);
1654         bp->geneve_port = 0;
1655 }
1656
1657 void bnxt_free_all_hwrm_resources(struct bnxt *bp)
1658 {
1659         struct bnxt_vnic_info *vnic;
1660         unsigned int i;
1661
1662         if (bp->vnic_info == NULL)
1663                 return;
1664
1665         vnic = &bp->vnic_info[0];
1666         if (BNXT_PF(bp))
1667                 bnxt_hwrm_cfa_l2_clear_rx_mask(bp, vnic);
1668
1669         /* VNIC resources */
1670         for (i = 0; i < bp->nr_vnics; i++) {
1671                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
1672
1673                 bnxt_clear_hwrm_vnic_filters(bp, vnic);
1674
1675                 bnxt_hwrm_vnic_ctx_free(bp, vnic);
1676
1677                 bnxt_hwrm_vnic_tpa_cfg(bp, vnic, false);
1678
1679                 bnxt_hwrm_vnic_free(bp, vnic);
1680         }
1681         /* Ring resources */
1682         bnxt_free_all_hwrm_rings(bp);
1683         bnxt_free_all_hwrm_ring_grps(bp);
1684         bnxt_free_all_hwrm_stat_ctxs(bp);
1685         bnxt_free_tunnel_ports(bp);
1686 }
1687
1688 static uint16_t bnxt_parse_eth_link_duplex(uint32_t conf_link_speed)
1689 {
1690         uint8_t hw_link_duplex = HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH;
1691
1692         if ((conf_link_speed & ETH_LINK_SPEED_FIXED) == ETH_LINK_SPEED_AUTONEG)
1693                 return HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH;
1694
1695         switch (conf_link_speed) {
1696         case ETH_LINK_SPEED_10M_HD:
1697         case ETH_LINK_SPEED_100M_HD:
1698                 return HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_HALF;
1699         }
1700         return hw_link_duplex;
1701 }
1702
1703 static uint16_t bnxt_parse_eth_link_speed(uint32_t conf_link_speed)
1704 {
1705         uint16_t eth_link_speed = 0;
1706
1707         if (conf_link_speed == ETH_LINK_SPEED_AUTONEG)
1708                 return ETH_LINK_SPEED_AUTONEG;
1709
1710         switch (conf_link_speed & ~ETH_LINK_SPEED_FIXED) {
1711         case ETH_LINK_SPEED_100M:
1712         case ETH_LINK_SPEED_100M_HD:
1713                 eth_link_speed =
1714                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_100MB;
1715                 break;
1716         case ETH_LINK_SPEED_1G:
1717                 eth_link_speed =
1718                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_1GB;
1719                 break;
1720         case ETH_LINK_SPEED_2_5G:
1721                 eth_link_speed =
1722                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_2_5GB;
1723                 break;
1724         case ETH_LINK_SPEED_10G:
1725                 eth_link_speed =
1726                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_10GB;
1727                 break;
1728         case ETH_LINK_SPEED_20G:
1729                 eth_link_speed =
1730                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_20GB;
1731                 break;
1732         case ETH_LINK_SPEED_25G:
1733                 eth_link_speed =
1734                         HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_25GB;
1735                 break;
1736         case ETH_LINK_SPEED_40G:
1737                 eth_link_speed =
1738                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_40GB;
1739                 break;
1740         case ETH_LINK_SPEED_50G:
1741                 eth_link_speed =
1742                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_50GB;
1743                 break;
1744         default:
1745                 RTE_LOG(ERR, PMD,
1746                         "Unsupported link speed %d; default to AUTO\n",
1747                         conf_link_speed);
1748                 break;
1749         }
1750         return eth_link_speed;
1751 }
1752
1753 #define BNXT_SUPPORTED_SPEEDS (ETH_LINK_SPEED_100M | ETH_LINK_SPEED_100M_HD | \
1754                 ETH_LINK_SPEED_1G | ETH_LINK_SPEED_2_5G | \
1755                 ETH_LINK_SPEED_10G | ETH_LINK_SPEED_20G | ETH_LINK_SPEED_25G | \
1756                 ETH_LINK_SPEED_40G | ETH_LINK_SPEED_50G)
1757
1758 static int bnxt_valid_link_speed(uint32_t link_speed, uint8_t port_id)
1759 {
1760         uint32_t one_speed;
1761
1762         if (link_speed == ETH_LINK_SPEED_AUTONEG)
1763                 return 0;
1764
1765         if (link_speed & ETH_LINK_SPEED_FIXED) {
1766                 one_speed = link_speed & ~ETH_LINK_SPEED_FIXED;
1767
1768                 if (one_speed & (one_speed - 1)) {
1769                         RTE_LOG(ERR, PMD,
1770                                 "Invalid advertised speeds (%u) for port %u\n",
1771                                 link_speed, port_id);
1772                         return -EINVAL;
1773                 }
1774                 if ((one_speed & BNXT_SUPPORTED_SPEEDS) != one_speed) {
1775                         RTE_LOG(ERR, PMD,
1776                                 "Unsupported advertised speed (%u) for port %u\n",
1777                                 link_speed, port_id);
1778                         return -EINVAL;
1779                 }
1780         } else {
1781                 if (!(link_speed & BNXT_SUPPORTED_SPEEDS)) {
1782                         RTE_LOG(ERR, PMD,
1783                                 "Unsupported advertised speeds (%u) for port %u\n",
1784                                 link_speed, port_id);
1785                         return -EINVAL;
1786                 }
1787         }
1788         return 0;
1789 }
1790
1791 static uint16_t bnxt_parse_eth_link_speed_mask(uint32_t link_speed)
1792 {
1793         uint16_t ret = 0;
1794
1795         if (link_speed == ETH_LINK_SPEED_AUTONEG)
1796                 link_speed = BNXT_SUPPORTED_SPEEDS;
1797
1798         if (link_speed & ETH_LINK_SPEED_100M)
1799                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100MB;
1800         if (link_speed & ETH_LINK_SPEED_100M_HD)
1801                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100MB;
1802         if (link_speed & ETH_LINK_SPEED_1G)
1803                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_1GB;
1804         if (link_speed & ETH_LINK_SPEED_2_5G)
1805                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_2_5GB;
1806         if (link_speed & ETH_LINK_SPEED_10G)
1807                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_10GB;
1808         if (link_speed & ETH_LINK_SPEED_20G)
1809                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_20GB;
1810         if (link_speed & ETH_LINK_SPEED_25G)
1811                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_25GB;
1812         if (link_speed & ETH_LINK_SPEED_40G)
1813                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_40GB;
1814         if (link_speed & ETH_LINK_SPEED_50G)
1815                 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_50GB;
1816         return ret;
1817 }
1818
1819 static uint32_t bnxt_parse_hw_link_speed(uint16_t hw_link_speed)
1820 {
1821         uint32_t eth_link_speed = ETH_SPEED_NUM_NONE;
1822
1823         switch (hw_link_speed) {
1824         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100MB:
1825                 eth_link_speed = ETH_SPEED_NUM_100M;
1826                 break;
1827         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_1GB:
1828                 eth_link_speed = ETH_SPEED_NUM_1G;
1829                 break;
1830         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2_5GB:
1831                 eth_link_speed = ETH_SPEED_NUM_2_5G;
1832                 break;
1833         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_10GB:
1834                 eth_link_speed = ETH_SPEED_NUM_10G;
1835                 break;
1836         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_20GB:
1837                 eth_link_speed = ETH_SPEED_NUM_20G;
1838                 break;
1839         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_25GB:
1840                 eth_link_speed = ETH_SPEED_NUM_25G;
1841                 break;
1842         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_40GB:
1843                 eth_link_speed = ETH_SPEED_NUM_40G;
1844                 break;
1845         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_50GB:
1846                 eth_link_speed = ETH_SPEED_NUM_50G;
1847                 break;
1848         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2GB:
1849         default:
1850                 RTE_LOG(ERR, PMD, "HWRM link speed %d not defined\n",
1851                         hw_link_speed);
1852                 break;
1853         }
1854         return eth_link_speed;
1855 }
1856
1857 static uint16_t bnxt_parse_hw_link_duplex(uint16_t hw_link_duplex)
1858 {
1859         uint16_t eth_link_duplex = ETH_LINK_FULL_DUPLEX;
1860
1861         switch (hw_link_duplex) {
1862         case HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH:
1863         case HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_FULL:
1864                 eth_link_duplex = ETH_LINK_FULL_DUPLEX;
1865                 break;
1866         case HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_HALF:
1867                 eth_link_duplex = ETH_LINK_HALF_DUPLEX;
1868                 break;
1869         default:
1870                 RTE_LOG(ERR, PMD, "HWRM link duplex %d not defined\n",
1871                         hw_link_duplex);
1872                 break;
1873         }
1874         return eth_link_duplex;
1875 }
1876
1877 int bnxt_get_hwrm_link_config(struct bnxt *bp, struct rte_eth_link *link)
1878 {
1879         int rc = 0;
1880         struct bnxt_link_info *link_info = &bp->link_info;
1881
1882         rc = bnxt_hwrm_port_phy_qcfg(bp, link_info);
1883         if (rc) {
1884                 RTE_LOG(ERR, PMD,
1885                         "Get link config failed with rc %d\n", rc);
1886                 goto exit;
1887         }
1888         if (link_info->link_up)
1889                 link->link_speed =
1890                         bnxt_parse_hw_link_speed(link_info->link_speed);
1891         else
1892                 link->link_speed = ETH_SPEED_NUM_NONE;
1893         link->link_duplex = bnxt_parse_hw_link_duplex(link_info->duplex);
1894         link->link_status = link_info->link_up;
1895         link->link_autoneg = link_info->auto_mode ==
1896                 HWRM_PORT_PHY_QCFG_OUTPUT_AUTO_MODE_NONE ?
1897                 ETH_LINK_SPEED_FIXED : ETH_LINK_SPEED_AUTONEG;
1898 exit:
1899         return rc;
1900 }
1901
1902 int bnxt_set_hwrm_link_config(struct bnxt *bp, bool link_up)
1903 {
1904         int rc = 0;
1905         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
1906         struct bnxt_link_info link_req;
1907         uint16_t speed;
1908
1909         if (BNXT_NPAR_PF(bp) || BNXT_VF(bp))
1910                 return 0;
1911
1912         rc = bnxt_valid_link_speed(dev_conf->link_speeds,
1913                         bp->eth_dev->data->port_id);
1914         if (rc)
1915                 goto error;
1916
1917         memset(&link_req, 0, sizeof(link_req));
1918         link_req.link_up = link_up;
1919         if (!link_up)
1920                 goto port_phy_cfg;
1921
1922         speed = bnxt_parse_eth_link_speed(dev_conf->link_speeds);
1923         link_req.phy_flags = HWRM_PORT_PHY_CFG_INPUT_FLAGS_RESET_PHY;
1924         if (speed == 0) {
1925                 link_req.phy_flags |=
1926                                 HWRM_PORT_PHY_CFG_INPUT_FLAGS_RESTART_AUTONEG;
1927                 link_req.auto_mode =
1928                                 HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_SPEED_MASK;
1929                 link_req.auto_link_speed_mask =
1930                         bnxt_parse_eth_link_speed_mask(dev_conf->link_speeds);
1931         } else {
1932                 link_req.phy_flags |= HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE;
1933                 link_req.link_speed = speed;
1934                 RTE_LOG(INFO, PMD, "Set Link Speed %x\n", speed);
1935         }
1936         link_req.duplex = bnxt_parse_eth_link_duplex(dev_conf->link_speeds);
1937         link_req.auto_pause = bp->link_info.auto_pause;
1938         link_req.force_pause = bp->link_info.force_pause;
1939
1940 port_phy_cfg:
1941         rc = bnxt_hwrm_port_phy_cfg(bp, &link_req);
1942         if (rc) {
1943                 RTE_LOG(ERR, PMD,
1944                         "Set link config failed with rc %d\n", rc);
1945         }
1946
1947         rte_delay_ms(BNXT_LINK_WAIT_INTERVAL);
1948 error:
1949         return rc;
1950 }
1951
1952 /* JIRA 22088 */
1953 int bnxt_hwrm_func_qcfg(struct bnxt *bp)
1954 {
1955         struct hwrm_func_qcfg_input req = {0};
1956         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
1957         int rc = 0;
1958
1959         HWRM_PREP(req, FUNC_QCFG, -1, resp);
1960         req.fid = rte_cpu_to_le_16(0xffff);
1961
1962         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
1963
1964         HWRM_CHECK_RESULT;
1965
1966         /* Hard Coded.. 0xfff VLAN ID mask */
1967         bp->vlan = rte_le_to_cpu_16(resp->vlan) & 0xfff;
1968
1969         switch (resp->port_partition_type) {
1970         case HWRM_FUNC_QCFG_OUTPUT_PORT_PARTITION_TYPE_NPAR1_0:
1971         case HWRM_FUNC_QCFG_OUTPUT_PORT_PARTITION_TYPE_NPAR1_5:
1972         case HWRM_FUNC_QCFG_OUTPUT_PORT_PARTITION_TYPE_NPAR2_0:
1973                 bp->port_partition_type = resp->port_partition_type;
1974                 break;
1975         default:
1976                 bp->port_partition_type = 0;
1977                 break;
1978         }
1979
1980         return rc;
1981 }
1982
1983 static void copy_func_cfg_to_qcaps(struct hwrm_func_cfg_input *fcfg,
1984                                    struct hwrm_func_qcaps_output *qcaps)
1985 {
1986         qcaps->max_rsscos_ctx = fcfg->num_rsscos_ctxs;
1987         memcpy(qcaps->mac_address, fcfg->dflt_mac_addr,
1988                sizeof(qcaps->mac_address));
1989         qcaps->max_l2_ctxs = fcfg->num_l2_ctxs;
1990         qcaps->max_rx_rings = fcfg->num_rx_rings;
1991         qcaps->max_tx_rings = fcfg->num_tx_rings;
1992         qcaps->max_cmpl_rings = fcfg->num_cmpl_rings;
1993         qcaps->max_stat_ctx = fcfg->num_stat_ctxs;
1994         qcaps->max_vfs = 0;
1995         qcaps->first_vf_id = 0;
1996         qcaps->max_vnics = fcfg->num_vnics;
1997         qcaps->max_decap_records = 0;
1998         qcaps->max_encap_records = 0;
1999         qcaps->max_tx_wm_flows = 0;
2000         qcaps->max_tx_em_flows = 0;
2001         qcaps->max_rx_wm_flows = 0;
2002         qcaps->max_rx_em_flows = 0;
2003         qcaps->max_flow_id = 0;
2004         qcaps->max_mcast_filters = fcfg->num_mcast_filters;
2005         qcaps->max_sp_tx_rings = 0;
2006         qcaps->max_hw_ring_grps = fcfg->num_hw_ring_grps;
2007 }
2008
2009 static int bnxt_hwrm_pf_func_cfg(struct bnxt *bp, int tx_rings)
2010 {
2011         struct hwrm_func_cfg_input req = {0};
2012         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2013         int rc;
2014
2015         req.enables = rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_MTU |
2016                         HWRM_FUNC_CFG_INPUT_ENABLES_MRU |
2017                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RSSCOS_CTXS |
2018                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_STAT_CTXS |
2019                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_CMPL_RINGS |
2020                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_TX_RINGS |
2021                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RX_RINGS |
2022                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_L2_CTXS |
2023                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_VNICS |
2024                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_HW_RING_GRPS);
2025         req.flags = rte_cpu_to_le_32(bp->pf.func_cfg_flags);
2026         req.mtu = rte_cpu_to_le_16(bp->eth_dev->data->mtu + ETHER_HDR_LEN +
2027                                    ETHER_CRC_LEN + VLAN_TAG_SIZE);
2028         req.mru = rte_cpu_to_le_16(bp->eth_dev->data->mtu + ETHER_HDR_LEN +
2029                                    ETHER_CRC_LEN + VLAN_TAG_SIZE);
2030         req.num_rsscos_ctxs = rte_cpu_to_le_16(bp->max_rsscos_ctx);
2031         req.num_stat_ctxs = rte_cpu_to_le_16(bp->max_stat_ctx);
2032         req.num_cmpl_rings = rte_cpu_to_le_16(bp->max_cp_rings);
2033         req.num_tx_rings = rte_cpu_to_le_16(tx_rings);
2034         req.num_rx_rings = rte_cpu_to_le_16(bp->max_rx_rings);
2035         req.num_l2_ctxs = rte_cpu_to_le_16(bp->max_l2_ctx);
2036         req.num_vnics = rte_cpu_to_le_16(bp->max_vnics);
2037         req.num_hw_ring_grps = rte_cpu_to_le_16(bp->max_ring_grps);
2038         req.fid = rte_cpu_to_le_16(0xffff);
2039
2040         HWRM_PREP(req, FUNC_CFG, -1, resp);
2041
2042         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2043         HWRM_CHECK_RESULT;
2044
2045         return rc;
2046 }
2047
2048 static void populate_vf_func_cfg_req(struct bnxt *bp,
2049                                      struct hwrm_func_cfg_input *req,
2050                                      int num_vfs)
2051 {
2052         req->enables = rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_MTU |
2053                         HWRM_FUNC_CFG_INPUT_ENABLES_MRU |
2054                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RSSCOS_CTXS |
2055                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_STAT_CTXS |
2056                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_CMPL_RINGS |
2057                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_TX_RINGS |
2058                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_RX_RINGS |
2059                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_L2_CTXS |
2060                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_VNICS |
2061                         HWRM_FUNC_CFG_INPUT_ENABLES_NUM_HW_RING_GRPS);
2062
2063         req->mtu = rte_cpu_to_le_16(bp->eth_dev->data->mtu + ETHER_HDR_LEN +
2064                                     ETHER_CRC_LEN + VLAN_TAG_SIZE);
2065         req->mru = rte_cpu_to_le_16(bp->eth_dev->data->mtu + ETHER_HDR_LEN +
2066                                     ETHER_CRC_LEN + VLAN_TAG_SIZE);
2067         req->num_rsscos_ctxs = rte_cpu_to_le_16(bp->max_rsscos_ctx /
2068                                                 (num_vfs + 1));
2069         req->num_stat_ctxs = rte_cpu_to_le_16(bp->max_stat_ctx / (num_vfs + 1));
2070         req->num_cmpl_rings = rte_cpu_to_le_16(bp->max_cp_rings /
2071                                                (num_vfs + 1));
2072         req->num_tx_rings = rte_cpu_to_le_16(bp->max_tx_rings / (num_vfs + 1));
2073         req->num_rx_rings = rte_cpu_to_le_16(bp->max_rx_rings / (num_vfs + 1));
2074         req->num_l2_ctxs = rte_cpu_to_le_16(bp->max_l2_ctx / (num_vfs + 1));
2075         /* TODO: For now, do not support VMDq/RFS on VFs. */
2076         req->num_vnics = rte_cpu_to_le_16(1);
2077         req->num_hw_ring_grps = rte_cpu_to_le_16(bp->max_ring_grps /
2078                                                  (num_vfs + 1));
2079 }
2080
2081 static void add_random_mac_if_needed(struct bnxt *bp,
2082                                      struct hwrm_func_cfg_input *cfg_req,
2083                                      int vf)
2084 {
2085         struct ether_addr mac;
2086
2087         if (bnxt_hwrm_func_qcfg_vf_default_mac(bp, vf, &mac))
2088                 return;
2089
2090         if (memcmp(mac.addr_bytes, "\x00\x00\x00\x00\x00", 6) == 0) {
2091                 cfg_req->enables |=
2092                 rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
2093                 eth_random_addr(cfg_req->dflt_mac_addr);
2094                 bp->pf.vf_info[vf].random_mac = true;
2095         } else {
2096                 memcpy(cfg_req->dflt_mac_addr, mac.addr_bytes, ETHER_ADDR_LEN);
2097         }
2098 }
2099
2100 static void reserve_resources_from_vf(struct bnxt *bp,
2101                                       struct hwrm_func_cfg_input *cfg_req,
2102                                       int vf)
2103 {
2104         struct hwrm_func_qcaps_input req = {0};
2105         struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
2106         int rc;
2107
2108         /* Get the actual allocated values now */
2109         HWRM_PREP(req, FUNC_QCAPS, -1, resp);
2110         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
2111         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2112
2113         if (rc) {
2114                 RTE_LOG(ERR, PMD, "hwrm_func_qcaps failed rc:%d\n", rc);
2115                 copy_func_cfg_to_qcaps(cfg_req, resp);
2116         } else if (resp->error_code) {
2117                 rc = rte_le_to_cpu_16(resp->error_code);
2118                 RTE_LOG(ERR, PMD, "hwrm_func_qcaps error %d\n", rc);
2119                 copy_func_cfg_to_qcaps(cfg_req, resp);
2120         }
2121
2122         bp->max_rsscos_ctx -= rte_le_to_cpu_16(resp->max_rsscos_ctx);
2123         bp->max_stat_ctx -= rte_le_to_cpu_16(resp->max_stat_ctx);
2124         bp->max_cp_rings -= rte_le_to_cpu_16(resp->max_cmpl_rings);
2125         bp->max_tx_rings -= rte_le_to_cpu_16(resp->max_tx_rings);
2126         bp->max_rx_rings -= rte_le_to_cpu_16(resp->max_rx_rings);
2127         bp->max_l2_ctx -= rte_le_to_cpu_16(resp->max_l2_ctxs);
2128         /*
2129          * TODO: While not supporting VMDq with VFs, max_vnics is always
2130          * forced to 1 in this case
2131          */
2132         //bp->max_vnics -= rte_le_to_cpu_16(esp->max_vnics);
2133         bp->max_ring_grps -= rte_le_to_cpu_16(resp->max_hw_ring_grps);
2134 }
2135
2136 int bnxt_hwrm_func_qcfg_current_vf_vlan(struct bnxt *bp, int vf)
2137 {
2138         struct hwrm_func_qcfg_input req = {0};
2139         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
2140         int rc;
2141
2142         /* Check for zero MAC address */
2143         HWRM_PREP(req, FUNC_QCFG, -1, resp);
2144         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
2145         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2146         if (rc) {
2147                 RTE_LOG(ERR, PMD, "hwrm_func_qcfg failed rc:%d\n", rc);
2148                 return -1;
2149         } else if (resp->error_code) {
2150                 rc = rte_le_to_cpu_16(resp->error_code);
2151                 RTE_LOG(ERR, PMD, "hwrm_func_qcfg error %d\n", rc);
2152                 return -1;
2153         }
2154         return rte_le_to_cpu_16(resp->vlan);
2155 }
2156
2157 static int update_pf_resource_max(struct bnxt *bp)
2158 {
2159         struct hwrm_func_qcfg_input req = {0};
2160         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
2161         int rc;
2162
2163         /* And copy the allocated numbers into the pf struct */
2164         HWRM_PREP(req, FUNC_QCFG, -1, resp);
2165         req.fid = rte_cpu_to_le_16(0xffff);
2166         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2167         HWRM_CHECK_RESULT;
2168
2169         /* Only TX ring value reflects actual allocation? TODO */
2170         bp->max_tx_rings = rte_le_to_cpu_16(resp->alloc_tx_rings);
2171         bp->pf.evb_mode = resp->evb_mode;
2172
2173         return rc;
2174 }
2175
2176 int bnxt_hwrm_allocate_pf_only(struct bnxt *bp)
2177 {
2178         int rc;
2179
2180         if (!BNXT_PF(bp)) {
2181                 RTE_LOG(ERR, PMD, "Attempt to allcoate VFs on a VF!\n");
2182                 return -1;
2183         }
2184
2185         rc = bnxt_hwrm_func_qcaps(bp);
2186         if (rc)
2187                 return rc;
2188
2189         bp->pf.func_cfg_flags &=
2190                 ~(HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_ENABLE |
2191                   HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_DISABLE);
2192         bp->pf.func_cfg_flags |=
2193                 HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_DISABLE;
2194         rc = bnxt_hwrm_pf_func_cfg(bp, bp->max_tx_rings);
2195         return rc;
2196 }
2197
2198 int bnxt_hwrm_allocate_vfs(struct bnxt *bp, int num_vfs)
2199 {
2200         struct hwrm_func_cfg_input req = {0};
2201         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2202         int i;
2203         size_t sz;
2204         int rc = 0;
2205         size_t req_buf_sz;
2206
2207         if (!BNXT_PF(bp)) {
2208                 RTE_LOG(ERR, PMD, "Attempt to allcoate VFs on a VF!\n");
2209                 return -1;
2210         }
2211
2212         rc = bnxt_hwrm_func_qcaps(bp);
2213
2214         if (rc)
2215                 return rc;
2216
2217         bp->pf.active_vfs = num_vfs;
2218
2219         /*
2220          * First, configure the PF to only use one TX ring.  This ensures that
2221          * there are enough rings for all VFs.
2222          *
2223          * If we don't do this, when we call func_alloc() later, we will lock
2224          * extra rings to the PF that won't be available during func_cfg() of
2225          * the VFs.
2226          *
2227          * This has been fixed with firmware versions above 20.6.54
2228          */
2229         bp->pf.func_cfg_flags &=
2230                 ~(HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_ENABLE |
2231                   HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_DISABLE);
2232         bp->pf.func_cfg_flags |=
2233                 HWRM_FUNC_CFG_INPUT_FLAGS_STD_TX_RING_MODE_ENABLE;
2234         rc = bnxt_hwrm_pf_func_cfg(bp, 1);
2235         if (rc)
2236                 return rc;
2237
2238         /*
2239          * Now, create and register a buffer to hold forwarded VF requests
2240          */
2241         req_buf_sz = num_vfs * HWRM_MAX_REQ_LEN;
2242         bp->pf.vf_req_buf = rte_malloc("bnxt_vf_fwd", req_buf_sz,
2243                 page_roundup(num_vfs * HWRM_MAX_REQ_LEN));
2244         if (bp->pf.vf_req_buf == NULL) {
2245                 rc = -ENOMEM;
2246                 goto error_free;
2247         }
2248         for (sz = 0; sz < req_buf_sz; sz += getpagesize())
2249                 rte_mem_lock_page(((char *)bp->pf.vf_req_buf) + sz);
2250         for (i = 0; i < num_vfs; i++)
2251                 bp->pf.vf_info[i].req_buf = ((char *)bp->pf.vf_req_buf) +
2252                                         (i * HWRM_MAX_REQ_LEN);
2253
2254         rc = bnxt_hwrm_func_buf_rgtr(bp);
2255         if (rc)
2256                 goto error_free;
2257
2258         populate_vf_func_cfg_req(bp, &req, num_vfs);
2259
2260         bp->pf.active_vfs = 0;
2261         for (i = 0; i < num_vfs; i++) {
2262                 add_random_mac_if_needed(bp, &req, i);
2263
2264                 HWRM_PREP(req, FUNC_CFG, -1, resp);
2265                 req.flags = rte_cpu_to_le_32(bp->pf.vf_info[i].func_cfg_flags);
2266                 req.fid = rte_cpu_to_le_16(bp->pf.vf_info[i].fid);
2267                 rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2268
2269                 /* Clear enable flag for next pass */
2270                 req.enables &= ~rte_cpu_to_le_32(
2271                                 HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_MAC_ADDR);
2272
2273                 if (rc || resp->error_code) {
2274                         RTE_LOG(ERR, PMD,
2275                                 "Failed to initizlie VF %d\n", i);
2276                         RTE_LOG(ERR, PMD,
2277                                 "Not all VFs available. (%d, %d)\n",
2278                                 rc, resp->error_code);
2279                         break;
2280                 }
2281
2282                 reserve_resources_from_vf(bp, &req, i);
2283                 bp->pf.active_vfs++;
2284         }
2285
2286         /*
2287          * Now configure the PF to use "the rest" of the resources
2288          * We're using STD_TX_RING_MODE here though which will limit the TX
2289          * rings.  This will allow QoS to function properly.  Not setting this
2290          * will cause PF rings to break bandwidth settings.
2291          */
2292         rc = bnxt_hwrm_pf_func_cfg(bp, bp->max_tx_rings);
2293         if (rc)
2294                 goto error_free;
2295
2296         rc = update_pf_resource_max(bp);
2297         if (rc)
2298                 goto error_free;
2299
2300         return rc;
2301
2302 error_free:
2303         bnxt_hwrm_func_buf_unrgtr(bp);
2304         return rc;
2305 }
2306
2307 int bnxt_hwrm_pf_evb_mode(struct bnxt *bp)
2308 {
2309         struct hwrm_func_cfg_input req = {0};
2310         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2311         int rc;
2312
2313         HWRM_PREP(req, FUNC_CFG, -1, resp);
2314
2315         req.fid = rte_cpu_to_le_16(0xffff);
2316         req.enables = rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_EVB_MODE);
2317         req.evb_mode = bp->pf.evb_mode;
2318
2319         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2320         HWRM_CHECK_RESULT;
2321
2322         return rc;
2323 }
2324
2325 int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, uint16_t port,
2326                                 uint8_t tunnel_type)
2327 {
2328         struct hwrm_tunnel_dst_port_alloc_input req = {0};
2329         struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
2330         int rc = 0;
2331
2332         HWRM_PREP(req, TUNNEL_DST_PORT_ALLOC, -1, resp);
2333         req.tunnel_type = tunnel_type;
2334         req.tunnel_dst_port_val = port;
2335         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2336         HWRM_CHECK_RESULT;
2337
2338         switch (tunnel_type) {
2339         case HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_VXLAN:
2340                 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
2341                 bp->vxlan_port = port;
2342                 break;
2343         case HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_GENEVE:
2344                 bp->geneve_fw_dst_port_id = resp->tunnel_dst_port_id;
2345                 bp->geneve_port = port;
2346                 break;
2347         default:
2348                 break;
2349         }
2350         return rc;
2351 }
2352
2353 int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, uint16_t port,
2354                                 uint8_t tunnel_type)
2355 {
2356         struct hwrm_tunnel_dst_port_free_input req = {0};
2357         struct hwrm_tunnel_dst_port_free_output *resp = bp->hwrm_cmd_resp_addr;
2358         int rc = 0;
2359
2360         HWRM_PREP(req, TUNNEL_DST_PORT_FREE, -1, resp);
2361         req.tunnel_type = tunnel_type;
2362         req.tunnel_dst_port_id = rte_cpu_to_be_16(port);
2363         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2364         HWRM_CHECK_RESULT;
2365
2366         return rc;
2367 }
2368
2369 int bnxt_hwrm_func_cfg_vf_set_flags(struct bnxt *bp, uint16_t vf)
2370 {
2371         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2372         struct hwrm_func_cfg_input req = {0};
2373         int rc;
2374
2375         HWRM_PREP(req, FUNC_CFG, -1, resp);
2376         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
2377         req.flags = rte_cpu_to_le_32(bp->pf.vf_info[vf].func_cfg_flags);
2378         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2379         HWRM_CHECK_RESULT;
2380
2381         return rc;
2382 }
2383
2384 void vf_vnic_set_rxmask_cb(struct bnxt_vnic_info *vnic, void *flagp)
2385 {
2386         uint32_t *flag = flagp;
2387
2388         vnic->flags = *flag;
2389 }
2390
2391 int bnxt_set_rx_mask_no_vlan(struct bnxt *bp, struct bnxt_vnic_info *vnic)
2392 {
2393         return bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
2394 }
2395
2396 int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
2397 {
2398         int rc = 0;
2399         struct hwrm_func_buf_rgtr_input req = {.req_type = 0 };
2400         struct hwrm_func_buf_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
2401
2402         HWRM_PREP(req, FUNC_BUF_RGTR, -1, resp);
2403
2404         req.req_buf_num_pages = rte_cpu_to_le_16(1);
2405         req.req_buf_page_size = rte_cpu_to_le_16(
2406                          page_getenum(bp->pf.active_vfs * HWRM_MAX_REQ_LEN));
2407         req.req_buf_len = rte_cpu_to_le_16(HWRM_MAX_REQ_LEN);
2408         req.req_buf_page_addr[0] =
2409                 rte_cpu_to_le_64(rte_mem_virt2phy(bp->pf.vf_req_buf));
2410         if (req.req_buf_page_addr[0] == 0) {
2411                 RTE_LOG(ERR, PMD,
2412                         "unable to map buffer address to physical memory\n");
2413                 return -ENOMEM;
2414         }
2415
2416         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2417
2418         HWRM_CHECK_RESULT;
2419
2420         return rc;
2421 }
2422
2423 int bnxt_hwrm_func_buf_unrgtr(struct bnxt *bp)
2424 {
2425         int rc = 0;
2426         struct hwrm_func_buf_unrgtr_input req = {.req_type = 0 };
2427         struct hwrm_func_buf_unrgtr_output *resp = bp->hwrm_cmd_resp_addr;
2428
2429         HWRM_PREP(req, FUNC_BUF_UNRGTR, -1, resp);
2430
2431         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2432
2433         HWRM_CHECK_RESULT;
2434
2435         return rc;
2436 }
2437
2438 int bnxt_hwrm_func_cfg_def_cp(struct bnxt *bp)
2439 {
2440         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2441         struct hwrm_func_cfg_input req = {0};
2442         int rc;
2443
2444         HWRM_PREP(req, FUNC_CFG, -1, resp);
2445         req.fid = rte_cpu_to_le_16(0xffff);
2446         req.flags = rte_cpu_to_le_32(bp->pf.func_cfg_flags);
2447         req.enables = rte_cpu_to_le_32(
2448                         HWRM_FUNC_CFG_INPUT_ENABLES_ASYNC_EVENT_CR);
2449         req.async_event_cr = rte_cpu_to_le_16(
2450                         bp->def_cp_ring->cp_ring_struct->fw_ring_id);
2451         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2452         HWRM_CHECK_RESULT;
2453
2454         return rc;
2455 }
2456
2457 int bnxt_hwrm_vf_func_cfg_def_cp(struct bnxt *bp)
2458 {
2459         struct hwrm_func_vf_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2460         struct hwrm_func_vf_cfg_input req = {0};
2461         int rc;
2462
2463         HWRM_PREP(req, FUNC_VF_CFG, -1, resp);
2464         req.enables = rte_cpu_to_le_32(
2465                         HWRM_FUNC_CFG_INPUT_ENABLES_ASYNC_EVENT_CR);
2466         req.async_event_cr = rte_cpu_to_le_16(
2467                         bp->def_cp_ring->cp_ring_struct->fw_ring_id);
2468         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2469         HWRM_CHECK_RESULT;
2470
2471         return rc;
2472 }
2473
2474 int bnxt_hwrm_set_default_vlan(struct bnxt *bp, int vf, uint8_t is_vf)
2475 {
2476         struct hwrm_func_cfg_input req = {0};
2477         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2478         uint16_t dflt_vlan, fid;
2479         uint32_t func_cfg_flags;
2480         int rc = 0;
2481
2482         HWRM_PREP(req, FUNC_CFG, -1, resp);
2483
2484         if (is_vf) {
2485                 dflt_vlan = bp->pf.vf_info[vf].dflt_vlan;
2486                 fid = bp->pf.vf_info[vf].fid;
2487                 func_cfg_flags = bp->pf.vf_info[vf].func_cfg_flags;
2488         } else {
2489                 fid = rte_cpu_to_le_16(0xffff);
2490                 func_cfg_flags = bp->pf.func_cfg_flags;
2491                 dflt_vlan = bp->vlan;
2492         }
2493
2494         req.flags = rte_cpu_to_le_32(func_cfg_flags);
2495         req.fid = rte_cpu_to_le_16(fid);
2496         req.enables |= rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_VLAN);
2497         req.dflt_vlan = rte_cpu_to_le_16(dflt_vlan);
2498
2499         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2500         HWRM_CHECK_RESULT;
2501
2502         return rc;
2503 }
2504
2505 int bnxt_hwrm_func_bw_cfg(struct bnxt *bp, uint16_t vf,
2506                         uint16_t max_bw, uint16_t enables)
2507 {
2508         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2509         struct hwrm_func_cfg_input req = {0};
2510         int rc;
2511
2512         HWRM_PREP(req, FUNC_CFG, -1, resp);
2513         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
2514         req.enables |= rte_cpu_to_le_32(enables);
2515         req.flags = rte_cpu_to_le_32(bp->pf.vf_info[vf].func_cfg_flags);
2516         req.max_bw = rte_cpu_to_le_32(max_bw);
2517         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2518         HWRM_CHECK_RESULT;
2519
2520         return rc;
2521 }
2522
2523 int bnxt_hwrm_set_vf_vlan(struct bnxt *bp, int vf)
2524 {
2525         struct hwrm_func_cfg_input req = {0};
2526         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2527         int rc = 0;
2528
2529         HWRM_PREP(req, FUNC_CFG, -1, resp);
2530         req.flags = rte_cpu_to_le_32(bp->pf.vf_info[vf].func_cfg_flags);
2531         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
2532         req.enables |= rte_cpu_to_le_32(HWRM_FUNC_CFG_INPUT_ENABLES_DFLT_VLAN);
2533         req.dflt_vlan = rte_cpu_to_le_16(bp->pf.vf_info[vf].dflt_vlan);
2534
2535         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2536         HWRM_CHECK_RESULT;
2537
2538         return rc;
2539 }
2540
2541 int bnxt_hwrm_reject_fwd_resp(struct bnxt *bp, uint16_t target_id,
2542                               void *encaped, size_t ec_size)
2543 {
2544         int rc = 0;
2545         struct hwrm_reject_fwd_resp_input req = {.req_type = 0};
2546         struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
2547
2548         if (ec_size > sizeof(req.encap_request))
2549                 return -1;
2550
2551         HWRM_PREP(req, REJECT_FWD_RESP, -1, resp);
2552
2553         req.encap_resp_target_id = rte_cpu_to_le_16(target_id);
2554         memcpy(req.encap_request, encaped, ec_size);
2555
2556         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2557
2558         HWRM_CHECK_RESULT;
2559
2560         return rc;
2561 }
2562
2563 int bnxt_hwrm_func_qcfg_vf_default_mac(struct bnxt *bp, uint16_t vf,
2564                                        struct ether_addr *mac)
2565 {
2566         struct hwrm_func_qcfg_input req = {0};
2567         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
2568         int rc;
2569
2570         HWRM_PREP(req, FUNC_QCFG, -1, resp);
2571         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
2572         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2573
2574         HWRM_CHECK_RESULT;
2575
2576         memcpy(mac->addr_bytes, resp->mac_address, ETHER_ADDR_LEN);
2577         return rc;
2578 }
2579
2580 int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, uint16_t target_id,
2581                             void *encaped, size_t ec_size)
2582 {
2583         int rc = 0;
2584         struct hwrm_exec_fwd_resp_input req = {.req_type = 0};
2585         struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
2586
2587         if (ec_size > sizeof(req.encap_request))
2588                 return -1;
2589
2590         HWRM_PREP(req, EXEC_FWD_RESP, -1, resp);
2591
2592         req.encap_resp_target_id = rte_cpu_to_le_16(target_id);
2593         memcpy(req.encap_request, encaped, ec_size);
2594
2595         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2596
2597         HWRM_CHECK_RESULT;
2598
2599         return rc;
2600 }
2601
2602 int bnxt_hwrm_ctx_qstats(struct bnxt *bp, uint32_t cid, int idx,
2603                          struct rte_eth_stats *stats)
2604 {
2605         int rc = 0;
2606         struct hwrm_stat_ctx_query_input req = {.req_type = 0};
2607         struct hwrm_stat_ctx_query_output *resp = bp->hwrm_cmd_resp_addr;
2608
2609         HWRM_PREP(req, STAT_CTX_QUERY, -1, resp);
2610
2611         req.stat_ctx_id = rte_cpu_to_le_32(cid);
2612
2613         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2614
2615         HWRM_CHECK_RESULT;
2616
2617         stats->q_ipackets[idx] = rte_le_to_cpu_64(resp->rx_ucast_pkts);
2618         stats->q_ipackets[idx] += rte_le_to_cpu_64(resp->rx_mcast_pkts);
2619         stats->q_ipackets[idx] += rte_le_to_cpu_64(resp->rx_bcast_pkts);
2620         stats->q_ibytes[idx] = rte_le_to_cpu_64(resp->rx_ucast_bytes);
2621         stats->q_ibytes[idx] += rte_le_to_cpu_64(resp->rx_mcast_bytes);
2622         stats->q_ibytes[idx] += rte_le_to_cpu_64(resp->rx_bcast_bytes);
2623
2624         stats->q_opackets[idx] = rte_le_to_cpu_64(resp->tx_ucast_pkts);
2625         stats->q_opackets[idx] += rte_le_to_cpu_64(resp->tx_mcast_pkts);
2626         stats->q_opackets[idx] += rte_le_to_cpu_64(resp->tx_bcast_pkts);
2627         stats->q_obytes[idx] = rte_le_to_cpu_64(resp->tx_ucast_bytes);
2628         stats->q_obytes[idx] += rte_le_to_cpu_64(resp->tx_mcast_bytes);
2629         stats->q_obytes[idx] += rte_le_to_cpu_64(resp->tx_bcast_bytes);
2630
2631         stats->q_errors[idx] = rte_le_to_cpu_64(resp->rx_err_pkts);
2632         stats->q_errors[idx] += rte_le_to_cpu_64(resp->tx_err_pkts);
2633         stats->q_errors[idx] += rte_le_to_cpu_64(resp->rx_drop_pkts);
2634
2635         return rc;
2636 }
2637
2638 int bnxt_hwrm_port_qstats(struct bnxt *bp)
2639 {
2640         struct hwrm_port_qstats_input req = {0};
2641         struct hwrm_port_qstats_output *resp = bp->hwrm_cmd_resp_addr;
2642         struct bnxt_pf_info *pf = &bp->pf;
2643         int rc;
2644
2645         if (!(bp->flags & BNXT_FLAG_PORT_STATS))
2646                 return 0;
2647
2648         HWRM_PREP(req, PORT_QSTATS, -1, resp);
2649         req.port_id = rte_cpu_to_le_16(pf->port_id);
2650         req.tx_stat_host_addr = rte_cpu_to_le_64(bp->hw_tx_port_stats_map);
2651         req.rx_stat_host_addr = rte_cpu_to_le_64(bp->hw_rx_port_stats_map);
2652         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2653         HWRM_CHECK_RESULT;
2654         return rc;
2655 }
2656
2657 int bnxt_hwrm_port_clr_stats(struct bnxt *bp)
2658 {
2659         struct hwrm_port_clr_stats_input req = {0};
2660         struct hwrm_port_clr_stats_output *resp = bp->hwrm_cmd_resp_addr;
2661         struct bnxt_pf_info *pf = &bp->pf;
2662         int rc;
2663
2664         if (!(bp->flags & BNXT_FLAG_PORT_STATS))
2665                 return 0;
2666
2667         HWRM_PREP(req, PORT_CLR_STATS, -1, resp);
2668         req.port_id = rte_cpu_to_le_16(pf->port_id);
2669         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2670         HWRM_CHECK_RESULT;
2671         return rc;
2672 }
2673
2674 int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
2675 {
2676         struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
2677         struct hwrm_port_led_qcaps_input req = {0};
2678         int rc;
2679
2680         if (BNXT_VF(bp))
2681                 return 0;
2682
2683         HWRM_PREP(req, PORT_LED_QCAPS, -1, resp);
2684         req.port_id = bp->pf.port_id;
2685         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2686         HWRM_CHECK_RESULT;
2687
2688         if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
2689                 unsigned int i;
2690
2691                 bp->num_leds = resp->num_leds;
2692                 memcpy(bp->leds, &resp->led0_id,
2693                         sizeof(bp->leds[0]) * bp->num_leds);
2694                 for (i = 0; i < bp->num_leds; i++) {
2695                         struct bnxt_led_info *led = &bp->leds[i];
2696
2697                         uint16_t caps = led->led_state_caps;
2698
2699                         if (!led->led_group_id ||
2700                                 !BNXT_LED_ALT_BLINK_CAP(caps)) {
2701                                 bp->num_leds = 0;
2702                                 break;
2703                         }
2704                 }
2705         }
2706         return rc;
2707 }
2708
2709 int bnxt_hwrm_port_led_cfg(struct bnxt *bp, bool led_on)
2710 {
2711         struct hwrm_port_led_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2712         struct hwrm_port_led_cfg_input req = {0};
2713         struct bnxt_led_cfg *led_cfg;
2714         uint8_t led_state = HWRM_PORT_LED_QCFG_OUTPUT_LED0_STATE_DEFAULT;
2715         uint16_t duration = 0;
2716         int rc, i;
2717
2718         if (!bp->num_leds || BNXT_VF(bp))
2719                 return -EOPNOTSUPP;
2720
2721         HWRM_PREP(req, PORT_LED_CFG, -1, resp);
2722         if (led_on) {
2723                 led_state = HWRM_PORT_LED_CFG_INPUT_LED0_STATE_BLINKALT;
2724                 duration = rte_cpu_to_le_16(500);
2725         }
2726         req.port_id = bp->pf.port_id;
2727         req.num_leds = bp->num_leds;
2728         led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
2729         for (i = 0; i < bp->num_leds; i++, led_cfg++) {
2730                 req.enables |= BNXT_LED_DFLT_ENABLES(i);
2731                 led_cfg->led_id = bp->leds[i].led_id;
2732                 led_cfg->led_state = led_state;
2733                 led_cfg->led_blink_on = duration;
2734                 led_cfg->led_blink_off = duration;
2735                 led_cfg->led_group_id = bp->leds[i].led_group_id;
2736         }
2737
2738         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2739         HWRM_CHECK_RESULT;
2740
2741         return rc;
2742 }
2743
2744 static void bnxt_vnic_count(struct bnxt_vnic_info *vnic, void *cbdata)
2745 {
2746         uint32_t *count = cbdata;
2747
2748         if (vnic->func_default)
2749                 *count = *count + 1;
2750 }
2751
2752 static int bnxt_vnic_count_hwrm_stub(struct bnxt *bp __rte_unused,
2753                                      struct bnxt_vnic_info *vnic __rte_unused)
2754 {
2755         return 0;
2756 }
2757
2758 int bnxt_vf_default_vnic_count(struct bnxt *bp, uint16_t vf)
2759 {
2760         uint32_t count = 0;
2761
2762         bnxt_hwrm_func_vf_vnic_query_and_config(bp, vf, bnxt_vnic_count,
2763             &count, bnxt_vnic_count_hwrm_stub);
2764
2765         return count;
2766 }
2767
2768 static int bnxt_hwrm_func_vf_vnic_query(struct bnxt *bp, uint16_t vf,
2769                                         uint16_t *vnic_ids)
2770 {
2771         struct hwrm_func_vf_vnic_ids_query_input req = {0};
2772         struct hwrm_func_vf_vnic_ids_query_output *resp =
2773                                                 bp->hwrm_cmd_resp_addr;
2774         int rc;
2775
2776         /* First query all VNIC ids */
2777         HWRM_PREP(req, FUNC_VF_VNIC_IDS_QUERY, -1, resp_vf_vnic_ids);
2778
2779         req.vf_id = rte_cpu_to_le_16(bp->pf.first_vf_id + vf);
2780         req.max_vnic_id_cnt = rte_cpu_to_le_32(bp->pf.total_vnics);
2781         req.vnic_id_tbl_addr = rte_cpu_to_le_64(rte_mem_virt2phy(vnic_ids));
2782
2783         if (req.vnic_id_tbl_addr == 0) {
2784                 RTE_LOG(ERR, PMD,
2785                 "unable to map VNIC ID table address to physical memory\n");
2786                 return -ENOMEM;
2787         }
2788         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2789         if (rc) {
2790                 RTE_LOG(ERR, PMD, "hwrm_func_vf_vnic_query failed rc:%d\n", rc);
2791                 return -1;
2792         } else if (resp->error_code) {
2793                 rc = rte_le_to_cpu_16(resp->error_code);
2794                 RTE_LOG(ERR, PMD, "hwrm_func_vf_vnic_query error %d\n", rc);
2795                 return -1;
2796         }
2797
2798         return rte_le_to_cpu_32(resp->vnic_id_cnt);
2799 }
2800
2801 /*
2802  * This function queries the VNIC IDs  for a specified VF. It then calls
2803  * the vnic_cb to update the necessary field in vnic_info with cbdata.
2804  * Then it calls the hwrm_cb function to program this new vnic configuration.
2805  */
2806 int bnxt_hwrm_func_vf_vnic_query_and_config(struct bnxt *bp, uint16_t vf,
2807         void (*vnic_cb)(struct bnxt_vnic_info *, void *), void *cbdata,
2808         int (*hwrm_cb)(struct bnxt *bp, struct bnxt_vnic_info *vnic))
2809 {
2810         struct bnxt_vnic_info vnic;
2811         int rc = 0;
2812         int i, num_vnic_ids;
2813         uint16_t *vnic_ids;
2814         size_t vnic_id_sz;
2815         size_t sz;
2816
2817         /* First query all VNIC ids */
2818         vnic_id_sz = bp->pf.total_vnics * sizeof(*vnic_ids);
2819         vnic_ids = rte_malloc("bnxt_hwrm_vf_vnic_ids_query", vnic_id_sz,
2820                         RTE_CACHE_LINE_SIZE);
2821         if (vnic_ids == NULL) {
2822                 rc = -ENOMEM;
2823                 return rc;
2824         }
2825         for (sz = 0; sz < vnic_id_sz; sz += getpagesize())
2826                 rte_mem_lock_page(((char *)vnic_ids) + sz);
2827
2828         num_vnic_ids = bnxt_hwrm_func_vf_vnic_query(bp, vf, vnic_ids);
2829
2830         if (num_vnic_ids < 0)
2831                 return num_vnic_ids;
2832
2833         /* Retrieve VNIC, update bd_stall then update */
2834
2835         for (i = 0; i < num_vnic_ids; i++) {
2836                 memset(&vnic, 0, sizeof(struct bnxt_vnic_info));
2837                 vnic.fw_vnic_id = rte_le_to_cpu_16(vnic_ids[i]);
2838                 rc = bnxt_hwrm_vnic_qcfg(bp, &vnic, bp->pf.first_vf_id + vf);
2839                 if (rc)
2840                         break;
2841                 if (vnic.mru == 4)      /* Indicates unallocated */
2842                         continue;
2843
2844                 vnic_cb(&vnic, cbdata);
2845
2846                 rc = hwrm_cb(bp, &vnic);
2847                 if (rc)
2848                         break;
2849         }
2850
2851         rte_free(vnic_ids);
2852
2853         return rc;
2854 }
2855
2856 int bnxt_hwrm_func_cfg_vf_set_vlan_anti_spoof(struct bnxt *bp, uint16_t vf,
2857                                               bool on)
2858 {
2859         struct hwrm_func_cfg_output *resp = bp->hwrm_cmd_resp_addr;
2860         struct hwrm_func_cfg_input req = {0};
2861         int rc;
2862
2863         HWRM_PREP(req, FUNC_CFG, -1, resp);
2864         req.fid = rte_cpu_to_le_16(bp->pf.vf_info[vf].fid);
2865         req.enables |= rte_cpu_to_le_32(
2866                         HWRM_FUNC_CFG_INPUT_ENABLES_VLAN_ANTISPOOF_MODE);
2867         req.vlan_antispoof_mode = on ?
2868                 HWRM_FUNC_CFG_INPUT_VLAN_ANTISPOOF_MODE_VALIDATE_VLAN :
2869                 HWRM_FUNC_CFG_INPUT_VLAN_ANTISPOOF_MODE_NOCHECK;
2870         rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
2871         HWRM_CHECK_RESULT;
2872
2873         return rc;
2874 }
2875
2876 int bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(struct bnxt *bp, int vf)
2877 {
2878         struct bnxt_vnic_info vnic;
2879         uint16_t *vnic_ids;
2880         size_t vnic_id_sz;
2881         int num_vnic_ids, i;
2882         size_t sz;
2883         int rc;
2884
2885         vnic_id_sz = bp->pf.total_vnics * sizeof(*vnic_ids);
2886         vnic_ids = rte_malloc("bnxt_hwrm_vf_vnic_ids_query", vnic_id_sz,
2887                         RTE_CACHE_LINE_SIZE);
2888         if (vnic_ids == NULL) {
2889                 rc = -ENOMEM;
2890                 return rc;
2891         }
2892
2893         for (sz = 0; sz < vnic_id_sz; sz += getpagesize())
2894                 rte_mem_lock_page(((char *)vnic_ids) + sz);
2895
2896         rc = bnxt_hwrm_func_vf_vnic_query(bp, vf, vnic_ids);
2897         if (rc <= 0)
2898                 goto exit;
2899         num_vnic_ids = rc;
2900
2901         /*
2902          * Loop through to find the default VNIC ID.
2903          * TODO: The easier way would be to obtain the resp->dflt_vnic_id
2904          * by sending the hwrm_func_qcfg command to the firmware.
2905          */
2906         for (i = 0; i < num_vnic_ids; i++) {
2907                 memset(&vnic, 0, sizeof(struct bnxt_vnic_info));
2908                 vnic.fw_vnic_id = rte_le_to_cpu_16(vnic_ids[i]);
2909                 rc = bnxt_hwrm_vnic_qcfg(bp, &vnic,
2910                                         bp->pf.first_vf_id + vf);
2911                 if (rc)
2912                         goto exit;
2913                 if (vnic.func_default) {
2914                         rte_free(vnic_ids);
2915                         return vnic.fw_vnic_id;
2916                 }
2917         }
2918         /* Could not find a default VNIC. */
2919         RTE_LOG(ERR, PMD, "No default VNIC\n");
2920 exit:
2921         rte_free(vnic_ids);
2922         return -1;
2923 }