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