xref: /openbmc/linux/drivers/infiniband/hw/efa/efa_com.c (revision ac8f933664c3a0e2d42f6ee9a2a6d25f87cb23f6)
10420e542SGal Pressman // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
20420e542SGal Pressman /*
3*fd867e74SMichael Margolin  * Copyright 2018-2024 Amazon.com, Inc. or its affiliates. All rights reserved.
40420e542SGal Pressman  */
50420e542SGal Pressman 
60420e542SGal Pressman #include "efa_com.h"
70420e542SGal Pressman #include "efa_regs_defs.h"
80420e542SGal Pressman 
90420e542SGal Pressman #define ADMIN_CMD_TIMEOUT_US 30000000 /* usecs */
100420e542SGal Pressman 
110420e542SGal Pressman #define EFA_REG_READ_TIMEOUT_US 50000 /* usecs */
120420e542SGal Pressman #define EFA_MMIO_READ_INVALID 0xffffffff
130420e542SGal Pressman 
140420e542SGal Pressman #define EFA_POLL_INTERVAL_MS 100 /* msecs */
150420e542SGal Pressman 
160420e542SGal Pressman #define EFA_ASYNC_QUEUE_DEPTH 16
170420e542SGal Pressman #define EFA_ADMIN_QUEUE_DEPTH 32
180420e542SGal Pressman 
190420e542SGal Pressman #define EFA_CTRL_MAJOR          0
200420e542SGal Pressman #define EFA_CTRL_MINOR          0
210420e542SGal Pressman #define EFA_CTRL_SUB_MINOR      1
220420e542SGal Pressman 
230420e542SGal Pressman enum efa_cmd_status {
240420e542SGal Pressman 	EFA_CMD_SUBMITTED,
250420e542SGal Pressman 	EFA_CMD_COMPLETED,
260420e542SGal Pressman };
270420e542SGal Pressman 
280420e542SGal Pressman struct efa_comp_ctx {
290420e542SGal Pressman 	struct completion wait_event;
300420e542SGal Pressman 	struct efa_admin_acq_entry *user_cqe;
310420e542SGal Pressman 	u32 comp_size;
320420e542SGal Pressman 	enum efa_cmd_status status;
330420e542SGal Pressman 	u8 cmd_opcode;
340420e542SGal Pressman 	u8 occupied;
350420e542SGal Pressman };
360420e542SGal Pressman 
efa_com_cmd_str(u8 cmd)370420e542SGal Pressman static const char *efa_com_cmd_str(u8 cmd)
380420e542SGal Pressman {
390420e542SGal Pressman #define EFA_CMD_STR_CASE(_cmd) case EFA_ADMIN_##_cmd: return #_cmd
400420e542SGal Pressman 
410420e542SGal Pressman 	switch (cmd) {
420420e542SGal Pressman 	EFA_CMD_STR_CASE(CREATE_QP);
430420e542SGal Pressman 	EFA_CMD_STR_CASE(MODIFY_QP);
440420e542SGal Pressman 	EFA_CMD_STR_CASE(QUERY_QP);
450420e542SGal Pressman 	EFA_CMD_STR_CASE(DESTROY_QP);
460420e542SGal Pressman 	EFA_CMD_STR_CASE(CREATE_AH);
470420e542SGal Pressman 	EFA_CMD_STR_CASE(DESTROY_AH);
480420e542SGal Pressman 	EFA_CMD_STR_CASE(REG_MR);
490420e542SGal Pressman 	EFA_CMD_STR_CASE(DEREG_MR);
500420e542SGal Pressman 	EFA_CMD_STR_CASE(CREATE_CQ);
510420e542SGal Pressman 	EFA_CMD_STR_CASE(DESTROY_CQ);
520420e542SGal Pressman 	EFA_CMD_STR_CASE(GET_FEATURE);
530420e542SGal Pressman 	EFA_CMD_STR_CASE(SET_FEATURE);
540420e542SGal Pressman 	EFA_CMD_STR_CASE(GET_STATS);
550420e542SGal Pressman 	EFA_CMD_STR_CASE(ALLOC_PD);
560420e542SGal Pressman 	EFA_CMD_STR_CASE(DEALLOC_PD);
570420e542SGal Pressman 	EFA_CMD_STR_CASE(ALLOC_UAR);
580420e542SGal Pressman 	EFA_CMD_STR_CASE(DEALLOC_UAR);
592a152512SGal Pressman 	EFA_CMD_STR_CASE(CREATE_EQ);
602a152512SGal Pressman 	EFA_CMD_STR_CASE(DESTROY_EQ);
610420e542SGal Pressman 	default: return "unknown command opcode";
620420e542SGal Pressman 	}
630420e542SGal Pressman #undef EFA_CMD_STR_CASE
640420e542SGal Pressman }
650420e542SGal Pressman 
efa_com_set_dma_addr(dma_addr_t addr,u32 * addr_high,u32 * addr_low)662a152512SGal Pressman void efa_com_set_dma_addr(dma_addr_t addr, u32 *addr_high, u32 *addr_low)
672a152512SGal Pressman {
682a152512SGal Pressman 	*addr_low = lower_32_bits(addr);
692a152512SGal Pressman 	*addr_high = upper_32_bits(addr);
702a152512SGal Pressman }
712a152512SGal Pressman 
efa_com_reg_read32(struct efa_com_dev * edev,u16 offset)720420e542SGal Pressman static u32 efa_com_reg_read32(struct efa_com_dev *edev, u16 offset)
730420e542SGal Pressman {
740420e542SGal Pressman 	struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
750420e542SGal Pressman 	struct efa_admin_mmio_req_read_less_resp *read_resp;
760420e542SGal Pressman 	unsigned long exp_time;
7788d03307SGal Pressman 	u32 mmio_read_reg = 0;
780420e542SGal Pressman 	u32 err;
790420e542SGal Pressman 
800420e542SGal Pressman 	read_resp = mmio_read->read_resp;
810420e542SGal Pressman 
820420e542SGal Pressman 	spin_lock(&mmio_read->lock);
830420e542SGal Pressman 	mmio_read->seq_num++;
840420e542SGal Pressman 
850420e542SGal Pressman 	/* trash DMA req_id to identify when hardware is done */
860420e542SGal Pressman 	read_resp->req_id = mmio_read->seq_num + 0x9aL;
8788d03307SGal Pressman 	EFA_SET(&mmio_read_reg, EFA_REGS_MMIO_REG_READ_REG_OFF, offset);
8888d03307SGal Pressman 	EFA_SET(&mmio_read_reg, EFA_REGS_MMIO_REG_READ_REQ_ID,
8988d03307SGal Pressman 		mmio_read->seq_num);
900420e542SGal Pressman 
910420e542SGal Pressman 	writel(mmio_read_reg, edev->reg_bar + EFA_REGS_MMIO_REG_READ_OFF);
920420e542SGal Pressman 
930420e542SGal Pressman 	exp_time = jiffies + usecs_to_jiffies(mmio_read->mmio_read_timeout);
940420e542SGal Pressman 	do {
950420e542SGal Pressman 		if (READ_ONCE(read_resp->req_id) == mmio_read->seq_num)
960420e542SGal Pressman 			break;
970420e542SGal Pressman 		udelay(1);
980420e542SGal Pressman 	} while (time_is_after_jiffies(exp_time));
990420e542SGal Pressman 
1000420e542SGal Pressman 	if (read_resp->req_id != mmio_read->seq_num) {
101cfa1f5f2SGal Pressman 		ibdev_err_ratelimited(
102cfa1f5f2SGal Pressman 			edev->efa_dev,
1030420e542SGal Pressman 			"Reading register timed out. expected: req id[%u] offset[%#x] actual: req id[%u] offset[%#x]\n",
1040420e542SGal Pressman 			mmio_read->seq_num, offset, read_resp->req_id,
1050420e542SGal Pressman 			read_resp->reg_off);
1060420e542SGal Pressman 		err = EFA_MMIO_READ_INVALID;
1070420e542SGal Pressman 		goto out;
1080420e542SGal Pressman 	}
1090420e542SGal Pressman 
1100420e542SGal Pressman 	if (read_resp->reg_off != offset) {
111cfa1f5f2SGal Pressman 		ibdev_err_ratelimited(
112cfa1f5f2SGal Pressman 			edev->efa_dev,
1130420e542SGal Pressman 			"Reading register failed: wrong offset provided\n");
1140420e542SGal Pressman 		err = EFA_MMIO_READ_INVALID;
1150420e542SGal Pressman 		goto out;
1160420e542SGal Pressman 	}
1170420e542SGal Pressman 
1180420e542SGal Pressman 	err = read_resp->reg_val;
1190420e542SGal Pressman out:
1200420e542SGal Pressman 	spin_unlock(&mmio_read->lock);
1210420e542SGal Pressman 	return err;
1220420e542SGal Pressman }
1230420e542SGal Pressman 
efa_com_admin_init_sq(struct efa_com_dev * edev)1240420e542SGal Pressman static int efa_com_admin_init_sq(struct efa_com_dev *edev)
1250420e542SGal Pressman {
1260420e542SGal Pressman 	struct efa_com_admin_queue *aq = &edev->aq;
1270420e542SGal Pressman 	struct efa_com_admin_sq *sq = &aq->sq;
1280420e542SGal Pressman 	u16 size = aq->depth * sizeof(*sq->entries);
12988d03307SGal Pressman 	u32 aq_caps = 0;
1300420e542SGal Pressman 	u32 addr_high;
1310420e542SGal Pressman 	u32 addr_low;
1320420e542SGal Pressman 
1330420e542SGal Pressman 	sq->entries =
1340420e542SGal Pressman 		dma_alloc_coherent(aq->dmadev, size, &sq->dma_addr, GFP_KERNEL);
1350420e542SGal Pressman 	if (!sq->entries)
1360420e542SGal Pressman 		return -ENOMEM;
1370420e542SGal Pressman 
1380420e542SGal Pressman 	spin_lock_init(&sq->lock);
1390420e542SGal Pressman 
1400420e542SGal Pressman 	sq->cc = 0;
1410420e542SGal Pressman 	sq->pc = 0;
1420420e542SGal Pressman 	sq->phase = 1;
1430420e542SGal Pressman 
1440420e542SGal Pressman 	sq->db_addr = (u32 __iomem *)(edev->reg_bar + EFA_REGS_AQ_PROD_DB_OFF);
1450420e542SGal Pressman 
146c2a50568SGal Pressman 	addr_high = upper_32_bits(sq->dma_addr);
147c2a50568SGal Pressman 	addr_low = lower_32_bits(sq->dma_addr);
1480420e542SGal Pressman 
1490420e542SGal Pressman 	writel(addr_low, edev->reg_bar + EFA_REGS_AQ_BASE_LO_OFF);
1500420e542SGal Pressman 	writel(addr_high, edev->reg_bar + EFA_REGS_AQ_BASE_HI_OFF);
1510420e542SGal Pressman 
15288d03307SGal Pressman 	EFA_SET(&aq_caps, EFA_REGS_AQ_CAPS_AQ_DEPTH, aq->depth);
15388d03307SGal Pressman 	EFA_SET(&aq_caps, EFA_REGS_AQ_CAPS_AQ_ENTRY_SIZE,
15488d03307SGal Pressman 		sizeof(struct efa_admin_aq_entry));
1550420e542SGal Pressman 
1560420e542SGal Pressman 	writel(aq_caps, edev->reg_bar + EFA_REGS_AQ_CAPS_OFF);
1570420e542SGal Pressman 
1580420e542SGal Pressman 	return 0;
1590420e542SGal Pressman }
1600420e542SGal Pressman 
efa_com_admin_init_cq(struct efa_com_dev * edev)1610420e542SGal Pressman static int efa_com_admin_init_cq(struct efa_com_dev *edev)
1620420e542SGal Pressman {
1630420e542SGal Pressman 	struct efa_com_admin_queue *aq = &edev->aq;
1640420e542SGal Pressman 	struct efa_com_admin_cq *cq = &aq->cq;
1650420e542SGal Pressman 	u16 size = aq->depth * sizeof(*cq->entries);
16688d03307SGal Pressman 	u32 acq_caps = 0;
1670420e542SGal Pressman 	u32 addr_high;
1680420e542SGal Pressman 	u32 addr_low;
1690420e542SGal Pressman 
1700420e542SGal Pressman 	cq->entries =
1710420e542SGal Pressman 		dma_alloc_coherent(aq->dmadev, size, &cq->dma_addr, GFP_KERNEL);
1720420e542SGal Pressman 	if (!cq->entries)
1730420e542SGal Pressman 		return -ENOMEM;
1740420e542SGal Pressman 
1750420e542SGal Pressman 	spin_lock_init(&cq->lock);
1760420e542SGal Pressman 
1770420e542SGal Pressman 	cq->cc = 0;
1780420e542SGal Pressman 	cq->phase = 1;
1790420e542SGal Pressman 
180c2a50568SGal Pressman 	addr_high = upper_32_bits(cq->dma_addr);
181c2a50568SGal Pressman 	addr_low = lower_32_bits(cq->dma_addr);
1820420e542SGal Pressman 
1830420e542SGal Pressman 	writel(addr_low, edev->reg_bar + EFA_REGS_ACQ_BASE_LO_OFF);
1840420e542SGal Pressman 	writel(addr_high, edev->reg_bar + EFA_REGS_ACQ_BASE_HI_OFF);
1850420e542SGal Pressman 
18688d03307SGal Pressman 	EFA_SET(&acq_caps, EFA_REGS_ACQ_CAPS_ACQ_DEPTH, aq->depth);
18788d03307SGal Pressman 	EFA_SET(&acq_caps, EFA_REGS_ACQ_CAPS_ACQ_ENTRY_SIZE,
18888d03307SGal Pressman 		sizeof(struct efa_admin_acq_entry));
18988d03307SGal Pressman 	EFA_SET(&acq_caps, EFA_REGS_ACQ_CAPS_ACQ_MSIX_VECTOR,
19088d03307SGal Pressman 		aq->msix_vector_idx);
1910420e542SGal Pressman 
1920420e542SGal Pressman 	writel(acq_caps, edev->reg_bar + EFA_REGS_ACQ_CAPS_OFF);
1930420e542SGal Pressman 
1940420e542SGal Pressman 	return 0;
1950420e542SGal Pressman }
1960420e542SGal Pressman 
efa_com_admin_init_aenq(struct efa_com_dev * edev,struct efa_aenq_handlers * aenq_handlers)1970420e542SGal Pressman static int efa_com_admin_init_aenq(struct efa_com_dev *edev,
1980420e542SGal Pressman 				   struct efa_aenq_handlers *aenq_handlers)
1990420e542SGal Pressman {
2000420e542SGal Pressman 	struct efa_com_aenq *aenq = &edev->aenq;
20188d03307SGal Pressman 	u32 addr_low, addr_high;
20288d03307SGal Pressman 	u32 aenq_caps = 0;
2030420e542SGal Pressman 	u16 size;
2040420e542SGal Pressman 
2050420e542SGal Pressman 	if (!aenq_handlers) {
2060420e542SGal Pressman 		ibdev_err(edev->efa_dev, "aenq handlers pointer is NULL\n");
2070420e542SGal Pressman 		return -EINVAL;
2080420e542SGal Pressman 	}
2090420e542SGal Pressman 
2100420e542SGal Pressman 	size = EFA_ASYNC_QUEUE_DEPTH * sizeof(*aenq->entries);
2110420e542SGal Pressman 	aenq->entries = dma_alloc_coherent(edev->dmadev, size, &aenq->dma_addr,
2120420e542SGal Pressman 					   GFP_KERNEL);
2130420e542SGal Pressman 	if (!aenq->entries)
2140420e542SGal Pressman 		return -ENOMEM;
2150420e542SGal Pressman 
2160420e542SGal Pressman 	aenq->aenq_handlers = aenq_handlers;
2170420e542SGal Pressman 	aenq->depth = EFA_ASYNC_QUEUE_DEPTH;
2180420e542SGal Pressman 	aenq->cc = 0;
2190420e542SGal Pressman 	aenq->phase = 1;
2200420e542SGal Pressman 
221c2a50568SGal Pressman 	addr_low = lower_32_bits(aenq->dma_addr);
222c2a50568SGal Pressman 	addr_high = upper_32_bits(aenq->dma_addr);
2230420e542SGal Pressman 
2240420e542SGal Pressman 	writel(addr_low, edev->reg_bar + EFA_REGS_AENQ_BASE_LO_OFF);
2250420e542SGal Pressman 	writel(addr_high, edev->reg_bar + EFA_REGS_AENQ_BASE_HI_OFF);
2260420e542SGal Pressman 
22788d03307SGal Pressman 	EFA_SET(&aenq_caps, EFA_REGS_AENQ_CAPS_AENQ_DEPTH, aenq->depth);
22888d03307SGal Pressman 	EFA_SET(&aenq_caps, EFA_REGS_AENQ_CAPS_AENQ_ENTRY_SIZE,
22988d03307SGal Pressman 		sizeof(struct efa_admin_aenq_entry));
23088d03307SGal Pressman 	EFA_SET(&aenq_caps, EFA_REGS_AENQ_CAPS_AENQ_MSIX_VECTOR,
23188d03307SGal Pressman 		aenq->msix_vector_idx);
2320420e542SGal Pressman 	writel(aenq_caps, edev->reg_bar + EFA_REGS_AENQ_CAPS_OFF);
2330420e542SGal Pressman 
2340420e542SGal Pressman 	/*
2350420e542SGal Pressman 	 * Init cons_db to mark that all entries in the queue
2360420e542SGal Pressman 	 * are initially available
2370420e542SGal Pressman 	 */
2380420e542SGal Pressman 	writel(edev->aenq.cc, edev->reg_bar + EFA_REGS_AENQ_CONS_DB_OFF);
2390420e542SGal Pressman 
2400420e542SGal Pressman 	return 0;
2410420e542SGal Pressman }
2420420e542SGal Pressman 
2430420e542SGal Pressman /* ID to be used with efa_com_get_comp_ctx */
efa_com_alloc_ctx_id(struct efa_com_admin_queue * aq)2440420e542SGal Pressman static u16 efa_com_alloc_ctx_id(struct efa_com_admin_queue *aq)
2450420e542SGal Pressman {
2460420e542SGal Pressman 	u16 ctx_id;
2470420e542SGal Pressman 
2480420e542SGal Pressman 	spin_lock(&aq->comp_ctx_lock);
2490420e542SGal Pressman 	ctx_id = aq->comp_ctx_pool[aq->comp_ctx_pool_next];
2500420e542SGal Pressman 	aq->comp_ctx_pool_next++;
2510420e542SGal Pressman 	spin_unlock(&aq->comp_ctx_lock);
2520420e542SGal Pressman 
2530420e542SGal Pressman 	return ctx_id;
2540420e542SGal Pressman }
2550420e542SGal Pressman 
efa_com_dealloc_ctx_id(struct efa_com_admin_queue * aq,u16 ctx_id)2560420e542SGal Pressman static void efa_com_dealloc_ctx_id(struct efa_com_admin_queue *aq,
2570420e542SGal Pressman 				   u16 ctx_id)
2580420e542SGal Pressman {
2590420e542SGal Pressman 	spin_lock(&aq->comp_ctx_lock);
2600420e542SGal Pressman 	aq->comp_ctx_pool_next--;
2610420e542SGal Pressman 	aq->comp_ctx_pool[aq->comp_ctx_pool_next] = ctx_id;
2620420e542SGal Pressman 	spin_unlock(&aq->comp_ctx_lock);
2630420e542SGal Pressman }
2640420e542SGal Pressman 
efa_com_put_comp_ctx(struct efa_com_admin_queue * aq,struct efa_comp_ctx * comp_ctx)2650420e542SGal Pressman static inline void efa_com_put_comp_ctx(struct efa_com_admin_queue *aq,
2660420e542SGal Pressman 					struct efa_comp_ctx *comp_ctx)
2670420e542SGal Pressman {
26888d03307SGal Pressman 	u16 cmd_id = EFA_GET(&comp_ctx->user_cqe->acq_common_descriptor.command,
26988d03307SGal Pressman 			     EFA_ADMIN_ACQ_COMMON_DESC_COMMAND_ID);
270bcde9a83SDaniel Kranzdorf 	u16 ctx_id = cmd_id & (aq->depth - 1);
2710420e542SGal Pressman 
272bcde9a83SDaniel Kranzdorf 	ibdev_dbg(aq->efa_dev, "Put completion command_id %#x\n", cmd_id);
2730420e542SGal Pressman 	comp_ctx->occupied = 0;
274bcde9a83SDaniel Kranzdorf 	efa_com_dealloc_ctx_id(aq, ctx_id);
2750420e542SGal Pressman }
2760420e542SGal Pressman 
efa_com_get_comp_ctx(struct efa_com_admin_queue * aq,u16 cmd_id,bool capture)2770420e542SGal Pressman static struct efa_comp_ctx *efa_com_get_comp_ctx(struct efa_com_admin_queue *aq,
278bcde9a83SDaniel Kranzdorf 						 u16 cmd_id, bool capture)
2790420e542SGal Pressman {
280bcde9a83SDaniel Kranzdorf 	u16 ctx_id = cmd_id & (aq->depth - 1);
2810420e542SGal Pressman 
282bcde9a83SDaniel Kranzdorf 	if (aq->comp_ctx[ctx_id].occupied && capture) {
283cfa1f5f2SGal Pressman 		ibdev_err_ratelimited(
284cfa1f5f2SGal Pressman 			aq->efa_dev,
285bcde9a83SDaniel Kranzdorf 			"Completion context for command_id %#x is occupied\n",
286bcde9a83SDaniel Kranzdorf 			cmd_id);
2870420e542SGal Pressman 		return NULL;
2880420e542SGal Pressman 	}
2890420e542SGal Pressman 
2900420e542SGal Pressman 	if (capture) {
291bcde9a83SDaniel Kranzdorf 		aq->comp_ctx[ctx_id].occupied = 1;
292bcde9a83SDaniel Kranzdorf 		ibdev_dbg(aq->efa_dev,
293bcde9a83SDaniel Kranzdorf 			  "Take completion ctxt for command_id %#x\n", cmd_id);
2940420e542SGal Pressman 	}
2950420e542SGal Pressman 
296bcde9a83SDaniel Kranzdorf 	return &aq->comp_ctx[ctx_id];
2970420e542SGal Pressman }
2980420e542SGal Pressman 
__efa_com_submit_admin_cmd(struct efa_com_admin_queue * aq,struct efa_admin_aq_entry * cmd,size_t cmd_size_in_bytes,struct efa_admin_acq_entry * comp,size_t comp_size_in_bytes)2990420e542SGal Pressman static struct efa_comp_ctx *__efa_com_submit_admin_cmd(struct efa_com_admin_queue *aq,
3000420e542SGal Pressman 						       struct efa_admin_aq_entry *cmd,
3010420e542SGal Pressman 						       size_t cmd_size_in_bytes,
3020420e542SGal Pressman 						       struct efa_admin_acq_entry *comp,
3030420e542SGal Pressman 						       size_t comp_size_in_bytes)
3040420e542SGal Pressman {
30564c26487SGal Pressman 	struct efa_admin_aq_entry *aqe;
3060420e542SGal Pressman 	struct efa_comp_ctx *comp_ctx;
3070420e542SGal Pressman 	u16 queue_size_mask;
308bcde9a83SDaniel Kranzdorf 	u16 cmd_id;
3090420e542SGal Pressman 	u16 ctx_id;
3100420e542SGal Pressman 	u16 pi;
3110420e542SGal Pressman 
3120420e542SGal Pressman 	queue_size_mask = aq->depth - 1;
3130420e542SGal Pressman 	pi = aq->sq.pc & queue_size_mask;
3140420e542SGal Pressman 
3150420e542SGal Pressman 	ctx_id = efa_com_alloc_ctx_id(aq);
3160420e542SGal Pressman 
317bcde9a83SDaniel Kranzdorf 	/* cmd_id LSBs are the ctx_id and MSBs are entropy bits from pc */
318bcde9a83SDaniel Kranzdorf 	cmd_id = ctx_id & queue_size_mask;
319bcde9a83SDaniel Kranzdorf 	cmd_id |= aq->sq.pc & ~queue_size_mask;
320bcde9a83SDaniel Kranzdorf 	cmd_id &= EFA_ADMIN_AQ_COMMON_DESC_COMMAND_ID_MASK;
321bcde9a83SDaniel Kranzdorf 
322bcde9a83SDaniel Kranzdorf 	cmd->aq_common_descriptor.command_id = cmd_id;
32388d03307SGal Pressman 	EFA_SET(&cmd->aq_common_descriptor.flags,
32488d03307SGal Pressman 		EFA_ADMIN_AQ_COMMON_DESC_PHASE, aq->sq.phase);
3250420e542SGal Pressman 
326bcde9a83SDaniel Kranzdorf 	comp_ctx = efa_com_get_comp_ctx(aq, cmd_id, true);
3270420e542SGal Pressman 	if (!comp_ctx) {
3280420e542SGal Pressman 		efa_com_dealloc_ctx_id(aq, ctx_id);
3290420e542SGal Pressman 		return ERR_PTR(-EINVAL);
3300420e542SGal Pressman 	}
3310420e542SGal Pressman 
3320420e542SGal Pressman 	comp_ctx->status = EFA_CMD_SUBMITTED;
3330420e542SGal Pressman 	comp_ctx->comp_size = comp_size_in_bytes;
3340420e542SGal Pressman 	comp_ctx->user_cqe = comp;
3350420e542SGal Pressman 	comp_ctx->cmd_opcode = cmd->aq_common_descriptor.opcode;
3360420e542SGal Pressman 
3370420e542SGal Pressman 	reinit_completion(&comp_ctx->wait_event);
3380420e542SGal Pressman 
33964c26487SGal Pressman 	aqe = &aq->sq.entries[pi];
34064c26487SGal Pressman 	memset(aqe, 0, sizeof(*aqe));
34164c26487SGal Pressman 	memcpy(aqe, cmd, cmd_size_in_bytes);
3420420e542SGal Pressman 
3430420e542SGal Pressman 	aq->sq.pc++;
3440420e542SGal Pressman 	atomic64_inc(&aq->stats.submitted_cmd);
3450420e542SGal Pressman 
3460420e542SGal Pressman 	if ((aq->sq.pc & queue_size_mask) == 0)
3470420e542SGal Pressman 		aq->sq.phase = !aq->sq.phase;
3480420e542SGal Pressman 
3490420e542SGal Pressman 	/* barrier not needed in case of writel */
3500420e542SGal Pressman 	writel(aq->sq.pc, aq->sq.db_addr);
3510420e542SGal Pressman 
3520420e542SGal Pressman 	return comp_ctx;
3530420e542SGal Pressman }
3540420e542SGal Pressman 
efa_com_init_comp_ctxt(struct efa_com_admin_queue * aq)3550420e542SGal Pressman static inline int efa_com_init_comp_ctxt(struct efa_com_admin_queue *aq)
3560420e542SGal Pressman {
3570420e542SGal Pressman 	size_t pool_size = aq->depth * sizeof(*aq->comp_ctx_pool);
3580420e542SGal Pressman 	size_t size = aq->depth * sizeof(struct efa_comp_ctx);
3590420e542SGal Pressman 	struct efa_comp_ctx *comp_ctx;
3600420e542SGal Pressman 	u16 i;
3610420e542SGal Pressman 
3620420e542SGal Pressman 	aq->comp_ctx = devm_kzalloc(aq->dmadev, size, GFP_KERNEL);
3630420e542SGal Pressman 	aq->comp_ctx_pool = devm_kzalloc(aq->dmadev, pool_size, GFP_KERNEL);
3640420e542SGal Pressman 	if (!aq->comp_ctx || !aq->comp_ctx_pool) {
3650420e542SGal Pressman 		devm_kfree(aq->dmadev, aq->comp_ctx_pool);
3660420e542SGal Pressman 		devm_kfree(aq->dmadev, aq->comp_ctx);
3670420e542SGal Pressman 		return -ENOMEM;
3680420e542SGal Pressman 	}
3690420e542SGal Pressman 
3700420e542SGal Pressman 	for (i = 0; i < aq->depth; i++) {
3710420e542SGal Pressman 		comp_ctx = efa_com_get_comp_ctx(aq, i, false);
3720420e542SGal Pressman 		if (comp_ctx)
3730420e542SGal Pressman 			init_completion(&comp_ctx->wait_event);
3740420e542SGal Pressman 
3750420e542SGal Pressman 		aq->comp_ctx_pool[i] = i;
3760420e542SGal Pressman 	}
3770420e542SGal Pressman 
3780420e542SGal Pressman 	spin_lock_init(&aq->comp_ctx_lock);
3790420e542SGal Pressman 
3800420e542SGal Pressman 	aq->comp_ctx_pool_next = 0;
3810420e542SGal Pressman 
3820420e542SGal Pressman 	return 0;
3830420e542SGal Pressman }
3840420e542SGal Pressman 
efa_com_submit_admin_cmd(struct efa_com_admin_queue * aq,struct efa_admin_aq_entry * cmd,size_t cmd_size_in_bytes,struct efa_admin_acq_entry * comp,size_t comp_size_in_bytes)3850420e542SGal Pressman static struct efa_comp_ctx *efa_com_submit_admin_cmd(struct efa_com_admin_queue *aq,
3860420e542SGal Pressman 						     struct efa_admin_aq_entry *cmd,
3870420e542SGal Pressman 						     size_t cmd_size_in_bytes,
3880420e542SGal Pressman 						     struct efa_admin_acq_entry *comp,
3890420e542SGal Pressman 						     size_t comp_size_in_bytes)
3900420e542SGal Pressman {
3910420e542SGal Pressman 	struct efa_comp_ctx *comp_ctx;
3920420e542SGal Pressman 
3930420e542SGal Pressman 	spin_lock(&aq->sq.lock);
3940420e542SGal Pressman 	if (!test_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state)) {
395cfa1f5f2SGal Pressman 		ibdev_err_ratelimited(aq->efa_dev, "Admin queue is closed\n");
3960420e542SGal Pressman 		spin_unlock(&aq->sq.lock);
3970420e542SGal Pressman 		return ERR_PTR(-ENODEV);
3980420e542SGal Pressman 	}
3990420e542SGal Pressman 
4000420e542SGal Pressman 	comp_ctx = __efa_com_submit_admin_cmd(aq, cmd, cmd_size_in_bytes, comp,
4010420e542SGal Pressman 					      comp_size_in_bytes);
4020420e542SGal Pressman 	spin_unlock(&aq->sq.lock);
4030420e542SGal Pressman 	if (IS_ERR(comp_ctx))
4040420e542SGal Pressman 		clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
4050420e542SGal Pressman 
4060420e542SGal Pressman 	return comp_ctx;
4070420e542SGal Pressman }
4080420e542SGal Pressman 
efa_com_handle_single_admin_completion(struct efa_com_admin_queue * aq,struct efa_admin_acq_entry * cqe)409*fd867e74SMichael Margolin static int efa_com_handle_single_admin_completion(struct efa_com_admin_queue *aq,
4100420e542SGal Pressman 						  struct efa_admin_acq_entry *cqe)
4110420e542SGal Pressman {
4120420e542SGal Pressman 	struct efa_comp_ctx *comp_ctx;
4130420e542SGal Pressman 	u16 cmd_id;
4140420e542SGal Pressman 
41588d03307SGal Pressman 	cmd_id = EFA_GET(&cqe->acq_common_descriptor.command,
41688d03307SGal Pressman 			 EFA_ADMIN_ACQ_COMMON_DESC_COMMAND_ID);
4170420e542SGal Pressman 
4180420e542SGal Pressman 	comp_ctx = efa_com_get_comp_ctx(aq, cmd_id, false);
419*fd867e74SMichael Margolin 	if (comp_ctx->status != EFA_CMD_SUBMITTED) {
4200420e542SGal Pressman 		ibdev_err(aq->efa_dev,
421*fd867e74SMichael Margolin 			  "Received completion with unexpected command id[%d], sq producer: %d, sq consumer: %d, cq consumer: %d\n",
422*fd867e74SMichael Margolin 			  cmd_id, aq->sq.pc, aq->sq.cc, aq->cq.cc);
423*fd867e74SMichael Margolin 		return -EINVAL;
4240420e542SGal Pressman 	}
4250420e542SGal Pressman 
4260420e542SGal Pressman 	comp_ctx->status = EFA_CMD_COMPLETED;
4270420e542SGal Pressman 	memcpy(comp_ctx->user_cqe, cqe, comp_ctx->comp_size);
4280420e542SGal Pressman 
4290420e542SGal Pressman 	if (!test_bit(EFA_AQ_STATE_POLLING_BIT, &aq->state))
4300420e542SGal Pressman 		complete(&comp_ctx->wait_event);
431*fd867e74SMichael Margolin 
432*fd867e74SMichael Margolin 	return 0;
4330420e542SGal Pressman }
4340420e542SGal Pressman 
efa_com_handle_admin_completion(struct efa_com_admin_queue * aq)4350420e542SGal Pressman static void efa_com_handle_admin_completion(struct efa_com_admin_queue *aq)
4360420e542SGal Pressman {
4370420e542SGal Pressman 	struct efa_admin_acq_entry *cqe;
4380420e542SGal Pressman 	u16 queue_size_mask;
439*fd867e74SMichael Margolin 	u16 comp_cmds = 0;
4400420e542SGal Pressman 	u8 phase;
441*fd867e74SMichael Margolin 	int err;
4420420e542SGal Pressman 	u16 ci;
4430420e542SGal Pressman 
4440420e542SGal Pressman 	queue_size_mask = aq->depth - 1;
4450420e542SGal Pressman 
4460420e542SGal Pressman 	ci = aq->cq.cc & queue_size_mask;
4470420e542SGal Pressman 	phase = aq->cq.phase;
4480420e542SGal Pressman 
4490420e542SGal Pressman 	cqe = &aq->cq.entries[ci];
4500420e542SGal Pressman 
4510420e542SGal Pressman 	/* Go over all the completions */
4520420e542SGal Pressman 	while ((READ_ONCE(cqe->acq_common_descriptor.flags) &
4530420e542SGal Pressman 		EFA_ADMIN_ACQ_COMMON_DESC_PHASE_MASK) == phase) {
4540420e542SGal Pressman 		/*
4550420e542SGal Pressman 		 * Do not read the rest of the completion entry before the
4560420e542SGal Pressman 		 * phase bit was validated
4570420e542SGal Pressman 		 */
4580420e542SGal Pressman 		dma_rmb();
459*fd867e74SMichael Margolin 		err = efa_com_handle_single_admin_completion(aq, cqe);
460*fd867e74SMichael Margolin 		if (!err)
461*fd867e74SMichael Margolin 			comp_cmds++;
4620420e542SGal Pressman 
463*fd867e74SMichael Margolin 		aq->cq.cc++;
4640420e542SGal Pressman 		ci++;
4650420e542SGal Pressman 		if (ci == aq->depth) {
4660420e542SGal Pressman 			ci = 0;
4670420e542SGal Pressman 			phase = !phase;
4680420e542SGal Pressman 		}
4690420e542SGal Pressman 
4700420e542SGal Pressman 		cqe = &aq->cq.entries[ci];
4710420e542SGal Pressman 	}
4720420e542SGal Pressman 
4730420e542SGal Pressman 	aq->cq.phase = phase;
474*fd867e74SMichael Margolin 	aq->sq.cc += comp_cmds;
475*fd867e74SMichael Margolin 	atomic64_add(comp_cmds, &aq->stats.completed_cmd);
4760420e542SGal Pressman }
4770420e542SGal Pressman 
efa_com_comp_status_to_errno(u8 comp_status)4780420e542SGal Pressman static int efa_com_comp_status_to_errno(u8 comp_status)
4790420e542SGal Pressman {
4800420e542SGal Pressman 	switch (comp_status) {
4810420e542SGal Pressman 	case EFA_ADMIN_SUCCESS:
4820420e542SGal Pressman 		return 0;
4830420e542SGal Pressman 	case EFA_ADMIN_RESOURCE_ALLOCATION_FAILURE:
4840420e542SGal Pressman 		return -ENOMEM;
4850420e542SGal Pressman 	case EFA_ADMIN_UNSUPPORTED_OPCODE:
4860420e542SGal Pressman 		return -EOPNOTSUPP;
4870420e542SGal Pressman 	case EFA_ADMIN_BAD_OPCODE:
4880420e542SGal Pressman 	case EFA_ADMIN_MALFORMED_REQUEST:
4890420e542SGal Pressman 	case EFA_ADMIN_ILLEGAL_PARAMETER:
4900420e542SGal Pressman 	case EFA_ADMIN_UNKNOWN_ERROR:
4910420e542SGal Pressman 		return -EINVAL;
4920420e542SGal Pressman 	default:
4930420e542SGal Pressman 		return -EINVAL;
4940420e542SGal Pressman 	}
4950420e542SGal Pressman }
4960420e542SGal Pressman 
efa_com_wait_and_process_admin_cq_polling(struct efa_comp_ctx * comp_ctx,struct efa_com_admin_queue * aq)4970420e542SGal Pressman static int efa_com_wait_and_process_admin_cq_polling(struct efa_comp_ctx *comp_ctx,
4980420e542SGal Pressman 						     struct efa_com_admin_queue *aq)
4990420e542SGal Pressman {
5000420e542SGal Pressman 	unsigned long timeout;
5010420e542SGal Pressman 	unsigned long flags;
5020420e542SGal Pressman 	int err;
5030420e542SGal Pressman 
5040420e542SGal Pressman 	timeout = jiffies + usecs_to_jiffies(aq->completion_timeout);
5050420e542SGal Pressman 
5060420e542SGal Pressman 	while (1) {
5070420e542SGal Pressman 		spin_lock_irqsave(&aq->cq.lock, flags);
5080420e542SGal Pressman 		efa_com_handle_admin_completion(aq);
5090420e542SGal Pressman 		spin_unlock_irqrestore(&aq->cq.lock, flags);
5100420e542SGal Pressman 
5110420e542SGal Pressman 		if (comp_ctx->status != EFA_CMD_SUBMITTED)
5120420e542SGal Pressman 			break;
5130420e542SGal Pressman 
5140420e542SGal Pressman 		if (time_is_before_jiffies(timeout)) {
515cfa1f5f2SGal Pressman 			ibdev_err_ratelimited(
516cfa1f5f2SGal Pressman 				aq->efa_dev,
5170420e542SGal Pressman 				"Wait for completion (polling) timeout\n");
5180420e542SGal Pressman 			/* EFA didn't have any completion */
5190420e542SGal Pressman 			atomic64_inc(&aq->stats.no_completion);
5200420e542SGal Pressman 
5210420e542SGal Pressman 			clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
5220420e542SGal Pressman 			err = -ETIME;
5230420e542SGal Pressman 			goto out;
5240420e542SGal Pressman 		}
5250420e542SGal Pressman 
5260420e542SGal Pressman 		msleep(aq->poll_interval);
5270420e542SGal Pressman 	}
5280420e542SGal Pressman 
52968fb9f3eSGal Pressman 	err = efa_com_comp_status_to_errno(comp_ctx->user_cqe->acq_common_descriptor.status);
5300420e542SGal Pressman out:
5310420e542SGal Pressman 	efa_com_put_comp_ctx(aq, comp_ctx);
5320420e542SGal Pressman 	return err;
5330420e542SGal Pressman }
5340420e542SGal Pressman 
efa_com_wait_and_process_admin_cq_interrupts(struct efa_comp_ctx * comp_ctx,struct efa_com_admin_queue * aq)5350420e542SGal Pressman static int efa_com_wait_and_process_admin_cq_interrupts(struct efa_comp_ctx *comp_ctx,
5360420e542SGal Pressman 							struct efa_com_admin_queue *aq)
5370420e542SGal Pressman {
5380420e542SGal Pressman 	unsigned long flags;
5390420e542SGal Pressman 	int err;
5400420e542SGal Pressman 
5410420e542SGal Pressman 	wait_for_completion_timeout(&comp_ctx->wait_event,
5420420e542SGal Pressman 				    usecs_to_jiffies(aq->completion_timeout));
5430420e542SGal Pressman 
5440420e542SGal Pressman 	/*
5450420e542SGal Pressman 	 * In case the command wasn't completed find out the root cause.
5460420e542SGal Pressman 	 * There might be 2 kinds of errors
5470420e542SGal Pressman 	 * 1) No completion (timeout reached)
5480420e542SGal Pressman 	 * 2) There is completion but the device didn't get any msi-x interrupt.
5490420e542SGal Pressman 	 */
5500420e542SGal Pressman 	if (comp_ctx->status == EFA_CMD_SUBMITTED) {
5510420e542SGal Pressman 		spin_lock_irqsave(&aq->cq.lock, flags);
5520420e542SGal Pressman 		efa_com_handle_admin_completion(aq);
5530420e542SGal Pressman 		spin_unlock_irqrestore(&aq->cq.lock, flags);
5540420e542SGal Pressman 
5550420e542SGal Pressman 		atomic64_inc(&aq->stats.no_completion);
5560420e542SGal Pressman 
5570420e542SGal Pressman 		if (comp_ctx->status == EFA_CMD_COMPLETED)
558cfa1f5f2SGal Pressman 			ibdev_err_ratelimited(
559cfa1f5f2SGal Pressman 				aq->efa_dev,
5600420e542SGal Pressman 				"The device sent a completion but the driver didn't receive any MSI-X interrupt for admin cmd %s(%d) status %d (ctx: 0x%p, sq producer: %d, sq consumer: %d, cq consumer: %d)\n",
5610420e542SGal Pressman 				efa_com_cmd_str(comp_ctx->cmd_opcode),
5620420e542SGal Pressman 				comp_ctx->cmd_opcode, comp_ctx->status,
5630420e542SGal Pressman 				comp_ctx, aq->sq.pc, aq->sq.cc, aq->cq.cc);
5640420e542SGal Pressman 		else
565cfa1f5f2SGal Pressman 			ibdev_err_ratelimited(
566cfa1f5f2SGal Pressman 				aq->efa_dev,
5670420e542SGal Pressman 				"The device didn't send any completion for admin cmd %s(%d) status %d (ctx 0x%p, sq producer: %d, sq consumer: %d, cq consumer: %d)\n",
5680420e542SGal Pressman 				efa_com_cmd_str(comp_ctx->cmd_opcode),
5690420e542SGal Pressman 				comp_ctx->cmd_opcode, comp_ctx->status,
5700420e542SGal Pressman 				comp_ctx, aq->sq.pc, aq->sq.cc, aq->cq.cc);
5710420e542SGal Pressman 
5720420e542SGal Pressman 		clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
5730420e542SGal Pressman 		err = -ETIME;
5740420e542SGal Pressman 		goto out;
5750420e542SGal Pressman 	}
5760420e542SGal Pressman 
57768fb9f3eSGal Pressman 	err = efa_com_comp_status_to_errno(comp_ctx->user_cqe->acq_common_descriptor.status);
5780420e542SGal Pressman out:
5790420e542SGal Pressman 	efa_com_put_comp_ctx(aq, comp_ctx);
5800420e542SGal Pressman 	return err;
5810420e542SGal Pressman }
5820420e542SGal Pressman 
5830420e542SGal Pressman /*
5840420e542SGal Pressman  * There are two types to wait for completion.
5850420e542SGal Pressman  * Polling mode - wait until the completion is available.
5860420e542SGal Pressman  * Async mode - wait on wait queue until the completion is ready
5870420e542SGal Pressman  * (or the timeout expired).
5880420e542SGal Pressman  * It is expected that the IRQ called efa_com_handle_admin_completion
5890420e542SGal Pressman  * to mark the completions.
5900420e542SGal Pressman  */
efa_com_wait_and_process_admin_cq(struct efa_comp_ctx * comp_ctx,struct efa_com_admin_queue * aq)5910420e542SGal Pressman static int efa_com_wait_and_process_admin_cq(struct efa_comp_ctx *comp_ctx,
5920420e542SGal Pressman 					     struct efa_com_admin_queue *aq)
5930420e542SGal Pressman {
5940420e542SGal Pressman 	if (test_bit(EFA_AQ_STATE_POLLING_BIT, &aq->state))
5950420e542SGal Pressman 		return efa_com_wait_and_process_admin_cq_polling(comp_ctx, aq);
5960420e542SGal Pressman 
5970420e542SGal Pressman 	return efa_com_wait_and_process_admin_cq_interrupts(comp_ctx, aq);
5980420e542SGal Pressman }
5990420e542SGal Pressman 
6000420e542SGal Pressman /**
6010420e542SGal Pressman  * efa_com_cmd_exec - Execute admin command
6020420e542SGal Pressman  * @aq: admin queue.
6030420e542SGal Pressman  * @cmd: the admin command to execute.
6040420e542SGal Pressman  * @cmd_size: the command size.
6050420e542SGal Pressman  * @comp: command completion return entry.
6060420e542SGal Pressman  * @comp_size: command completion size.
6070420e542SGal Pressman  * Submit an admin command and then wait until the device will return a
6080420e542SGal Pressman  * completion.
6090420e542SGal Pressman  * The completion will be copied into comp.
6100420e542SGal Pressman  *
6110420e542SGal Pressman  * @return - 0 on success, negative value on failure.
6120420e542SGal Pressman  */
efa_com_cmd_exec(struct efa_com_admin_queue * aq,struct efa_admin_aq_entry * cmd,size_t cmd_size,struct efa_admin_acq_entry * comp,size_t comp_size)6130420e542SGal Pressman int efa_com_cmd_exec(struct efa_com_admin_queue *aq,
6140420e542SGal Pressman 		     struct efa_admin_aq_entry *cmd,
6150420e542SGal Pressman 		     size_t cmd_size,
6160420e542SGal Pressman 		     struct efa_admin_acq_entry *comp,
6170420e542SGal Pressman 		     size_t comp_size)
6180420e542SGal Pressman {
6190420e542SGal Pressman 	struct efa_comp_ctx *comp_ctx;
6200420e542SGal Pressman 	int err;
6210420e542SGal Pressman 
6220420e542SGal Pressman 	might_sleep();
6230420e542SGal Pressman 
6240420e542SGal Pressman 	/* In case of queue FULL */
6250420e542SGal Pressman 	down(&aq->avail_cmds);
6260420e542SGal Pressman 
6270420e542SGal Pressman 	ibdev_dbg(aq->efa_dev, "%s (opcode %d)\n",
6280420e542SGal Pressman 		  efa_com_cmd_str(cmd->aq_common_descriptor.opcode),
6290420e542SGal Pressman 		  cmd->aq_common_descriptor.opcode);
6300420e542SGal Pressman 	comp_ctx = efa_com_submit_admin_cmd(aq, cmd, cmd_size, comp, comp_size);
6310420e542SGal Pressman 	if (IS_ERR(comp_ctx)) {
632cfa1f5f2SGal Pressman 		ibdev_err_ratelimited(
633cfa1f5f2SGal Pressman 			aq->efa_dev,
6340420e542SGal Pressman 			"Failed to submit command %s (opcode %u) err %ld\n",
6350420e542SGal Pressman 			efa_com_cmd_str(cmd->aq_common_descriptor.opcode),
6360420e542SGal Pressman 			cmd->aq_common_descriptor.opcode, PTR_ERR(comp_ctx));
6370420e542SGal Pressman 
6380420e542SGal Pressman 		up(&aq->avail_cmds);
639f86e3437SGal Pressman 		atomic64_inc(&aq->stats.cmd_err);
6400420e542SGal Pressman 		return PTR_ERR(comp_ctx);
6410420e542SGal Pressman 	}
6420420e542SGal Pressman 
6430420e542SGal Pressman 	err = efa_com_wait_and_process_admin_cq(comp_ctx, aq);
644f86e3437SGal Pressman 	if (err) {
645cfa1f5f2SGal Pressman 		ibdev_err_ratelimited(
646cfa1f5f2SGal Pressman 			aq->efa_dev,
6470420e542SGal Pressman 			"Failed to process command %s (opcode %u) comp_status %d err %d\n",
6480420e542SGal Pressman 			efa_com_cmd_str(cmd->aq_common_descriptor.opcode),
64968fb9f3eSGal Pressman 			cmd->aq_common_descriptor.opcode,
65068fb9f3eSGal Pressman 			comp_ctx->user_cqe->acq_common_descriptor.status, err);
651f86e3437SGal Pressman 		atomic64_inc(&aq->stats.cmd_err);
652f86e3437SGal Pressman 	}
6530420e542SGal Pressman 
6540420e542SGal Pressman 	up(&aq->avail_cmds);
6550420e542SGal Pressman 
6560420e542SGal Pressman 	return err;
6570420e542SGal Pressman }
6580420e542SGal Pressman 
6590420e542SGal Pressman /**
6600420e542SGal Pressman  * efa_com_admin_destroy - Destroy the admin and the async events queues.
6610420e542SGal Pressman  * @edev: EFA communication layer struct
6620420e542SGal Pressman  */
efa_com_admin_destroy(struct efa_com_dev * edev)6630420e542SGal Pressman void efa_com_admin_destroy(struct efa_com_dev *edev)
6640420e542SGal Pressman {
6650420e542SGal Pressman 	struct efa_com_admin_queue *aq = &edev->aq;
6660420e542SGal Pressman 	struct efa_com_aenq *aenq = &edev->aenq;
6670420e542SGal Pressman 	struct efa_com_admin_cq *cq = &aq->cq;
6680420e542SGal Pressman 	struct efa_com_admin_sq *sq = &aq->sq;
6690420e542SGal Pressman 	u16 size;
6700420e542SGal Pressman 
671e0e3f397SGal Pressman 	clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
6720420e542SGal Pressman 
6730420e542SGal Pressman 	devm_kfree(edev->dmadev, aq->comp_ctx_pool);
6740420e542SGal Pressman 	devm_kfree(edev->dmadev, aq->comp_ctx);
6750420e542SGal Pressman 
6760420e542SGal Pressman 	size = aq->depth * sizeof(*sq->entries);
6770420e542SGal Pressman 	dma_free_coherent(edev->dmadev, size, sq->entries, sq->dma_addr);
6780420e542SGal Pressman 
6790420e542SGal Pressman 	size = aq->depth * sizeof(*cq->entries);
6800420e542SGal Pressman 	dma_free_coherent(edev->dmadev, size, cq->entries, cq->dma_addr);
6810420e542SGal Pressman 
6820420e542SGal Pressman 	size = aenq->depth * sizeof(*aenq->entries);
6830420e542SGal Pressman 	dma_free_coherent(edev->dmadev, size, aenq->entries, aenq->dma_addr);
6840420e542SGal Pressman }
6850420e542SGal Pressman 
6860420e542SGal Pressman /**
6870420e542SGal Pressman  * efa_com_set_admin_polling_mode - Set the admin completion queue polling mode
6880420e542SGal Pressman  * @edev: EFA communication layer struct
6890420e542SGal Pressman  * @polling: Enable/Disable polling mode
6900420e542SGal Pressman  *
6910420e542SGal Pressman  * Set the admin completion mode.
6920420e542SGal Pressman  */
efa_com_set_admin_polling_mode(struct efa_com_dev * edev,bool polling)6930420e542SGal Pressman void efa_com_set_admin_polling_mode(struct efa_com_dev *edev, bool polling)
6940420e542SGal Pressman {
6950420e542SGal Pressman 	u32 mask_value = 0;
6960420e542SGal Pressman 
6970420e542SGal Pressman 	if (polling)
69856a7a721SGal Pressman 		EFA_SET(&mask_value, EFA_REGS_INTR_MASK_EN, 1);
6990420e542SGal Pressman 
7000420e542SGal Pressman 	writel(mask_value, edev->reg_bar + EFA_REGS_INTR_MASK_OFF);
7010420e542SGal Pressman 	if (polling)
7020420e542SGal Pressman 		set_bit(EFA_AQ_STATE_POLLING_BIT, &edev->aq.state);
7030420e542SGal Pressman 	else
7040420e542SGal Pressman 		clear_bit(EFA_AQ_STATE_POLLING_BIT, &edev->aq.state);
7050420e542SGal Pressman }
7060420e542SGal Pressman 
efa_com_stats_init(struct efa_com_dev * edev)7070420e542SGal Pressman static void efa_com_stats_init(struct efa_com_dev *edev)
7080420e542SGal Pressman {
7090420e542SGal Pressman 	atomic64_t *s = (atomic64_t *)&edev->aq.stats;
7100420e542SGal Pressman 	int i;
7110420e542SGal Pressman 
7120420e542SGal Pressman 	for (i = 0; i < sizeof(edev->aq.stats) / sizeof(*s); i++, s++)
7130420e542SGal Pressman 		atomic64_set(s, 0);
7140420e542SGal Pressman }
7150420e542SGal Pressman 
7160420e542SGal Pressman /**
7170420e542SGal Pressman  * efa_com_admin_init - Init the admin and the async queues
7180420e542SGal Pressman  * @edev: EFA communication layer struct
7190420e542SGal Pressman  * @aenq_handlers: Those handlers to be called upon event.
7200420e542SGal Pressman  *
7210420e542SGal Pressman  * Initialize the admin submission and completion queues.
7220420e542SGal Pressman  * Initialize the asynchronous events notification queues.
7230420e542SGal Pressman  *
7240420e542SGal Pressman  * @return - 0 on success, negative value on failure.
7250420e542SGal Pressman  */
efa_com_admin_init(struct efa_com_dev * edev,struct efa_aenq_handlers * aenq_handlers)7260420e542SGal Pressman int efa_com_admin_init(struct efa_com_dev *edev,
7270420e542SGal Pressman 		       struct efa_aenq_handlers *aenq_handlers)
7280420e542SGal Pressman {
7290420e542SGal Pressman 	struct efa_com_admin_queue *aq = &edev->aq;
7300420e542SGal Pressman 	u32 timeout;
7310420e542SGal Pressman 	u32 dev_sts;
7320420e542SGal Pressman 	u32 cap;
7330420e542SGal Pressman 	int err;
7340420e542SGal Pressman 
7350420e542SGal Pressman 	dev_sts = efa_com_reg_read32(edev, EFA_REGS_DEV_STS_OFF);
73688d03307SGal Pressman 	if (!EFA_GET(&dev_sts, EFA_REGS_DEV_STS_READY)) {
7370420e542SGal Pressman 		ibdev_err(edev->efa_dev,
7380420e542SGal Pressman 			  "Device isn't ready, abort com init %#x\n", dev_sts);
7390420e542SGal Pressman 		return -ENODEV;
7400420e542SGal Pressman 	}
7410420e542SGal Pressman 
7420420e542SGal Pressman 	aq->depth = EFA_ADMIN_QUEUE_DEPTH;
7430420e542SGal Pressman 
7440420e542SGal Pressman 	aq->dmadev = edev->dmadev;
7450420e542SGal Pressman 	aq->efa_dev = edev->efa_dev;
7460420e542SGal Pressman 	set_bit(EFA_AQ_STATE_POLLING_BIT, &aq->state);
7470420e542SGal Pressman 
7480420e542SGal Pressman 	sema_init(&aq->avail_cmds, aq->depth);
7490420e542SGal Pressman 
7500420e542SGal Pressman 	efa_com_stats_init(edev);
7510420e542SGal Pressman 
7520420e542SGal Pressman 	err = efa_com_init_comp_ctxt(aq);
7530420e542SGal Pressman 	if (err)
7540420e542SGal Pressman 		return err;
7550420e542SGal Pressman 
7560420e542SGal Pressman 	err = efa_com_admin_init_sq(edev);
7570420e542SGal Pressman 	if (err)
7580420e542SGal Pressman 		goto err_destroy_comp_ctxt;
7590420e542SGal Pressman 
7600420e542SGal Pressman 	err = efa_com_admin_init_cq(edev);
7610420e542SGal Pressman 	if (err)
7620420e542SGal Pressman 		goto err_destroy_sq;
7630420e542SGal Pressman 
7640420e542SGal Pressman 	efa_com_set_admin_polling_mode(edev, false);
7650420e542SGal Pressman 
7660420e542SGal Pressman 	err = efa_com_admin_init_aenq(edev, aenq_handlers);
7670420e542SGal Pressman 	if (err)
7680420e542SGal Pressman 		goto err_destroy_cq;
7690420e542SGal Pressman 
7700420e542SGal Pressman 	cap = efa_com_reg_read32(edev, EFA_REGS_CAPS_OFF);
77188d03307SGal Pressman 	timeout = EFA_GET(&cap, EFA_REGS_CAPS_ADMIN_CMD_TO);
7720420e542SGal Pressman 	if (timeout)
7730420e542SGal Pressman 		/* the resolution of timeout reg is 100ms */
7740420e542SGal Pressman 		aq->completion_timeout = timeout * 100000;
7750420e542SGal Pressman 	else
7760420e542SGal Pressman 		aq->completion_timeout = ADMIN_CMD_TIMEOUT_US;
7770420e542SGal Pressman 
7780420e542SGal Pressman 	aq->poll_interval = EFA_POLL_INTERVAL_MS;
7790420e542SGal Pressman 
7800420e542SGal Pressman 	set_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
7810420e542SGal Pressman 
7820420e542SGal Pressman 	return 0;
7830420e542SGal Pressman 
7840420e542SGal Pressman err_destroy_cq:
7850420e542SGal Pressman 	dma_free_coherent(edev->dmadev, aq->depth * sizeof(*aq->cq.entries),
7860420e542SGal Pressman 			  aq->cq.entries, aq->cq.dma_addr);
7870420e542SGal Pressman err_destroy_sq:
7880420e542SGal Pressman 	dma_free_coherent(edev->dmadev, aq->depth * sizeof(*aq->sq.entries),
7890420e542SGal Pressman 			  aq->sq.entries, aq->sq.dma_addr);
7900420e542SGal Pressman err_destroy_comp_ctxt:
7910420e542SGal Pressman 	devm_kfree(edev->dmadev, aq->comp_ctx);
7920420e542SGal Pressman 
7930420e542SGal Pressman 	return err;
7940420e542SGal Pressman }
7950420e542SGal Pressman 
7960420e542SGal Pressman /**
7970420e542SGal Pressman  * efa_com_admin_q_comp_intr_handler - admin queue interrupt handler
7980420e542SGal Pressman  * @edev: EFA communication layer struct
7990420e542SGal Pressman  *
8000420e542SGal Pressman  * This method goes over the admin completion queue and wakes up
8010420e542SGal Pressman  * all the pending threads that wait on the commands wait event.
8020420e542SGal Pressman  *
8039b3ae173SLee Jones  * Note: Should be called after MSI-X interrupt.
8040420e542SGal Pressman  */
efa_com_admin_q_comp_intr_handler(struct efa_com_dev * edev)8050420e542SGal Pressman void efa_com_admin_q_comp_intr_handler(struct efa_com_dev *edev)
8060420e542SGal Pressman {
8070420e542SGal Pressman 	unsigned long flags;
8080420e542SGal Pressman 
8090420e542SGal Pressman 	spin_lock_irqsave(&edev->aq.cq.lock, flags);
8100420e542SGal Pressman 	efa_com_handle_admin_completion(&edev->aq);
8110420e542SGal Pressman 	spin_unlock_irqrestore(&edev->aq.cq.lock, flags);
8120420e542SGal Pressman }
8130420e542SGal Pressman 
8140420e542SGal Pressman /*
8150420e542SGal Pressman  * efa_handle_specific_aenq_event:
8160420e542SGal Pressman  * return the handler that is relevant to the specific event group
8170420e542SGal Pressman  */
efa_com_get_specific_aenq_cb(struct efa_com_dev * edev,u16 group)8180420e542SGal Pressman static efa_aenq_handler efa_com_get_specific_aenq_cb(struct efa_com_dev *edev,
8190420e542SGal Pressman 						     u16 group)
8200420e542SGal Pressman {
8210420e542SGal Pressman 	struct efa_aenq_handlers *aenq_handlers = edev->aenq.aenq_handlers;
8220420e542SGal Pressman 
8230420e542SGal Pressman 	if (group < EFA_MAX_HANDLERS && aenq_handlers->handlers[group])
8240420e542SGal Pressman 		return aenq_handlers->handlers[group];
8250420e542SGal Pressman 
8260420e542SGal Pressman 	return aenq_handlers->unimplemented_handler;
8270420e542SGal Pressman }
8280420e542SGal Pressman 
8290420e542SGal Pressman /**
8300420e542SGal Pressman  * efa_com_aenq_intr_handler - AENQ interrupt handler
8310420e542SGal Pressman  * @edev: EFA communication layer struct
8320420e542SGal Pressman  * @data: Data of interrupt handler.
8330420e542SGal Pressman  *
8340420e542SGal Pressman  * Go over the async event notification queue and call the proper aenq handler.
8350420e542SGal Pressman  */
efa_com_aenq_intr_handler(struct efa_com_dev * edev,void * data)8360420e542SGal Pressman void efa_com_aenq_intr_handler(struct efa_com_dev *edev, void *data)
8370420e542SGal Pressman {
8380420e542SGal Pressman 	struct efa_admin_aenq_common_desc *aenq_common;
8390420e542SGal Pressman 	struct efa_com_aenq *aenq = &edev->aenq;
8400420e542SGal Pressman 	struct efa_admin_aenq_entry *aenq_e;
8410420e542SGal Pressman 	efa_aenq_handler handler_cb;
8420420e542SGal Pressman 	u32 processed = 0;
8430420e542SGal Pressman 	u8 phase;
8440420e542SGal Pressman 	u32 ci;
8450420e542SGal Pressman 
8460420e542SGal Pressman 	ci = aenq->cc & (aenq->depth - 1);
8470420e542SGal Pressman 	phase = aenq->phase;
8480420e542SGal Pressman 	aenq_e = &aenq->entries[ci]; /* Get first entry */
8490420e542SGal Pressman 	aenq_common = &aenq_e->aenq_common_desc;
8500420e542SGal Pressman 
8510420e542SGal Pressman 	/* Go over all the events */
8520420e542SGal Pressman 	while ((READ_ONCE(aenq_common->flags) &
8530420e542SGal Pressman 		EFA_ADMIN_AENQ_COMMON_DESC_PHASE_MASK) == phase) {
8540420e542SGal Pressman 		/*
8550420e542SGal Pressman 		 * Do not read the rest of the completion entry before the
8560420e542SGal Pressman 		 * phase bit was validated
8570420e542SGal Pressman 		 */
8580420e542SGal Pressman 		dma_rmb();
8590420e542SGal Pressman 
8600420e542SGal Pressman 		/* Handle specific event*/
8610420e542SGal Pressman 		handler_cb = efa_com_get_specific_aenq_cb(edev,
8620420e542SGal Pressman 							  aenq_common->group);
8630420e542SGal Pressman 		handler_cb(data, aenq_e); /* call the actual event handler*/
8640420e542SGal Pressman 
8650420e542SGal Pressman 		/* Get next event entry */
8660420e542SGal Pressman 		ci++;
8670420e542SGal Pressman 		processed++;
8680420e542SGal Pressman 
8690420e542SGal Pressman 		if (ci == aenq->depth) {
8700420e542SGal Pressman 			ci = 0;
8710420e542SGal Pressman 			phase = !phase;
8720420e542SGal Pressman 		}
8730420e542SGal Pressman 		aenq_e = &aenq->entries[ci];
8740420e542SGal Pressman 		aenq_common = &aenq_e->aenq_common_desc;
8750420e542SGal Pressman 	}
8760420e542SGal Pressman 
8770420e542SGal Pressman 	aenq->cc += processed;
8780420e542SGal Pressman 	aenq->phase = phase;
8790420e542SGal Pressman 
8800420e542SGal Pressman 	/* Don't update aenq doorbell if there weren't any processed events */
8810420e542SGal Pressman 	if (!processed)
8820420e542SGal Pressman 		return;
8830420e542SGal Pressman 
8840420e542SGal Pressman 	/* barrier not needed in case of writel */
8850420e542SGal Pressman 	writel(aenq->cc, edev->reg_bar + EFA_REGS_AENQ_CONS_DB_OFF);
8860420e542SGal Pressman }
8870420e542SGal Pressman 
efa_com_mmio_reg_read_resp_addr_init(struct efa_com_dev * edev)8880420e542SGal Pressman static void efa_com_mmio_reg_read_resp_addr_init(struct efa_com_dev *edev)
8890420e542SGal Pressman {
8900420e542SGal Pressman 	struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
8910420e542SGal Pressman 	u32 addr_high;
8920420e542SGal Pressman 	u32 addr_low;
8930420e542SGal Pressman 
8940420e542SGal Pressman 	/* dma_addr_bits is unknown at this point */
8950420e542SGal Pressman 	addr_high = (mmio_read->read_resp_dma_addr >> 32) & GENMASK(31, 0);
8960420e542SGal Pressman 	addr_low = mmio_read->read_resp_dma_addr & GENMASK(31, 0);
8970420e542SGal Pressman 
8980420e542SGal Pressman 	writel(addr_high, edev->reg_bar + EFA_REGS_MMIO_RESP_HI_OFF);
8990420e542SGal Pressman 	writel(addr_low, edev->reg_bar + EFA_REGS_MMIO_RESP_LO_OFF);
9000420e542SGal Pressman }
9010420e542SGal Pressman 
efa_com_mmio_reg_read_init(struct efa_com_dev * edev)9020420e542SGal Pressman int efa_com_mmio_reg_read_init(struct efa_com_dev *edev)
9030420e542SGal Pressman {
9040420e542SGal Pressman 	struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
9050420e542SGal Pressman 
9060420e542SGal Pressman 	spin_lock_init(&mmio_read->lock);
9070420e542SGal Pressman 	mmio_read->read_resp =
9080420e542SGal Pressman 		dma_alloc_coherent(edev->dmadev, sizeof(*mmio_read->read_resp),
9090420e542SGal Pressman 				   &mmio_read->read_resp_dma_addr, GFP_KERNEL);
9100420e542SGal Pressman 	if (!mmio_read->read_resp)
9110420e542SGal Pressman 		return -ENOMEM;
9120420e542SGal Pressman 
9130420e542SGal Pressman 	efa_com_mmio_reg_read_resp_addr_init(edev);
9140420e542SGal Pressman 
9150420e542SGal Pressman 	mmio_read->read_resp->req_id = 0;
9160420e542SGal Pressman 	mmio_read->seq_num = 0;
9170420e542SGal Pressman 	mmio_read->mmio_read_timeout = EFA_REG_READ_TIMEOUT_US;
9180420e542SGal Pressman 
9190420e542SGal Pressman 	return 0;
9200420e542SGal Pressman }
9210420e542SGal Pressman 
efa_com_mmio_reg_read_destroy(struct efa_com_dev * edev)9220420e542SGal Pressman void efa_com_mmio_reg_read_destroy(struct efa_com_dev *edev)
9230420e542SGal Pressman {
9240420e542SGal Pressman 	struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
9250420e542SGal Pressman 
9260420e542SGal Pressman 	dma_free_coherent(edev->dmadev, sizeof(*mmio_read->read_resp),
9270420e542SGal Pressman 			  mmio_read->read_resp, mmio_read->read_resp_dma_addr);
9280420e542SGal Pressman }
9290420e542SGal Pressman 
efa_com_validate_version(struct efa_com_dev * edev)9300420e542SGal Pressman int efa_com_validate_version(struct efa_com_dev *edev)
9310420e542SGal Pressman {
93288d03307SGal Pressman 	u32 min_ctrl_ver = 0;
9330420e542SGal Pressman 	u32 ctrl_ver_masked;
93488d03307SGal Pressman 	u32 min_ver = 0;
9350420e542SGal Pressman 	u32 ctrl_ver;
9360420e542SGal Pressman 	u32 ver;
9370420e542SGal Pressman 
9380420e542SGal Pressman 	/*
9390420e542SGal Pressman 	 * Make sure the EFA version and the controller version are at least
9400420e542SGal Pressman 	 * as the driver expects
9410420e542SGal Pressman 	 */
9420420e542SGal Pressman 	ver = efa_com_reg_read32(edev, EFA_REGS_VERSION_OFF);
9430420e542SGal Pressman 	ctrl_ver = efa_com_reg_read32(edev,
9440420e542SGal Pressman 				      EFA_REGS_CONTROLLER_VERSION_OFF);
9450420e542SGal Pressman 
9460420e542SGal Pressman 	ibdev_dbg(edev->efa_dev, "efa device version: %d.%d\n",
94788d03307SGal Pressman 		  EFA_GET(&ver, EFA_REGS_VERSION_MAJOR_VERSION),
94888d03307SGal Pressman 		  EFA_GET(&ver, EFA_REGS_VERSION_MINOR_VERSION));
9490420e542SGal Pressman 
95088d03307SGal Pressman 	EFA_SET(&min_ver, EFA_REGS_VERSION_MAJOR_VERSION,
95188d03307SGal Pressman 		EFA_ADMIN_API_VERSION_MAJOR);
95288d03307SGal Pressman 	EFA_SET(&min_ver, EFA_REGS_VERSION_MINOR_VERSION,
95388d03307SGal Pressman 		EFA_ADMIN_API_VERSION_MINOR);
95488d03307SGal Pressman 	if (ver < min_ver) {
9550420e542SGal Pressman 		ibdev_err(edev->efa_dev,
9560420e542SGal Pressman 			  "EFA version is lower than the minimal version the driver supports\n");
9570420e542SGal Pressman 		return -EOPNOTSUPP;
9580420e542SGal Pressman 	}
9590420e542SGal Pressman 
96088d03307SGal Pressman 	ibdev_dbg(
96188d03307SGal Pressman 		edev->efa_dev,
9620420e542SGal Pressman 		"efa controller version: %d.%d.%d implementation version %d\n",
96388d03307SGal Pressman 		EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION),
96488d03307SGal Pressman 		EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION),
96588d03307SGal Pressman 		EFA_GET(&ctrl_ver,
96688d03307SGal Pressman 			EFA_REGS_CONTROLLER_VERSION_SUBMINOR_VERSION),
96788d03307SGal Pressman 		EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_IMPL_ID));
9680420e542SGal Pressman 
9690420e542SGal Pressman 	ctrl_ver_masked =
97088d03307SGal Pressman 		EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION) |
97188d03307SGal Pressman 		EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION) |
97288d03307SGal Pressman 		EFA_GET(&ctrl_ver,
97388d03307SGal Pressman 			EFA_REGS_CONTROLLER_VERSION_SUBMINOR_VERSION);
9740420e542SGal Pressman 
97588d03307SGal Pressman 	EFA_SET(&min_ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION,
97688d03307SGal Pressman 		EFA_CTRL_MAJOR);
97788d03307SGal Pressman 	EFA_SET(&min_ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION,
97888d03307SGal Pressman 		EFA_CTRL_MINOR);
97988d03307SGal Pressman 	EFA_SET(&min_ctrl_ver, EFA_REGS_CONTROLLER_VERSION_SUBMINOR_VERSION,
98088d03307SGal Pressman 		EFA_CTRL_SUB_MINOR);
9810420e542SGal Pressman 	/* Validate the ctrl version without the implementation ID */
98288d03307SGal Pressman 	if (ctrl_ver_masked < min_ctrl_ver) {
9830420e542SGal Pressman 		ibdev_err(edev->efa_dev,
9840420e542SGal Pressman 			  "EFA ctrl version is lower than the minimal ctrl version the driver supports\n");
9850420e542SGal Pressman 		return -EOPNOTSUPP;
9860420e542SGal Pressman 	}
9870420e542SGal Pressman 
9880420e542SGal Pressman 	return 0;
9890420e542SGal Pressman }
9900420e542SGal Pressman 
9910420e542SGal Pressman /**
9920420e542SGal Pressman  * efa_com_get_dma_width - Retrieve physical dma address width the device
9930420e542SGal Pressman  * supports.
9940420e542SGal Pressman  * @edev: EFA communication layer struct
9950420e542SGal Pressman  *
9960420e542SGal Pressman  * Retrieve the maximum physical address bits the device can handle.
9970420e542SGal Pressman  *
9980420e542SGal Pressman  * @return: > 0 on Success and negative value otherwise.
9990420e542SGal Pressman  */
efa_com_get_dma_width(struct efa_com_dev * edev)10000420e542SGal Pressman int efa_com_get_dma_width(struct efa_com_dev *edev)
10010420e542SGal Pressman {
10020420e542SGal Pressman 	u32 caps = efa_com_reg_read32(edev, EFA_REGS_CAPS_OFF);
10030420e542SGal Pressman 	int width;
10040420e542SGal Pressman 
100588d03307SGal Pressman 	width = EFA_GET(&caps, EFA_REGS_CAPS_DMA_ADDR_WIDTH);
10060420e542SGal Pressman 
10070420e542SGal Pressman 	ibdev_dbg(edev->efa_dev, "DMA width: %d\n", width);
10080420e542SGal Pressman 
10090420e542SGal Pressman 	if (width < 32 || width > 64) {
10100420e542SGal Pressman 		ibdev_err(edev->efa_dev, "DMA width illegal value: %d\n", width);
10110420e542SGal Pressman 		return -EINVAL;
10120420e542SGal Pressman 	}
10130420e542SGal Pressman 
10140420e542SGal Pressman 	edev->dma_addr_bits = width;
10150420e542SGal Pressman 
10160420e542SGal Pressman 	return width;
10170420e542SGal Pressman }
10180420e542SGal Pressman 
wait_for_reset_state(struct efa_com_dev * edev,u32 timeout,int on)101988d03307SGal Pressman static int wait_for_reset_state(struct efa_com_dev *edev, u32 timeout, int on)
10200420e542SGal Pressman {
10210420e542SGal Pressman 	u32 val, i;
10220420e542SGal Pressman 
10230420e542SGal Pressman 	for (i = 0; i < timeout; i++) {
10240420e542SGal Pressman 		val = efa_com_reg_read32(edev, EFA_REGS_DEV_STS_OFF);
10250420e542SGal Pressman 
102688d03307SGal Pressman 		if (EFA_GET(&val, EFA_REGS_DEV_STS_RESET_IN_PROGRESS) == on)
10270420e542SGal Pressman 			return 0;
10280420e542SGal Pressman 
10290420e542SGal Pressman 		ibdev_dbg(edev->efa_dev, "Reset indication val %d\n", val);
10300420e542SGal Pressman 		msleep(EFA_POLL_INTERVAL_MS);
10310420e542SGal Pressman 	}
10320420e542SGal Pressman 
10330420e542SGal Pressman 	return -ETIME;
10340420e542SGal Pressman }
10350420e542SGal Pressman 
10360420e542SGal Pressman /**
10370420e542SGal Pressman  * efa_com_dev_reset - Perform device FLR to the device.
10380420e542SGal Pressman  * @edev: EFA communication layer struct
10390420e542SGal Pressman  * @reset_reason: Specify what is the trigger for the reset in case of an error.
10400420e542SGal Pressman  *
10410420e542SGal Pressman  * @return - 0 on success, negative value on failure.
10420420e542SGal Pressman  */
efa_com_dev_reset(struct efa_com_dev * edev,enum efa_regs_reset_reason_types reset_reason)10430420e542SGal Pressman int efa_com_dev_reset(struct efa_com_dev *edev,
10440420e542SGal Pressman 		      enum efa_regs_reset_reason_types reset_reason)
10450420e542SGal Pressman {
104688d03307SGal Pressman 	u32 stat, timeout, cap;
104788d03307SGal Pressman 	u32 reset_val = 0;
10480420e542SGal Pressman 	int err;
10490420e542SGal Pressman 
10500420e542SGal Pressman 	stat = efa_com_reg_read32(edev, EFA_REGS_DEV_STS_OFF);
10510420e542SGal Pressman 	cap = efa_com_reg_read32(edev, EFA_REGS_CAPS_OFF);
10520420e542SGal Pressman 
105388d03307SGal Pressman 	if (!EFA_GET(&stat, EFA_REGS_DEV_STS_READY)) {
10540420e542SGal Pressman 		ibdev_err(edev->efa_dev,
10550420e542SGal Pressman 			  "Device isn't ready, can't reset device\n");
10560420e542SGal Pressman 		return -EINVAL;
10570420e542SGal Pressman 	}
10580420e542SGal Pressman 
105988d03307SGal Pressman 	timeout = EFA_GET(&cap, EFA_REGS_CAPS_RESET_TIMEOUT);
10600420e542SGal Pressman 	if (!timeout) {
10610420e542SGal Pressman 		ibdev_err(edev->efa_dev, "Invalid timeout value\n");
10620420e542SGal Pressman 		return -EINVAL;
10630420e542SGal Pressman 	}
10640420e542SGal Pressman 
10650420e542SGal Pressman 	/* start reset */
106688d03307SGal Pressman 	EFA_SET(&reset_val, EFA_REGS_DEV_CTL_DEV_RESET, 1);
106788d03307SGal Pressman 	EFA_SET(&reset_val, EFA_REGS_DEV_CTL_RESET_REASON, reset_reason);
10680420e542SGal Pressman 	writel(reset_val, edev->reg_bar + EFA_REGS_DEV_CTL_OFF);
10690420e542SGal Pressman 
10700420e542SGal Pressman 	/* reset clears the mmio readless address, restore it */
10710420e542SGal Pressman 	efa_com_mmio_reg_read_resp_addr_init(edev);
10720420e542SGal Pressman 
107388d03307SGal Pressman 	err = wait_for_reset_state(edev, timeout, 1);
10740420e542SGal Pressman 	if (err) {
10750420e542SGal Pressman 		ibdev_err(edev->efa_dev, "Reset indication didn't turn on\n");
10760420e542SGal Pressman 		return err;
10770420e542SGal Pressman 	}
10780420e542SGal Pressman 
10790420e542SGal Pressman 	/* reset done */
10800420e542SGal Pressman 	writel(0, edev->reg_bar + EFA_REGS_DEV_CTL_OFF);
10810420e542SGal Pressman 	err = wait_for_reset_state(edev, timeout, 0);
10820420e542SGal Pressman 	if (err) {
10830420e542SGal Pressman 		ibdev_err(edev->efa_dev, "Reset indication didn't turn off\n");
10840420e542SGal Pressman 		return err;
10850420e542SGal Pressman 	}
10860420e542SGal Pressman 
108788d03307SGal Pressman 	timeout = EFA_GET(&cap, EFA_REGS_CAPS_ADMIN_CMD_TO);
10880420e542SGal Pressman 	if (timeout)
10890420e542SGal Pressman 		/* the resolution of timeout reg is 100ms */
10900420e542SGal Pressman 		edev->aq.completion_timeout = timeout * 100000;
10910420e542SGal Pressman 	else
10920420e542SGal Pressman 		edev->aq.completion_timeout = ADMIN_CMD_TIMEOUT_US;
10930420e542SGal Pressman 
10940420e542SGal Pressman 	return 0;
10950420e542SGal Pressman }
10962a152512SGal Pressman 
efa_com_create_eq(struct efa_com_dev * edev,struct efa_com_create_eq_params * params,struct efa_com_create_eq_result * result)10972a152512SGal Pressman static int efa_com_create_eq(struct efa_com_dev *edev,
10982a152512SGal Pressman 			     struct efa_com_create_eq_params *params,
10992a152512SGal Pressman 			     struct efa_com_create_eq_result *result)
11002a152512SGal Pressman {
11012a152512SGal Pressman 	struct efa_com_admin_queue *aq = &edev->aq;
11022a152512SGal Pressman 	struct efa_admin_create_eq_resp resp = {};
11032a152512SGal Pressman 	struct efa_admin_create_eq_cmd cmd = {};
11042a152512SGal Pressman 	int err;
11052a152512SGal Pressman 
11062a152512SGal Pressman 	cmd.aq_common_descriptor.opcode = EFA_ADMIN_CREATE_EQ;
11072a152512SGal Pressman 	EFA_SET(&cmd.caps, EFA_ADMIN_CREATE_EQ_CMD_ENTRY_SIZE_WORDS,
11082a152512SGal Pressman 		params->entry_size_in_bytes / 4);
11092a152512SGal Pressman 	cmd.depth = params->depth;
11102a152512SGal Pressman 	cmd.event_bitmask = params->event_bitmask;
11112a152512SGal Pressman 	cmd.msix_vec = params->msix_vec;
11122a152512SGal Pressman 
11132a152512SGal Pressman 	efa_com_set_dma_addr(params->dma_addr, &cmd.ba.mem_addr_high,
11142a152512SGal Pressman 			     &cmd.ba.mem_addr_low);
11152a152512SGal Pressman 
11162a152512SGal Pressman 	err = efa_com_cmd_exec(aq,
11172a152512SGal Pressman 			       (struct efa_admin_aq_entry *)&cmd,
11182a152512SGal Pressman 			       sizeof(cmd),
11192a152512SGal Pressman 			       (struct efa_admin_acq_entry *)&resp,
11202a152512SGal Pressman 			       sizeof(resp));
11212a152512SGal Pressman 	if (err) {
11222a152512SGal Pressman 		ibdev_err_ratelimited(edev->efa_dev,
11232a152512SGal Pressman 				      "Failed to create eq[%d]\n", err);
11242a152512SGal Pressman 		return err;
11252a152512SGal Pressman 	}
11262a152512SGal Pressman 
11272a152512SGal Pressman 	result->eqn = resp.eqn;
11282a152512SGal Pressman 
11292a152512SGal Pressman 	return 0;
11302a152512SGal Pressman }
11312a152512SGal Pressman 
efa_com_destroy_eq(struct efa_com_dev * edev,struct efa_com_destroy_eq_params * params)11322a152512SGal Pressman static void efa_com_destroy_eq(struct efa_com_dev *edev,
11332a152512SGal Pressman 			       struct efa_com_destroy_eq_params *params)
11342a152512SGal Pressman {
11352a152512SGal Pressman 	struct efa_com_admin_queue *aq = &edev->aq;
11362a152512SGal Pressman 	struct efa_admin_destroy_eq_resp resp = {};
11372a152512SGal Pressman 	struct efa_admin_destroy_eq_cmd cmd = {};
11382a152512SGal Pressman 	int err;
11392a152512SGal Pressman 
11402a152512SGal Pressman 	cmd.aq_common_descriptor.opcode = EFA_ADMIN_DESTROY_EQ;
11412a152512SGal Pressman 	cmd.eqn = params->eqn;
11422a152512SGal Pressman 
11432a152512SGal Pressman 	err = efa_com_cmd_exec(aq,
11442a152512SGal Pressman 			       (struct efa_admin_aq_entry *)&cmd,
11452a152512SGal Pressman 			       sizeof(cmd),
11462a152512SGal Pressman 			       (struct efa_admin_acq_entry *)&resp,
11472a152512SGal Pressman 			       sizeof(resp));
11482a152512SGal Pressman 	if (err)
11492a152512SGal Pressman 		ibdev_err_ratelimited(edev->efa_dev,
11502a152512SGal Pressman 				      "Failed to destroy EQ-%u [%d]\n", cmd.eqn,
11512a152512SGal Pressman 				      err);
11522a152512SGal Pressman }
11532a152512SGal Pressman 
efa_com_arm_eq(struct efa_com_dev * edev,struct efa_com_eq * eeq)11542a152512SGal Pressman static void efa_com_arm_eq(struct efa_com_dev *edev, struct efa_com_eq *eeq)
11552a152512SGal Pressman {
11562a152512SGal Pressman 	u32 val = 0;
11572a152512SGal Pressman 
11582a152512SGal Pressman 	EFA_SET(&val, EFA_REGS_EQ_DB_EQN, eeq->eqn);
11592a152512SGal Pressman 	EFA_SET(&val, EFA_REGS_EQ_DB_ARM, 1);
11602a152512SGal Pressman 
11612a152512SGal Pressman 	writel(val, edev->reg_bar + EFA_REGS_EQ_DB_OFF);
11622a152512SGal Pressman }
11632a152512SGal Pressman 
efa_com_eq_comp_intr_handler(struct efa_com_dev * edev,struct efa_com_eq * eeq)11642a152512SGal Pressman void efa_com_eq_comp_intr_handler(struct efa_com_dev *edev,
11652a152512SGal Pressman 				  struct efa_com_eq *eeq)
11662a152512SGal Pressman {
11672a152512SGal Pressman 	struct efa_admin_eqe *eqe;
11682a152512SGal Pressman 	u32 processed = 0;
11692a152512SGal Pressman 	u8 phase;
11702a152512SGal Pressman 	u32 ci;
11712a152512SGal Pressman 
11722a152512SGal Pressman 	ci = eeq->cc & (eeq->depth - 1);
11732a152512SGal Pressman 	phase = eeq->phase;
11742a152512SGal Pressman 	eqe = &eeq->eqes[ci];
11752a152512SGal Pressman 
11762a152512SGal Pressman 	/* Go over all the events */
11772a152512SGal Pressman 	while ((READ_ONCE(eqe->common) & EFA_ADMIN_EQE_PHASE_MASK) == phase) {
11782a152512SGal Pressman 		/*
11792a152512SGal Pressman 		 * Do not read the rest of the completion entry before the
11802a152512SGal Pressman 		 * phase bit was validated
11812a152512SGal Pressman 		 */
11822a152512SGal Pressman 		dma_rmb();
11832a152512SGal Pressman 
11842a152512SGal Pressman 		eeq->cb(eeq, eqe);
11852a152512SGal Pressman 
11862a152512SGal Pressman 		/* Get next event entry */
11872a152512SGal Pressman 		ci++;
11882a152512SGal Pressman 		processed++;
11892a152512SGal Pressman 
11902a152512SGal Pressman 		if (ci == eeq->depth) {
11912a152512SGal Pressman 			ci = 0;
11922a152512SGal Pressman 			phase = !phase;
11932a152512SGal Pressman 		}
11942a152512SGal Pressman 
11952a152512SGal Pressman 		eqe = &eeq->eqes[ci];
11962a152512SGal Pressman 	}
11972a152512SGal Pressman 
11982a152512SGal Pressman 	eeq->cc += processed;
11992a152512SGal Pressman 	eeq->phase = phase;
12002a152512SGal Pressman 	efa_com_arm_eq(eeq->edev, eeq);
12012a152512SGal Pressman }
12022a152512SGal Pressman 
efa_com_eq_destroy(struct efa_com_dev * edev,struct efa_com_eq * eeq)12032a152512SGal Pressman void efa_com_eq_destroy(struct efa_com_dev *edev, struct efa_com_eq *eeq)
12042a152512SGal Pressman {
12052a152512SGal Pressman 	struct efa_com_destroy_eq_params params = {
12062a152512SGal Pressman 		.eqn = eeq->eqn,
12072a152512SGal Pressman 	};
12082a152512SGal Pressman 
12092a152512SGal Pressman 	efa_com_destroy_eq(edev, &params);
12102a152512SGal Pressman 	dma_free_coherent(edev->dmadev, eeq->depth * sizeof(*eeq->eqes),
12112a152512SGal Pressman 			  eeq->eqes, eeq->dma_addr);
12122a152512SGal Pressman }
12132a152512SGal Pressman 
efa_com_eq_init(struct efa_com_dev * edev,struct efa_com_eq * eeq,efa_eqe_handler cb,u16 depth,u8 msix_vec)12142a152512SGal Pressman int efa_com_eq_init(struct efa_com_dev *edev, struct efa_com_eq *eeq,
12152a152512SGal Pressman 		    efa_eqe_handler cb, u16 depth, u8 msix_vec)
12162a152512SGal Pressman {
12172a152512SGal Pressman 	struct efa_com_create_eq_params params = {};
12182a152512SGal Pressman 	struct efa_com_create_eq_result result = {};
12192a152512SGal Pressman 	int err;
12202a152512SGal Pressman 
12212a152512SGal Pressman 	params.depth = depth;
12222a152512SGal Pressman 	params.entry_size_in_bytes = sizeof(*eeq->eqes);
12232a152512SGal Pressman 	EFA_SET(&params.event_bitmask,
12242a152512SGal Pressman 		EFA_ADMIN_CREATE_EQ_CMD_COMPLETION_EVENTS, 1);
12252a152512SGal Pressman 	params.msix_vec = msix_vec;
12262a152512SGal Pressman 
12272a152512SGal Pressman 	eeq->eqes = dma_alloc_coherent(edev->dmadev,
12282a152512SGal Pressman 				       params.depth * sizeof(*eeq->eqes),
12292a152512SGal Pressman 				       &params.dma_addr, GFP_KERNEL);
12302a152512SGal Pressman 	if (!eeq->eqes)
12312a152512SGal Pressman 		return -ENOMEM;
12322a152512SGal Pressman 
12332a152512SGal Pressman 	err = efa_com_create_eq(edev, &params, &result);
12342a152512SGal Pressman 	if (err)
12352a152512SGal Pressman 		goto err_free_coherent;
12362a152512SGal Pressman 
12372a152512SGal Pressman 	eeq->eqn = result.eqn;
12382a152512SGal Pressman 	eeq->edev = edev;
12392a152512SGal Pressman 	eeq->dma_addr = params.dma_addr;
12402a152512SGal Pressman 	eeq->phase = 1;
12412a152512SGal Pressman 	eeq->depth = params.depth;
12422a152512SGal Pressman 	eeq->cb = cb;
12432a152512SGal Pressman 	efa_com_arm_eq(edev, eeq);
12442a152512SGal Pressman 
12452a152512SGal Pressman 	return 0;
12462a152512SGal Pressman 
12472a152512SGal Pressman err_free_coherent:
12482a152512SGal Pressman 	dma_free_coherent(edev->dmadev, params.depth * sizeof(*eeq->eqes),
12492a152512SGal Pressman 			  eeq->eqes, params.dma_addr);
12502a152512SGal Pressman 	return err;
12512a152512SGal Pressman }
1252