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