Merge branch 'md-next' of https://git.kernel.org/pub/scm/linux/kernel/git/song/md...
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / eq.c
1 /*
2  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  */
32
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>
41 #endif
42 #include "mlx5_core.h"
43 #include "lib/eq.h"
44 #include "fpga/core.h"
45 #include "eswitch.h"
46 #include "lib/clock.h"
47 #include "diag/fw_tracer.h"
48
49 enum {
50         MLX5_EQE_OWNER_INIT_VAL = 0x1,
51 };
52
53 enum {
54         MLX5_EQ_STATE_ARMED             = 0x9,
55         MLX5_EQ_STATE_FIRED             = 0xa,
56         MLX5_EQ_STATE_ALWAYS_ARMED      = 0xb,
57 };
58
59 enum {
60         MLX5_EQ_DOORBEL_OFFSET  = 0x40,
61 };
62
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.
66  */
67 enum {
68         MLX5_EQ_POLLING_BUDGET  = 128,
69 };
70
71 static_assert(MLX5_EQ_POLLING_BUDGET <= MLX5_NUM_SPARE_EQE);
72
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;
78
79         struct atomic_notifier_head nh[MLX5_EVENT_TYPE_MAX];
80
81         /* Since CQ DB is stored in async_eq */
82         struct mlx5_nb          cq_err_nb;
83
84         struct mutex            lock; /* sync async eqs creations */
85         int                     num_comp_eqs;
86         struct mlx5_irq_table   *irq_table;
87 };
88
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))
101
102 static int mlx5_cmd_destroy_eq(struct mlx5_core_dev *dev, u8 eqn)
103 {
104         u32 in[MLX5_ST_SZ_DW(destroy_eq_in)] = {};
105
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);
109 }
110
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)
113 {
114         struct mlx5_cq_table *table = &eq->cq_table;
115         struct mlx5_core_cq *cq = NULL;
116
117         rcu_read_lock();
118         cq = radix_tree_lookup(&table->tree, cqn);
119         if (likely(cq))
120                 mlx5_cq_hold(cq);
121         rcu_read_unlock();
122
123         return cq;
124 }
125
126 static int mlx5_eq_comp_int(struct notifier_block *nb,
127                             __always_unused unsigned long action,
128                             __always_unused void *data)
129 {
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;
134         int num_eqes = 0;
135         u32 cqn = -1;
136
137         eqe = next_eqe_sw(eq);
138         if (!eqe)
139                 goto out;
140
141         do {
142                 struct mlx5_core_cq *cq;
143
144                 /* Make sure we read EQ entry contents after we've
145                  * checked the ownership bit.
146                  */
147                 dma_rmb();
148                 /* Assume (eqe->type) is always MLX5_EVENT_TYPE_COMP */
149                 cqn = be32_to_cpu(eqe->data.comp.cqn) & 0xffffff;
150
151                 cq = mlx5_eq_cq_get(eq, cqn);
152                 if (likely(cq)) {
153                         ++cq->arm_sn;
154                         cq->comp(cq, eqe);
155                         mlx5_cq_put(cq);
156                 } else {
157                         dev_dbg_ratelimited(eq->dev->device,
158                                             "Completion event for bogus CQ 0x%x\n", cqn);
159                 }
160
161                 ++eq->cons_index;
162
163         } while ((++num_eqes < MLX5_EQ_POLLING_BUDGET) && (eqe = next_eqe_sw(eq)));
164
165 out:
166         eq_update_ci(eq, 1);
167
168         if (cqn != -1)
169                 tasklet_schedule(&eq_comp->tasklet_ctx.task);
170
171         return 0;
172 }
173
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
177  * resort.
178  */
179 u32 mlx5_eq_poll_irq_disabled(struct mlx5_eq_comp *eq)
180 {
181         u32 count_eqe;
182
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);
188
189         return count_eqe;
190 }
191
192 static int mlx5_eq_async_int(struct notifier_block *nb,
193                              unsigned long action, void *data)
194 {
195         struct mlx5_eq_async *eq_async =
196                 container_of(nb, struct mlx5_eq_async, irq_nb);
197         struct mlx5_eq *eq = &eq_async->core;
198         struct mlx5_eq_table *eqt;
199         struct mlx5_core_dev *dev;
200         struct mlx5_eqe *eqe;
201         int num_eqes = 0;
202
203         dev = eq->dev;
204         eqt = dev->priv.eq_table;
205
206         eqe = next_eqe_sw(eq);
207         if (!eqe)
208                 goto out;
209
210         do {
211                 /*
212                  * Make sure we read EQ entry contents after we've
213                  * checked the ownership bit.
214                  */
215                 dma_rmb();
216
217                 atomic_notifier_call_chain(&eqt->nh[eqe->type], eqe->type, eqe);
218                 atomic_notifier_call_chain(&eqt->nh[MLX5_EVENT_TYPE_NOTIFY_ANY], eqe->type, eqe);
219
220                 ++eq->cons_index;
221
222         } while ((++num_eqes < MLX5_EQ_POLLING_BUDGET) && (eqe = next_eqe_sw(eq)));
223
224 out:
225         eq_update_ci(eq, 1);
226
227         return 0;
228 }
229
230 static void init_eq_buf(struct mlx5_eq *eq)
231 {
232         struct mlx5_eqe *eqe;
233         int i;
234
235         for (i = 0; i < eq->nent; i++) {
236                 eqe = get_eqe(eq, i);
237                 eqe->owner = MLX5_EQE_OWNER_INIT_VAL;
238         }
239 }
240
241 static int
242 create_map_eq(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
243               struct mlx5_eq_param *param)
244 {
245         struct mlx5_cq_table *cq_table = &eq->cq_table;
246         u32 out[MLX5_ST_SZ_DW(create_eq_out)] = {0};
247         struct mlx5_priv *priv = &dev->priv;
248         u8 vecidx = param->irq_index;
249         __be64 *pas;
250         void *eqc;
251         int inlen;
252         u32 *in;
253         int err;
254         int i;
255
256         /* Init CQ table */
257         memset(cq_table, 0, sizeof(*cq_table));
258         spin_lock_init(&cq_table->lock);
259         INIT_RADIX_TREE(&cq_table->tree, GFP_ATOMIC);
260
261         eq->nent = roundup_pow_of_two(param->nent + MLX5_NUM_SPARE_EQE);
262         eq->cons_index = 0;
263         err = mlx5_buf_alloc(dev, eq->nent * MLX5_EQE_SIZE, &eq->buf);
264         if (err)
265                 return err;
266
267         init_eq_buf(eq);
268
269         inlen = MLX5_ST_SZ_BYTES(create_eq_in) +
270                 MLX5_FLD_SZ_BYTES(create_eq_in, pas[0]) * eq->buf.npages;
271
272         in = kvzalloc(inlen, GFP_KERNEL);
273         if (!in) {
274                 err = -ENOMEM;
275                 goto err_buf;
276         }
277
278         pas = (__be64 *)MLX5_ADDR_OF(create_eq_in, in, pas);
279         mlx5_fill_page_array(&eq->buf, pas);
280
281         MLX5_SET(create_eq_in, in, opcode, MLX5_CMD_OP_CREATE_EQ);
282         if (!param->mask[0] && MLX5_CAP_GEN(dev, log_max_uctx))
283                 MLX5_SET(create_eq_in, in, uid, MLX5_SHARED_RESOURCE_UID);
284
285         for (i = 0; i < 4; i++)
286                 MLX5_ARRAY_SET64(create_eq_in, in, event_bitmask, i,
287                                  param->mask[i]);
288
289         eqc = MLX5_ADDR_OF(create_eq_in, in, eq_context_entry);
290         MLX5_SET(eqc, eqc, log_eq_size, ilog2(eq->nent));
291         MLX5_SET(eqc, eqc, uar_page, priv->uar->index);
292         MLX5_SET(eqc, eqc, intr, vecidx);
293         MLX5_SET(eqc, eqc, log_page_size,
294                  eq->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
295
296         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
297         if (err)
298                 goto err_in;
299
300         eq->vecidx = vecidx;
301         eq->eqn = MLX5_GET(create_eq_out, out, eq_number);
302         eq->irqn = pci_irq_vector(dev->pdev, vecidx);
303         eq->dev = dev;
304         eq->doorbell = priv->uar->map + MLX5_EQ_DOORBEL_OFFSET;
305
306         err = mlx5_debug_eq_add(dev, eq);
307         if (err)
308                 goto err_eq;
309
310         kvfree(in);
311         return 0;
312
313 err_eq:
314         mlx5_cmd_destroy_eq(dev, eq->eqn);
315
316 err_in:
317         kvfree(in);
318
319 err_buf:
320         mlx5_buf_free(dev, &eq->buf);
321         return err;
322 }
323
324 /**
325  * mlx5_eq_enable - Enable EQ for receiving EQEs
326  * @dev : Device which owns the eq
327  * @eq  : EQ to enable
328  * @nb  : Notifier call block
329  *
330  * Must be called after EQ is created in device.
331  *
332  * @return: 0 if no error
333  */
334 int mlx5_eq_enable(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
335                    struct notifier_block *nb)
336 {
337         struct mlx5_eq_table *eq_table = dev->priv.eq_table;
338         int err;
339
340         err = mlx5_irq_attach_nb(eq_table->irq_table, eq->vecidx, nb);
341         if (!err)
342                 eq_update_ci(eq, 1);
343
344         return err;
345 }
346 EXPORT_SYMBOL(mlx5_eq_enable);
347
348 /**
349  * mlx5_eq_disable - Disable EQ for receiving EQEs
350  * @dev : Device which owns the eq
351  * @eq  : EQ to disable
352  * @nb  : Notifier call block
353  *
354  * Must be called before EQ is destroyed.
355  */
356 void mlx5_eq_disable(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
357                      struct notifier_block *nb)
358 {
359         struct mlx5_eq_table *eq_table = dev->priv.eq_table;
360
361         mlx5_irq_detach_nb(eq_table->irq_table, eq->vecidx, nb);
362 }
363 EXPORT_SYMBOL(mlx5_eq_disable);
364
365 static int destroy_unmap_eq(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
366 {
367         int err;
368
369         mlx5_debug_eq_remove(dev, eq);
370
371         err = mlx5_cmd_destroy_eq(dev, eq->eqn);
372         if (err)
373                 mlx5_core_warn(dev, "failed to destroy a previously created eq: eqn %d\n",
374                                eq->eqn);
375         synchronize_irq(eq->irqn);
376
377         mlx5_buf_free(dev, &eq->buf);
378
379         return err;
380 }
381
382 int mlx5_eq_add_cq(struct mlx5_eq *eq, struct mlx5_core_cq *cq)
383 {
384         struct mlx5_cq_table *table = &eq->cq_table;
385         int err;
386
387         spin_lock(&table->lock);
388         err = radix_tree_insert(&table->tree, cq->cqn, cq);
389         spin_unlock(&table->lock);
390
391         return err;
392 }
393
394 void mlx5_eq_del_cq(struct mlx5_eq *eq, struct mlx5_core_cq *cq)
395 {
396         struct mlx5_cq_table *table = &eq->cq_table;
397         struct mlx5_core_cq *tmp;
398
399         spin_lock(&table->lock);
400         tmp = radix_tree_delete(&table->tree, cq->cqn);
401         spin_unlock(&table->lock);
402
403         if (!tmp) {
404                 mlx5_core_dbg(eq->dev, "cq 0x%x not found in eq 0x%x tree\n",
405                               eq->eqn, cq->cqn);
406                 return;
407         }
408
409         if (tmp != cq)
410                 mlx5_core_dbg(eq->dev, "corruption on cqn 0x%x in eq 0x%x\n",
411                               eq->eqn, cq->cqn);
412 }
413
414 int mlx5_eq_table_init(struct mlx5_core_dev *dev)
415 {
416         struct mlx5_eq_table *eq_table;
417         int i;
418
419         eq_table = kvzalloc(sizeof(*eq_table), GFP_KERNEL);
420         if (!eq_table)
421                 return -ENOMEM;
422
423         dev->priv.eq_table = eq_table;
424
425         mlx5_eq_debugfs_init(dev);
426
427         mutex_init(&eq_table->lock);
428         for (i = 0; i < MLX5_EVENT_TYPE_MAX; i++)
429                 ATOMIC_INIT_NOTIFIER_HEAD(&eq_table->nh[i]);
430
431         eq_table->irq_table = dev->priv.irq_table;
432         return 0;
433 }
434
435 void mlx5_eq_table_cleanup(struct mlx5_core_dev *dev)
436 {
437         mlx5_eq_debugfs_cleanup(dev);
438         kvfree(dev->priv.eq_table);
439 }
440
441 /* Async EQs */
442
443 static int create_async_eq(struct mlx5_core_dev *dev,
444                            struct mlx5_eq *eq, struct mlx5_eq_param *param)
445 {
446         struct mlx5_eq_table *eq_table = dev->priv.eq_table;
447         int err;
448
449         mutex_lock(&eq_table->lock);
450         /* Async EQs must share irq index 0 */
451         if (param->irq_index != 0) {
452                 err = -EINVAL;
453                 goto unlock;
454         }
455
456         err = create_map_eq(dev, eq, param);
457 unlock:
458         mutex_unlock(&eq_table->lock);
459         return err;
460 }
461
462 static int destroy_async_eq(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
463 {
464         struct mlx5_eq_table *eq_table = dev->priv.eq_table;
465         int err;
466
467         mutex_lock(&eq_table->lock);
468         err = destroy_unmap_eq(dev, eq);
469         mutex_unlock(&eq_table->lock);
470         return err;
471 }
472
473 static int cq_err_event_notifier(struct notifier_block *nb,
474                                  unsigned long type, void *data)
475 {
476         struct mlx5_eq_table *eqt;
477         struct mlx5_core_cq *cq;
478         struct mlx5_eqe *eqe;
479         struct mlx5_eq *eq;
480         u32 cqn;
481
482         /* type == MLX5_EVENT_TYPE_CQ_ERROR */
483
484         eqt = mlx5_nb_cof(nb, struct mlx5_eq_table, cq_err_nb);
485         eq  = &eqt->async_eq.core;
486         eqe = data;
487
488         cqn = be32_to_cpu(eqe->data.cq_err.cqn) & 0xffffff;
489         mlx5_core_warn(eq->dev, "CQ error on CQN 0x%x, syndrome 0x%x\n",
490                        cqn, eqe->data.cq_err.syndrome);
491
492         cq = mlx5_eq_cq_get(eq, cqn);
493         if (unlikely(!cq)) {
494                 mlx5_core_warn(eq->dev, "Async event for bogus CQ 0x%x\n", cqn);
495                 return NOTIFY_OK;
496         }
497
498         if (cq->event)
499                 cq->event(cq, type);
500
501         mlx5_cq_put(cq);
502
503         return NOTIFY_OK;
504 }
505
506 static void gather_user_async_events(struct mlx5_core_dev *dev, u64 mask[4])
507 {
508         __be64 *user_unaffiliated_events;
509         __be64 *user_affiliated_events;
510         int i;
511
512         user_affiliated_events =
513                 MLX5_CAP_DEV_EVENT(dev, user_affiliated_events);
514         user_unaffiliated_events =
515                 MLX5_CAP_DEV_EVENT(dev, user_unaffiliated_events);
516
517         for (i = 0; i < 4; i++)
518                 mask[i] |= be64_to_cpu(user_affiliated_events[i] |
519                                        user_unaffiliated_events[i]);
520 }
521
522 static void gather_async_events_mask(struct mlx5_core_dev *dev, u64 mask[4])
523 {
524         u64 async_event_mask = MLX5_ASYNC_EVENT_MASK;
525
526         if (MLX5_VPORT_MANAGER(dev))
527                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_NIC_VPORT_CHANGE);
528
529         if (MLX5_CAP_GEN(dev, general_notification_event))
530                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_GENERAL_EVENT);
531
532         if (MLX5_CAP_GEN(dev, port_module_event))
533                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_PORT_MODULE_EVENT);
534         else
535                 mlx5_core_dbg(dev, "port_module_event is not set\n");
536
537         if (MLX5_PPS_CAP(dev))
538                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_PPS_EVENT);
539
540         if (MLX5_CAP_GEN(dev, fpga))
541                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_FPGA_ERROR) |
542                                     (1ull << MLX5_EVENT_TYPE_FPGA_QP_ERROR);
543         if (MLX5_CAP_GEN_MAX(dev, dct))
544                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_DCT_DRAINED);
545
546         if (MLX5_CAP_GEN(dev, temp_warn_event))
547                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_TEMP_WARN_EVENT);
548
549         if (MLX5_CAP_MCAM_REG(dev, tracer_registers))
550                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_DEVICE_TRACER);
551
552         if (MLX5_CAP_GEN(dev, max_num_of_monitor_counters))
553                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_MONITOR_COUNTER);
554
555         if (mlx5_eswitch_is_funcs_handler(dev))
556                 async_event_mask |=
557                         (1ull << MLX5_EVENT_TYPE_ESW_FUNCTIONS_CHANGED);
558
559         mask[0] = async_event_mask;
560
561         if (MLX5_CAP_GEN(dev, event_cap))
562                 gather_user_async_events(dev, mask);
563 }
564
565 static int
566 setup_async_eq(struct mlx5_core_dev *dev, struct mlx5_eq_async *eq,
567                struct mlx5_eq_param *param, const char *name)
568 {
569         int err;
570
571         eq->irq_nb.notifier_call = mlx5_eq_async_int;
572
573         err = create_async_eq(dev, &eq->core, param);
574         if (err) {
575                 mlx5_core_warn(dev, "failed to create %s EQ %d\n", name, err);
576                 return err;
577         }
578         err = mlx5_eq_enable(dev, &eq->core, &eq->irq_nb);
579         if (err) {
580                 mlx5_core_warn(dev, "failed to enable %s EQ %d\n", name, err);
581                 destroy_async_eq(dev, &eq->core);
582         }
583         return err;
584 }
585
586 static void cleanup_async_eq(struct mlx5_core_dev *dev,
587                              struct mlx5_eq_async *eq, const char *name)
588 {
589         int err;
590
591         mlx5_eq_disable(dev, &eq->core, &eq->irq_nb);
592         err = destroy_async_eq(dev, &eq->core);
593         if (err)
594                 mlx5_core_err(dev, "failed to destroy %s eq, err(%d)\n",
595                               name, err);
596 }
597
598 static int create_async_eqs(struct mlx5_core_dev *dev)
599 {
600         struct mlx5_eq_table *table = dev->priv.eq_table;
601         struct mlx5_eq_param param = {};
602         int err;
603
604         MLX5_NB_INIT(&table->cq_err_nb, cq_err_event_notifier, CQ_ERROR);
605         mlx5_eq_notifier_register(dev, &table->cq_err_nb);
606
607         param = (struct mlx5_eq_param) {
608                 .irq_index = 0,
609                 .nent = MLX5_NUM_CMD_EQE,
610                 .mask[0] = 1ull << MLX5_EVENT_TYPE_CMD,
611         };
612         mlx5_cmd_allowed_opcode(dev, MLX5_CMD_OP_CREATE_EQ);
613         err = setup_async_eq(dev, &table->cmd_eq, &param, "cmd");
614         if (err)
615                 goto err1;
616
617         mlx5_cmd_use_events(dev);
618         mlx5_cmd_allowed_opcode(dev, CMD_ALLOWED_OPCODE_ALL);
619
620         param = (struct mlx5_eq_param) {
621                 .irq_index = 0,
622                 .nent = MLX5_NUM_ASYNC_EQE,
623         };
624
625         gather_async_events_mask(dev, param.mask);
626         err = setup_async_eq(dev, &table->async_eq, &param, "async");
627         if (err)
628                 goto err2;
629
630         param = (struct mlx5_eq_param) {
631                 .irq_index = 0,
632                 .nent = /* TODO: sriov max_vf + */ 1,
633                 .mask[0] = 1ull << MLX5_EVENT_TYPE_PAGE_REQUEST,
634         };
635
636         err = setup_async_eq(dev, &table->pages_eq, &param, "pages");
637         if (err)
638                 goto err3;
639
640         return 0;
641
642 err3:
643         cleanup_async_eq(dev, &table->async_eq, "async");
644 err2:
645         mlx5_cmd_use_polling(dev);
646         cleanup_async_eq(dev, &table->cmd_eq, "cmd");
647 err1:
648         mlx5_cmd_allowed_opcode(dev, CMD_ALLOWED_OPCODE_ALL);
649         mlx5_eq_notifier_unregister(dev, &table->cq_err_nb);
650         return err;
651 }
652
653 static void destroy_async_eqs(struct mlx5_core_dev *dev)
654 {
655         struct mlx5_eq_table *table = dev->priv.eq_table;
656
657         cleanup_async_eq(dev, &table->pages_eq, "pages");
658         cleanup_async_eq(dev, &table->async_eq, "async");
659         mlx5_cmd_use_polling(dev);
660         cleanup_async_eq(dev, &table->cmd_eq, "cmd");
661         mlx5_eq_notifier_unregister(dev, &table->cq_err_nb);
662 }
663
664 struct mlx5_eq *mlx5_get_async_eq(struct mlx5_core_dev *dev)
665 {
666         return &dev->priv.eq_table->async_eq.core;
667 }
668
669 void mlx5_eq_synchronize_async_irq(struct mlx5_core_dev *dev)
670 {
671         synchronize_irq(dev->priv.eq_table->async_eq.core.irqn);
672 }
673
674 void mlx5_eq_synchronize_cmd_irq(struct mlx5_core_dev *dev)
675 {
676         synchronize_irq(dev->priv.eq_table->cmd_eq.core.irqn);
677 }
678
679 /* Generic EQ API for mlx5_core consumers
680  * Needed For RDMA ODP EQ for now
681  */
682 struct mlx5_eq *
683 mlx5_eq_create_generic(struct mlx5_core_dev *dev,
684                        struct mlx5_eq_param *param)
685 {
686         struct mlx5_eq *eq = kvzalloc(sizeof(*eq), GFP_KERNEL);
687         int err;
688
689         if (!eq)
690                 return ERR_PTR(-ENOMEM);
691
692         err = create_async_eq(dev, eq, param);
693         if (err) {
694                 kvfree(eq);
695                 eq = ERR_PTR(err);
696         }
697
698         return eq;
699 }
700 EXPORT_SYMBOL(mlx5_eq_create_generic);
701
702 int mlx5_eq_destroy_generic(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
703 {
704         int err;
705
706         if (IS_ERR(eq))
707                 return -EINVAL;
708
709         err = destroy_async_eq(dev, eq);
710         if (err)
711                 goto out;
712
713         kvfree(eq);
714 out:
715         return err;
716 }
717 EXPORT_SYMBOL(mlx5_eq_destroy_generic);
718
719 struct mlx5_eqe *mlx5_eq_get_eqe(struct mlx5_eq *eq, u32 cc)
720 {
721         u32 ci = eq->cons_index + cc;
722         struct mlx5_eqe *eqe;
723
724         eqe = get_eqe(eq, ci & (eq->nent - 1));
725         eqe = ((eqe->owner & 1) ^ !!(ci & eq->nent)) ? NULL : eqe;
726         /* Make sure we read EQ entry contents after we've
727          * checked the ownership bit.
728          */
729         if (eqe)
730                 dma_rmb();
731
732         return eqe;
733 }
734 EXPORT_SYMBOL(mlx5_eq_get_eqe);
735
736 void mlx5_eq_update_ci(struct mlx5_eq *eq, u32 cc, bool arm)
737 {
738         __be32 __iomem *addr = eq->doorbell + (arm ? 0 : 2);
739         u32 val;
740
741         eq->cons_index += cc;
742         val = (eq->cons_index & 0xffffff) | (eq->eqn << 24);
743
744         __raw_writel((__force u32)cpu_to_be32(val), addr);
745         /* We still want ordering, just not swabbing, so add a barrier */
746         wmb();
747 }
748 EXPORT_SYMBOL(mlx5_eq_update_ci);
749
750 static void destroy_comp_eqs(struct mlx5_core_dev *dev)
751 {
752         struct mlx5_eq_table *table = dev->priv.eq_table;
753         struct mlx5_eq_comp *eq, *n;
754
755         list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
756                 list_del(&eq->list);
757                 mlx5_eq_disable(dev, &eq->core, &eq->irq_nb);
758                 if (destroy_unmap_eq(dev, &eq->core))
759                         mlx5_core_warn(dev, "failed to destroy comp EQ 0x%x\n",
760                                        eq->core.eqn);
761                 tasklet_disable(&eq->tasklet_ctx.task);
762                 kfree(eq);
763         }
764 }
765
766 static int create_comp_eqs(struct mlx5_core_dev *dev)
767 {
768         struct mlx5_eq_table *table = dev->priv.eq_table;
769         struct mlx5_eq_comp *eq;
770         int ncomp_eqs;
771         int nent;
772         int err;
773         int i;
774
775         INIT_LIST_HEAD(&table->comp_eqs_list);
776         ncomp_eqs = table->num_comp_eqs;
777         nent = MLX5_COMP_EQ_SIZE;
778         for (i = 0; i < ncomp_eqs; i++) {
779                 int vecidx = i + MLX5_IRQ_VEC_COMP_BASE;
780                 struct mlx5_eq_param param = {};
781
782                 eq = kzalloc(sizeof(*eq), GFP_KERNEL);
783                 if (!eq) {
784                         err = -ENOMEM;
785                         goto clean;
786                 }
787
788                 INIT_LIST_HEAD(&eq->tasklet_ctx.list);
789                 INIT_LIST_HEAD(&eq->tasklet_ctx.process_list);
790                 spin_lock_init(&eq->tasklet_ctx.lock);
791                 tasklet_init(&eq->tasklet_ctx.task, mlx5_cq_tasklet_cb,
792                              (unsigned long)&eq->tasklet_ctx);
793
794                 eq->irq_nb.notifier_call = mlx5_eq_comp_int;
795                 param = (struct mlx5_eq_param) {
796                         .irq_index = vecidx,
797                         .nent = nent,
798                 };
799                 err = create_map_eq(dev, &eq->core, &param);
800                 if (err) {
801                         kfree(eq);
802                         goto clean;
803                 }
804                 err = mlx5_eq_enable(dev, &eq->core, &eq->irq_nb);
805                 if (err) {
806                         destroy_unmap_eq(dev, &eq->core);
807                         kfree(eq);
808                         goto clean;
809                 }
810
811                 mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->core.eqn);
812                 /* add tail, to keep the list ordered, for mlx5_vector2eqn to work */
813                 list_add_tail(&eq->list, &table->comp_eqs_list);
814         }
815
816         return 0;
817
818 clean:
819         destroy_comp_eqs(dev);
820         return err;
821 }
822
823 int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn,
824                     unsigned int *irqn)
825 {
826         struct mlx5_eq_table *table = dev->priv.eq_table;
827         struct mlx5_eq_comp *eq, *n;
828         int err = -ENOENT;
829         int i = 0;
830
831         list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
832                 if (i++ == vector) {
833                         *eqn = eq->core.eqn;
834                         *irqn = eq->core.irqn;
835                         err = 0;
836                         break;
837                 }
838         }
839
840         return err;
841 }
842 EXPORT_SYMBOL(mlx5_vector2eqn);
843
844 unsigned int mlx5_comp_vectors_count(struct mlx5_core_dev *dev)
845 {
846         return dev->priv.eq_table->num_comp_eqs;
847 }
848 EXPORT_SYMBOL(mlx5_comp_vectors_count);
849
850 struct cpumask *
851 mlx5_comp_irq_get_affinity_mask(struct mlx5_core_dev *dev, int vector)
852 {
853         int vecidx = vector + MLX5_IRQ_VEC_COMP_BASE;
854
855         return mlx5_irq_get_affinity_mask(dev->priv.eq_table->irq_table,
856                                           vecidx);
857 }
858 EXPORT_SYMBOL(mlx5_comp_irq_get_affinity_mask);
859
860 #ifdef CONFIG_RFS_ACCEL
861 struct cpu_rmap *mlx5_eq_table_get_rmap(struct mlx5_core_dev *dev)
862 {
863         return mlx5_irq_get_rmap(dev->priv.eq_table->irq_table);
864 }
865 #endif
866
867 struct mlx5_eq_comp *mlx5_eqn2comp_eq(struct mlx5_core_dev *dev, int eqn)
868 {
869         struct mlx5_eq_table *table = dev->priv.eq_table;
870         struct mlx5_eq_comp *eq;
871
872         list_for_each_entry(eq, &table->comp_eqs_list, list) {
873                 if (eq->core.eqn == eqn)
874                         return eq;
875         }
876
877         return ERR_PTR(-ENOENT);
878 }
879
880 /* This function should only be called after mlx5_cmd_force_teardown_hca */
881 void mlx5_core_eq_free_irqs(struct mlx5_core_dev *dev)
882 {
883         struct mlx5_eq_table *table = dev->priv.eq_table;
884
885         mutex_lock(&table->lock); /* sync with create/destroy_async_eq */
886         mlx5_irq_table_destroy(dev);
887         mutex_unlock(&table->lock);
888 }
889
890 int mlx5_eq_table_create(struct mlx5_core_dev *dev)
891 {
892         struct mlx5_eq_table *eq_table = dev->priv.eq_table;
893         int err;
894
895         eq_table->num_comp_eqs =
896                 mlx5_irq_get_num_comp(eq_table->irq_table);
897
898         err = create_async_eqs(dev);
899         if (err) {
900                 mlx5_core_err(dev, "Failed to create async EQs\n");
901                 goto err_async_eqs;
902         }
903
904         err = create_comp_eqs(dev);
905         if (err) {
906                 mlx5_core_err(dev, "Failed to create completion EQs\n");
907                 goto err_comp_eqs;
908         }
909
910         return 0;
911 err_comp_eqs:
912         destroy_async_eqs(dev);
913 err_async_eqs:
914         return err;
915 }
916
917 void mlx5_eq_table_destroy(struct mlx5_core_dev *dev)
918 {
919         destroy_comp_eqs(dev);
920         destroy_async_eqs(dev);
921 }
922
923 int mlx5_eq_notifier_register(struct mlx5_core_dev *dev, struct mlx5_nb *nb)
924 {
925         struct mlx5_eq_table *eqt = dev->priv.eq_table;
926
927         return atomic_notifier_chain_register(&eqt->nh[nb->event_type], &nb->nb);
928 }
929 EXPORT_SYMBOL(mlx5_eq_notifier_register);
930
931 int mlx5_eq_notifier_unregister(struct mlx5_core_dev *dev, struct mlx5_nb *nb)
932 {
933         struct mlx5_eq_table *eqt = dev->priv.eq_table;
934
935         return atomic_notifier_chain_unregister(&eqt->nh[nb->event_type], &nb->nb);
936 }
937 EXPORT_SYMBOL(mlx5_eq_notifier_unregister);