// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2019-2021 Broadcom.
 */

#include <asm/io.h>
#include <dm.h>
#include <linux/delay.h>
#include <memalign.h>
#include <net.h>

#include "bnxt.h"
#include "bnxt_dbg.h"

#define bnxt_down_chip(bp)     bnxt_hwrm_run(down_chip, bp, 0)
#define bnxt_bring_chip(bp)    bnxt_hwrm_run(bring_chip, bp, 1)

/* Broadcom ethernet driver PCI APIs. */
static void bnxt_bring_pci(struct bnxt *bp)
{
	u16 cmd_reg = 0;

	dm_pci_read_config16(bp->pdev, PCI_VENDOR_ID, &bp->vendor_id);
	dm_pci_read_config16(bp->pdev, PCI_DEVICE_ID, &bp->device_id);
	dm_pci_read_config16(bp->pdev, PCI_SUBSYSTEM_VENDOR_ID, &bp->subsystem_vendor);
	dm_pci_read_config16(bp->pdev, PCI_SUBSYSTEM_ID, &bp->subsystem_device);
	dm_pci_read_config16(bp->pdev, PCI_COMMAND, &bp->cmd_reg);
	dm_pci_read_config8(bp->pdev, PCI_INTERRUPT_LINE, &bp->irq);
	bp->bar0 = dm_pci_map_bar(bp->pdev, PCI_BASE_ADDRESS_0, 0, 0,
				  PCI_REGION_TYPE, PCI_REGION_MEM);
	bp->bar1 = dm_pci_map_bar(bp->pdev, PCI_BASE_ADDRESS_2, 0, 0,
				  PCI_REGION_TYPE, PCI_REGION_MEM);
	bp->bar2 = dm_pci_map_bar(bp->pdev, PCI_BASE_ADDRESS_4, 0, 0,
				  PCI_REGION_TYPE, PCI_REGION_MEM);
	cmd_reg = bp->cmd_reg | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
	cmd_reg |= PCI_COMMAND_INTX_DISABLE; /* disable intr */
	dm_pci_write_config16(bp->pdev, PCI_COMMAND, cmd_reg);
	dm_pci_read_config16(bp->pdev, PCI_COMMAND, &cmd_reg);
	dbg_pci(bp, __func__, cmd_reg);
}

int bnxt_free_rx_iob(struct bnxt *bp)
{
	unsigned int i;

	if (!(FLAG_TEST(bp->flag_hwrm, VALID_RX_IOB)))
		return STATUS_SUCCESS;

	for (i = 0; i < bp->rx.buf_cnt; i++) {
		if (bp->rx.iob[i]) {
			free(bp->rx.iob[i]);
			bp->rx.iob[i] = NULL;
		}
	}

	FLAG_RESET(bp->flag_hwrm, VALID_RX_IOB);

	return STATUS_SUCCESS;
}

static void set_rx_desc(u8 *buf, void *iob, u16 cons_id, u32 iob_idx)
{
	struct rx_prod_pkt_bd *desc;
	u16 off = cons_id * sizeof(struct rx_prod_pkt_bd);

	desc = (struct rx_prod_pkt_bd *)&buf[off];
	desc->flags_type = RX_PROD_PKT_BD_TYPE_RX_PROD_PKT;
	desc->len	 = MAX_ETHERNET_PACKET_BUFFER_SIZE;
	desc->opaque	 = iob_idx;
	desc->dma.addr = virt_to_bus(iob);
}

static int bnxt_alloc_rx_iob(struct bnxt *bp, u16 cons_id, u16 iob_idx)
{
	void *iob;

	iob = memalign(BNXT_DMA_ALIGNMENT, RX_STD_DMA_ALIGNED);
	if (!iob)
		return -ENOMEM;

	dbg_rx_iob(iob, iob_idx, cons_id);
	set_rx_desc((u8 *)bp->rx.bd_virt, iob, cons_id, (u32)iob_idx);
	bp->rx.iob[iob_idx] = iob;

	return 0;
}

void bnxt_mm_init(struct bnxt *bp, const char *func)
{
	memset(bp->hwrm_addr_req, 0, REQ_BUFFER_SIZE);
	memset(bp->hwrm_addr_resp, 0, RESP_BUFFER_SIZE);
	memset(bp->cq.bd_virt, 0, CQ_RING_DMA_BUFFER_SIZE);
	memset(bp->tx.bd_virt, 0, TX_RING_DMA_BUFFER_SIZE);
	memset(bp->rx.bd_virt, 0, RX_RING_DMA_BUFFER_SIZE);

	bp->data_addr_mapping = virt_to_bus(bp->hwrm_addr_data);
	bp->req_addr_mapping  = virt_to_bus(bp->hwrm_addr_req);
	bp->resp_addr_mapping = virt_to_bus(bp->hwrm_addr_resp);
	bp->wait_link_timeout = LINK_DEFAULT_TIMEOUT;
	bp->link_status       = STATUS_LINK_DOWN;
	bp->media_change      = 1;
	bp->mtu               = MAX_ETHERNET_PACKET_BUFFER_SIZE;
	bp->hwrm_max_req_len  = HWRM_MAX_REQ_LEN;
	bp->rx.buf_cnt        = NUM_RX_BUFFERS;
	bp->rx.ring_cnt       = MAX_RX_DESC_CNT;
	bp->tx.ring_cnt       = MAX_TX_DESC_CNT;
	bp->cq.ring_cnt       = MAX_CQ_DESC_CNT;
	bp->cq.completion_bit = 0x1;
	bp->link_set          = LINK_SPEED_DRV_100G;
	dbg_mem(bp, func);
}

void bnxt_free_mem(struct bnxt *bp)
{
	if (bp->cq.bd_virt) {
		free(bp->cq.bd_virt);
		bp->cq.bd_virt = NULL;
	}

	if (bp->rx.bd_virt) {
		free(bp->rx.bd_virt);
		bp->rx.bd_virt = NULL;
	}

	if (bp->tx.bd_virt) {
		free(bp->tx.bd_virt);
		bp->tx.bd_virt = NULL;
	}

	if (bp->hwrm_addr_resp) {
		free(bp->hwrm_addr_resp);
		bp->resp_addr_mapping = 0;
		bp->hwrm_addr_resp = NULL;
	}

	if (bp->hwrm_addr_req) {
		free(bp->hwrm_addr_req);
		bp->req_addr_mapping = 0;
		bp->hwrm_addr_req = NULL;
	}

	if (bp->hwrm_addr_data) {
		free(bp->hwrm_addr_data);
		bp->data_addr_mapping = 0;
		bp->hwrm_addr_data = NULL;
	}

	dbg_mem_free_done(__func__);
}

int bnxt_alloc_mem(struct bnxt *bp)
{
	bp->hwrm_addr_data = memalign(BNXT_DMA_ALIGNMENT, DMA_BUF_SIZE_ALIGNED);
	bp->hwrm_addr_req = memalign(BNXT_DMA_ALIGNMENT, REQ_BUF_SIZE_ALIGNED);
	bp->hwrm_addr_resp = MEM_HWRM_RESP;

	memset(&bp->tx, 0, sizeof(struct lm_tx_info_t));
	memset(&bp->rx, 0, sizeof(struct lm_rx_info_t));
	memset(&bp->cq, 0, sizeof(struct lm_cmp_info_t));

	bp->tx.bd_virt = memalign(BNXT_DMA_ALIGNMENT, TX_RING_DMA_BUFFER_SIZE);
	bp->rx.bd_virt = memalign(BNXT_DMA_ALIGNMENT, RX_RING_DMA_BUFFER_SIZE);
	bp->cq.bd_virt = memalign(BNXT_DMA_ALIGNMENT, CQ_RING_DMA_BUFFER_SIZE);

	if (bp->hwrm_addr_req &&
	    bp->hwrm_addr_resp &&
	    bp->hwrm_addr_data &&
	    bp->tx.bd_virt &&
	    bp->rx.bd_virt &&
	    bp->cq.bd_virt) {
		bnxt_mm_init(bp, __func__);
		return STATUS_SUCCESS;
	}

	dbg_mem_alloc_fail(__func__);
	bnxt_free_mem(bp);

	return -ENOMEM;
}

static void hwrm_init(struct bnxt *bp, struct input *req, u16 cmd, u16 len)
{
	memset(req, 0, len);
	req->req_type  = cmd;
	req->cmpl_ring = (u16)HWRM_NA_SIGNATURE;
	req->target_id = (u16)HWRM_NA_SIGNATURE;
	req->resp_addr = bp->resp_addr_mapping;
	req->seq_id    = bp->seq_id++;
}

static void hwrm_write_req(struct bnxt *bp, void *req, u32 cnt)
{
	u32 i = 0;

	for (i = 0; i < cnt; i++)
		writel(((u32 *)req)[i], bp->bar0 + GRC_COM_CHAN_BASE + (i * 4));

	writel(0x1, (bp->bar0 + GRC_COM_CHAN_BASE + GRC_COM_CHAN_TRIG));
}

static void short_hwrm_cmd_req(struct bnxt *bp, u16 len)
{
	struct hwrm_short_input sreq;

	memset(&sreq, 0, sizeof(struct hwrm_short_input));
	sreq.req_type  = (u16)((struct input *)bp->hwrm_addr_req)->req_type;
	sreq.signature = SHORT_REQ_SIGNATURE_SHORT_CMD;
	sreq.size      = len;
	sreq.req_addr  = bp->req_addr_mapping;
	dbg_short_cmd((u8 *)&sreq, __func__, sizeof(struct hwrm_short_input));
	hwrm_write_req(bp, &sreq, sizeof(struct hwrm_short_input) / 4);
}

static int wait_resp(struct bnxt *bp, u32 tmo, u16 len, const char *func)
{
	struct input *req = (struct input *)bp->hwrm_addr_req;
	struct output *resp = (struct output *)bp->hwrm_addr_resp;
	u8  *ptr = (u8 *)resp;
	u32 idx;
	u32 wait_cnt = HWRM_CMD_DEFAULT_MULTIPLAYER((u32)tmo);
	u16 resp_len = 0;
	u16 ret = STATUS_TIMEOUT;

	if (len > bp->hwrm_max_req_len)
		short_hwrm_cmd_req(bp, len);
	else
		hwrm_write_req(bp, req, (u32)(len / 4));

	for (idx = 0; idx < wait_cnt; idx++) {
		resp_len = resp->resp_len;
		if (resp->seq_id == req->seq_id && resp->req_type == req->req_type &&
		    ptr[resp_len - 1] == 1) {
			bp->last_resp_code = resp->error_code;
			ret = resp->error_code;
			break;
		}

		udelay(HWRM_CMD_POLL_WAIT_TIME);
	}

	dbg_hw_cmd(bp, func, len, resp_len, tmo, ret);

	return (int)ret;
}

static void bnxt_db_cq(struct bnxt *bp)
{
	writel(CQ_DOORBELL_KEY_IDX(bp->cq.cons_idx), bp->bar1);
}

static void bnxt_db_rx(struct bnxt *bp, u32 idx)
{
	writel(RX_DOORBELL_KEY_RX | idx, bp->bar1);
}

static void bnxt_db_tx(struct bnxt *bp, u32 idx)
{
	writel((u32)(TX_DOORBELL_KEY_TX | idx), bp->bar1);
}

int iob_pad(void *packet, int length)
{
	if (length >= ETH_ZLEN)
		return length;

	memset(((u8 *)packet + length), 0x00, (ETH_ZLEN - length));

	return ETH_ZLEN;
}

static inline u32 bnxt_tx_avail(struct bnxt *bp)
{
	barrier();

	return TX_AVAIL(bp->tx.ring_cnt) -
			((bp->tx.prod_id - bp->tx.cons_id) &
			(bp->tx.ring_cnt - 1));
}

void set_txq(struct bnxt *bp, int entry, dma_addr_t mapping, int len)
{
	struct tx_bd_short *prod_bd;

	prod_bd = (struct tx_bd_short *)BD_NOW(bp->tx.bd_virt,
					       entry,
					       sizeof(struct tx_bd_short));
	if (len < 512)
		prod_bd->flags_type = TX_BD_SHORT_FLAGS_LHINT_LT512;
	else if (len < 1024)
		prod_bd->flags_type = TX_BD_SHORT_FLAGS_LHINT_LT1K;
	else if (len < 2048)
		prod_bd->flags_type = TX_BD_SHORT_FLAGS_LHINT_LT2K;
	else
		prod_bd->flags_type = TX_BD_SHORT_FLAGS_LHINT_GTE2K;

	prod_bd->flags_type |= TX_BD_FLAGS;
	prod_bd->dma.addr = mapping;
	prod_bd->len      = len;
	prod_bd->opaque   = (u32)entry;
	dump_tx_bd(prod_bd, (u16)(sizeof(struct tx_bd_short)));
}

static void bnxt_tx_complete(struct bnxt *bp)
{
	bp->tx.cons_id = NEXT_IDX(bp->tx.cons_id, bp->tx.ring_cnt);
	bp->tx.cnt++;
	dump_tx_stat(bp);
}

int post_rx_buffers(struct bnxt *bp)
{
	u16 cons_id = (bp->rx.cons_idx % bp->rx.ring_cnt);
	u16 iob_idx;

	while (bp->rx.iob_cnt < bp->rx.buf_cnt) {
		iob_idx = (cons_id % bp->rx.buf_cnt);
		if (!bp->rx.iob[iob_idx]) {
			if (bnxt_alloc_rx_iob(bp, cons_id, iob_idx) < 0) {
				dbg_rx_alloc_iob_fail(iob_idx, cons_id);
				break;
			}
		}

		cons_id = NEXT_IDX(cons_id, bp->rx.ring_cnt);
		bp->rx.iob_cnt++;
	}

	if (cons_id != bp->rx.cons_idx) {
		dbg_rx_cid(bp->rx.cons_idx, cons_id);
		bp->rx.cons_idx = cons_id;
		bnxt_db_rx(bp, (u32)cons_id);
	}

	FLAG_SET(bp->flag_hwrm, VALID_RX_IOB);

	return STATUS_SUCCESS;
}

u8 bnxt_rx_drop(struct bnxt *bp, u8 *rx_buf, struct rx_pkt_cmpl_hi *rx_cmp_hi)
{
	u8  chksum_err = 0;
	u8  i;
	u16 error_flags;

	error_flags = (rx_cmp_hi->errors_v2 >>
		RX_PKT_CMPL_ERRORS_BUFFER_ERROR_SFT);
	if (rx_cmp_hi->errors_v2 == 0x20 || rx_cmp_hi->errors_v2 == 0x21)
		chksum_err = 1;

	if (error_flags && !chksum_err) {
		bp->rx.err++;
		return 1;
	}

	for (i = 0; i < 6; i++) {
		if (rx_buf[6 + i] != bp->mac_set[i])
			break;
	}

	if (i == 6) {
		bp->rx.dropped++;
		return 2; /* Drop the loopback packets */
	}

	return 0;
}

static void bnxt_adv_cq_index(struct bnxt *bp, u16 count)
{
	u16 cons_idx = bp->cq.cons_idx + count;

	if (cons_idx >= MAX_CQ_DESC_CNT) {
		/* Toggle completion bit when the ring wraps. */
		bp->cq.completion_bit ^= 1;
		cons_idx = cons_idx - MAX_CQ_DESC_CNT;
	}

	bp->cq.cons_idx = cons_idx;
}

void bnxt_adv_rx_index(struct bnxt *bp, u8 *iob, u32 iob_idx)
{
	u16 cons_id = (bp->rx.cons_idx % bp->rx.ring_cnt);

	set_rx_desc((u8 *)bp->rx.bd_virt, (void *)iob, cons_id, iob_idx);
	cons_id = NEXT_IDX(cons_id, bp->rx.ring_cnt);
	if (cons_id != bp->rx.cons_idx) {
		dbg_rx_cid(bp->rx.cons_idx, cons_id);
		bp->rx.cons_idx = cons_id;
		bnxt_db_rx(bp, (u32)cons_id);
	}
}

void rx_process(struct bnxt *bp, struct rx_pkt_cmpl *rx_cmp,
		struct rx_pkt_cmpl_hi *rx_cmp_hi)
{
	u32 desc_idx = rx_cmp->opaque;
	u8  *iob = bp->rx.iob[desc_idx];

	dump_rx_bd(rx_cmp, rx_cmp_hi, desc_idx);
	bp->rx.iob_len = rx_cmp->len;
	bp->rx.iob_rx  = iob;
	if (bnxt_rx_drop(bp, iob, rx_cmp_hi))
		bp->rx.iob_recv = PKT_DROPPED;
	else
		bp->rx.iob_recv = PKT_RECEIVED;

	bp->rx.rx_cnt++;

	dbg_rxp(bp->rx.iob_rx, bp->rx.iob_len, bp->rx.iob_recv);
	bnxt_adv_rx_index(bp, iob, desc_idx);
	bnxt_adv_cq_index(bp, 2); /* Rx completion is 2 entries. */
}

static int bnxt_rx_complete(struct bnxt *bp, struct rx_pkt_cmpl *rx_cmp)
{
	struct rx_pkt_cmpl_hi *rx_cmp_hi;
	u8  completion_bit = bp->cq.completion_bit;

	if (bp->cq.cons_idx == (bp->cq.ring_cnt - 1)) {
		rx_cmp_hi = (struct rx_pkt_cmpl_hi *)bp->cq.bd_virt;
		completion_bit ^= 0x1; /* Ring has wrapped. */
	} else {
		rx_cmp_hi = (struct rx_pkt_cmpl_hi *)(rx_cmp + 1);
	}

	if (!((rx_cmp_hi->errors_v2 & RX_PKT_CMPL_V2) ^ completion_bit))
		rx_process(bp, rx_cmp, rx_cmp_hi);

	return NO_MORE_CQ_BD_TO_SERVICE;
}

static int bnxt_hwrm_ver_get(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_ver_get_input);
	struct hwrm_ver_get_input *req;
	struct hwrm_ver_get_output *resp;
	int rc;

	req = (struct hwrm_ver_get_input *)bp->hwrm_addr_req;
	resp = (struct hwrm_ver_get_output *)bp->hwrm_addr_resp;
	hwrm_init(bp, (void *)req, (u16)HWRM_VER_GET, cmd_len);
	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
	req->hwrm_intf_min = HWRM_VERSION_MINOR;
	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
	rc = wait_resp(bp, HWRM_CMD_DEFAULT_TIMEOUT, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	bp->hwrm_spec_code =
		resp->hwrm_intf_maj_8b << 16 |
		resp->hwrm_intf_min_8b << 8 |
		resp->hwrm_intf_upd_8b;
	bp->hwrm_cmd_timeout = (u32)resp->def_req_timeout;
	if (!bp->hwrm_cmd_timeout)
		bp->hwrm_cmd_timeout = (u32)HWRM_CMD_DEFAULT_TIMEOUT;

	if (resp->hwrm_intf_maj_8b >= 1)
		bp->hwrm_max_req_len = resp->max_req_win_len;

	bp->chip_id =
		resp->chip_rev << 24 |
		resp->chip_metal << 16 |
		resp->chip_bond_id << 8 |
		resp->chip_platform_type;
	bp->chip_num = resp->chip_num;
	if ((resp->dev_caps_cfg & SHORT_CMD_SUPPORTED) &&
	    (resp->dev_caps_cfg & SHORT_CMD_REQUIRED))
		FLAG_SET(bp->flags, BNXT_FLAG_HWRM_SHORT_CMD_SUPP);

	bp->hwrm_max_ext_req_len = resp->max_ext_req_len;
	bp->fw_maj  = resp->hwrm_fw_maj_8b;
	bp->fw_min  = resp->hwrm_fw_min_8b;
	bp->fw_bld  = resp->hwrm_fw_bld_8b;
	bp->fw_rsvd = resp->hwrm_fw_rsvd_8b;
	print_fw_ver(resp, bp->hwrm_cmd_timeout);

	return STATUS_SUCCESS;
}

/* Broadcom ethernet driver Function HW cmds APIs. */
static int bnxt_hwrm_func_resource_qcaps(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_func_resource_qcaps_input);
	struct hwrm_func_resource_qcaps_input *req;
	struct hwrm_func_resource_qcaps_output *resp;
	int rc;

	req = (struct hwrm_func_resource_qcaps_input *)bp->hwrm_addr_req;
	resp = (struct hwrm_func_resource_qcaps_output *)bp->hwrm_addr_resp;
	hwrm_init(bp, (void *)req, (u16)HWRM_FUNC_RESOURCE_QCAPS, cmd_len);
	req->fid = (u16)HWRM_NA_SIGNATURE;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc != STATUS_SUCCESS)
		return STATUS_SUCCESS;

	FLAG_SET(bp->flags, BNXT_FLAG_RESOURCE_QCAPS_SUPPORT);
	/* VFs */
	bp->max_vfs = resp->max_vfs;
	bp->vf_res_strategy = resp->vf_reservation_strategy;
	/* vNICs */
	bp->min_vnics = resp->min_vnics;
	bp->max_vnics = resp->max_vnics;
	/* MSI-X */
	bp->max_msix = resp->max_msix;
	/* Ring Groups */
	bp->min_hw_ring_grps = resp->min_hw_ring_grps;
	bp->max_hw_ring_grps = resp->max_hw_ring_grps;
	/* TX Rings */
	bp->min_tx_rings = resp->min_tx_rings;
	bp->max_tx_rings = resp->max_tx_rings;
	/* RX Rings */
	bp->min_rx_rings = resp->min_rx_rings;
	bp->max_rx_rings = resp->max_rx_rings;
	/* Completion Rings */
	bp->min_cp_rings = resp->min_cmpl_rings;
	bp->max_cp_rings = resp->max_cmpl_rings;
	/* RSS Contexts */
	bp->min_rsscos_ctxs = resp->min_rsscos_ctx;
	bp->max_rsscos_ctxs = resp->max_rsscos_ctx;
	/* L2 Contexts */
	bp->min_l2_ctxs = resp->min_l2_ctxs;
	bp->max_l2_ctxs = resp->max_l2_ctxs;
	/* Statistic Contexts */
	bp->min_stat_ctxs = resp->min_stat_ctx;
	bp->max_stat_ctxs = resp->max_stat_ctx;
	dbg_func_resource_qcaps(bp);

	return STATUS_SUCCESS;
}

static u32 set_ring_info(struct bnxt *bp)
{
	u32 enables = 0;

	bp->num_cmpl_rings   = DEFAULT_NUMBER_OF_CMPL_RINGS;
	bp->num_tx_rings     = DEFAULT_NUMBER_OF_TX_RINGS;
	bp->num_rx_rings     = DEFAULT_NUMBER_OF_RX_RINGS;
	bp->num_hw_ring_grps = DEFAULT_NUMBER_OF_RING_GRPS;
	bp->num_stat_ctxs    = DEFAULT_NUMBER_OF_STAT_CTXS;
	if (bp->min_cp_rings <= DEFAULT_NUMBER_OF_CMPL_RINGS)
		bp->num_cmpl_rings = bp->min_cp_rings;

	if (bp->min_tx_rings <= DEFAULT_NUMBER_OF_TX_RINGS)
		bp->num_tx_rings = bp->min_tx_rings;

	if (bp->min_rx_rings <= DEFAULT_NUMBER_OF_RX_RINGS)
		bp->num_rx_rings = bp->min_rx_rings;

	if (bp->min_hw_ring_grps <= DEFAULT_NUMBER_OF_RING_GRPS)
		bp->num_hw_ring_grps = bp->min_hw_ring_grps;

	if (bp->min_stat_ctxs <= DEFAULT_NUMBER_OF_STAT_CTXS)
		bp->num_stat_ctxs = bp->min_stat_ctxs;

	print_num_rings(bp);
	enables = (FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
		FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS |
		FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS |
		FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS |
		FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS);

	return enables;
}

static void bnxt_hwrm_assign_resources(struct bnxt *bp)
{
	struct hwrm_func_cfg_input *req;
	u32 enables = 0;

	if (FLAG_TEST(bp->flags, BNXT_FLAG_RESOURCE_QCAPS_SUPPORT))
		enables = set_ring_info(bp);

	req = (struct hwrm_func_cfg_input *)bp->hwrm_addr_req;
	req->num_cmpl_rings   = bp->num_cmpl_rings;
	req->num_tx_rings     = bp->num_tx_rings;
	req->num_rx_rings     = bp->num_rx_rings;
	req->num_stat_ctxs    = bp->num_stat_ctxs;
	req->num_hw_ring_grps = bp->num_hw_ring_grps;
	req->enables = enables;
}

int bnxt_hwrm_nvm_flush(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_nvm_flush_input);
	struct hwrm_nvm_flush_input *req;
	int rc;

	req = (struct hwrm_nvm_flush_input *)bp->hwrm_addr_req;

	hwrm_init(bp, (void *)req, (u16)HWRM_NVM_FLUSH, cmd_len);

	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_func_qcaps_req(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_func_qcaps_input);
	struct hwrm_func_qcaps_input *req;
	struct hwrm_func_qcaps_output *resp;
	int rc;

	req = (struct hwrm_func_qcaps_input *)bp->hwrm_addr_req;
	resp = (struct hwrm_func_qcaps_output *)bp->hwrm_addr_resp;
	hwrm_init(bp, (void *)req, (u16)HWRM_FUNC_QCAPS, cmd_len);
	req->fid = (u16)HWRM_NA_SIGNATURE;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	bp->fid = resp->fid;
	bp->port_idx = (u8)resp->port_id;

	/* Get MAC address for this PF */
	memcpy(&bp->mac_addr[0], &resp->mac_address[0], ETH_ALEN);

	memcpy(&bp->mac_set[0], &bp->mac_addr[0], ETH_ALEN);

	print_func_qcaps(bp);

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_func_qcfg_req(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_func_qcfg_input);
	struct hwrm_func_qcfg_input *req;
	struct hwrm_func_qcfg_output *resp;
	int rc;

	req = (struct hwrm_func_qcfg_input *)bp->hwrm_addr_req;
	resp = (struct hwrm_func_qcfg_output *)bp->hwrm_addr_resp;
	hwrm_init(bp, (void *)req, (u16)HWRM_FUNC_QCFG, cmd_len);
	req->fid = (u16)HWRM_NA_SIGNATURE;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	if (resp->flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)
		FLAG_SET(bp->flags, BNXT_FLAG_MULTI_HOST);

	if (resp->port_partition_type &
		FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0)
		FLAG_SET(bp->flags, BNXT_FLAG_NPAR_MODE);

	bp->ordinal_value = (u8)resp->pci_id & 0x0F;
	bp->stat_ctx_id   = resp->stat_ctx_id;
	memcpy(&bp->mac_addr[0], &resp->mac_address[0], ETH_ALEN);
	print_func_qcfg(bp);
	dbg_flags(__func__, bp->flags);

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_func_reset_req(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_func_reset_input);
	struct hwrm_func_reset_input *req;

	req = (struct hwrm_func_reset_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_FUNC_RESET, cmd_len);
	req->func_reset_level = FUNC_RESET_REQ_FUNC_RESET_LEVEL_RESETME;

	return wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
}

static int bnxt_hwrm_func_cfg_req(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_func_cfg_input);
	struct hwrm_func_cfg_input *req;

	req = (struct hwrm_func_cfg_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_FUNC_CFG, cmd_len);
	req->fid = (u16)HWRM_NA_SIGNATURE;
	bnxt_hwrm_assign_resources(bp);

	return wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
}

static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_func_drv_rgtr_input);
	struct hwrm_func_drv_rgtr_input *req;
	int rc;

	req = (struct hwrm_func_drv_rgtr_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_FUNC_DRV_RGTR, cmd_len);
	/* Register with HWRM */
	req->enables = FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
			FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD |
			FUNC_DRV_RGTR_REQ_ENABLES_VER;
	req->async_event_fwd[0] |= 0x01;
	req->os_type = FUNC_DRV_RGTR_REQ_OS_TYPE_OTHER;
	req->ver_maj = DRIVER_VERSION_MAJOR;
	req->ver_min = DRIVER_VERSION_MINOR;
	req->ver_upd = DRIVER_VERSION_UPDATE;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	FLAG_SET(bp->flag_hwrm, VALID_DRIVER_REG);

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_func_drv_unrgtr_input);
	struct hwrm_func_drv_unrgtr_input *req;
	int rc;

	if (!(FLAG_TEST(bp->flag_hwrm, VALID_DRIVER_REG)))
		return STATUS_SUCCESS;

	req = (struct hwrm_func_drv_unrgtr_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_FUNC_DRV_UNRGTR, cmd_len);
	req->flags = FUNC_DRV_UNRGTR_REQ_FLAGS_PREPARE_FOR_SHUTDOWN;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	FLAG_RESET(bp->flag_hwrm, VALID_DRIVER_REG);

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_cfa_l2_filter_alloc(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_cfa_l2_filter_alloc_input);
	struct hwrm_cfa_l2_filter_alloc_input *req;
	struct hwrm_cfa_l2_filter_alloc_output *resp;
	int rc;
	u32 flags = CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX;
	u32 enables;

	req = (struct hwrm_cfa_l2_filter_alloc_input *)bp->hwrm_addr_req;
	resp = (struct hwrm_cfa_l2_filter_alloc_output *)bp->hwrm_addr_resp;
	enables = CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
		CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
		CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK;

	hwrm_init(bp, (void *)req, (u16)HWRM_CFA_L2_FILTER_ALLOC, cmd_len);
	req->flags      = flags;
	req->enables    = enables;
	memcpy((char *)&req->l2_addr[0], (char *)&bp->mac_set[0], ETH_ALEN);
	memset((char *)&req->l2_addr_mask[0], 0xff, ETH_ALEN);
	memcpy((char *)&req->t_l2_addr[0], (char *)&bp->mac_set[0], ETH_ALEN);
	memset((char *)&req->t_l2_addr_mask[0], 0xff, ETH_ALEN);
	req->src_type = CFA_L2_FILTER_ALLOC_REQ_SRC_TYPE_NPORT;
	req->src_id   = (u32)bp->port_idx;
	req->dst_id   = bp->vnic_id;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	FLAG_SET(bp->flag_hwrm, VALID_L2_FILTER);
	bp->l2_filter_id = resp->l2_filter_id;

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_cfa_l2_filter_free(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_cfa_l2_filter_free_input);
	struct hwrm_cfa_l2_filter_free_input *req;
	int rc;

	if (!(FLAG_TEST(bp->flag_hwrm, VALID_L2_FILTER)))
		return STATUS_SUCCESS;

	req = (struct hwrm_cfa_l2_filter_free_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_CFA_L2_FILTER_FREE, cmd_len);
	req->l2_filter_id = bp->l2_filter_id;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	FLAG_RESET(bp->flag_hwrm, VALID_L2_FILTER);

	return STATUS_SUCCESS;
}

u32 bnxt_set_rx_mask(u32 rx_mask)
{
	u32 mask = 0;

	if (!rx_mask)
		return mask;
	mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
	if (rx_mask != RX_MASK_ACCEPT_NONE) {
		if (rx_mask & RX_MASK_ACCEPT_MULTICAST)
			mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;

		if (rx_mask & RX_MASK_ACCEPT_ALL_MULTICAST)
			mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;

		if (rx_mask & RX_MASK_PROMISCUOUS_MODE)
			mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
	}

	return mask;
}

static int bnxt_hwrm_set_rx_mask(struct bnxt *bp, u32 rx_mask)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_cfa_l2_set_rx_mask_input);
	struct hwrm_cfa_l2_set_rx_mask_input *req;
	u32 mask = bnxt_set_rx_mask(rx_mask);

	req = (struct hwrm_cfa_l2_set_rx_mask_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_CFA_L2_SET_RX_MASK, cmd_len);
	req->vnic_id = bp->vnic_id;
	req->mask    = mask;

	return wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
}

static int bnxt_hwrm_port_mac_cfg(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_port_mac_cfg_input);
	struct hwrm_port_mac_cfg_input *req;

	req = (struct hwrm_port_mac_cfg_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_PORT_MAC_CFG, cmd_len);
	req->lpbk = PORT_MAC_CFG_REQ_LPBK_NONE;

	return wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
}

static int bnxt_hwrm_port_phy_qcfg(struct bnxt *bp, u16 idx)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_port_phy_qcfg_input);
	struct hwrm_port_phy_qcfg_input *req;
	struct hwrm_port_phy_qcfg_output *resp;
	int rc;

	req = (struct hwrm_port_phy_qcfg_input *)bp->hwrm_addr_req;
	resp = (struct hwrm_port_phy_qcfg_output *)bp->hwrm_addr_resp;
	hwrm_init(bp, (void *)req, (u16)HWRM_PORT_PHY_QCFG, cmd_len);
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	if (idx & SUPPORT_SPEEDS)
		bp->support_speeds = resp->support_speeds;

	if (idx & DETECT_MEDIA)
		bp->media_detect = resp->module_status;

	if (idx & PHY_SPEED)
		bp->current_link_speed = resp->link_speed;

	if (idx & PHY_STATUS) {
		if (resp->link == PORT_PHY_QCFG_RESP_LINK_LINK)
			bp->link_status = STATUS_LINK_ACTIVE;
		else
			bp->link_status = STATUS_LINK_DOWN;
	}

	return STATUS_SUCCESS;
}

u16 set_link_speed_mask(u16 link_cap)
{
	u16 speed_mask = 0;

	if (link_cap & SPEED_CAPABILITY_DRV_100M)
		speed_mask |= PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_MASK_100MB;

	if (link_cap & SPEED_CAPABILITY_DRV_1G)
		speed_mask |= PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_MASK_1GB;

	if (link_cap & SPEED_CAPABILITY_DRV_10G)
		speed_mask |= PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_MASK_10GB;

	if (link_cap & SPEED_CAPABILITY_DRV_25G)
		speed_mask |= PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_MASK_25GB;

	if (link_cap & SPEED_CAPABILITY_DRV_40G)
		speed_mask |= PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_MASK_40GB;

	if (link_cap & SPEED_CAPABILITY_DRV_50G)
		speed_mask |= PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_MASK_50GB;

	if (link_cap & SPEED_CAPABILITY_DRV_100G)
		speed_mask |= PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_MASK_100GB;

	return speed_mask;
}

static int bnxt_hwrm_port_phy_cfg(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_port_phy_cfg_input);
	struct hwrm_port_phy_cfg_input *req;
	u32 flags;
	u32 enables = 0;
	u16 force_link_speed = 0;
	u16 auto_link_speed_mask = 0;
	u8  auto_mode = 0;
	u8  auto_pause = 0;
	u8  auto_duplex = 0;

	/*
	 * If multi_host or NPAR is set to TRUE,
	 * do not issue hwrm_port_phy_cfg
	 */
	if (FLAG_TEST(bp->flags, PORT_PHY_FLAGS)) {
		dbg_flags(__func__, bp->flags);
		return STATUS_SUCCESS;
	}

	req = (struct hwrm_port_phy_cfg_input *)bp->hwrm_addr_req;
	flags = PORT_PHY_CFG_REQ_FLAGS_FORCE |
		PORT_PHY_CFG_REQ_FLAGS_RESET_PHY;

	switch (GET_MEDIUM_SPEED(bp->medium)) {
	case MEDIUM_SPEED_1000MBPS:
		force_link_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB;
		break;
	case MEDIUM_SPEED_10GBPS:
		force_link_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB;
		break;
	case MEDIUM_SPEED_25GBPS:
		force_link_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB;
		break;
	case MEDIUM_SPEED_40GBPS:
		force_link_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB;
		break;
	case MEDIUM_SPEED_50GBPS:
		force_link_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB;
		break;
	case MEDIUM_SPEED_100GBPS:
		force_link_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_100GB;
		break;
	default:
		/* Enable AUTONEG by default */
		auto_mode = PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
		flags &= ~PORT_PHY_CFG_REQ_FLAGS_FORCE;
		enables |= PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE |
			PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK |
			PORT_PHY_CFG_REQ_ENABLES_AUTO_DUPLEX |
			PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE;
		auto_pause = PORT_PHY_CFG_REQ_AUTO_PAUSE_TX |
				PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
		auto_duplex = PORT_PHY_CFG_REQ_AUTO_DUPLEX_BOTH;
		auto_link_speed_mask = bp->support_speeds;
		break;
	}

	hwrm_init(bp, (void *)req, (u16)HWRM_PORT_PHY_CFG, cmd_len);
	req->flags                = flags;
	req->enables              = enables;
	req->port_id              = bp->port_idx;
	req->force_link_speed     = force_link_speed;
	req->auto_mode            = auto_mode;
	req->auto_duplex          = auto_duplex;
	req->auto_pause           = auto_pause;
	req->auto_link_speed_mask = auto_link_speed_mask;

	return wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
}

static int bnxt_qphy_link(struct bnxt *bp)
{
	u16 flag = QCFG_PHY_ALL;

	/* Query Link Status */
	if (bnxt_hwrm_port_phy_qcfg(bp, flag) != STATUS_SUCCESS)
		return STATUS_FAILURE;

	if (bp->link_status != STATUS_LINK_ACTIVE) {
		/*
		 * Configure link if it is not up.
		 * try to bring link up, but don't return
		 * failure if port_phy_cfg() fails
		 */
		bnxt_hwrm_port_phy_cfg(bp);
		/* refresh link speed values after bringing link up */
		if (bnxt_hwrm_port_phy_qcfg(bp, flag) != STATUS_SUCCESS)
			return STATUS_FAILURE;
	}

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_stat_ctx_alloc_input);
	struct hwrm_stat_ctx_alloc_input *req;
	struct hwrm_stat_ctx_alloc_output *resp;
	int rc;

	req = (struct hwrm_stat_ctx_alloc_input *)bp->hwrm_addr_req;
	resp = (struct hwrm_stat_ctx_alloc_output *)bp->hwrm_addr_resp;
	hwrm_init(bp, (void *)req, (u16)HWRM_STAT_CTX_ALLOC, cmd_len);
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	FLAG_SET(bp->flag_hwrm, VALID_STAT_CTX);
	bp->stat_ctx_id = (u16)resp->stat_ctx_id;

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_stat_ctx_free_input);
	struct hwrm_stat_ctx_free_input *req;
	int rc;

	if (!(FLAG_TEST(bp->flag_hwrm, VALID_STAT_CTX)))
		return STATUS_SUCCESS;

	req = (struct hwrm_stat_ctx_free_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_STAT_CTX_FREE, cmd_len);
	req->stat_ctx_id = (u32)bp->stat_ctx_id;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	FLAG_RESET(bp->flag_hwrm, VALID_STAT_CTX);

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_ring_free_grp(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_ring_grp_free_input);
	struct hwrm_ring_grp_free_input *req;
	int rc;

	if (!(FLAG_TEST(bp->flag_hwrm, VALID_RING_GRP)))
		return STATUS_SUCCESS;

	req = (struct hwrm_ring_grp_free_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_RING_GRP_FREE, cmd_len);
	req->ring_group_id = (u32)bp->ring_grp_id;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	FLAG_RESET(bp->flag_hwrm, VALID_RING_GRP);

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_ring_alloc_grp(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_ring_grp_alloc_input);
	struct hwrm_ring_grp_alloc_input *req;
	struct hwrm_ring_grp_alloc_output *resp;
	int rc;

	req = (struct hwrm_ring_grp_alloc_input *)bp->hwrm_addr_req;
	resp = (struct hwrm_ring_grp_alloc_output *)bp->hwrm_addr_resp;
	hwrm_init(bp, (void *)req, (u16)HWRM_RING_GRP_ALLOC, cmd_len);
	req->cr = bp->cq_ring_id;
	req->rr = bp->rx_ring_id;
	req->ar = (u16)HWRM_NA_SIGNATURE;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	FLAG_SET(bp->flag_hwrm, VALID_RING_GRP);
	bp->ring_grp_id = (u16)resp->ring_group_id;

	return STATUS_SUCCESS;
}

int bnxt_hwrm_ring_free(struct bnxt *bp, u16 ring_id, u8 ring_type)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_ring_free_input);
	struct hwrm_ring_free_input *req;

	req = (struct hwrm_ring_free_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_RING_FREE, cmd_len);
	req->ring_type = ring_type;
	req->ring_id   = ring_id;

	return wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
}

static int bnxt_hwrm_ring_alloc(struct bnxt *bp,
				dma_addr_t ring_map,
				u16 length,
				u16 ring_id,
				u8 ring_type,
				u8 int_mode)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_ring_alloc_input);
	struct hwrm_ring_alloc_input *req;
	struct hwrm_ring_alloc_output *resp;
	int rc;

	req = (struct hwrm_ring_alloc_input *)bp->hwrm_addr_req;
	resp = (struct hwrm_ring_alloc_output *)bp->hwrm_addr_resp;
	hwrm_init(bp, (void *)req, (u16)HWRM_RING_ALLOC, cmd_len);
	req->ring_type     = ring_type;
	req->page_tbl_addr = ring_map;
	req->page_size     = LM_PAGE_SIZE;
	req->length        = (u32)length;
	req->cmpl_ring_id  = ring_id;
	req->int_mode      = int_mode;
	if (ring_type == RING_ALLOC_REQ_RING_TYPE_TX) {
		req->queue_id    = TX_RING_QID;
	} else if (ring_type == RING_ALLOC_REQ_RING_TYPE_RX) {
		req->queue_id    = RX_RING_QID;
		req->enables     = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID;
		req->rx_buf_size = MAX_ETHERNET_PACKET_BUFFER_SIZE;
	}

	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	if (ring_type == RING_ALLOC_REQ_RING_TYPE_L2_CMPL) {
		FLAG_SET(bp->flag_hwrm, VALID_RING_CQ);
		bp->cq_ring_id = resp->ring_id;
	} else if (ring_type == RING_ALLOC_REQ_RING_TYPE_TX) {
		FLAG_SET(bp->flag_hwrm, VALID_RING_TX);
		bp->tx_ring_id = resp->ring_id;
	} else if (ring_type == RING_ALLOC_REQ_RING_TYPE_RX) {
		FLAG_SET(bp->flag_hwrm, VALID_RING_RX);
		bp->rx_ring_id = resp->ring_id;
	}

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_ring_alloc_cq(struct bnxt *bp)
{
	return bnxt_hwrm_ring_alloc(bp,
				    virt_to_bus(bp->cq.bd_virt),
				    bp->cq.ring_cnt,
				    0,
				    RING_ALLOC_REQ_RING_TYPE_L2_CMPL,
				    BNXT_CQ_INTR_MODE());
}

static int bnxt_hwrm_ring_alloc_tx(struct bnxt *bp)
{
	return bnxt_hwrm_ring_alloc(bp,
				    virt_to_bus(bp->tx.bd_virt),
				    bp->tx.ring_cnt, bp->cq_ring_id,
				    RING_ALLOC_REQ_RING_TYPE_TX,
				    BNXT_INTR_MODE());
}

static int bnxt_hwrm_ring_alloc_rx(struct bnxt *bp)
{
	return bnxt_hwrm_ring_alloc(bp,
				    virt_to_bus(bp->rx.bd_virt),
				    bp->rx.ring_cnt,
				    bp->cq_ring_id,
				    RING_ALLOC_REQ_RING_TYPE_RX,
				    BNXT_INTR_MODE());
}

static int bnxt_hwrm_ring_free_cq(struct bnxt *bp)
{
	int ret = STATUS_SUCCESS;

	if (!(FLAG_TEST(bp->flag_hwrm, VALID_RING_CQ)))
		return ret;

	ret = RING_FREE(bp, bp->cq_ring_id, RING_FREE_REQ_RING_TYPE_L2_CMPL);
	if (ret == STATUS_SUCCESS)
		FLAG_RESET(bp->flag_hwrm, VALID_RING_CQ);

	return ret;
}

static int bnxt_hwrm_ring_free_tx(struct bnxt *bp)
{
	int ret = STATUS_SUCCESS;

	if (!(FLAG_TEST(bp->flag_hwrm, VALID_RING_TX)))
		return ret;

	ret = RING_FREE(bp, bp->tx_ring_id, RING_FREE_REQ_RING_TYPE_TX);
	if (ret == STATUS_SUCCESS)
		FLAG_RESET(bp->flag_hwrm, VALID_RING_TX);

	return ret;
}

static int bnxt_hwrm_ring_free_rx(struct bnxt *bp)
{
	int ret = STATUS_SUCCESS;

	if (!(FLAG_TEST(bp->flag_hwrm, VALID_RING_RX)))
		return ret;

	ret = RING_FREE(bp, bp->rx_ring_id, RING_FREE_REQ_RING_TYPE_RX);
	if (ret == STATUS_SUCCESS)
		FLAG_RESET(bp->flag_hwrm, VALID_RING_RX);

	return ret;
}

static int bnxt_hwrm_vnic_alloc(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_vnic_alloc_input);
	struct hwrm_vnic_alloc_input *req;
	struct hwrm_vnic_alloc_output *resp;
	int rc;

	req = (struct hwrm_vnic_alloc_input *)bp->hwrm_addr_req;
	resp = (struct hwrm_vnic_alloc_output *)bp->hwrm_addr_resp;
	hwrm_init(bp, (void *)req, (u16)HWRM_VNIC_ALLOC, cmd_len);
	req->flags = VNIC_ALLOC_REQ_FLAGS_DEFAULT;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	FLAG_SET(bp->flag_hwrm, VALID_VNIC_ID);
	bp->vnic_id = resp->vnic_id;

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_vnic_free(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_vnic_free_input);
	struct hwrm_vnic_free_input *req;
	int rc;

	if (!(FLAG_TEST(bp->flag_hwrm, VALID_VNIC_ID)))
		return STATUS_SUCCESS;

	req = (struct hwrm_vnic_free_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_VNIC_FREE, cmd_len);
	req->vnic_id = bp->vnic_id;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
	if (rc)
		return STATUS_FAILURE;

	FLAG_RESET(bp->flag_hwrm, VALID_VNIC_ID);

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_vnic_cfg(struct bnxt *bp)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_vnic_cfg_input);
	struct hwrm_vnic_cfg_input *req;

	req = (struct hwrm_vnic_cfg_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_VNIC_CFG, cmd_len);
	req->enables = VNIC_CFG_REQ_ENABLES_MRU;
	req->mru     = bp->mtu;
	req->enables |= VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP;
	req->dflt_ring_grp = bp->ring_grp_id;
	req->vnic_id = bp->vnic_id;

	return wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);
}

static int set_phy_speed(struct bnxt *bp)
{
	char name[20];
	u16 flag = PHY_STATUS | PHY_SPEED | DETECT_MEDIA;

	/* Query Link Status */
	if (bnxt_hwrm_port_phy_qcfg(bp, flag) != STATUS_SUCCESS)
		return STATUS_FAILURE;

	switch (bp->current_link_speed) {
	case PORT_PHY_QCFG_RESP_LINK_SPEED_100GB:
		sprintf(name, "%s %s", str_100, str_gbps);
		break;
	case PORT_PHY_QCFG_RESP_LINK_SPEED_50GB:
		sprintf(name, "%s %s", str_50, str_gbps);
		break;
	case PORT_PHY_QCFG_RESP_LINK_SPEED_40GB:
		sprintf(name, "%s %s", str_40, str_gbps);
		break;
	case PORT_PHY_QCFG_RESP_LINK_SPEED_25GB:
		sprintf(name, "%s %s", str_25, str_gbps);
		break;
	case PORT_PHY_QCFG_RESP_LINK_SPEED_20GB:
		sprintf(name, "%s %s", str_20, str_gbps);
		break;
	case PORT_PHY_QCFG_RESP_LINK_SPEED_10GB:
		sprintf(name, "%s %s", str_10, str_gbps);
		break;
	case PORT_PHY_QCFG_RESP_LINK_SPEED_2_5GB:
		sprintf(name, "%s %s", str_2_5, str_gbps);
		break;
	case PORT_PHY_QCFG_RESP_LINK_SPEED_2GB:
		sprintf(name, "%s %s", str_2, str_gbps);
		break;
	case PORT_PHY_QCFG_RESP_LINK_SPEED_1GB:
		sprintf(name, "%s %s", str_1, str_gbps);
		break;
	case PORT_PHY_QCFG_RESP_LINK_SPEED_100MB:
		sprintf(name, "%s %s", str_100, str_mbps);
		break;
	case PORT_PHY_QCFG_RESP_LINK_SPEED_10MB:
		sprintf(name, "%s %s", str_10, str_mbps);
		break;
	default:
		sprintf(name, "%s %x", str_unknown, bp->current_link_speed);
	}

	dbg_phy_speed(bp, name);

	return STATUS_SUCCESS;
}

static int set_phy_link(struct bnxt *bp, u32 tmo)
{
	int ret;

	set_phy_speed(bp);
	dbg_link_status(bp);
	ret = STATUS_FAILURE;
	if (bp->link_status == STATUS_LINK_ACTIVE) {
		dbg_link_state(bp, tmo);
		ret = STATUS_SUCCESS;
	}

	return ret;
}

static int get_phy_link(struct bnxt *bp)
{
	u16 flag = PHY_STATUS | PHY_SPEED | DETECT_MEDIA;

	dbg_chip_info(bp);
	/* Query Link Status */
	if (bnxt_hwrm_port_phy_qcfg(bp, flag) != STATUS_SUCCESS)
		return STATUS_FAILURE;

	set_phy_link(bp, 100);

	return STATUS_SUCCESS;
}

static int bnxt_hwrm_set_async_event(struct bnxt *bp)
{
	int rc;
	u16 cmd_len = (u16)sizeof(struct hwrm_func_cfg_input);
	struct hwrm_func_cfg_input *req;

	req = (struct hwrm_func_cfg_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_FUNC_CFG, cmd_len);
	req->fid = (u16)HWRM_NA_SIGNATURE;
	req->enables = FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR;
	req->async_event_cr = bp->cq_ring_id;
	rc = wait_resp(bp, bp->hwrm_cmd_timeout, cmd_len, __func__);

	return rc;
}

int bnxt_hwrm_get_nvmem(struct bnxt *bp,
			u16 data_len,
			u16 option_num,
			u16 dimensions,
			u16 index_0)
{
	u16 cmd_len = (u16)sizeof(struct hwrm_nvm_get_variable_input);
	struct hwrm_nvm_get_variable_input *req;

	req = (struct hwrm_nvm_get_variable_input *)bp->hwrm_addr_req;
	hwrm_init(bp, (void *)req, (u16)HWRM_NVM_GET_VARIABLE, cmd_len);
	req->dest_data_addr = bp->data_addr_mapping;
	req->data_len       = data_len;
	req->option_num     = option_num;
	req->dimensions     = dimensions;
	req->index_0        = index_0;

	return wait_resp(bp,
			 HWRM_CMD_FLASH_MULTIPLAYER(bp->hwrm_cmd_timeout),
			 cmd_len,
			 __func__);
}

static void set_medium(struct bnxt *bp)
{
	switch (bp->link_set & LINK_SPEED_DRV_MASK) {
	case LINK_SPEED_DRV_1G:
		bp->medium = SET_MEDIUM_SPEED(bp, MEDIUM_SPEED_1000MBPS);
		break;
	case LINK_SPEED_DRV_2_5G:
		bp->medium = SET_MEDIUM_SPEED(bp, MEDIUM_SPEED_2500MBPS);
		break;
	case LINK_SPEED_DRV_10G:
		bp->medium = SET_MEDIUM_SPEED(bp, MEDIUM_SPEED_10GBPS);
		break;
	case LINK_SPEED_DRV_25G:
		bp->medium = SET_MEDIUM_SPEED(bp, MEDIUM_SPEED_25GBPS);
		break;
	case LINK_SPEED_DRV_40G:
		bp->medium = SET_MEDIUM_SPEED(bp, MEDIUM_SPEED_40GBPS);
		break;
	case LINK_SPEED_DRV_50G:
		bp->medium = SET_MEDIUM_SPEED(bp, MEDIUM_SPEED_50GBPS);
		break;
	case LINK_SPEED_DRV_100G:
		bp->medium = SET_MEDIUM_SPEED(bp, MEDIUM_SPEED_100GBPS);
		break;
	case LINK_SPEED_DRV_200G:
		bp->medium = SET_MEDIUM_SPEED(bp, MEDIUM_SPEED_200GBPS);
		break;
	case LINK_SPEED_DRV_AUTONEG:
		bp->medium = SET_MEDIUM_SPEED(bp, MEDIUM_SPEED_AUTONEG);
		break;
	default:
		bp->medium = SET_MEDIUM_DUPLEX(bp, MEDIUM_FULL_DUPLEX);
		break;
	}
}

static int bnxt_hwrm_get_link_speed(struct bnxt *bp)
{
	u32 *ptr32 = (u32 *)bp->hwrm_addr_data;

	if (bnxt_hwrm_get_nvmem(bp,
				4,
				(u16)LINK_SPEED_DRV_NUM,
				1,
				(u16)bp->port_idx) != STATUS_SUCCESS)
		return STATUS_FAILURE;

	bp->link_set  = *ptr32;
	bp->link_set &= SPEED_DRV_MASK;
	set_medium(bp);

	return STATUS_SUCCESS;
}

typedef int (*hwrm_func_t)(struct bnxt *bp);

hwrm_func_t down_chip[] = {
	bnxt_hwrm_cfa_l2_filter_free,    /* Free l2 filter  */
	bnxt_free_rx_iob,                /* Free rx iob     */
	bnxt_hwrm_vnic_free,             /* Free vnic       */
	bnxt_hwrm_ring_free_grp,         /* Free ring group */
	bnxt_hwrm_ring_free_rx,          /* Free rx ring    */
	bnxt_hwrm_ring_free_tx,          /* Free tx ring    */
	bnxt_hwrm_ring_free_cq,          /* Free CQ ring    */
	bnxt_hwrm_stat_ctx_free,         /* Free Stat ctx   */
	bnxt_hwrm_func_drv_unrgtr,       /* unreg driver    */
	NULL,
};

hwrm_func_t bring_chip[] = {
	bnxt_hwrm_ver_get,              /* HWRM_VER_GET                 */
	bnxt_hwrm_func_reset_req,       /* HWRM_FUNC_RESET              */
	bnxt_hwrm_func_drv_rgtr,        /* HWRM_FUNC_DRV_RGTR           */
	bnxt_hwrm_func_resource_qcaps,  /* HWRM_FUNC_RESOURCE_QCAPS     */
	bnxt_hwrm_func_qcfg_req,        /* HWRM_FUNC_QCFG               */
	bnxt_hwrm_func_qcaps_req,       /* HWRM_FUNC_QCAPS              */
	bnxt_hwrm_get_link_speed,       /* HWRM_NVM_GET_VARIABLE - 203  */
	bnxt_hwrm_port_mac_cfg,         /* HWRM_PORT_MAC_CFG            */
	bnxt_qphy_link,                 /* HWRM_PORT_PHY_QCFG           */
	bnxt_hwrm_func_cfg_req,         /* HWRM_FUNC_CFG - ring resource*/
	bnxt_hwrm_stat_ctx_alloc,       /* Allocate Stat Ctx ID         */
	bnxt_hwrm_ring_alloc_cq,        /* Allocate CQ Ring             */
	bnxt_hwrm_ring_alloc_tx,        /* Allocate Tx ring             */
	bnxt_hwrm_ring_alloc_rx,        /* Allocate Rx Ring             */
	bnxt_hwrm_ring_alloc_grp,       /* Create Ring Group            */
	post_rx_buffers,                /* Post RX buffers              */
	bnxt_hwrm_set_async_event,      /* ENABLES_ASYNC_EVENT_CR       */
	bnxt_hwrm_vnic_alloc,           /* Alloc VNIC                   */
	bnxt_hwrm_vnic_cfg,             /* Config VNIC                  */
	bnxt_hwrm_cfa_l2_filter_alloc,  /* Alloc L2 Filter              */
	get_phy_link,                   /* Get Physical Link            */
	NULL,
};

int bnxt_hwrm_run(hwrm_func_t cmds[], struct bnxt *bp, int flag)
{
	hwrm_func_t *ptr;
	int ret;
	int status = STATUS_SUCCESS;

	for (ptr = cmds; *ptr; ++ptr) {
		ret = (*ptr)(bp);
		if (ret) {
			status = STATUS_FAILURE;
			/* Continue till all cleanup routines are called */
			if (flag)
				return STATUS_FAILURE;
		}
	}

	return status;
}

/* Broadcom ethernet driver Network interface APIs. */
static int bnxt_start(struct udevice *dev)
{
	struct bnxt *bp = dev_get_priv(dev);

	if (bnxt_hwrm_set_rx_mask(bp, RX_MASK) != STATUS_SUCCESS)
		return STATUS_FAILURE;

	bp->card_en = true;
	return STATUS_SUCCESS;
}

static int bnxt_send(struct udevice *dev, void *packet, int length)
{
	struct bnxt *bp = dev_get_priv(dev);
	int len;
	u16 entry;
	dma_addr_t mapping;

	if (bnxt_tx_avail(bp) < 1) {
		dbg_no_tx_bd();
		return -ENOBUFS;
	}

	entry   = bp->tx.prod_id;
	len     = iob_pad(packet, length);
	mapping = virt_to_bus(packet);
	set_txq(bp, entry, mapping, len);
	entry   = NEXT_IDX(entry, bp->tx.ring_cnt);
	dump_tx_pkt(packet, mapping, len);
	bnxt_db_tx(bp, (u32)entry);
	bp->tx.prod_id = entry;
	bp->tx.cnt_req++;
	bnxt_tx_complete(bp);

	return 0;
}

static void bnxt_link_evt(struct bnxt *bp, struct cmpl_base *cmp)
{
	struct hwrm_async_event_cmpl *evt;

	evt = (struct hwrm_async_event_cmpl *)cmp;
	switch (evt->event_id) {
	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
		if (evt->event_data1 & 0x01)
			bp->link_status = STATUS_LINK_ACTIVE;
		else
			bp->link_status = STATUS_LINK_DOWN;

		set_phy_link(bp, 0);
		break;
	default:
		break;
	}
}

static int bnxt_recv(struct udevice *dev, int flags, uchar **packetp)
{
	struct bnxt *bp = dev_get_priv(dev);
	struct cmpl_base *cmp;
	u16 old_cons_idx = bp->cq.cons_idx;
	int done = SERVICE_NEXT_CQ_BD;
	u32 cq_type;

	while (done == SERVICE_NEXT_CQ_BD) {
		cmp = (struct cmpl_base *)BD_NOW(bp->cq.bd_virt,
			bp->cq.cons_idx,
			sizeof(struct cmpl_base));
		if ((cmp->info3_v & CMPL_BASE_V) ^ bp->cq.completion_bit)
			break;

		cq_type = cmp->type & CMPL_BASE_TYPE_MASK;
		dump_evt((u8 *)cmp, cq_type, bp->cq.cons_idx);
		dump_CQ(cmp, bp->cq.cons_idx);
		switch (cq_type) {
		case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
			bnxt_link_evt(bp, cmp);
			fallthrough;
		case CMPL_BASE_TYPE_TX_L2:
		case CMPL_BASE_TYPE_STAT_EJECT:
			bnxt_adv_cq_index(bp, 1);
			break;
		case CMPL_BASE_TYPE_RX_L2:
			done = bnxt_rx_complete(bp, (struct rx_pkt_cmpl *)cmp);
			break;
		default:
			done = NO_MORE_CQ_BD_TO_SERVICE;
			break;
		}
	}

	if (bp->cq.cons_idx != old_cons_idx)
		bnxt_db_cq(bp);

	if (bp->rx.iob_recv == PKT_RECEIVED) {
		*packetp = bp->rx.iob_rx;
		return bp->rx.iob_len;
	}

	return -EAGAIN;
}

static void bnxt_stop(struct udevice *dev)
{
	struct bnxt *bp = dev_get_priv(dev);

	if (bp->card_en) {
		bnxt_hwrm_set_rx_mask(bp, 0);
		bp->card_en = false;
	}
}

static int bnxt_free_pkt(struct udevice *dev, uchar *packet, int length)
{
	struct bnxt *bp = dev_get_priv(dev);

	dbg_rx_pkt(bp, __func__, packet, length);
	bp->rx.iob_recv = PKT_DONE;
	bp->rx.iob_len  = 0;
	bp->rx.iob_rx   = NULL;

	return 0;
}

static int bnxt_read_rom_hwaddr(struct udevice *dev)
{
	struct eth_pdata *plat = dev_get_plat(dev);
	struct bnxt *bp = dev_get_priv(dev);

	memcpy(plat->enetaddr, bp->mac_set, ETH_ALEN);

	return 0;
}

static const struct eth_ops bnxt_eth_ops = {
	.start           = bnxt_start,
	.send            = bnxt_send,
	.recv            = bnxt_recv,
	.stop            = bnxt_stop,
	.free_pkt        = bnxt_free_pkt,
	.read_rom_hwaddr = bnxt_read_rom_hwaddr,
};

static const struct udevice_id bnxt_eth_ids[] = {
	{ .compatible = "broadcom,nxe" },
	{ }
};

static int bnxt_eth_bind(struct udevice *dev)
{
	char name[20];

	sprintf(name, "bnxt_eth%u", dev_seq(dev));

	return device_set_name(dev, name);
}

static int bnxt_eth_probe(struct udevice *dev)
{
	struct bnxt *bp = dev_get_priv(dev);
	int ret;

	ret = bnxt_alloc_mem(bp);
	if (ret) {
		printf("*** error: bnxt_alloc_mem failed! ***\n");
		return ret;
	}

	bp->cardnum = dev_seq(dev);
	bp->name = dev->name;
	bp->pdev = (struct udevice *)dev;

	bnxt_bring_pci(bp);

	ret = bnxt_bring_chip(bp);
	if (ret) {
		printf("*** error: bnxt_bring_chip failed! ***\n");
		return -ENODATA;
	}

	return 0;
}

static int bnxt_eth_remove(struct udevice *dev)
{
	struct bnxt *bp = dev_get_priv(dev);

	bnxt_down_chip(bp);
	bnxt_free_mem(bp);

	return 0;
}

static struct pci_device_id bnxt_nics[] = {
	{PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_NXT_57320)},
	{}
};

U_BOOT_DRIVER(eth_bnxt) = {
	.name                     = "eth_bnxt",
	.id                       = UCLASS_ETH,
	.of_match                 = bnxt_eth_ids,
	.bind                     = bnxt_eth_bind,
	.probe                    = bnxt_eth_probe,
	.remove                   = bnxt_eth_remove,
	.ops                      = &bnxt_eth_ops,
	.priv_auto                = sizeof(struct bnxt),
	.plat_auto                = sizeof(struct eth_pdata),
	.flags                    = DM_FLAG_ACTIVE_DMA,
};

U_BOOT_PCI_DEVICE(eth_bnxt, bnxt_nics);
