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