Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[platform/kernel/linux-exynos.git] / drivers / net / ethernet / mellanox / mlx5 / core / main.c
1 /*
2  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/highmem.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/errno.h>
37 #include <linux/pci.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/slab.h>
40 #include <linux/io-mapping.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/mlx5/driver.h>
44 #include <linux/mlx5/cq.h>
45 #include <linux/mlx5/qp.h>
46 #include <linux/mlx5/srq.h>
47 #include <linux/debugfs.h>
48 #include <linux/kmod.h>
49 #include <linux/mlx5/mlx5_ifc.h>
50 #include <linux/mlx5/vport.h>
51 #ifdef CONFIG_RFS_ACCEL
52 #include <linux/cpu_rmap.h>
53 #endif
54 #include <net/devlink.h>
55 #include "mlx5_core.h"
56 #include "fs_core.h"
57 #ifdef CONFIG_MLX5_CORE_EN
58 #include "eswitch.h"
59 #endif
60 #include "lib/mlx5.h"
61 #include "fpga/core.h"
62 #include "accel/ipsec.h"
63
64 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
65 MODULE_DESCRIPTION("Mellanox Connect-IB, ConnectX-4 core driver");
66 MODULE_LICENSE("Dual BSD/GPL");
67 MODULE_VERSION(DRIVER_VERSION);
68
69 unsigned int mlx5_core_debug_mask;
70 module_param_named(debug_mask, mlx5_core_debug_mask, uint, 0644);
71 MODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
72
73 #define MLX5_DEFAULT_PROF       2
74 static unsigned int prof_sel = MLX5_DEFAULT_PROF;
75 module_param_named(prof_sel, prof_sel, uint, 0444);
76 MODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");
77
78 enum {
79         MLX5_ATOMIC_REQ_MODE_BE = 0x0,
80         MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS = 0x1,
81 };
82
83 static struct mlx5_profile profile[] = {
84         [0] = {
85                 .mask           = 0,
86         },
87         [1] = {
88                 .mask           = MLX5_PROF_MASK_QP_SIZE,
89                 .log_max_qp     = 12,
90         },
91         [2] = {
92                 .mask           = MLX5_PROF_MASK_QP_SIZE |
93                                   MLX5_PROF_MASK_MR_CACHE,
94                 .log_max_qp     = 18,
95                 .mr_cache[0]    = {
96                         .size   = 500,
97                         .limit  = 250
98                 },
99                 .mr_cache[1]    = {
100                         .size   = 500,
101                         .limit  = 250
102                 },
103                 .mr_cache[2]    = {
104                         .size   = 500,
105                         .limit  = 250
106                 },
107                 .mr_cache[3]    = {
108                         .size   = 500,
109                         .limit  = 250
110                 },
111                 .mr_cache[4]    = {
112                         .size   = 500,
113                         .limit  = 250
114                 },
115                 .mr_cache[5]    = {
116                         .size   = 500,
117                         .limit  = 250
118                 },
119                 .mr_cache[6]    = {
120                         .size   = 500,
121                         .limit  = 250
122                 },
123                 .mr_cache[7]    = {
124                         .size   = 500,
125                         .limit  = 250
126                 },
127                 .mr_cache[8]    = {
128                         .size   = 500,
129                         .limit  = 250
130                 },
131                 .mr_cache[9]    = {
132                         .size   = 500,
133                         .limit  = 250
134                 },
135                 .mr_cache[10]   = {
136                         .size   = 500,
137                         .limit  = 250
138                 },
139                 .mr_cache[11]   = {
140                         .size   = 500,
141                         .limit  = 250
142                 },
143                 .mr_cache[12]   = {
144                         .size   = 64,
145                         .limit  = 32
146                 },
147                 .mr_cache[13]   = {
148                         .size   = 32,
149                         .limit  = 16
150                 },
151                 .mr_cache[14]   = {
152                         .size   = 16,
153                         .limit  = 8
154                 },
155                 .mr_cache[15]   = {
156                         .size   = 8,
157                         .limit  = 4
158                 },
159                 .mr_cache[16]   = {
160                         .size   = 8,
161                         .limit  = 4
162                 },
163                 .mr_cache[17]   = {
164                         .size   = 8,
165                         .limit  = 4
166                 },
167                 .mr_cache[18]   = {
168                         .size   = 8,
169                         .limit  = 4
170                 },
171                 .mr_cache[19]   = {
172                         .size   = 4,
173                         .limit  = 2
174                 },
175                 .mr_cache[20]   = {
176                         .size   = 4,
177                         .limit  = 2
178                 },
179         },
180 };
181
182 #define FW_INIT_TIMEOUT_MILI            2000
183 #define FW_INIT_WAIT_MS                 2
184 #define FW_PRE_INIT_TIMEOUT_MILI        10000
185
186 static int wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili)
187 {
188         unsigned long end = jiffies + msecs_to_jiffies(max_wait_mili);
189         int err = 0;
190
191         while (fw_initializing(dev)) {
192                 if (time_after(jiffies, end)) {
193                         err = -EBUSY;
194                         break;
195                 }
196                 msleep(FW_INIT_WAIT_MS);
197         }
198
199         return err;
200 }
201
202 static void mlx5_set_driver_version(struct mlx5_core_dev *dev)
203 {
204         int driver_ver_sz = MLX5_FLD_SZ_BYTES(set_driver_version_in,
205                                               driver_version);
206         u8 in[MLX5_ST_SZ_BYTES(set_driver_version_in)] = {0};
207         u8 out[MLX5_ST_SZ_BYTES(set_driver_version_out)] = {0};
208         int remaining_size = driver_ver_sz;
209         char *string;
210
211         if (!MLX5_CAP_GEN(dev, driver_version))
212                 return;
213
214         string = MLX5_ADDR_OF(set_driver_version_in, in, driver_version);
215
216         strncpy(string, "Linux", remaining_size);
217
218         remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
219         strncat(string, ",", remaining_size);
220
221         remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
222         strncat(string, DRIVER_NAME, remaining_size);
223
224         remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
225         strncat(string, ",", remaining_size);
226
227         remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
228         strncat(string, DRIVER_VERSION, remaining_size);
229
230         /*Send the command*/
231         MLX5_SET(set_driver_version_in, in, opcode,
232                  MLX5_CMD_OP_SET_DRIVER_VERSION);
233
234         mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
235 }
236
237 static int set_dma_caps(struct pci_dev *pdev)
238 {
239         int err;
240
241         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
242         if (err) {
243                 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
244                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
245                 if (err) {
246                         dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
247                         return err;
248                 }
249         }
250
251         err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
252         if (err) {
253                 dev_warn(&pdev->dev,
254                          "Warning: couldn't set 64-bit consistent PCI DMA mask\n");
255                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
256                 if (err) {
257                         dev_err(&pdev->dev,
258                                 "Can't set consistent PCI DMA mask, aborting\n");
259                         return err;
260                 }
261         }
262
263         dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
264         return err;
265 }
266
267 static int mlx5_pci_enable_device(struct mlx5_core_dev *dev)
268 {
269         struct pci_dev *pdev = dev->pdev;
270         int err = 0;
271
272         mutex_lock(&dev->pci_status_mutex);
273         if (dev->pci_status == MLX5_PCI_STATUS_DISABLED) {
274                 err = pci_enable_device(pdev);
275                 if (!err)
276                         dev->pci_status = MLX5_PCI_STATUS_ENABLED;
277         }
278         mutex_unlock(&dev->pci_status_mutex);
279
280         return err;
281 }
282
283 static void mlx5_pci_disable_device(struct mlx5_core_dev *dev)
284 {
285         struct pci_dev *pdev = dev->pdev;
286
287         mutex_lock(&dev->pci_status_mutex);
288         if (dev->pci_status == MLX5_PCI_STATUS_ENABLED) {
289                 pci_disable_device(pdev);
290                 dev->pci_status = MLX5_PCI_STATUS_DISABLED;
291         }
292         mutex_unlock(&dev->pci_status_mutex);
293 }
294
295 static int request_bar(struct pci_dev *pdev)
296 {
297         int err = 0;
298
299         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
300                 dev_err(&pdev->dev, "Missing registers BAR, aborting\n");
301                 return -ENODEV;
302         }
303
304         err = pci_request_regions(pdev, DRIVER_NAME);
305         if (err)
306                 dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
307
308         return err;
309 }
310
311 static void release_bar(struct pci_dev *pdev)
312 {
313         pci_release_regions(pdev);
314 }
315
316 static int mlx5_alloc_irq_vectors(struct mlx5_core_dev *dev)
317 {
318         struct mlx5_priv *priv = &dev->priv;
319         struct mlx5_eq_table *table = &priv->eq_table;
320         struct irq_affinity irqdesc = {
321                 .pre_vectors = MLX5_EQ_VEC_COMP_BASE,
322         };
323         int num_eqs = 1 << MLX5_CAP_GEN(dev, log_max_eq);
324         int nvec;
325
326         nvec = MLX5_CAP_GEN(dev, num_ports) * num_online_cpus() +
327                MLX5_EQ_VEC_COMP_BASE;
328         nvec = min_t(int, nvec, num_eqs);
329         if (nvec <= MLX5_EQ_VEC_COMP_BASE)
330                 return -ENOMEM;
331
332         priv->irq_info = kcalloc(nvec, sizeof(*priv->irq_info), GFP_KERNEL);
333         if (!priv->irq_info)
334                 goto err_free_msix;
335
336         nvec = pci_alloc_irq_vectors_affinity(dev->pdev,
337                         MLX5_EQ_VEC_COMP_BASE + 1, nvec,
338                         PCI_IRQ_MSIX | PCI_IRQ_AFFINITY,
339                         &irqdesc);
340         if (nvec < 0)
341                 return nvec;
342
343         table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
344
345         return 0;
346
347 err_free_msix:
348         kfree(priv->irq_info);
349         return -ENOMEM;
350 }
351
352 static void mlx5_free_irq_vectors(struct mlx5_core_dev *dev)
353 {
354         struct mlx5_priv *priv = &dev->priv;
355
356         pci_free_irq_vectors(dev->pdev);
357         kfree(priv->irq_info);
358 }
359
360 struct mlx5_reg_host_endianness {
361         u8      he;
362         u8      rsvd[15];
363 };
364
365 #define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos))
366
367 enum {
368         MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) |
369                                 MLX5_DEV_CAP_FLAG_DCT,
370 };
371
372 static u16 to_fw_pkey_sz(struct mlx5_core_dev *dev, u32 size)
373 {
374         switch (size) {
375         case 128:
376                 return 0;
377         case 256:
378                 return 1;
379         case 512:
380                 return 2;
381         case 1024:
382                 return 3;
383         case 2048:
384                 return 4;
385         case 4096:
386                 return 5;
387         default:
388                 mlx5_core_warn(dev, "invalid pkey table size %d\n", size);
389                 return 0;
390         }
391 }
392
393 static int mlx5_core_get_caps_mode(struct mlx5_core_dev *dev,
394                                    enum mlx5_cap_type cap_type,
395                                    enum mlx5_cap_mode cap_mode)
396 {
397         u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
398         int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
399         void *out, *hca_caps;
400         u16 opmod = (cap_type << 1) | (cap_mode & 0x01);
401         int err;
402
403         memset(in, 0, sizeof(in));
404         out = kzalloc(out_sz, GFP_KERNEL);
405         if (!out)
406                 return -ENOMEM;
407
408         MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
409         MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
410         err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
411         if (err) {
412                 mlx5_core_warn(dev,
413                                "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n",
414                                cap_type, cap_mode, err);
415                 goto query_ex;
416         }
417
418         hca_caps =  MLX5_ADDR_OF(query_hca_cap_out, out, capability);
419
420         switch (cap_mode) {
421         case HCA_CAP_OPMOD_GET_MAX:
422                 memcpy(dev->caps.hca_max[cap_type], hca_caps,
423                        MLX5_UN_SZ_BYTES(hca_cap_union));
424                 break;
425         case HCA_CAP_OPMOD_GET_CUR:
426                 memcpy(dev->caps.hca_cur[cap_type], hca_caps,
427                        MLX5_UN_SZ_BYTES(hca_cap_union));
428                 break;
429         default:
430                 mlx5_core_warn(dev,
431                                "Tried to query dev cap type(%x) with wrong opmode(%x)\n",
432                                cap_type, cap_mode);
433                 err = -EINVAL;
434                 break;
435         }
436 query_ex:
437         kfree(out);
438         return err;
439 }
440
441 int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type)
442 {
443         int ret;
444
445         ret = mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_CUR);
446         if (ret)
447                 return ret;
448         return mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_MAX);
449 }
450
451 static int set_caps(struct mlx5_core_dev *dev, void *in, int in_sz, int opmod)
452 {
453         u32 out[MLX5_ST_SZ_DW(set_hca_cap_out)] = {0};
454
455         MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
456         MLX5_SET(set_hca_cap_in, in, op_mod, opmod << 1);
457         return mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
458 }
459
460 static int handle_hca_cap_atomic(struct mlx5_core_dev *dev)
461 {
462         void *set_ctx;
463         void *set_hca_cap;
464         int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
465         int req_endianness;
466         int err;
467
468         if (MLX5_CAP_GEN(dev, atomic)) {
469                 err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC);
470                 if (err)
471                         return err;
472         } else {
473                 return 0;
474         }
475
476         req_endianness =
477                 MLX5_CAP_ATOMIC(dev,
478                                 supported_atomic_req_8B_endianness_mode_1);
479
480         if (req_endianness != MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS)
481                 return 0;
482
483         set_ctx = kzalloc(set_sz, GFP_KERNEL);
484         if (!set_ctx)
485                 return -ENOMEM;
486
487         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
488
489         /* Set requestor to host endianness */
490         MLX5_SET(atomic_caps, set_hca_cap, atomic_req_8B_endianness_mode,
491                  MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS);
492
493         err = set_caps(dev, set_ctx, set_sz, MLX5_SET_HCA_CAP_OP_MOD_ATOMIC);
494
495         kfree(set_ctx);
496         return err;
497 }
498
499 static int handle_hca_cap(struct mlx5_core_dev *dev)
500 {
501         void *set_ctx = NULL;
502         struct mlx5_profile *prof = dev->profile;
503         int err = -ENOMEM;
504         int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
505         void *set_hca_cap;
506
507         set_ctx = kzalloc(set_sz, GFP_KERNEL);
508         if (!set_ctx)
509                 goto query_ex;
510
511         err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL);
512         if (err)
513                 goto query_ex;
514
515         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
516                                    capability);
517         memcpy(set_hca_cap, dev->caps.hca_cur[MLX5_CAP_GENERAL],
518                MLX5_ST_SZ_BYTES(cmd_hca_cap));
519
520         mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
521                       mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
522                       128);
523         /* we limit the size of the pkey table to 128 entries for now */
524         MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
525                  to_fw_pkey_sz(dev, 128));
526
527         /* Check log_max_qp from HCA caps to set in current profile */
528         if (MLX5_CAP_GEN_MAX(dev, log_max_qp) < profile[prof_sel].log_max_qp) {
529                 mlx5_core_warn(dev, "log_max_qp value in current profile is %d, changing it to HCA capability limit (%d)\n",
530                                profile[prof_sel].log_max_qp,
531                                MLX5_CAP_GEN_MAX(dev, log_max_qp));
532                 profile[prof_sel].log_max_qp = MLX5_CAP_GEN_MAX(dev, log_max_qp);
533         }
534         if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
535                 MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
536                          prof->log_max_qp);
537
538         /* disable cmdif checksum */
539         MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
540
541         /* Enable 4K UAR only when HCA supports it and page size is bigger
542          * than 4K.
543          */
544         if (MLX5_CAP_GEN_MAX(dev, uar_4k) && PAGE_SIZE > 4096)
545                 MLX5_SET(cmd_hca_cap, set_hca_cap, uar_4k, 1);
546
547         MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12);
548
549         if (MLX5_CAP_GEN_MAX(dev, cache_line_128byte))
550                 MLX5_SET(cmd_hca_cap,
551                          set_hca_cap,
552                          cache_line_128byte,
553                          cache_line_size() == 128 ? 1 : 0);
554
555         err = set_caps(dev, set_ctx, set_sz,
556                        MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE);
557
558 query_ex:
559         kfree(set_ctx);
560         return err;
561 }
562
563 static int set_hca_ctrl(struct mlx5_core_dev *dev)
564 {
565         struct mlx5_reg_host_endianness he_in;
566         struct mlx5_reg_host_endianness he_out;
567         int err;
568
569         if (!mlx5_core_is_pf(dev))
570                 return 0;
571
572         memset(&he_in, 0, sizeof(he_in));
573         he_in.he = MLX5_SET_HOST_ENDIANNESS;
574         err = mlx5_core_access_reg(dev, &he_in,  sizeof(he_in),
575                                         &he_out, sizeof(he_out),
576                                         MLX5_REG_HOST_ENDIANNESS, 0, 1);
577         return err;
578 }
579
580 static int mlx5_core_set_hca_defaults(struct mlx5_core_dev *dev)
581 {
582         int ret = 0;
583
584         /* Disable local_lb by default */
585         if ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
586             MLX5_CAP_GEN(dev, disable_local_lb))
587                 ret = mlx5_nic_vport_update_local_lb(dev, false);
588
589         return ret;
590 }
591
592 int mlx5_core_enable_hca(struct mlx5_core_dev *dev, u16 func_id)
593 {
594         u32 out[MLX5_ST_SZ_DW(enable_hca_out)] = {0};
595         u32 in[MLX5_ST_SZ_DW(enable_hca_in)]   = {0};
596
597         MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA);
598         MLX5_SET(enable_hca_in, in, function_id, func_id);
599         return mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
600 }
601
602 int mlx5_core_disable_hca(struct mlx5_core_dev *dev, u16 func_id)
603 {
604         u32 out[MLX5_ST_SZ_DW(disable_hca_out)] = {0};
605         u32 in[MLX5_ST_SZ_DW(disable_hca_in)]   = {0};
606
607         MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA);
608         MLX5_SET(disable_hca_in, in, function_id, func_id);
609         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
610 }
611
612 u64 mlx5_read_internal_timer(struct mlx5_core_dev *dev)
613 {
614         u32 timer_h, timer_h1, timer_l;
615
616         timer_h = ioread32be(&dev->iseg->internal_timer_h);
617         timer_l = ioread32be(&dev->iseg->internal_timer_l);
618         timer_h1 = ioread32be(&dev->iseg->internal_timer_h);
619         if (timer_h != timer_h1) /* wrap around */
620                 timer_l = ioread32be(&dev->iseg->internal_timer_l);
621
622         return (u64)timer_l | (u64)timer_h1 << 32;
623 }
624
625 int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn,
626                     unsigned int *irqn)
627 {
628         struct mlx5_eq_table *table = &dev->priv.eq_table;
629         struct mlx5_eq *eq, *n;
630         int err = -ENOENT;
631
632         spin_lock(&table->lock);
633         list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
634                 if (eq->index == vector) {
635                         *eqn = eq->eqn;
636                         *irqn = eq->irqn;
637                         err = 0;
638                         break;
639                 }
640         }
641         spin_unlock(&table->lock);
642
643         return err;
644 }
645 EXPORT_SYMBOL(mlx5_vector2eqn);
646
647 struct mlx5_eq *mlx5_eqn2eq(struct mlx5_core_dev *dev, int eqn)
648 {
649         struct mlx5_eq_table *table = &dev->priv.eq_table;
650         struct mlx5_eq *eq;
651
652         spin_lock(&table->lock);
653         list_for_each_entry(eq, &table->comp_eqs_list, list)
654                 if (eq->eqn == eqn) {
655                         spin_unlock(&table->lock);
656                         return eq;
657                 }
658
659         spin_unlock(&table->lock);
660
661         return ERR_PTR(-ENOENT);
662 }
663
664 static void free_comp_eqs(struct mlx5_core_dev *dev)
665 {
666         struct mlx5_eq_table *table = &dev->priv.eq_table;
667         struct mlx5_eq *eq, *n;
668
669 #ifdef CONFIG_RFS_ACCEL
670         if (dev->rmap) {
671                 free_irq_cpu_rmap(dev->rmap);
672                 dev->rmap = NULL;
673         }
674 #endif
675         spin_lock(&table->lock);
676         list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
677                 list_del(&eq->list);
678                 spin_unlock(&table->lock);
679                 if (mlx5_destroy_unmap_eq(dev, eq))
680                         mlx5_core_warn(dev, "failed to destroy EQ 0x%x\n",
681                                        eq->eqn);
682                 kfree(eq);
683                 spin_lock(&table->lock);
684         }
685         spin_unlock(&table->lock);
686 }
687
688 static int alloc_comp_eqs(struct mlx5_core_dev *dev)
689 {
690         struct mlx5_eq_table *table = &dev->priv.eq_table;
691         char name[MLX5_MAX_IRQ_NAME];
692         struct mlx5_eq *eq;
693         int ncomp_vec;
694         int nent;
695         int err;
696         int i;
697
698         INIT_LIST_HEAD(&table->comp_eqs_list);
699         ncomp_vec = table->num_comp_vectors;
700         nent = MLX5_COMP_EQ_SIZE;
701 #ifdef CONFIG_RFS_ACCEL
702         dev->rmap = alloc_irq_cpu_rmap(ncomp_vec);
703         if (!dev->rmap)
704                 return -ENOMEM;
705 #endif
706         for (i = 0; i < ncomp_vec; i++) {
707                 eq = kzalloc(sizeof(*eq), GFP_KERNEL);
708                 if (!eq) {
709                         err = -ENOMEM;
710                         goto clean;
711                 }
712
713 #ifdef CONFIG_RFS_ACCEL
714                 irq_cpu_rmap_add(dev->rmap, pci_irq_vector(dev->pdev,
715                                  MLX5_EQ_VEC_COMP_BASE + i));
716 #endif
717                 snprintf(name, MLX5_MAX_IRQ_NAME, "mlx5_comp%d", i);
718                 err = mlx5_create_map_eq(dev, eq,
719                                          i + MLX5_EQ_VEC_COMP_BASE, nent, 0,
720                                          name, MLX5_EQ_TYPE_COMP);
721                 if (err) {
722                         kfree(eq);
723                         goto clean;
724                 }
725                 mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->eqn);
726                 eq->index = i;
727                 spin_lock(&table->lock);
728                 list_add_tail(&eq->list, &table->comp_eqs_list);
729                 spin_unlock(&table->lock);
730         }
731
732         return 0;
733
734 clean:
735         free_comp_eqs(dev);
736         return err;
737 }
738
739 static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
740 {
741         u32 query_in[MLX5_ST_SZ_DW(query_issi_in)]   = {0};
742         u32 query_out[MLX5_ST_SZ_DW(query_issi_out)] = {0};
743         u32 sup_issi;
744         int err;
745
746         MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
747         err = mlx5_cmd_exec(dev, query_in, sizeof(query_in),
748                             query_out, sizeof(query_out));
749         if (err) {
750                 u32 syndrome;
751                 u8 status;
752
753                 mlx5_cmd_mbox_status(query_out, &status, &syndrome);
754                 if (!status || syndrome == MLX5_DRIVER_SYND) {
755                         mlx5_core_err(dev, "Failed to query ISSI err(%d) status(%d) synd(%d)\n",
756                                       err, status, syndrome);
757                         return err;
758                 }
759
760                 mlx5_core_warn(dev, "Query ISSI is not supported by FW, ISSI is 0\n");
761                 dev->issi = 0;
762                 return 0;
763         }
764
765         sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);
766
767         if (sup_issi & (1 << 1)) {
768                 u32 set_in[MLX5_ST_SZ_DW(set_issi_in)]   = {0};
769                 u32 set_out[MLX5_ST_SZ_DW(set_issi_out)] = {0};
770
771                 MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
772                 MLX5_SET(set_issi_in, set_in, current_issi, 1);
773                 err = mlx5_cmd_exec(dev, set_in, sizeof(set_in),
774                                     set_out, sizeof(set_out));
775                 if (err) {
776                         mlx5_core_err(dev, "Failed to set ISSI to 1 err(%d)\n",
777                                       err);
778                         return err;
779                 }
780
781                 dev->issi = 1;
782
783                 return 0;
784         } else if (sup_issi & (1 << 0) || !sup_issi) {
785                 return 0;
786         }
787
788         return -EOPNOTSUPP;
789 }
790
791
792 static int mlx5_pci_init(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
793 {
794         struct pci_dev *pdev = dev->pdev;
795         int err = 0;
796
797         pci_set_drvdata(dev->pdev, dev);
798         strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
799         priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
800
801         mutex_init(&priv->pgdir_mutex);
802         INIT_LIST_HEAD(&priv->pgdir_list);
803         spin_lock_init(&priv->mkey_lock);
804
805         mutex_init(&priv->alloc_mutex);
806
807         priv->numa_node = dev_to_node(&dev->pdev->dev);
808
809         priv->dbg_root = debugfs_create_dir(dev_name(&pdev->dev), mlx5_debugfs_root);
810         if (!priv->dbg_root)
811                 return -ENOMEM;
812
813         err = mlx5_pci_enable_device(dev);
814         if (err) {
815                 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
816                 goto err_dbg;
817         }
818
819         err = request_bar(pdev);
820         if (err) {
821                 dev_err(&pdev->dev, "error requesting BARs, aborting\n");
822                 goto err_disable;
823         }
824
825         pci_set_master(pdev);
826
827         err = set_dma_caps(pdev);
828         if (err) {
829                 dev_err(&pdev->dev, "Failed setting DMA capabilities mask, aborting\n");
830                 goto err_clr_master;
831         }
832
833         dev->iseg_base = pci_resource_start(dev->pdev, 0);
834         dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
835         if (!dev->iseg) {
836                 err = -ENOMEM;
837                 dev_err(&pdev->dev, "Failed mapping initialization segment, aborting\n");
838                 goto err_clr_master;
839         }
840
841         return 0;
842
843 err_clr_master:
844         pci_clear_master(dev->pdev);
845         release_bar(dev->pdev);
846 err_disable:
847         mlx5_pci_disable_device(dev);
848
849 err_dbg:
850         debugfs_remove(priv->dbg_root);
851         return err;
852 }
853
854 static void mlx5_pci_close(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
855 {
856         iounmap(dev->iseg);
857         pci_clear_master(dev->pdev);
858         release_bar(dev->pdev);
859         mlx5_pci_disable_device(dev);
860         debugfs_remove(priv->dbg_root);
861 }
862
863 static int mlx5_init_once(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
864 {
865         struct pci_dev *pdev = dev->pdev;
866         int err;
867
868         err = mlx5_query_board_id(dev);
869         if (err) {
870                 dev_err(&pdev->dev, "query board id failed\n");
871                 goto out;
872         }
873
874         err = mlx5_eq_init(dev);
875         if (err) {
876                 dev_err(&pdev->dev, "failed to initialize eq\n");
877                 goto out;
878         }
879
880         err = mlx5_init_cq_table(dev);
881         if (err) {
882                 dev_err(&pdev->dev, "failed to initialize cq table\n");
883                 goto err_eq_cleanup;
884         }
885
886         mlx5_init_qp_table(dev);
887
888         mlx5_init_srq_table(dev);
889
890         mlx5_init_mkey_table(dev);
891
892         mlx5_init_reserved_gids(dev);
893
894         err = mlx5_init_rl_table(dev);
895         if (err) {
896                 dev_err(&pdev->dev, "Failed to init rate limiting\n");
897                 goto err_tables_cleanup;
898         }
899
900 #ifdef CONFIG_MLX5_CORE_EN
901         err = mlx5_eswitch_init(dev);
902         if (err) {
903                 dev_err(&pdev->dev, "Failed to init eswitch %d\n", err);
904                 goto err_rl_cleanup;
905         }
906 #endif
907
908         err = mlx5_sriov_init(dev);
909         if (err) {
910                 dev_err(&pdev->dev, "Failed to init sriov %d\n", err);
911                 goto err_eswitch_cleanup;
912         }
913
914         err = mlx5_fpga_init(dev);
915         if (err) {
916                 dev_err(&pdev->dev, "Failed to init fpga device %d\n", err);
917                 goto err_sriov_cleanup;
918         }
919
920         return 0;
921
922 err_sriov_cleanup:
923         mlx5_sriov_cleanup(dev);
924 err_eswitch_cleanup:
925 #ifdef CONFIG_MLX5_CORE_EN
926         mlx5_eswitch_cleanup(dev->priv.eswitch);
927
928 err_rl_cleanup:
929 #endif
930         mlx5_cleanup_rl_table(dev);
931
932 err_tables_cleanup:
933         mlx5_cleanup_mkey_table(dev);
934         mlx5_cleanup_srq_table(dev);
935         mlx5_cleanup_qp_table(dev);
936         mlx5_cleanup_cq_table(dev);
937
938 err_eq_cleanup:
939         mlx5_eq_cleanup(dev);
940
941 out:
942         return err;
943 }
944
945 static void mlx5_cleanup_once(struct mlx5_core_dev *dev)
946 {
947         mlx5_fpga_cleanup(dev);
948         mlx5_sriov_cleanup(dev);
949 #ifdef CONFIG_MLX5_CORE_EN
950         mlx5_eswitch_cleanup(dev->priv.eswitch);
951 #endif
952         mlx5_cleanup_rl_table(dev);
953         mlx5_cleanup_reserved_gids(dev);
954         mlx5_cleanup_mkey_table(dev);
955         mlx5_cleanup_srq_table(dev);
956         mlx5_cleanup_qp_table(dev);
957         mlx5_cleanup_cq_table(dev);
958         mlx5_eq_cleanup(dev);
959 }
960
961 static int mlx5_load_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
962                          bool boot)
963 {
964         struct pci_dev *pdev = dev->pdev;
965         int err;
966
967         mutex_lock(&dev->intf_state_mutex);
968         if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
969                 dev_warn(&dev->pdev->dev, "%s: interface is up, NOP\n",
970                          __func__);
971                 goto out;
972         }
973
974         dev_info(&pdev->dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
975                  fw_rev_min(dev), fw_rev_sub(dev));
976
977         /* on load removing any previous indication of internal error, device is
978          * up
979          */
980         dev->state = MLX5_DEVICE_STATE_UP;
981
982         /* wait for firmware to accept initialization segments configurations
983          */
984         err = wait_fw_init(dev, FW_PRE_INIT_TIMEOUT_MILI);
985         if (err) {
986                 dev_err(&dev->pdev->dev, "Firmware over %d MS in pre-initializing state, aborting\n",
987                         FW_PRE_INIT_TIMEOUT_MILI);
988                 goto out_err;
989         }
990
991         err = mlx5_cmd_init(dev);
992         if (err) {
993                 dev_err(&pdev->dev, "Failed initializing command interface, aborting\n");
994                 goto out_err;
995         }
996
997         err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI);
998         if (err) {
999                 dev_err(&dev->pdev->dev, "Firmware over %d MS in initializing state, aborting\n",
1000                         FW_INIT_TIMEOUT_MILI);
1001                 goto err_cmd_cleanup;
1002         }
1003
1004         err = mlx5_core_enable_hca(dev, 0);
1005         if (err) {
1006                 dev_err(&pdev->dev, "enable hca failed\n");
1007                 goto err_cmd_cleanup;
1008         }
1009
1010         err = mlx5_core_set_issi(dev);
1011         if (err) {
1012                 dev_err(&pdev->dev, "failed to set issi\n");
1013                 goto err_disable_hca;
1014         }
1015
1016         err = mlx5_satisfy_startup_pages(dev, 1);
1017         if (err) {
1018                 dev_err(&pdev->dev, "failed to allocate boot pages\n");
1019                 goto err_disable_hca;
1020         }
1021
1022         err = set_hca_ctrl(dev);
1023         if (err) {
1024                 dev_err(&pdev->dev, "set_hca_ctrl failed\n");
1025                 goto reclaim_boot_pages;
1026         }
1027
1028         err = handle_hca_cap(dev);
1029         if (err) {
1030                 dev_err(&pdev->dev, "handle_hca_cap failed\n");
1031                 goto reclaim_boot_pages;
1032         }
1033
1034         err = handle_hca_cap_atomic(dev);
1035         if (err) {
1036                 dev_err(&pdev->dev, "handle_hca_cap_atomic failed\n");
1037                 goto reclaim_boot_pages;
1038         }
1039
1040         err = mlx5_satisfy_startup_pages(dev, 0);
1041         if (err) {
1042                 dev_err(&pdev->dev, "failed to allocate init pages\n");
1043                 goto reclaim_boot_pages;
1044         }
1045
1046         err = mlx5_pagealloc_start(dev);
1047         if (err) {
1048                 dev_err(&pdev->dev, "mlx5_pagealloc_start failed\n");
1049                 goto reclaim_boot_pages;
1050         }
1051
1052         err = mlx5_cmd_init_hca(dev);
1053         if (err) {
1054                 dev_err(&pdev->dev, "init hca failed\n");
1055                 goto err_pagealloc_stop;
1056         }
1057
1058         mlx5_set_driver_version(dev);
1059
1060         mlx5_start_health_poll(dev);
1061
1062         err = mlx5_query_hca_caps(dev);
1063         if (err) {
1064                 dev_err(&pdev->dev, "query hca failed\n");
1065                 goto err_stop_poll;
1066         }
1067
1068         if (boot && mlx5_init_once(dev, priv)) {
1069                 dev_err(&pdev->dev, "sw objs init failed\n");
1070                 goto err_stop_poll;
1071         }
1072
1073         err = mlx5_alloc_irq_vectors(dev);
1074         if (err) {
1075                 dev_err(&pdev->dev, "alloc irq vectors failed\n");
1076                 goto err_cleanup_once;
1077         }
1078
1079         dev->priv.uar = mlx5_get_uars_page(dev);
1080         if (!dev->priv.uar) {
1081                 dev_err(&pdev->dev, "Failed allocating uar, aborting\n");
1082                 goto err_disable_msix;
1083         }
1084
1085         err = mlx5_start_eqs(dev);
1086         if (err) {
1087                 dev_err(&pdev->dev, "Failed to start pages and async EQs\n");
1088                 goto err_put_uars;
1089         }
1090
1091         err = alloc_comp_eqs(dev);
1092         if (err) {
1093                 dev_err(&pdev->dev, "Failed to alloc completion EQs\n");
1094                 goto err_stop_eqs;
1095         }
1096
1097         err = mlx5_init_fs(dev);
1098         if (err) {
1099                 dev_err(&pdev->dev, "Failed to init flow steering\n");
1100                 goto err_fs;
1101         }
1102
1103         err = mlx5_core_set_hca_defaults(dev);
1104         if (err) {
1105                 dev_err(&pdev->dev, "Failed to set hca defaults\n");
1106                 goto err_fs;
1107         }
1108
1109 #ifdef CONFIG_MLX5_CORE_EN
1110         mlx5_eswitch_attach(dev->priv.eswitch);
1111 #endif
1112
1113         err = mlx5_sriov_attach(dev);
1114         if (err) {
1115                 dev_err(&pdev->dev, "sriov init failed %d\n", err);
1116                 goto err_sriov;
1117         }
1118
1119         err = mlx5_fpga_device_start(dev);
1120         if (err) {
1121                 dev_err(&pdev->dev, "fpga device start failed %d\n", err);
1122                 goto err_fpga_start;
1123         }
1124         err = mlx5_accel_ipsec_init(dev);
1125         if (err) {
1126                 dev_err(&pdev->dev, "IPSec device start failed %d\n", err);
1127                 goto err_ipsec_start;
1128         }
1129
1130         if (mlx5_device_registered(dev)) {
1131                 mlx5_attach_device(dev);
1132         } else {
1133                 err = mlx5_register_device(dev);
1134                 if (err) {
1135                         dev_err(&pdev->dev, "mlx5_register_device failed %d\n", err);
1136                         goto err_reg_dev;
1137                 }
1138         }
1139
1140         set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1141 out:
1142         mutex_unlock(&dev->intf_state_mutex);
1143
1144         return 0;
1145
1146 err_reg_dev:
1147         mlx5_accel_ipsec_cleanup(dev);
1148 err_ipsec_start:
1149         mlx5_fpga_device_stop(dev);
1150
1151 err_fpga_start:
1152         mlx5_sriov_detach(dev);
1153
1154 err_sriov:
1155 #ifdef CONFIG_MLX5_CORE_EN
1156         mlx5_eswitch_detach(dev->priv.eswitch);
1157 #endif
1158         mlx5_cleanup_fs(dev);
1159
1160 err_fs:
1161         free_comp_eqs(dev);
1162
1163 err_stop_eqs:
1164         mlx5_stop_eqs(dev);
1165
1166 err_put_uars:
1167         mlx5_put_uars_page(dev, priv->uar);
1168
1169 err_disable_msix:
1170         mlx5_free_irq_vectors(dev);
1171
1172 err_cleanup_once:
1173         if (boot)
1174                 mlx5_cleanup_once(dev);
1175
1176 err_stop_poll:
1177         mlx5_stop_health_poll(dev);
1178         if (mlx5_cmd_teardown_hca(dev)) {
1179                 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1180                 goto out_err;
1181         }
1182
1183 err_pagealloc_stop:
1184         mlx5_pagealloc_stop(dev);
1185
1186 reclaim_boot_pages:
1187         mlx5_reclaim_startup_pages(dev);
1188
1189 err_disable_hca:
1190         mlx5_core_disable_hca(dev, 0);
1191
1192 err_cmd_cleanup:
1193         mlx5_cmd_cleanup(dev);
1194
1195 out_err:
1196         dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
1197         mutex_unlock(&dev->intf_state_mutex);
1198
1199         return err;
1200 }
1201
1202 static int mlx5_unload_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
1203                            bool cleanup)
1204 {
1205         int err = 0;
1206
1207         if (cleanup)
1208                 mlx5_drain_health_recovery(dev);
1209
1210         mutex_lock(&dev->intf_state_mutex);
1211         if (!test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1212                 dev_warn(&dev->pdev->dev, "%s: interface is down, NOP\n",
1213                          __func__);
1214                 if (cleanup)
1215                         mlx5_cleanup_once(dev);
1216                 goto out;
1217         }
1218
1219         clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1220
1221         if (mlx5_device_registered(dev))
1222                 mlx5_detach_device(dev);
1223
1224         mlx5_accel_ipsec_cleanup(dev);
1225         mlx5_fpga_device_stop(dev);
1226
1227         mlx5_sriov_detach(dev);
1228 #ifdef CONFIG_MLX5_CORE_EN
1229         mlx5_eswitch_detach(dev->priv.eswitch);
1230 #endif
1231         mlx5_cleanup_fs(dev);
1232         free_comp_eqs(dev);
1233         mlx5_stop_eqs(dev);
1234         mlx5_put_uars_page(dev, priv->uar);
1235         mlx5_free_irq_vectors(dev);
1236         if (cleanup)
1237                 mlx5_cleanup_once(dev);
1238         mlx5_stop_health_poll(dev);
1239         err = mlx5_cmd_teardown_hca(dev);
1240         if (err) {
1241                 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1242                 goto out;
1243         }
1244         mlx5_pagealloc_stop(dev);
1245         mlx5_reclaim_startup_pages(dev);
1246         mlx5_core_disable_hca(dev, 0);
1247         mlx5_cmd_cleanup(dev);
1248
1249 out:
1250         mutex_unlock(&dev->intf_state_mutex);
1251         return err;
1252 }
1253
1254 struct mlx5_core_event_handler {
1255         void (*event)(struct mlx5_core_dev *dev,
1256                       enum mlx5_dev_event event,
1257                       void *data);
1258 };
1259
1260 static const struct devlink_ops mlx5_devlink_ops = {
1261 #ifdef CONFIG_MLX5_CORE_EN
1262         .eswitch_mode_set = mlx5_devlink_eswitch_mode_set,
1263         .eswitch_mode_get = mlx5_devlink_eswitch_mode_get,
1264         .eswitch_inline_mode_set = mlx5_devlink_eswitch_inline_mode_set,
1265         .eswitch_inline_mode_get = mlx5_devlink_eswitch_inline_mode_get,
1266         .eswitch_encap_mode_set = mlx5_devlink_eswitch_encap_mode_set,
1267         .eswitch_encap_mode_get = mlx5_devlink_eswitch_encap_mode_get,
1268 #endif
1269 };
1270
1271 #define MLX5_IB_MOD "mlx5_ib"
1272 static int init_one(struct pci_dev *pdev,
1273                     const struct pci_device_id *id)
1274 {
1275         struct mlx5_core_dev *dev;
1276         struct devlink *devlink;
1277         struct mlx5_priv *priv;
1278         int err;
1279
1280         devlink = devlink_alloc(&mlx5_devlink_ops, sizeof(*dev));
1281         if (!devlink) {
1282                 dev_err(&pdev->dev, "kzalloc failed\n");
1283                 return -ENOMEM;
1284         }
1285
1286         dev = devlink_priv(devlink);
1287         priv = &dev->priv;
1288         priv->pci_dev_data = id->driver_data;
1289
1290         pci_set_drvdata(pdev, dev);
1291
1292         dev->pdev = pdev;
1293         dev->event = mlx5_core_event;
1294         dev->profile = &profile[prof_sel];
1295
1296         INIT_LIST_HEAD(&priv->ctx_list);
1297         spin_lock_init(&priv->ctx_lock);
1298         mutex_init(&dev->pci_status_mutex);
1299         mutex_init(&dev->intf_state_mutex);
1300
1301 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1302         err = init_srcu_struct(&priv->pfault_srcu);
1303         if (err) {
1304                 dev_err(&pdev->dev, "init_srcu_struct failed with error code %d\n",
1305                         err);
1306                 goto clean_dev;
1307         }
1308 #endif
1309         mutex_init(&priv->bfregs.reg_head.lock);
1310         mutex_init(&priv->bfregs.wc_head.lock);
1311         INIT_LIST_HEAD(&priv->bfregs.reg_head.list);
1312         INIT_LIST_HEAD(&priv->bfregs.wc_head.list);
1313
1314         err = mlx5_pci_init(dev, priv);
1315         if (err) {
1316                 dev_err(&pdev->dev, "mlx5_pci_init failed with error code %d\n", err);
1317                 goto clean_srcu;
1318         }
1319
1320         err = mlx5_health_init(dev);
1321         if (err) {
1322                 dev_err(&pdev->dev, "mlx5_health_init failed with error code %d\n", err);
1323                 goto close_pci;
1324         }
1325
1326         mlx5_pagealloc_init(dev);
1327
1328         err = mlx5_load_one(dev, priv, true);
1329         if (err) {
1330                 dev_err(&pdev->dev, "mlx5_load_one failed with error code %d\n", err);
1331                 goto clean_health;
1332         }
1333
1334         request_module_nowait(MLX5_IB_MOD);
1335
1336         err = devlink_register(devlink, &pdev->dev);
1337         if (err)
1338                 goto clean_load;
1339
1340         pci_save_state(pdev);
1341         return 0;
1342
1343 clean_load:
1344         mlx5_unload_one(dev, priv, true);
1345 clean_health:
1346         mlx5_pagealloc_cleanup(dev);
1347         mlx5_health_cleanup(dev);
1348 close_pci:
1349         mlx5_pci_close(dev, priv);
1350 clean_srcu:
1351 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1352         cleanup_srcu_struct(&priv->pfault_srcu);
1353 clean_dev:
1354 #endif
1355         pci_set_drvdata(pdev, NULL);
1356         devlink_free(devlink);
1357
1358         return err;
1359 }
1360
1361 static void remove_one(struct pci_dev *pdev)
1362 {
1363         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1364         struct devlink *devlink = priv_to_devlink(dev);
1365         struct mlx5_priv *priv = &dev->priv;
1366
1367         devlink_unregister(devlink);
1368         mlx5_unregister_device(dev);
1369
1370         if (mlx5_unload_one(dev, priv, true)) {
1371                 dev_err(&dev->pdev->dev, "mlx5_unload_one failed\n");
1372                 mlx5_health_cleanup(dev);
1373                 return;
1374         }
1375
1376         mlx5_pagealloc_cleanup(dev);
1377         mlx5_health_cleanup(dev);
1378         mlx5_pci_close(dev, priv);
1379 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1380         cleanup_srcu_struct(&priv->pfault_srcu);
1381 #endif
1382         pci_set_drvdata(pdev, NULL);
1383         devlink_free(devlink);
1384 }
1385
1386 static pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev,
1387                                               pci_channel_state_t state)
1388 {
1389         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1390         struct mlx5_priv *priv = &dev->priv;
1391
1392         dev_info(&pdev->dev, "%s was called\n", __func__);
1393
1394         mlx5_enter_error_state(dev, false);
1395         mlx5_unload_one(dev, priv, false);
1396         /* In case of kernel call drain the health wq */
1397         if (state) {
1398                 mlx5_drain_health_wq(dev);
1399                 mlx5_pci_disable_device(dev);
1400         }
1401
1402         return state == pci_channel_io_perm_failure ?
1403                 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1404 }
1405
1406 /* wait for the device to show vital signs by waiting
1407  * for the health counter to start counting.
1408  */
1409 static int wait_vital(struct pci_dev *pdev)
1410 {
1411         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1412         struct mlx5_core_health *health = &dev->priv.health;
1413         const int niter = 100;
1414         u32 last_count = 0;
1415         u32 count;
1416         int i;
1417
1418         for (i = 0; i < niter; i++) {
1419                 count = ioread32be(health->health_counter);
1420                 if (count && count != 0xffffffff) {
1421                         if (last_count && last_count != count) {
1422                                 dev_info(&pdev->dev, "Counter value 0x%x after %d iterations\n", count, i);
1423                                 return 0;
1424                         }
1425                         last_count = count;
1426                 }
1427                 msleep(50);
1428         }
1429
1430         return -ETIMEDOUT;
1431 }
1432
1433 static pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev)
1434 {
1435         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1436         int err;
1437
1438         dev_info(&pdev->dev, "%s was called\n", __func__);
1439
1440         err = mlx5_pci_enable_device(dev);
1441         if (err) {
1442                 dev_err(&pdev->dev, "%s: mlx5_pci_enable_device failed with error code: %d\n"
1443                         , __func__, err);
1444                 return PCI_ERS_RESULT_DISCONNECT;
1445         }
1446
1447         pci_set_master(pdev);
1448         pci_restore_state(pdev);
1449         pci_save_state(pdev);
1450
1451         if (wait_vital(pdev)) {
1452                 dev_err(&pdev->dev, "%s: wait_vital timed out\n", __func__);
1453                 return PCI_ERS_RESULT_DISCONNECT;
1454         }
1455
1456         return PCI_ERS_RESULT_RECOVERED;
1457 }
1458
1459 static void mlx5_pci_resume(struct pci_dev *pdev)
1460 {
1461         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1462         struct mlx5_priv *priv = &dev->priv;
1463         int err;
1464
1465         dev_info(&pdev->dev, "%s was called\n", __func__);
1466
1467         err = mlx5_load_one(dev, priv, false);
1468         if (err)
1469                 dev_err(&pdev->dev, "%s: mlx5_load_one failed with error code: %d\n"
1470                         , __func__, err);
1471         else
1472                 dev_info(&pdev->dev, "%s: device recovered\n", __func__);
1473 }
1474
1475 static const struct pci_error_handlers mlx5_err_handler = {
1476         .error_detected = mlx5_pci_err_detected,
1477         .slot_reset     = mlx5_pci_slot_reset,
1478         .resume         = mlx5_pci_resume
1479 };
1480
1481 static int mlx5_try_fast_unload(struct mlx5_core_dev *dev)
1482 {
1483         int ret;
1484
1485         if (!MLX5_CAP_GEN(dev, force_teardown)) {
1486                 mlx5_core_dbg(dev, "force teardown is not supported in the firmware\n");
1487                 return -EOPNOTSUPP;
1488         }
1489
1490         if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
1491                 mlx5_core_dbg(dev, "Device in internal error state, giving up\n");
1492                 return -EAGAIN;
1493         }
1494
1495         ret = mlx5_cmd_force_teardown_hca(dev);
1496         if (ret) {
1497                 mlx5_core_dbg(dev, "Firmware couldn't do fast unload error: %d\n", ret);
1498                 return ret;
1499         }
1500
1501         mlx5_enter_error_state(dev, true);
1502
1503         return 0;
1504 }
1505
1506 static void shutdown(struct pci_dev *pdev)
1507 {
1508         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1509         struct mlx5_priv *priv = &dev->priv;
1510         int err;
1511
1512         dev_info(&pdev->dev, "Shutdown was called\n");
1513         err = mlx5_try_fast_unload(dev);
1514         if (err)
1515                 mlx5_unload_one(dev, priv, false);
1516         mlx5_pci_disable_device(dev);
1517 }
1518
1519 static const struct pci_device_id mlx5_core_pci_table[] = {
1520         { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTIB) },
1521         { PCI_VDEVICE(MELLANOX, 0x1012), MLX5_PCI_DEV_IS_VF},   /* Connect-IB VF */
1522         { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4) },
1523         { PCI_VDEVICE(MELLANOX, 0x1014), MLX5_PCI_DEV_IS_VF},   /* ConnectX-4 VF */
1524         { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4_LX) },
1525         { PCI_VDEVICE(MELLANOX, 0x1016), MLX5_PCI_DEV_IS_VF},   /* ConnectX-4LX VF */
1526         { PCI_VDEVICE(MELLANOX, 0x1017) },                      /* ConnectX-5, PCIe 3.0 */
1527         { PCI_VDEVICE(MELLANOX, 0x1018), MLX5_PCI_DEV_IS_VF},   /* ConnectX-5 VF */
1528         { PCI_VDEVICE(MELLANOX, 0x1019) },                      /* ConnectX-5 Ex */
1529         { PCI_VDEVICE(MELLANOX, 0x101a), MLX5_PCI_DEV_IS_VF},   /* ConnectX-5 Ex VF */
1530         { PCI_VDEVICE(MELLANOX, 0x101b) },                      /* ConnectX-6 */
1531         { PCI_VDEVICE(MELLANOX, 0x101c), MLX5_PCI_DEV_IS_VF},   /* ConnectX-6 VF */
1532         { PCI_VDEVICE(MELLANOX, 0xa2d2) },                      /* BlueField integrated ConnectX-5 network controller */
1533         { PCI_VDEVICE(MELLANOX, 0xa2d3), MLX5_PCI_DEV_IS_VF},   /* BlueField integrated ConnectX-5 network controller VF */
1534         { 0, }
1535 };
1536
1537 MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
1538
1539 void mlx5_disable_device(struct mlx5_core_dev *dev)
1540 {
1541         mlx5_pci_err_detected(dev->pdev, 0);
1542 }
1543
1544 void mlx5_recover_device(struct mlx5_core_dev *dev)
1545 {
1546         mlx5_pci_disable_device(dev);
1547         if (mlx5_pci_slot_reset(dev->pdev) == PCI_ERS_RESULT_RECOVERED)
1548                 mlx5_pci_resume(dev->pdev);
1549 }
1550
1551 static struct pci_driver mlx5_core_driver = {
1552         .name           = DRIVER_NAME,
1553         .id_table       = mlx5_core_pci_table,
1554         .probe          = init_one,
1555         .remove         = remove_one,
1556         .shutdown       = shutdown,
1557         .err_handler    = &mlx5_err_handler,
1558         .sriov_configure   = mlx5_core_sriov_configure,
1559 };
1560
1561 static void mlx5_core_verify_params(void)
1562 {
1563         if (prof_sel >= ARRAY_SIZE(profile)) {
1564                 pr_warn("mlx5_core: WARNING: Invalid module parameter prof_sel %d, valid range 0-%zu, changing back to default(%d)\n",
1565                         prof_sel,
1566                         ARRAY_SIZE(profile) - 1,
1567                         MLX5_DEFAULT_PROF);
1568                 prof_sel = MLX5_DEFAULT_PROF;
1569         }
1570 }
1571
1572 static int __init init(void)
1573 {
1574         int err;
1575
1576         mlx5_core_verify_params();
1577         mlx5_register_debugfs();
1578
1579         err = pci_register_driver(&mlx5_core_driver);
1580         if (err)
1581                 goto err_debug;
1582
1583 #ifdef CONFIG_MLX5_CORE_EN
1584         mlx5e_init();
1585 #endif
1586
1587         return 0;
1588
1589 err_debug:
1590         mlx5_unregister_debugfs();
1591         return err;
1592 }
1593
1594 static void __exit cleanup(void)
1595 {
1596 #ifdef CONFIG_MLX5_CORE_EN
1597         mlx5e_cleanup();
1598 #endif
1599         pci_unregister_driver(&mlx5_core_driver);
1600         mlx5_unregister_debugfs();
1601 }
1602
1603 module_init(init);
1604 module_exit(cleanup);