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