ee04aab65a9f2e09764486c02b78a353709acd0a
[linux-2.6-microblaze.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/eq.h>
38 #include <linux/mlx5/cmd.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 struct mlx5_irq_info {
64         cpumask_var_t mask;
65         char name[MLX5_MAX_IRQ_NAME];
66         void *context; /* dev_id provided to request_irq */
67 };
68
69 struct mlx5_eq_table {
70         struct list_head        comp_eqs_list;
71         struct mlx5_eq          pages_eq;
72         struct mlx5_eq          cmd_eq;
73         struct mlx5_eq          async_eq;
74
75         struct atomic_notifier_head nh[MLX5_EVENT_TYPE_MAX];
76
77         /* Since CQ DB is stored in async_eq */
78         struct mlx5_nb          cq_err_nb;
79
80         struct mutex            lock; /* sync async eqs creations */
81         int                     num_comp_vectors;
82         struct mlx5_irq_info    *irq_info;
83 #ifdef CONFIG_RFS_ACCEL
84         struct cpu_rmap         *rmap;
85 #endif
86 };
87
88 #define MLX5_ASYNC_EVENT_MASK ((1ull << MLX5_EVENT_TYPE_PATH_MIG)           | \
89                                (1ull << MLX5_EVENT_TYPE_COMM_EST)           | \
90                                (1ull << MLX5_EVENT_TYPE_SQ_DRAINED)         | \
91                                (1ull << MLX5_EVENT_TYPE_CQ_ERROR)           | \
92                                (1ull << MLX5_EVENT_TYPE_WQ_CATAS_ERROR)     | \
93                                (1ull << MLX5_EVENT_TYPE_PATH_MIG_FAILED)    | \
94                                (1ull << MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR) | \
95                                (1ull << MLX5_EVENT_TYPE_WQ_ACCESS_ERROR)    | \
96                                (1ull << MLX5_EVENT_TYPE_PORT_CHANGE)        | \
97                                (1ull << MLX5_EVENT_TYPE_SRQ_CATAS_ERROR)    | \
98                                (1ull << MLX5_EVENT_TYPE_SRQ_LAST_WQE)       | \
99                                (1ull << MLX5_EVENT_TYPE_SRQ_RQ_LIMIT))
100
101 static int mlx5_cmd_destroy_eq(struct mlx5_core_dev *dev, u8 eqn)
102 {
103         u32 out[MLX5_ST_SZ_DW(destroy_eq_out)] = {0};
104         u32 in[MLX5_ST_SZ_DW(destroy_eq_in)]   = {0};
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(dev, in, sizeof(in), out, sizeof(out));
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         spin_lock(&table->lock);
118         cq = radix_tree_lookup(&table->tree, cqn);
119         if (likely(cq))
120                 mlx5_cq_hold(cq);
121         spin_unlock(&table->lock);
122
123         return cq;
124 }
125
126 static irqreturn_t mlx5_eq_comp_int(int irq, void *eq_ptr)
127 {
128         struct mlx5_eq_comp *eq_comp = eq_ptr;
129         struct mlx5_eq *eq = eq_ptr;
130         struct mlx5_eqe *eqe;
131         int set_ci = 0;
132         u32 cqn = -1;
133
134         while ((eqe = next_eqe_sw(eq))) {
135                 struct mlx5_core_cq *cq;
136                 /* Make sure we read EQ entry contents after we've
137                  * checked the ownership bit.
138                  */
139                 dma_rmb();
140                 /* Assume (eqe->type) is always MLX5_EVENT_TYPE_COMP */
141                 cqn = be32_to_cpu(eqe->data.comp.cqn) & 0xffffff;
142
143                 cq = mlx5_eq_cq_get(eq, cqn);
144                 if (likely(cq)) {
145                         ++cq->arm_sn;
146                         cq->comp(cq);
147                         mlx5_cq_put(cq);
148                 } else {
149                         mlx5_core_warn(eq->dev, "Completion event for bogus CQ 0x%x\n", cqn);
150                 }
151
152                 ++eq->cons_index;
153                 ++set_ci;
154
155                 /* The HCA will think the queue has overflowed if we
156                  * don't tell it we've been processing events.  We
157                  * create our EQs with MLX5_NUM_SPARE_EQE extra
158                  * entries, so we must update our consumer index at
159                  * least that often.
160                  */
161                 if (unlikely(set_ci >= MLX5_NUM_SPARE_EQE)) {
162                         eq_update_ci(eq, 0);
163                         set_ci = 0;
164                 }
165         }
166
167         eq_update_ci(eq, 1);
168
169         if (cqn != -1)
170                 tasklet_schedule(&eq_comp->tasklet_ctx.task);
171
172         return IRQ_HANDLED;
173 }
174
175 /* Some architectures don't latch interrupts when they are disabled, so using
176  * mlx5_eq_poll_irq_disabled could end up losing interrupts while trying to
177  * avoid losing them.  It is not recommended to use it, unless this is the last
178  * resort.
179  */
180 u32 mlx5_eq_poll_irq_disabled(struct mlx5_eq_comp *eq)
181 {
182         u32 count_eqe;
183
184         disable_irq(eq->core.irqn);
185         count_eqe = eq->core.cons_index;
186         mlx5_eq_comp_int(eq->core.irqn, eq);
187         count_eqe = eq->core.cons_index - count_eqe;
188         enable_irq(eq->core.irqn);
189
190         return count_eqe;
191 }
192
193 static irqreturn_t mlx5_eq_async_int(int irq, void *eq_ptr)
194 {
195         struct mlx5_eq *eq = eq_ptr;
196         struct mlx5_eq_table *eqt;
197         struct mlx5_core_dev *dev;
198         struct mlx5_eqe *eqe;
199         int set_ci = 0;
200
201         dev = eq->dev;
202         eqt = dev->priv.eq_table;
203
204         while ((eqe = next_eqe_sw(eq))) {
205                 /*
206                  * Make sure we read EQ entry contents after we've
207                  * checked the ownership bit.
208                  */
209                 dma_rmb();
210
211                 if (likely(eqe->type < MLX5_EVENT_TYPE_MAX))
212                         atomic_notifier_call_chain(&eqt->nh[eqe->type], eqe->type, eqe);
213                 else
214                         mlx5_core_warn_once(dev, "notifier_call_chain is not setup for eqe: %d\n", eqe->type);
215
216                 atomic_notifier_call_chain(&eqt->nh[MLX5_EVENT_TYPE_NOTIFY_ANY], eqe->type, eqe);
217
218                 ++eq->cons_index;
219                 ++set_ci;
220
221                 /* The HCA will think the queue has overflowed if we
222                  * don't tell it we've been processing events.  We
223                  * create our EQs with MLX5_NUM_SPARE_EQE extra
224                  * entries, so we must update our consumer index at
225                  * least that often.
226                  */
227                 if (unlikely(set_ci >= MLX5_NUM_SPARE_EQE)) {
228                         eq_update_ci(eq, 0);
229                         set_ci = 0;
230                 }
231         }
232
233         eq_update_ci(eq, 1);
234
235         return IRQ_HANDLED;
236 }
237
238 static void init_eq_buf(struct mlx5_eq *eq)
239 {
240         struct mlx5_eqe *eqe;
241         int i;
242
243         for (i = 0; i < eq->nent; i++) {
244                 eqe = get_eqe(eq, i);
245                 eqe->owner = MLX5_EQE_OWNER_INIT_VAL;
246         }
247 }
248
249 static int
250 create_map_eq(struct mlx5_core_dev *dev, struct mlx5_eq *eq, const char *name,
251               struct mlx5_eq_param *param)
252 {
253         struct mlx5_eq_table *eq_table = dev->priv.eq_table;
254         struct mlx5_cq_table *cq_table = &eq->cq_table;
255         u32 out[MLX5_ST_SZ_DW(create_eq_out)] = {0};
256         struct mlx5_priv *priv = &dev->priv;
257         u8 vecidx = param->index;
258         __be64 *pas;
259         void *eqc;
260         int inlen;
261         u32 *in;
262         int err;
263
264         if (eq_table->irq_info[vecidx].context)
265                 return -EEXIST;
266
267         /* Init CQ table */
268         memset(cq_table, 0, sizeof(*cq_table));
269         spin_lock_init(&cq_table->lock);
270         INIT_RADIX_TREE(&cq_table->tree, GFP_ATOMIC);
271
272         eq->nent = roundup_pow_of_two(param->nent + MLX5_NUM_SPARE_EQE);
273         eq->cons_index = 0;
274         err = mlx5_buf_alloc(dev, eq->nent * MLX5_EQE_SIZE, &eq->buf);
275         if (err)
276                 return err;
277
278         init_eq_buf(eq);
279
280         inlen = MLX5_ST_SZ_BYTES(create_eq_in) +
281                 MLX5_FLD_SZ_BYTES(create_eq_in, pas[0]) * eq->buf.npages;
282
283         in = kvzalloc(inlen, GFP_KERNEL);
284         if (!in) {
285                 err = -ENOMEM;
286                 goto err_buf;
287         }
288
289         pas = (__be64 *)MLX5_ADDR_OF(create_eq_in, in, pas);
290         mlx5_fill_page_array(&eq->buf, pas);
291
292         MLX5_SET(create_eq_in, in, opcode, MLX5_CMD_OP_CREATE_EQ);
293         MLX5_SET64(create_eq_in, in, event_bitmask, param->mask);
294
295         eqc = MLX5_ADDR_OF(create_eq_in, in, eq_context_entry);
296         MLX5_SET(eqc, eqc, log_eq_size, ilog2(eq->nent));
297         MLX5_SET(eqc, eqc, uar_page, priv->uar->index);
298         MLX5_SET(eqc, eqc, intr, vecidx);
299         MLX5_SET(eqc, eqc, log_page_size,
300                  eq->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
301
302         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
303         if (err)
304                 goto err_in;
305
306         snprintf(eq_table->irq_info[vecidx].name, MLX5_MAX_IRQ_NAME, "%s@pci:%s",
307                  name, pci_name(dev->pdev));
308         eq_table->irq_info[vecidx].context = param->context;
309
310         eq->vecidx = vecidx;
311         eq->eqn = MLX5_GET(create_eq_out, out, eq_number);
312         eq->irqn = pci_irq_vector(dev->pdev, vecidx);
313         eq->dev = dev;
314         eq->doorbell = priv->uar->map + MLX5_EQ_DOORBEL_OFFSET;
315         err = request_irq(eq->irqn, param->handler, 0,
316                           eq_table->irq_info[vecidx].name, param->context);
317         if (err)
318                 goto err_eq;
319
320         err = mlx5_debug_eq_add(dev, eq);
321         if (err)
322                 goto err_irq;
323
324         /* EQs are created in ARMED state
325          */
326         eq_update_ci(eq, 1);
327
328         kvfree(in);
329         return 0;
330
331 err_irq:
332         free_irq(eq->irqn, eq);
333
334 err_eq:
335         mlx5_cmd_destroy_eq(dev, eq->eqn);
336
337 err_in:
338         kvfree(in);
339
340 err_buf:
341         mlx5_buf_free(dev, &eq->buf);
342         return err;
343 }
344
345 static int destroy_unmap_eq(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
346 {
347         struct mlx5_eq_table *eq_table = dev->priv.eq_table;
348         struct mlx5_irq_info *irq_info;
349         int err;
350
351         irq_info = &eq_table->irq_info[eq->vecidx];
352
353         mlx5_debug_eq_remove(dev, eq);
354
355         free_irq(eq->irqn, irq_info->context);
356         irq_info->context = NULL;
357
358         err = mlx5_cmd_destroy_eq(dev, eq->eqn);
359         if (err)
360                 mlx5_core_warn(dev, "failed to destroy a previously created eq: eqn %d\n",
361                                eq->eqn);
362         synchronize_irq(eq->irqn);
363
364         mlx5_buf_free(dev, &eq->buf);
365
366         return err;
367 }
368
369 int mlx5_eq_add_cq(struct mlx5_eq *eq, struct mlx5_core_cq *cq)
370 {
371         struct mlx5_cq_table *table = &eq->cq_table;
372         int err;
373
374         spin_lock_irq(&table->lock);
375         err = radix_tree_insert(&table->tree, cq->cqn, cq);
376         spin_unlock_irq(&table->lock);
377
378         return err;
379 }
380
381 int mlx5_eq_del_cq(struct mlx5_eq *eq, struct mlx5_core_cq *cq)
382 {
383         struct mlx5_cq_table *table = &eq->cq_table;
384         struct mlx5_core_cq *tmp;
385
386         spin_lock_irq(&table->lock);
387         tmp = radix_tree_delete(&table->tree, cq->cqn);
388         spin_unlock_irq(&table->lock);
389
390         if (!tmp) {
391                 mlx5_core_warn(eq->dev, "cq 0x%x not found in eq 0x%x tree\n", eq->eqn, cq->cqn);
392                 return -ENOENT;
393         }
394
395         if (tmp != cq) {
396                 mlx5_core_warn(eq->dev, "corruption on cqn 0x%x in eq 0x%x\n", eq->eqn, cq->cqn);
397                 return -EINVAL;
398         }
399
400         return 0;
401 }
402
403 int mlx5_eq_table_init(struct mlx5_core_dev *dev)
404 {
405         struct mlx5_eq_table *eq_table;
406         int i, err;
407
408         eq_table = kvzalloc(sizeof(*eq_table), GFP_KERNEL);
409         if (!eq_table)
410                 return -ENOMEM;
411
412         dev->priv.eq_table = eq_table;
413
414         err = mlx5_eq_debugfs_init(dev);
415         if (err)
416                 goto kvfree_eq_table;
417
418         mutex_init(&eq_table->lock);
419         for (i = 0; i < MLX5_EVENT_TYPE_MAX; i++)
420                 ATOMIC_INIT_NOTIFIER_HEAD(&eq_table->nh[i]);
421
422         return 0;
423
424 kvfree_eq_table:
425         kvfree(eq_table);
426         dev->priv.eq_table = NULL;
427         return err;
428 }
429
430 void mlx5_eq_table_cleanup(struct mlx5_core_dev *dev)
431 {
432         mlx5_eq_debugfs_cleanup(dev);
433         kvfree(dev->priv.eq_table);
434 }
435
436 /* Async EQs */
437
438 static int create_async_eq(struct mlx5_core_dev *dev, const char *name,
439                            struct mlx5_eq *eq, struct mlx5_eq_param *param)
440 {
441         struct mlx5_eq_table *eq_table = dev->priv.eq_table;
442         int err;
443
444         mutex_lock(&eq_table->lock);
445         if (param->index >= MLX5_EQ_MAX_ASYNC_EQS) {
446                 err = -ENOSPC;
447                 goto unlock;
448         }
449
450         err = create_map_eq(dev, eq, name, param);
451 unlock:
452         mutex_unlock(&eq_table->lock);
453         return err;
454 }
455
456 static int destroy_async_eq(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
457 {
458         struct mlx5_eq_table *eq_table = dev->priv.eq_table;
459         int err;
460
461         mutex_lock(&eq_table->lock);
462         err = destroy_unmap_eq(dev, eq);
463         mutex_unlock(&eq_table->lock);
464         return err;
465 }
466
467 static int cq_err_event_notifier(struct notifier_block *nb,
468                                  unsigned long type, void *data)
469 {
470         struct mlx5_eq_table *eqt;
471         struct mlx5_core_cq *cq;
472         struct mlx5_eqe *eqe;
473         struct mlx5_eq *eq;
474         u32 cqn;
475
476         /* type == MLX5_EVENT_TYPE_CQ_ERROR */
477
478         eqt = mlx5_nb_cof(nb, struct mlx5_eq_table, cq_err_nb);
479         eq  = &eqt->async_eq;
480         eqe = data;
481
482         cqn = be32_to_cpu(eqe->data.cq_err.cqn) & 0xffffff;
483         mlx5_core_warn(eq->dev, "CQ error on CQN 0x%x, syndrome 0x%x\n",
484                        cqn, eqe->data.cq_err.syndrome);
485
486         cq = mlx5_eq_cq_get(eq, cqn);
487         if (unlikely(!cq)) {
488                 mlx5_core_warn(eq->dev, "Async event for bogus CQ 0x%x\n", cqn);
489                 return NOTIFY_OK;
490         }
491
492         cq->event(cq, type);
493
494         mlx5_cq_put(cq);
495
496         return NOTIFY_OK;
497 }
498
499 static u64 gather_async_events_mask(struct mlx5_core_dev *dev)
500 {
501         u64 async_event_mask = MLX5_ASYNC_EVENT_MASK;
502
503         if (MLX5_VPORT_MANAGER(dev))
504                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_NIC_VPORT_CHANGE);
505
506         if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH &&
507             MLX5_CAP_GEN(dev, general_notification_event))
508                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_GENERAL_EVENT);
509
510         if (MLX5_CAP_GEN(dev, port_module_event))
511                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_PORT_MODULE_EVENT);
512         else
513                 mlx5_core_dbg(dev, "port_module_event is not set\n");
514
515         if (MLX5_PPS_CAP(dev))
516                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_PPS_EVENT);
517
518         if (MLX5_CAP_GEN(dev, fpga))
519                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_FPGA_ERROR) |
520                                     (1ull << MLX5_EVENT_TYPE_FPGA_QP_ERROR);
521         if (MLX5_CAP_GEN_MAX(dev, dct))
522                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_DCT_DRAINED);
523
524         if (MLX5_CAP_GEN(dev, temp_warn_event))
525                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_TEMP_WARN_EVENT);
526
527         if (MLX5_CAP_MCAM_REG(dev, tracer_registers))
528                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_DEVICE_TRACER);
529
530         if (MLX5_CAP_GEN(dev, max_num_of_monitor_counters))
531                 async_event_mask |= (1ull << MLX5_EVENT_TYPE_MONITOR_COUNTER);
532
533         return async_event_mask;
534 }
535
536 static int create_async_eqs(struct mlx5_core_dev *dev)
537 {
538         struct mlx5_eq_table *table = dev->priv.eq_table;
539         struct mlx5_eq_param param = {};
540         int err;
541
542         MLX5_NB_INIT(&table->cq_err_nb, cq_err_event_notifier, CQ_ERROR);
543         mlx5_eq_notifier_register(dev, &table->cq_err_nb);
544
545         param = (struct mlx5_eq_param) {
546                 .index = MLX5_EQ_CMD_IDX,
547                 .mask = 1ull << MLX5_EVENT_TYPE_CMD,
548                 .nent = MLX5_NUM_CMD_EQE,
549                 .context = &table->cmd_eq,
550                 .handler = mlx5_eq_async_int,
551         };
552         err = create_async_eq(dev, "mlx5_cmd_eq", &table->cmd_eq, &param);
553         if (err) {
554                 mlx5_core_warn(dev, "failed to create cmd EQ %d\n", err);
555                 goto err0;
556         }
557
558         mlx5_cmd_use_events(dev);
559
560         param = (struct mlx5_eq_param) {
561                 .index = MLX5_EQ_ASYNC_IDX,
562                 .mask = gather_async_events_mask(dev),
563                 .nent = MLX5_NUM_ASYNC_EQE,
564                 .context = &table->async_eq,
565                 .handler = mlx5_eq_async_int,
566         };
567         err = create_async_eq(dev, "mlx5_async_eq", &table->async_eq, &param);
568         if (err) {
569                 mlx5_core_warn(dev, "failed to create async EQ %d\n", err);
570                 goto err1;
571         }
572
573         param = (struct mlx5_eq_param) {
574                 .index = MLX5_EQ_PAGEREQ_IDX,
575                 .mask =  1 << MLX5_EVENT_TYPE_PAGE_REQUEST,
576                 .nent = /* TODO: sriov max_vf + */ 1,
577                 .context = &table->pages_eq,
578                 .handler = mlx5_eq_async_int,
579         };
580         err = create_async_eq(dev, "mlx5_pages_eq", &table->pages_eq, &param);
581         if (err) {
582                 mlx5_core_warn(dev, "failed to create pages EQ %d\n", err);
583                 goto err2;
584         }
585
586         return err;
587
588 err2:
589         destroy_async_eq(dev, &table->async_eq);
590
591 err1:
592         mlx5_cmd_use_polling(dev);
593         destroy_async_eq(dev, &table->cmd_eq);
594 err0:
595         mlx5_eq_notifier_unregister(dev, &table->cq_err_nb);
596         return err;
597 }
598
599 static void destroy_async_eqs(struct mlx5_core_dev *dev)
600 {
601         struct mlx5_eq_table *table = dev->priv.eq_table;
602         int err;
603
604         err = destroy_async_eq(dev, &table->pages_eq);
605         if (err)
606                 mlx5_core_err(dev, "failed to destroy pages eq, err(%d)\n",
607                               err);
608
609         err = destroy_async_eq(dev, &table->async_eq);
610         if (err)
611                 mlx5_core_err(dev, "failed to destroy async eq, err(%d)\n",
612                               err);
613
614         mlx5_cmd_use_polling(dev);
615
616         err = destroy_async_eq(dev, &table->cmd_eq);
617         if (err)
618                 mlx5_core_err(dev, "failed to destroy command eq, err(%d)\n",
619                               err);
620
621         mlx5_eq_notifier_unregister(dev, &table->cq_err_nb);
622 }
623
624 struct mlx5_eq *mlx5_get_async_eq(struct mlx5_core_dev *dev)
625 {
626         return &dev->priv.eq_table->async_eq;
627 }
628
629 void mlx5_eq_synchronize_async_irq(struct mlx5_core_dev *dev)
630 {
631         synchronize_irq(dev->priv.eq_table->async_eq.irqn);
632 }
633
634 void mlx5_eq_synchronize_cmd_irq(struct mlx5_core_dev *dev)
635 {
636         synchronize_irq(dev->priv.eq_table->cmd_eq.irqn);
637 }
638
639 /* Generic EQ API for mlx5_core consumers
640  * Needed For RDMA ODP EQ for now
641  */
642 struct mlx5_eq *
643 mlx5_eq_create_generic(struct mlx5_core_dev *dev, const char *name,
644                        struct mlx5_eq_param *param)
645 {
646         struct mlx5_eq *eq = kvzalloc(sizeof(*eq), GFP_KERNEL);
647         int err;
648
649         if (!eq)
650                 return ERR_PTR(-ENOMEM);
651
652         err = create_async_eq(dev, name, eq, param);
653         if (err) {
654                 kvfree(eq);
655                 eq = ERR_PTR(err);
656         }
657
658         return eq;
659 }
660 EXPORT_SYMBOL(mlx5_eq_create_generic);
661
662 int mlx5_eq_destroy_generic(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
663 {
664         int err;
665
666         if (IS_ERR(eq))
667                 return -EINVAL;
668
669         err = destroy_async_eq(dev, eq);
670         if (err)
671                 goto out;
672
673         kvfree(eq);
674 out:
675         return err;
676 }
677 EXPORT_SYMBOL(mlx5_eq_destroy_generic);
678
679 struct mlx5_eqe *mlx5_eq_get_eqe(struct mlx5_eq *eq, u32 cc)
680 {
681         u32 ci = eq->cons_index + cc;
682         struct mlx5_eqe *eqe;
683
684         eqe = get_eqe(eq, ci & (eq->nent - 1));
685         eqe = ((eqe->owner & 1) ^ !!(ci & eq->nent)) ? NULL : eqe;
686         /* Make sure we read EQ entry contents after we've
687          * checked the ownership bit.
688          */
689         if (eqe)
690                 dma_rmb();
691
692         return eqe;
693 }
694 EXPORT_SYMBOL(mlx5_eq_get_eqe);
695
696 void mlx5_eq_update_ci(struct mlx5_eq *eq, u32 cc, bool arm)
697 {
698         __be32 __iomem *addr = eq->doorbell + (arm ? 0 : 2);
699         u32 val;
700
701         eq->cons_index += cc;
702         val = (eq->cons_index & 0xffffff) | (eq->eqn << 24);
703
704         __raw_writel((__force u32)cpu_to_be32(val), addr);
705         /* We still want ordering, just not swabbing, so add a barrier */
706         mb();
707 }
708 EXPORT_SYMBOL(mlx5_eq_update_ci);
709
710 /* Completion EQs */
711
712 static int set_comp_irq_affinity_hint(struct mlx5_core_dev *mdev, int i)
713 {
714         struct mlx5_priv *priv  = &mdev->priv;
715         int vecidx = MLX5_EQ_VEC_COMP_BASE + i;
716         int irq = pci_irq_vector(mdev->pdev, vecidx);
717         struct mlx5_irq_info *irq_info = &priv->eq_table->irq_info[vecidx];
718
719         if (!zalloc_cpumask_var(&irq_info->mask, GFP_KERNEL)) {
720                 mlx5_core_warn(mdev, "zalloc_cpumask_var failed");
721                 return -ENOMEM;
722         }
723
724         cpumask_set_cpu(cpumask_local_spread(i, priv->numa_node),
725                         irq_info->mask);
726
727         if (IS_ENABLED(CONFIG_SMP) &&
728             irq_set_affinity_hint(irq, irq_info->mask))
729                 mlx5_core_warn(mdev, "irq_set_affinity_hint failed, irq 0x%.4x", irq);
730
731         return 0;
732 }
733
734 static void clear_comp_irq_affinity_hint(struct mlx5_core_dev *mdev, int i)
735 {
736         int vecidx = MLX5_EQ_VEC_COMP_BASE + i;
737         struct mlx5_priv *priv  = &mdev->priv;
738         int irq = pci_irq_vector(mdev->pdev, vecidx);
739         struct mlx5_irq_info *irq_info = &priv->eq_table->irq_info[vecidx];
740
741         irq_set_affinity_hint(irq, NULL);
742         free_cpumask_var(irq_info->mask);
743 }
744
745 static int set_comp_irq_affinity_hints(struct mlx5_core_dev *mdev)
746 {
747         int err;
748         int i;
749
750         for (i = 0; i < mdev->priv.eq_table->num_comp_vectors; i++) {
751                 err = set_comp_irq_affinity_hint(mdev, i);
752                 if (err)
753                         goto err_out;
754         }
755
756         return 0;
757
758 err_out:
759         for (i--; i >= 0; i--)
760                 clear_comp_irq_affinity_hint(mdev, i);
761
762         return err;
763 }
764
765 static void clear_comp_irqs_affinity_hints(struct mlx5_core_dev *mdev)
766 {
767         int i;
768
769         for (i = 0; i < mdev->priv.eq_table->num_comp_vectors; i++)
770                 clear_comp_irq_affinity_hint(mdev, i);
771 }
772
773 static void destroy_comp_eqs(struct mlx5_core_dev *dev)
774 {
775         struct mlx5_eq_table *table = dev->priv.eq_table;
776         struct mlx5_eq_comp *eq, *n;
777
778         clear_comp_irqs_affinity_hints(dev);
779
780 #ifdef CONFIG_RFS_ACCEL
781         if (table->rmap) {
782                 free_irq_cpu_rmap(table->rmap);
783                 table->rmap = NULL;
784         }
785 #endif
786         list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
787                 list_del(&eq->list);
788                 if (destroy_unmap_eq(dev, &eq->core))
789                         mlx5_core_warn(dev, "failed to destroy comp EQ 0x%x\n",
790                                        eq->core.eqn);
791                 tasklet_disable(&eq->tasklet_ctx.task);
792                 kfree(eq);
793         }
794 }
795
796 static int create_comp_eqs(struct mlx5_core_dev *dev)
797 {
798         struct mlx5_eq_table *table = dev->priv.eq_table;
799         char name[MLX5_MAX_IRQ_NAME];
800         struct mlx5_eq_comp *eq;
801         int ncomp_vec;
802         int nent;
803         int err;
804         int i;
805
806         INIT_LIST_HEAD(&table->comp_eqs_list);
807         ncomp_vec = table->num_comp_vectors;
808         nent = MLX5_COMP_EQ_SIZE;
809 #ifdef CONFIG_RFS_ACCEL
810         table->rmap = alloc_irq_cpu_rmap(ncomp_vec);
811         if (!table->rmap)
812                 return -ENOMEM;
813 #endif
814         for (i = 0; i < ncomp_vec; i++) {
815                 int vecidx = i + MLX5_EQ_VEC_COMP_BASE;
816                 struct mlx5_eq_param param = {};
817
818                 eq = kzalloc(sizeof(*eq), GFP_KERNEL);
819                 if (!eq) {
820                         err = -ENOMEM;
821                         goto clean;
822                 }
823
824                 INIT_LIST_HEAD(&eq->tasklet_ctx.list);
825                 INIT_LIST_HEAD(&eq->tasklet_ctx.process_list);
826                 spin_lock_init(&eq->tasklet_ctx.lock);
827                 tasklet_init(&eq->tasklet_ctx.task, mlx5_cq_tasklet_cb,
828                              (unsigned long)&eq->tasklet_ctx);
829
830 #ifdef CONFIG_RFS_ACCEL
831                 irq_cpu_rmap_add(table->rmap, pci_irq_vector(dev->pdev, vecidx));
832 #endif
833                 snprintf(name, MLX5_MAX_IRQ_NAME, "mlx5_comp%d", i);
834                 param = (struct mlx5_eq_param) {
835                         .index = vecidx,
836                         .mask = 0,
837                         .nent = nent,
838                         .context = &eq->core,
839                         .handler = mlx5_eq_comp_int
840                 };
841                 err = create_map_eq(dev, &eq->core, name, &param);
842                 if (err) {
843                         kfree(eq);
844                         goto clean;
845                 }
846                 mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->core.eqn);
847                 /* add tail, to keep the list ordered, for mlx5_vector2eqn to work */
848                 list_add_tail(&eq->list, &table->comp_eqs_list);
849         }
850
851         err = set_comp_irq_affinity_hints(dev);
852         if (err) {
853                 mlx5_core_err(dev, "Failed to alloc affinity hint cpumask\n");
854                 goto clean;
855         }
856
857         return 0;
858
859 clean:
860         destroy_comp_eqs(dev);
861         return err;
862 }
863
864 int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn,
865                     unsigned int *irqn)
866 {
867         struct mlx5_eq_table *table = dev->priv.eq_table;
868         struct mlx5_eq_comp *eq, *n;
869         int err = -ENOENT;
870         int i = 0;
871
872         list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
873                 if (i++ == vector) {
874                         *eqn = eq->core.eqn;
875                         *irqn = eq->core.irqn;
876                         err = 0;
877                         break;
878                 }
879         }
880
881         return err;
882 }
883 EXPORT_SYMBOL(mlx5_vector2eqn);
884
885 unsigned int mlx5_comp_vectors_count(struct mlx5_core_dev *dev)
886 {
887         return dev->priv.eq_table->num_comp_vectors;
888 }
889 EXPORT_SYMBOL(mlx5_comp_vectors_count);
890
891 struct cpumask *
892 mlx5_comp_irq_get_affinity_mask(struct mlx5_core_dev *dev, int vector)
893 {
894         /* TODO: consider irq_get_affinity_mask(irq) */
895         return dev->priv.eq_table->irq_info[vector + MLX5_EQ_VEC_COMP_BASE].mask;
896 }
897 EXPORT_SYMBOL(mlx5_comp_irq_get_affinity_mask);
898
899 struct cpu_rmap *mlx5_eq_table_get_rmap(struct mlx5_core_dev *dev)
900 {
901 #ifdef CONFIG_RFS_ACCEL
902         return dev->priv.eq_table->rmap;
903 #else
904         return NULL;
905 #endif
906 }
907
908 struct mlx5_eq_comp *mlx5_eqn2comp_eq(struct mlx5_core_dev *dev, int eqn)
909 {
910         struct mlx5_eq_table *table = dev->priv.eq_table;
911         struct mlx5_eq_comp *eq;
912
913         list_for_each_entry(eq, &table->comp_eqs_list, list) {
914                 if (eq->core.eqn == eqn)
915                         return eq;
916         }
917
918         return ERR_PTR(-ENOENT);
919 }
920
921 /* This function should only be called after mlx5_cmd_force_teardown_hca */
922 void mlx5_core_eq_free_irqs(struct mlx5_core_dev *dev)
923 {
924         struct mlx5_eq_table *table = dev->priv.eq_table;
925         int i, max_eqs;
926
927         clear_comp_irqs_affinity_hints(dev);
928
929 #ifdef CONFIG_RFS_ACCEL
930         if (table->rmap) {
931                 free_irq_cpu_rmap(table->rmap);
932                 table->rmap = NULL;
933         }
934 #endif
935
936         mutex_lock(&table->lock); /* sync with create/destroy_async_eq */
937         max_eqs = table->num_comp_vectors + MLX5_EQ_VEC_COMP_BASE;
938         for (i = max_eqs - 1; i >= 0; i--) {
939                 if (!table->irq_info[i].context)
940                         continue;
941                 free_irq(pci_irq_vector(dev->pdev, i), table->irq_info[i].context);
942                 table->irq_info[i].context = NULL;
943         }
944         mutex_unlock(&table->lock);
945         pci_free_irq_vectors(dev->pdev);
946 }
947
948 static int alloc_irq_vectors(struct mlx5_core_dev *dev)
949 {
950         struct mlx5_priv *priv = &dev->priv;
951         struct mlx5_eq_table *table = priv->eq_table;
952         int num_eqs = MLX5_CAP_GEN(dev, max_num_eqs) ?
953                       MLX5_CAP_GEN(dev, max_num_eqs) :
954                       1 << MLX5_CAP_GEN(dev, log_max_eq);
955         int nvec;
956         int err;
957
958         nvec = MLX5_CAP_GEN(dev, num_ports) * num_online_cpus() +
959                MLX5_EQ_VEC_COMP_BASE;
960         nvec = min_t(int, nvec, num_eqs);
961         if (nvec <= MLX5_EQ_VEC_COMP_BASE)
962                 return -ENOMEM;
963
964         table->irq_info = kcalloc(nvec, sizeof(*table->irq_info), GFP_KERNEL);
965         if (!table->irq_info)
966                 return -ENOMEM;
967
968         nvec = pci_alloc_irq_vectors(dev->pdev, MLX5_EQ_VEC_COMP_BASE + 1,
969                                      nvec, PCI_IRQ_MSIX);
970         if (nvec < 0) {
971                 err = nvec;
972                 goto err_free_irq_info;
973         }
974
975         table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
976
977         return 0;
978
979 err_free_irq_info:
980         kfree(table->irq_info);
981         return err;
982 }
983
984 static void free_irq_vectors(struct mlx5_core_dev *dev)
985 {
986         struct mlx5_priv *priv = &dev->priv;
987
988         pci_free_irq_vectors(dev->pdev);
989         kfree(priv->eq_table->irq_info);
990 }
991
992 int mlx5_eq_table_create(struct mlx5_core_dev *dev)
993 {
994         int err;
995
996         err = alloc_irq_vectors(dev);
997         if (err) {
998                 mlx5_core_err(dev, "alloc irq vectors failed\n");
999                 return err;
1000         }
1001
1002         err = create_async_eqs(dev);
1003         if (err) {
1004                 mlx5_core_err(dev, "Failed to create async EQs\n");
1005                 goto err_async_eqs;
1006         }
1007
1008         err = create_comp_eqs(dev);
1009         if (err) {
1010                 mlx5_core_err(dev, "Failed to create completion EQs\n");
1011                 goto err_comp_eqs;
1012         }
1013
1014         return 0;
1015 err_comp_eqs:
1016         destroy_async_eqs(dev);
1017 err_async_eqs:
1018         free_irq_vectors(dev);
1019         return err;
1020 }
1021
1022 void mlx5_eq_table_destroy(struct mlx5_core_dev *dev)
1023 {
1024         destroy_comp_eqs(dev);
1025         destroy_async_eqs(dev);
1026         free_irq_vectors(dev);
1027 }
1028
1029 int mlx5_eq_notifier_register(struct mlx5_core_dev *dev, struct mlx5_nb *nb)
1030 {
1031         struct mlx5_eq_table *eqt = dev->priv.eq_table;
1032
1033         if (nb->event_type >= MLX5_EVENT_TYPE_MAX)
1034                 return -EINVAL;
1035
1036         return atomic_notifier_chain_register(&eqt->nh[nb->event_type], &nb->nb);
1037 }
1038
1039 int mlx5_eq_notifier_unregister(struct mlx5_core_dev *dev, struct mlx5_nb *nb)
1040 {
1041         struct mlx5_eq_table *eqt = dev->priv.eq_table;
1042
1043         if (nb->event_type >= MLX5_EVENT_TYPE_MAX)
1044                 return -EINVAL;
1045
1046         return atomic_notifier_chain_unregister(&eqt->nh[nb->event_type], &nb->nb);
1047 }