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