net/mlx5: Split the load/unload flow into hardware and software flows
[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 static int mlx5_init_once(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
1097 {
1098         struct pci_dev *pdev = dev->pdev;
1099         int err;
1100
1101         err = mlx5_query_hca_caps(dev);
1102         if (err) {
1103                 dev_err(&pdev->dev, "query hca failed\n");
1104                 goto out;
1105         }
1106
1107         err = mlx5_query_board_id(dev);
1108         if (err) {
1109                 dev_err(&pdev->dev, "query board id failed\n");
1110                 goto out;
1111         }
1112
1113         err = mlx5_eq_init(dev);
1114         if (err) {
1115                 dev_err(&pdev->dev, "failed to initialize eq\n");
1116                 goto out;
1117         }
1118
1119         MLX5_INIT_DOORBELL_LOCK(&priv->cq_uar_lock);
1120
1121         err = mlx5_init_cq_table(dev);
1122         if (err) {
1123                 dev_err(&pdev->dev, "failed to initialize cq table\n");
1124                 goto err_eq_cleanup;
1125         }
1126
1127         mlx5_init_qp_table(dev);
1128
1129         mlx5_init_srq_table(dev);
1130
1131         mlx5_init_mkey_table(dev);
1132
1133         err = mlx5_init_rl_table(dev);
1134         if (err) {
1135                 dev_err(&pdev->dev, "Failed to init rate limiting\n");
1136                 goto err_tables_cleanup;
1137         }
1138
1139         return 0;
1140
1141 err_tables_cleanup:
1142         mlx5_cleanup_mkey_table(dev);
1143         mlx5_cleanup_srq_table(dev);
1144         mlx5_cleanup_qp_table(dev);
1145         mlx5_cleanup_cq_table(dev);
1146
1147 err_eq_cleanup:
1148         mlx5_eq_cleanup(dev);
1149
1150 out:
1151         return err;
1152 }
1153
1154 static void mlx5_cleanup_once(struct mlx5_core_dev *dev)
1155 {
1156         mlx5_cleanup_rl_table(dev);
1157         mlx5_cleanup_mkey_table(dev);
1158         mlx5_cleanup_srq_table(dev);
1159         mlx5_cleanup_qp_table(dev);
1160         mlx5_cleanup_cq_table(dev);
1161         mlx5_eq_cleanup(dev);
1162 }
1163
1164 static int mlx5_load_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
1165                          bool boot)
1166 {
1167         struct pci_dev *pdev = dev->pdev;
1168         int err;
1169
1170         mutex_lock(&dev->intf_state_mutex);
1171         if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1172                 dev_warn(&dev->pdev->dev, "%s: interface is up, NOP\n",
1173                          __func__);
1174                 goto out;
1175         }
1176
1177         dev_info(&pdev->dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
1178                  fw_rev_min(dev), fw_rev_sub(dev));
1179
1180         /* on load removing any previous indication of internal error, device is
1181          * up
1182          */
1183         dev->state = MLX5_DEVICE_STATE_UP;
1184
1185         err = mlx5_cmd_init(dev);
1186         if (err) {
1187                 dev_err(&pdev->dev, "Failed initializing command interface, aborting\n");
1188                 goto out_err;
1189         }
1190
1191         err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI);
1192         if (err) {
1193                 dev_err(&dev->pdev->dev, "Firmware over %d MS in initializing state, aborting\n",
1194                         FW_INIT_TIMEOUT_MILI);
1195                 goto out_err;
1196         }
1197
1198         err = mlx5_core_enable_hca(dev, 0);
1199         if (err) {
1200                 dev_err(&pdev->dev, "enable hca failed\n");
1201                 goto err_cmd_cleanup;
1202         }
1203
1204         err = mlx5_core_set_issi(dev);
1205         if (err) {
1206                 dev_err(&pdev->dev, "failed to set issi\n");
1207                 goto err_disable_hca;
1208         }
1209
1210         err = mlx5_satisfy_startup_pages(dev, 1);
1211         if (err) {
1212                 dev_err(&pdev->dev, "failed to allocate boot pages\n");
1213                 goto err_disable_hca;
1214         }
1215
1216         err = set_hca_ctrl(dev);
1217         if (err) {
1218                 dev_err(&pdev->dev, "set_hca_ctrl failed\n");
1219                 goto reclaim_boot_pages;
1220         }
1221
1222         err = handle_hca_cap(dev);
1223         if (err) {
1224                 dev_err(&pdev->dev, "handle_hca_cap failed\n");
1225                 goto reclaim_boot_pages;
1226         }
1227
1228         err = handle_hca_cap_atomic(dev);
1229         if (err) {
1230                 dev_err(&pdev->dev, "handle_hca_cap_atomic failed\n");
1231                 goto reclaim_boot_pages;
1232         }
1233
1234         err = mlx5_satisfy_startup_pages(dev, 0);
1235         if (err) {
1236                 dev_err(&pdev->dev, "failed to allocate init pages\n");
1237                 goto reclaim_boot_pages;
1238         }
1239
1240         err = mlx5_pagealloc_start(dev);
1241         if (err) {
1242                 dev_err(&pdev->dev, "mlx5_pagealloc_start failed\n");
1243                 goto reclaim_boot_pages;
1244         }
1245
1246         err = mlx5_cmd_init_hca(dev);
1247         if (err) {
1248                 dev_err(&pdev->dev, "init hca failed\n");
1249                 goto err_pagealloc_stop;
1250         }
1251
1252         mlx5_start_health_poll(dev);
1253
1254         if (boot && mlx5_init_once(dev, priv)) {
1255                 dev_err(&pdev->dev, "sw objs init failed\n");
1256                 goto err_stop_poll;
1257         }
1258
1259         err = mlx5_enable_msix(dev);
1260         if (err) {
1261                 dev_err(&pdev->dev, "enable msix failed\n");
1262                 goto err_cleanup_once;
1263         }
1264
1265         err = mlx5_alloc_uuars(dev, &priv->uuari);
1266         if (err) {
1267                 dev_err(&pdev->dev, "Failed allocating uar, aborting\n");
1268                 goto err_disable_msix;
1269         }
1270
1271         err = mlx5_start_eqs(dev);
1272         if (err) {
1273                 dev_err(&pdev->dev, "Failed to start pages and async EQs\n");
1274                 goto err_free_uar;
1275         }
1276
1277         err = alloc_comp_eqs(dev);
1278         if (err) {
1279                 dev_err(&pdev->dev, "Failed to alloc completion EQs\n");
1280                 goto err_stop_eqs;
1281         }
1282
1283         err = mlx5_irq_set_affinity_hints(dev);
1284         if (err) {
1285                 dev_err(&pdev->dev, "Failed to alloc affinity hint cpumask\n");
1286                 goto err_affinity_hints;
1287         }
1288
1289         err = mlx5_init_fs(dev);
1290         if (err) {
1291                 dev_err(&pdev->dev, "Failed to init flow steering\n");
1292                 goto err_fs;
1293         }
1294
1295 #ifdef CONFIG_MLX5_CORE_EN
1296         err = mlx5_eswitch_init(dev);
1297         if (err) {
1298                 dev_err(&pdev->dev, "eswitch init failed %d\n", err);
1299                 goto err_reg_dev;
1300         }
1301 #endif
1302
1303         err = mlx5_sriov_init(dev);
1304         if (err) {
1305                 dev_err(&pdev->dev, "sriov init failed %d\n", err);
1306                 goto err_sriov;
1307         }
1308
1309         if (mlx5_device_registered(dev)) {
1310                 mlx5_attach_device(dev);
1311         } else {
1312                 err = mlx5_register_device(dev);
1313                 if (err) {
1314                         dev_err(&pdev->dev, "mlx5_register_device failed %d\n", err);
1315                         goto err_reg_dev;
1316                 }
1317         }
1318
1319         clear_bit(MLX5_INTERFACE_STATE_DOWN, &dev->intf_state);
1320         set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1321 out:
1322         mutex_unlock(&dev->intf_state_mutex);
1323
1324         return 0;
1325
1326 err_reg_dev:
1327         mlx5_sriov_cleanup(dev);
1328
1329 err_sriov:
1330 #ifdef CONFIG_MLX5_CORE_EN
1331         mlx5_eswitch_cleanup(dev->priv.eswitch);
1332 #endif
1333         mlx5_cleanup_fs(dev);
1334
1335 err_fs:
1336         mlx5_irq_clear_affinity_hints(dev);
1337
1338 err_affinity_hints:
1339         free_comp_eqs(dev);
1340
1341 err_stop_eqs:
1342         mlx5_stop_eqs(dev);
1343
1344 err_free_uar:
1345         mlx5_free_uuars(dev, &priv->uuari);
1346
1347 err_disable_msix:
1348         mlx5_disable_msix(dev);
1349
1350 err_cleanup_once:
1351         if (boot)
1352                 mlx5_cleanup_once(dev);
1353
1354 err_stop_poll:
1355         mlx5_stop_health_poll(dev);
1356         if (mlx5_cmd_teardown_hca(dev)) {
1357                 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1358                 goto out_err;
1359         }
1360
1361 err_pagealloc_stop:
1362         mlx5_pagealloc_stop(dev);
1363
1364 reclaim_boot_pages:
1365         mlx5_reclaim_startup_pages(dev);
1366
1367 err_disable_hca:
1368         mlx5_core_disable_hca(dev, 0);
1369
1370 err_cmd_cleanup:
1371         mlx5_cmd_cleanup(dev);
1372
1373 out_err:
1374         dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
1375         mutex_unlock(&dev->intf_state_mutex);
1376
1377         return err;
1378 }
1379
1380 static int mlx5_unload_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
1381                            bool cleanup)
1382 {
1383         int err = 0;
1384
1385         mutex_lock(&dev->intf_state_mutex);
1386         if (test_bit(MLX5_INTERFACE_STATE_DOWN, &dev->intf_state)) {
1387                 dev_warn(&dev->pdev->dev, "%s: interface is down, NOP\n",
1388                          __func__);
1389                 if (cleanup)
1390                         mlx5_cleanup_once(dev);
1391                 goto out;
1392         }
1393
1394         if (mlx5_device_registered(dev))
1395                 mlx5_detach_device(dev);
1396
1397         mlx5_sriov_cleanup(dev);
1398 #ifdef CONFIG_MLX5_CORE_EN
1399         mlx5_eswitch_cleanup(dev->priv.eswitch);
1400 #endif
1401         mlx5_cleanup_fs(dev);
1402         mlx5_irq_clear_affinity_hints(dev);
1403         free_comp_eqs(dev);
1404         mlx5_stop_eqs(dev);
1405         mlx5_free_uuars(dev, &priv->uuari);
1406         mlx5_disable_msix(dev);
1407         if (cleanup)
1408                 mlx5_cleanup_once(dev);
1409         mlx5_stop_health_poll(dev);
1410         err = mlx5_cmd_teardown_hca(dev);
1411         if (err) {
1412                 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1413                 goto out;
1414         }
1415         mlx5_pagealloc_stop(dev);
1416         mlx5_reclaim_startup_pages(dev);
1417         mlx5_core_disable_hca(dev, 0);
1418         mlx5_cmd_cleanup(dev);
1419
1420 out:
1421         clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1422         set_bit(MLX5_INTERFACE_STATE_DOWN, &dev->intf_state);
1423         mutex_unlock(&dev->intf_state_mutex);
1424         return err;
1425 }
1426
1427 void mlx5_core_event(struct mlx5_core_dev *dev, enum mlx5_dev_event event,
1428                      unsigned long param)
1429 {
1430         struct mlx5_priv *priv = &dev->priv;
1431         struct mlx5_device_context *dev_ctx;
1432         unsigned long flags;
1433
1434         spin_lock_irqsave(&priv->ctx_lock, flags);
1435
1436         list_for_each_entry(dev_ctx, &priv->ctx_list, list)
1437                 if (dev_ctx->intf->event)
1438                         dev_ctx->intf->event(dev, dev_ctx->context, event, param);
1439
1440         spin_unlock_irqrestore(&priv->ctx_lock, flags);
1441 }
1442
1443 struct mlx5_core_event_handler {
1444         void (*event)(struct mlx5_core_dev *dev,
1445                       enum mlx5_dev_event event,
1446                       void *data);
1447 };
1448
1449 static const struct devlink_ops mlx5_devlink_ops = {
1450 #ifdef CONFIG_MLX5_CORE_EN
1451         .eswitch_mode_set = mlx5_devlink_eswitch_mode_set,
1452         .eswitch_mode_get = mlx5_devlink_eswitch_mode_get,
1453 #endif
1454 };
1455
1456 #define MLX5_IB_MOD "mlx5_ib"
1457 static int init_one(struct pci_dev *pdev,
1458                     const struct pci_device_id *id)
1459 {
1460         struct mlx5_core_dev *dev;
1461         struct devlink *devlink;
1462         struct mlx5_priv *priv;
1463         int err;
1464
1465         devlink = devlink_alloc(&mlx5_devlink_ops, sizeof(*dev));
1466         if (!devlink) {
1467                 dev_err(&pdev->dev, "kzalloc failed\n");
1468                 return -ENOMEM;
1469         }
1470
1471         dev = devlink_priv(devlink);
1472         priv = &dev->priv;
1473         priv->pci_dev_data = id->driver_data;
1474
1475         pci_set_drvdata(pdev, dev);
1476
1477         if (prof_sel < 0 || prof_sel >= ARRAY_SIZE(profile)) {
1478                 mlx5_core_warn(dev,
1479                                "selected profile out of range, selecting default (%d)\n",
1480                                MLX5_DEFAULT_PROF);
1481                 prof_sel = MLX5_DEFAULT_PROF;
1482         }
1483         dev->profile = &profile[prof_sel];
1484         dev->pdev = pdev;
1485         dev->event = mlx5_core_event;
1486
1487         INIT_LIST_HEAD(&priv->ctx_list);
1488         spin_lock_init(&priv->ctx_lock);
1489         mutex_init(&dev->pci_status_mutex);
1490         mutex_init(&dev->intf_state_mutex);
1491         err = mlx5_pci_init(dev, priv);
1492         if (err) {
1493                 dev_err(&pdev->dev, "mlx5_pci_init failed with error code %d\n", err);
1494                 goto clean_dev;
1495         }
1496
1497         err = mlx5_health_init(dev);
1498         if (err) {
1499                 dev_err(&pdev->dev, "mlx5_health_init failed with error code %d\n", err);
1500                 goto close_pci;
1501         }
1502
1503         mlx5_pagealloc_init(dev);
1504
1505         err = mlx5_load_one(dev, priv, true);
1506         if (err) {
1507                 dev_err(&pdev->dev, "mlx5_load_one failed with error code %d\n", err);
1508                 goto clean_health;
1509         }
1510
1511         err = request_module_nowait(MLX5_IB_MOD);
1512         if (err)
1513                 pr_info("failed request module on %s\n", MLX5_IB_MOD);
1514
1515         err = devlink_register(devlink, &pdev->dev);
1516         if (err)
1517                 goto clean_load;
1518
1519         return 0;
1520
1521 clean_load:
1522         mlx5_unload_one(dev, priv, true);
1523 clean_health:
1524         mlx5_pagealloc_cleanup(dev);
1525         mlx5_health_cleanup(dev);
1526 close_pci:
1527         mlx5_pci_close(dev, priv);
1528 clean_dev:
1529         pci_set_drvdata(pdev, NULL);
1530         devlink_free(devlink);
1531
1532         return err;
1533 }
1534
1535 static void remove_one(struct pci_dev *pdev)
1536 {
1537         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1538         struct devlink *devlink = priv_to_devlink(dev);
1539         struct mlx5_priv *priv = &dev->priv;
1540
1541         devlink_unregister(devlink);
1542         mlx5_unregister_device(dev);
1543
1544         if (mlx5_unload_one(dev, priv, true)) {
1545                 dev_err(&dev->pdev->dev, "mlx5_unload_one failed\n");
1546                 mlx5_health_cleanup(dev);
1547                 return;
1548         }
1549
1550         mlx5_pagealloc_cleanup(dev);
1551         mlx5_health_cleanup(dev);
1552         mlx5_pci_close(dev, priv);
1553         pci_set_drvdata(pdev, NULL);
1554         devlink_free(devlink);
1555 }
1556
1557 static pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev,
1558                                               pci_channel_state_t state)
1559 {
1560         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1561         struct mlx5_priv *priv = &dev->priv;
1562
1563         dev_info(&pdev->dev, "%s was called\n", __func__);
1564         mlx5_enter_error_state(dev);
1565         mlx5_unload_one(dev, priv, false);
1566         pci_save_state(pdev);
1567         mlx5_pci_disable_device(dev);
1568         return state == pci_channel_io_perm_failure ?
1569                 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1570 }
1571
1572 /* wait for the device to show vital signs by waiting
1573  * for the health counter to start counting.
1574  */
1575 static int wait_vital(struct pci_dev *pdev)
1576 {
1577         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1578         struct mlx5_core_health *health = &dev->priv.health;
1579         const int niter = 100;
1580         u32 last_count = 0;
1581         u32 count;
1582         int i;
1583
1584         for (i = 0; i < niter; i++) {
1585                 count = ioread32be(health->health_counter);
1586                 if (count && count != 0xffffffff) {
1587                         if (last_count && last_count != count) {
1588                                 dev_info(&pdev->dev, "Counter value 0x%x after %d iterations\n", count, i);
1589                                 return 0;
1590                         }
1591                         last_count = count;
1592                 }
1593                 msleep(50);
1594         }
1595
1596         return -ETIMEDOUT;
1597 }
1598
1599 static pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev)
1600 {
1601         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1602         int err;
1603
1604         dev_info(&pdev->dev, "%s was called\n", __func__);
1605
1606         err = mlx5_pci_enable_device(dev);
1607         if (err) {
1608                 dev_err(&pdev->dev, "%s: mlx5_pci_enable_device failed with error code: %d\n"
1609                         , __func__, err);
1610                 return PCI_ERS_RESULT_DISCONNECT;
1611         }
1612
1613         pci_set_master(pdev);
1614         pci_restore_state(pdev);
1615
1616         if (wait_vital(pdev)) {
1617                 dev_err(&pdev->dev, "%s: wait_vital timed out\n", __func__);
1618                 return PCI_ERS_RESULT_DISCONNECT;
1619         }
1620
1621         return PCI_ERS_RESULT_RECOVERED;
1622 }
1623
1624 void mlx5_disable_device(struct mlx5_core_dev *dev)
1625 {
1626         mlx5_pci_err_detected(dev->pdev, 0);
1627 }
1628
1629 static void mlx5_pci_resume(struct pci_dev *pdev)
1630 {
1631         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1632         struct mlx5_priv *priv = &dev->priv;
1633         int err;
1634
1635         dev_info(&pdev->dev, "%s was called\n", __func__);
1636
1637         err = mlx5_load_one(dev, priv, false);
1638         if (err)
1639                 dev_err(&pdev->dev, "%s: mlx5_load_one failed with error code: %d\n"
1640                         , __func__, err);
1641         else
1642                 dev_info(&pdev->dev, "%s: device recovered\n", __func__);
1643 }
1644
1645 static const struct pci_error_handlers mlx5_err_handler = {
1646         .error_detected = mlx5_pci_err_detected,
1647         .slot_reset     = mlx5_pci_slot_reset,
1648         .resume         = mlx5_pci_resume
1649 };
1650
1651 static void shutdown(struct pci_dev *pdev)
1652 {
1653         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1654         struct mlx5_priv *priv = &dev->priv;
1655
1656         dev_info(&pdev->dev, "Shutdown was called\n");
1657         /* Notify mlx5 clients that the kernel is being shut down */
1658         set_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &dev->intf_state);
1659         mlx5_unload_one(dev, priv, false);
1660         mlx5_pci_disable_device(dev);
1661 }
1662
1663 static const struct pci_device_id mlx5_core_pci_table[] = {
1664         { PCI_VDEVICE(MELLANOX, 0x1011) },                      /* Connect-IB */
1665         { PCI_VDEVICE(MELLANOX, 0x1012), MLX5_PCI_DEV_IS_VF},   /* Connect-IB VF */
1666         { PCI_VDEVICE(MELLANOX, 0x1013) },                      /* ConnectX-4 */
1667         { PCI_VDEVICE(MELLANOX, 0x1014), MLX5_PCI_DEV_IS_VF},   /* ConnectX-4 VF */
1668         { PCI_VDEVICE(MELLANOX, 0x1015) },                      /* ConnectX-4LX */
1669         { PCI_VDEVICE(MELLANOX, 0x1016), MLX5_PCI_DEV_IS_VF},   /* ConnectX-4LX VF */
1670         { PCI_VDEVICE(MELLANOX, 0x1017) },                      /* ConnectX-5, PCIe 3.0 */
1671         { PCI_VDEVICE(MELLANOX, 0x1018), MLX5_PCI_DEV_IS_VF},   /* ConnectX-5 VF */
1672         { PCI_VDEVICE(MELLANOX, 0x1019) },                      /* ConnectX-5, PCIe 4.0 */
1673         { 0, }
1674 };
1675
1676 MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
1677
1678 static struct pci_driver mlx5_core_driver = {
1679         .name           = DRIVER_NAME,
1680         .id_table       = mlx5_core_pci_table,
1681         .probe          = init_one,
1682         .remove         = remove_one,
1683         .shutdown       = shutdown,
1684         .err_handler    = &mlx5_err_handler,
1685         .sriov_configure   = mlx5_core_sriov_configure,
1686 };
1687
1688 static int __init init(void)
1689 {
1690         int err;
1691
1692         mlx5_register_debugfs();
1693
1694         err = pci_register_driver(&mlx5_core_driver);
1695         if (err)
1696                 goto err_debug;
1697
1698 #ifdef CONFIG_MLX5_CORE_EN
1699         mlx5e_init();
1700 #endif
1701
1702         return 0;
1703
1704 err_debug:
1705         mlx5_unregister_debugfs();
1706         return err;
1707 }
1708
1709 static void __exit cleanup(void)
1710 {
1711 #ifdef CONFIG_MLX5_CORE_EN
1712         mlx5e_cleanup();
1713 #endif
1714         pci_unregister_driver(&mlx5_core_driver);
1715         mlx5_unregister_debugfs();
1716 }
1717
1718 module_init(init);
1719 module_exit(cleanup);