4 * Copyright(c) Broadcom Limited.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #include <rte_byteorder.h>
35 #include <rte_common.h>
36 #include <rte_cycles.h>
37 #include <rte_malloc.h>
38 #include <rte_memzone.h>
39 #include <rte_version.h>
42 #include "bnxt_hwrm.h"
43 #include "hsi_struct_def_dpdk.h"
45 #define HWRM_CMD_TIMEOUT 2000
48 * HWRM Functions (sent to HWRM)
49 * These are named bnxt_hwrm_*() and return -1 if bnxt_hwrm_send_message()
50 * fails (ie: a timeout), and a positive non-zero HWRM error code if the HWRM
51 * command was failed by the ChiMP.
54 static int bnxt_hwrm_send_message_locked(struct bnxt *bp, void *msg,
58 struct input *req = msg;
59 struct output *resp = bp->hwrm_cmd_resp_addr;
64 /* Write request msg to hwrm channel */
65 for (i = 0; i < msg_len; i += 4) {
66 bar = (uint8_t *)bp->bar0 + i;
67 *(volatile uint32_t *)bar = *data;
71 /* Zero the rest of the request space */
72 for (; i < bp->max_req_len; i += 4) {
73 bar = (uint8_t *)bp->bar0 + i;
74 *(volatile uint32_t *)bar = 0;
77 /* Ring channel doorbell */
78 bar = (uint8_t *)bp->bar0 + 0x100;
79 *(volatile uint32_t *)bar = 1;
81 /* Poll for the valid bit */
82 for (i = 0; i < HWRM_CMD_TIMEOUT; i++) {
83 /* Sanity check on the resp->resp_len */
85 if (resp->resp_len && resp->resp_len <=
87 /* Last byte of resp contains the valid key */
88 valid = (uint8_t *)resp + resp->resp_len - 1;
89 if (*valid == HWRM_RESP_VALID_KEY)
95 if (i >= HWRM_CMD_TIMEOUT) {
96 RTE_LOG(ERR, PMD, "Error sending msg %x\n",
106 static int bnxt_hwrm_send_message(struct bnxt *bp, void *msg, uint32_t msg_len)
110 rte_spinlock_lock(&bp->hwrm_lock);
111 rc = bnxt_hwrm_send_message_locked(bp, msg, msg_len);
112 rte_spinlock_unlock(&bp->hwrm_lock);
116 #define HWRM_PREP(req, type, cr, resp) \
117 memset(bp->hwrm_cmd_resp_addr, 0, bp->max_resp_len); \
118 req.req_type = rte_cpu_to_le_16(HWRM_##type); \
119 req.cmpl_ring = rte_cpu_to_le_16(cr); \
120 req.seq_id = rte_cpu_to_le_16(bp->hwrm_cmd_seq++); \
121 req.target_id = rte_cpu_to_le_16(0xffff); \
122 req.resp_addr = rte_cpu_to_le_64(bp->hwrm_cmd_resp_dma_addr)
124 #define HWRM_CHECK_RESULT \
127 RTE_LOG(ERR, PMD, "%s failed rc:%d\n", \
131 if (resp->error_code) { \
132 rc = rte_le_to_cpu_16(resp->error_code); \
133 RTE_LOG(ERR, PMD, "%s error %d\n", __func__, rc); \
138 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
141 struct hwrm_func_qcaps_input req = {.req_type = 0 };
142 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
144 HWRM_PREP(req, FUNC_QCAPS, -1, resp);
146 req.fid = rte_cpu_to_le_16(0xffff);
148 rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
153 struct bnxt_pf_info *pf = &bp->pf;
155 pf->fw_fid = rte_le_to_cpu_32(resp->fid);
156 pf->port_id = resp->port_id;
157 memcpy(pf->mac_addr, resp->perm_mac_address, ETHER_ADDR_LEN);
158 pf->max_rsscos_ctx = rte_le_to_cpu_16(resp->max_rsscos_ctx);
159 pf->max_cp_rings = rte_le_to_cpu_16(resp->max_cmpl_rings);
160 pf->max_tx_rings = rte_le_to_cpu_16(resp->max_tx_rings);
161 pf->max_rx_rings = rte_le_to_cpu_16(resp->max_rx_rings);
162 pf->max_l2_ctx = rte_le_to_cpu_16(resp->max_l2_ctxs);
163 pf->max_vnics = rte_le_to_cpu_16(resp->max_vnics);
164 pf->first_vf_id = rte_le_to_cpu_16(resp->first_vf_id);
165 pf->max_vfs = rte_le_to_cpu_16(resp->max_vfs);
167 struct bnxt_vf_info *vf = &bp->vf;
169 vf->fw_fid = rte_le_to_cpu_32(resp->fid);
170 memcpy(vf->mac_addr, &resp->perm_mac_address, ETHER_ADDR_LEN);
171 vf->max_rsscos_ctx = rte_le_to_cpu_16(resp->max_rsscos_ctx);
172 vf->max_cp_rings = rte_le_to_cpu_16(resp->max_cmpl_rings);
173 vf->max_tx_rings = rte_le_to_cpu_16(resp->max_tx_rings);
174 vf->max_rx_rings = rte_le_to_cpu_16(resp->max_rx_rings);
175 vf->max_l2_ctx = rte_le_to_cpu_16(resp->max_l2_ctxs);
176 vf->max_vnics = rte_le_to_cpu_16(resp->max_vnics);
182 int bnxt_hwrm_func_driver_register(struct bnxt *bp, uint32_t flags,
183 uint32_t *vf_req_fwd)
186 struct hwrm_func_drv_rgtr_input req = {.req_type = 0 };
187 struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
189 if (bp->flags & BNXT_FLAG_REGISTERED)
192 HWRM_PREP(req, FUNC_DRV_RGTR, -1, resp);
194 req.enables = HWRM_FUNC_DRV_RGTR_INPUT_ENABLES_VER;
195 req.ver_maj = RTE_VER_YEAR;
196 req.ver_min = RTE_VER_MONTH;
197 req.ver_upd = RTE_VER_MINOR;
199 memcpy(req.vf_req_fwd, vf_req_fwd, sizeof(req.vf_req_fwd));
201 rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
205 bp->flags |= BNXT_FLAG_REGISTERED;
210 int bnxt_hwrm_ver_get(struct bnxt *bp)
213 struct hwrm_ver_get_input req = {.req_type = 0 };
214 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
217 uint16_t max_resp_len;
218 char type[RTE_MEMZONE_NAMESIZE];
220 HWRM_PREP(req, VER_GET, -1, resp);
222 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
223 req.hwrm_intf_min = HWRM_VERSION_MINOR;
224 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
227 * Hold the lock since we may be adjusting the response pointers.
229 rte_spinlock_lock(&bp->hwrm_lock);
230 rc = bnxt_hwrm_send_message_locked(bp, &req, sizeof(req));
234 RTE_LOG(INFO, PMD, "%d.%d.%d:%d.%d.%d\n",
235 resp->hwrm_intf_maj, resp->hwrm_intf_min,
237 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld);
239 my_version = HWRM_VERSION_MAJOR << 16;
240 my_version |= HWRM_VERSION_MINOR << 8;
241 my_version |= HWRM_VERSION_UPDATE;
243 fw_version = resp->hwrm_intf_maj << 16;
244 fw_version |= resp->hwrm_intf_min << 8;
245 fw_version |= resp->hwrm_intf_upd;
247 if (resp->hwrm_intf_maj != HWRM_VERSION_MAJOR) {
248 RTE_LOG(ERR, PMD, "Unsupported firmware API version\n");
253 if (my_version != fw_version) {
254 RTE_LOG(INFO, PMD, "BNXT Driver/HWRM API mismatch.\n");
255 if (my_version < fw_version) {
257 "Firmware API version is newer than driver.\n");
259 "The driver may be missing features.\n");
262 "Firmware API version is older than driver.\n");
264 "Not all driver features may be functional.\n");
268 if (bp->max_req_len > resp->max_req_win_len) {
269 RTE_LOG(ERR, PMD, "Unsupported request length\n");
272 bp->max_req_len = resp->max_req_win_len;
273 max_resp_len = resp->max_resp_len;
274 if (bp->max_resp_len != max_resp_len) {
275 sprintf(type, "bnxt_hwrm_%04x:%02x:%02x:%02x",
276 bp->pdev->addr.domain, bp->pdev->addr.bus,
277 bp->pdev->addr.devid, bp->pdev->addr.function);
279 rte_free(bp->hwrm_cmd_resp_addr);
281 bp->hwrm_cmd_resp_addr = rte_malloc(type, max_resp_len, 0);
282 if (bp->hwrm_cmd_resp_addr == NULL) {
286 bp->hwrm_cmd_resp_dma_addr =
287 rte_malloc_virt2phy(bp->hwrm_cmd_resp_addr);
288 bp->max_resp_len = max_resp_len;
292 rte_spinlock_unlock(&bp->hwrm_lock);
296 int bnxt_hwrm_func_driver_unregister(struct bnxt *bp, uint32_t flags)
299 struct hwrm_func_drv_unrgtr_input req = {.req_type = 0 };
300 struct hwrm_func_drv_unrgtr_output *resp = bp->hwrm_cmd_resp_addr;
302 if (!(bp->flags & BNXT_FLAG_REGISTERED))
305 HWRM_PREP(req, FUNC_DRV_UNRGTR, -1, resp);
308 rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
312 bp->flags &= ~BNXT_FLAG_REGISTERED;
317 static int bnxt_hwrm_port_phy_cfg(struct bnxt *bp, struct bnxt_link_info *conf)
320 struct hwrm_port_phy_cfg_input req = {.req_type = 0};
321 struct hwrm_port_phy_cfg_output *resp = bp->hwrm_cmd_resp_addr;
323 HWRM_PREP(req, PORT_PHY_CFG, -1, resp);
325 req.flags = conf->phy_flags;
327 req.force_link_speed = conf->link_speed;
329 * Note, ChiMP FW 20.2.1 and 20.2.2 return an error when we set
330 * any auto mode, even "none".
332 if (req.auto_mode == HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_NONE) {
333 req.flags |= HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE;
335 req.auto_mode = conf->auto_mode;
337 HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_MODE;
338 req.auto_link_speed_mask = conf->auto_link_speed_mask;
340 HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_LINK_SPEED_MASK;
341 req.auto_link_speed = conf->auto_link_speed;
343 HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_LINK_SPEED;
345 req.auto_duplex = conf->duplex;
346 req.enables |= HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_DUPLEX;
347 req.auto_pause = conf->auto_pause;
348 /* Set force_pause if there is no auto or if there is a force */
351 HWRM_PORT_PHY_CFG_INPUT_ENABLES_AUTO_PAUSE;
354 HWRM_PORT_PHY_CFG_INPUT_ENABLES_FORCE_PAUSE;
355 req.force_pause = conf->force_pause;
358 HWRM_PORT_PHY_CFG_INPUT_ENABLES_FORCE_PAUSE;
360 req.flags &= ~HWRM_PORT_PHY_CFG_INPUT_FLAGS_RESTART_AUTONEG;
361 req.flags |= HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE_LINK_DOWN;
362 req.force_link_speed = 0;
365 rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
372 int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
375 struct hwrm_queue_qportcfg_input req = {.req_type = 0 };
376 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
378 HWRM_PREP(req, QUEUE_QPORTCFG, -1, resp);
380 rc = bnxt_hwrm_send_message(bp, &req, sizeof(req));
384 #define GET_QUEUE_INFO(x) \
385 bp->cos_queue[x].id = resp->queue_id##x; \
386 bp->cos_queue[x].profile = resp->queue_id##x##_service_profile
401 * HWRM utility functions
404 void bnxt_free_hwrm_resources(struct bnxt *bp)
406 /* Release memzone */
407 rte_free(bp->hwrm_cmd_resp_addr);
408 bp->hwrm_cmd_resp_addr = NULL;
409 bp->hwrm_cmd_resp_dma_addr = 0;
412 int bnxt_alloc_hwrm_resources(struct bnxt *bp)
414 struct rte_pci_device *pdev = bp->pdev;
415 char type[RTE_MEMZONE_NAMESIZE];
417 sprintf(type, "bnxt_hwrm_%04x:%02x:%02x:%02x", pdev->addr.domain,
418 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
419 bp->max_req_len = HWRM_MAX_REQ_LEN;
420 bp->max_resp_len = HWRM_MAX_RESP_LEN;
421 bp->hwrm_cmd_resp_addr = rte_malloc(type, bp->max_resp_len, 0);
422 if (bp->hwrm_cmd_resp_addr == NULL)
424 bp->hwrm_cmd_resp_dma_addr =
425 rte_malloc_virt2phy(bp->hwrm_cmd_resp_addr);
426 rte_spinlock_init(&bp->hwrm_lock);
431 static uint16_t bnxt_parse_eth_link_duplex(uint32_t conf_link_speed)
433 uint8_t hw_link_duplex = HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH;
435 if ((conf_link_speed & ETH_LINK_SPEED_FIXED) == ETH_LINK_SPEED_AUTONEG)
436 return HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_BOTH;
438 switch (conf_link_speed) {
439 case ETH_LINK_SPEED_10M_HD:
440 case ETH_LINK_SPEED_100M_HD:
441 return HWRM_PORT_PHY_CFG_INPUT_AUTO_DUPLEX_HALF;
443 return hw_link_duplex;
446 static uint16_t bnxt_parse_eth_link_speed(uint32_t conf_link_speed)
448 uint16_t eth_link_speed = 0;
450 if ((conf_link_speed & ETH_LINK_SPEED_FIXED) == ETH_LINK_SPEED_AUTONEG)
451 return ETH_LINK_SPEED_AUTONEG;
453 switch (conf_link_speed & ~ETH_LINK_SPEED_FIXED) {
454 case ETH_LINK_SPEED_100M:
455 case ETH_LINK_SPEED_100M_HD:
457 HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_10MB;
459 case ETH_LINK_SPEED_1G:
461 HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_1GB;
463 case ETH_LINK_SPEED_2_5G:
465 HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_2_5GB;
467 case ETH_LINK_SPEED_10G:
469 HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_10GB;
471 case ETH_LINK_SPEED_20G:
473 HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_20GB;
475 case ETH_LINK_SPEED_25G:
477 HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_25GB;
479 case ETH_LINK_SPEED_40G:
481 HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_40GB;
483 case ETH_LINK_SPEED_50G:
485 HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_50GB;
489 "Unsupported link speed %d; default to AUTO\n",
493 return eth_link_speed;
496 #define BNXT_SUPPORTED_SPEEDS (ETH_LINK_SPEED_100M | ETH_LINK_SPEED_100M_HD | \
497 ETH_LINK_SPEED_1G | ETH_LINK_SPEED_2_5G | \
498 ETH_LINK_SPEED_10G | ETH_LINK_SPEED_20G | ETH_LINK_SPEED_25G | \
499 ETH_LINK_SPEED_40G | ETH_LINK_SPEED_50G)
501 static int bnxt_valid_link_speed(uint32_t link_speed, uint8_t port_id)
505 if (link_speed == ETH_LINK_SPEED_AUTONEG)
508 if (link_speed & ETH_LINK_SPEED_FIXED) {
509 one_speed = link_speed & ~ETH_LINK_SPEED_FIXED;
511 if (one_speed & (one_speed - 1)) {
513 "Invalid advertised speeds (%u) for port %u\n",
514 link_speed, port_id);
517 if ((one_speed & BNXT_SUPPORTED_SPEEDS) != one_speed) {
519 "Unsupported advertised speed (%u) for port %u\n",
520 link_speed, port_id);
524 if (!(link_speed & BNXT_SUPPORTED_SPEEDS)) {
526 "Unsupported advertised speeds (%u) for port %u\n",
527 link_speed, port_id);
534 static uint16_t bnxt_parse_eth_link_speed_mask(uint32_t link_speed)
538 if (link_speed == ETH_LINK_SPEED_AUTONEG)
539 link_speed = BNXT_SUPPORTED_SPEEDS;
541 if (link_speed & ETH_LINK_SPEED_100M)
542 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100MB;
543 if (link_speed & ETH_LINK_SPEED_100M_HD)
544 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_100MB;
545 if (link_speed & ETH_LINK_SPEED_1G)
546 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_1GB;
547 if (link_speed & ETH_LINK_SPEED_2_5G)
548 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_2_5GB;
549 if (link_speed & ETH_LINK_SPEED_10G)
550 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_10GB;
551 if (link_speed & ETH_LINK_SPEED_20G)
552 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_20GB;
553 if (link_speed & ETH_LINK_SPEED_25G)
554 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_25GB;
555 if (link_speed & ETH_LINK_SPEED_40G)
556 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_40GB;
557 if (link_speed & ETH_LINK_SPEED_50G)
558 ret |= HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_MASK_50GB;
562 int bnxt_set_hwrm_link_config(struct bnxt *bp, bool link_up)
565 struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
566 struct bnxt_link_info link_req;
569 rc = bnxt_valid_link_speed(dev_conf->link_speeds,
570 bp->eth_dev->data->port_id);
574 memset(&link_req, 0, sizeof(link_req));
575 speed = bnxt_parse_eth_link_speed(dev_conf->link_speeds);
576 link_req.link_up = link_up;
579 HWRM_PORT_PHY_CFG_INPUT_FLAGS_RESTART_AUTONEG;
581 HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_ONE_OR_BELOW;
582 link_req.auto_link_speed_mask =
583 bnxt_parse_eth_link_speed_mask(dev_conf->link_speeds);
584 link_req.auto_link_speed =
585 HWRM_PORT_PHY_CFG_INPUT_AUTO_LINK_SPEED_50GB;
587 link_req.auto_mode = HWRM_PORT_PHY_CFG_INPUT_AUTO_MODE_NONE;
588 link_req.phy_flags = HWRM_PORT_PHY_CFG_INPUT_FLAGS_FORCE |
589 HWRM_PORT_PHY_CFG_INPUT_FLAGS_RESET_PHY;
590 link_req.link_speed = speed;
592 link_req.duplex = bnxt_parse_eth_link_duplex(dev_conf->link_speeds);
593 link_req.auto_pause = bp->link_info.auto_pause;
594 link_req.force_pause = bp->link_info.force_pause;
596 rc = bnxt_hwrm_port_phy_cfg(bp, &link_req);
599 "Set link config failed with rc %d\n", rc);