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