net/mlx5: Implement RoCE LAG feature
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / main.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/highmem.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/errno.h>
37 #include <linux/pci.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/slab.h>
40 #include <linux/io-mapping.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/mlx5/driver.h>
44 #include <linux/mlx5/cq.h>
45 #include <linux/mlx5/qp.h>
46 #include <linux/mlx5/srq.h>
47 #include <linux/debugfs.h>
48 #include <linux/kmod.h>
49 #include <linux/delay.h>
50 #include <linux/mlx5/mlx5_ifc.h>
51 #ifdef CONFIG_RFS_ACCEL
52 #include <linux/cpu_rmap.h>
53 #endif
54 #include <net/devlink.h>
55 #include "mlx5_core.h"
56 #include "fs_core.h"
57 #ifdef CONFIG_MLX5_CORE_EN
58 #include "eswitch.h"
59 #endif
60
61 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
62 MODULE_DESCRIPTION("Mellanox Connect-IB, ConnectX-4 core driver");
63 MODULE_LICENSE("Dual BSD/GPL");
64 MODULE_VERSION(DRIVER_VERSION);
65
66 int mlx5_core_debug_mask;
67 module_param_named(debug_mask, mlx5_core_debug_mask, int, 0644);
68 MODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
69
70 #define MLX5_DEFAULT_PROF       2
71 static int prof_sel = MLX5_DEFAULT_PROF;
72 module_param_named(prof_sel, prof_sel, int, 0444);
73 MODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");
74
75 static LIST_HEAD(intf_list);
76
77 LIST_HEAD(mlx5_dev_list);
78 DEFINE_MUTEX(mlx5_intf_mutex);
79
80 struct mlx5_device_context {
81         struct list_head        list;
82         struct mlx5_interface  *intf;
83         void                   *context;
84 };
85
86 enum {
87         MLX5_ATOMIC_REQ_MODE_BE = 0x0,
88         MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS = 0x1,
89 };
90
91 static struct mlx5_profile profile[] = {
92         [0] = {
93                 .mask           = 0,
94         },
95         [1] = {
96                 .mask           = MLX5_PROF_MASK_QP_SIZE,
97                 .log_max_qp     = 12,
98         },
99         [2] = {
100                 .mask           = MLX5_PROF_MASK_QP_SIZE |
101                                   MLX5_PROF_MASK_MR_CACHE,
102                 .log_max_qp     = 17,
103                 .mr_cache[0]    = {
104                         .size   = 500,
105                         .limit  = 250
106                 },
107                 .mr_cache[1]    = {
108                         .size   = 500,
109                         .limit  = 250
110                 },
111                 .mr_cache[2]    = {
112                         .size   = 500,
113                         .limit  = 250
114                 },
115                 .mr_cache[3]    = {
116                         .size   = 500,
117                         .limit  = 250
118                 },
119                 .mr_cache[4]    = {
120                         .size   = 500,
121                         .limit  = 250
122                 },
123                 .mr_cache[5]    = {
124                         .size   = 500,
125                         .limit  = 250
126                 },
127                 .mr_cache[6]    = {
128                         .size   = 500,
129                         .limit  = 250
130                 },
131                 .mr_cache[7]    = {
132                         .size   = 500,
133                         .limit  = 250
134                 },
135                 .mr_cache[8]    = {
136                         .size   = 500,
137                         .limit  = 250
138                 },
139                 .mr_cache[9]    = {
140                         .size   = 500,
141                         .limit  = 250
142                 },
143                 .mr_cache[10]   = {
144                         .size   = 500,
145                         .limit  = 250
146                 },
147                 .mr_cache[11]   = {
148                         .size   = 500,
149                         .limit  = 250
150                 },
151                 .mr_cache[12]   = {
152                         .size   = 64,
153                         .limit  = 32
154                 },
155                 .mr_cache[13]   = {
156                         .size   = 32,
157                         .limit  = 16
158                 },
159                 .mr_cache[14]   = {
160                         .size   = 16,
161                         .limit  = 8
162                 },
163                 .mr_cache[15]   = {
164                         .size   = 8,
165                         .limit  = 4
166                 },
167         },
168 };
169
170 #define FW_INIT_TIMEOUT_MILI    2000
171 #define FW_INIT_WAIT_MS         2
172
173 static int wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili)
174 {
175         unsigned long end = jiffies + msecs_to_jiffies(max_wait_mili);
176         int err = 0;
177
178         while (fw_initializing(dev)) {
179                 if (time_after(jiffies, end)) {
180                         err = -EBUSY;
181                         break;
182                 }
183                 msleep(FW_INIT_WAIT_MS);
184         }
185
186         return err;
187 }
188
189 static int set_dma_caps(struct pci_dev *pdev)
190 {
191         int err;
192
193         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
194         if (err) {
195                 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
196                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
197                 if (err) {
198                         dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
199                         return err;
200                 }
201         }
202
203         err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
204         if (err) {
205                 dev_warn(&pdev->dev,
206                          "Warning: couldn't set 64-bit consistent PCI DMA mask\n");
207                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
208                 if (err) {
209                         dev_err(&pdev->dev,
210                                 "Can't set consistent PCI DMA mask, aborting\n");
211                         return err;
212                 }
213         }
214
215         dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
216         return err;
217 }
218
219 static int mlx5_pci_enable_device(struct mlx5_core_dev *dev)
220 {
221         struct pci_dev *pdev = dev->pdev;
222         int err = 0;
223
224         mutex_lock(&dev->pci_status_mutex);
225         if (dev->pci_status == MLX5_PCI_STATUS_DISABLED) {
226                 err = pci_enable_device(pdev);
227                 if (!err)
228                         dev->pci_status = MLX5_PCI_STATUS_ENABLED;
229         }
230         mutex_unlock(&dev->pci_status_mutex);
231
232         return err;
233 }
234
235 static void mlx5_pci_disable_device(struct mlx5_core_dev *dev)
236 {
237         struct pci_dev *pdev = dev->pdev;
238
239         mutex_lock(&dev->pci_status_mutex);
240         if (dev->pci_status == MLX5_PCI_STATUS_ENABLED) {
241                 pci_disable_device(pdev);
242                 dev->pci_status = MLX5_PCI_STATUS_DISABLED;
243         }
244         mutex_unlock(&dev->pci_status_mutex);
245 }
246
247 static int request_bar(struct pci_dev *pdev)
248 {
249         int err = 0;
250
251         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
252                 dev_err(&pdev->dev, "Missing registers BAR, aborting\n");
253                 return -ENODEV;
254         }
255
256         err = pci_request_regions(pdev, DRIVER_NAME);
257         if (err)
258                 dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
259
260         return err;
261 }
262
263 static void release_bar(struct pci_dev *pdev)
264 {
265         pci_release_regions(pdev);
266 }
267
268 static int mlx5_enable_msix(struct mlx5_core_dev *dev)
269 {
270         struct mlx5_priv *priv = &dev->priv;
271         struct mlx5_eq_table *table = &priv->eq_table;
272         int num_eqs = 1 << MLX5_CAP_GEN(dev, log_max_eq);
273         int nvec;
274         int i;
275
276         nvec = MLX5_CAP_GEN(dev, num_ports) * num_online_cpus() +
277                MLX5_EQ_VEC_COMP_BASE;
278         nvec = min_t(int, nvec, num_eqs);
279         if (nvec <= MLX5_EQ_VEC_COMP_BASE)
280                 return -ENOMEM;
281
282         priv->msix_arr = kcalloc(nvec, sizeof(*priv->msix_arr), GFP_KERNEL);
283
284         priv->irq_info = kcalloc(nvec, sizeof(*priv->irq_info), GFP_KERNEL);
285         if (!priv->msix_arr || !priv->irq_info)
286                 goto err_free_msix;
287
288         for (i = 0; i < nvec; i++)
289                 priv->msix_arr[i].entry = i;
290
291         nvec = pci_enable_msix_range(dev->pdev, priv->msix_arr,
292                                      MLX5_EQ_VEC_COMP_BASE + 1, nvec);
293         if (nvec < 0)
294                 return nvec;
295
296         table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
297
298         return 0;
299
300 err_free_msix:
301         kfree(priv->irq_info);
302         kfree(priv->msix_arr);
303         return -ENOMEM;
304 }
305
306 static void mlx5_disable_msix(struct mlx5_core_dev *dev)
307 {
308         struct mlx5_priv *priv = &dev->priv;
309
310         pci_disable_msix(dev->pdev);
311         kfree(priv->irq_info);
312         kfree(priv->msix_arr);
313 }
314
315 struct mlx5_reg_host_endianess {
316         u8      he;
317         u8      rsvd[15];
318 };
319
320
321 #define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos))
322
323 enum {
324         MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) |
325                                 MLX5_DEV_CAP_FLAG_DCT,
326 };
327
328 static u16 to_fw_pkey_sz(struct mlx5_core_dev *dev, u32 size)
329 {
330         switch (size) {
331         case 128:
332                 return 0;
333         case 256:
334                 return 1;
335         case 512:
336                 return 2;
337         case 1024:
338                 return 3;
339         case 2048:
340                 return 4;
341         case 4096:
342                 return 5;
343         default:
344                 mlx5_core_warn(dev, "invalid pkey table size %d\n", size);
345                 return 0;
346         }
347 }
348
349 static int mlx5_core_get_caps_mode(struct mlx5_core_dev *dev,
350                                    enum mlx5_cap_type cap_type,
351                                    enum mlx5_cap_mode cap_mode)
352 {
353         u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
354         int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
355         void *out, *hca_caps;
356         u16 opmod = (cap_type << 1) | (cap_mode & 0x01);
357         int err;
358
359         memset(in, 0, sizeof(in));
360         out = kzalloc(out_sz, GFP_KERNEL);
361         if (!out)
362                 return -ENOMEM;
363
364         MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
365         MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
366         err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
367         if (err) {
368                 mlx5_core_warn(dev,
369                                "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n",
370                                cap_type, cap_mode, err);
371                 goto query_ex;
372         }
373
374         hca_caps =  MLX5_ADDR_OF(query_hca_cap_out, out, capability);
375
376         switch (cap_mode) {
377         case HCA_CAP_OPMOD_GET_MAX:
378                 memcpy(dev->hca_caps_max[cap_type], hca_caps,
379                        MLX5_UN_SZ_BYTES(hca_cap_union));
380                 break;
381         case HCA_CAP_OPMOD_GET_CUR:
382                 memcpy(dev->hca_caps_cur[cap_type], hca_caps,
383                        MLX5_UN_SZ_BYTES(hca_cap_union));
384                 break;
385         default:
386                 mlx5_core_warn(dev,
387                                "Tried to query dev cap type(%x) with wrong opmode(%x)\n",
388                                cap_type, cap_mode);
389                 err = -EINVAL;
390                 break;
391         }
392 query_ex:
393         kfree(out);
394         return err;
395 }
396
397 int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type)
398 {
399         int ret;
400
401         ret = mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_CUR);
402         if (ret)
403                 return ret;
404         return mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_MAX);
405 }
406
407 static int set_caps(struct mlx5_core_dev *dev, void *in, int in_sz, int opmod)
408 {
409         u32 out[MLX5_ST_SZ_DW(set_hca_cap_out)] = {0};
410
411         MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
412         MLX5_SET(set_hca_cap_in, in, op_mod, opmod << 1);
413         return mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
414 }
415
416 static int handle_hca_cap_atomic(struct mlx5_core_dev *dev)
417 {
418         void *set_ctx;
419         void *set_hca_cap;
420         int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
421         int req_endianness;
422         int err;
423
424         if (MLX5_CAP_GEN(dev, atomic)) {
425                 err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC);
426                 if (err)
427                         return err;
428         } else {
429                 return 0;
430         }
431
432         req_endianness =
433                 MLX5_CAP_ATOMIC(dev,
434                                 supported_atomic_req_8B_endianess_mode_1);
435
436         if (req_endianness != MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS)
437                 return 0;
438
439         set_ctx = kzalloc(set_sz, GFP_KERNEL);
440         if (!set_ctx)
441                 return -ENOMEM;
442
443         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
444
445         /* Set requestor to host endianness */
446         MLX5_SET(atomic_caps, set_hca_cap, atomic_req_8B_endianess_mode,
447                  MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS);
448
449         err = set_caps(dev, set_ctx, set_sz, MLX5_SET_HCA_CAP_OP_MOD_ATOMIC);
450
451         kfree(set_ctx);
452         return err;
453 }
454
455 static int handle_hca_cap(struct mlx5_core_dev *dev)
456 {
457         void *set_ctx = NULL;
458         struct mlx5_profile *prof = dev->profile;
459         int err = -ENOMEM;
460         int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
461         void *set_hca_cap;
462
463         set_ctx = kzalloc(set_sz, GFP_KERNEL);
464         if (!set_ctx)
465                 goto query_ex;
466
467         err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL);
468         if (err)
469                 goto query_ex;
470
471         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
472                                    capability);
473         memcpy(set_hca_cap, dev->hca_caps_cur[MLX5_CAP_GENERAL],
474                MLX5_ST_SZ_BYTES(cmd_hca_cap));
475
476         mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
477                       mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
478                       128);
479         /* we limit the size of the pkey table to 128 entries for now */
480         MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
481                  to_fw_pkey_sz(dev, 128));
482
483         if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
484                 MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
485                          prof->log_max_qp);
486
487         /* disable cmdif checksum */
488         MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
489
490         MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12);
491
492         err = set_caps(dev, set_ctx, set_sz,
493                        MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE);
494
495 query_ex:
496         kfree(set_ctx);
497         return err;
498 }
499
500 static int set_hca_ctrl(struct mlx5_core_dev *dev)
501 {
502         struct mlx5_reg_host_endianess he_in;
503         struct mlx5_reg_host_endianess he_out;
504         int err;
505
506         if (!mlx5_core_is_pf(dev))
507                 return 0;
508
509         memset(&he_in, 0, sizeof(he_in));
510         he_in.he = MLX5_SET_HOST_ENDIANNESS;
511         err = mlx5_core_access_reg(dev, &he_in,  sizeof(he_in),
512                                         &he_out, sizeof(he_out),
513                                         MLX5_REG_HOST_ENDIANNESS, 0, 1);
514         return err;
515 }
516
517 int mlx5_core_enable_hca(struct mlx5_core_dev *dev, u16 func_id)
518 {
519         u32 out[MLX5_ST_SZ_DW(enable_hca_out)] = {0};
520         u32 in[MLX5_ST_SZ_DW(enable_hca_in)]   = {0};
521
522         MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA);
523         MLX5_SET(enable_hca_in, in, function_id, func_id);
524         return mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
525 }
526
527 int mlx5_core_disable_hca(struct mlx5_core_dev *dev, u16 func_id)
528 {
529         u32 out[MLX5_ST_SZ_DW(disable_hca_out)] = {0};
530         u32 in[MLX5_ST_SZ_DW(disable_hca_in)]   = {0};
531
532         MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA);
533         MLX5_SET(disable_hca_in, in, function_id, func_id);
534         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
535 }
536
537 cycle_t mlx5_read_internal_timer(struct mlx5_core_dev *dev)
538 {
539         u32 timer_h, timer_h1, timer_l;
540
541         timer_h = ioread32be(&dev->iseg->internal_timer_h);
542         timer_l = ioread32be(&dev->iseg->internal_timer_l);
543         timer_h1 = ioread32be(&dev->iseg->internal_timer_h);
544         if (timer_h != timer_h1) /* wrap around */
545                 timer_l = ioread32be(&dev->iseg->internal_timer_l);
546
547         return (cycle_t)timer_l | (cycle_t)timer_h1 << 32;
548 }
549
550 static int mlx5_irq_set_affinity_hint(struct mlx5_core_dev *mdev, int i)
551 {
552         struct mlx5_priv *priv  = &mdev->priv;
553         struct msix_entry *msix = priv->msix_arr;
554         int irq                 = msix[i + MLX5_EQ_VEC_COMP_BASE].vector;
555         int numa_node           = priv->numa_node;
556         int err;
557
558         if (!zalloc_cpumask_var(&priv->irq_info[i].mask, GFP_KERNEL)) {
559                 mlx5_core_warn(mdev, "zalloc_cpumask_var failed");
560                 return -ENOMEM;
561         }
562
563         cpumask_set_cpu(cpumask_local_spread(i, numa_node),
564                         priv->irq_info[i].mask);
565
566         err = irq_set_affinity_hint(irq, priv->irq_info[i].mask);
567         if (err) {
568                 mlx5_core_warn(mdev, "irq_set_affinity_hint failed,irq 0x%.4x",
569                                irq);
570                 goto err_clear_mask;
571         }
572
573         return 0;
574
575 err_clear_mask:
576         free_cpumask_var(priv->irq_info[i].mask);
577         return err;
578 }
579
580 static void mlx5_irq_clear_affinity_hint(struct mlx5_core_dev *mdev, int i)
581 {
582         struct mlx5_priv *priv  = &mdev->priv;
583         struct msix_entry *msix = priv->msix_arr;
584         int irq                 = msix[i + MLX5_EQ_VEC_COMP_BASE].vector;
585
586         irq_set_affinity_hint(irq, NULL);
587         free_cpumask_var(priv->irq_info[i].mask);
588 }
589
590 static int mlx5_irq_set_affinity_hints(struct mlx5_core_dev *mdev)
591 {
592         int err;
593         int i;
594
595         for (i = 0; i < mdev->priv.eq_table.num_comp_vectors; i++) {
596                 err = mlx5_irq_set_affinity_hint(mdev, i);
597                 if (err)
598                         goto err_out;
599         }
600
601         return 0;
602
603 err_out:
604         for (i--; i >= 0; i--)
605                 mlx5_irq_clear_affinity_hint(mdev, i);
606
607         return err;
608 }
609
610 static void mlx5_irq_clear_affinity_hints(struct mlx5_core_dev *mdev)
611 {
612         int i;
613
614         for (i = 0; i < mdev->priv.eq_table.num_comp_vectors; i++)
615                 mlx5_irq_clear_affinity_hint(mdev, i);
616 }
617
618 int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn,
619                     unsigned int *irqn)
620 {
621         struct mlx5_eq_table *table = &dev->priv.eq_table;
622         struct mlx5_eq *eq, *n;
623         int err = -ENOENT;
624
625         spin_lock(&table->lock);
626         list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
627                 if (eq->index == vector) {
628                         *eqn = eq->eqn;
629                         *irqn = eq->irqn;
630                         err = 0;
631                         break;
632                 }
633         }
634         spin_unlock(&table->lock);
635
636         return err;
637 }
638 EXPORT_SYMBOL(mlx5_vector2eqn);
639
640 struct mlx5_eq *mlx5_eqn2eq(struct mlx5_core_dev *dev, int eqn)
641 {
642         struct mlx5_eq_table *table = &dev->priv.eq_table;
643         struct mlx5_eq *eq;
644
645         spin_lock(&table->lock);
646         list_for_each_entry(eq, &table->comp_eqs_list, list)
647                 if (eq->eqn == eqn) {
648                         spin_unlock(&table->lock);
649                         return eq;
650                 }
651
652         spin_unlock(&table->lock);
653
654         return ERR_PTR(-ENOENT);
655 }
656
657 static void free_comp_eqs(struct mlx5_core_dev *dev)
658 {
659         struct mlx5_eq_table *table = &dev->priv.eq_table;
660         struct mlx5_eq *eq, *n;
661
662 #ifdef CONFIG_RFS_ACCEL
663         if (dev->rmap) {
664                 free_irq_cpu_rmap(dev->rmap);
665                 dev->rmap = NULL;
666         }
667 #endif
668         spin_lock(&table->lock);
669         list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
670                 list_del(&eq->list);
671                 spin_unlock(&table->lock);
672                 if (mlx5_destroy_unmap_eq(dev, eq))
673                         mlx5_core_warn(dev, "failed to destroy EQ 0x%x\n",
674                                        eq->eqn);
675                 kfree(eq);
676                 spin_lock(&table->lock);
677         }
678         spin_unlock(&table->lock);
679 }
680
681 static int alloc_comp_eqs(struct mlx5_core_dev *dev)
682 {
683         struct mlx5_eq_table *table = &dev->priv.eq_table;
684         char name[MLX5_MAX_IRQ_NAME];
685         struct mlx5_eq *eq;
686         int ncomp_vec;
687         int nent;
688         int err;
689         int i;
690
691         INIT_LIST_HEAD(&table->comp_eqs_list);
692         ncomp_vec = table->num_comp_vectors;
693         nent = MLX5_COMP_EQ_SIZE;
694 #ifdef CONFIG_RFS_ACCEL
695         dev->rmap = alloc_irq_cpu_rmap(ncomp_vec);
696         if (!dev->rmap)
697                 return -ENOMEM;
698 #endif
699         for (i = 0; i < ncomp_vec; i++) {
700                 eq = kzalloc(sizeof(*eq), GFP_KERNEL);
701                 if (!eq) {
702                         err = -ENOMEM;
703                         goto clean;
704                 }
705
706 #ifdef CONFIG_RFS_ACCEL
707                 irq_cpu_rmap_add(dev->rmap,
708                                  dev->priv.msix_arr[i + MLX5_EQ_VEC_COMP_BASE].vector);
709 #endif
710                 snprintf(name, MLX5_MAX_IRQ_NAME, "mlx5_comp%d", i);
711                 err = mlx5_create_map_eq(dev, eq,
712                                          i + MLX5_EQ_VEC_COMP_BASE, nent, 0,
713                                          name, &dev->priv.uuari.uars[0]);
714                 if (err) {
715                         kfree(eq);
716                         goto clean;
717                 }
718                 mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->eqn);
719                 eq->index = i;
720                 spin_lock(&table->lock);
721                 list_add_tail(&eq->list, &table->comp_eqs_list);
722                 spin_unlock(&table->lock);
723         }
724
725         return 0;
726
727 clean:
728         free_comp_eqs(dev);
729         return err;
730 }
731
732 static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
733 {
734         u32 query_in[MLX5_ST_SZ_DW(query_issi_in)]   = {0};
735         u32 query_out[MLX5_ST_SZ_DW(query_issi_out)] = {0};
736         u32 sup_issi;
737         int err;
738
739         MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
740         err = mlx5_cmd_exec(dev, query_in, sizeof(query_in),
741                             query_out, sizeof(query_out));
742         if (err) {
743                 u32 syndrome;
744                 u8 status;
745
746                 mlx5_cmd_mbox_status(query_out, &status, &syndrome);
747                 if (status == MLX5_CMD_STAT_BAD_OP_ERR) {
748                         pr_debug("Only ISSI 0 is supported\n");
749                         return 0;
750                 }
751
752                 pr_err("failed to query ISSI err(%d)\n", err);
753                 return err;
754         }
755
756         sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);
757
758         if (sup_issi & (1 << 1)) {
759                 u32 set_in[MLX5_ST_SZ_DW(set_issi_in)]   = {0};
760                 u32 set_out[MLX5_ST_SZ_DW(set_issi_out)] = {0};
761
762                 MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
763                 MLX5_SET(set_issi_in, set_in, current_issi, 1);
764                 err = mlx5_cmd_exec(dev, set_in, sizeof(set_in),
765                                     set_out, sizeof(set_out));
766                 if (err) {
767                         pr_err("failed to set ISSI=1 err(%d)\n", err);
768                         return err;
769                 }
770
771                 dev->issi = 1;
772
773                 return 0;
774         } else if (sup_issi & (1 << 0) || !sup_issi) {
775                 return 0;
776         }
777
778         return -ENOTSUPP;
779 }
780
781 static void mlx5_add_device(struct mlx5_interface *intf, struct mlx5_priv *priv)
782 {
783         struct mlx5_device_context *dev_ctx;
784         struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv);
785
786         dev_ctx = kmalloc(sizeof(*dev_ctx), GFP_KERNEL);
787         if (!dev_ctx)
788                 return;
789
790         dev_ctx->intf    = intf;
791         dev_ctx->context = intf->add(dev);
792
793         if (dev_ctx->context) {
794                 spin_lock_irq(&priv->ctx_lock);
795                 list_add_tail(&dev_ctx->list, &priv->ctx_list);
796                 spin_unlock_irq(&priv->ctx_lock);
797         } else {
798                 kfree(dev_ctx);
799         }
800 }
801
802 static void mlx5_remove_device(struct mlx5_interface *intf, struct mlx5_priv *priv)
803 {
804         struct mlx5_device_context *dev_ctx;
805         struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv);
806
807         list_for_each_entry(dev_ctx, &priv->ctx_list, list)
808                 if (dev_ctx->intf == intf) {
809                         spin_lock_irq(&priv->ctx_lock);
810                         list_del(&dev_ctx->list);
811                         spin_unlock_irq(&priv->ctx_lock);
812
813                         intf->remove(dev, dev_ctx->context);
814                         kfree(dev_ctx);
815                         return;
816                 }
817 }
818
819 static int mlx5_register_device(struct mlx5_core_dev *dev)
820 {
821         struct mlx5_priv *priv = &dev->priv;
822         struct mlx5_interface *intf;
823
824         mutex_lock(&mlx5_intf_mutex);
825         list_add_tail(&priv->dev_list, &mlx5_dev_list);
826         list_for_each_entry(intf, &intf_list, list)
827                 mlx5_add_device(intf, priv);
828         mutex_unlock(&mlx5_intf_mutex);
829
830         return 0;
831 }
832
833 static void mlx5_unregister_device(struct mlx5_core_dev *dev)
834 {
835         struct mlx5_priv *priv = &dev->priv;
836         struct mlx5_interface *intf;
837
838         mutex_lock(&mlx5_intf_mutex);
839         list_for_each_entry(intf, &intf_list, list)
840                 mlx5_remove_device(intf, priv);
841         list_del(&priv->dev_list);
842         mutex_unlock(&mlx5_intf_mutex);
843 }
844
845 int mlx5_register_interface(struct mlx5_interface *intf)
846 {
847         struct mlx5_priv *priv;
848
849         if (!intf->add || !intf->remove)
850                 return -EINVAL;
851
852         mutex_lock(&mlx5_intf_mutex);
853         list_add_tail(&intf->list, &intf_list);
854         list_for_each_entry(priv, &mlx5_dev_list, dev_list)
855                 mlx5_add_device(intf, priv);
856         mutex_unlock(&mlx5_intf_mutex);
857
858         return 0;
859 }
860 EXPORT_SYMBOL(mlx5_register_interface);
861
862 void mlx5_unregister_interface(struct mlx5_interface *intf)
863 {
864         struct mlx5_priv *priv;
865
866         mutex_lock(&mlx5_intf_mutex);
867         list_for_each_entry(priv, &mlx5_dev_list, dev_list)
868                 mlx5_remove_device(intf, priv);
869         list_del(&intf->list);
870         mutex_unlock(&mlx5_intf_mutex);
871 }
872 EXPORT_SYMBOL(mlx5_unregister_interface);
873
874 void *mlx5_get_protocol_dev(struct mlx5_core_dev *mdev, int protocol)
875 {
876         struct mlx5_priv *priv = &mdev->priv;
877         struct mlx5_device_context *dev_ctx;
878         unsigned long flags;
879         void *result = NULL;
880
881         spin_lock_irqsave(&priv->ctx_lock, flags);
882
883         list_for_each_entry(dev_ctx, &mdev->priv.ctx_list, list)
884                 if ((dev_ctx->intf->protocol == protocol) &&
885                     dev_ctx->intf->get_dev) {
886                         result = dev_ctx->intf->get_dev(dev_ctx->context);
887                         break;
888                 }
889
890         spin_unlock_irqrestore(&priv->ctx_lock, flags);
891
892         return result;
893 }
894 EXPORT_SYMBOL(mlx5_get_protocol_dev);
895
896 /* Must be called with intf_mutex held */
897 void mlx5_add_dev_by_protocol(struct mlx5_core_dev *dev, int protocol)
898 {
899         struct mlx5_interface *intf;
900
901         list_for_each_entry(intf, &intf_list, list)
902                 if (intf->protocol == protocol) {
903                         mlx5_add_device(intf, &dev->priv);
904                         break;
905                 }
906 }
907
908 /* Must be called with intf_mutex held */
909 void mlx5_remove_dev_by_protocol(struct mlx5_core_dev *dev, int protocol)
910 {
911         struct mlx5_interface *intf;
912
913         list_for_each_entry(intf, &intf_list, list)
914                 if (intf->protocol == protocol) {
915                         mlx5_remove_device(intf, &dev->priv);
916                         break;
917                 }
918 }
919
920 static int mlx5_pci_init(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
921 {
922         struct pci_dev *pdev = dev->pdev;
923         int err = 0;
924
925         pci_set_drvdata(dev->pdev, dev);
926         strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
927         priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
928
929         mutex_init(&priv->pgdir_mutex);
930         INIT_LIST_HEAD(&priv->pgdir_list);
931         spin_lock_init(&priv->mkey_lock);
932
933         mutex_init(&priv->alloc_mutex);
934
935         priv->numa_node = dev_to_node(&dev->pdev->dev);
936
937         priv->dbg_root = debugfs_create_dir(dev_name(&pdev->dev), mlx5_debugfs_root);
938         if (!priv->dbg_root)
939                 return -ENOMEM;
940
941         err = mlx5_pci_enable_device(dev);
942         if (err) {
943                 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
944                 goto err_dbg;
945         }
946
947         err = request_bar(pdev);
948         if (err) {
949                 dev_err(&pdev->dev, "error requesting BARs, aborting\n");
950                 goto err_disable;
951         }
952
953         pci_set_master(pdev);
954
955         err = set_dma_caps(pdev);
956         if (err) {
957                 dev_err(&pdev->dev, "Failed setting DMA capabilities mask, aborting\n");
958                 goto err_clr_master;
959         }
960
961         dev->iseg_base = pci_resource_start(dev->pdev, 0);
962         dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
963         if (!dev->iseg) {
964                 err = -ENOMEM;
965                 dev_err(&pdev->dev, "Failed mapping initialization segment, aborting\n");
966                 goto err_clr_master;
967         }
968
969         return 0;
970
971 err_clr_master:
972         pci_clear_master(dev->pdev);
973         release_bar(dev->pdev);
974 err_disable:
975         mlx5_pci_disable_device(dev);
976
977 err_dbg:
978         debugfs_remove(priv->dbg_root);
979         return err;
980 }
981
982 static void mlx5_pci_close(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
983 {
984         iounmap(dev->iseg);
985         pci_clear_master(dev->pdev);
986         release_bar(dev->pdev);
987         mlx5_pci_disable_device(dev);
988         debugfs_remove(priv->dbg_root);
989 }
990
991 #define MLX5_IB_MOD "mlx5_ib"
992 static int mlx5_load_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
993 {
994         struct pci_dev *pdev = dev->pdev;
995         int err;
996
997         mutex_lock(&dev->intf_state_mutex);
998         if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
999                 dev_warn(&dev->pdev->dev, "%s: interface is up, NOP\n",
1000                          __func__);
1001                 goto out;
1002         }
1003
1004         dev_info(&pdev->dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
1005                  fw_rev_min(dev), fw_rev_sub(dev));
1006
1007         /* on load removing any previous indication of internal error, device is
1008          * up
1009          */
1010         dev->state = MLX5_DEVICE_STATE_UP;
1011
1012         err = mlx5_cmd_init(dev);
1013         if (err) {
1014                 dev_err(&pdev->dev, "Failed initializing command interface, aborting\n");
1015                 goto out_err;
1016         }
1017
1018         err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI);
1019         if (err) {
1020                 dev_err(&dev->pdev->dev, "Firmware over %d MS in initializing state, aborting\n",
1021                         FW_INIT_TIMEOUT_MILI);
1022                 goto out_err;
1023         }
1024
1025         mlx5_pagealloc_init(dev);
1026
1027         err = mlx5_core_enable_hca(dev, 0);
1028         if (err) {
1029                 dev_err(&pdev->dev, "enable hca failed\n");
1030                 goto err_pagealloc_cleanup;
1031         }
1032
1033         err = mlx5_core_set_issi(dev);
1034         if (err) {
1035                 dev_err(&pdev->dev, "failed to set issi\n");
1036                 goto err_disable_hca;
1037         }
1038
1039         err = mlx5_satisfy_startup_pages(dev, 1);
1040         if (err) {
1041                 dev_err(&pdev->dev, "failed to allocate boot pages\n");
1042                 goto err_disable_hca;
1043         }
1044
1045         err = set_hca_ctrl(dev);
1046         if (err) {
1047                 dev_err(&pdev->dev, "set_hca_ctrl failed\n");
1048                 goto reclaim_boot_pages;
1049         }
1050
1051         err = handle_hca_cap(dev);
1052         if (err) {
1053                 dev_err(&pdev->dev, "handle_hca_cap failed\n");
1054                 goto reclaim_boot_pages;
1055         }
1056
1057         err = handle_hca_cap_atomic(dev);
1058         if (err) {
1059                 dev_err(&pdev->dev, "handle_hca_cap_atomic failed\n");
1060                 goto reclaim_boot_pages;
1061         }
1062
1063         err = mlx5_satisfy_startup_pages(dev, 0);
1064         if (err) {
1065                 dev_err(&pdev->dev, "failed to allocate init pages\n");
1066                 goto reclaim_boot_pages;
1067         }
1068
1069         err = mlx5_pagealloc_start(dev);
1070         if (err) {
1071                 dev_err(&pdev->dev, "mlx5_pagealloc_start failed\n");
1072                 goto reclaim_boot_pages;
1073         }
1074
1075         err = mlx5_cmd_init_hca(dev);
1076         if (err) {
1077                 dev_err(&pdev->dev, "init hca failed\n");
1078                 goto err_pagealloc_stop;
1079         }
1080
1081         mlx5_start_health_poll(dev);
1082
1083         err = mlx5_query_hca_caps(dev);
1084         if (err) {
1085                 dev_err(&pdev->dev, "query hca failed\n");
1086                 goto err_stop_poll;
1087         }
1088
1089         err = mlx5_query_board_id(dev);
1090         if (err) {
1091                 dev_err(&pdev->dev, "query board id failed\n");
1092                 goto err_stop_poll;
1093         }
1094
1095         err = mlx5_enable_msix(dev);
1096         if (err) {
1097                 dev_err(&pdev->dev, "enable msix failed\n");
1098                 goto err_stop_poll;
1099         }
1100
1101         err = mlx5_eq_init(dev);
1102         if (err) {
1103                 dev_err(&pdev->dev, "failed to initialize eq\n");
1104                 goto disable_msix;
1105         }
1106
1107         err = mlx5_alloc_uuars(dev, &priv->uuari);
1108         if (err) {
1109                 dev_err(&pdev->dev, "Failed allocating uar, aborting\n");
1110                 goto err_eq_cleanup;
1111         }
1112
1113         err = mlx5_start_eqs(dev);
1114         if (err) {
1115                 dev_err(&pdev->dev, "Failed to start pages and async EQs\n");
1116                 goto err_free_uar;
1117         }
1118
1119         err = alloc_comp_eqs(dev);
1120         if (err) {
1121                 dev_err(&pdev->dev, "Failed to alloc completion EQs\n");
1122                 goto err_stop_eqs;
1123         }
1124
1125         err = mlx5_irq_set_affinity_hints(dev);
1126         if (err)
1127                 dev_err(&pdev->dev, "Failed to alloc affinity hint cpumask\n");
1128
1129         MLX5_INIT_DOORBELL_LOCK(&priv->cq_uar_lock);
1130
1131         mlx5_init_cq_table(dev);
1132         mlx5_init_qp_table(dev);
1133         mlx5_init_srq_table(dev);
1134         mlx5_init_mkey_table(dev);
1135
1136         err = mlx5_init_fs(dev);
1137         if (err) {
1138                 dev_err(&pdev->dev, "Failed to init flow steering\n");
1139                 goto err_fs;
1140         }
1141
1142         err = mlx5_init_rl_table(dev);
1143         if (err) {
1144                 dev_err(&pdev->dev, "Failed to init rate limiting\n");
1145                 goto err_rl;
1146         }
1147
1148 #ifdef CONFIG_MLX5_CORE_EN
1149         err = mlx5_eswitch_init(dev);
1150         if (err) {
1151                 dev_err(&pdev->dev, "eswitch init failed %d\n", err);
1152                 goto err_reg_dev;
1153         }
1154 #endif
1155
1156         err = mlx5_sriov_init(dev);
1157         if (err) {
1158                 dev_err(&pdev->dev, "sriov init failed %d\n", err);
1159                 goto err_sriov;
1160         }
1161
1162         err = mlx5_register_device(dev);
1163         if (err) {
1164                 dev_err(&pdev->dev, "mlx5_register_device failed %d\n", err);
1165                 goto err_reg_dev;
1166         }
1167
1168         err = request_module_nowait(MLX5_IB_MOD);
1169         if (err)
1170                 pr_info("failed request module on %s\n", MLX5_IB_MOD);
1171
1172         clear_bit(MLX5_INTERFACE_STATE_DOWN, &dev->intf_state);
1173         set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1174 out:
1175         mutex_unlock(&dev->intf_state_mutex);
1176
1177         return 0;
1178
1179 err_sriov:
1180         if (mlx5_sriov_cleanup(dev))
1181                 dev_err(&dev->pdev->dev, "sriov cleanup failed\n");
1182
1183 #ifdef CONFIG_MLX5_CORE_EN
1184         mlx5_eswitch_cleanup(dev->priv.eswitch);
1185 #endif
1186 err_reg_dev:
1187         mlx5_cleanup_rl_table(dev);
1188 err_rl:
1189         mlx5_cleanup_fs(dev);
1190 err_fs:
1191         mlx5_cleanup_mkey_table(dev);
1192         mlx5_cleanup_srq_table(dev);
1193         mlx5_cleanup_qp_table(dev);
1194         mlx5_cleanup_cq_table(dev);
1195         mlx5_irq_clear_affinity_hints(dev);
1196         free_comp_eqs(dev);
1197
1198 err_stop_eqs:
1199         mlx5_stop_eqs(dev);
1200
1201 err_free_uar:
1202         mlx5_free_uuars(dev, &priv->uuari);
1203
1204 err_eq_cleanup:
1205         mlx5_eq_cleanup(dev);
1206
1207 disable_msix:
1208         mlx5_disable_msix(dev);
1209
1210 err_stop_poll:
1211         mlx5_stop_health_poll(dev);
1212         if (mlx5_cmd_teardown_hca(dev)) {
1213                 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1214                 goto out_err;
1215         }
1216
1217 err_pagealloc_stop:
1218         mlx5_pagealloc_stop(dev);
1219
1220 reclaim_boot_pages:
1221         mlx5_reclaim_startup_pages(dev);
1222
1223 err_disable_hca:
1224         mlx5_core_disable_hca(dev, 0);
1225
1226 err_pagealloc_cleanup:
1227         mlx5_pagealloc_cleanup(dev);
1228         mlx5_cmd_cleanup(dev);
1229
1230 out_err:
1231         dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
1232         mutex_unlock(&dev->intf_state_mutex);
1233
1234         return err;
1235 }
1236
1237 static int mlx5_unload_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
1238 {
1239         int err = 0;
1240
1241         err = mlx5_sriov_cleanup(dev);
1242         if (err) {
1243                 dev_warn(&dev->pdev->dev, "%s: sriov cleanup failed - abort\n",
1244                          __func__);
1245                 return err;
1246         }
1247
1248         mutex_lock(&dev->intf_state_mutex);
1249         if (test_bit(MLX5_INTERFACE_STATE_DOWN, &dev->intf_state)) {
1250                 dev_warn(&dev->pdev->dev, "%s: interface is down, NOP\n",
1251                          __func__);
1252                 goto out;
1253         }
1254         mlx5_unregister_device(dev);
1255 #ifdef CONFIG_MLX5_CORE_EN
1256         mlx5_eswitch_cleanup(dev->priv.eswitch);
1257 #endif
1258
1259         mlx5_cleanup_rl_table(dev);
1260         mlx5_cleanup_fs(dev);
1261         mlx5_cleanup_mkey_table(dev);
1262         mlx5_cleanup_srq_table(dev);
1263         mlx5_cleanup_qp_table(dev);
1264         mlx5_cleanup_cq_table(dev);
1265         mlx5_irq_clear_affinity_hints(dev);
1266         free_comp_eqs(dev);
1267         mlx5_stop_eqs(dev);
1268         mlx5_free_uuars(dev, &priv->uuari);
1269         mlx5_eq_cleanup(dev);
1270         mlx5_disable_msix(dev);
1271         mlx5_stop_health_poll(dev);
1272         err = mlx5_cmd_teardown_hca(dev);
1273         if (err) {
1274                 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1275                 goto out;
1276         }
1277         mlx5_pagealloc_stop(dev);
1278         mlx5_reclaim_startup_pages(dev);
1279         mlx5_core_disable_hca(dev, 0);
1280         mlx5_pagealloc_cleanup(dev);
1281         mlx5_cmd_cleanup(dev);
1282
1283 out:
1284         clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1285         set_bit(MLX5_INTERFACE_STATE_DOWN, &dev->intf_state);
1286         mutex_unlock(&dev->intf_state_mutex);
1287         return err;
1288 }
1289
1290 void mlx5_core_event(struct mlx5_core_dev *dev, enum mlx5_dev_event event,
1291                      unsigned long param)
1292 {
1293         struct mlx5_priv *priv = &dev->priv;
1294         struct mlx5_device_context *dev_ctx;
1295         unsigned long flags;
1296
1297         spin_lock_irqsave(&priv->ctx_lock, flags);
1298
1299         list_for_each_entry(dev_ctx, &priv->ctx_list, list)
1300                 if (dev_ctx->intf->event)
1301                         dev_ctx->intf->event(dev, dev_ctx->context, event, param);
1302
1303         spin_unlock_irqrestore(&priv->ctx_lock, flags);
1304 }
1305
1306 struct mlx5_core_event_handler {
1307         void (*event)(struct mlx5_core_dev *dev,
1308                       enum mlx5_dev_event event,
1309                       void *data);
1310 };
1311
1312 static const struct devlink_ops mlx5_devlink_ops = {
1313 #ifdef CONFIG_MLX5_CORE_EN
1314         .eswitch_mode_set = mlx5_devlink_eswitch_mode_set,
1315         .eswitch_mode_get = mlx5_devlink_eswitch_mode_get,
1316 #endif
1317 };
1318
1319 static int init_one(struct pci_dev *pdev,
1320                     const struct pci_device_id *id)
1321 {
1322         struct mlx5_core_dev *dev;
1323         struct devlink *devlink;
1324         struct mlx5_priv *priv;
1325         int err;
1326
1327         devlink = devlink_alloc(&mlx5_devlink_ops, sizeof(*dev));
1328         if (!devlink) {
1329                 dev_err(&pdev->dev, "kzalloc failed\n");
1330                 return -ENOMEM;
1331         }
1332
1333         dev = devlink_priv(devlink);
1334         priv = &dev->priv;
1335         priv->pci_dev_data = id->driver_data;
1336
1337         pci_set_drvdata(pdev, dev);
1338
1339         if (prof_sel < 0 || prof_sel >= ARRAY_SIZE(profile)) {
1340                 mlx5_core_warn(dev,
1341                                "selected profile out of range, selecting default (%d)\n",
1342                                MLX5_DEFAULT_PROF);
1343                 prof_sel = MLX5_DEFAULT_PROF;
1344         }
1345         dev->profile = &profile[prof_sel];
1346         dev->pdev = pdev;
1347         dev->event = mlx5_core_event;
1348
1349         INIT_LIST_HEAD(&priv->ctx_list);
1350         spin_lock_init(&priv->ctx_lock);
1351         mutex_init(&dev->pci_status_mutex);
1352         mutex_init(&dev->intf_state_mutex);
1353         err = mlx5_pci_init(dev, priv);
1354         if (err) {
1355                 dev_err(&pdev->dev, "mlx5_pci_init failed with error code %d\n", err);
1356                 goto clean_dev;
1357         }
1358
1359         err = mlx5_health_init(dev);
1360         if (err) {
1361                 dev_err(&pdev->dev, "mlx5_health_init failed with error code %d\n", err);
1362                 goto close_pci;
1363         }
1364
1365         err = mlx5_load_one(dev, priv);
1366         if (err) {
1367                 dev_err(&pdev->dev, "mlx5_load_one failed with error code %d\n", err);
1368                 goto clean_health;
1369         }
1370
1371         err = devlink_register(devlink, &pdev->dev);
1372         if (err)
1373                 goto clean_load;
1374
1375         return 0;
1376
1377 clean_load:
1378         mlx5_unload_one(dev, priv);
1379 clean_health:
1380         mlx5_health_cleanup(dev);
1381 close_pci:
1382         mlx5_pci_close(dev, priv);
1383 clean_dev:
1384         pci_set_drvdata(pdev, NULL);
1385         devlink_free(devlink);
1386
1387         return err;
1388 }
1389
1390 static void remove_one(struct pci_dev *pdev)
1391 {
1392         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1393         struct devlink *devlink = priv_to_devlink(dev);
1394         struct mlx5_priv *priv = &dev->priv;
1395
1396         devlink_unregister(devlink);
1397         if (mlx5_unload_one(dev, priv)) {
1398                 dev_err(&dev->pdev->dev, "mlx5_unload_one failed\n");
1399                 mlx5_health_cleanup(dev);
1400                 return;
1401         }
1402         mlx5_health_cleanup(dev);
1403         mlx5_pci_close(dev, priv);
1404         pci_set_drvdata(pdev, NULL);
1405         devlink_free(devlink);
1406 }
1407
1408 static pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev,
1409                                               pci_channel_state_t state)
1410 {
1411         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1412         struct mlx5_priv *priv = &dev->priv;
1413
1414         dev_info(&pdev->dev, "%s was called\n", __func__);
1415         mlx5_enter_error_state(dev);
1416         mlx5_unload_one(dev, priv);
1417         mlx5_pci_disable_device(dev);
1418         return state == pci_channel_io_perm_failure ?
1419                 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1420 }
1421
1422 static pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev)
1423 {
1424         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1425         int err = 0;
1426
1427         dev_info(&pdev->dev, "%s was called\n", __func__);
1428
1429         err = mlx5_pci_enable_device(dev);
1430         if (err) {
1431                 dev_err(&pdev->dev, "%s: mlx5_pci_enable_device failed with error code: %d\n"
1432                         , __func__, err);
1433                 return PCI_ERS_RESULT_DISCONNECT;
1434         }
1435         pci_set_master(pdev);
1436         pci_set_power_state(pdev, PCI_D0);
1437         pci_restore_state(pdev);
1438
1439         return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
1440 }
1441
1442 void mlx5_disable_device(struct mlx5_core_dev *dev)
1443 {
1444         mlx5_pci_err_detected(dev->pdev, 0);
1445 }
1446
1447 /* wait for the device to show vital signs by waiting
1448  * for the health counter to start counting.
1449  */
1450 static int wait_vital(struct pci_dev *pdev)
1451 {
1452         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1453         struct mlx5_core_health *health = &dev->priv.health;
1454         const int niter = 100;
1455         u32 last_count = 0;
1456         u32 count;
1457         int i;
1458
1459         for (i = 0; i < niter; i++) {
1460                 count = ioread32be(health->health_counter);
1461                 if (count && count != 0xffffffff) {
1462                         if (last_count && last_count != count) {
1463                                 dev_info(&pdev->dev, "Counter value 0x%x after %d iterations\n", count, i);
1464                                 return 0;
1465                         }
1466                         last_count = count;
1467                 }
1468                 msleep(50);
1469         }
1470
1471         return -ETIMEDOUT;
1472 }
1473
1474 static void mlx5_pci_resume(struct pci_dev *pdev)
1475 {
1476         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1477         struct mlx5_priv *priv = &dev->priv;
1478         int err;
1479
1480         dev_info(&pdev->dev, "%s was called\n", __func__);
1481
1482         pci_save_state(pdev);
1483         err = wait_vital(pdev);
1484         if (err) {
1485                 dev_err(&pdev->dev, "%s: wait_vital timed out\n", __func__);
1486                 return;
1487         }
1488
1489         err = mlx5_load_one(dev, priv);
1490         if (err)
1491                 dev_err(&pdev->dev, "%s: mlx5_load_one failed with error code: %d\n"
1492                         , __func__, err);
1493         else
1494                 dev_info(&pdev->dev, "%s: device recovered\n", __func__);
1495 }
1496
1497 static const struct pci_error_handlers mlx5_err_handler = {
1498         .error_detected = mlx5_pci_err_detected,
1499         .slot_reset     = mlx5_pci_slot_reset,
1500         .resume         = mlx5_pci_resume
1501 };
1502
1503 static void shutdown(struct pci_dev *pdev)
1504 {
1505         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1506         struct mlx5_priv *priv = &dev->priv;
1507
1508         dev_info(&pdev->dev, "Shutdown was called\n");
1509         /* Notify mlx5 clients that the kernel is being shut down */
1510         set_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &dev->intf_state);
1511         mlx5_unload_one(dev, priv);
1512         mlx5_pci_disable_device(dev);
1513 }
1514
1515 static const struct pci_device_id mlx5_core_pci_table[] = {
1516         { PCI_VDEVICE(MELLANOX, 0x1011) },                      /* Connect-IB */
1517         { PCI_VDEVICE(MELLANOX, 0x1012), MLX5_PCI_DEV_IS_VF},   /* Connect-IB VF */
1518         { PCI_VDEVICE(MELLANOX, 0x1013) },                      /* ConnectX-4 */
1519         { PCI_VDEVICE(MELLANOX, 0x1014), MLX5_PCI_DEV_IS_VF},   /* ConnectX-4 VF */
1520         { PCI_VDEVICE(MELLANOX, 0x1015) },                      /* ConnectX-4LX */
1521         { PCI_VDEVICE(MELLANOX, 0x1016), MLX5_PCI_DEV_IS_VF},   /* ConnectX-4LX VF */
1522         { PCI_VDEVICE(MELLANOX, 0x1017) },                      /* ConnectX-5, PCIe 3.0 */
1523         { PCI_VDEVICE(MELLANOX, 0x1018), MLX5_PCI_DEV_IS_VF},   /* ConnectX-5 VF */
1524         { PCI_VDEVICE(MELLANOX, 0x1019) },                      /* ConnectX-5, PCIe 4.0 */
1525         { 0, }
1526 };
1527
1528 MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
1529
1530 static struct pci_driver mlx5_core_driver = {
1531         .name           = DRIVER_NAME,
1532         .id_table       = mlx5_core_pci_table,
1533         .probe          = init_one,
1534         .remove         = remove_one,
1535         .shutdown       = shutdown,
1536         .err_handler    = &mlx5_err_handler,
1537         .sriov_configure   = mlx5_core_sriov_configure,
1538 };
1539
1540 static int __init init(void)
1541 {
1542         int err;
1543
1544         mlx5_register_debugfs();
1545
1546         err = pci_register_driver(&mlx5_core_driver);
1547         if (err)
1548                 goto err_debug;
1549
1550 #ifdef CONFIG_MLX5_CORE_EN
1551         mlx5e_init();
1552 #endif
1553
1554         return 0;
1555
1556 err_debug:
1557         mlx5_unregister_debugfs();
1558         return err;
1559 }
1560
1561 static void __exit cleanup(void)
1562 {
1563 #ifdef CONFIG_MLX5_CORE_EN
1564         mlx5e_cleanup();
1565 #endif
1566         pci_unregister_driver(&mlx5_core_driver);
1567         mlx5_unregister_debugfs();
1568 }
1569
1570 module_init(init);
1571 module_exit(cleanup);