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