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#ifndef BNXT_HWRM_H
1162306a36Sopenharmony_ci#define BNXT_HWRM_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include "bnxt_hsi.h"
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_cienum bnxt_hwrm_ctx_flags {
1662306a36Sopenharmony_ci	/* Update the HWRM_API_FLAGS right below for any new non-internal bit added here */
1762306a36Sopenharmony_ci	BNXT_HWRM_INTERNAL_CTX_OWNED	= BIT(0), /* caller owns the context */
1862306a36Sopenharmony_ci	BNXT_HWRM_INTERNAL_RESP_DIRTY	= BIT(1), /* response contains data */
1962306a36Sopenharmony_ci	BNXT_HWRM_CTX_SILENT		= BIT(2), /* squelch firmware errors */
2062306a36Sopenharmony_ci	BNXT_HWRM_FULL_WAIT		= BIT(3), /* wait for full timeout of HWRM command */
2162306a36Sopenharmony_ci};
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define HWRM_API_FLAGS (BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT)
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_cistruct bnxt_hwrm_ctx {
2662306a36Sopenharmony_ci	u64 sentinel;
2762306a36Sopenharmony_ci	dma_addr_t dma_handle;
2862306a36Sopenharmony_ci	struct output *resp;
2962306a36Sopenharmony_ci	struct input *req;
3062306a36Sopenharmony_ci	dma_addr_t slice_handle;
3162306a36Sopenharmony_ci	void *slice_addr;
3262306a36Sopenharmony_ci	u32 slice_size;
3362306a36Sopenharmony_ci	u32 req_len;
3462306a36Sopenharmony_ci	enum bnxt_hwrm_ctx_flags flags;
3562306a36Sopenharmony_ci	unsigned int timeout;
3662306a36Sopenharmony_ci	u32 allocated;
3762306a36Sopenharmony_ci	gfp_t gfp;
3862306a36Sopenharmony_ci};
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cienum bnxt_hwrm_wait_state {
4162306a36Sopenharmony_ci	BNXT_HWRM_PENDING,
4262306a36Sopenharmony_ci	BNXT_HWRM_DEFERRED,
4362306a36Sopenharmony_ci	BNXT_HWRM_COMPLETE,
4462306a36Sopenharmony_ci	BNXT_HWRM_CANCELLED,
4562306a36Sopenharmony_ci};
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cienum bnxt_hwrm_chnl { BNXT_HWRM_CHNL_CHIMP, BNXT_HWRM_CHNL_KONG };
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cistruct bnxt_hwrm_wait_token {
5062306a36Sopenharmony_ci	struct rcu_head rcu;
5162306a36Sopenharmony_ci	struct hlist_node node;
5262306a36Sopenharmony_ci	enum bnxt_hwrm_wait_state state;
5362306a36Sopenharmony_ci	enum bnxt_hwrm_chnl dst;
5462306a36Sopenharmony_ci	u16 seq_id;
5562306a36Sopenharmony_ci};
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_civoid hwrm_update_token(struct bnxt *bp, u16 seq, enum bnxt_hwrm_wait_state s);
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci#define BNXT_HWRM_MAX_REQ_LEN		(bp->hwrm_max_req_len)
6062306a36Sopenharmony_ci#define BNXT_HWRM_SHORT_REQ_LEN		sizeof(struct hwrm_short_input)
6162306a36Sopenharmony_ci#define HWRM_CMD_MAX_TIMEOUT		40000U
6262306a36Sopenharmony_ci#define SHORT_HWRM_CMD_TIMEOUT		20
6362306a36Sopenharmony_ci#define HWRM_CMD_TIMEOUT		(bp->hwrm_cmd_timeout)
6462306a36Sopenharmony_ci#define HWRM_RESET_TIMEOUT		((HWRM_CMD_TIMEOUT) * 4)
6562306a36Sopenharmony_ci#define BNXT_HWRM_TARGET		0xffff
6662306a36Sopenharmony_ci#define BNXT_HWRM_NO_CMPL_RING		-1
6762306a36Sopenharmony_ci#define BNXT_HWRM_REQ_MAX_SIZE		128
6862306a36Sopenharmony_ci#define BNXT_HWRM_DMA_SIZE		(2 * PAGE_SIZE) /* space for req+resp */
6962306a36Sopenharmony_ci#define BNXT_HWRM_RESP_RESERVED		PAGE_SIZE
7062306a36Sopenharmony_ci#define BNXT_HWRM_RESP_OFFSET		(BNXT_HWRM_DMA_SIZE -		\
7162306a36Sopenharmony_ci					 BNXT_HWRM_RESP_RESERVED)
7262306a36Sopenharmony_ci#define BNXT_HWRM_CTX_OFFSET		(BNXT_HWRM_RESP_OFFSET -	\
7362306a36Sopenharmony_ci					 sizeof(struct bnxt_hwrm_ctx))
7462306a36Sopenharmony_ci#define BNXT_HWRM_DMA_ALIGN		16
7562306a36Sopenharmony_ci#define BNXT_HWRM_SENTINEL		0xb6e1f68a12e9a7eb /* arbitrary value */
7662306a36Sopenharmony_ci#define BNXT_HWRM_REQS_PER_PAGE		(BNXT_PAGE_SIZE /	\
7762306a36Sopenharmony_ci					 BNXT_HWRM_REQ_MAX_SIZE)
7862306a36Sopenharmony_ci#define HWRM_SHORT_MIN_TIMEOUT		3
7962306a36Sopenharmony_ci#define HWRM_SHORT_MAX_TIMEOUT		10
8062306a36Sopenharmony_ci#define HWRM_SHORT_TIMEOUT_COUNTER	5
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci#define HWRM_MIN_TIMEOUT		25
8362306a36Sopenharmony_ci#define HWRM_MAX_TIMEOUT		40
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_cistatic inline unsigned int hwrm_total_timeout(unsigned int n)
8662306a36Sopenharmony_ci{
8762306a36Sopenharmony_ci	return n <= HWRM_SHORT_TIMEOUT_COUNTER ? n * HWRM_SHORT_MIN_TIMEOUT :
8862306a36Sopenharmony_ci		HWRM_SHORT_TIMEOUT_COUNTER * HWRM_SHORT_MIN_TIMEOUT +
8962306a36Sopenharmony_ci		(n - HWRM_SHORT_TIMEOUT_COUNTER) * HWRM_MIN_TIMEOUT;
9062306a36Sopenharmony_ci}
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci#define HWRM_VALID_BIT_DELAY_USEC	50000
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistatic inline bool bnxt_cfa_hwrm_message(u16 req_type)
9662306a36Sopenharmony_ci{
9762306a36Sopenharmony_ci	switch (req_type) {
9862306a36Sopenharmony_ci	case HWRM_CFA_ENCAP_RECORD_ALLOC:
9962306a36Sopenharmony_ci	case HWRM_CFA_ENCAP_RECORD_FREE:
10062306a36Sopenharmony_ci	case HWRM_CFA_DECAP_FILTER_ALLOC:
10162306a36Sopenharmony_ci	case HWRM_CFA_DECAP_FILTER_FREE:
10262306a36Sopenharmony_ci	case HWRM_CFA_EM_FLOW_ALLOC:
10362306a36Sopenharmony_ci	case HWRM_CFA_EM_FLOW_FREE:
10462306a36Sopenharmony_ci	case HWRM_CFA_EM_FLOW_CFG:
10562306a36Sopenharmony_ci	case HWRM_CFA_FLOW_ALLOC:
10662306a36Sopenharmony_ci	case HWRM_CFA_FLOW_FREE:
10762306a36Sopenharmony_ci	case HWRM_CFA_FLOW_INFO:
10862306a36Sopenharmony_ci	case HWRM_CFA_FLOW_FLUSH:
10962306a36Sopenharmony_ci	case HWRM_CFA_FLOW_STATS:
11062306a36Sopenharmony_ci	case HWRM_CFA_METER_PROFILE_ALLOC:
11162306a36Sopenharmony_ci	case HWRM_CFA_METER_PROFILE_FREE:
11262306a36Sopenharmony_ci	case HWRM_CFA_METER_PROFILE_CFG:
11362306a36Sopenharmony_ci	case HWRM_CFA_METER_INSTANCE_ALLOC:
11462306a36Sopenharmony_ci	case HWRM_CFA_METER_INSTANCE_FREE:
11562306a36Sopenharmony_ci		return true;
11662306a36Sopenharmony_ci	default:
11762306a36Sopenharmony_ci		return false;
11862306a36Sopenharmony_ci	}
11962306a36Sopenharmony_ci}
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_cistatic inline bool bnxt_kong_hwrm_message(struct bnxt *bp, struct input *req)
12262306a36Sopenharmony_ci{
12362306a36Sopenharmony_ci	return (bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL &&
12462306a36Sopenharmony_ci		(bnxt_cfa_hwrm_message(le16_to_cpu(req->req_type)) ||
12562306a36Sopenharmony_ci		 le16_to_cpu(req->target_id) == HWRM_TARGET_ID_KONG));
12662306a36Sopenharmony_ci}
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciint __hwrm_req_init(struct bnxt *bp, void **req, u16 req_type, u32 req_len);
12962306a36Sopenharmony_ci#define hwrm_req_init(bp, req, req_type) \
13062306a36Sopenharmony_ci	__hwrm_req_init((bp), (void **)&(req), (req_type), sizeof(*(req)))
13162306a36Sopenharmony_civoid *hwrm_req_hold(struct bnxt *bp, void *req);
13262306a36Sopenharmony_civoid hwrm_req_drop(struct bnxt *bp, void *req);
13362306a36Sopenharmony_civoid hwrm_req_flags(struct bnxt *bp, void *req, enum bnxt_hwrm_ctx_flags flags);
13462306a36Sopenharmony_civoid hwrm_req_timeout(struct bnxt *bp, void *req, unsigned int timeout);
13562306a36Sopenharmony_ciint hwrm_req_send(struct bnxt *bp, void *req);
13662306a36Sopenharmony_ciint hwrm_req_send_silent(struct bnxt *bp, void *req);
13762306a36Sopenharmony_ciint hwrm_req_replace(struct bnxt *bp, void *req, void *new_req, u32 len);
13862306a36Sopenharmony_civoid hwrm_req_alloc_flags(struct bnxt *bp, void *req, gfp_t flags);
13962306a36Sopenharmony_civoid *hwrm_req_dma_slice(struct bnxt *bp, void *req, u32 size, dma_addr_t *dma);
14062306a36Sopenharmony_ci#endif
141