1 // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
3 * Copyright 2018-2021 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
19 #define EFA_CTRL_MAJOR 0
20 #define EFA_CTRL_MINOR 0
21 #define EFA_CTRL_SUB_MINOR 1
29 struct completion wait_event;
30 struct efa_admin_acq_entry *user_cqe;
32 enum efa_cmd_status status;
37 static const char *efa_com_cmd_str(u8 cmd)
39 #define EFA_CMD_STR_CASE(_cmd) case EFA_ADMIN_##_cmd: return #_cmd
42 EFA_CMD_STR_CASE(CREATE_QP);
43 EFA_CMD_STR_CASE(MODIFY_QP);
44 EFA_CMD_STR_CASE(QUERY_QP);
45 EFA_CMD_STR_CASE(DESTROY_QP);
46 EFA_CMD_STR_CASE(CREATE_AH);
47 EFA_CMD_STR_CASE(DESTROY_AH);
48 EFA_CMD_STR_CASE(REG_MR);
49 EFA_CMD_STR_CASE(DEREG_MR);
50 EFA_CMD_STR_CASE(CREATE_CQ);
51 EFA_CMD_STR_CASE(DESTROY_CQ);
52 EFA_CMD_STR_CASE(GET_FEATURE);
53 EFA_CMD_STR_CASE(SET_FEATURE);
54 EFA_CMD_STR_CASE(GET_STATS);
55 EFA_CMD_STR_CASE(ALLOC_PD);
56 EFA_CMD_STR_CASE(DEALLOC_PD);
57 EFA_CMD_STR_CASE(ALLOC_UAR);
58 EFA_CMD_STR_CASE(DEALLOC_UAR);
59 EFA_CMD_STR_CASE(CREATE_EQ);
60 EFA_CMD_STR_CASE(DESTROY_EQ);
61 default: return "unknown command opcode";
63 #undef EFA_CMD_STR_CASE
66 void efa_com_set_dma_addr(dma_addr_t addr, u32 *addr_high, u32 *addr_low)
68 *addr_low = lower_32_bits(addr);
69 *addr_high = upper_32_bits(addr);
72 static u32 efa_com_reg_read32(struct efa_com_dev *edev, u16 offset)
74 struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
75 struct efa_admin_mmio_req_read_less_resp *read_resp;
76 unsigned long exp_time;
77 u32 mmio_read_reg = 0;
80 read_resp = mmio_read->read_resp;
82 spin_lock(&mmio_read->lock);
85 /* trash DMA req_id to identify when hardware is done */
86 read_resp->req_id = mmio_read->seq_num + 0x9aL;
87 EFA_SET(&mmio_read_reg, EFA_REGS_MMIO_REG_READ_REG_OFF, offset);
88 EFA_SET(&mmio_read_reg, EFA_REGS_MMIO_REG_READ_REQ_ID,
91 writel(mmio_read_reg, edev->reg_bar + EFA_REGS_MMIO_REG_READ_OFF);
93 exp_time = jiffies + usecs_to_jiffies(mmio_read->mmio_read_timeout);
95 if (READ_ONCE(read_resp->req_id) == mmio_read->seq_num)
98 } while (time_is_after_jiffies(exp_time));
100 if (read_resp->req_id != mmio_read->seq_num) {
101 ibdev_err_ratelimited(
103 "Reading register timed out. expected: req id[%u] offset[%#x] actual: req id[%u] offset[%#x]\n",
104 mmio_read->seq_num, offset, read_resp->req_id,
106 err = EFA_MMIO_READ_INVALID;
110 if (read_resp->reg_off != offset) {
111 ibdev_err_ratelimited(
113 "Reading register failed: wrong offset provided\n");
114 err = EFA_MMIO_READ_INVALID;
118 err = read_resp->reg_val;
120 spin_unlock(&mmio_read->lock);
124 static int efa_com_admin_init_sq(struct efa_com_dev *edev)
126 struct efa_com_admin_queue *aq = &edev->aq;
127 struct efa_com_admin_sq *sq = &aq->sq;
128 u16 size = aq->depth * sizeof(*sq->entries);
134 dma_alloc_coherent(aq->dmadev, size, &sq->dma_addr, GFP_KERNEL);
138 spin_lock_init(&sq->lock);
144 sq->db_addr = (u32 __iomem *)(edev->reg_bar + EFA_REGS_AQ_PROD_DB_OFF);
146 addr_high = upper_32_bits(sq->dma_addr);
147 addr_low = lower_32_bits(sq->dma_addr);
149 writel(addr_low, edev->reg_bar + EFA_REGS_AQ_BASE_LO_OFF);
150 writel(addr_high, edev->reg_bar + EFA_REGS_AQ_BASE_HI_OFF);
152 EFA_SET(&aq_caps, EFA_REGS_AQ_CAPS_AQ_DEPTH, aq->depth);
153 EFA_SET(&aq_caps, EFA_REGS_AQ_CAPS_AQ_ENTRY_SIZE,
154 sizeof(struct efa_admin_aq_entry));
156 writel(aq_caps, edev->reg_bar + EFA_REGS_AQ_CAPS_OFF);
161 static int efa_com_admin_init_cq(struct efa_com_dev *edev)
163 struct efa_com_admin_queue *aq = &edev->aq;
164 struct efa_com_admin_cq *cq = &aq->cq;
165 u16 size = aq->depth * sizeof(*cq->entries);
171 dma_alloc_coherent(aq->dmadev, size, &cq->dma_addr, GFP_KERNEL);
175 spin_lock_init(&cq->lock);
180 addr_high = upper_32_bits(cq->dma_addr);
181 addr_low = lower_32_bits(cq->dma_addr);
183 writel(addr_low, edev->reg_bar + EFA_REGS_ACQ_BASE_LO_OFF);
184 writel(addr_high, edev->reg_bar + EFA_REGS_ACQ_BASE_HI_OFF);
186 EFA_SET(&acq_caps, EFA_REGS_ACQ_CAPS_ACQ_DEPTH, aq->depth);
187 EFA_SET(&acq_caps, EFA_REGS_ACQ_CAPS_ACQ_ENTRY_SIZE,
188 sizeof(struct efa_admin_acq_entry));
189 EFA_SET(&acq_caps, EFA_REGS_ACQ_CAPS_ACQ_MSIX_VECTOR,
190 aq->msix_vector_idx);
192 writel(acq_caps, edev->reg_bar + EFA_REGS_ACQ_CAPS_OFF);
197 static int efa_com_admin_init_aenq(struct efa_com_dev *edev,
198 struct efa_aenq_handlers *aenq_handlers)
200 struct efa_com_aenq *aenq = &edev->aenq;
201 u32 addr_low, addr_high;
205 if (!aenq_handlers) {
206 ibdev_err(edev->efa_dev, "aenq handlers pointer is NULL\n");
210 size = EFA_ASYNC_QUEUE_DEPTH * sizeof(*aenq->entries);
211 aenq->entries = dma_alloc_coherent(edev->dmadev, size, &aenq->dma_addr,
216 aenq->aenq_handlers = aenq_handlers;
217 aenq->depth = EFA_ASYNC_QUEUE_DEPTH;
221 addr_low = lower_32_bits(aenq->dma_addr);
222 addr_high = upper_32_bits(aenq->dma_addr);
224 writel(addr_low, edev->reg_bar + EFA_REGS_AENQ_BASE_LO_OFF);
225 writel(addr_high, edev->reg_bar + EFA_REGS_AENQ_BASE_HI_OFF);
227 EFA_SET(&aenq_caps, EFA_REGS_AENQ_CAPS_AENQ_DEPTH, aenq->depth);
228 EFA_SET(&aenq_caps, EFA_REGS_AENQ_CAPS_AENQ_ENTRY_SIZE,
229 sizeof(struct efa_admin_aenq_entry));
230 EFA_SET(&aenq_caps, EFA_REGS_AENQ_CAPS_AENQ_MSIX_VECTOR,
231 aenq->msix_vector_idx);
232 writel(aenq_caps, edev->reg_bar + EFA_REGS_AENQ_CAPS_OFF);
235 * Init cons_db to mark that all entries in the queue
236 * are initially available
238 writel(edev->aenq.cc, edev->reg_bar + EFA_REGS_AENQ_CONS_DB_OFF);
243 /* ID to be used with efa_com_get_comp_ctx */
244 static u16 efa_com_alloc_ctx_id(struct efa_com_admin_queue *aq)
248 spin_lock(&aq->comp_ctx_lock);
249 ctx_id = aq->comp_ctx_pool[aq->comp_ctx_pool_next];
250 aq->comp_ctx_pool_next++;
251 spin_unlock(&aq->comp_ctx_lock);
256 static void efa_com_dealloc_ctx_id(struct efa_com_admin_queue *aq,
259 spin_lock(&aq->comp_ctx_lock);
260 aq->comp_ctx_pool_next--;
261 aq->comp_ctx_pool[aq->comp_ctx_pool_next] = ctx_id;
262 spin_unlock(&aq->comp_ctx_lock);
265 static inline void efa_com_put_comp_ctx(struct efa_com_admin_queue *aq,
266 struct efa_comp_ctx *comp_ctx)
268 u16 cmd_id = EFA_GET(&comp_ctx->user_cqe->acq_common_descriptor.command,
269 EFA_ADMIN_ACQ_COMMON_DESC_COMMAND_ID);
270 u16 ctx_id = cmd_id & (aq->depth - 1);
272 ibdev_dbg(aq->efa_dev, "Put completion command_id %#x\n", cmd_id);
273 comp_ctx->occupied = 0;
274 efa_com_dealloc_ctx_id(aq, ctx_id);
277 static struct efa_comp_ctx *efa_com_get_comp_ctx(struct efa_com_admin_queue *aq,
278 u16 cmd_id, bool capture)
280 u16 ctx_id = cmd_id & (aq->depth - 1);
282 if (aq->comp_ctx[ctx_id].occupied && capture) {
283 ibdev_err_ratelimited(
285 "Completion context for command_id %#x is occupied\n",
291 aq->comp_ctx[ctx_id].occupied = 1;
292 ibdev_dbg(aq->efa_dev,
293 "Take completion ctxt for command_id %#x\n", cmd_id);
296 return &aq->comp_ctx[ctx_id];
299 static struct efa_comp_ctx *__efa_com_submit_admin_cmd(struct efa_com_admin_queue *aq,
300 struct efa_admin_aq_entry *cmd,
301 size_t cmd_size_in_bytes,
302 struct efa_admin_acq_entry *comp,
303 size_t comp_size_in_bytes)
305 struct efa_admin_aq_entry *aqe;
306 struct efa_comp_ctx *comp_ctx;
312 queue_size_mask = aq->depth - 1;
313 pi = aq->sq.pc & queue_size_mask;
315 ctx_id = efa_com_alloc_ctx_id(aq);
317 /* cmd_id LSBs are the ctx_id and MSBs are entropy bits from pc */
318 cmd_id = ctx_id & queue_size_mask;
319 cmd_id |= aq->sq.pc & ~queue_size_mask;
320 cmd_id &= EFA_ADMIN_AQ_COMMON_DESC_COMMAND_ID_MASK;
322 cmd->aq_common_descriptor.command_id = cmd_id;
323 EFA_SET(&cmd->aq_common_descriptor.flags,
324 EFA_ADMIN_AQ_COMMON_DESC_PHASE, aq->sq.phase);
326 comp_ctx = efa_com_get_comp_ctx(aq, cmd_id, true);
328 efa_com_dealloc_ctx_id(aq, ctx_id);
329 return ERR_PTR(-EINVAL);
332 comp_ctx->status = EFA_CMD_SUBMITTED;
333 comp_ctx->comp_size = comp_size_in_bytes;
334 comp_ctx->user_cqe = comp;
335 comp_ctx->cmd_opcode = cmd->aq_common_descriptor.opcode;
337 reinit_completion(&comp_ctx->wait_event);
339 aqe = &aq->sq.entries[pi];
340 memset(aqe, 0, sizeof(*aqe));
341 memcpy(aqe, cmd, cmd_size_in_bytes);
344 atomic64_inc(&aq->stats.submitted_cmd);
346 if ((aq->sq.pc & queue_size_mask) == 0)
347 aq->sq.phase = !aq->sq.phase;
349 /* barrier not needed in case of writel */
350 writel(aq->sq.pc, aq->sq.db_addr);
355 static inline int efa_com_init_comp_ctxt(struct efa_com_admin_queue *aq)
357 size_t pool_size = aq->depth * sizeof(*aq->comp_ctx_pool);
358 size_t size = aq->depth * sizeof(struct efa_comp_ctx);
359 struct efa_comp_ctx *comp_ctx;
362 aq->comp_ctx = devm_kzalloc(aq->dmadev, size, GFP_KERNEL);
363 aq->comp_ctx_pool = devm_kzalloc(aq->dmadev, pool_size, GFP_KERNEL);
364 if (!aq->comp_ctx || !aq->comp_ctx_pool) {
365 devm_kfree(aq->dmadev, aq->comp_ctx_pool);
366 devm_kfree(aq->dmadev, aq->comp_ctx);
370 for (i = 0; i < aq->depth; i++) {
371 comp_ctx = efa_com_get_comp_ctx(aq, i, false);
373 init_completion(&comp_ctx->wait_event);
375 aq->comp_ctx_pool[i] = i;
378 spin_lock_init(&aq->comp_ctx_lock);
380 aq->comp_ctx_pool_next = 0;
385 static struct efa_comp_ctx *efa_com_submit_admin_cmd(struct efa_com_admin_queue *aq,
386 struct efa_admin_aq_entry *cmd,
387 size_t cmd_size_in_bytes,
388 struct efa_admin_acq_entry *comp,
389 size_t comp_size_in_bytes)
391 struct efa_comp_ctx *comp_ctx;
393 spin_lock(&aq->sq.lock);
394 if (!test_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state)) {
395 ibdev_err_ratelimited(aq->efa_dev, "Admin queue is closed\n");
396 spin_unlock(&aq->sq.lock);
397 return ERR_PTR(-ENODEV);
400 comp_ctx = __efa_com_submit_admin_cmd(aq, cmd, cmd_size_in_bytes, comp,
402 spin_unlock(&aq->sq.lock);
403 if (IS_ERR(comp_ctx))
404 clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
409 static void efa_com_handle_single_admin_completion(struct efa_com_admin_queue *aq,
410 struct efa_admin_acq_entry *cqe)
412 struct efa_comp_ctx *comp_ctx;
415 cmd_id = EFA_GET(&cqe->acq_common_descriptor.command,
416 EFA_ADMIN_ACQ_COMMON_DESC_COMMAND_ID);
418 comp_ctx = efa_com_get_comp_ctx(aq, cmd_id, false);
420 ibdev_err(aq->efa_dev,
421 "comp_ctx is NULL. Changing the admin queue running state\n");
422 clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
426 comp_ctx->status = EFA_CMD_COMPLETED;
427 memcpy(comp_ctx->user_cqe, cqe, comp_ctx->comp_size);
429 if (!test_bit(EFA_AQ_STATE_POLLING_BIT, &aq->state))
430 complete(&comp_ctx->wait_event);
433 static void efa_com_handle_admin_completion(struct efa_com_admin_queue *aq)
435 struct efa_admin_acq_entry *cqe;
441 queue_size_mask = aq->depth - 1;
443 ci = aq->cq.cc & queue_size_mask;
444 phase = aq->cq.phase;
446 cqe = &aq->cq.entries[ci];
448 /* Go over all the completions */
449 while ((READ_ONCE(cqe->acq_common_descriptor.flags) &
450 EFA_ADMIN_ACQ_COMMON_DESC_PHASE_MASK) == phase) {
452 * Do not read the rest of the completion entry before the
453 * phase bit was validated
456 efa_com_handle_single_admin_completion(aq, cqe);
460 if (ci == aq->depth) {
465 cqe = &aq->cq.entries[ci];
468 aq->cq.cc += comp_num;
469 aq->cq.phase = phase;
470 aq->sq.cc += comp_num;
471 atomic64_add(comp_num, &aq->stats.completed_cmd);
474 static int efa_com_comp_status_to_errno(u8 comp_status)
476 switch (comp_status) {
477 case EFA_ADMIN_SUCCESS:
479 case EFA_ADMIN_RESOURCE_ALLOCATION_FAILURE:
481 case EFA_ADMIN_UNSUPPORTED_OPCODE:
483 case EFA_ADMIN_BAD_OPCODE:
484 case EFA_ADMIN_MALFORMED_REQUEST:
485 case EFA_ADMIN_ILLEGAL_PARAMETER:
486 case EFA_ADMIN_UNKNOWN_ERROR:
493 static int efa_com_wait_and_process_admin_cq_polling(struct efa_comp_ctx *comp_ctx,
494 struct efa_com_admin_queue *aq)
496 unsigned long timeout;
500 timeout = jiffies + usecs_to_jiffies(aq->completion_timeout);
503 spin_lock_irqsave(&aq->cq.lock, flags);
504 efa_com_handle_admin_completion(aq);
505 spin_unlock_irqrestore(&aq->cq.lock, flags);
507 if (comp_ctx->status != EFA_CMD_SUBMITTED)
510 if (time_is_before_jiffies(timeout)) {
511 ibdev_err_ratelimited(
513 "Wait for completion (polling) timeout\n");
514 /* EFA didn't have any completion */
515 atomic64_inc(&aq->stats.no_completion);
517 clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
522 msleep(aq->poll_interval);
525 err = efa_com_comp_status_to_errno(comp_ctx->user_cqe->acq_common_descriptor.status);
527 efa_com_put_comp_ctx(aq, comp_ctx);
531 static int efa_com_wait_and_process_admin_cq_interrupts(struct efa_comp_ctx *comp_ctx,
532 struct efa_com_admin_queue *aq)
537 wait_for_completion_timeout(&comp_ctx->wait_event,
538 usecs_to_jiffies(aq->completion_timeout));
541 * In case the command wasn't completed find out the root cause.
542 * There might be 2 kinds of errors
543 * 1) No completion (timeout reached)
544 * 2) There is completion but the device didn't get any msi-x interrupt.
546 if (comp_ctx->status == EFA_CMD_SUBMITTED) {
547 spin_lock_irqsave(&aq->cq.lock, flags);
548 efa_com_handle_admin_completion(aq);
549 spin_unlock_irqrestore(&aq->cq.lock, flags);
551 atomic64_inc(&aq->stats.no_completion);
553 if (comp_ctx->status == EFA_CMD_COMPLETED)
554 ibdev_err_ratelimited(
556 "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",
557 efa_com_cmd_str(comp_ctx->cmd_opcode),
558 comp_ctx->cmd_opcode, comp_ctx->status,
559 comp_ctx, aq->sq.pc, aq->sq.cc, aq->cq.cc);
561 ibdev_err_ratelimited(
563 "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",
564 efa_com_cmd_str(comp_ctx->cmd_opcode),
565 comp_ctx->cmd_opcode, comp_ctx->status,
566 comp_ctx, aq->sq.pc, aq->sq.cc, aq->cq.cc);
568 clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
573 err = efa_com_comp_status_to_errno(comp_ctx->user_cqe->acq_common_descriptor.status);
575 efa_com_put_comp_ctx(aq, comp_ctx);
580 * There are two types to wait for completion.
581 * Polling mode - wait until the completion is available.
582 * Async mode - wait on wait queue until the completion is ready
583 * (or the timeout expired).
584 * It is expected that the IRQ called efa_com_handle_admin_completion
585 * to mark the completions.
587 static int efa_com_wait_and_process_admin_cq(struct efa_comp_ctx *comp_ctx,
588 struct efa_com_admin_queue *aq)
590 if (test_bit(EFA_AQ_STATE_POLLING_BIT, &aq->state))
591 return efa_com_wait_and_process_admin_cq_polling(comp_ctx, aq);
593 return efa_com_wait_and_process_admin_cq_interrupts(comp_ctx, aq);
597 * efa_com_cmd_exec - Execute admin command
599 * @cmd: the admin command to execute.
600 * @cmd_size: the command size.
601 * @comp: command completion return entry.
602 * @comp_size: command completion size.
603 * Submit an admin command and then wait until the device will return a
605 * The completion will be copied into comp.
607 * @return - 0 on success, negative value on failure.
609 int efa_com_cmd_exec(struct efa_com_admin_queue *aq,
610 struct efa_admin_aq_entry *cmd,
612 struct efa_admin_acq_entry *comp,
615 struct efa_comp_ctx *comp_ctx;
620 /* In case of queue FULL */
621 down(&aq->avail_cmds);
623 ibdev_dbg(aq->efa_dev, "%s (opcode %d)\n",
624 efa_com_cmd_str(cmd->aq_common_descriptor.opcode),
625 cmd->aq_common_descriptor.opcode);
626 comp_ctx = efa_com_submit_admin_cmd(aq, cmd, cmd_size, comp, comp_size);
627 if (IS_ERR(comp_ctx)) {
628 ibdev_err_ratelimited(
630 "Failed to submit command %s (opcode %u) err %ld\n",
631 efa_com_cmd_str(cmd->aq_common_descriptor.opcode),
632 cmd->aq_common_descriptor.opcode, PTR_ERR(comp_ctx));
635 atomic64_inc(&aq->stats.cmd_err);
636 return PTR_ERR(comp_ctx);
639 err = efa_com_wait_and_process_admin_cq(comp_ctx, aq);
641 ibdev_err_ratelimited(
643 "Failed to process command %s (opcode %u) comp_status %d err %d\n",
644 efa_com_cmd_str(cmd->aq_common_descriptor.opcode),
645 cmd->aq_common_descriptor.opcode,
646 comp_ctx->user_cqe->acq_common_descriptor.status, err);
647 atomic64_inc(&aq->stats.cmd_err);
656 * efa_com_admin_destroy - Destroy the admin and the async events queues.
657 * @edev: EFA communication layer struct
659 void efa_com_admin_destroy(struct efa_com_dev *edev)
661 struct efa_com_admin_queue *aq = &edev->aq;
662 struct efa_com_aenq *aenq = &edev->aenq;
663 struct efa_com_admin_cq *cq = &aq->cq;
664 struct efa_com_admin_sq *sq = &aq->sq;
667 clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
669 devm_kfree(edev->dmadev, aq->comp_ctx_pool);
670 devm_kfree(edev->dmadev, aq->comp_ctx);
672 size = aq->depth * sizeof(*sq->entries);
673 dma_free_coherent(edev->dmadev, size, sq->entries, sq->dma_addr);
675 size = aq->depth * sizeof(*cq->entries);
676 dma_free_coherent(edev->dmadev, size, cq->entries, cq->dma_addr);
678 size = aenq->depth * sizeof(*aenq->entries);
679 dma_free_coherent(edev->dmadev, size, aenq->entries, aenq->dma_addr);
683 * efa_com_set_admin_polling_mode - Set the admin completion queue polling mode
684 * @edev: EFA communication layer struct
685 * @polling: Enable/Disable polling mode
687 * Set the admin completion mode.
689 void efa_com_set_admin_polling_mode(struct efa_com_dev *edev, bool polling)
694 EFA_SET(&mask_value, EFA_REGS_INTR_MASK_EN, 1);
696 writel(mask_value, edev->reg_bar + EFA_REGS_INTR_MASK_OFF);
698 set_bit(EFA_AQ_STATE_POLLING_BIT, &edev->aq.state);
700 clear_bit(EFA_AQ_STATE_POLLING_BIT, &edev->aq.state);
703 static void efa_com_stats_init(struct efa_com_dev *edev)
705 atomic64_t *s = (atomic64_t *)&edev->aq.stats;
708 for (i = 0; i < sizeof(edev->aq.stats) / sizeof(*s); i++, s++)
713 * efa_com_admin_init - Init the admin and the async queues
714 * @edev: EFA communication layer struct
715 * @aenq_handlers: Those handlers to be called upon event.
717 * Initialize the admin submission and completion queues.
718 * Initialize the asynchronous events notification queues.
720 * @return - 0 on success, negative value on failure.
722 int efa_com_admin_init(struct efa_com_dev *edev,
723 struct efa_aenq_handlers *aenq_handlers)
725 struct efa_com_admin_queue *aq = &edev->aq;
731 dev_sts = efa_com_reg_read32(edev, EFA_REGS_DEV_STS_OFF);
732 if (!EFA_GET(&dev_sts, EFA_REGS_DEV_STS_READY)) {
733 ibdev_err(edev->efa_dev,
734 "Device isn't ready, abort com init %#x\n", dev_sts);
738 aq->depth = EFA_ADMIN_QUEUE_DEPTH;
740 aq->dmadev = edev->dmadev;
741 aq->efa_dev = edev->efa_dev;
742 set_bit(EFA_AQ_STATE_POLLING_BIT, &aq->state);
744 sema_init(&aq->avail_cmds, aq->depth);
746 efa_com_stats_init(edev);
748 err = efa_com_init_comp_ctxt(aq);
752 err = efa_com_admin_init_sq(edev);
754 goto err_destroy_comp_ctxt;
756 err = efa_com_admin_init_cq(edev);
760 efa_com_set_admin_polling_mode(edev, false);
762 err = efa_com_admin_init_aenq(edev, aenq_handlers);
766 cap = efa_com_reg_read32(edev, EFA_REGS_CAPS_OFF);
767 timeout = EFA_GET(&cap, EFA_REGS_CAPS_ADMIN_CMD_TO);
769 /* the resolution of timeout reg is 100ms */
770 aq->completion_timeout = timeout * 100000;
772 aq->completion_timeout = ADMIN_CMD_TIMEOUT_US;
774 aq->poll_interval = EFA_POLL_INTERVAL_MS;
776 set_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
781 dma_free_coherent(edev->dmadev, aq->depth * sizeof(*aq->cq.entries),
782 aq->cq.entries, aq->cq.dma_addr);
784 dma_free_coherent(edev->dmadev, aq->depth * sizeof(*aq->sq.entries),
785 aq->sq.entries, aq->sq.dma_addr);
786 err_destroy_comp_ctxt:
787 devm_kfree(edev->dmadev, aq->comp_ctx);
793 * efa_com_admin_q_comp_intr_handler - admin queue interrupt handler
794 * @edev: EFA communication layer struct
796 * This method goes over the admin completion queue and wakes up
797 * all the pending threads that wait on the commands wait event.
799 * Note: Should be called after MSI-X interrupt.
801 void efa_com_admin_q_comp_intr_handler(struct efa_com_dev *edev)
805 spin_lock_irqsave(&edev->aq.cq.lock, flags);
806 efa_com_handle_admin_completion(&edev->aq);
807 spin_unlock_irqrestore(&edev->aq.cq.lock, flags);
811 * efa_handle_specific_aenq_event:
812 * return the handler that is relevant to the specific event group
814 static efa_aenq_handler efa_com_get_specific_aenq_cb(struct efa_com_dev *edev,
817 struct efa_aenq_handlers *aenq_handlers = edev->aenq.aenq_handlers;
819 if (group < EFA_MAX_HANDLERS && aenq_handlers->handlers[group])
820 return aenq_handlers->handlers[group];
822 return aenq_handlers->unimplemented_handler;
826 * efa_com_aenq_intr_handler - AENQ interrupt handler
827 * @edev: EFA communication layer struct
828 * @data: Data of interrupt handler.
830 * Go over the async event notification queue and call the proper aenq handler.
832 void efa_com_aenq_intr_handler(struct efa_com_dev *edev, void *data)
834 struct efa_admin_aenq_common_desc *aenq_common;
835 struct efa_com_aenq *aenq = &edev->aenq;
836 struct efa_admin_aenq_entry *aenq_e;
837 efa_aenq_handler handler_cb;
842 ci = aenq->cc & (aenq->depth - 1);
844 aenq_e = &aenq->entries[ci]; /* Get first entry */
845 aenq_common = &aenq_e->aenq_common_desc;
847 /* Go over all the events */
848 while ((READ_ONCE(aenq_common->flags) &
849 EFA_ADMIN_AENQ_COMMON_DESC_PHASE_MASK) == phase) {
851 * Do not read the rest of the completion entry before the
852 * phase bit was validated
856 /* Handle specific event*/
857 handler_cb = efa_com_get_specific_aenq_cb(edev,
859 handler_cb(data, aenq_e); /* call the actual event handler*/
861 /* Get next event entry */
865 if (ci == aenq->depth) {
869 aenq_e = &aenq->entries[ci];
870 aenq_common = &aenq_e->aenq_common_desc;
873 aenq->cc += processed;
876 /* Don't update aenq doorbell if there weren't any processed events */
880 /* barrier not needed in case of writel */
881 writel(aenq->cc, edev->reg_bar + EFA_REGS_AENQ_CONS_DB_OFF);
884 static void efa_com_mmio_reg_read_resp_addr_init(struct efa_com_dev *edev)
886 struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
890 /* dma_addr_bits is unknown at this point */
891 addr_high = (mmio_read->read_resp_dma_addr >> 32) & GENMASK(31, 0);
892 addr_low = mmio_read->read_resp_dma_addr & GENMASK(31, 0);
894 writel(addr_high, edev->reg_bar + EFA_REGS_MMIO_RESP_HI_OFF);
895 writel(addr_low, edev->reg_bar + EFA_REGS_MMIO_RESP_LO_OFF);
898 int efa_com_mmio_reg_read_init(struct efa_com_dev *edev)
900 struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
902 spin_lock_init(&mmio_read->lock);
903 mmio_read->read_resp =
904 dma_alloc_coherent(edev->dmadev, sizeof(*mmio_read->read_resp),
905 &mmio_read->read_resp_dma_addr, GFP_KERNEL);
906 if (!mmio_read->read_resp)
909 efa_com_mmio_reg_read_resp_addr_init(edev);
911 mmio_read->read_resp->req_id = 0;
912 mmio_read->seq_num = 0;
913 mmio_read->mmio_read_timeout = EFA_REG_READ_TIMEOUT_US;
918 void efa_com_mmio_reg_read_destroy(struct efa_com_dev *edev)
920 struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
922 dma_free_coherent(edev->dmadev, sizeof(*mmio_read->read_resp),
923 mmio_read->read_resp, mmio_read->read_resp_dma_addr);
926 int efa_com_validate_version(struct efa_com_dev *edev)
928 u32 min_ctrl_ver = 0;
935 * Make sure the EFA version and the controller version are at least
936 * as the driver expects
938 ver = efa_com_reg_read32(edev, EFA_REGS_VERSION_OFF);
939 ctrl_ver = efa_com_reg_read32(edev,
940 EFA_REGS_CONTROLLER_VERSION_OFF);
942 ibdev_dbg(edev->efa_dev, "efa device version: %d.%d\n",
943 EFA_GET(&ver, EFA_REGS_VERSION_MAJOR_VERSION),
944 EFA_GET(&ver, EFA_REGS_VERSION_MINOR_VERSION));
946 EFA_SET(&min_ver, EFA_REGS_VERSION_MAJOR_VERSION,
947 EFA_ADMIN_API_VERSION_MAJOR);
948 EFA_SET(&min_ver, EFA_REGS_VERSION_MINOR_VERSION,
949 EFA_ADMIN_API_VERSION_MINOR);
951 ibdev_err(edev->efa_dev,
952 "EFA version is lower than the minimal version the driver supports\n");
958 "efa controller version: %d.%d.%d implementation version %d\n",
959 EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION),
960 EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION),
962 EFA_REGS_CONTROLLER_VERSION_SUBMINOR_VERSION),
963 EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_IMPL_ID));
966 EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION) |
967 EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION) |
969 EFA_REGS_CONTROLLER_VERSION_SUBMINOR_VERSION);
971 EFA_SET(&min_ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION,
973 EFA_SET(&min_ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION,
975 EFA_SET(&min_ctrl_ver, EFA_REGS_CONTROLLER_VERSION_SUBMINOR_VERSION,
977 /* Validate the ctrl version without the implementation ID */
978 if (ctrl_ver_masked < min_ctrl_ver) {
979 ibdev_err(edev->efa_dev,
980 "EFA ctrl version is lower than the minimal ctrl version the driver supports\n");
988 * efa_com_get_dma_width - Retrieve physical dma address width the device
990 * @edev: EFA communication layer struct
992 * Retrieve the maximum physical address bits the device can handle.
994 * @return: > 0 on Success and negative value otherwise.
996 int efa_com_get_dma_width(struct efa_com_dev *edev)
998 u32 caps = efa_com_reg_read32(edev, EFA_REGS_CAPS_OFF);
1001 width = EFA_GET(&caps, EFA_REGS_CAPS_DMA_ADDR_WIDTH);
1003 ibdev_dbg(edev->efa_dev, "DMA width: %d\n", width);
1005 if (width < 32 || width > 64) {
1006 ibdev_err(edev->efa_dev, "DMA width illegal value: %d\n", width);
1010 edev->dma_addr_bits = width;
1015 static int wait_for_reset_state(struct efa_com_dev *edev, u32 timeout, int on)
1019 for (i = 0; i < timeout; i++) {
1020 val = efa_com_reg_read32(edev, EFA_REGS_DEV_STS_OFF);
1022 if (EFA_GET(&val, EFA_REGS_DEV_STS_RESET_IN_PROGRESS) == on)
1025 ibdev_dbg(edev->efa_dev, "Reset indication val %d\n", val);
1026 msleep(EFA_POLL_INTERVAL_MS);
1033 * efa_com_dev_reset - Perform device FLR to the device.
1034 * @edev: EFA communication layer struct
1035 * @reset_reason: Specify what is the trigger for the reset in case of an error.
1037 * @return - 0 on success, negative value on failure.
1039 int efa_com_dev_reset(struct efa_com_dev *edev,
1040 enum efa_regs_reset_reason_types reset_reason)
1042 u32 stat, timeout, cap;
1046 stat = efa_com_reg_read32(edev, EFA_REGS_DEV_STS_OFF);
1047 cap = efa_com_reg_read32(edev, EFA_REGS_CAPS_OFF);
1049 if (!EFA_GET(&stat, EFA_REGS_DEV_STS_READY)) {
1050 ibdev_err(edev->efa_dev,
1051 "Device isn't ready, can't reset device\n");
1055 timeout = EFA_GET(&cap, EFA_REGS_CAPS_RESET_TIMEOUT);
1057 ibdev_err(edev->efa_dev, "Invalid timeout value\n");
1062 EFA_SET(&reset_val, EFA_REGS_DEV_CTL_DEV_RESET, 1);
1063 EFA_SET(&reset_val, EFA_REGS_DEV_CTL_RESET_REASON, reset_reason);
1064 writel(reset_val, edev->reg_bar + EFA_REGS_DEV_CTL_OFF);
1066 /* reset clears the mmio readless address, restore it */
1067 efa_com_mmio_reg_read_resp_addr_init(edev);
1069 err = wait_for_reset_state(edev, timeout, 1);
1071 ibdev_err(edev->efa_dev, "Reset indication didn't turn on\n");
1076 writel(0, edev->reg_bar + EFA_REGS_DEV_CTL_OFF);
1077 err = wait_for_reset_state(edev, timeout, 0);
1079 ibdev_err(edev->efa_dev, "Reset indication didn't turn off\n");
1083 timeout = EFA_GET(&cap, EFA_REGS_CAPS_ADMIN_CMD_TO);
1085 /* the resolution of timeout reg is 100ms */
1086 edev->aq.completion_timeout = timeout * 100000;
1088 edev->aq.completion_timeout = ADMIN_CMD_TIMEOUT_US;
1093 static int efa_com_create_eq(struct efa_com_dev *edev,
1094 struct efa_com_create_eq_params *params,
1095 struct efa_com_create_eq_result *result)
1097 struct efa_com_admin_queue *aq = &edev->aq;
1098 struct efa_admin_create_eq_resp resp = {};
1099 struct efa_admin_create_eq_cmd cmd = {};
1102 cmd.aq_common_descriptor.opcode = EFA_ADMIN_CREATE_EQ;
1103 EFA_SET(&cmd.caps, EFA_ADMIN_CREATE_EQ_CMD_ENTRY_SIZE_WORDS,
1104 params->entry_size_in_bytes / 4);
1105 cmd.depth = params->depth;
1106 cmd.event_bitmask = params->event_bitmask;
1107 cmd.msix_vec = params->msix_vec;
1109 efa_com_set_dma_addr(params->dma_addr, &cmd.ba.mem_addr_high,
1110 &cmd.ba.mem_addr_low);
1112 err = efa_com_cmd_exec(aq,
1113 (struct efa_admin_aq_entry *)&cmd,
1115 (struct efa_admin_acq_entry *)&resp,
1118 ibdev_err_ratelimited(edev->efa_dev,
1119 "Failed to create eq[%d]\n", err);
1123 result->eqn = resp.eqn;
1128 static void efa_com_destroy_eq(struct efa_com_dev *edev,
1129 struct efa_com_destroy_eq_params *params)
1131 struct efa_com_admin_queue *aq = &edev->aq;
1132 struct efa_admin_destroy_eq_resp resp = {};
1133 struct efa_admin_destroy_eq_cmd cmd = {};
1136 cmd.aq_common_descriptor.opcode = EFA_ADMIN_DESTROY_EQ;
1137 cmd.eqn = params->eqn;
1139 err = efa_com_cmd_exec(aq,
1140 (struct efa_admin_aq_entry *)&cmd,
1142 (struct efa_admin_acq_entry *)&resp,
1145 ibdev_err_ratelimited(edev->efa_dev,
1146 "Failed to destroy EQ-%u [%d]\n", cmd.eqn,
1150 static void efa_com_arm_eq(struct efa_com_dev *edev, struct efa_com_eq *eeq)
1154 EFA_SET(&val, EFA_REGS_EQ_DB_EQN, eeq->eqn);
1155 EFA_SET(&val, EFA_REGS_EQ_DB_ARM, 1);
1157 writel(val, edev->reg_bar + EFA_REGS_EQ_DB_OFF);
1160 void efa_com_eq_comp_intr_handler(struct efa_com_dev *edev,
1161 struct efa_com_eq *eeq)
1163 struct efa_admin_eqe *eqe;
1168 ci = eeq->cc & (eeq->depth - 1);
1170 eqe = &eeq->eqes[ci];
1172 /* Go over all the events */
1173 while ((READ_ONCE(eqe->common) & EFA_ADMIN_EQE_PHASE_MASK) == phase) {
1175 * Do not read the rest of the completion entry before the
1176 * phase bit was validated
1182 /* Get next event entry */
1186 if (ci == eeq->depth) {
1191 eqe = &eeq->eqes[ci];
1194 eeq->cc += processed;
1196 efa_com_arm_eq(eeq->edev, eeq);
1199 void efa_com_eq_destroy(struct efa_com_dev *edev, struct efa_com_eq *eeq)
1201 struct efa_com_destroy_eq_params params = {
1205 efa_com_destroy_eq(edev, ¶ms);
1206 dma_free_coherent(edev->dmadev, eeq->depth * sizeof(*eeq->eqes),
1207 eeq->eqes, eeq->dma_addr);
1210 int efa_com_eq_init(struct efa_com_dev *edev, struct efa_com_eq *eeq,
1211 efa_eqe_handler cb, u16 depth, u8 msix_vec)
1213 struct efa_com_create_eq_params params = {};
1214 struct efa_com_create_eq_result result = {};
1217 params.depth = depth;
1218 params.entry_size_in_bytes = sizeof(*eeq->eqes);
1219 EFA_SET(¶ms.event_bitmask,
1220 EFA_ADMIN_CREATE_EQ_CMD_COMPLETION_EVENTS, 1);
1221 params.msix_vec = msix_vec;
1223 eeq->eqes = dma_alloc_coherent(edev->dmadev,
1224 params.depth * sizeof(*eeq->eqes),
1225 ¶ms.dma_addr, GFP_KERNEL);
1229 err = efa_com_create_eq(edev, ¶ms, &result);
1231 goto err_free_coherent;
1233 eeq->eqn = result.eqn;
1235 eeq->dma_addr = params.dma_addr;
1237 eeq->depth = params.depth;
1239 efa_com_arm_eq(edev, eeq);
1244 dma_free_coherent(edev->dmadev, params.depth * sizeof(*eeq->eqes),
1245 eeq->eqes, params.dma_addr);