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