1 // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
3 * Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All rights reserved.
7 #include "efa_regs_defs.h"
9 #define ADMIN_CMD_TIMEOUT_US 30000000 /* usecs */
11 #define EFA_REG_READ_TIMEOUT_US 50000 /* usecs */
12 #define EFA_MMIO_READ_INVALID 0xffffffff
14 #define EFA_POLL_INTERVAL_MS 100 /* msecs */
16 #define EFA_ASYNC_QUEUE_DEPTH 16
17 #define EFA_ADMIN_QUEUE_DEPTH 32
20 ((EFA_ADMIN_API_VERSION_MAJOR << EFA_REGS_VERSION_MAJOR_VERSION_SHIFT) | \
21 (EFA_ADMIN_API_VERSION_MINOR & EFA_REGS_VERSION_MINOR_VERSION_MASK))
23 #define EFA_CTRL_MAJOR 0
24 #define EFA_CTRL_MINOR 0
25 #define EFA_CTRL_SUB_MINOR 1
27 #define MIN_EFA_CTRL_VER \
28 (((EFA_CTRL_MAJOR) << \
29 (EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION_SHIFT)) | \
30 ((EFA_CTRL_MINOR) << \
31 (EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION_SHIFT)) | \
34 #define EFA_DMA_ADDR_TO_UINT32_LOW(x) ((u32)((u64)(x)))
35 #define EFA_DMA_ADDR_TO_UINT32_HIGH(x) ((u32)(((u64)(x)) >> 32))
37 #define EFA_REGS_ADMIN_INTR_MASK 1
45 struct completion wait_event;
46 struct efa_admin_acq_entry *user_cqe;
48 enum efa_cmd_status status;
49 /* status from the device */
55 static const char *efa_com_cmd_str(u8 cmd)
57 #define EFA_CMD_STR_CASE(_cmd) case EFA_ADMIN_##_cmd: return #_cmd
60 EFA_CMD_STR_CASE(CREATE_QP);
61 EFA_CMD_STR_CASE(MODIFY_QP);
62 EFA_CMD_STR_CASE(QUERY_QP);
63 EFA_CMD_STR_CASE(DESTROY_QP);
64 EFA_CMD_STR_CASE(CREATE_AH);
65 EFA_CMD_STR_CASE(DESTROY_AH);
66 EFA_CMD_STR_CASE(REG_MR);
67 EFA_CMD_STR_CASE(DEREG_MR);
68 EFA_CMD_STR_CASE(CREATE_CQ);
69 EFA_CMD_STR_CASE(DESTROY_CQ);
70 EFA_CMD_STR_CASE(GET_FEATURE);
71 EFA_CMD_STR_CASE(SET_FEATURE);
72 EFA_CMD_STR_CASE(GET_STATS);
73 EFA_CMD_STR_CASE(ALLOC_PD);
74 EFA_CMD_STR_CASE(DEALLOC_PD);
75 EFA_CMD_STR_CASE(ALLOC_UAR);
76 EFA_CMD_STR_CASE(DEALLOC_UAR);
77 default: return "unknown command opcode";
79 #undef EFA_CMD_STR_CASE
82 static u32 efa_com_reg_read32(struct efa_com_dev *edev, u16 offset)
84 struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
85 struct efa_admin_mmio_req_read_less_resp *read_resp;
86 unsigned long exp_time;
90 read_resp = mmio_read->read_resp;
92 spin_lock(&mmio_read->lock);
95 /* trash DMA req_id to identify when hardware is done */
96 read_resp->req_id = mmio_read->seq_num + 0x9aL;
97 mmio_read_reg = (offset << EFA_REGS_MMIO_REG_READ_REG_OFF_SHIFT) &
98 EFA_REGS_MMIO_REG_READ_REG_OFF_MASK;
99 mmio_read_reg |= mmio_read->seq_num &
100 EFA_REGS_MMIO_REG_READ_REQ_ID_MASK;
102 writel(mmio_read_reg, edev->reg_bar + EFA_REGS_MMIO_REG_READ_OFF);
104 exp_time = jiffies + usecs_to_jiffies(mmio_read->mmio_read_timeout);
106 if (READ_ONCE(read_resp->req_id) == mmio_read->seq_num)
109 } while (time_is_after_jiffies(exp_time));
111 if (read_resp->req_id != mmio_read->seq_num) {
112 ibdev_err_ratelimited(
114 "Reading register timed out. expected: req id[%u] offset[%#x] actual: req id[%u] offset[%#x]\n",
115 mmio_read->seq_num, offset, read_resp->req_id,
117 err = EFA_MMIO_READ_INVALID;
121 if (read_resp->reg_off != offset) {
122 ibdev_err_ratelimited(
124 "Reading register failed: wrong offset provided\n");
125 err = EFA_MMIO_READ_INVALID;
129 err = read_resp->reg_val;
131 spin_unlock(&mmio_read->lock);
135 static int efa_com_admin_init_sq(struct efa_com_dev *edev)
137 struct efa_com_admin_queue *aq = &edev->aq;
138 struct efa_com_admin_sq *sq = &aq->sq;
139 u16 size = aq->depth * sizeof(*sq->entries);
145 dma_alloc_coherent(aq->dmadev, size, &sq->dma_addr, GFP_KERNEL);
149 spin_lock_init(&sq->lock);
155 sq->db_addr = (u32 __iomem *)(edev->reg_bar + EFA_REGS_AQ_PROD_DB_OFF);
157 addr_high = EFA_DMA_ADDR_TO_UINT32_HIGH(sq->dma_addr);
158 addr_low = EFA_DMA_ADDR_TO_UINT32_LOW(sq->dma_addr);
160 writel(addr_low, edev->reg_bar + EFA_REGS_AQ_BASE_LO_OFF);
161 writel(addr_high, edev->reg_bar + EFA_REGS_AQ_BASE_HI_OFF);
163 aq_caps = aq->depth & EFA_REGS_AQ_CAPS_AQ_DEPTH_MASK;
164 aq_caps |= (sizeof(struct efa_admin_aq_entry) <<
165 EFA_REGS_AQ_CAPS_AQ_ENTRY_SIZE_SHIFT) &
166 EFA_REGS_AQ_CAPS_AQ_ENTRY_SIZE_MASK;
168 writel(aq_caps, edev->reg_bar + EFA_REGS_AQ_CAPS_OFF);
173 static int efa_com_admin_init_cq(struct efa_com_dev *edev)
175 struct efa_com_admin_queue *aq = &edev->aq;
176 struct efa_com_admin_cq *cq = &aq->cq;
177 u16 size = aq->depth * sizeof(*cq->entries);
183 dma_alloc_coherent(aq->dmadev, size, &cq->dma_addr, GFP_KERNEL);
187 spin_lock_init(&cq->lock);
192 addr_high = EFA_DMA_ADDR_TO_UINT32_HIGH(cq->dma_addr);
193 addr_low = EFA_DMA_ADDR_TO_UINT32_LOW(cq->dma_addr);
195 writel(addr_low, edev->reg_bar + EFA_REGS_ACQ_BASE_LO_OFF);
196 writel(addr_high, edev->reg_bar + EFA_REGS_ACQ_BASE_HI_OFF);
198 acq_caps = aq->depth & EFA_REGS_ACQ_CAPS_ACQ_DEPTH_MASK;
199 acq_caps |= (sizeof(struct efa_admin_acq_entry) <<
200 EFA_REGS_ACQ_CAPS_ACQ_ENTRY_SIZE_SHIFT) &
201 EFA_REGS_ACQ_CAPS_ACQ_ENTRY_SIZE_MASK;
202 acq_caps |= (aq->msix_vector_idx <<
203 EFA_REGS_ACQ_CAPS_ACQ_MSIX_VECTOR_SHIFT) &
204 EFA_REGS_ACQ_CAPS_ACQ_MSIX_VECTOR_MASK;
206 writel(acq_caps, edev->reg_bar + EFA_REGS_ACQ_CAPS_OFF);
211 static int efa_com_admin_init_aenq(struct efa_com_dev *edev,
212 struct efa_aenq_handlers *aenq_handlers)
214 struct efa_com_aenq *aenq = &edev->aenq;
215 u32 addr_low, addr_high, aenq_caps;
218 if (!aenq_handlers) {
219 ibdev_err(edev->efa_dev, "aenq handlers pointer is NULL\n");
223 size = EFA_ASYNC_QUEUE_DEPTH * sizeof(*aenq->entries);
224 aenq->entries = dma_alloc_coherent(edev->dmadev, size, &aenq->dma_addr,
229 aenq->aenq_handlers = aenq_handlers;
230 aenq->depth = EFA_ASYNC_QUEUE_DEPTH;
234 addr_low = EFA_DMA_ADDR_TO_UINT32_LOW(aenq->dma_addr);
235 addr_high = EFA_DMA_ADDR_TO_UINT32_HIGH(aenq->dma_addr);
237 writel(addr_low, edev->reg_bar + EFA_REGS_AENQ_BASE_LO_OFF);
238 writel(addr_high, edev->reg_bar + EFA_REGS_AENQ_BASE_HI_OFF);
240 aenq_caps = aenq->depth & EFA_REGS_AENQ_CAPS_AENQ_DEPTH_MASK;
241 aenq_caps |= (sizeof(struct efa_admin_aenq_entry) <<
242 EFA_REGS_AENQ_CAPS_AENQ_ENTRY_SIZE_SHIFT) &
243 EFA_REGS_AENQ_CAPS_AENQ_ENTRY_SIZE_MASK;
244 aenq_caps |= (aenq->msix_vector_idx
245 << EFA_REGS_AENQ_CAPS_AENQ_MSIX_VECTOR_SHIFT) &
246 EFA_REGS_AENQ_CAPS_AENQ_MSIX_VECTOR_MASK;
247 writel(aenq_caps, edev->reg_bar + EFA_REGS_AENQ_CAPS_OFF);
250 * Init cons_db to mark that all entries in the queue
251 * are initially available
253 writel(edev->aenq.cc, edev->reg_bar + EFA_REGS_AENQ_CONS_DB_OFF);
258 /* ID to be used with efa_com_get_comp_ctx */
259 static u16 efa_com_alloc_ctx_id(struct efa_com_admin_queue *aq)
263 spin_lock(&aq->comp_ctx_lock);
264 ctx_id = aq->comp_ctx_pool[aq->comp_ctx_pool_next];
265 aq->comp_ctx_pool_next++;
266 spin_unlock(&aq->comp_ctx_lock);
271 static void efa_com_dealloc_ctx_id(struct efa_com_admin_queue *aq,
274 spin_lock(&aq->comp_ctx_lock);
275 aq->comp_ctx_pool_next--;
276 aq->comp_ctx_pool[aq->comp_ctx_pool_next] = ctx_id;
277 spin_unlock(&aq->comp_ctx_lock);
280 static inline void efa_com_put_comp_ctx(struct efa_com_admin_queue *aq,
281 struct efa_comp_ctx *comp_ctx)
283 u16 cmd_id = comp_ctx->user_cqe->acq_common_descriptor.command &
284 EFA_ADMIN_ACQ_COMMON_DESC_COMMAND_ID_MASK;
285 u16 ctx_id = cmd_id & (aq->depth - 1);
287 ibdev_dbg(aq->efa_dev, "Put completion command_id %#x\n", cmd_id);
288 comp_ctx->occupied = 0;
289 efa_com_dealloc_ctx_id(aq, ctx_id);
292 static struct efa_comp_ctx *efa_com_get_comp_ctx(struct efa_com_admin_queue *aq,
293 u16 cmd_id, bool capture)
295 u16 ctx_id = cmd_id & (aq->depth - 1);
297 if (aq->comp_ctx[ctx_id].occupied && capture) {
298 ibdev_err_ratelimited(
300 "Completion context for command_id %#x is occupied\n",
306 aq->comp_ctx[ctx_id].occupied = 1;
307 ibdev_dbg(aq->efa_dev,
308 "Take completion ctxt for command_id %#x\n", cmd_id);
311 return &aq->comp_ctx[ctx_id];
314 static struct efa_comp_ctx *__efa_com_submit_admin_cmd(struct efa_com_admin_queue *aq,
315 struct efa_admin_aq_entry *cmd,
316 size_t cmd_size_in_bytes,
317 struct efa_admin_acq_entry *comp,
318 size_t comp_size_in_bytes)
320 struct efa_comp_ctx *comp_ctx;
326 queue_size_mask = aq->depth - 1;
327 pi = aq->sq.pc & queue_size_mask;
329 ctx_id = efa_com_alloc_ctx_id(aq);
331 /* cmd_id LSBs are the ctx_id and MSBs are entropy bits from pc */
332 cmd_id = ctx_id & queue_size_mask;
333 cmd_id |= aq->sq.pc & ~queue_size_mask;
334 cmd_id &= EFA_ADMIN_AQ_COMMON_DESC_COMMAND_ID_MASK;
336 cmd->aq_common_descriptor.command_id = cmd_id;
337 cmd->aq_common_descriptor.flags |= aq->sq.phase &
338 EFA_ADMIN_AQ_COMMON_DESC_PHASE_MASK;
340 comp_ctx = efa_com_get_comp_ctx(aq, cmd_id, true);
342 efa_com_dealloc_ctx_id(aq, ctx_id);
343 return ERR_PTR(-EINVAL);
346 comp_ctx->status = EFA_CMD_SUBMITTED;
347 comp_ctx->comp_size = comp_size_in_bytes;
348 comp_ctx->user_cqe = comp;
349 comp_ctx->cmd_opcode = cmd->aq_common_descriptor.opcode;
351 reinit_completion(&comp_ctx->wait_event);
353 memcpy(&aq->sq.entries[pi], cmd, cmd_size_in_bytes);
356 atomic64_inc(&aq->stats.submitted_cmd);
358 if ((aq->sq.pc & queue_size_mask) == 0)
359 aq->sq.phase = !aq->sq.phase;
361 /* barrier not needed in case of writel */
362 writel(aq->sq.pc, aq->sq.db_addr);
367 static inline int efa_com_init_comp_ctxt(struct efa_com_admin_queue *aq)
369 size_t pool_size = aq->depth * sizeof(*aq->comp_ctx_pool);
370 size_t size = aq->depth * sizeof(struct efa_comp_ctx);
371 struct efa_comp_ctx *comp_ctx;
374 aq->comp_ctx = devm_kzalloc(aq->dmadev, size, GFP_KERNEL);
375 aq->comp_ctx_pool = devm_kzalloc(aq->dmadev, pool_size, GFP_KERNEL);
376 if (!aq->comp_ctx || !aq->comp_ctx_pool) {
377 devm_kfree(aq->dmadev, aq->comp_ctx_pool);
378 devm_kfree(aq->dmadev, aq->comp_ctx);
382 for (i = 0; i < aq->depth; i++) {
383 comp_ctx = efa_com_get_comp_ctx(aq, i, false);
385 init_completion(&comp_ctx->wait_event);
387 aq->comp_ctx_pool[i] = i;
390 spin_lock_init(&aq->comp_ctx_lock);
392 aq->comp_ctx_pool_next = 0;
397 static struct efa_comp_ctx *efa_com_submit_admin_cmd(struct efa_com_admin_queue *aq,
398 struct efa_admin_aq_entry *cmd,
399 size_t cmd_size_in_bytes,
400 struct efa_admin_acq_entry *comp,
401 size_t comp_size_in_bytes)
403 struct efa_comp_ctx *comp_ctx;
405 spin_lock(&aq->sq.lock);
406 if (!test_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state)) {
407 ibdev_err_ratelimited(aq->efa_dev, "Admin queue is closed\n");
408 spin_unlock(&aq->sq.lock);
409 return ERR_PTR(-ENODEV);
412 comp_ctx = __efa_com_submit_admin_cmd(aq, cmd, cmd_size_in_bytes, comp,
414 spin_unlock(&aq->sq.lock);
415 if (IS_ERR(comp_ctx))
416 clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
421 static void efa_com_handle_single_admin_completion(struct efa_com_admin_queue *aq,
422 struct efa_admin_acq_entry *cqe)
424 struct efa_comp_ctx *comp_ctx;
427 cmd_id = cqe->acq_common_descriptor.command &
428 EFA_ADMIN_ACQ_COMMON_DESC_COMMAND_ID_MASK;
430 comp_ctx = efa_com_get_comp_ctx(aq, cmd_id, false);
432 ibdev_err(aq->efa_dev,
433 "comp_ctx is NULL. Changing the admin queue running state\n");
434 clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
438 comp_ctx->status = EFA_CMD_COMPLETED;
439 comp_ctx->comp_status = cqe->acq_common_descriptor.status;
440 if (comp_ctx->user_cqe)
441 memcpy(comp_ctx->user_cqe, cqe, comp_ctx->comp_size);
443 if (!test_bit(EFA_AQ_STATE_POLLING_BIT, &aq->state))
444 complete(&comp_ctx->wait_event);
447 static void efa_com_handle_admin_completion(struct efa_com_admin_queue *aq)
449 struct efa_admin_acq_entry *cqe;
455 queue_size_mask = aq->depth - 1;
457 ci = aq->cq.cc & queue_size_mask;
458 phase = aq->cq.phase;
460 cqe = &aq->cq.entries[ci];
462 /* Go over all the completions */
463 while ((READ_ONCE(cqe->acq_common_descriptor.flags) &
464 EFA_ADMIN_ACQ_COMMON_DESC_PHASE_MASK) == phase) {
466 * Do not read the rest of the completion entry before the
467 * phase bit was validated
470 efa_com_handle_single_admin_completion(aq, cqe);
474 if (ci == aq->depth) {
479 cqe = &aq->cq.entries[ci];
482 aq->cq.cc += comp_num;
483 aq->cq.phase = phase;
484 aq->sq.cc += comp_num;
485 atomic64_add(comp_num, &aq->stats.completed_cmd);
488 static int efa_com_comp_status_to_errno(u8 comp_status)
490 switch (comp_status) {
491 case EFA_ADMIN_SUCCESS:
493 case EFA_ADMIN_RESOURCE_ALLOCATION_FAILURE:
495 case EFA_ADMIN_UNSUPPORTED_OPCODE:
497 case EFA_ADMIN_BAD_OPCODE:
498 case EFA_ADMIN_MALFORMED_REQUEST:
499 case EFA_ADMIN_ILLEGAL_PARAMETER:
500 case EFA_ADMIN_UNKNOWN_ERROR:
507 static int efa_com_wait_and_process_admin_cq_polling(struct efa_comp_ctx *comp_ctx,
508 struct efa_com_admin_queue *aq)
510 unsigned long timeout;
514 timeout = jiffies + usecs_to_jiffies(aq->completion_timeout);
517 spin_lock_irqsave(&aq->cq.lock, flags);
518 efa_com_handle_admin_completion(aq);
519 spin_unlock_irqrestore(&aq->cq.lock, flags);
521 if (comp_ctx->status != EFA_CMD_SUBMITTED)
524 if (time_is_before_jiffies(timeout)) {
525 ibdev_err_ratelimited(
527 "Wait for completion (polling) timeout\n");
528 /* EFA didn't have any completion */
529 atomic64_inc(&aq->stats.no_completion);
531 clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
536 msleep(aq->poll_interval);
539 err = efa_com_comp_status_to_errno(comp_ctx->comp_status);
541 efa_com_put_comp_ctx(aq, comp_ctx);
545 static int efa_com_wait_and_process_admin_cq_interrupts(struct efa_comp_ctx *comp_ctx,
546 struct efa_com_admin_queue *aq)
551 wait_for_completion_timeout(&comp_ctx->wait_event,
552 usecs_to_jiffies(aq->completion_timeout));
555 * In case the command wasn't completed find out the root cause.
556 * There might be 2 kinds of errors
557 * 1) No completion (timeout reached)
558 * 2) There is completion but the device didn't get any msi-x interrupt.
560 if (comp_ctx->status == EFA_CMD_SUBMITTED) {
561 spin_lock_irqsave(&aq->cq.lock, flags);
562 efa_com_handle_admin_completion(aq);
563 spin_unlock_irqrestore(&aq->cq.lock, flags);
565 atomic64_inc(&aq->stats.no_completion);
567 if (comp_ctx->status == EFA_CMD_COMPLETED)
568 ibdev_err_ratelimited(
570 "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",
571 efa_com_cmd_str(comp_ctx->cmd_opcode),
572 comp_ctx->cmd_opcode, comp_ctx->status,
573 comp_ctx, aq->sq.pc, aq->sq.cc, aq->cq.cc);
575 ibdev_err_ratelimited(
577 "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",
578 efa_com_cmd_str(comp_ctx->cmd_opcode),
579 comp_ctx->cmd_opcode, comp_ctx->status,
580 comp_ctx, aq->sq.pc, aq->sq.cc, aq->cq.cc);
582 clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
587 err = efa_com_comp_status_to_errno(comp_ctx->comp_status);
589 efa_com_put_comp_ctx(aq, comp_ctx);
594 * There are two types to wait for completion.
595 * Polling mode - wait until the completion is available.
596 * Async mode - wait on wait queue until the completion is ready
597 * (or the timeout expired).
598 * It is expected that the IRQ called efa_com_handle_admin_completion
599 * to mark the completions.
601 static int efa_com_wait_and_process_admin_cq(struct efa_comp_ctx *comp_ctx,
602 struct efa_com_admin_queue *aq)
604 if (test_bit(EFA_AQ_STATE_POLLING_BIT, &aq->state))
605 return efa_com_wait_and_process_admin_cq_polling(comp_ctx, aq);
607 return efa_com_wait_and_process_admin_cq_interrupts(comp_ctx, aq);
611 * efa_com_cmd_exec - Execute admin command
613 * @cmd: the admin command to execute.
614 * @cmd_size: the command size.
615 * @comp: command completion return entry.
616 * @comp_size: command completion size.
617 * Submit an admin command and then wait until the device will return a
619 * The completion will be copied into comp.
621 * @return - 0 on success, negative value on failure.
623 int efa_com_cmd_exec(struct efa_com_admin_queue *aq,
624 struct efa_admin_aq_entry *cmd,
626 struct efa_admin_acq_entry *comp,
629 struct efa_comp_ctx *comp_ctx;
634 /* In case of queue FULL */
635 down(&aq->avail_cmds);
637 ibdev_dbg(aq->efa_dev, "%s (opcode %d)\n",
638 efa_com_cmd_str(cmd->aq_common_descriptor.opcode),
639 cmd->aq_common_descriptor.opcode);
640 comp_ctx = efa_com_submit_admin_cmd(aq, cmd, cmd_size, comp, comp_size);
641 if (IS_ERR(comp_ctx)) {
642 ibdev_err_ratelimited(
644 "Failed to submit command %s (opcode %u) err %ld\n",
645 efa_com_cmd_str(cmd->aq_common_descriptor.opcode),
646 cmd->aq_common_descriptor.opcode, PTR_ERR(comp_ctx));
649 return PTR_ERR(comp_ctx);
652 err = efa_com_wait_and_process_admin_cq(comp_ctx, aq);
654 ibdev_err_ratelimited(
656 "Failed to process command %s (opcode %u) comp_status %d err %d\n",
657 efa_com_cmd_str(cmd->aq_common_descriptor.opcode),
658 cmd->aq_common_descriptor.opcode, comp_ctx->comp_status,
667 * efa_com_admin_destroy - Destroy the admin and the async events queues.
668 * @edev: EFA communication layer struct
670 void efa_com_admin_destroy(struct efa_com_dev *edev)
672 struct efa_com_admin_queue *aq = &edev->aq;
673 struct efa_com_aenq *aenq = &edev->aenq;
674 struct efa_com_admin_cq *cq = &aq->cq;
675 struct efa_com_admin_sq *sq = &aq->sq;
678 clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
680 devm_kfree(edev->dmadev, aq->comp_ctx_pool);
681 devm_kfree(edev->dmadev, aq->comp_ctx);
683 size = aq->depth * sizeof(*sq->entries);
684 dma_free_coherent(edev->dmadev, size, sq->entries, sq->dma_addr);
686 size = aq->depth * sizeof(*cq->entries);
687 dma_free_coherent(edev->dmadev, size, cq->entries, cq->dma_addr);
689 size = aenq->depth * sizeof(*aenq->entries);
690 dma_free_coherent(edev->dmadev, size, aenq->entries, aenq->dma_addr);
694 * efa_com_set_admin_polling_mode - Set the admin completion queue polling mode
695 * @edev: EFA communication layer struct
696 * @polling: Enable/Disable polling mode
698 * Set the admin completion mode.
700 void efa_com_set_admin_polling_mode(struct efa_com_dev *edev, bool polling)
705 mask_value = EFA_REGS_ADMIN_INTR_MASK;
707 writel(mask_value, edev->reg_bar + EFA_REGS_INTR_MASK_OFF);
709 set_bit(EFA_AQ_STATE_POLLING_BIT, &edev->aq.state);
711 clear_bit(EFA_AQ_STATE_POLLING_BIT, &edev->aq.state);
714 static void efa_com_stats_init(struct efa_com_dev *edev)
716 atomic64_t *s = (atomic64_t *)&edev->aq.stats;
719 for (i = 0; i < sizeof(edev->aq.stats) / sizeof(*s); i++, s++)
724 * efa_com_admin_init - Init the admin and the async queues
725 * @edev: EFA communication layer struct
726 * @aenq_handlers: Those handlers to be called upon event.
728 * Initialize the admin submission and completion queues.
729 * Initialize the asynchronous events notification queues.
731 * @return - 0 on success, negative value on failure.
733 int efa_com_admin_init(struct efa_com_dev *edev,
734 struct efa_aenq_handlers *aenq_handlers)
736 struct efa_com_admin_queue *aq = &edev->aq;
742 dev_sts = efa_com_reg_read32(edev, EFA_REGS_DEV_STS_OFF);
743 if (!(dev_sts & EFA_REGS_DEV_STS_READY_MASK)) {
744 ibdev_err(edev->efa_dev,
745 "Device isn't ready, abort com init %#x\n", dev_sts);
749 aq->depth = EFA_ADMIN_QUEUE_DEPTH;
751 aq->dmadev = edev->dmadev;
752 aq->efa_dev = edev->efa_dev;
753 set_bit(EFA_AQ_STATE_POLLING_BIT, &aq->state);
755 sema_init(&aq->avail_cmds, aq->depth);
757 efa_com_stats_init(edev);
759 err = efa_com_init_comp_ctxt(aq);
763 err = efa_com_admin_init_sq(edev);
765 goto err_destroy_comp_ctxt;
767 err = efa_com_admin_init_cq(edev);
771 efa_com_set_admin_polling_mode(edev, false);
773 err = efa_com_admin_init_aenq(edev, aenq_handlers);
777 cap = efa_com_reg_read32(edev, EFA_REGS_CAPS_OFF);
778 timeout = (cap & EFA_REGS_CAPS_ADMIN_CMD_TO_MASK) >>
779 EFA_REGS_CAPS_ADMIN_CMD_TO_SHIFT;
781 /* the resolution of timeout reg is 100ms */
782 aq->completion_timeout = timeout * 100000;
784 aq->completion_timeout = ADMIN_CMD_TIMEOUT_US;
786 aq->poll_interval = EFA_POLL_INTERVAL_MS;
788 set_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
793 dma_free_coherent(edev->dmadev, aq->depth * sizeof(*aq->cq.entries),
794 aq->cq.entries, aq->cq.dma_addr);
796 dma_free_coherent(edev->dmadev, aq->depth * sizeof(*aq->sq.entries),
797 aq->sq.entries, aq->sq.dma_addr);
798 err_destroy_comp_ctxt:
799 devm_kfree(edev->dmadev, aq->comp_ctx);
805 * efa_com_admin_q_comp_intr_handler - admin queue interrupt handler
806 * @edev: EFA communication layer struct
808 * This method goes over the admin completion queue and wakes up
809 * all the pending threads that wait on the commands wait event.
811 * @note: Should be called after MSI-X interrupt.
813 void efa_com_admin_q_comp_intr_handler(struct efa_com_dev *edev)
817 spin_lock_irqsave(&edev->aq.cq.lock, flags);
818 efa_com_handle_admin_completion(&edev->aq);
819 spin_unlock_irqrestore(&edev->aq.cq.lock, flags);
823 * efa_handle_specific_aenq_event:
824 * return the handler that is relevant to the specific event group
826 static efa_aenq_handler efa_com_get_specific_aenq_cb(struct efa_com_dev *edev,
829 struct efa_aenq_handlers *aenq_handlers = edev->aenq.aenq_handlers;
831 if (group < EFA_MAX_HANDLERS && aenq_handlers->handlers[group])
832 return aenq_handlers->handlers[group];
834 return aenq_handlers->unimplemented_handler;
838 * efa_com_aenq_intr_handler - AENQ interrupt handler
839 * @edev: EFA communication layer struct
840 * @data: Data of interrupt handler.
842 * Go over the async event notification queue and call the proper aenq handler.
844 void efa_com_aenq_intr_handler(struct efa_com_dev *edev, void *data)
846 struct efa_admin_aenq_common_desc *aenq_common;
847 struct efa_com_aenq *aenq = &edev->aenq;
848 struct efa_admin_aenq_entry *aenq_e;
849 efa_aenq_handler handler_cb;
854 ci = aenq->cc & (aenq->depth - 1);
856 aenq_e = &aenq->entries[ci]; /* Get first entry */
857 aenq_common = &aenq_e->aenq_common_desc;
859 /* Go over all the events */
860 while ((READ_ONCE(aenq_common->flags) &
861 EFA_ADMIN_AENQ_COMMON_DESC_PHASE_MASK) == phase) {
863 * Do not read the rest of the completion entry before the
864 * phase bit was validated
868 /* Handle specific event*/
869 handler_cb = efa_com_get_specific_aenq_cb(edev,
871 handler_cb(data, aenq_e); /* call the actual event handler*/
873 /* Get next event entry */
877 if (ci == aenq->depth) {
881 aenq_e = &aenq->entries[ci];
882 aenq_common = &aenq_e->aenq_common_desc;
885 aenq->cc += processed;
888 /* Don't update aenq doorbell if there weren't any processed events */
892 /* barrier not needed in case of writel */
893 writel(aenq->cc, edev->reg_bar + EFA_REGS_AENQ_CONS_DB_OFF);
896 static void efa_com_mmio_reg_read_resp_addr_init(struct efa_com_dev *edev)
898 struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
902 /* dma_addr_bits is unknown at this point */
903 addr_high = (mmio_read->read_resp_dma_addr >> 32) & GENMASK(31, 0);
904 addr_low = mmio_read->read_resp_dma_addr & GENMASK(31, 0);
906 writel(addr_high, edev->reg_bar + EFA_REGS_MMIO_RESP_HI_OFF);
907 writel(addr_low, edev->reg_bar + EFA_REGS_MMIO_RESP_LO_OFF);
910 int efa_com_mmio_reg_read_init(struct efa_com_dev *edev)
912 struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
914 spin_lock_init(&mmio_read->lock);
915 mmio_read->read_resp =
916 dma_alloc_coherent(edev->dmadev, sizeof(*mmio_read->read_resp),
917 &mmio_read->read_resp_dma_addr, GFP_KERNEL);
918 if (!mmio_read->read_resp)
921 efa_com_mmio_reg_read_resp_addr_init(edev);
923 mmio_read->read_resp->req_id = 0;
924 mmio_read->seq_num = 0;
925 mmio_read->mmio_read_timeout = EFA_REG_READ_TIMEOUT_US;
930 void efa_com_mmio_reg_read_destroy(struct efa_com_dev *edev)
932 struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
934 dma_free_coherent(edev->dmadev, sizeof(*mmio_read->read_resp),
935 mmio_read->read_resp, mmio_read->read_resp_dma_addr);
938 int efa_com_validate_version(struct efa_com_dev *edev)
945 * Make sure the EFA version and the controller version are at least
946 * as the driver expects
948 ver = efa_com_reg_read32(edev, EFA_REGS_VERSION_OFF);
949 ctrl_ver = efa_com_reg_read32(edev,
950 EFA_REGS_CONTROLLER_VERSION_OFF);
952 ibdev_dbg(edev->efa_dev, "efa device version: %d.%d\n",
953 (ver & EFA_REGS_VERSION_MAJOR_VERSION_MASK) >>
954 EFA_REGS_VERSION_MAJOR_VERSION_SHIFT,
955 ver & EFA_REGS_VERSION_MINOR_VERSION_MASK);
957 if (ver < MIN_EFA_VER) {
958 ibdev_err(edev->efa_dev,
959 "EFA version is lower than the minimal version the driver supports\n");
963 ibdev_dbg(edev->efa_dev,
964 "efa controller version: %d.%d.%d implementation version %d\n",
965 (ctrl_ver & EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION_MASK) >>
966 EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION_SHIFT,
967 (ctrl_ver & EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION_MASK) >>
968 EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION_SHIFT,
969 (ctrl_ver & EFA_REGS_CONTROLLER_VERSION_SUBMINOR_VERSION_MASK),
970 (ctrl_ver & EFA_REGS_CONTROLLER_VERSION_IMPL_ID_MASK) >>
971 EFA_REGS_CONTROLLER_VERSION_IMPL_ID_SHIFT);
974 (ctrl_ver & EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION_MASK) |
975 (ctrl_ver & EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION_MASK) |
976 (ctrl_ver & EFA_REGS_CONTROLLER_VERSION_SUBMINOR_VERSION_MASK);
978 /* Validate the ctrl version without the implementation ID */
979 if (ctrl_ver_masked < MIN_EFA_CTRL_VER) {
980 ibdev_err(edev->efa_dev,
981 "EFA ctrl version is lower than the minimal ctrl version the driver supports\n");
989 * efa_com_get_dma_width - Retrieve physical dma address width the device
991 * @edev: EFA communication layer struct
993 * Retrieve the maximum physical address bits the device can handle.
995 * @return: > 0 on Success and negative value otherwise.
997 int efa_com_get_dma_width(struct efa_com_dev *edev)
999 u32 caps = efa_com_reg_read32(edev, EFA_REGS_CAPS_OFF);
1002 width = (caps & EFA_REGS_CAPS_DMA_ADDR_WIDTH_MASK) >>
1003 EFA_REGS_CAPS_DMA_ADDR_WIDTH_SHIFT;
1005 ibdev_dbg(edev->efa_dev, "DMA width: %d\n", width);
1007 if (width < 32 || width > 64) {
1008 ibdev_err(edev->efa_dev, "DMA width illegal value: %d\n", width);
1012 edev->dma_addr_bits = width;
1017 static int wait_for_reset_state(struct efa_com_dev *edev, u32 timeout,
1022 for (i = 0; i < timeout; i++) {
1023 val = efa_com_reg_read32(edev, EFA_REGS_DEV_STS_OFF);
1025 if ((val & EFA_REGS_DEV_STS_RESET_IN_PROGRESS_MASK) ==
1029 ibdev_dbg(edev->efa_dev, "Reset indication val %d\n", val);
1030 msleep(EFA_POLL_INTERVAL_MS);
1037 * efa_com_dev_reset - Perform device FLR to the device.
1038 * @edev: EFA communication layer struct
1039 * @reset_reason: Specify what is the trigger for the reset in case of an error.
1041 * @return - 0 on success, negative value on failure.
1043 int efa_com_dev_reset(struct efa_com_dev *edev,
1044 enum efa_regs_reset_reason_types reset_reason)
1046 u32 stat, timeout, cap, reset_val;
1049 stat = efa_com_reg_read32(edev, EFA_REGS_DEV_STS_OFF);
1050 cap = efa_com_reg_read32(edev, EFA_REGS_CAPS_OFF);
1052 if (!(stat & EFA_REGS_DEV_STS_READY_MASK)) {
1053 ibdev_err(edev->efa_dev,
1054 "Device isn't ready, can't reset device\n");
1058 timeout = (cap & EFA_REGS_CAPS_RESET_TIMEOUT_MASK) >>
1059 EFA_REGS_CAPS_RESET_TIMEOUT_SHIFT;
1061 ibdev_err(edev->efa_dev, "Invalid timeout value\n");
1066 reset_val = EFA_REGS_DEV_CTL_DEV_RESET_MASK;
1067 reset_val |= (reset_reason << EFA_REGS_DEV_CTL_RESET_REASON_SHIFT) &
1068 EFA_REGS_DEV_CTL_RESET_REASON_MASK;
1069 writel(reset_val, edev->reg_bar + EFA_REGS_DEV_CTL_OFF);
1071 /* reset clears the mmio readless address, restore it */
1072 efa_com_mmio_reg_read_resp_addr_init(edev);
1074 err = wait_for_reset_state(edev, timeout,
1075 EFA_REGS_DEV_STS_RESET_IN_PROGRESS_MASK);
1077 ibdev_err(edev->efa_dev, "Reset indication didn't turn on\n");
1082 writel(0, edev->reg_bar + EFA_REGS_DEV_CTL_OFF);
1083 err = wait_for_reset_state(edev, timeout, 0);
1085 ibdev_err(edev->efa_dev, "Reset indication didn't turn off\n");
1089 timeout = (cap & EFA_REGS_CAPS_ADMIN_CMD_TO_MASK) >>
1090 EFA_REGS_CAPS_ADMIN_CMD_TO_SHIFT;
1092 /* the resolution of timeout reg is 100ms */
1093 edev->aq.completion_timeout = timeout * 100000;
1095 edev->aq.completion_timeout = ADMIN_CMD_TIMEOUT_US;