2729afc13ab48d24f69a63e2565955c9cef7873c
[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 "devlink.h"
60 #include "lib/mlx5.h"
61 #include "fpga/core.h"
62 #include "fpga/ipsec.h"
63 #include "accel/ipsec.h"
64 #include "accel/tls.h"
65 #include "lib/clock.h"
66 #include "lib/vxlan.h"
67 #include "lib/geneve.h"
68 #include "lib/devcom.h"
69 #include "lib/pci_vsc.h"
70 #include "diag/fw_tracer.h"
71 #include "ecpf.h"
72 #include "lib/hv_vhca.h"
73 #include "diag/rsc_dump.h"
74
75 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
76 MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) core driver");
77 MODULE_LICENSE("Dual BSD/GPL");
78 MODULE_VERSION(DRIVER_VERSION);
79
80 unsigned int mlx5_core_debug_mask;
81 module_param_named(debug_mask, mlx5_core_debug_mask, uint, 0644);
82 MODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
83
84 #define MLX5_DEFAULT_PROF       2
85 static unsigned int prof_sel = MLX5_DEFAULT_PROF;
86 module_param_named(prof_sel, prof_sel, uint, 0444);
87 MODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");
88
89 static u32 sw_owner_id[4];
90
91 enum {
92         MLX5_ATOMIC_REQ_MODE_BE = 0x0,
93         MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS = 0x1,
94 };
95
96 static struct mlx5_profile profile[] = {
97         [0] = {
98                 .mask           = 0,
99         },
100         [1] = {
101                 .mask           = MLX5_PROF_MASK_QP_SIZE,
102                 .log_max_qp     = 12,
103         },
104         [2] = {
105                 .mask           = MLX5_PROF_MASK_QP_SIZE |
106                                   MLX5_PROF_MASK_MR_CACHE,
107                 .log_max_qp     = 18,
108                 .mr_cache[0]    = {
109                         .size   = 500,
110                         .limit  = 250
111                 },
112                 .mr_cache[1]    = {
113                         .size   = 500,
114                         .limit  = 250
115                 },
116                 .mr_cache[2]    = {
117                         .size   = 500,
118                         .limit  = 250
119                 },
120                 .mr_cache[3]    = {
121                         .size   = 500,
122                         .limit  = 250
123                 },
124                 .mr_cache[4]    = {
125                         .size   = 500,
126                         .limit  = 250
127                 },
128                 .mr_cache[5]    = {
129                         .size   = 500,
130                         .limit  = 250
131                 },
132                 .mr_cache[6]    = {
133                         .size   = 500,
134                         .limit  = 250
135                 },
136                 .mr_cache[7]    = {
137                         .size   = 500,
138                         .limit  = 250
139                 },
140                 .mr_cache[8]    = {
141                         .size   = 500,
142                         .limit  = 250
143                 },
144                 .mr_cache[9]    = {
145                         .size   = 500,
146                         .limit  = 250
147                 },
148                 .mr_cache[10]   = {
149                         .size   = 500,
150                         .limit  = 250
151                 },
152                 .mr_cache[11]   = {
153                         .size   = 500,
154                         .limit  = 250
155                 },
156                 .mr_cache[12]   = {
157                         .size   = 64,
158                         .limit  = 32
159                 },
160                 .mr_cache[13]   = {
161                         .size   = 32,
162                         .limit  = 16
163                 },
164                 .mr_cache[14]   = {
165                         .size   = 16,
166                         .limit  = 8
167                 },
168                 .mr_cache[15]   = {
169                         .size   = 8,
170                         .limit  = 4
171                 },
172         },
173 };
174
175 #define FW_INIT_TIMEOUT_MILI            2000
176 #define FW_INIT_WAIT_MS                 2
177 #define FW_PRE_INIT_TIMEOUT_MILI        120000
178 #define FW_INIT_WARN_MESSAGE_INTERVAL   20000
179
180 static int fw_initializing(struct mlx5_core_dev *dev)
181 {
182         return ioread32be(&dev->iseg->initializing) >> 31;
183 }
184
185 static int wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili,
186                         u32 warn_time_mili)
187 {
188         unsigned long warn = jiffies + msecs_to_jiffies(warn_time_mili);
189         unsigned long end = jiffies + msecs_to_jiffies(max_wait_mili);
190         int err = 0;
191
192         BUILD_BUG_ON(FW_PRE_INIT_TIMEOUT_MILI < FW_INIT_WARN_MESSAGE_INTERVAL);
193
194         while (fw_initializing(dev)) {
195                 if (time_after(jiffies, end)) {
196                         err = -EBUSY;
197                         break;
198                 }
199                 if (warn_time_mili && time_after(jiffies, warn)) {
200                         mlx5_core_warn(dev, "Waiting for FW initialization, timeout abort in %ds\n",
201                                        jiffies_to_msecs(end - warn) / 1000);
202                         warn = jiffies + msecs_to_jiffies(warn_time_mili);
203                 }
204                 msleep(FW_INIT_WAIT_MS);
205         }
206
207         return err;
208 }
209
210 static void mlx5_set_driver_version(struct mlx5_core_dev *dev)
211 {
212         int driver_ver_sz = MLX5_FLD_SZ_BYTES(set_driver_version_in,
213                                               driver_version);
214         u8 in[MLX5_ST_SZ_BYTES(set_driver_version_in)] = {};
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_in(dev, set_driver_version, in);
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_inout(dev, query_hca_cap, in, out);
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 opmod)
415 {
416         MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
417         MLX5_SET(set_hca_cap_in, in, op_mod, opmod << 1);
418         return mlx5_cmd_exec_in(dev, set_hca_cap, in);
419 }
420
421 static int handle_hca_cap_atomic(struct mlx5_core_dev *dev, void *set_ctx)
422 {
423         void *set_hca_cap;
424         int req_endianness;
425         int err;
426
427         if (!MLX5_CAP_GEN(dev, atomic))
428                 return 0;
429
430         err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC);
431         if (err)
432                 return err;
433
434         req_endianness =
435                 MLX5_CAP_ATOMIC(dev,
436                                 supported_atomic_req_8B_endianness_mode_1);
437
438         if (req_endianness != MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS)
439                 return 0;
440
441         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
442
443         /* Set requestor to host endianness */
444         MLX5_SET(atomic_caps, set_hca_cap, atomic_req_8B_endianness_mode,
445                  MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS);
446
447         return set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MOD_ATOMIC);
448 }
449
450 static int handle_hca_cap_odp(struct mlx5_core_dev *dev, void *set_ctx)
451 {
452         void *set_hca_cap;
453         bool do_set = false;
454         int err;
455
456         if (!IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING) ||
457             !MLX5_CAP_GEN(dev, pg))
458                 return 0;
459
460         err = mlx5_core_get_caps(dev, MLX5_CAP_ODP);
461         if (err)
462                 return err;
463
464         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
465         memcpy(set_hca_cap, dev->caps.hca_cur[MLX5_CAP_ODP],
466                MLX5_ST_SZ_BYTES(odp_cap));
467
468 #define ODP_CAP_SET_MAX(dev, field)                                            \
469         do {                                                                   \
470                 u32 _res = MLX5_CAP_ODP_MAX(dev, field);                       \
471                 if (_res) {                                                    \
472                         do_set = true;                                         \
473                         MLX5_SET(odp_cap, set_hca_cap, field, _res);           \
474                 }                                                              \
475         } while (0)
476
477         ODP_CAP_SET_MAX(dev, ud_odp_caps.srq_receive);
478         ODP_CAP_SET_MAX(dev, rc_odp_caps.srq_receive);
479         ODP_CAP_SET_MAX(dev, xrc_odp_caps.srq_receive);
480         ODP_CAP_SET_MAX(dev, xrc_odp_caps.send);
481         ODP_CAP_SET_MAX(dev, xrc_odp_caps.receive);
482         ODP_CAP_SET_MAX(dev, xrc_odp_caps.write);
483         ODP_CAP_SET_MAX(dev, xrc_odp_caps.read);
484         ODP_CAP_SET_MAX(dev, xrc_odp_caps.atomic);
485         ODP_CAP_SET_MAX(dev, dc_odp_caps.srq_receive);
486         ODP_CAP_SET_MAX(dev, dc_odp_caps.send);
487         ODP_CAP_SET_MAX(dev, dc_odp_caps.receive);
488         ODP_CAP_SET_MAX(dev, dc_odp_caps.write);
489         ODP_CAP_SET_MAX(dev, dc_odp_caps.read);
490         ODP_CAP_SET_MAX(dev, dc_odp_caps.atomic);
491
492         if (!do_set)
493                 return 0;
494
495         return set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MOD_ODP);
496 }
497
498 static int handle_hca_cap(struct mlx5_core_dev *dev, void *set_ctx)
499 {
500         struct mlx5_profile *prof = dev->profile;
501         void *set_hca_cap;
502         int err;
503
504         err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL);
505         if (err)
506                 return err;
507
508         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
509                                    capability);
510         memcpy(set_hca_cap, dev->caps.hca_cur[MLX5_CAP_GENERAL],
511                MLX5_ST_SZ_BYTES(cmd_hca_cap));
512
513         mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
514                       mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
515                       128);
516         /* we limit the size of the pkey table to 128 entries for now */
517         MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
518                  to_fw_pkey_sz(dev, 128));
519
520         /* Check log_max_qp from HCA caps to set in current profile */
521         if (MLX5_CAP_GEN_MAX(dev, log_max_qp) < profile[prof_sel].log_max_qp) {
522                 mlx5_core_warn(dev, "log_max_qp value in current profile is %d, changing it to HCA capability limit (%d)\n",
523                                profile[prof_sel].log_max_qp,
524                                MLX5_CAP_GEN_MAX(dev, log_max_qp));
525                 profile[prof_sel].log_max_qp = MLX5_CAP_GEN_MAX(dev, log_max_qp);
526         }
527         if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
528                 MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
529                          prof->log_max_qp);
530
531         /* disable cmdif checksum */
532         MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
533
534         /* Enable 4K UAR only when HCA supports it and page size is bigger
535          * than 4K.
536          */
537         if (MLX5_CAP_GEN_MAX(dev, uar_4k) && PAGE_SIZE > 4096)
538                 MLX5_SET(cmd_hca_cap, set_hca_cap, uar_4k, 1);
539
540         MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12);
541
542         if (MLX5_CAP_GEN_MAX(dev, cache_line_128byte))
543                 MLX5_SET(cmd_hca_cap,
544                          set_hca_cap,
545                          cache_line_128byte,
546                          cache_line_size() >= 128 ? 1 : 0);
547
548         if (MLX5_CAP_GEN_MAX(dev, dct))
549                 MLX5_SET(cmd_hca_cap, set_hca_cap, dct, 1);
550
551         if (MLX5_CAP_GEN_MAX(dev, num_vhca_ports))
552                 MLX5_SET(cmd_hca_cap,
553                          set_hca_cap,
554                          num_vhca_ports,
555                          MLX5_CAP_GEN_MAX(dev, num_vhca_ports));
556
557         if (MLX5_CAP_GEN_MAX(dev, release_all_pages))
558                 MLX5_SET(cmd_hca_cap, set_hca_cap, release_all_pages, 1);
559
560         return set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE);
561 }
562
563 static int handle_hca_cap_roce(struct mlx5_core_dev *dev, void *set_ctx)
564 {
565         void *set_hca_cap;
566         int err;
567
568         if (!MLX5_CAP_GEN(dev, roce))
569                 return 0;
570
571         err = mlx5_core_get_caps(dev, MLX5_CAP_ROCE);
572         if (err)
573                 return err;
574
575         if (MLX5_CAP_ROCE(dev, sw_r_roce_src_udp_port) ||
576             !MLX5_CAP_ROCE_MAX(dev, sw_r_roce_src_udp_port))
577                 return 0;
578
579         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
580         memcpy(set_hca_cap, dev->caps.hca_cur[MLX5_CAP_ROCE],
581                MLX5_ST_SZ_BYTES(roce_cap));
582         MLX5_SET(roce_cap, set_hca_cap, sw_r_roce_src_udp_port, 1);
583
584         err = set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MOD_ROCE);
585         return err;
586 }
587
588 static int set_hca_cap(struct mlx5_core_dev *dev)
589 {
590         int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
591         void *set_ctx;
592         int err;
593
594         set_ctx = kzalloc(set_sz, GFP_KERNEL);
595         if (!set_ctx)
596                 return -ENOMEM;
597
598         err = handle_hca_cap(dev, set_ctx);
599         if (err) {
600                 mlx5_core_err(dev, "handle_hca_cap failed\n");
601                 goto out;
602         }
603
604         memset(set_ctx, 0, set_sz);
605         err = handle_hca_cap_atomic(dev, set_ctx);
606         if (err) {
607                 mlx5_core_err(dev, "handle_hca_cap_atomic failed\n");
608                 goto out;
609         }
610
611         memset(set_ctx, 0, set_sz);
612         err = handle_hca_cap_odp(dev, set_ctx);
613         if (err) {
614                 mlx5_core_err(dev, "handle_hca_cap_odp failed\n");
615                 goto out;
616         }
617
618         memset(set_ctx, 0, set_sz);
619         err = handle_hca_cap_roce(dev, set_ctx);
620         if (err) {
621                 mlx5_core_err(dev, "handle_hca_cap_roce failed\n");
622                 goto out;
623         }
624
625 out:
626         kfree(set_ctx);
627         return err;
628 }
629
630 static int set_hca_ctrl(struct mlx5_core_dev *dev)
631 {
632         struct mlx5_reg_host_endianness he_in;
633         struct mlx5_reg_host_endianness he_out;
634         int err;
635
636         if (!mlx5_core_is_pf(dev))
637                 return 0;
638
639         memset(&he_in, 0, sizeof(he_in));
640         he_in.he = MLX5_SET_HOST_ENDIANNESS;
641         err = mlx5_core_access_reg(dev, &he_in,  sizeof(he_in),
642                                         &he_out, sizeof(he_out),
643                                         MLX5_REG_HOST_ENDIANNESS, 0, 1);
644         return err;
645 }
646
647 static int mlx5_core_set_hca_defaults(struct mlx5_core_dev *dev)
648 {
649         int ret = 0;
650
651         /* Disable local_lb by default */
652         if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH)
653                 ret = mlx5_nic_vport_update_local_lb(dev, false);
654
655         return ret;
656 }
657
658 int mlx5_core_enable_hca(struct mlx5_core_dev *dev, u16 func_id)
659 {
660         u32 in[MLX5_ST_SZ_DW(enable_hca_in)] = {};
661
662         MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA);
663         MLX5_SET(enable_hca_in, in, function_id, func_id);
664         MLX5_SET(enable_hca_in, in, embedded_cpu_function,
665                  dev->caps.embedded_cpu);
666         return mlx5_cmd_exec_in(dev, enable_hca, in);
667 }
668
669 int mlx5_core_disable_hca(struct mlx5_core_dev *dev, u16 func_id)
670 {
671         u32 in[MLX5_ST_SZ_DW(disable_hca_in)] = {};
672
673         MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA);
674         MLX5_SET(disable_hca_in, in, function_id, func_id);
675         MLX5_SET(enable_hca_in, in, embedded_cpu_function,
676                  dev->caps.embedded_cpu);
677         return mlx5_cmd_exec_in(dev, disable_hca, in);
678 }
679
680 static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
681 {
682         u32 query_out[MLX5_ST_SZ_DW(query_issi_out)] = {};
683         u32 query_in[MLX5_ST_SZ_DW(query_issi_in)] = {};
684         u32 sup_issi;
685         int err;
686
687         MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
688         err = mlx5_cmd_exec_inout(dev, query_issi, query_in, query_out);
689         if (err) {
690                 u32 syndrome;
691                 u8 status;
692
693                 mlx5_cmd_mbox_status(query_out, &status, &syndrome);
694                 if (!status || syndrome == MLX5_DRIVER_SYND) {
695                         mlx5_core_err(dev, "Failed to query ISSI err(%d) status(%d) synd(%d)\n",
696                                       err, status, syndrome);
697                         return err;
698                 }
699
700                 mlx5_core_warn(dev, "Query ISSI is not supported by FW, ISSI is 0\n");
701                 dev->issi = 0;
702                 return 0;
703         }
704
705         sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);
706
707         if (sup_issi & (1 << 1)) {
708                 u32 set_in[MLX5_ST_SZ_DW(set_issi_in)] = {};
709
710                 MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
711                 MLX5_SET(set_issi_in, set_in, current_issi, 1);
712                 err = mlx5_cmd_exec_in(dev, set_issi, set_in);
713                 if (err) {
714                         mlx5_core_err(dev, "Failed to set ISSI to 1 err(%d)\n",
715                                       err);
716                         return err;
717                 }
718
719                 dev->issi = 1;
720
721                 return 0;
722         } else if (sup_issi & (1 << 0) || !sup_issi) {
723                 return 0;
724         }
725
726         return -EOPNOTSUPP;
727 }
728
729 static int mlx5_pci_init(struct mlx5_core_dev *dev, struct pci_dev *pdev,
730                          const struct pci_device_id *id)
731 {
732         struct mlx5_priv *priv = &dev->priv;
733         int err = 0;
734
735         mutex_init(&dev->pci_status_mutex);
736         pci_set_drvdata(dev->pdev, dev);
737
738         dev->bar_addr = pci_resource_start(pdev, 0);
739         priv->numa_node = dev_to_node(&dev->pdev->dev);
740
741         err = mlx5_pci_enable_device(dev);
742         if (err) {
743                 mlx5_core_err(dev, "Cannot enable PCI device, aborting\n");
744                 return err;
745         }
746
747         err = request_bar(pdev);
748         if (err) {
749                 mlx5_core_err(dev, "error requesting BARs, aborting\n");
750                 goto err_disable;
751         }
752
753         pci_set_master(pdev);
754
755         err = set_dma_caps(pdev);
756         if (err) {
757                 mlx5_core_err(dev, "Failed setting DMA capabilities mask, aborting\n");
758                 goto err_clr_master;
759         }
760
761         if (pci_enable_atomic_ops_to_root(pdev, PCI_EXP_DEVCAP2_ATOMIC_COMP32) &&
762             pci_enable_atomic_ops_to_root(pdev, PCI_EXP_DEVCAP2_ATOMIC_COMP64) &&
763             pci_enable_atomic_ops_to_root(pdev, PCI_EXP_DEVCAP2_ATOMIC_COMP128))
764                 mlx5_core_dbg(dev, "Enabling pci atomics failed\n");
765
766         dev->iseg_base = dev->bar_addr;
767         dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
768         if (!dev->iseg) {
769                 err = -ENOMEM;
770                 mlx5_core_err(dev, "Failed mapping initialization segment, aborting\n");
771                 goto err_clr_master;
772         }
773
774         mlx5_pci_vsc_init(dev);
775         dev->caps.embedded_cpu = mlx5_read_embedded_cpu(dev);
776         return 0;
777
778 err_clr_master:
779         pci_clear_master(dev->pdev);
780         release_bar(dev->pdev);
781 err_disable:
782         mlx5_pci_disable_device(dev);
783         return err;
784 }
785
786 static void mlx5_pci_close(struct mlx5_core_dev *dev)
787 {
788         /* health work might still be active, and it needs pci bar in
789          * order to know the NIC state. Therefore, drain the health WQ
790          * before removing the pci bars
791          */
792         mlx5_drain_health_wq(dev);
793         iounmap(dev->iseg);
794         pci_clear_master(dev->pdev);
795         release_bar(dev->pdev);
796         mlx5_pci_disable_device(dev);
797 }
798
799 static int mlx5_init_once(struct mlx5_core_dev *dev)
800 {
801         int err;
802
803         dev->priv.devcom = mlx5_devcom_register_device(dev);
804         if (IS_ERR(dev->priv.devcom))
805                 mlx5_core_err(dev, "failed to register with devcom (0x%p)\n",
806                               dev->priv.devcom);
807
808         err = mlx5_query_board_id(dev);
809         if (err) {
810                 mlx5_core_err(dev, "query board id failed\n");
811                 goto err_devcom;
812         }
813
814         err = mlx5_irq_table_init(dev);
815         if (err) {
816                 mlx5_core_err(dev, "failed to initialize irq table\n");
817                 goto err_devcom;
818         }
819
820         err = mlx5_eq_table_init(dev);
821         if (err) {
822                 mlx5_core_err(dev, "failed to initialize eq\n");
823                 goto err_irq_cleanup;
824         }
825
826         err = mlx5_events_init(dev);
827         if (err) {
828                 mlx5_core_err(dev, "failed to initialize events\n");
829                 goto err_eq_cleanup;
830         }
831
832         mlx5_cq_debugfs_init(dev);
833
834         mlx5_init_reserved_gids(dev);
835
836         mlx5_init_clock(dev);
837
838         dev->vxlan = mlx5_vxlan_create(dev);
839         dev->geneve = mlx5_geneve_create(dev);
840
841         err = mlx5_init_rl_table(dev);
842         if (err) {
843                 mlx5_core_err(dev, "Failed to init rate limiting\n");
844                 goto err_tables_cleanup;
845         }
846
847         err = mlx5_mpfs_init(dev);
848         if (err) {
849                 mlx5_core_err(dev, "Failed to init l2 table %d\n", err);
850                 goto err_rl_cleanup;
851         }
852
853         err = mlx5_sriov_init(dev);
854         if (err) {
855                 mlx5_core_err(dev, "Failed to init sriov %d\n", err);
856                 goto err_mpfs_cleanup;
857         }
858
859         err = mlx5_eswitch_init(dev);
860         if (err) {
861                 mlx5_core_err(dev, "Failed to init eswitch %d\n", err);
862                 goto err_sriov_cleanup;
863         }
864
865         err = mlx5_fpga_init(dev);
866         if (err) {
867                 mlx5_core_err(dev, "Failed to init fpga device %d\n", err);
868                 goto err_eswitch_cleanup;
869         }
870
871         dev->dm = mlx5_dm_create(dev);
872         if (IS_ERR(dev->dm))
873                 mlx5_core_warn(dev, "Failed to init device memory%d\n", err);
874
875         dev->tracer = mlx5_fw_tracer_create(dev);
876         dev->hv_vhca = mlx5_hv_vhca_create(dev);
877         dev->rsc_dump = mlx5_rsc_dump_create(dev);
878
879         return 0;
880
881 err_eswitch_cleanup:
882         mlx5_eswitch_cleanup(dev->priv.eswitch);
883 err_sriov_cleanup:
884         mlx5_sriov_cleanup(dev);
885 err_mpfs_cleanup:
886         mlx5_mpfs_cleanup(dev);
887 err_rl_cleanup:
888         mlx5_cleanup_rl_table(dev);
889 err_tables_cleanup:
890         mlx5_geneve_destroy(dev->geneve);
891         mlx5_vxlan_destroy(dev->vxlan);
892         mlx5_cq_debugfs_cleanup(dev);
893         mlx5_events_cleanup(dev);
894 err_eq_cleanup:
895         mlx5_eq_table_cleanup(dev);
896 err_irq_cleanup:
897         mlx5_irq_table_cleanup(dev);
898 err_devcom:
899         mlx5_devcom_unregister_device(dev->priv.devcom);
900
901         return err;
902 }
903
904 static void mlx5_cleanup_once(struct mlx5_core_dev *dev)
905 {
906         mlx5_rsc_dump_destroy(dev);
907         mlx5_hv_vhca_destroy(dev->hv_vhca);
908         mlx5_fw_tracer_destroy(dev->tracer);
909         mlx5_dm_cleanup(dev);
910         mlx5_fpga_cleanup(dev);
911         mlx5_eswitch_cleanup(dev->priv.eswitch);
912         mlx5_sriov_cleanup(dev);
913         mlx5_mpfs_cleanup(dev);
914         mlx5_cleanup_rl_table(dev);
915         mlx5_geneve_destroy(dev->geneve);
916         mlx5_vxlan_destroy(dev->vxlan);
917         mlx5_cleanup_clock(dev);
918         mlx5_cleanup_reserved_gids(dev);
919         mlx5_cq_debugfs_cleanup(dev);
920         mlx5_events_cleanup(dev);
921         mlx5_eq_table_cleanup(dev);
922         mlx5_irq_table_cleanup(dev);
923         mlx5_devcom_unregister_device(dev->priv.devcom);
924 }
925
926 static int mlx5_function_setup(struct mlx5_core_dev *dev, bool boot)
927 {
928         int err;
929
930         mlx5_core_info(dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
931                        fw_rev_min(dev), fw_rev_sub(dev));
932
933         /* Only PFs hold the relevant PCIe information for this query */
934         if (mlx5_core_is_pf(dev))
935                 pcie_print_link_status(dev->pdev);
936
937         /* wait for firmware to accept initialization segments configurations
938          */
939         err = wait_fw_init(dev, FW_PRE_INIT_TIMEOUT_MILI, FW_INIT_WARN_MESSAGE_INTERVAL);
940         if (err) {
941                 mlx5_core_err(dev, "Firmware over %d MS in pre-initializing state, aborting\n",
942                               FW_PRE_INIT_TIMEOUT_MILI);
943                 return err;
944         }
945
946         err = mlx5_cmd_init(dev);
947         if (err) {
948                 mlx5_core_err(dev, "Failed initializing command interface, aborting\n");
949                 return err;
950         }
951
952         err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI, 0);
953         if (err) {
954                 mlx5_core_err(dev, "Firmware over %d MS in initializing state, aborting\n",
955                               FW_INIT_TIMEOUT_MILI);
956                 goto err_cmd_cleanup;
957         }
958
959         mlx5_cmd_set_state(dev, MLX5_CMDIF_STATE_UP);
960
961         err = mlx5_core_enable_hca(dev, 0);
962         if (err) {
963                 mlx5_core_err(dev, "enable hca failed\n");
964                 goto err_cmd_cleanup;
965         }
966
967         err = mlx5_core_set_issi(dev);
968         if (err) {
969                 mlx5_core_err(dev, "failed to set issi\n");
970                 goto err_disable_hca;
971         }
972
973         err = mlx5_satisfy_startup_pages(dev, 1);
974         if (err) {
975                 mlx5_core_err(dev, "failed to allocate boot pages\n");
976                 goto err_disable_hca;
977         }
978
979         err = set_hca_ctrl(dev);
980         if (err) {
981                 mlx5_core_err(dev, "set_hca_ctrl failed\n");
982                 goto reclaim_boot_pages;
983         }
984
985         err = set_hca_cap(dev);
986         if (err) {
987                 mlx5_core_err(dev, "set_hca_cap failed\n");
988                 goto reclaim_boot_pages;
989         }
990
991         err = mlx5_satisfy_startup_pages(dev, 0);
992         if (err) {
993                 mlx5_core_err(dev, "failed to allocate init pages\n");
994                 goto reclaim_boot_pages;
995         }
996
997         err = mlx5_cmd_init_hca(dev, sw_owner_id);
998         if (err) {
999                 mlx5_core_err(dev, "init hca failed\n");
1000                 goto reclaim_boot_pages;
1001         }
1002
1003         mlx5_set_driver_version(dev);
1004
1005         mlx5_start_health_poll(dev);
1006
1007         err = mlx5_query_hca_caps(dev);
1008         if (err) {
1009                 mlx5_core_err(dev, "query hca failed\n");
1010                 goto stop_health;
1011         }
1012
1013         return 0;
1014
1015 stop_health:
1016         mlx5_stop_health_poll(dev, boot);
1017 reclaim_boot_pages:
1018         mlx5_reclaim_startup_pages(dev);
1019 err_disable_hca:
1020         mlx5_core_disable_hca(dev, 0);
1021 err_cmd_cleanup:
1022         mlx5_cmd_set_state(dev, MLX5_CMDIF_STATE_DOWN);
1023         mlx5_cmd_cleanup(dev);
1024
1025         return err;
1026 }
1027
1028 static int mlx5_function_teardown(struct mlx5_core_dev *dev, bool boot)
1029 {
1030         int err;
1031
1032         mlx5_stop_health_poll(dev, boot);
1033         err = mlx5_cmd_teardown_hca(dev);
1034         if (err) {
1035                 mlx5_core_err(dev, "tear_down_hca failed, skip cleanup\n");
1036                 return err;
1037         }
1038         mlx5_reclaim_startup_pages(dev);
1039         mlx5_core_disable_hca(dev, 0);
1040         mlx5_cmd_set_state(dev, MLX5_CMDIF_STATE_DOWN);
1041         mlx5_cmd_cleanup(dev);
1042
1043         return 0;
1044 }
1045
1046 static int mlx5_load(struct mlx5_core_dev *dev)
1047 {
1048         int err;
1049
1050         dev->priv.uar = mlx5_get_uars_page(dev);
1051         if (IS_ERR(dev->priv.uar)) {
1052                 mlx5_core_err(dev, "Failed allocating uar, aborting\n");
1053                 err = PTR_ERR(dev->priv.uar);
1054                 return err;
1055         }
1056
1057         mlx5_events_start(dev);
1058         mlx5_pagealloc_start(dev);
1059
1060         err = mlx5_irq_table_create(dev);
1061         if (err) {
1062                 mlx5_core_err(dev, "Failed to alloc IRQs\n");
1063                 goto err_irq_table;
1064         }
1065
1066         err = mlx5_eq_table_create(dev);
1067         if (err) {
1068                 mlx5_core_err(dev, "Failed to create EQs\n");
1069                 goto err_eq_table;
1070         }
1071
1072         err = mlx5_fw_tracer_init(dev->tracer);
1073         if (err) {
1074                 mlx5_core_err(dev, "Failed to init FW tracer\n");
1075                 goto err_fw_tracer;
1076         }
1077
1078         mlx5_hv_vhca_init(dev->hv_vhca);
1079
1080         err = mlx5_rsc_dump_init(dev);
1081         if (err) {
1082                 mlx5_core_err(dev, "Failed to init Resource dump\n");
1083                 goto err_rsc_dump;
1084         }
1085
1086         err = mlx5_fpga_device_start(dev);
1087         if (err) {
1088                 mlx5_core_err(dev, "fpga device start failed %d\n", err);
1089                 goto err_fpga_start;
1090         }
1091
1092         err = mlx5_accel_ipsec_init(dev);
1093         if (err) {
1094                 mlx5_core_err(dev, "IPSec device start failed %d\n", err);
1095                 goto err_ipsec_start;
1096         }
1097
1098         err = mlx5_accel_tls_init(dev);
1099         if (err) {
1100                 mlx5_core_err(dev, "TLS device start failed %d\n", err);
1101                 goto err_tls_start;
1102         }
1103
1104         err = mlx5_init_fs(dev);
1105         if (err) {
1106                 mlx5_core_err(dev, "Failed to init flow steering\n");
1107                 goto err_fs;
1108         }
1109
1110         err = mlx5_core_set_hca_defaults(dev);
1111         if (err) {
1112                 mlx5_core_err(dev, "Failed to set hca defaults\n");
1113                 goto err_sriov;
1114         }
1115
1116         err = mlx5_sriov_attach(dev);
1117         if (err) {
1118                 mlx5_core_err(dev, "sriov init failed %d\n", err);
1119                 goto err_sriov;
1120         }
1121
1122         err = mlx5_ec_init(dev);
1123         if (err) {
1124                 mlx5_core_err(dev, "Failed to init embedded CPU\n");
1125                 goto err_ec;
1126         }
1127
1128         return 0;
1129
1130 err_ec:
1131         mlx5_sriov_detach(dev);
1132 err_sriov:
1133         mlx5_cleanup_fs(dev);
1134 err_fs:
1135         mlx5_accel_tls_cleanup(dev);
1136 err_tls_start:
1137         mlx5_accel_ipsec_cleanup(dev);
1138 err_ipsec_start:
1139         mlx5_fpga_device_stop(dev);
1140 err_fpga_start:
1141         mlx5_rsc_dump_cleanup(dev);
1142 err_rsc_dump:
1143         mlx5_hv_vhca_cleanup(dev->hv_vhca);
1144         mlx5_fw_tracer_cleanup(dev->tracer);
1145 err_fw_tracer:
1146         mlx5_eq_table_destroy(dev);
1147 err_eq_table:
1148         mlx5_irq_table_destroy(dev);
1149 err_irq_table:
1150         mlx5_pagealloc_stop(dev);
1151         mlx5_events_stop(dev);
1152         mlx5_put_uars_page(dev, dev->priv.uar);
1153         return err;
1154 }
1155
1156 static void mlx5_unload(struct mlx5_core_dev *dev)
1157 {
1158         mlx5_ec_cleanup(dev);
1159         mlx5_sriov_detach(dev);
1160         mlx5_cleanup_fs(dev);
1161         mlx5_accel_ipsec_cleanup(dev);
1162         mlx5_accel_tls_cleanup(dev);
1163         mlx5_fpga_device_stop(dev);
1164         mlx5_rsc_dump_cleanup(dev);
1165         mlx5_hv_vhca_cleanup(dev->hv_vhca);
1166         mlx5_fw_tracer_cleanup(dev->tracer);
1167         mlx5_eq_table_destroy(dev);
1168         mlx5_irq_table_destroy(dev);
1169         mlx5_pagealloc_stop(dev);
1170         mlx5_events_stop(dev);
1171         mlx5_put_uars_page(dev, dev->priv.uar);
1172 }
1173
1174 int mlx5_load_one(struct mlx5_core_dev *dev, bool boot)
1175 {
1176         int err = 0;
1177
1178         mutex_lock(&dev->intf_state_mutex);
1179         if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1180                 mlx5_core_warn(dev, "interface is up, NOP\n");
1181                 goto out;
1182         }
1183         /* remove any previous indication of internal error */
1184         dev->state = MLX5_DEVICE_STATE_UP;
1185
1186         err = mlx5_function_setup(dev, boot);
1187         if (err)
1188                 goto err_function;
1189
1190         if (boot) {
1191                 err = mlx5_init_once(dev);
1192                 if (err) {
1193                         mlx5_core_err(dev, "sw objs init failed\n");
1194                         goto function_teardown;
1195                 }
1196         }
1197
1198         err = mlx5_load(dev);
1199         if (err)
1200                 goto err_load;
1201
1202         if (boot) {
1203                 err = mlx5_devlink_register(priv_to_devlink(dev), dev->device);
1204                 if (err)
1205                         goto err_devlink_reg;
1206         }
1207
1208         if (mlx5_device_registered(dev))
1209                 mlx5_attach_device(dev);
1210         else
1211                 mlx5_register_device(dev);
1212
1213         set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1214
1215         mutex_unlock(&dev->intf_state_mutex);
1216         return 0;
1217
1218 err_devlink_reg:
1219         mlx5_unload(dev);
1220 err_load:
1221         if (boot)
1222                 mlx5_cleanup_once(dev);
1223 function_teardown:
1224         mlx5_function_teardown(dev, boot);
1225 err_function:
1226         dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
1227 out:
1228         mutex_unlock(&dev->intf_state_mutex);
1229         return err;
1230 }
1231
1232 void mlx5_unload_one(struct mlx5_core_dev *dev, bool cleanup)
1233 {
1234         if (cleanup)
1235                 mlx5_unregister_device(dev);
1236
1237         mutex_lock(&dev->intf_state_mutex);
1238         if (!test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1239                 mlx5_core_warn(dev, "%s: interface is down, NOP\n",
1240                                __func__);
1241                 if (cleanup)
1242                         mlx5_cleanup_once(dev);
1243                 goto out;
1244         }
1245
1246         clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1247
1248         if (mlx5_device_registered(dev))
1249                 mlx5_detach_device(dev);
1250
1251         mlx5_unload(dev);
1252
1253         if (cleanup)
1254                 mlx5_cleanup_once(dev);
1255
1256         mlx5_function_teardown(dev, cleanup);
1257 out:
1258         mutex_unlock(&dev->intf_state_mutex);
1259 }
1260
1261 static int mlx5_mdev_init(struct mlx5_core_dev *dev, int profile_idx)
1262 {
1263         struct mlx5_priv *priv = &dev->priv;
1264         int err;
1265
1266         dev->profile = &profile[profile_idx];
1267
1268         INIT_LIST_HEAD(&priv->ctx_list);
1269         spin_lock_init(&priv->ctx_lock);
1270         mutex_init(&dev->intf_state_mutex);
1271
1272         mutex_init(&priv->bfregs.reg_head.lock);
1273         mutex_init(&priv->bfregs.wc_head.lock);
1274         INIT_LIST_HEAD(&priv->bfregs.reg_head.list);
1275         INIT_LIST_HEAD(&priv->bfregs.wc_head.list);
1276
1277         mutex_init(&priv->alloc_mutex);
1278         mutex_init(&priv->pgdir_mutex);
1279         INIT_LIST_HEAD(&priv->pgdir_list);
1280
1281         priv->dbg_root = debugfs_create_dir(dev_name(dev->device),
1282                                             mlx5_debugfs_root);
1283         if (!priv->dbg_root) {
1284                 dev_err(dev->device, "mlx5_core: error, Cannot create debugfs dir, aborting\n");
1285                 goto err_dbg_root;
1286         }
1287
1288         err = mlx5_health_init(dev);
1289         if (err)
1290                 goto err_health_init;
1291
1292         err = mlx5_pagealloc_init(dev);
1293         if (err)
1294                 goto err_pagealloc_init;
1295
1296         return 0;
1297
1298 err_pagealloc_init:
1299         mlx5_health_cleanup(dev);
1300 err_health_init:
1301         debugfs_remove(dev->priv.dbg_root);
1302 err_dbg_root:
1303         mutex_destroy(&priv->pgdir_mutex);
1304         mutex_destroy(&priv->alloc_mutex);
1305         mutex_destroy(&priv->bfregs.wc_head.lock);
1306         mutex_destroy(&priv->bfregs.reg_head.lock);
1307         mutex_destroy(&dev->intf_state_mutex);
1308         return err;
1309 }
1310
1311 static void mlx5_mdev_uninit(struct mlx5_core_dev *dev)
1312 {
1313         struct mlx5_priv *priv = &dev->priv;
1314
1315         mlx5_pagealloc_cleanup(dev);
1316         mlx5_health_cleanup(dev);
1317         debugfs_remove_recursive(dev->priv.dbg_root);
1318         mutex_destroy(&priv->pgdir_mutex);
1319         mutex_destroy(&priv->alloc_mutex);
1320         mutex_destroy(&priv->bfregs.wc_head.lock);
1321         mutex_destroy(&priv->bfregs.reg_head.lock);
1322         mutex_destroy(&dev->intf_state_mutex);
1323 }
1324
1325 #define MLX5_IB_MOD "mlx5_ib"
1326 static int init_one(struct pci_dev *pdev, const struct pci_device_id *id)
1327 {
1328         struct mlx5_core_dev *dev;
1329         struct devlink *devlink;
1330         int err;
1331
1332         devlink = mlx5_devlink_alloc();
1333         if (!devlink) {
1334                 dev_err(&pdev->dev, "devlink alloc failed\n");
1335                 return -ENOMEM;
1336         }
1337
1338         dev = devlink_priv(devlink);
1339         dev->device = &pdev->dev;
1340         dev->pdev = pdev;
1341
1342         dev->coredev_type = id->driver_data & MLX5_PCI_DEV_IS_VF ?
1343                          MLX5_COREDEV_VF : MLX5_COREDEV_PF;
1344
1345         err = mlx5_mdev_init(dev, prof_sel);
1346         if (err)
1347                 goto mdev_init_err;
1348
1349         err = mlx5_pci_init(dev, pdev, id);
1350         if (err) {
1351                 mlx5_core_err(dev, "mlx5_pci_init failed with error code %d\n",
1352                               err);
1353                 goto pci_init_err;
1354         }
1355
1356         err = mlx5_load_one(dev, true);
1357         if (err) {
1358                 mlx5_core_err(dev, "mlx5_load_one failed with error code %d\n",
1359                               err);
1360                 goto err_load_one;
1361         }
1362
1363         request_module_nowait(MLX5_IB_MOD);
1364
1365         err = mlx5_crdump_enable(dev);
1366         if (err)
1367                 dev_err(&pdev->dev, "mlx5_crdump_enable failed with error code %d\n", err);
1368
1369         pci_save_state(pdev);
1370         devlink_reload_enable(devlink);
1371         return 0;
1372
1373 err_load_one:
1374         mlx5_pci_close(dev);
1375 pci_init_err:
1376         mlx5_mdev_uninit(dev);
1377 mdev_init_err:
1378         mlx5_devlink_free(devlink);
1379
1380         return err;
1381 }
1382
1383 static void remove_one(struct pci_dev *pdev)
1384 {
1385         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1386         struct devlink *devlink = priv_to_devlink(dev);
1387
1388         devlink_reload_disable(devlink);
1389         mlx5_crdump_disable(dev);
1390         mlx5_devlink_unregister(devlink);
1391
1392         mlx5_drain_health_wq(dev);
1393         mlx5_unload_one(dev, true);
1394         mlx5_pci_close(dev);
1395         mlx5_mdev_uninit(dev);
1396         mlx5_devlink_free(devlink);
1397 }
1398
1399 static pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev,
1400                                               pci_channel_state_t state)
1401 {
1402         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1403
1404         mlx5_core_info(dev, "%s was called\n", __func__);
1405
1406         mlx5_enter_error_state(dev, false);
1407         mlx5_error_sw_reset(dev);
1408         mlx5_unload_one(dev, false);
1409         mlx5_drain_health_wq(dev);
1410         mlx5_pci_disable_device(dev);
1411
1412         return state == pci_channel_io_perm_failure ?
1413                 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1414 }
1415
1416 /* wait for the device to show vital signs by waiting
1417  * for the health counter to start counting.
1418  */
1419 static int wait_vital(struct pci_dev *pdev)
1420 {
1421         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1422         struct mlx5_core_health *health = &dev->priv.health;
1423         const int niter = 100;
1424         u32 last_count = 0;
1425         u32 count;
1426         int i;
1427
1428         for (i = 0; i < niter; i++) {
1429                 count = ioread32be(health->health_counter);
1430                 if (count && count != 0xffffffff) {
1431                         if (last_count && last_count != count) {
1432                                 mlx5_core_info(dev,
1433                                                "wait vital counter value 0x%x after %d iterations\n",
1434                                                count, i);
1435                                 return 0;
1436                         }
1437                         last_count = count;
1438                 }
1439                 msleep(50);
1440         }
1441
1442         return -ETIMEDOUT;
1443 }
1444
1445 static pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev)
1446 {
1447         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1448         int err;
1449
1450         mlx5_core_info(dev, "%s was called\n", __func__);
1451
1452         err = mlx5_pci_enable_device(dev);
1453         if (err) {
1454                 mlx5_core_err(dev, "%s: mlx5_pci_enable_device failed with error code: %d\n",
1455                               __func__, err);
1456                 return PCI_ERS_RESULT_DISCONNECT;
1457         }
1458
1459         pci_set_master(pdev);
1460         pci_restore_state(pdev);
1461         pci_save_state(pdev);
1462
1463         if (wait_vital(pdev)) {
1464                 mlx5_core_err(dev, "%s: wait_vital timed out\n", __func__);
1465                 return PCI_ERS_RESULT_DISCONNECT;
1466         }
1467
1468         return PCI_ERS_RESULT_RECOVERED;
1469 }
1470
1471 static void mlx5_pci_resume(struct pci_dev *pdev)
1472 {
1473         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1474         int err;
1475
1476         mlx5_core_info(dev, "%s was called\n", __func__);
1477
1478         err = mlx5_load_one(dev, false);
1479         if (err)
1480                 mlx5_core_err(dev, "%s: mlx5_load_one failed with error code: %d\n",
1481                               __func__, err);
1482         else
1483                 mlx5_core_info(dev, "%s: device recovered\n", __func__);
1484 }
1485
1486 static const struct pci_error_handlers mlx5_err_handler = {
1487         .error_detected = mlx5_pci_err_detected,
1488         .slot_reset     = mlx5_pci_slot_reset,
1489         .resume         = mlx5_pci_resume
1490 };
1491
1492 static int mlx5_try_fast_unload(struct mlx5_core_dev *dev)
1493 {
1494         bool fast_teardown = false, force_teardown = false;
1495         int ret = 1;
1496
1497         fast_teardown = MLX5_CAP_GEN(dev, fast_teardown);
1498         force_teardown = MLX5_CAP_GEN(dev, force_teardown);
1499
1500         mlx5_core_dbg(dev, "force teardown firmware support=%d\n", force_teardown);
1501         mlx5_core_dbg(dev, "fast teardown firmware support=%d\n", fast_teardown);
1502
1503         if (!fast_teardown && !force_teardown)
1504                 return -EOPNOTSUPP;
1505
1506         if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
1507                 mlx5_core_dbg(dev, "Device in internal error state, giving up\n");
1508                 return -EAGAIN;
1509         }
1510
1511         /* Panic tear down fw command will stop the PCI bus communication
1512          * with the HCA, so the health polll is no longer needed.
1513          */
1514         mlx5_drain_health_wq(dev);
1515         mlx5_stop_health_poll(dev, false);
1516
1517         ret = mlx5_cmd_fast_teardown_hca(dev);
1518         if (!ret)
1519                 goto succeed;
1520
1521         ret = mlx5_cmd_force_teardown_hca(dev);
1522         if (!ret)
1523                 goto succeed;
1524
1525         mlx5_core_dbg(dev, "Firmware couldn't do fast unload error: %d\n", ret);
1526         mlx5_start_health_poll(dev);
1527         return ret;
1528
1529 succeed:
1530         mlx5_enter_error_state(dev, true);
1531
1532         /* Some platforms requiring freeing the IRQ's in the shutdown
1533          * flow. If they aren't freed they can't be allocated after
1534          * kexec. There is no need to cleanup the mlx5_core software
1535          * contexts.
1536          */
1537         mlx5_core_eq_free_irqs(dev);
1538
1539         return 0;
1540 }
1541
1542 static void shutdown(struct pci_dev *pdev)
1543 {
1544         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1545         int err;
1546
1547         mlx5_core_info(dev, "Shutdown was called\n");
1548         err = mlx5_try_fast_unload(dev);
1549         if (err)
1550                 mlx5_unload_one(dev, false);
1551         mlx5_pci_disable_device(dev);
1552 }
1553
1554 static int mlx5_suspend(struct pci_dev *pdev, pm_message_t state)
1555 {
1556         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1557
1558         mlx5_unload_one(dev, false);
1559
1560         return 0;
1561 }
1562
1563 static int mlx5_resume(struct pci_dev *pdev)
1564 {
1565         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1566
1567         return mlx5_load_one(dev, false);
1568 }
1569
1570 static const struct pci_device_id mlx5_core_pci_table[] = {
1571         { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTIB) },
1572         { PCI_VDEVICE(MELLANOX, 0x1012), MLX5_PCI_DEV_IS_VF},   /* Connect-IB VF */
1573         { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4) },
1574         { PCI_VDEVICE(MELLANOX, 0x1014), MLX5_PCI_DEV_IS_VF},   /* ConnectX-4 VF */
1575         { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4_LX) },
1576         { PCI_VDEVICE(MELLANOX, 0x1016), MLX5_PCI_DEV_IS_VF},   /* ConnectX-4LX VF */
1577         { PCI_VDEVICE(MELLANOX, 0x1017) },                      /* ConnectX-5, PCIe 3.0 */
1578         { PCI_VDEVICE(MELLANOX, 0x1018), MLX5_PCI_DEV_IS_VF},   /* ConnectX-5 VF */
1579         { PCI_VDEVICE(MELLANOX, 0x1019) },                      /* ConnectX-5 Ex */
1580         { PCI_VDEVICE(MELLANOX, 0x101a), MLX5_PCI_DEV_IS_VF},   /* ConnectX-5 Ex VF */
1581         { PCI_VDEVICE(MELLANOX, 0x101b) },                      /* ConnectX-6 */
1582         { PCI_VDEVICE(MELLANOX, 0x101c), MLX5_PCI_DEV_IS_VF},   /* ConnectX-6 VF */
1583         { PCI_VDEVICE(MELLANOX, 0x101d) },                      /* ConnectX-6 Dx */
1584         { PCI_VDEVICE(MELLANOX, 0x101e), MLX5_PCI_DEV_IS_VF},   /* ConnectX Family mlx5Gen Virtual Function */
1585         { PCI_VDEVICE(MELLANOX, 0x101f) },                      /* ConnectX-6 LX */
1586         { PCI_VDEVICE(MELLANOX, 0x1021) },                      /* ConnectX-7 */
1587         { PCI_VDEVICE(MELLANOX, 0xa2d2) },                      /* BlueField integrated ConnectX-5 network controller */
1588         { PCI_VDEVICE(MELLANOX, 0xa2d3), MLX5_PCI_DEV_IS_VF},   /* BlueField integrated ConnectX-5 network controller VF */
1589         { PCI_VDEVICE(MELLANOX, 0xa2d6) },                      /* BlueField-2 integrated ConnectX-6 Dx network controller */
1590         { 0, }
1591 };
1592
1593 MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
1594
1595 void mlx5_disable_device(struct mlx5_core_dev *dev)
1596 {
1597         mlx5_error_sw_reset(dev);
1598         mlx5_unload_one(dev, false);
1599 }
1600
1601 void mlx5_recover_device(struct mlx5_core_dev *dev)
1602 {
1603         mlx5_pci_disable_device(dev);
1604         if (mlx5_pci_slot_reset(dev->pdev) == PCI_ERS_RESULT_RECOVERED)
1605                 mlx5_pci_resume(dev->pdev);
1606 }
1607
1608 static struct pci_driver mlx5_core_driver = {
1609         .name           = DRIVER_NAME,
1610         .id_table       = mlx5_core_pci_table,
1611         .probe          = init_one,
1612         .remove         = remove_one,
1613         .suspend        = mlx5_suspend,
1614         .resume         = mlx5_resume,
1615         .shutdown       = shutdown,
1616         .err_handler    = &mlx5_err_handler,
1617         .sriov_configure   = mlx5_core_sriov_configure,
1618 };
1619
1620 static void mlx5_core_verify_params(void)
1621 {
1622         if (prof_sel >= ARRAY_SIZE(profile)) {
1623                 pr_warn("mlx5_core: WARNING: Invalid module parameter prof_sel %d, valid range 0-%zu, changing back to default(%d)\n",
1624                         prof_sel,
1625                         ARRAY_SIZE(profile) - 1,
1626                         MLX5_DEFAULT_PROF);
1627                 prof_sel = MLX5_DEFAULT_PROF;
1628         }
1629 }
1630
1631 static int __init init(void)
1632 {
1633         int err;
1634
1635         get_random_bytes(&sw_owner_id, sizeof(sw_owner_id));
1636
1637         mlx5_core_verify_params();
1638         mlx5_accel_ipsec_build_fs_cmds();
1639         mlx5_register_debugfs();
1640
1641         err = pci_register_driver(&mlx5_core_driver);
1642         if (err)
1643                 goto err_debug;
1644
1645 #ifdef CONFIG_MLX5_CORE_EN
1646         mlx5e_init();
1647 #endif
1648
1649         return 0;
1650
1651 err_debug:
1652         mlx5_unregister_debugfs();
1653         return err;
1654 }
1655
1656 static void __exit cleanup(void)
1657 {
1658 #ifdef CONFIG_MLX5_CORE_EN
1659         mlx5e_cleanup();
1660 #endif
1661         pci_unregister_driver(&mlx5_core_driver);
1662         mlx5_unregister_debugfs();
1663 }
1664
1665 module_init(init);
1666 module_exit(cleanup);