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