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