2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/interrupt.h>
34 #include <linux/notifier.h>
35 #include <linux/module.h>
36 #include <linux/mlx5/driver.h>
37 #include <linux/mlx5/vport.h>
38 #include <linux/mlx5/eq.h>
39 #ifdef CONFIG_RFS_ACCEL
40 #include <linux/cpu_rmap.h>
42 #include "mlx5_core.h"
44 #include "fpga/core.h"
46 #include "lib/clock.h"
47 #include "diag/fw_tracer.h"
50 MLX5_EQE_OWNER_INIT_VAL = 0x1,
54 MLX5_EQ_STATE_ARMED = 0x9,
55 MLX5_EQ_STATE_FIRED = 0xa,
56 MLX5_EQ_STATE_ALWAYS_ARMED = 0xb,
60 MLX5_EQ_DOORBEL_OFFSET = 0x40,
63 /* budget must be smaller than MLX5_NUM_SPARE_EQE to guarantee that we update
64 * the ci before we polled all the entries in the EQ. MLX5_NUM_SPARE_EQE is
65 * used to set the EQ size, budget must be smaller than the EQ size.
68 MLX5_EQ_POLLING_BUDGET = 128,
71 static_assert(MLX5_EQ_POLLING_BUDGET <= MLX5_NUM_SPARE_EQE);
73 struct mlx5_eq_table {
74 struct list_head comp_eqs_list;
75 struct mlx5_eq_async pages_eq;
76 struct mlx5_eq_async cmd_eq;
77 struct mlx5_eq_async async_eq;
79 struct atomic_notifier_head nh[MLX5_EVENT_TYPE_MAX];
81 /* Since CQ DB is stored in async_eq */
82 struct mlx5_nb cq_err_nb;
84 struct mutex lock; /* sync async eqs creations */
86 struct mlx5_irq_table *irq_table;
89 #define MLX5_ASYNC_EVENT_MASK ((1ull << MLX5_EVENT_TYPE_PATH_MIG) | \
90 (1ull << MLX5_EVENT_TYPE_COMM_EST) | \
91 (1ull << MLX5_EVENT_TYPE_SQ_DRAINED) | \
92 (1ull << MLX5_EVENT_TYPE_CQ_ERROR) | \
93 (1ull << MLX5_EVENT_TYPE_WQ_CATAS_ERROR) | \
94 (1ull << MLX5_EVENT_TYPE_PATH_MIG_FAILED) | \
95 (1ull << MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR) | \
96 (1ull << MLX5_EVENT_TYPE_WQ_ACCESS_ERROR) | \
97 (1ull << MLX5_EVENT_TYPE_PORT_CHANGE) | \
98 (1ull << MLX5_EVENT_TYPE_SRQ_CATAS_ERROR) | \
99 (1ull << MLX5_EVENT_TYPE_SRQ_LAST_WQE) | \
100 (1ull << MLX5_EVENT_TYPE_SRQ_RQ_LIMIT))
102 static int mlx5_cmd_destroy_eq(struct mlx5_core_dev *dev, u8 eqn)
104 u32 in[MLX5_ST_SZ_DW(destroy_eq_in)] = {};
106 MLX5_SET(destroy_eq_in, in, opcode, MLX5_CMD_OP_DESTROY_EQ);
107 MLX5_SET(destroy_eq_in, in, eq_number, eqn);
108 return mlx5_cmd_exec_in(dev, destroy_eq, in);
111 /* caller must eventually call mlx5_cq_put on the returned cq */
112 static struct mlx5_core_cq *mlx5_eq_cq_get(struct mlx5_eq *eq, u32 cqn)
114 struct mlx5_cq_table *table = &eq->cq_table;
115 struct mlx5_core_cq *cq = NULL;
118 cq = radix_tree_lookup(&table->tree, cqn);
126 static int mlx5_eq_comp_int(struct notifier_block *nb,
127 __always_unused unsigned long action,
128 __always_unused void *data)
130 struct mlx5_eq_comp *eq_comp =
131 container_of(nb, struct mlx5_eq_comp, irq_nb);
132 struct mlx5_eq *eq = &eq_comp->core;
133 struct mlx5_eqe *eqe;
137 eqe = next_eqe_sw(eq);
142 struct mlx5_core_cq *cq;
144 /* Make sure we read EQ entry contents after we've
145 * checked the ownership bit.
148 /* Assume (eqe->type) is always MLX5_EVENT_TYPE_COMP */
149 cqn = be32_to_cpu(eqe->data.comp.cqn) & 0xffffff;
151 cq = mlx5_eq_cq_get(eq, cqn);
157 dev_dbg_ratelimited(eq->dev->device,
158 "Completion event for bogus CQ 0x%x\n", cqn);
163 } while ((++num_eqes < MLX5_EQ_POLLING_BUDGET) && (eqe = next_eqe_sw(eq)));
169 tasklet_schedule(&eq_comp->tasklet_ctx.task);
174 /* Some architectures don't latch interrupts when they are disabled, so using
175 * mlx5_eq_poll_irq_disabled could end up losing interrupts while trying to
176 * avoid losing them. It is not recommended to use it, unless this is the last
179 u32 mlx5_eq_poll_irq_disabled(struct mlx5_eq_comp *eq)
183 disable_irq(eq->core.irqn);
184 count_eqe = eq->core.cons_index;
185 mlx5_eq_comp_int(&eq->irq_nb, 0, NULL);
186 count_eqe = eq->core.cons_index - count_eqe;
187 enable_irq(eq->core.irqn);
192 static void mlx5_eq_async_int_lock(struct mlx5_eq_async *eq, bool recovery,
193 unsigned long *flags)
194 __acquires(&eq->lock)
197 spin_lock(&eq->lock);
199 spin_lock_irqsave(&eq->lock, *flags);
202 static void mlx5_eq_async_int_unlock(struct mlx5_eq_async *eq, bool recovery,
203 unsigned long *flags)
204 __releases(&eq->lock)
207 spin_unlock(&eq->lock);
209 spin_unlock_irqrestore(&eq->lock, *flags);
212 enum async_eq_nb_action {
213 ASYNC_EQ_IRQ_HANDLER = 0,
214 ASYNC_EQ_RECOVER = 1,
217 static int mlx5_eq_async_int(struct notifier_block *nb,
218 unsigned long action, void *data)
220 struct mlx5_eq_async *eq_async =
221 container_of(nb, struct mlx5_eq_async, irq_nb);
222 struct mlx5_eq *eq = &eq_async->core;
223 struct mlx5_eq_table *eqt;
224 struct mlx5_core_dev *dev;
225 struct mlx5_eqe *eqe;
231 eqt = dev->priv.eq_table;
233 recovery = action == ASYNC_EQ_RECOVER;
234 mlx5_eq_async_int_lock(eq_async, recovery, &flags);
236 eqe = next_eqe_sw(eq);
242 * Make sure we read EQ entry contents after we've
243 * checked the ownership bit.
247 atomic_notifier_call_chain(&eqt->nh[eqe->type], eqe->type, eqe);
248 atomic_notifier_call_chain(&eqt->nh[MLX5_EVENT_TYPE_NOTIFY_ANY], eqe->type, eqe);
252 } while ((++num_eqes < MLX5_EQ_POLLING_BUDGET) && (eqe = next_eqe_sw(eq)));
256 mlx5_eq_async_int_unlock(eq_async, recovery, &flags);
258 return unlikely(recovery) ? num_eqes : 0;
261 void mlx5_cmd_eq_recover(struct mlx5_core_dev *dev)
263 struct mlx5_eq_async *eq = &dev->priv.eq_table->cmd_eq;
266 eqes = mlx5_eq_async_int(&eq->irq_nb, ASYNC_EQ_RECOVER, NULL);
268 mlx5_core_warn(dev, "Recovered %d EQEs on cmd_eq\n", eqes);
271 static void init_eq_buf(struct mlx5_eq *eq)
273 struct mlx5_eqe *eqe;
276 for (i = 0; i < eq_get_size(eq); i++) {
277 eqe = get_eqe(eq, i);
278 eqe->owner = MLX5_EQE_OWNER_INIT_VAL;
283 create_map_eq(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
284 struct mlx5_eq_param *param)
286 u8 log_eq_size = order_base_2(param->nent + MLX5_NUM_SPARE_EQE);
287 struct mlx5_cq_table *cq_table = &eq->cq_table;
288 u32 out[MLX5_ST_SZ_DW(create_eq_out)] = {0};
289 u8 log_eq_stride = ilog2(MLX5_EQE_SIZE);
290 struct mlx5_priv *priv = &dev->priv;
291 u8 vecidx = param->irq_index;
300 memset(cq_table, 0, sizeof(*cq_table));
301 spin_lock_init(&cq_table->lock);
302 INIT_RADIX_TREE(&cq_table->tree, GFP_ATOMIC);
306 err = mlx5_frag_buf_alloc_node(dev, wq_get_byte_sz(log_eq_size, log_eq_stride),
307 &eq->frag_buf, dev->priv.numa_node);
311 mlx5_init_fbc(eq->frag_buf.frags, log_eq_stride, log_eq_size, &eq->fbc);
314 inlen = MLX5_ST_SZ_BYTES(create_eq_in) +
315 MLX5_FLD_SZ_BYTES(create_eq_in, pas[0]) * eq->frag_buf.npages;
317 in = kvzalloc(inlen, GFP_KERNEL);
323 pas = (__be64 *)MLX5_ADDR_OF(create_eq_in, in, pas);
324 mlx5_fill_page_frag_array(&eq->frag_buf, pas);
326 MLX5_SET(create_eq_in, in, opcode, MLX5_CMD_OP_CREATE_EQ);
327 if (!param->mask[0] && MLX5_CAP_GEN(dev, log_max_uctx))
328 MLX5_SET(create_eq_in, in, uid, MLX5_SHARED_RESOURCE_UID);
330 for (i = 0; i < 4; i++)
331 MLX5_ARRAY_SET64(create_eq_in, in, event_bitmask, i,
334 eqc = MLX5_ADDR_OF(create_eq_in, in, eq_context_entry);
335 MLX5_SET(eqc, eqc, log_eq_size, eq->fbc.log_sz);
336 MLX5_SET(eqc, eqc, uar_page, priv->uar->index);
337 MLX5_SET(eqc, eqc, intr, vecidx);
338 MLX5_SET(eqc, eqc, log_page_size,
339 eq->frag_buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
341 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
346 eq->eqn = MLX5_GET(create_eq_out, out, eq_number);
347 eq->irqn = pci_irq_vector(dev->pdev, vecidx);
349 eq->doorbell = priv->uar->map + MLX5_EQ_DOORBEL_OFFSET;
351 err = mlx5_debug_eq_add(dev, eq);
359 mlx5_cmd_destroy_eq(dev, eq->eqn);
365 mlx5_frag_buf_free(dev, &eq->frag_buf);
370 * mlx5_eq_enable - Enable EQ for receiving EQEs
371 * @dev : Device which owns the eq
373 * @nb : Notifier call block
375 * Must be called after EQ is created in device.
377 * @return: 0 if no error
379 int mlx5_eq_enable(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
380 struct notifier_block *nb)
382 struct mlx5_eq_table *eq_table = dev->priv.eq_table;
385 err = mlx5_irq_attach_nb(eq_table->irq_table, eq->vecidx, nb);
391 EXPORT_SYMBOL(mlx5_eq_enable);
394 * mlx5_eq_disable - Disable EQ for receiving EQEs
395 * @dev : Device which owns the eq
396 * @eq : EQ to disable
397 * @nb : Notifier call block
399 * Must be called before EQ is destroyed.
401 void mlx5_eq_disable(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
402 struct notifier_block *nb)
404 struct mlx5_eq_table *eq_table = dev->priv.eq_table;
406 mlx5_irq_detach_nb(eq_table->irq_table, eq->vecidx, nb);
408 EXPORT_SYMBOL(mlx5_eq_disable);
410 static int destroy_unmap_eq(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
414 mlx5_debug_eq_remove(dev, eq);
416 err = mlx5_cmd_destroy_eq(dev, eq->eqn);
418 mlx5_core_warn(dev, "failed to destroy a previously created eq: eqn %d\n",
420 synchronize_irq(eq->irqn);
422 mlx5_frag_buf_free(dev, &eq->frag_buf);
427 int mlx5_eq_add_cq(struct mlx5_eq *eq, struct mlx5_core_cq *cq)
429 struct mlx5_cq_table *table = &eq->cq_table;
432 spin_lock(&table->lock);
433 err = radix_tree_insert(&table->tree, cq->cqn, cq);
434 spin_unlock(&table->lock);
439 void mlx5_eq_del_cq(struct mlx5_eq *eq, struct mlx5_core_cq *cq)
441 struct mlx5_cq_table *table = &eq->cq_table;
442 struct mlx5_core_cq *tmp;
444 spin_lock(&table->lock);
445 tmp = radix_tree_delete(&table->tree, cq->cqn);
446 spin_unlock(&table->lock);
449 mlx5_core_dbg(eq->dev, "cq 0x%x not found in eq 0x%x tree\n",
455 mlx5_core_dbg(eq->dev, "corruption on cqn 0x%x in eq 0x%x\n",
459 int mlx5_eq_table_init(struct mlx5_core_dev *dev)
461 struct mlx5_eq_table *eq_table;
464 eq_table = kvzalloc(sizeof(*eq_table), GFP_KERNEL);
468 dev->priv.eq_table = eq_table;
470 mlx5_eq_debugfs_init(dev);
472 mutex_init(&eq_table->lock);
473 for (i = 0; i < MLX5_EVENT_TYPE_MAX; i++)
474 ATOMIC_INIT_NOTIFIER_HEAD(&eq_table->nh[i]);
476 eq_table->irq_table = mlx5_irq_table_get(dev);
480 void mlx5_eq_table_cleanup(struct mlx5_core_dev *dev)
482 mlx5_eq_debugfs_cleanup(dev);
483 kvfree(dev->priv.eq_table);
488 static int create_async_eq(struct mlx5_core_dev *dev,
489 struct mlx5_eq *eq, struct mlx5_eq_param *param)
491 struct mlx5_eq_table *eq_table = dev->priv.eq_table;
494 mutex_lock(&eq_table->lock);
495 /* Async EQs must share irq index 0 */
496 if (param->irq_index != 0) {
501 err = create_map_eq(dev, eq, param);
503 mutex_unlock(&eq_table->lock);
507 static int destroy_async_eq(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
509 struct mlx5_eq_table *eq_table = dev->priv.eq_table;
512 mutex_lock(&eq_table->lock);
513 err = destroy_unmap_eq(dev, eq);
514 mutex_unlock(&eq_table->lock);
518 static int cq_err_event_notifier(struct notifier_block *nb,
519 unsigned long type, void *data)
521 struct mlx5_eq_table *eqt;
522 struct mlx5_core_cq *cq;
523 struct mlx5_eqe *eqe;
527 /* type == MLX5_EVENT_TYPE_CQ_ERROR */
529 eqt = mlx5_nb_cof(nb, struct mlx5_eq_table, cq_err_nb);
530 eq = &eqt->async_eq.core;
533 cqn = be32_to_cpu(eqe->data.cq_err.cqn) & 0xffffff;
534 mlx5_core_warn(eq->dev, "CQ error on CQN 0x%x, syndrome 0x%x\n",
535 cqn, eqe->data.cq_err.syndrome);
537 cq = mlx5_eq_cq_get(eq, cqn);
539 mlx5_core_warn(eq->dev, "Async event for bogus CQ 0x%x\n", cqn);
551 static void gather_user_async_events(struct mlx5_core_dev *dev, u64 mask[4])
553 __be64 *user_unaffiliated_events;
554 __be64 *user_affiliated_events;
557 user_affiliated_events =
558 MLX5_CAP_DEV_EVENT(dev, user_affiliated_events);
559 user_unaffiliated_events =
560 MLX5_CAP_DEV_EVENT(dev, user_unaffiliated_events);
562 for (i = 0; i < 4; i++)
563 mask[i] |= be64_to_cpu(user_affiliated_events[i] |
564 user_unaffiliated_events[i]);
567 static void gather_async_events_mask(struct mlx5_core_dev *dev, u64 mask[4])
569 u64 async_event_mask = MLX5_ASYNC_EVENT_MASK;
571 if (MLX5_VPORT_MANAGER(dev))
572 async_event_mask |= (1ull << MLX5_EVENT_TYPE_NIC_VPORT_CHANGE);
574 if (MLX5_CAP_GEN(dev, general_notification_event))
575 async_event_mask |= (1ull << MLX5_EVENT_TYPE_GENERAL_EVENT);
577 if (MLX5_CAP_GEN(dev, port_module_event))
578 async_event_mask |= (1ull << MLX5_EVENT_TYPE_PORT_MODULE_EVENT);
580 mlx5_core_dbg(dev, "port_module_event is not set\n");
582 if (MLX5_PPS_CAP(dev))
583 async_event_mask |= (1ull << MLX5_EVENT_TYPE_PPS_EVENT);
585 if (MLX5_CAP_GEN(dev, fpga))
586 async_event_mask |= (1ull << MLX5_EVENT_TYPE_FPGA_ERROR) |
587 (1ull << MLX5_EVENT_TYPE_FPGA_QP_ERROR);
588 if (MLX5_CAP_GEN_MAX(dev, dct))
589 async_event_mask |= (1ull << MLX5_EVENT_TYPE_DCT_DRAINED);
591 if (MLX5_CAP_GEN(dev, temp_warn_event))
592 async_event_mask |= (1ull << MLX5_EVENT_TYPE_TEMP_WARN_EVENT);
594 if (MLX5_CAP_MCAM_REG(dev, tracer_registers))
595 async_event_mask |= (1ull << MLX5_EVENT_TYPE_DEVICE_TRACER);
597 if (MLX5_CAP_GEN(dev, max_num_of_monitor_counters))
598 async_event_mask |= (1ull << MLX5_EVENT_TYPE_MONITOR_COUNTER);
600 if (mlx5_eswitch_is_funcs_handler(dev))
602 (1ull << MLX5_EVENT_TYPE_ESW_FUNCTIONS_CHANGED);
604 if (MLX5_CAP_GEN_MAX(dev, vhca_state))
605 async_event_mask |= (1ull << MLX5_EVENT_TYPE_VHCA_STATE_CHANGE);
607 mask[0] = async_event_mask;
609 if (MLX5_CAP_GEN(dev, event_cap))
610 gather_user_async_events(dev, mask);
614 setup_async_eq(struct mlx5_core_dev *dev, struct mlx5_eq_async *eq,
615 struct mlx5_eq_param *param, const char *name)
619 eq->irq_nb.notifier_call = mlx5_eq_async_int;
620 spin_lock_init(&eq->lock);
622 err = create_async_eq(dev, &eq->core, param);
624 mlx5_core_warn(dev, "failed to create %s EQ %d\n", name, err);
627 err = mlx5_eq_enable(dev, &eq->core, &eq->irq_nb);
629 mlx5_core_warn(dev, "failed to enable %s EQ %d\n", name, err);
630 destroy_async_eq(dev, &eq->core);
635 static void cleanup_async_eq(struct mlx5_core_dev *dev,
636 struct mlx5_eq_async *eq, const char *name)
640 mlx5_eq_disable(dev, &eq->core, &eq->irq_nb);
641 err = destroy_async_eq(dev, &eq->core);
643 mlx5_core_err(dev, "failed to destroy %s eq, err(%d)\n",
647 static int create_async_eqs(struct mlx5_core_dev *dev)
649 struct mlx5_eq_table *table = dev->priv.eq_table;
650 struct mlx5_eq_param param = {};
653 MLX5_NB_INIT(&table->cq_err_nb, cq_err_event_notifier, CQ_ERROR);
654 mlx5_eq_notifier_register(dev, &table->cq_err_nb);
656 param = (struct mlx5_eq_param) {
658 .nent = MLX5_NUM_CMD_EQE,
659 .mask[0] = 1ull << MLX5_EVENT_TYPE_CMD,
661 mlx5_cmd_allowed_opcode(dev, MLX5_CMD_OP_CREATE_EQ);
662 err = setup_async_eq(dev, &table->cmd_eq, ¶m, "cmd");
666 mlx5_cmd_use_events(dev);
667 mlx5_cmd_allowed_opcode(dev, CMD_ALLOWED_OPCODE_ALL);
669 param = (struct mlx5_eq_param) {
671 .nent = MLX5_NUM_ASYNC_EQE,
674 gather_async_events_mask(dev, param.mask);
675 err = setup_async_eq(dev, &table->async_eq, ¶m, "async");
679 param = (struct mlx5_eq_param) {
681 .nent = /* TODO: sriov max_vf + */ 1,
682 .mask[0] = 1ull << MLX5_EVENT_TYPE_PAGE_REQUEST,
685 err = setup_async_eq(dev, &table->pages_eq, ¶m, "pages");
692 cleanup_async_eq(dev, &table->async_eq, "async");
694 mlx5_cmd_use_polling(dev);
695 cleanup_async_eq(dev, &table->cmd_eq, "cmd");
697 mlx5_cmd_allowed_opcode(dev, CMD_ALLOWED_OPCODE_ALL);
698 mlx5_eq_notifier_unregister(dev, &table->cq_err_nb);
702 static void destroy_async_eqs(struct mlx5_core_dev *dev)
704 struct mlx5_eq_table *table = dev->priv.eq_table;
706 cleanup_async_eq(dev, &table->pages_eq, "pages");
707 cleanup_async_eq(dev, &table->async_eq, "async");
708 mlx5_cmd_allowed_opcode(dev, MLX5_CMD_OP_DESTROY_EQ);
709 mlx5_cmd_use_polling(dev);
710 cleanup_async_eq(dev, &table->cmd_eq, "cmd");
711 mlx5_cmd_allowed_opcode(dev, CMD_ALLOWED_OPCODE_ALL);
712 mlx5_eq_notifier_unregister(dev, &table->cq_err_nb);
715 struct mlx5_eq *mlx5_get_async_eq(struct mlx5_core_dev *dev)
717 return &dev->priv.eq_table->async_eq.core;
720 void mlx5_eq_synchronize_async_irq(struct mlx5_core_dev *dev)
722 synchronize_irq(dev->priv.eq_table->async_eq.core.irqn);
725 void mlx5_eq_synchronize_cmd_irq(struct mlx5_core_dev *dev)
727 synchronize_irq(dev->priv.eq_table->cmd_eq.core.irqn);
730 /* Generic EQ API for mlx5_core consumers
731 * Needed For RDMA ODP EQ for now
734 mlx5_eq_create_generic(struct mlx5_core_dev *dev,
735 struct mlx5_eq_param *param)
737 struct mlx5_eq *eq = kvzalloc(sizeof(*eq), GFP_KERNEL);
741 return ERR_PTR(-ENOMEM);
743 err = create_async_eq(dev, eq, param);
751 EXPORT_SYMBOL(mlx5_eq_create_generic);
753 int mlx5_eq_destroy_generic(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
760 err = destroy_async_eq(dev, eq);
768 EXPORT_SYMBOL(mlx5_eq_destroy_generic);
770 struct mlx5_eqe *mlx5_eq_get_eqe(struct mlx5_eq *eq, u32 cc)
772 u32 ci = eq->cons_index + cc;
773 u32 nent = eq_get_size(eq);
774 struct mlx5_eqe *eqe;
776 eqe = get_eqe(eq, ci & (nent - 1));
777 eqe = ((eqe->owner & 1) ^ !!(ci & nent)) ? NULL : eqe;
778 /* Make sure we read EQ entry contents after we've
779 * checked the ownership bit.
786 EXPORT_SYMBOL(mlx5_eq_get_eqe);
788 void mlx5_eq_update_ci(struct mlx5_eq *eq, u32 cc, bool arm)
790 __be32 __iomem *addr = eq->doorbell + (arm ? 0 : 2);
793 eq->cons_index += cc;
794 val = (eq->cons_index & 0xffffff) | (eq->eqn << 24);
796 __raw_writel((__force u32)cpu_to_be32(val), addr);
797 /* We still want ordering, just not swabbing, so add a barrier */
800 EXPORT_SYMBOL(mlx5_eq_update_ci);
802 static void destroy_comp_eqs(struct mlx5_core_dev *dev)
804 struct mlx5_eq_table *table = dev->priv.eq_table;
805 struct mlx5_eq_comp *eq, *n;
807 list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
809 mlx5_eq_disable(dev, &eq->core, &eq->irq_nb);
810 if (destroy_unmap_eq(dev, &eq->core))
811 mlx5_core_warn(dev, "failed to destroy comp EQ 0x%x\n",
813 tasklet_disable(&eq->tasklet_ctx.task);
818 static int create_comp_eqs(struct mlx5_core_dev *dev)
820 struct mlx5_eq_table *table = dev->priv.eq_table;
821 struct mlx5_eq_comp *eq;
827 INIT_LIST_HEAD(&table->comp_eqs_list);
828 ncomp_eqs = table->num_comp_eqs;
829 nent = MLX5_COMP_EQ_SIZE;
830 for (i = 0; i < ncomp_eqs; i++) {
831 int vecidx = i + MLX5_IRQ_VEC_COMP_BASE;
832 struct mlx5_eq_param param = {};
834 eq = kzalloc(sizeof(*eq), GFP_KERNEL);
840 INIT_LIST_HEAD(&eq->tasklet_ctx.list);
841 INIT_LIST_HEAD(&eq->tasklet_ctx.process_list);
842 spin_lock_init(&eq->tasklet_ctx.lock);
843 tasklet_setup(&eq->tasklet_ctx.task, mlx5_cq_tasklet_cb);
845 eq->irq_nb.notifier_call = mlx5_eq_comp_int;
846 param = (struct mlx5_eq_param) {
850 err = create_map_eq(dev, &eq->core, ¶m);
855 err = mlx5_eq_enable(dev, &eq->core, &eq->irq_nb);
857 destroy_unmap_eq(dev, &eq->core);
862 mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->core.eqn);
863 /* add tail, to keep the list ordered, for mlx5_vector2eqn to work */
864 list_add_tail(&eq->list, &table->comp_eqs_list);
870 destroy_comp_eqs(dev);
874 int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn,
877 struct mlx5_eq_table *table = dev->priv.eq_table;
878 struct mlx5_eq_comp *eq, *n;
882 list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
885 *irqn = eq->core.irqn;
893 EXPORT_SYMBOL(mlx5_vector2eqn);
895 unsigned int mlx5_comp_vectors_count(struct mlx5_core_dev *dev)
897 return dev->priv.eq_table->num_comp_eqs;
899 EXPORT_SYMBOL(mlx5_comp_vectors_count);
902 mlx5_comp_irq_get_affinity_mask(struct mlx5_core_dev *dev, int vector)
904 int vecidx = vector + MLX5_IRQ_VEC_COMP_BASE;
906 return mlx5_irq_get_affinity_mask(dev->priv.eq_table->irq_table,
909 EXPORT_SYMBOL(mlx5_comp_irq_get_affinity_mask);
911 #ifdef CONFIG_RFS_ACCEL
912 struct cpu_rmap *mlx5_eq_table_get_rmap(struct mlx5_core_dev *dev)
914 return mlx5_irq_get_rmap(dev->priv.eq_table->irq_table);
918 struct mlx5_eq_comp *mlx5_eqn2comp_eq(struct mlx5_core_dev *dev, int eqn)
920 struct mlx5_eq_table *table = dev->priv.eq_table;
921 struct mlx5_eq_comp *eq;
923 list_for_each_entry(eq, &table->comp_eqs_list, list) {
924 if (eq->core.eqn == eqn)
928 return ERR_PTR(-ENOENT);
931 /* This function should only be called after mlx5_cmd_force_teardown_hca */
932 void mlx5_core_eq_free_irqs(struct mlx5_core_dev *dev)
934 struct mlx5_eq_table *table = dev->priv.eq_table;
936 mutex_lock(&table->lock); /* sync with create/destroy_async_eq */
937 mlx5_irq_table_destroy(dev);
938 mutex_unlock(&table->lock);
941 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
942 #define MLX5_MAX_ASYNC_EQS 4
944 #define MLX5_MAX_ASYNC_EQS 3
947 int mlx5_eq_table_create(struct mlx5_core_dev *dev)
949 struct mlx5_eq_table *eq_table = dev->priv.eq_table;
950 int num_eqs = MLX5_CAP_GEN(dev, max_num_eqs) ?
951 MLX5_CAP_GEN(dev, max_num_eqs) :
952 1 << MLX5_CAP_GEN(dev, log_max_eq);
955 eq_table->num_comp_eqs =
957 mlx5_irq_get_num_comp(eq_table->irq_table),
958 num_eqs - MLX5_MAX_ASYNC_EQS);
960 err = create_async_eqs(dev);
962 mlx5_core_err(dev, "Failed to create async EQs\n");
966 err = create_comp_eqs(dev);
968 mlx5_core_err(dev, "Failed to create completion EQs\n");
974 destroy_async_eqs(dev);
979 void mlx5_eq_table_destroy(struct mlx5_core_dev *dev)
981 destroy_comp_eqs(dev);
982 destroy_async_eqs(dev);
985 int mlx5_eq_notifier_register(struct mlx5_core_dev *dev, struct mlx5_nb *nb)
987 struct mlx5_eq_table *eqt = dev->priv.eq_table;
989 return atomic_notifier_chain_register(&eqt->nh[nb->event_type], &nb->nb);
991 EXPORT_SYMBOL(mlx5_eq_notifier_register);
993 int mlx5_eq_notifier_unregister(struct mlx5_core_dev *dev, struct mlx5_nb *nb)
995 struct mlx5_eq_table *eqt = dev->priv.eq_table;
997 return atomic_notifier_chain_unregister(&eqt->nh[nb->event_type], &nb->nb);
999 EXPORT_SYMBOL(mlx5_eq_notifier_unregister);