162306a36Sopenharmony_ci/* Broadcom NetXtreme-C/E network driver.
262306a36Sopenharmony_ci *
362306a36Sopenharmony_ci * Copyright (c) 2020 Broadcom Limited
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
662306a36Sopenharmony_ci * it under the terms of the GNU General Public License as published by
762306a36Sopenharmony_ci * the Free Software Foundation.
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <asm/byteorder.h>
1162306a36Sopenharmony_ci#include <linux/dma-mapping.h>
1262306a36Sopenharmony_ci#include <linux/dmapool.h>
1362306a36Sopenharmony_ci#include <linux/errno.h>
1462306a36Sopenharmony_ci#include <linux/ethtool.h>
1562306a36Sopenharmony_ci#include <linux/if_ether.h>
1662306a36Sopenharmony_ci#include <linux/io.h>
1762306a36Sopenharmony_ci#include <linux/irq.h>
1862306a36Sopenharmony_ci#include <linux/kernel.h>
1962306a36Sopenharmony_ci#include <linux/list.h>
2062306a36Sopenharmony_ci#include <linux/netdevice.h>
2162306a36Sopenharmony_ci#include <linux/pci.h>
2262306a36Sopenharmony_ci#include <linux/skbuff.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include "bnxt_hsi.h"
2562306a36Sopenharmony_ci#include "bnxt.h"
2662306a36Sopenharmony_ci#include "bnxt_hwrm.h"
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_cistatic u64 hwrm_calc_sentinel(struct bnxt_hwrm_ctx *ctx, u16 req_type)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	return (((uintptr_t)ctx) + req_type) ^ BNXT_HWRM_SENTINEL;
3162306a36Sopenharmony_ci}
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/**
3462306a36Sopenharmony_ci * __hwrm_req_init() - Initialize an HWRM request.
3562306a36Sopenharmony_ci * @bp: The driver context.
3662306a36Sopenharmony_ci * @req: A pointer to the request pointer to initialize.
3762306a36Sopenharmony_ci * @req_type: The request type. This will be converted to the little endian
3862306a36Sopenharmony_ci *	before being written to the req_type field of the returned request.
3962306a36Sopenharmony_ci * @req_len: The length of the request to be allocated.
4062306a36Sopenharmony_ci *
4162306a36Sopenharmony_ci * Allocate DMA resources and initialize a new HWRM request object of the
4262306a36Sopenharmony_ci * given type. The response address field in the request is configured with
4362306a36Sopenharmony_ci * the DMA bus address that has been mapped for the response and the passed
4462306a36Sopenharmony_ci * request is pointed to kernel virtual memory mapped for the request (such
4562306a36Sopenharmony_ci * that short_input indirection can be accomplished without copying). The
4662306a36Sopenharmony_ci * request’s target and completion ring are initialized to default values and
4762306a36Sopenharmony_ci * can be overridden by writing to the returned request object directly.
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * The initialized request can be further customized by writing to its fields
5062306a36Sopenharmony_ci * directly, taking care to covert such fields to little endian. The request
5162306a36Sopenharmony_ci * object will be consumed (and all its associated resources release) upon
5262306a36Sopenharmony_ci * passing it to hwrm_req_send() unless ownership of the request has been
5362306a36Sopenharmony_ci * claimed by the caller via a call to hwrm_req_hold(). If the request is not
5462306a36Sopenharmony_ci * consumed, either because it is never sent or because ownership has been
5562306a36Sopenharmony_ci * claimed, then it must be released by a call to hwrm_req_drop().
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * Return: zero on success, negative error code otherwise:
5862306a36Sopenharmony_ci *	E2BIG: the type of request pointer is too large to fit.
5962306a36Sopenharmony_ci *	ENOMEM: an allocation failure occurred.
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_ciint __hwrm_req_init(struct bnxt *bp, void **req, u16 req_type, u32 req_len)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	struct bnxt_hwrm_ctx *ctx;
6462306a36Sopenharmony_ci	dma_addr_t dma_handle;
6562306a36Sopenharmony_ci	u8 *req_addr;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	if (req_len > BNXT_HWRM_CTX_OFFSET)
6862306a36Sopenharmony_ci		return -E2BIG;
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	req_addr = dma_pool_alloc(bp->hwrm_dma_pool, GFP_KERNEL | __GFP_ZERO,
7162306a36Sopenharmony_ci				  &dma_handle);
7262306a36Sopenharmony_ci	if (!req_addr)
7362306a36Sopenharmony_ci		return -ENOMEM;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	ctx = (struct bnxt_hwrm_ctx *)(req_addr + BNXT_HWRM_CTX_OFFSET);
7662306a36Sopenharmony_ci	/* safety first, sentinel used to check for invalid requests */
7762306a36Sopenharmony_ci	ctx->sentinel = hwrm_calc_sentinel(ctx, req_type);
7862306a36Sopenharmony_ci	ctx->req_len = req_len;
7962306a36Sopenharmony_ci	ctx->req = (struct input *)req_addr;
8062306a36Sopenharmony_ci	ctx->resp = (struct output *)(req_addr + BNXT_HWRM_RESP_OFFSET);
8162306a36Sopenharmony_ci	ctx->dma_handle = dma_handle;
8262306a36Sopenharmony_ci	ctx->flags = 0; /* __GFP_ZERO, but be explicit regarding ownership */
8362306a36Sopenharmony_ci	ctx->timeout = bp->hwrm_cmd_timeout ?: DFLT_HWRM_CMD_TIMEOUT;
8462306a36Sopenharmony_ci	ctx->allocated = BNXT_HWRM_DMA_SIZE - BNXT_HWRM_CTX_OFFSET;
8562306a36Sopenharmony_ci	ctx->gfp = GFP_KERNEL;
8662306a36Sopenharmony_ci	ctx->slice_addr = NULL;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	/* initialize common request fields */
8962306a36Sopenharmony_ci	ctx->req->req_type = cpu_to_le16(req_type);
9062306a36Sopenharmony_ci	ctx->req->resp_addr = cpu_to_le64(dma_handle + BNXT_HWRM_RESP_OFFSET);
9162306a36Sopenharmony_ci	ctx->req->cmpl_ring = cpu_to_le16(BNXT_HWRM_NO_CMPL_RING);
9262306a36Sopenharmony_ci	ctx->req->target_id = cpu_to_le16(BNXT_HWRM_TARGET);
9362306a36Sopenharmony_ci	*req = ctx->req;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	return 0;
9662306a36Sopenharmony_ci}
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_cistatic struct bnxt_hwrm_ctx *__hwrm_ctx(struct bnxt *bp, u8 *req_addr)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	void *ctx_addr = req_addr + BNXT_HWRM_CTX_OFFSET;
10162306a36Sopenharmony_ci	struct input *req = (struct input *)req_addr;
10262306a36Sopenharmony_ci	struct bnxt_hwrm_ctx *ctx = ctx_addr;
10362306a36Sopenharmony_ci	u64 sentinel;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	if (!req) {
10662306a36Sopenharmony_ci		/* can only be due to software bug, be loud */
10762306a36Sopenharmony_ci		netdev_err(bp->dev, "null HWRM request");
10862306a36Sopenharmony_ci		dump_stack();
10962306a36Sopenharmony_ci		return NULL;
11062306a36Sopenharmony_ci	}
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	/* HWRM API has no type safety, verify sentinel to validate address */
11362306a36Sopenharmony_ci	sentinel = hwrm_calc_sentinel(ctx, le16_to_cpu(req->req_type));
11462306a36Sopenharmony_ci	if (ctx->sentinel != sentinel) {
11562306a36Sopenharmony_ci		/* can only be due to software bug, be loud */
11662306a36Sopenharmony_ci		netdev_err(bp->dev, "HWRM sentinel mismatch, req_type = %u\n",
11762306a36Sopenharmony_ci			   (u32)le16_to_cpu(req->req_type));
11862306a36Sopenharmony_ci		dump_stack();
11962306a36Sopenharmony_ci		return NULL;
12062306a36Sopenharmony_ci	}
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	return ctx;
12362306a36Sopenharmony_ci}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/**
12662306a36Sopenharmony_ci * hwrm_req_timeout() - Set the completion timeout for the request.
12762306a36Sopenharmony_ci * @bp: The driver context.
12862306a36Sopenharmony_ci * @req: The request to set the timeout.
12962306a36Sopenharmony_ci * @timeout: The timeout in milliseconds.
13062306a36Sopenharmony_ci *
13162306a36Sopenharmony_ci * Set the timeout associated with the request for subsequent calls to
13262306a36Sopenharmony_ci * hwrm_req_send(). Some requests are long running and require a different
13362306a36Sopenharmony_ci * timeout than the default.
13462306a36Sopenharmony_ci */
13562306a36Sopenharmony_civoid hwrm_req_timeout(struct bnxt *bp, void *req, unsigned int timeout)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	struct bnxt_hwrm_ctx *ctx = __hwrm_ctx(bp, req);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	if (ctx)
14062306a36Sopenharmony_ci		ctx->timeout = timeout;
14162306a36Sopenharmony_ci}
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci/**
14462306a36Sopenharmony_ci * hwrm_req_alloc_flags() - Sets GFP allocation flags for slices.
14562306a36Sopenharmony_ci * @bp: The driver context.
14662306a36Sopenharmony_ci * @req: The request for which calls to hwrm_req_dma_slice() will have altered
14762306a36Sopenharmony_ci *	allocation flags.
14862306a36Sopenharmony_ci * @gfp: A bitmask of GFP flags. These flags are passed to dma_alloc_coherent()
14962306a36Sopenharmony_ci *	whenever it is used to allocate backing memory for slices. Note that
15062306a36Sopenharmony_ci *	calls to hwrm_req_dma_slice() will not always result in new allocations,
15162306a36Sopenharmony_ci *	however, memory suballocated from the request buffer is already
15262306a36Sopenharmony_ci *	__GFP_ZERO.
15362306a36Sopenharmony_ci *
15462306a36Sopenharmony_ci * Sets the GFP allocation flags associated with the request for subsequent
15562306a36Sopenharmony_ci * calls to hwrm_req_dma_slice(). This can be useful for specifying __GFP_ZERO
15662306a36Sopenharmony_ci * for slice allocations.
15762306a36Sopenharmony_ci */
15862306a36Sopenharmony_civoid hwrm_req_alloc_flags(struct bnxt *bp, void *req, gfp_t gfp)
15962306a36Sopenharmony_ci{
16062306a36Sopenharmony_ci	struct bnxt_hwrm_ctx *ctx = __hwrm_ctx(bp, req);
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	if (ctx)
16362306a36Sopenharmony_ci		ctx->gfp = gfp;
16462306a36Sopenharmony_ci}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci/**
16762306a36Sopenharmony_ci * hwrm_req_replace() - Replace request data.
16862306a36Sopenharmony_ci * @bp: The driver context.
16962306a36Sopenharmony_ci * @req: The request to modify. A call to hwrm_req_replace() is conceptually
17062306a36Sopenharmony_ci *	an assignment of new_req to req. Subsequent calls to HWRM API functions,
17162306a36Sopenharmony_ci *	such as hwrm_req_send(), should thus use req and not new_req (in fact,
17262306a36Sopenharmony_ci *	calls to HWRM API functions will fail if non-managed request objects
17362306a36Sopenharmony_ci *	are passed).
17462306a36Sopenharmony_ci * @len: The length of new_req.
17562306a36Sopenharmony_ci * @new_req: The pre-built request to copy or reference.
17662306a36Sopenharmony_ci *
17762306a36Sopenharmony_ci * Replaces the request data in req with that of new_req. This is useful in
17862306a36Sopenharmony_ci * scenarios where a request object has already been constructed by a third
17962306a36Sopenharmony_ci * party prior to creating a resource managed request using hwrm_req_init().
18062306a36Sopenharmony_ci * Depending on the length, hwrm_req_replace() will either copy the new
18162306a36Sopenharmony_ci * request data into the DMA memory allocated for req, or it will simply
18262306a36Sopenharmony_ci * reference the new request and use it in lieu of req during subsequent
18362306a36Sopenharmony_ci * calls to hwrm_req_send(). The resource management is associated with
18462306a36Sopenharmony_ci * req and is independent of and does not apply to new_req. The caller must
18562306a36Sopenharmony_ci * ensure that the lifetime of new_req is least as long as req. Any slices
18662306a36Sopenharmony_ci * that may have been associated with the original request are released.
18762306a36Sopenharmony_ci *
18862306a36Sopenharmony_ci * Return: zero on success, negative error code otherwise:
18962306a36Sopenharmony_ci *     E2BIG: Request is too large.
19062306a36Sopenharmony_ci *     EINVAL: Invalid request to modify.
19162306a36Sopenharmony_ci */
19262306a36Sopenharmony_ciint hwrm_req_replace(struct bnxt *bp, void *req, void *new_req, u32 len)
19362306a36Sopenharmony_ci{
19462306a36Sopenharmony_ci	struct bnxt_hwrm_ctx *ctx = __hwrm_ctx(bp, req);
19562306a36Sopenharmony_ci	struct input *internal_req = req;
19662306a36Sopenharmony_ci	u16 req_type;
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	if (!ctx)
19962306a36Sopenharmony_ci		return -EINVAL;
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci	if (len > BNXT_HWRM_CTX_OFFSET)
20262306a36Sopenharmony_ci		return -E2BIG;
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	/* free any existing slices */
20562306a36Sopenharmony_ci	ctx->allocated = BNXT_HWRM_DMA_SIZE - BNXT_HWRM_CTX_OFFSET;
20662306a36Sopenharmony_ci	if (ctx->slice_addr) {
20762306a36Sopenharmony_ci		dma_free_coherent(&bp->pdev->dev, ctx->slice_size,
20862306a36Sopenharmony_ci				  ctx->slice_addr, ctx->slice_handle);
20962306a36Sopenharmony_ci		ctx->slice_addr = NULL;
21062306a36Sopenharmony_ci	}
21162306a36Sopenharmony_ci	ctx->gfp = GFP_KERNEL;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) || len > BNXT_HWRM_MAX_REQ_LEN) {
21462306a36Sopenharmony_ci		memcpy(internal_req, new_req, len);
21562306a36Sopenharmony_ci	} else {
21662306a36Sopenharmony_ci		internal_req->req_type = ((struct input *)new_req)->req_type;
21762306a36Sopenharmony_ci		ctx->req = new_req;
21862306a36Sopenharmony_ci	}
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	ctx->req_len = len;
22162306a36Sopenharmony_ci	ctx->req->resp_addr = cpu_to_le64(ctx->dma_handle +
22262306a36Sopenharmony_ci					  BNXT_HWRM_RESP_OFFSET);
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	/* update sentinel for potentially new request type */
22562306a36Sopenharmony_ci	req_type = le16_to_cpu(internal_req->req_type);
22662306a36Sopenharmony_ci	ctx->sentinel = hwrm_calc_sentinel(ctx, req_type);
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	return 0;
22962306a36Sopenharmony_ci}
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci/**
23262306a36Sopenharmony_ci * hwrm_req_flags() - Set non internal flags of the ctx
23362306a36Sopenharmony_ci * @bp: The driver context.
23462306a36Sopenharmony_ci * @req: The request containing the HWRM command
23562306a36Sopenharmony_ci * @flags: ctx flags that don't have BNXT_HWRM_INTERNAL_FLAG set
23662306a36Sopenharmony_ci *
23762306a36Sopenharmony_ci * ctx flags can be used by the callers to instruct how the subsequent
23862306a36Sopenharmony_ci * hwrm_req_send() should behave. Example: callers can use hwrm_req_flags
23962306a36Sopenharmony_ci * with BNXT_HWRM_CTX_SILENT to omit kernel prints of errors of hwrm_req_send()
24062306a36Sopenharmony_ci * or with BNXT_HWRM_FULL_WAIT enforce hwrm_req_send() to wait for full timeout
24162306a36Sopenharmony_ci * even if FW is not responding.
24262306a36Sopenharmony_ci * This generic function can be used to set any flag that is not an internal flag
24362306a36Sopenharmony_ci * of the HWRM module.
24462306a36Sopenharmony_ci */
24562306a36Sopenharmony_civoid hwrm_req_flags(struct bnxt *bp, void *req, enum bnxt_hwrm_ctx_flags flags)
24662306a36Sopenharmony_ci{
24762306a36Sopenharmony_ci	struct bnxt_hwrm_ctx *ctx = __hwrm_ctx(bp, req);
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	if (ctx)
25062306a36Sopenharmony_ci		ctx->flags |= (flags & HWRM_API_FLAGS);
25162306a36Sopenharmony_ci}
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci/**
25462306a36Sopenharmony_ci * hwrm_req_hold() - Claim ownership of the request's resources.
25562306a36Sopenharmony_ci * @bp: The driver context.
25662306a36Sopenharmony_ci * @req: A pointer to the request to own. The request will no longer be
25762306a36Sopenharmony_ci *	consumed by calls to hwrm_req_send().
25862306a36Sopenharmony_ci *
25962306a36Sopenharmony_ci * Take ownership of the request. Ownership places responsibility on the
26062306a36Sopenharmony_ci * caller to free the resources associated with the request via a call to
26162306a36Sopenharmony_ci * hwrm_req_drop(). The caller taking ownership implies that a subsequent
26262306a36Sopenharmony_ci * call to hwrm_req_send() will not consume the request (ie. sending will
26362306a36Sopenharmony_ci * not free the associated resources if the request is owned by the caller).
26462306a36Sopenharmony_ci * Taking ownership returns a reference to the response. Retaining and
26562306a36Sopenharmony_ci * accessing the response data is the most common reason to take ownership
26662306a36Sopenharmony_ci * of the request. Ownership can also be acquired in order to reuse the same
26762306a36Sopenharmony_ci * request object across multiple invocations of hwrm_req_send().
26862306a36Sopenharmony_ci *
26962306a36Sopenharmony_ci * Return: A pointer to the response object.
27062306a36Sopenharmony_ci *
27162306a36Sopenharmony_ci * The resources associated with the response will remain available to the
27262306a36Sopenharmony_ci * caller until ownership of the request is relinquished via a call to
27362306a36Sopenharmony_ci * hwrm_req_drop(). It is not possible for hwrm_req_hold() to return NULL if
27462306a36Sopenharmony_ci * a valid request is provided. A returned NULL value would imply a driver
27562306a36Sopenharmony_ci * bug and the implementation will complain loudly in the logs to aid in
27662306a36Sopenharmony_ci * detection. It should not be necessary to check the result for NULL.
27762306a36Sopenharmony_ci */
27862306a36Sopenharmony_civoid *hwrm_req_hold(struct bnxt *bp, void *req)
27962306a36Sopenharmony_ci{
28062306a36Sopenharmony_ci	struct bnxt_hwrm_ctx *ctx = __hwrm_ctx(bp, req);
28162306a36Sopenharmony_ci	struct input *input = (struct input *)req;
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	if (!ctx)
28462306a36Sopenharmony_ci		return NULL;
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci	if (ctx->flags & BNXT_HWRM_INTERNAL_CTX_OWNED) {
28762306a36Sopenharmony_ci		/* can only be due to software bug, be loud */
28862306a36Sopenharmony_ci		netdev_err(bp->dev, "HWRM context already owned, req_type = %u\n",
28962306a36Sopenharmony_ci			   (u32)le16_to_cpu(input->req_type));
29062306a36Sopenharmony_ci		dump_stack();
29162306a36Sopenharmony_ci		return NULL;
29262306a36Sopenharmony_ci	}
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci	ctx->flags |= BNXT_HWRM_INTERNAL_CTX_OWNED;
29562306a36Sopenharmony_ci	return ((u8 *)req) + BNXT_HWRM_RESP_OFFSET;
29662306a36Sopenharmony_ci}
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_cistatic void __hwrm_ctx_drop(struct bnxt *bp, struct bnxt_hwrm_ctx *ctx)
29962306a36Sopenharmony_ci{
30062306a36Sopenharmony_ci	void *addr = ((u8 *)ctx) - BNXT_HWRM_CTX_OFFSET;
30162306a36Sopenharmony_ci	dma_addr_t dma_handle = ctx->dma_handle; /* save before invalidate */
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	/* unmap any auxiliary DMA slice */
30462306a36Sopenharmony_ci	if (ctx->slice_addr)
30562306a36Sopenharmony_ci		dma_free_coherent(&bp->pdev->dev, ctx->slice_size,
30662306a36Sopenharmony_ci				  ctx->slice_addr, ctx->slice_handle);
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci	/* invalidate, ensure ownership, sentinel and dma_handle are cleared */
30962306a36Sopenharmony_ci	memset(ctx, 0, sizeof(struct bnxt_hwrm_ctx));
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci	/* return the buffer to the DMA pool */
31262306a36Sopenharmony_ci	if (dma_handle)
31362306a36Sopenharmony_ci		dma_pool_free(bp->hwrm_dma_pool, addr, dma_handle);
31462306a36Sopenharmony_ci}
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci/**
31762306a36Sopenharmony_ci * hwrm_req_drop() - Release all resources associated with the request.
31862306a36Sopenharmony_ci * @bp: The driver context.
31962306a36Sopenharmony_ci * @req: The request to consume, releasing the associated resources. The
32062306a36Sopenharmony_ci *	request object, any slices, and its associated response are no
32162306a36Sopenharmony_ci *	longer valid.
32262306a36Sopenharmony_ci *
32362306a36Sopenharmony_ci * It is legal to call hwrm_req_drop() on an unowned request, provided it
32462306a36Sopenharmony_ci * has not already been consumed by hwrm_req_send() (for example, to release
32562306a36Sopenharmony_ci * an aborted request). A given request should not be dropped more than once,
32662306a36Sopenharmony_ci * nor should it be dropped after having been consumed by hwrm_req_send(). To
32762306a36Sopenharmony_ci * do so is an error (the context will not be found and a stack trace will be
32862306a36Sopenharmony_ci * rendered in the kernel log).
32962306a36Sopenharmony_ci */
33062306a36Sopenharmony_civoid hwrm_req_drop(struct bnxt *bp, void *req)
33162306a36Sopenharmony_ci{
33262306a36Sopenharmony_ci	struct bnxt_hwrm_ctx *ctx = __hwrm_ctx(bp, req);
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci	if (ctx)
33562306a36Sopenharmony_ci		__hwrm_ctx_drop(bp, ctx);
33662306a36Sopenharmony_ci}
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_cistatic int __hwrm_to_stderr(u32 hwrm_err)
33962306a36Sopenharmony_ci{
34062306a36Sopenharmony_ci	switch (hwrm_err) {
34162306a36Sopenharmony_ci	case HWRM_ERR_CODE_SUCCESS:
34262306a36Sopenharmony_ci		return 0;
34362306a36Sopenharmony_ci	case HWRM_ERR_CODE_RESOURCE_LOCKED:
34462306a36Sopenharmony_ci		return -EROFS;
34562306a36Sopenharmony_ci	case HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED:
34662306a36Sopenharmony_ci		return -EACCES;
34762306a36Sopenharmony_ci	case HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR:
34862306a36Sopenharmony_ci		return -ENOSPC;
34962306a36Sopenharmony_ci	case HWRM_ERR_CODE_INVALID_PARAMS:
35062306a36Sopenharmony_ci	case HWRM_ERR_CODE_INVALID_FLAGS:
35162306a36Sopenharmony_ci	case HWRM_ERR_CODE_INVALID_ENABLES:
35262306a36Sopenharmony_ci	case HWRM_ERR_CODE_UNSUPPORTED_TLV:
35362306a36Sopenharmony_ci	case HWRM_ERR_CODE_UNSUPPORTED_OPTION_ERR:
35462306a36Sopenharmony_ci		return -EINVAL;
35562306a36Sopenharmony_ci	case HWRM_ERR_CODE_NO_BUFFER:
35662306a36Sopenharmony_ci		return -ENOMEM;
35762306a36Sopenharmony_ci	case HWRM_ERR_CODE_HOT_RESET_PROGRESS:
35862306a36Sopenharmony_ci	case HWRM_ERR_CODE_BUSY:
35962306a36Sopenharmony_ci		return -EAGAIN;
36062306a36Sopenharmony_ci	case HWRM_ERR_CODE_CMD_NOT_SUPPORTED:
36162306a36Sopenharmony_ci		return -EOPNOTSUPP;
36262306a36Sopenharmony_ci	case HWRM_ERR_CODE_PF_UNAVAILABLE:
36362306a36Sopenharmony_ci		return -ENODEV;
36462306a36Sopenharmony_ci	default:
36562306a36Sopenharmony_ci		return -EIO;
36662306a36Sopenharmony_ci	}
36762306a36Sopenharmony_ci}
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_cistatic struct bnxt_hwrm_wait_token *
37062306a36Sopenharmony_ci__hwrm_acquire_token(struct bnxt *bp, enum bnxt_hwrm_chnl dst)
37162306a36Sopenharmony_ci{
37262306a36Sopenharmony_ci	struct bnxt_hwrm_wait_token *token;
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci	token = kzalloc(sizeof(*token), GFP_KERNEL);
37562306a36Sopenharmony_ci	if (!token)
37662306a36Sopenharmony_ci		return NULL;
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci	mutex_lock(&bp->hwrm_cmd_lock);
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci	token->dst = dst;
38162306a36Sopenharmony_ci	token->state = BNXT_HWRM_PENDING;
38262306a36Sopenharmony_ci	if (dst == BNXT_HWRM_CHNL_CHIMP) {
38362306a36Sopenharmony_ci		token->seq_id = bp->hwrm_cmd_seq++;
38462306a36Sopenharmony_ci		hlist_add_head_rcu(&token->node, &bp->hwrm_pending_list);
38562306a36Sopenharmony_ci	} else {
38662306a36Sopenharmony_ci		token->seq_id = bp->hwrm_cmd_kong_seq++;
38762306a36Sopenharmony_ci	}
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci	return token;
39062306a36Sopenharmony_ci}
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_cistatic void
39362306a36Sopenharmony_ci__hwrm_release_token(struct bnxt *bp, struct bnxt_hwrm_wait_token *token)
39462306a36Sopenharmony_ci{
39562306a36Sopenharmony_ci	if (token->dst == BNXT_HWRM_CHNL_CHIMP) {
39662306a36Sopenharmony_ci		hlist_del_rcu(&token->node);
39762306a36Sopenharmony_ci		kfree_rcu(token, rcu);
39862306a36Sopenharmony_ci	} else {
39962306a36Sopenharmony_ci		kfree(token);
40062306a36Sopenharmony_ci	}
40162306a36Sopenharmony_ci	mutex_unlock(&bp->hwrm_cmd_lock);
40262306a36Sopenharmony_ci}
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_civoid
40562306a36Sopenharmony_cihwrm_update_token(struct bnxt *bp, u16 seq_id, enum bnxt_hwrm_wait_state state)
40662306a36Sopenharmony_ci{
40762306a36Sopenharmony_ci	struct bnxt_hwrm_wait_token *token;
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci	rcu_read_lock();
41062306a36Sopenharmony_ci	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node) {
41162306a36Sopenharmony_ci		if (token->seq_id == seq_id) {
41262306a36Sopenharmony_ci			WRITE_ONCE(token->state, state);
41362306a36Sopenharmony_ci			rcu_read_unlock();
41462306a36Sopenharmony_ci			return;
41562306a36Sopenharmony_ci		}
41662306a36Sopenharmony_ci	}
41762306a36Sopenharmony_ci	rcu_read_unlock();
41862306a36Sopenharmony_ci	netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
41962306a36Sopenharmony_ci}
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_cistatic void hwrm_req_dbg(struct bnxt *bp, struct input *req)
42262306a36Sopenharmony_ci{
42362306a36Sopenharmony_ci	u32 ring = le16_to_cpu(req->cmpl_ring);
42462306a36Sopenharmony_ci	u32 type = le16_to_cpu(req->req_type);
42562306a36Sopenharmony_ci	u32 tgt = le16_to_cpu(req->target_id);
42662306a36Sopenharmony_ci	u32 seq = le16_to_cpu(req->seq_id);
42762306a36Sopenharmony_ci	char opt[32] = "\n";
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci	if (unlikely(ring != (u16)BNXT_HWRM_NO_CMPL_RING))
43062306a36Sopenharmony_ci		snprintf(opt, 16, " ring %d\n", ring);
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci	if (unlikely(tgt != BNXT_HWRM_TARGET))
43362306a36Sopenharmony_ci		snprintf(opt + strlen(opt) - 1, 16, " tgt 0x%x\n", tgt);
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci	netdev_dbg(bp->dev, "sent hwrm req_type 0x%x seq id 0x%x%s",
43662306a36Sopenharmony_ci		   type, seq, opt);
43762306a36Sopenharmony_ci}
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ci#define hwrm_err(bp, ctx, fmt, ...)				       \
44062306a36Sopenharmony_ci	do {							       \
44162306a36Sopenharmony_ci		if ((ctx)->flags & BNXT_HWRM_CTX_SILENT)	       \
44262306a36Sopenharmony_ci			netdev_dbg((bp)->dev, fmt, __VA_ARGS__);       \
44362306a36Sopenharmony_ci		else						       \
44462306a36Sopenharmony_ci			netdev_err((bp)->dev, fmt, __VA_ARGS__);       \
44562306a36Sopenharmony_ci	} while (0)
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_cistatic bool hwrm_wait_must_abort(struct bnxt *bp, u32 req_type, u32 *fw_status)
44862306a36Sopenharmony_ci{
44962306a36Sopenharmony_ci	if (req_type == HWRM_VER_GET)
45062306a36Sopenharmony_ci		return false;
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci	if (!bp->fw_health || !bp->fw_health->status_reliable)
45362306a36Sopenharmony_ci		return false;
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci	*fw_status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
45662306a36Sopenharmony_ci	return *fw_status && !BNXT_FW_IS_HEALTHY(*fw_status);
45762306a36Sopenharmony_ci}
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_cistatic int __hwrm_send(struct bnxt *bp, struct bnxt_hwrm_ctx *ctx)
46062306a36Sopenharmony_ci{
46162306a36Sopenharmony_ci	u32 doorbell_offset = BNXT_GRCPF_REG_CHIMP_COMM_TRIGGER;
46262306a36Sopenharmony_ci	enum bnxt_hwrm_chnl dst = BNXT_HWRM_CHNL_CHIMP;
46362306a36Sopenharmony_ci	u32 bar_offset = BNXT_GRCPF_REG_CHIMP_COMM;
46462306a36Sopenharmony_ci	struct bnxt_hwrm_wait_token *token = NULL;
46562306a36Sopenharmony_ci	struct hwrm_short_input short_input = {0};
46662306a36Sopenharmony_ci	u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
46762306a36Sopenharmony_ci	unsigned int i, timeout, tmo_count;
46862306a36Sopenharmony_ci	u32 *data = (u32 *)ctx->req;
46962306a36Sopenharmony_ci	u32 msg_len = ctx->req_len;
47062306a36Sopenharmony_ci	u32 req_type, sts;
47162306a36Sopenharmony_ci	int rc = -EBUSY;
47262306a36Sopenharmony_ci	u16 len = 0;
47362306a36Sopenharmony_ci	u8 *valid;
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci	if (ctx->flags & BNXT_HWRM_INTERNAL_RESP_DIRTY)
47662306a36Sopenharmony_ci		memset(ctx->resp, 0, PAGE_SIZE);
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci	req_type = le16_to_cpu(ctx->req->req_type);
47962306a36Sopenharmony_ci	if (BNXT_NO_FW_ACCESS(bp) &&
48062306a36Sopenharmony_ci	    (req_type != HWRM_FUNC_RESET && req_type != HWRM_VER_GET)) {
48162306a36Sopenharmony_ci		netdev_dbg(bp->dev, "hwrm req_type 0x%x skipped, FW channel down\n",
48262306a36Sopenharmony_ci			   req_type);
48362306a36Sopenharmony_ci		goto exit;
48462306a36Sopenharmony_ci	}
48562306a36Sopenharmony_ci
48662306a36Sopenharmony_ci	if (msg_len > BNXT_HWRM_MAX_REQ_LEN &&
48762306a36Sopenharmony_ci	    msg_len > bp->hwrm_max_ext_req_len) {
48862306a36Sopenharmony_ci		rc = -E2BIG;
48962306a36Sopenharmony_ci		goto exit;
49062306a36Sopenharmony_ci	}
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci	if (bnxt_kong_hwrm_message(bp, ctx->req)) {
49362306a36Sopenharmony_ci		dst = BNXT_HWRM_CHNL_KONG;
49462306a36Sopenharmony_ci		bar_offset = BNXT_GRCPF_REG_KONG_COMM;
49562306a36Sopenharmony_ci		doorbell_offset = BNXT_GRCPF_REG_KONG_COMM_TRIGGER;
49662306a36Sopenharmony_ci		if (le16_to_cpu(ctx->req->cmpl_ring) != INVALID_HW_RING_ID) {
49762306a36Sopenharmony_ci			netdev_err(bp->dev, "Ring completions not supported for KONG commands, req_type = %d\n",
49862306a36Sopenharmony_ci				   req_type);
49962306a36Sopenharmony_ci			rc = -EINVAL;
50062306a36Sopenharmony_ci			goto exit;
50162306a36Sopenharmony_ci		}
50262306a36Sopenharmony_ci	}
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci	token = __hwrm_acquire_token(bp, dst);
50562306a36Sopenharmony_ci	if (!token) {
50662306a36Sopenharmony_ci		rc = -ENOMEM;
50762306a36Sopenharmony_ci		goto exit;
50862306a36Sopenharmony_ci	}
50962306a36Sopenharmony_ci	ctx->req->seq_id = cpu_to_le16(token->seq_id);
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci	if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
51262306a36Sopenharmony_ci	    msg_len > BNXT_HWRM_MAX_REQ_LEN) {
51362306a36Sopenharmony_ci		short_input.req_type = ctx->req->req_type;
51462306a36Sopenharmony_ci		short_input.signature =
51562306a36Sopenharmony_ci				cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
51662306a36Sopenharmony_ci		short_input.size = cpu_to_le16(msg_len);
51762306a36Sopenharmony_ci		short_input.req_addr = cpu_to_le64(ctx->dma_handle);
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci		data = (u32 *)&short_input;
52062306a36Sopenharmony_ci		msg_len = sizeof(short_input);
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci		max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
52362306a36Sopenharmony_ci	}
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci	/* Ensure any associated DMA buffers are written before doorbell */
52662306a36Sopenharmony_ci	wmb();
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ci	/* Write request msg to hwrm channel */
52962306a36Sopenharmony_ci	__iowrite32_copy(bp->bar0 + bar_offset, data, msg_len / 4);
53062306a36Sopenharmony_ci
53162306a36Sopenharmony_ci	for (i = msg_len; i < max_req_len; i += 4)
53262306a36Sopenharmony_ci		writel(0, bp->bar0 + bar_offset + i);
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci	/* Ring channel doorbell */
53562306a36Sopenharmony_ci	writel(1, bp->bar0 + doorbell_offset);
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci	hwrm_req_dbg(bp, ctx->req);
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ci	if (!pci_is_enabled(bp->pdev)) {
54062306a36Sopenharmony_ci		rc = -ENODEV;
54162306a36Sopenharmony_ci		goto exit;
54262306a36Sopenharmony_ci	}
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci	/* Limit timeout to an upper limit */
54562306a36Sopenharmony_ci	timeout = min(ctx->timeout, bp->hwrm_cmd_max_timeout ?: HWRM_CMD_MAX_TIMEOUT);
54662306a36Sopenharmony_ci	/* convert timeout to usec */
54762306a36Sopenharmony_ci	timeout *= 1000;
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci	i = 0;
55062306a36Sopenharmony_ci	/* Short timeout for the first few iterations:
55162306a36Sopenharmony_ci	 * number of loops = number of loops for short timeout +
55262306a36Sopenharmony_ci	 * number of loops for standard timeout.
55362306a36Sopenharmony_ci	 */
55462306a36Sopenharmony_ci	tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
55562306a36Sopenharmony_ci	timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
55662306a36Sopenharmony_ci	tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
55762306a36Sopenharmony_ci
55862306a36Sopenharmony_ci	if (le16_to_cpu(ctx->req->cmpl_ring) != INVALID_HW_RING_ID) {
55962306a36Sopenharmony_ci		/* Wait until hwrm response cmpl interrupt is processed */
56062306a36Sopenharmony_ci		while (READ_ONCE(token->state) < BNXT_HWRM_COMPLETE &&
56162306a36Sopenharmony_ci		       i++ < tmo_count) {
56262306a36Sopenharmony_ci			/* Abort the wait for completion if the FW health
56362306a36Sopenharmony_ci			 * check has failed.
56462306a36Sopenharmony_ci			 */
56562306a36Sopenharmony_ci			if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
56662306a36Sopenharmony_ci				goto exit;
56762306a36Sopenharmony_ci			/* on first few passes, just barely sleep */
56862306a36Sopenharmony_ci			if (i < HWRM_SHORT_TIMEOUT_COUNTER) {
56962306a36Sopenharmony_ci				usleep_range(HWRM_SHORT_MIN_TIMEOUT,
57062306a36Sopenharmony_ci					     HWRM_SHORT_MAX_TIMEOUT);
57162306a36Sopenharmony_ci			} else {
57262306a36Sopenharmony_ci				if (hwrm_wait_must_abort(bp, req_type, &sts)) {
57362306a36Sopenharmony_ci					hwrm_err(bp, ctx, "Resp cmpl intr abandoning msg: 0x%x due to firmware status: 0x%x\n",
57462306a36Sopenharmony_ci						 req_type, sts);
57562306a36Sopenharmony_ci					goto exit;
57662306a36Sopenharmony_ci				}
57762306a36Sopenharmony_ci				usleep_range(HWRM_MIN_TIMEOUT,
57862306a36Sopenharmony_ci					     HWRM_MAX_TIMEOUT);
57962306a36Sopenharmony_ci			}
58062306a36Sopenharmony_ci		}
58162306a36Sopenharmony_ci
58262306a36Sopenharmony_ci		if (READ_ONCE(token->state) != BNXT_HWRM_COMPLETE) {
58362306a36Sopenharmony_ci			hwrm_err(bp, ctx, "Resp cmpl intr err msg: 0x%x\n",
58462306a36Sopenharmony_ci				 req_type);
58562306a36Sopenharmony_ci			goto exit;
58662306a36Sopenharmony_ci		}
58762306a36Sopenharmony_ci		len = le16_to_cpu(READ_ONCE(ctx->resp->resp_len));
58862306a36Sopenharmony_ci		valid = ((u8 *)ctx->resp) + len - 1;
58962306a36Sopenharmony_ci	} else {
59062306a36Sopenharmony_ci		__le16 seen_out_of_seq = ctx->req->seq_id; /* will never see */
59162306a36Sopenharmony_ci		int j;
59262306a36Sopenharmony_ci
59362306a36Sopenharmony_ci		/* Check if response len is updated */
59462306a36Sopenharmony_ci		for (i = 0; i < tmo_count; i++) {
59562306a36Sopenharmony_ci			/* Abort the wait for completion if the FW health
59662306a36Sopenharmony_ci			 * check has failed.
59762306a36Sopenharmony_ci			 */
59862306a36Sopenharmony_ci			if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
59962306a36Sopenharmony_ci				goto exit;
60062306a36Sopenharmony_ci
60162306a36Sopenharmony_ci			if (token &&
60262306a36Sopenharmony_ci			    READ_ONCE(token->state) == BNXT_HWRM_DEFERRED) {
60362306a36Sopenharmony_ci				__hwrm_release_token(bp, token);
60462306a36Sopenharmony_ci				token = NULL;
60562306a36Sopenharmony_ci			}
60662306a36Sopenharmony_ci
60762306a36Sopenharmony_ci			len = le16_to_cpu(READ_ONCE(ctx->resp->resp_len));
60862306a36Sopenharmony_ci			if (len) {
60962306a36Sopenharmony_ci				__le16 resp_seq = READ_ONCE(ctx->resp->seq_id);
61062306a36Sopenharmony_ci
61162306a36Sopenharmony_ci				if (resp_seq == ctx->req->seq_id)
61262306a36Sopenharmony_ci					break;
61362306a36Sopenharmony_ci				if (resp_seq != seen_out_of_seq) {
61462306a36Sopenharmony_ci					netdev_warn(bp->dev, "Discarding out of seq response: 0x%x for msg {0x%x 0x%x}\n",
61562306a36Sopenharmony_ci						    le16_to_cpu(resp_seq),
61662306a36Sopenharmony_ci						    req_type,
61762306a36Sopenharmony_ci						    le16_to_cpu(ctx->req->seq_id));
61862306a36Sopenharmony_ci					seen_out_of_seq = resp_seq;
61962306a36Sopenharmony_ci				}
62062306a36Sopenharmony_ci			}
62162306a36Sopenharmony_ci
62262306a36Sopenharmony_ci			/* on first few passes, just barely sleep */
62362306a36Sopenharmony_ci			if (i < HWRM_SHORT_TIMEOUT_COUNTER) {
62462306a36Sopenharmony_ci				usleep_range(HWRM_SHORT_MIN_TIMEOUT,
62562306a36Sopenharmony_ci					     HWRM_SHORT_MAX_TIMEOUT);
62662306a36Sopenharmony_ci			} else {
62762306a36Sopenharmony_ci				if (hwrm_wait_must_abort(bp, req_type, &sts)) {
62862306a36Sopenharmony_ci					hwrm_err(bp, ctx, "Abandoning msg {0x%x 0x%x} len: %d due to firmware status: 0x%x\n",
62962306a36Sopenharmony_ci						 req_type,
63062306a36Sopenharmony_ci						 le16_to_cpu(ctx->req->seq_id),
63162306a36Sopenharmony_ci						 len, sts);
63262306a36Sopenharmony_ci					goto exit;
63362306a36Sopenharmony_ci				}
63462306a36Sopenharmony_ci				usleep_range(HWRM_MIN_TIMEOUT,
63562306a36Sopenharmony_ci					     HWRM_MAX_TIMEOUT);
63662306a36Sopenharmony_ci			}
63762306a36Sopenharmony_ci		}
63862306a36Sopenharmony_ci
63962306a36Sopenharmony_ci		if (i >= tmo_count) {
64062306a36Sopenharmony_ci			hwrm_err(bp, ctx, "Error (timeout: %u) msg {0x%x 0x%x} len:%d\n",
64162306a36Sopenharmony_ci				 hwrm_total_timeout(i), req_type,
64262306a36Sopenharmony_ci				 le16_to_cpu(ctx->req->seq_id), len);
64362306a36Sopenharmony_ci			goto exit;
64462306a36Sopenharmony_ci		}
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_ci		/* Last byte of resp contains valid bit */
64762306a36Sopenharmony_ci		valid = ((u8 *)ctx->resp) + len - 1;
64862306a36Sopenharmony_ci		for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; ) {
64962306a36Sopenharmony_ci			/* make sure we read from updated DMA memory */
65062306a36Sopenharmony_ci			dma_rmb();
65162306a36Sopenharmony_ci			if (*valid)
65262306a36Sopenharmony_ci				break;
65362306a36Sopenharmony_ci			if (j < 10) {
65462306a36Sopenharmony_ci				udelay(1);
65562306a36Sopenharmony_ci				j++;
65662306a36Sopenharmony_ci			} else {
65762306a36Sopenharmony_ci				usleep_range(20, 30);
65862306a36Sopenharmony_ci				j += 20;
65962306a36Sopenharmony_ci			}
66062306a36Sopenharmony_ci		}
66162306a36Sopenharmony_ci
66262306a36Sopenharmony_ci		if (j >= HWRM_VALID_BIT_DELAY_USEC) {
66362306a36Sopenharmony_ci			hwrm_err(bp, ctx, "Error (timeout: %u) msg {0x%x 0x%x} len:%d v:%d\n",
66462306a36Sopenharmony_ci				 hwrm_total_timeout(i) + j, req_type,
66562306a36Sopenharmony_ci				 le16_to_cpu(ctx->req->seq_id), len, *valid);
66662306a36Sopenharmony_ci			goto exit;
66762306a36Sopenharmony_ci		}
66862306a36Sopenharmony_ci	}
66962306a36Sopenharmony_ci
67062306a36Sopenharmony_ci	/* Zero valid bit for compatibility.  Valid bit in an older spec
67162306a36Sopenharmony_ci	 * may become a new field in a newer spec.  We must make sure that
67262306a36Sopenharmony_ci	 * a new field not implemented by old spec will read zero.
67362306a36Sopenharmony_ci	 */
67462306a36Sopenharmony_ci	*valid = 0;
67562306a36Sopenharmony_ci	rc = le16_to_cpu(ctx->resp->error_code);
67662306a36Sopenharmony_ci	if (rc == HWRM_ERR_CODE_BUSY && !(ctx->flags & BNXT_HWRM_CTX_SILENT))
67762306a36Sopenharmony_ci		netdev_warn(bp->dev, "FW returned busy, hwrm req_type 0x%x\n",
67862306a36Sopenharmony_ci			    req_type);
67962306a36Sopenharmony_ci	else if (rc && rc != HWRM_ERR_CODE_PF_UNAVAILABLE)
68062306a36Sopenharmony_ci		hwrm_err(bp, ctx, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
68162306a36Sopenharmony_ci			 req_type, token->seq_id, rc);
68262306a36Sopenharmony_ci	rc = __hwrm_to_stderr(rc);
68362306a36Sopenharmony_ciexit:
68462306a36Sopenharmony_ci	if (token)
68562306a36Sopenharmony_ci		__hwrm_release_token(bp, token);
68662306a36Sopenharmony_ci	if (ctx->flags & BNXT_HWRM_INTERNAL_CTX_OWNED)
68762306a36Sopenharmony_ci		ctx->flags |= BNXT_HWRM_INTERNAL_RESP_DIRTY;
68862306a36Sopenharmony_ci	else
68962306a36Sopenharmony_ci		__hwrm_ctx_drop(bp, ctx);
69062306a36Sopenharmony_ci	return rc;
69162306a36Sopenharmony_ci}
69262306a36Sopenharmony_ci
69362306a36Sopenharmony_ci/**
69462306a36Sopenharmony_ci * hwrm_req_send() - Execute an HWRM command.
69562306a36Sopenharmony_ci * @bp: The driver context.
69662306a36Sopenharmony_ci * @req: A pointer to the request to send. The DMA resources associated with
69762306a36Sopenharmony_ci *	the request will be released (ie. the request will be consumed) unless
69862306a36Sopenharmony_ci *	ownership of the request has been assumed by the caller via a call to
69962306a36Sopenharmony_ci *	hwrm_req_hold().
70062306a36Sopenharmony_ci *
70162306a36Sopenharmony_ci * Send an HWRM request to the device and wait for a response. The request is
70262306a36Sopenharmony_ci * consumed if it is not owned by the caller. This function will block until
70362306a36Sopenharmony_ci * the request has either completed or times out due to an error.
70462306a36Sopenharmony_ci *
70562306a36Sopenharmony_ci * Return: A result code.
70662306a36Sopenharmony_ci *
70762306a36Sopenharmony_ci * The result is zero on success, otherwise the negative error code indicates
70862306a36Sopenharmony_ci * one of the following errors:
70962306a36Sopenharmony_ci *	E2BIG: The request was too large.
71062306a36Sopenharmony_ci *	EBUSY: The firmware is in a fatal state or the request timed out
71162306a36Sopenharmony_ci *	EACCESS: HWRM access denied.
71262306a36Sopenharmony_ci *	ENOSPC: HWRM resource allocation error.
71362306a36Sopenharmony_ci *	EINVAL: Request parameters are invalid.
71462306a36Sopenharmony_ci *	ENOMEM: HWRM has no buffers.
71562306a36Sopenharmony_ci *	EAGAIN: HWRM busy or reset in progress.
71662306a36Sopenharmony_ci *	EOPNOTSUPP: Invalid request type.
71762306a36Sopenharmony_ci *	EIO: Any other error.
71862306a36Sopenharmony_ci * Error handling is orthogonal to request ownership. An unowned request will
71962306a36Sopenharmony_ci * still be consumed on error. If the caller owns the request, then the caller
72062306a36Sopenharmony_ci * is responsible for releasing the resources. Otherwise, hwrm_req_send() will
72162306a36Sopenharmony_ci * always consume the request.
72262306a36Sopenharmony_ci */
72362306a36Sopenharmony_ciint hwrm_req_send(struct bnxt *bp, void *req)
72462306a36Sopenharmony_ci{
72562306a36Sopenharmony_ci	struct bnxt_hwrm_ctx *ctx = __hwrm_ctx(bp, req);
72662306a36Sopenharmony_ci
72762306a36Sopenharmony_ci	if (!ctx)
72862306a36Sopenharmony_ci		return -EINVAL;
72962306a36Sopenharmony_ci
73062306a36Sopenharmony_ci	return __hwrm_send(bp, ctx);
73162306a36Sopenharmony_ci}
73262306a36Sopenharmony_ci
73362306a36Sopenharmony_ci/**
73462306a36Sopenharmony_ci * hwrm_req_send_silent() - A silent version of hwrm_req_send().
73562306a36Sopenharmony_ci * @bp: The driver context.
73662306a36Sopenharmony_ci * @req: The request to send without logging.
73762306a36Sopenharmony_ci *
73862306a36Sopenharmony_ci * The same as hwrm_req_send(), except that the request is silenced using
73962306a36Sopenharmony_ci * hwrm_req_silence() prior the call. This version of the function is
74062306a36Sopenharmony_ci * provided solely to preserve the legacy API’s flavor for this functionality.
74162306a36Sopenharmony_ci *
74262306a36Sopenharmony_ci * Return: A result code, see hwrm_req_send().
74362306a36Sopenharmony_ci */
74462306a36Sopenharmony_ciint hwrm_req_send_silent(struct bnxt *bp, void *req)
74562306a36Sopenharmony_ci{
74662306a36Sopenharmony_ci	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT);
74762306a36Sopenharmony_ci	return hwrm_req_send(bp, req);
74862306a36Sopenharmony_ci}
74962306a36Sopenharmony_ci
75062306a36Sopenharmony_ci/**
75162306a36Sopenharmony_ci * hwrm_req_dma_slice() - Allocate a slice of DMA mapped memory.
75262306a36Sopenharmony_ci * @bp: The driver context.
75362306a36Sopenharmony_ci * @req: The request for which indirect data will be associated.
75462306a36Sopenharmony_ci * @size: The size of the allocation.
75562306a36Sopenharmony_ci * @dma_handle: The bus address associated with the allocation. The HWRM API has
75662306a36Sopenharmony_ci *	no knowledge about the type of the request and so cannot infer how the
75762306a36Sopenharmony_ci *	caller intends to use the indirect data. Thus, the caller is
75862306a36Sopenharmony_ci *	responsible for configuring the request object appropriately to
75962306a36Sopenharmony_ci *	point to the associated indirect memory. Note, DMA handle has the
76062306a36Sopenharmony_ci *	same definition as it does in dma_alloc_coherent(), the caller is
76162306a36Sopenharmony_ci *	responsible for endian conversions via cpu_to_le64() before assigning
76262306a36Sopenharmony_ci *	this address.
76362306a36Sopenharmony_ci *
76462306a36Sopenharmony_ci * Allocates DMA mapped memory for indirect data related to a request. The
76562306a36Sopenharmony_ci * lifetime of the DMA resources will be bound to that of the request (ie.
76662306a36Sopenharmony_ci * they will be automatically released when the request is either consumed by
76762306a36Sopenharmony_ci * hwrm_req_send() or dropped by hwrm_req_drop()). Small allocations are
76862306a36Sopenharmony_ci * efficiently suballocated out of the request buffer space, hence the name
76962306a36Sopenharmony_ci * slice, while larger requests are satisfied via an underlying call to
77062306a36Sopenharmony_ci * dma_alloc_coherent(). Multiple suballocations are supported, however, only
77162306a36Sopenharmony_ci * one externally mapped region is.
77262306a36Sopenharmony_ci *
77362306a36Sopenharmony_ci * Return: The kernel virtual address of the DMA mapping.
77462306a36Sopenharmony_ci */
77562306a36Sopenharmony_civoid *
77662306a36Sopenharmony_cihwrm_req_dma_slice(struct bnxt *bp, void *req, u32 size, dma_addr_t *dma_handle)
77762306a36Sopenharmony_ci{
77862306a36Sopenharmony_ci	struct bnxt_hwrm_ctx *ctx = __hwrm_ctx(bp, req);
77962306a36Sopenharmony_ci	u8 *end = ((u8 *)req) + BNXT_HWRM_DMA_SIZE;
78062306a36Sopenharmony_ci	struct input *input = req;
78162306a36Sopenharmony_ci	u8 *addr, *req_addr = req;
78262306a36Sopenharmony_ci	u32 max_offset, offset;
78362306a36Sopenharmony_ci
78462306a36Sopenharmony_ci	if (!ctx)
78562306a36Sopenharmony_ci		return NULL;
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci	max_offset = BNXT_HWRM_DMA_SIZE - ctx->allocated;
78862306a36Sopenharmony_ci	offset = max_offset - size;
78962306a36Sopenharmony_ci	offset = ALIGN_DOWN(offset, BNXT_HWRM_DMA_ALIGN);
79062306a36Sopenharmony_ci	addr = req_addr + offset;
79162306a36Sopenharmony_ci
79262306a36Sopenharmony_ci	if (addr < req_addr + max_offset && req_addr + ctx->req_len <= addr) {
79362306a36Sopenharmony_ci		ctx->allocated = end - addr;
79462306a36Sopenharmony_ci		*dma_handle = ctx->dma_handle + offset;
79562306a36Sopenharmony_ci		return addr;
79662306a36Sopenharmony_ci	}
79762306a36Sopenharmony_ci
79862306a36Sopenharmony_ci	/* could not suballocate from ctx buffer, try create a new mapping */
79962306a36Sopenharmony_ci	if (ctx->slice_addr) {
80062306a36Sopenharmony_ci		/* if one exists, can only be due to software bug, be loud */
80162306a36Sopenharmony_ci		netdev_err(bp->dev, "HWRM refusing to reallocate DMA slice, req_type = %u\n",
80262306a36Sopenharmony_ci			   (u32)le16_to_cpu(input->req_type));
80362306a36Sopenharmony_ci		dump_stack();
80462306a36Sopenharmony_ci		return NULL;
80562306a36Sopenharmony_ci	}
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci	addr = dma_alloc_coherent(&bp->pdev->dev, size, dma_handle, ctx->gfp);
80862306a36Sopenharmony_ci
80962306a36Sopenharmony_ci	if (!addr)
81062306a36Sopenharmony_ci		return NULL;
81162306a36Sopenharmony_ci
81262306a36Sopenharmony_ci	ctx->slice_addr = addr;
81362306a36Sopenharmony_ci	ctx->slice_size = size;
81462306a36Sopenharmony_ci	ctx->slice_handle = *dma_handle;
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_ci	return addr;
81762306a36Sopenharmony_ci}
818