net/mlx5: E-Switch, Refactor eswitch ingress acl codes
[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 wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili,
181                         u32 warn_time_mili)
182 {
183         unsigned long warn = jiffies + msecs_to_jiffies(warn_time_mili);
184         unsigned long end = jiffies + msecs_to_jiffies(max_wait_mili);
185         int err = 0;
186
187         BUILD_BUG_ON(FW_PRE_INIT_TIMEOUT_MILI < FW_INIT_WARN_MESSAGE_INTERVAL);
188
189         while (fw_initializing(dev)) {
190                 if (time_after(jiffies, end)) {
191                         err = -EBUSY;
192                         break;
193                 }
194                 if (warn_time_mili && time_after(jiffies, warn)) {
195                         mlx5_core_warn(dev, "Waiting for FW initialization, timeout abort in %ds\n",
196                                        jiffies_to_msecs(end - warn) / 1000);
197                         warn = jiffies + msecs_to_jiffies(warn_time_mili);
198                 }
199                 msleep(FW_INIT_WAIT_MS);
200         }
201
202         return err;
203 }
204
205 static void mlx5_set_driver_version(struct mlx5_core_dev *dev)
206 {
207         int driver_ver_sz = MLX5_FLD_SZ_BYTES(set_driver_version_in,
208                                               driver_version);
209         u8 in[MLX5_ST_SZ_BYTES(set_driver_version_in)] = {};
210         int remaining_size = driver_ver_sz;
211         char *string;
212
213         if (!MLX5_CAP_GEN(dev, driver_version))
214                 return;
215
216         string = MLX5_ADDR_OF(set_driver_version_in, in, driver_version);
217
218         strncpy(string, "Linux", remaining_size);
219
220         remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
221         strncat(string, ",", remaining_size);
222
223         remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
224         strncat(string, DRIVER_NAME, remaining_size);
225
226         remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
227         strncat(string, ",", remaining_size);
228
229         remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
230         strncat(string, DRIVER_VERSION, remaining_size);
231
232         /*Send the command*/
233         MLX5_SET(set_driver_version_in, in, opcode,
234                  MLX5_CMD_OP_SET_DRIVER_VERSION);
235
236         mlx5_cmd_exec_in(dev, set_driver_version, in);
237 }
238
239 static int set_dma_caps(struct pci_dev *pdev)
240 {
241         int err;
242
243         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
244         if (err) {
245                 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
246                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
247                 if (err) {
248                         dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
249                         return err;
250                 }
251         }
252
253         err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
254         if (err) {
255                 dev_warn(&pdev->dev,
256                          "Warning: couldn't set 64-bit consistent PCI DMA mask\n");
257                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
258                 if (err) {
259                         dev_err(&pdev->dev,
260                                 "Can't set consistent PCI DMA mask, aborting\n");
261                         return err;
262                 }
263         }
264
265         dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
266         return err;
267 }
268
269 static int mlx5_pci_enable_device(struct mlx5_core_dev *dev)
270 {
271         struct pci_dev *pdev = dev->pdev;
272         int err = 0;
273
274         mutex_lock(&dev->pci_status_mutex);
275         if (dev->pci_status == MLX5_PCI_STATUS_DISABLED) {
276                 err = pci_enable_device(pdev);
277                 if (!err)
278                         dev->pci_status = MLX5_PCI_STATUS_ENABLED;
279         }
280         mutex_unlock(&dev->pci_status_mutex);
281
282         return err;
283 }
284
285 static void mlx5_pci_disable_device(struct mlx5_core_dev *dev)
286 {
287         struct pci_dev *pdev = dev->pdev;
288
289         mutex_lock(&dev->pci_status_mutex);
290         if (dev->pci_status == MLX5_PCI_STATUS_ENABLED) {
291                 pci_disable_device(pdev);
292                 dev->pci_status = MLX5_PCI_STATUS_DISABLED;
293         }
294         mutex_unlock(&dev->pci_status_mutex);
295 }
296
297 static int request_bar(struct pci_dev *pdev)
298 {
299         int err = 0;
300
301         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
302                 dev_err(&pdev->dev, "Missing registers BAR, aborting\n");
303                 return -ENODEV;
304         }
305
306         err = pci_request_regions(pdev, DRIVER_NAME);
307         if (err)
308                 dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
309
310         return err;
311 }
312
313 static void release_bar(struct pci_dev *pdev)
314 {
315         pci_release_regions(pdev);
316 }
317
318 struct mlx5_reg_host_endianness {
319         u8      he;
320         u8      rsvd[15];
321 };
322
323 #define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos))
324
325 enum {
326         MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) |
327                                 MLX5_DEV_CAP_FLAG_DCT,
328 };
329
330 static u16 to_fw_pkey_sz(struct mlx5_core_dev *dev, u32 size)
331 {
332         switch (size) {
333         case 128:
334                 return 0;
335         case 256:
336                 return 1;
337         case 512:
338                 return 2;
339         case 1024:
340                 return 3;
341         case 2048:
342                 return 4;
343         case 4096:
344                 return 5;
345         default:
346                 mlx5_core_warn(dev, "invalid pkey table size %d\n", size);
347                 return 0;
348         }
349 }
350
351 static int mlx5_core_get_caps_mode(struct mlx5_core_dev *dev,
352                                    enum mlx5_cap_type cap_type,
353                                    enum mlx5_cap_mode cap_mode)
354 {
355         u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
356         int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
357         void *out, *hca_caps;
358         u16 opmod = (cap_type << 1) | (cap_mode & 0x01);
359         int err;
360
361         memset(in, 0, sizeof(in));
362         out = kzalloc(out_sz, GFP_KERNEL);
363         if (!out)
364                 return -ENOMEM;
365
366         MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
367         MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
368         err = mlx5_cmd_exec_inout(dev, query_hca_cap, in, out);
369         if (err) {
370                 mlx5_core_warn(dev,
371                                "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n",
372                                cap_type, cap_mode, err);
373                 goto query_ex;
374         }
375
376         hca_caps =  MLX5_ADDR_OF(query_hca_cap_out, out, capability);
377
378         switch (cap_mode) {
379         case HCA_CAP_OPMOD_GET_MAX:
380                 memcpy(dev->caps.hca_max[cap_type], hca_caps,
381                        MLX5_UN_SZ_BYTES(hca_cap_union));
382                 break;
383         case HCA_CAP_OPMOD_GET_CUR:
384                 memcpy(dev->caps.hca_cur[cap_type], hca_caps,
385                        MLX5_UN_SZ_BYTES(hca_cap_union));
386                 break;
387         default:
388                 mlx5_core_warn(dev,
389                                "Tried to query dev cap type(%x) with wrong opmode(%x)\n",
390                                cap_type, cap_mode);
391                 err = -EINVAL;
392                 break;
393         }
394 query_ex:
395         kfree(out);
396         return err;
397 }
398
399 int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type)
400 {
401         int ret;
402
403         ret = mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_CUR);
404         if (ret)
405                 return ret;
406         return mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_MAX);
407 }
408
409 static int set_caps(struct mlx5_core_dev *dev, void *in, int opmod)
410 {
411         MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
412         MLX5_SET(set_hca_cap_in, in, op_mod, opmod << 1);
413         return mlx5_cmd_exec_in(dev, set_hca_cap, in);
414 }
415
416 static int handle_hca_cap_atomic(struct mlx5_core_dev *dev, void *set_ctx)
417 {
418         void *set_hca_cap;
419         int req_endianness;
420         int err;
421
422         if (!MLX5_CAP_GEN(dev, atomic))
423                 return 0;
424
425         err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC);
426         if (err)
427                 return err;
428
429         req_endianness =
430                 MLX5_CAP_ATOMIC(dev,
431                                 supported_atomic_req_8B_endianness_mode_1);
432
433         if (req_endianness != MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS)
434                 return 0;
435
436         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
437
438         /* Set requestor to host endianness */
439         MLX5_SET(atomic_caps, set_hca_cap, atomic_req_8B_endianness_mode,
440                  MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS);
441
442         return set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MOD_ATOMIC);
443 }
444
445 static int handle_hca_cap_odp(struct mlx5_core_dev *dev, void *set_ctx)
446 {
447         void *set_hca_cap;
448         bool do_set = false;
449         int err;
450
451         if (!IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING) ||
452             !MLX5_CAP_GEN(dev, pg))
453                 return 0;
454
455         err = mlx5_core_get_caps(dev, MLX5_CAP_ODP);
456         if (err)
457                 return err;
458
459         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
460         memcpy(set_hca_cap, dev->caps.hca_cur[MLX5_CAP_ODP],
461                MLX5_ST_SZ_BYTES(odp_cap));
462
463 #define ODP_CAP_SET_MAX(dev, field)                                            \
464         do {                                                                   \
465                 u32 _res = MLX5_CAP_ODP_MAX(dev, field);                       \
466                 if (_res) {                                                    \
467                         do_set = true;                                         \
468                         MLX5_SET(odp_cap, set_hca_cap, field, _res);           \
469                 }                                                              \
470         } while (0)
471
472         ODP_CAP_SET_MAX(dev, ud_odp_caps.srq_receive);
473         ODP_CAP_SET_MAX(dev, rc_odp_caps.srq_receive);
474         ODP_CAP_SET_MAX(dev, xrc_odp_caps.srq_receive);
475         ODP_CAP_SET_MAX(dev, xrc_odp_caps.send);
476         ODP_CAP_SET_MAX(dev, xrc_odp_caps.receive);
477         ODP_CAP_SET_MAX(dev, xrc_odp_caps.write);
478         ODP_CAP_SET_MAX(dev, xrc_odp_caps.read);
479         ODP_CAP_SET_MAX(dev, xrc_odp_caps.atomic);
480         ODP_CAP_SET_MAX(dev, dc_odp_caps.srq_receive);
481         ODP_CAP_SET_MAX(dev, dc_odp_caps.send);
482         ODP_CAP_SET_MAX(dev, dc_odp_caps.receive);
483         ODP_CAP_SET_MAX(dev, dc_odp_caps.write);
484         ODP_CAP_SET_MAX(dev, dc_odp_caps.read);
485         ODP_CAP_SET_MAX(dev, dc_odp_caps.atomic);
486
487         if (!do_set)
488                 return 0;
489
490         return set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MOD_ODP);
491 }
492
493 static int handle_hca_cap(struct mlx5_core_dev *dev, void *set_ctx)
494 {
495         struct mlx5_profile *prof = dev->profile;
496         void *set_hca_cap;
497         int err;
498
499         err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL);
500         if (err)
501                 return err;
502
503         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
504                                    capability);
505         memcpy(set_hca_cap, dev->caps.hca_cur[MLX5_CAP_GENERAL],
506                MLX5_ST_SZ_BYTES(cmd_hca_cap));
507
508         mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
509                       mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
510                       128);
511         /* we limit the size of the pkey table to 128 entries for now */
512         MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
513                  to_fw_pkey_sz(dev, 128));
514
515         /* Check log_max_qp from HCA caps to set in current profile */
516         if (MLX5_CAP_GEN_MAX(dev, log_max_qp) < profile[prof_sel].log_max_qp) {
517                 mlx5_core_warn(dev, "log_max_qp value in current profile is %d, changing it to HCA capability limit (%d)\n",
518                                profile[prof_sel].log_max_qp,
519                                MLX5_CAP_GEN_MAX(dev, log_max_qp));
520                 profile[prof_sel].log_max_qp = MLX5_CAP_GEN_MAX(dev, log_max_qp);
521         }
522         if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
523                 MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
524                          prof->log_max_qp);
525
526         /* disable cmdif checksum */
527         MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
528
529         /* Enable 4K UAR only when HCA supports it and page size is bigger
530          * than 4K.
531          */
532         if (MLX5_CAP_GEN_MAX(dev, uar_4k) && PAGE_SIZE > 4096)
533                 MLX5_SET(cmd_hca_cap, set_hca_cap, uar_4k, 1);
534
535         MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12);
536
537         if (MLX5_CAP_GEN_MAX(dev, cache_line_128byte))
538                 MLX5_SET(cmd_hca_cap,
539                          set_hca_cap,
540                          cache_line_128byte,
541                          cache_line_size() >= 128 ? 1 : 0);
542
543         if (MLX5_CAP_GEN_MAX(dev, dct))
544                 MLX5_SET(cmd_hca_cap, set_hca_cap, dct, 1);
545
546         if (MLX5_CAP_GEN_MAX(dev, num_vhca_ports))
547                 MLX5_SET(cmd_hca_cap,
548                          set_hca_cap,
549                          num_vhca_ports,
550                          MLX5_CAP_GEN_MAX(dev, num_vhca_ports));
551
552         if (MLX5_CAP_GEN_MAX(dev, release_all_pages))
553                 MLX5_SET(cmd_hca_cap, set_hca_cap, release_all_pages, 1);
554
555         return set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE);
556 }
557
558 static int handle_hca_cap_roce(struct mlx5_core_dev *dev, void *set_ctx)
559 {
560         void *set_hca_cap;
561         int err;
562
563         if (!MLX5_CAP_GEN(dev, roce))
564                 return 0;
565
566         err = mlx5_core_get_caps(dev, MLX5_CAP_ROCE);
567         if (err)
568                 return err;
569
570         if (MLX5_CAP_ROCE(dev, sw_r_roce_src_udp_port) ||
571             !MLX5_CAP_ROCE_MAX(dev, sw_r_roce_src_udp_port))
572                 return 0;
573
574         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
575         memcpy(set_hca_cap, dev->caps.hca_cur[MLX5_CAP_ROCE],
576                MLX5_ST_SZ_BYTES(roce_cap));
577         MLX5_SET(roce_cap, set_hca_cap, sw_r_roce_src_udp_port, 1);
578
579         err = set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MOD_ROCE);
580         return err;
581 }
582
583 static int set_hca_cap(struct mlx5_core_dev *dev)
584 {
585         int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
586         void *set_ctx;
587         int err;
588
589         set_ctx = kzalloc(set_sz, GFP_KERNEL);
590         if (!set_ctx)
591                 return -ENOMEM;
592
593         err = handle_hca_cap(dev, set_ctx);
594         if (err) {
595                 mlx5_core_err(dev, "handle_hca_cap failed\n");
596                 goto out;
597         }
598
599         memset(set_ctx, 0, set_sz);
600         err = handle_hca_cap_atomic(dev, set_ctx);
601         if (err) {
602                 mlx5_core_err(dev, "handle_hca_cap_atomic failed\n");
603                 goto out;
604         }
605
606         memset(set_ctx, 0, set_sz);
607         err = handle_hca_cap_odp(dev, set_ctx);
608         if (err) {
609                 mlx5_core_err(dev, "handle_hca_cap_odp failed\n");
610                 goto out;
611         }
612
613         memset(set_ctx, 0, set_sz);
614         err = handle_hca_cap_roce(dev, set_ctx);
615         if (err) {
616                 mlx5_core_err(dev, "handle_hca_cap_roce failed\n");
617                 goto out;
618         }
619
620 out:
621         kfree(set_ctx);
622         return err;
623 }
624
625 static int set_hca_ctrl(struct mlx5_core_dev *dev)
626 {
627         struct mlx5_reg_host_endianness he_in;
628         struct mlx5_reg_host_endianness he_out;
629         int err;
630
631         if (!mlx5_core_is_pf(dev))
632                 return 0;
633
634         memset(&he_in, 0, sizeof(he_in));
635         he_in.he = MLX5_SET_HOST_ENDIANNESS;
636         err = mlx5_core_access_reg(dev, &he_in,  sizeof(he_in),
637                                         &he_out, sizeof(he_out),
638                                         MLX5_REG_HOST_ENDIANNESS, 0, 1);
639         return err;
640 }
641
642 static int mlx5_core_set_hca_defaults(struct mlx5_core_dev *dev)
643 {
644         int ret = 0;
645
646         /* Disable local_lb by default */
647         if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH)
648                 ret = mlx5_nic_vport_update_local_lb(dev, false);
649
650         return ret;
651 }
652
653 int mlx5_core_enable_hca(struct mlx5_core_dev *dev, u16 func_id)
654 {
655         u32 in[MLX5_ST_SZ_DW(enable_hca_in)] = {};
656
657         MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA);
658         MLX5_SET(enable_hca_in, in, function_id, func_id);
659         MLX5_SET(enable_hca_in, in, embedded_cpu_function,
660                  dev->caps.embedded_cpu);
661         return mlx5_cmd_exec_in(dev, enable_hca, in);
662 }
663
664 int mlx5_core_disable_hca(struct mlx5_core_dev *dev, u16 func_id)
665 {
666         u32 in[MLX5_ST_SZ_DW(disable_hca_in)] = {};
667
668         MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA);
669         MLX5_SET(disable_hca_in, in, function_id, func_id);
670         MLX5_SET(enable_hca_in, in, embedded_cpu_function,
671                  dev->caps.embedded_cpu);
672         return mlx5_cmd_exec_in(dev, disable_hca, in);
673 }
674
675 static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
676 {
677         u32 query_out[MLX5_ST_SZ_DW(query_issi_out)] = {};
678         u32 query_in[MLX5_ST_SZ_DW(query_issi_in)] = {};
679         u32 sup_issi;
680         int err;
681
682         MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
683         err = mlx5_cmd_exec_inout(dev, query_issi, query_in, query_out);
684         if (err) {
685                 u32 syndrome;
686                 u8 status;
687
688                 mlx5_cmd_mbox_status(query_out, &status, &syndrome);
689                 if (!status || syndrome == MLX5_DRIVER_SYND) {
690                         mlx5_core_err(dev, "Failed to query ISSI err(%d) status(%d) synd(%d)\n",
691                                       err, status, syndrome);
692                         return err;
693                 }
694
695                 mlx5_core_warn(dev, "Query ISSI is not supported by FW, ISSI is 0\n");
696                 dev->issi = 0;
697                 return 0;
698         }
699
700         sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);
701
702         if (sup_issi & (1 << 1)) {
703                 u32 set_in[MLX5_ST_SZ_DW(set_issi_in)] = {};
704
705                 MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
706                 MLX5_SET(set_issi_in, set_in, current_issi, 1);
707                 err = mlx5_cmd_exec_in(dev, set_issi, set_in);
708                 if (err) {
709                         mlx5_core_err(dev, "Failed to set ISSI to 1 err(%d)\n",
710                                       err);
711                         return err;
712                 }
713
714                 dev->issi = 1;
715
716                 return 0;
717         } else if (sup_issi & (1 << 0) || !sup_issi) {
718                 return 0;
719         }
720
721         return -EOPNOTSUPP;
722 }
723
724 static int mlx5_pci_init(struct mlx5_core_dev *dev, struct pci_dev *pdev,
725                          const struct pci_device_id *id)
726 {
727         struct mlx5_priv *priv = &dev->priv;
728         int err = 0;
729
730         mutex_init(&dev->pci_status_mutex);
731         pci_set_drvdata(dev->pdev, dev);
732
733         dev->bar_addr = pci_resource_start(pdev, 0);
734         priv->numa_node = dev_to_node(&dev->pdev->dev);
735
736         err = mlx5_pci_enable_device(dev);
737         if (err) {
738                 mlx5_core_err(dev, "Cannot enable PCI device, aborting\n");
739                 return err;
740         }
741
742         err = request_bar(pdev);
743         if (err) {
744                 mlx5_core_err(dev, "error requesting BARs, aborting\n");
745                 goto err_disable;
746         }
747
748         pci_set_master(pdev);
749
750         err = set_dma_caps(pdev);
751         if (err) {
752                 mlx5_core_err(dev, "Failed setting DMA capabilities mask, aborting\n");
753                 goto err_clr_master;
754         }
755
756         if (pci_enable_atomic_ops_to_root(pdev, PCI_EXP_DEVCAP2_ATOMIC_COMP32) &&
757             pci_enable_atomic_ops_to_root(pdev, PCI_EXP_DEVCAP2_ATOMIC_COMP64) &&
758             pci_enable_atomic_ops_to_root(pdev, PCI_EXP_DEVCAP2_ATOMIC_COMP128))
759                 mlx5_core_dbg(dev, "Enabling pci atomics failed\n");
760
761         dev->iseg_base = dev->bar_addr;
762         dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
763         if (!dev->iseg) {
764                 err = -ENOMEM;
765                 mlx5_core_err(dev, "Failed mapping initialization segment, aborting\n");
766                 goto err_clr_master;
767         }
768
769         mlx5_pci_vsc_init(dev);
770         dev->caps.embedded_cpu = mlx5_read_embedded_cpu(dev);
771         return 0;
772
773 err_clr_master:
774         pci_clear_master(dev->pdev);
775         release_bar(dev->pdev);
776 err_disable:
777         mlx5_pci_disable_device(dev);
778         return err;
779 }
780
781 static void mlx5_pci_close(struct mlx5_core_dev *dev)
782 {
783         iounmap(dev->iseg);
784         pci_clear_master(dev->pdev);
785         release_bar(dev->pdev);
786         mlx5_pci_disable_device(dev);
787 }
788
789 static int mlx5_init_once(struct mlx5_core_dev *dev)
790 {
791         int err;
792
793         dev->priv.devcom = mlx5_devcom_register_device(dev);
794         if (IS_ERR(dev->priv.devcom))
795                 mlx5_core_err(dev, "failed to register with devcom (0x%p)\n",
796                               dev->priv.devcom);
797
798         err = mlx5_query_board_id(dev);
799         if (err) {
800                 mlx5_core_err(dev, "query board id failed\n");
801                 goto err_devcom;
802         }
803
804         err = mlx5_irq_table_init(dev);
805         if (err) {
806                 mlx5_core_err(dev, "failed to initialize irq table\n");
807                 goto err_devcom;
808         }
809
810         err = mlx5_eq_table_init(dev);
811         if (err) {
812                 mlx5_core_err(dev, "failed to initialize eq\n");
813                 goto err_irq_cleanup;
814         }
815
816         err = mlx5_events_init(dev);
817         if (err) {
818                 mlx5_core_err(dev, "failed to initialize events\n");
819                 goto err_eq_cleanup;
820         }
821
822         mlx5_cq_debugfs_init(dev);
823
824         mlx5_init_reserved_gids(dev);
825
826         mlx5_init_clock(dev);
827
828         dev->vxlan = mlx5_vxlan_create(dev);
829         dev->geneve = mlx5_geneve_create(dev);
830
831         err = mlx5_init_rl_table(dev);
832         if (err) {
833                 mlx5_core_err(dev, "Failed to init rate limiting\n");
834                 goto err_tables_cleanup;
835         }
836
837         err = mlx5_mpfs_init(dev);
838         if (err) {
839                 mlx5_core_err(dev, "Failed to init l2 table %d\n", err);
840                 goto err_rl_cleanup;
841         }
842
843         err = mlx5_sriov_init(dev);
844         if (err) {
845                 mlx5_core_err(dev, "Failed to init sriov %d\n", err);
846                 goto err_mpfs_cleanup;
847         }
848
849         err = mlx5_eswitch_init(dev);
850         if (err) {
851                 mlx5_core_err(dev, "Failed to init eswitch %d\n", err);
852                 goto err_sriov_cleanup;
853         }
854
855         err = mlx5_fpga_init(dev);
856         if (err) {
857                 mlx5_core_err(dev, "Failed to init fpga device %d\n", err);
858                 goto err_eswitch_cleanup;
859         }
860
861         dev->dm = mlx5_dm_create(dev);
862         if (IS_ERR(dev->dm))
863                 mlx5_core_warn(dev, "Failed to init device memory%d\n", err);
864
865         dev->tracer = mlx5_fw_tracer_create(dev);
866         dev->hv_vhca = mlx5_hv_vhca_create(dev);
867         dev->rsc_dump = mlx5_rsc_dump_create(dev);
868
869         return 0;
870
871 err_eswitch_cleanup:
872         mlx5_eswitch_cleanup(dev->priv.eswitch);
873 err_sriov_cleanup:
874         mlx5_sriov_cleanup(dev);
875 err_mpfs_cleanup:
876         mlx5_mpfs_cleanup(dev);
877 err_rl_cleanup:
878         mlx5_cleanup_rl_table(dev);
879 err_tables_cleanup:
880         mlx5_geneve_destroy(dev->geneve);
881         mlx5_vxlan_destroy(dev->vxlan);
882         mlx5_cq_debugfs_cleanup(dev);
883         mlx5_events_cleanup(dev);
884 err_eq_cleanup:
885         mlx5_eq_table_cleanup(dev);
886 err_irq_cleanup:
887         mlx5_irq_table_cleanup(dev);
888 err_devcom:
889         mlx5_devcom_unregister_device(dev->priv.devcom);
890
891         return err;
892 }
893
894 static void mlx5_cleanup_once(struct mlx5_core_dev *dev)
895 {
896         mlx5_rsc_dump_destroy(dev);
897         mlx5_hv_vhca_destroy(dev->hv_vhca);
898         mlx5_fw_tracer_destroy(dev->tracer);
899         mlx5_dm_cleanup(dev);
900         mlx5_fpga_cleanup(dev);
901         mlx5_eswitch_cleanup(dev->priv.eswitch);
902         mlx5_sriov_cleanup(dev);
903         mlx5_mpfs_cleanup(dev);
904         mlx5_cleanup_rl_table(dev);
905         mlx5_geneve_destroy(dev->geneve);
906         mlx5_vxlan_destroy(dev->vxlan);
907         mlx5_cleanup_clock(dev);
908         mlx5_cleanup_reserved_gids(dev);
909         mlx5_cq_debugfs_cleanup(dev);
910         mlx5_events_cleanup(dev);
911         mlx5_eq_table_cleanup(dev);
912         mlx5_irq_table_cleanup(dev);
913         mlx5_devcom_unregister_device(dev->priv.devcom);
914 }
915
916 static int mlx5_function_setup(struct mlx5_core_dev *dev, bool boot)
917 {
918         int err;
919
920         mlx5_core_info(dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
921                        fw_rev_min(dev), fw_rev_sub(dev));
922
923         /* Only PFs hold the relevant PCIe information for this query */
924         if (mlx5_core_is_pf(dev))
925                 pcie_print_link_status(dev->pdev);
926
927         /* wait for firmware to accept initialization segments configurations
928          */
929         err = wait_fw_init(dev, FW_PRE_INIT_TIMEOUT_MILI, FW_INIT_WARN_MESSAGE_INTERVAL);
930         if (err) {
931                 mlx5_core_err(dev, "Firmware over %d MS in pre-initializing state, aborting\n",
932                               FW_PRE_INIT_TIMEOUT_MILI);
933                 return err;
934         }
935
936         err = mlx5_cmd_init(dev);
937         if (err) {
938                 mlx5_core_err(dev, "Failed initializing command interface, aborting\n");
939                 return err;
940         }
941
942         err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI, 0);
943         if (err) {
944                 mlx5_core_err(dev, "Firmware over %d MS in initializing state, aborting\n",
945                               FW_INIT_TIMEOUT_MILI);
946                 goto err_cmd_cleanup;
947         }
948
949         mlx5_cmd_set_state(dev, MLX5_CMDIF_STATE_UP);
950
951         err = mlx5_core_enable_hca(dev, 0);
952         if (err) {
953                 mlx5_core_err(dev, "enable hca failed\n");
954                 goto err_cmd_cleanup;
955         }
956
957         err = mlx5_core_set_issi(dev);
958         if (err) {
959                 mlx5_core_err(dev, "failed to set issi\n");
960                 goto err_disable_hca;
961         }
962
963         err = mlx5_satisfy_startup_pages(dev, 1);
964         if (err) {
965                 mlx5_core_err(dev, "failed to allocate boot pages\n");
966                 goto err_disable_hca;
967         }
968
969         err = set_hca_ctrl(dev);
970         if (err) {
971                 mlx5_core_err(dev, "set_hca_ctrl failed\n");
972                 goto reclaim_boot_pages;
973         }
974
975         err = set_hca_cap(dev);
976         if (err) {
977                 mlx5_core_err(dev, "set_hca_cap failed\n");
978                 goto reclaim_boot_pages;
979         }
980
981         err = mlx5_satisfy_startup_pages(dev, 0);
982         if (err) {
983                 mlx5_core_err(dev, "failed to allocate init pages\n");
984                 goto reclaim_boot_pages;
985         }
986
987         err = mlx5_cmd_init_hca(dev, sw_owner_id);
988         if (err) {
989                 mlx5_core_err(dev, "init hca failed\n");
990                 goto reclaim_boot_pages;
991         }
992
993         mlx5_set_driver_version(dev);
994
995         mlx5_start_health_poll(dev);
996
997         err = mlx5_query_hca_caps(dev);
998         if (err) {
999                 mlx5_core_err(dev, "query hca failed\n");
1000                 goto stop_health;
1001         }
1002
1003         return 0;
1004
1005 stop_health:
1006         mlx5_stop_health_poll(dev, boot);
1007 reclaim_boot_pages:
1008         mlx5_reclaim_startup_pages(dev);
1009 err_disable_hca:
1010         mlx5_core_disable_hca(dev, 0);
1011 err_cmd_cleanup:
1012         mlx5_cmd_set_state(dev, MLX5_CMDIF_STATE_DOWN);
1013         mlx5_cmd_cleanup(dev);
1014
1015         return err;
1016 }
1017
1018 static int mlx5_function_teardown(struct mlx5_core_dev *dev, bool boot)
1019 {
1020         int err;
1021
1022         mlx5_stop_health_poll(dev, boot);
1023         err = mlx5_cmd_teardown_hca(dev);
1024         if (err) {
1025                 mlx5_core_err(dev, "tear_down_hca failed, skip cleanup\n");
1026                 return err;
1027         }
1028         mlx5_reclaim_startup_pages(dev);
1029         mlx5_core_disable_hca(dev, 0);
1030         mlx5_cmd_set_state(dev, MLX5_CMDIF_STATE_DOWN);
1031         mlx5_cmd_cleanup(dev);
1032
1033         return 0;
1034 }
1035
1036 static int mlx5_load(struct mlx5_core_dev *dev)
1037 {
1038         int err;
1039
1040         dev->priv.uar = mlx5_get_uars_page(dev);
1041         if (IS_ERR(dev->priv.uar)) {
1042                 mlx5_core_err(dev, "Failed allocating uar, aborting\n");
1043                 err = PTR_ERR(dev->priv.uar);
1044                 return err;
1045         }
1046
1047         mlx5_events_start(dev);
1048         mlx5_pagealloc_start(dev);
1049
1050         err = mlx5_irq_table_create(dev);
1051         if (err) {
1052                 mlx5_core_err(dev, "Failed to alloc IRQs\n");
1053                 goto err_irq_table;
1054         }
1055
1056         err = mlx5_eq_table_create(dev);
1057         if (err) {
1058                 mlx5_core_err(dev, "Failed to create EQs\n");
1059                 goto err_eq_table;
1060         }
1061
1062         err = mlx5_fw_tracer_init(dev->tracer);
1063         if (err) {
1064                 mlx5_core_err(dev, "Failed to init FW tracer\n");
1065                 goto err_fw_tracer;
1066         }
1067
1068         mlx5_hv_vhca_init(dev->hv_vhca);
1069
1070         err = mlx5_rsc_dump_init(dev);
1071         if (err) {
1072                 mlx5_core_err(dev, "Failed to init Resource dump\n");
1073                 goto err_rsc_dump;
1074         }
1075
1076         err = mlx5_fpga_device_start(dev);
1077         if (err) {
1078                 mlx5_core_err(dev, "fpga device start failed %d\n", err);
1079                 goto err_fpga_start;
1080         }
1081
1082         err = mlx5_accel_ipsec_init(dev);
1083         if (err) {
1084                 mlx5_core_err(dev, "IPSec device start failed %d\n", err);
1085                 goto err_ipsec_start;
1086         }
1087
1088         err = mlx5_accel_tls_init(dev);
1089         if (err) {
1090                 mlx5_core_err(dev, "TLS device start failed %d\n", err);
1091                 goto err_tls_start;
1092         }
1093
1094         err = mlx5_init_fs(dev);
1095         if (err) {
1096                 mlx5_core_err(dev, "Failed to init flow steering\n");
1097                 goto err_fs;
1098         }
1099
1100         err = mlx5_core_set_hca_defaults(dev);
1101         if (err) {
1102                 mlx5_core_err(dev, "Failed to set hca defaults\n");
1103                 goto err_sriov;
1104         }
1105
1106         err = mlx5_sriov_attach(dev);
1107         if (err) {
1108                 mlx5_core_err(dev, "sriov init failed %d\n", err);
1109                 goto err_sriov;
1110         }
1111
1112         err = mlx5_ec_init(dev);
1113         if (err) {
1114                 mlx5_core_err(dev, "Failed to init embedded CPU\n");
1115                 goto err_ec;
1116         }
1117
1118         return 0;
1119
1120 err_ec:
1121         mlx5_sriov_detach(dev);
1122 err_sriov:
1123         mlx5_cleanup_fs(dev);
1124 err_fs:
1125         mlx5_accel_tls_cleanup(dev);
1126 err_tls_start:
1127         mlx5_accel_ipsec_cleanup(dev);
1128 err_ipsec_start:
1129         mlx5_fpga_device_stop(dev);
1130 err_fpga_start:
1131         mlx5_rsc_dump_cleanup(dev);
1132 err_rsc_dump:
1133         mlx5_hv_vhca_cleanup(dev->hv_vhca);
1134         mlx5_fw_tracer_cleanup(dev->tracer);
1135 err_fw_tracer:
1136         mlx5_eq_table_destroy(dev);
1137 err_eq_table:
1138         mlx5_irq_table_destroy(dev);
1139 err_irq_table:
1140         mlx5_pagealloc_stop(dev);
1141         mlx5_events_stop(dev);
1142         mlx5_put_uars_page(dev, dev->priv.uar);
1143         return err;
1144 }
1145
1146 static void mlx5_unload(struct mlx5_core_dev *dev)
1147 {
1148         mlx5_ec_cleanup(dev);
1149         mlx5_sriov_detach(dev);
1150         mlx5_cleanup_fs(dev);
1151         mlx5_accel_ipsec_cleanup(dev);
1152         mlx5_accel_tls_cleanup(dev);
1153         mlx5_fpga_device_stop(dev);
1154         mlx5_rsc_dump_cleanup(dev);
1155         mlx5_hv_vhca_cleanup(dev->hv_vhca);
1156         mlx5_fw_tracer_cleanup(dev->tracer);
1157         mlx5_eq_table_destroy(dev);
1158         mlx5_irq_table_destroy(dev);
1159         mlx5_pagealloc_stop(dev);
1160         mlx5_events_stop(dev);
1161         mlx5_put_uars_page(dev, dev->priv.uar);
1162 }
1163
1164 int mlx5_load_one(struct mlx5_core_dev *dev, bool boot)
1165 {
1166         int err = 0;
1167
1168         mutex_lock(&dev->intf_state_mutex);
1169         if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1170                 mlx5_core_warn(dev, "interface is up, NOP\n");
1171                 goto out;
1172         }
1173         /* remove any previous indication of internal error */
1174         dev->state = MLX5_DEVICE_STATE_UP;
1175
1176         err = mlx5_function_setup(dev, boot);
1177         if (err)
1178                 goto err_function;
1179
1180         if (boot) {
1181                 err = mlx5_init_once(dev);
1182                 if (err) {
1183                         mlx5_core_err(dev, "sw objs init failed\n");
1184                         goto function_teardown;
1185                 }
1186         }
1187
1188         err = mlx5_load(dev);
1189         if (err)
1190                 goto err_load;
1191
1192         if (boot) {
1193                 err = mlx5_devlink_register(priv_to_devlink(dev), dev->device);
1194                 if (err)
1195                         goto err_devlink_reg;
1196         }
1197
1198         if (mlx5_device_registered(dev))
1199                 mlx5_attach_device(dev);
1200         else
1201                 mlx5_register_device(dev);
1202
1203         set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1204
1205         mutex_unlock(&dev->intf_state_mutex);
1206         return 0;
1207
1208 err_devlink_reg:
1209         mlx5_unload(dev);
1210 err_load:
1211         if (boot)
1212                 mlx5_cleanup_once(dev);
1213 function_teardown:
1214         mlx5_function_teardown(dev, boot);
1215 err_function:
1216         dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
1217 out:
1218         mutex_unlock(&dev->intf_state_mutex);
1219         return err;
1220 }
1221
1222 void mlx5_unload_one(struct mlx5_core_dev *dev, bool cleanup)
1223 {
1224         if (cleanup)
1225                 mlx5_unregister_device(dev);
1226
1227         mutex_lock(&dev->intf_state_mutex);
1228         if (!test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1229                 mlx5_core_warn(dev, "%s: interface is down, NOP\n",
1230                                __func__);
1231                 if (cleanup)
1232                         mlx5_cleanup_once(dev);
1233                 goto out;
1234         }
1235
1236         clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1237
1238         if (mlx5_device_registered(dev))
1239                 mlx5_detach_device(dev);
1240
1241         mlx5_unload(dev);
1242
1243         if (cleanup)
1244                 mlx5_cleanup_once(dev);
1245
1246         mlx5_function_teardown(dev, cleanup);
1247 out:
1248         mutex_unlock(&dev->intf_state_mutex);
1249 }
1250
1251 static int mlx5_mdev_init(struct mlx5_core_dev *dev, int profile_idx)
1252 {
1253         struct mlx5_priv *priv = &dev->priv;
1254         int err;
1255
1256         dev->profile = &profile[profile_idx];
1257
1258         INIT_LIST_HEAD(&priv->ctx_list);
1259         spin_lock_init(&priv->ctx_lock);
1260         mutex_init(&dev->intf_state_mutex);
1261
1262         mutex_init(&priv->bfregs.reg_head.lock);
1263         mutex_init(&priv->bfregs.wc_head.lock);
1264         INIT_LIST_HEAD(&priv->bfregs.reg_head.list);
1265         INIT_LIST_HEAD(&priv->bfregs.wc_head.list);
1266
1267         mutex_init(&priv->alloc_mutex);
1268         mutex_init(&priv->pgdir_mutex);
1269         INIT_LIST_HEAD(&priv->pgdir_list);
1270
1271         priv->dbg_root = debugfs_create_dir(dev_name(dev->device),
1272                                             mlx5_debugfs_root);
1273         if (!priv->dbg_root) {
1274                 dev_err(dev->device, "mlx5_core: error, Cannot create debugfs dir, aborting\n");
1275                 return -ENOMEM;
1276         }
1277
1278         err = mlx5_health_init(dev);
1279         if (err)
1280                 goto err_health_init;
1281
1282         err = mlx5_pagealloc_init(dev);
1283         if (err)
1284                 goto err_pagealloc_init;
1285
1286         return 0;
1287
1288 err_pagealloc_init:
1289         mlx5_health_cleanup(dev);
1290 err_health_init:
1291         debugfs_remove(dev->priv.dbg_root);
1292
1293         return err;
1294 }
1295
1296 static void mlx5_mdev_uninit(struct mlx5_core_dev *dev)
1297 {
1298         mlx5_pagealloc_cleanup(dev);
1299         mlx5_health_cleanup(dev);
1300         debugfs_remove_recursive(dev->priv.dbg_root);
1301 }
1302
1303 #define MLX5_IB_MOD "mlx5_ib"
1304 static int init_one(struct pci_dev *pdev, const struct pci_device_id *id)
1305 {
1306         struct mlx5_core_dev *dev;
1307         struct devlink *devlink;
1308         int err;
1309
1310         devlink = mlx5_devlink_alloc();
1311         if (!devlink) {
1312                 dev_err(&pdev->dev, "devlink alloc failed\n");
1313                 return -ENOMEM;
1314         }
1315
1316         dev = devlink_priv(devlink);
1317         dev->device = &pdev->dev;
1318         dev->pdev = pdev;
1319
1320         dev->coredev_type = id->driver_data & MLX5_PCI_DEV_IS_VF ?
1321                          MLX5_COREDEV_VF : MLX5_COREDEV_PF;
1322
1323         err = mlx5_mdev_init(dev, prof_sel);
1324         if (err)
1325                 goto mdev_init_err;
1326
1327         err = mlx5_pci_init(dev, pdev, id);
1328         if (err) {
1329                 mlx5_core_err(dev, "mlx5_pci_init failed with error code %d\n",
1330                               err);
1331                 goto pci_init_err;
1332         }
1333
1334         err = mlx5_load_one(dev, true);
1335         if (err) {
1336                 mlx5_core_err(dev, "mlx5_load_one failed with error code %d\n",
1337                               err);
1338                 goto err_load_one;
1339         }
1340
1341         request_module_nowait(MLX5_IB_MOD);
1342
1343         err = mlx5_crdump_enable(dev);
1344         if (err)
1345                 dev_err(&pdev->dev, "mlx5_crdump_enable failed with error code %d\n", err);
1346
1347         pci_save_state(pdev);
1348         return 0;
1349
1350 err_load_one:
1351         mlx5_pci_close(dev);
1352 pci_init_err:
1353         mlx5_mdev_uninit(dev);
1354 mdev_init_err:
1355         mlx5_devlink_free(devlink);
1356
1357         return err;
1358 }
1359
1360 static void remove_one(struct pci_dev *pdev)
1361 {
1362         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1363         struct devlink *devlink = priv_to_devlink(dev);
1364
1365         mlx5_crdump_disable(dev);
1366         mlx5_devlink_unregister(devlink);
1367
1368         mlx5_drain_health_wq(dev);
1369         mlx5_unload_one(dev, true);
1370         mlx5_pci_close(dev);
1371         mlx5_mdev_uninit(dev);
1372         mlx5_devlink_free(devlink);
1373 }
1374
1375 static pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev,
1376                                               pci_channel_state_t state)
1377 {
1378         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1379
1380         mlx5_core_info(dev, "%s was called\n", __func__);
1381
1382         mlx5_enter_error_state(dev, false);
1383         mlx5_error_sw_reset(dev);
1384         mlx5_unload_one(dev, false);
1385         mlx5_drain_health_wq(dev);
1386         mlx5_pci_disable_device(dev);
1387
1388         return state == pci_channel_io_perm_failure ?
1389                 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1390 }
1391
1392 /* wait for the device to show vital signs by waiting
1393  * for the health counter to start counting.
1394  */
1395 static int wait_vital(struct pci_dev *pdev)
1396 {
1397         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1398         struct mlx5_core_health *health = &dev->priv.health;
1399         const int niter = 100;
1400         u32 last_count = 0;
1401         u32 count;
1402         int i;
1403
1404         for (i = 0; i < niter; i++) {
1405                 count = ioread32be(health->health_counter);
1406                 if (count && count != 0xffffffff) {
1407                         if (last_count && last_count != count) {
1408                                 mlx5_core_info(dev,
1409                                                "wait vital counter value 0x%x after %d iterations\n",
1410                                                count, i);
1411                                 return 0;
1412                         }
1413                         last_count = count;
1414                 }
1415                 msleep(50);
1416         }
1417
1418         return -ETIMEDOUT;
1419 }
1420
1421 static pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev)
1422 {
1423         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1424         int err;
1425
1426         mlx5_core_info(dev, "%s was called\n", __func__);
1427
1428         err = mlx5_pci_enable_device(dev);
1429         if (err) {
1430                 mlx5_core_err(dev, "%s: mlx5_pci_enable_device failed with error code: %d\n",
1431                               __func__, err);
1432                 return PCI_ERS_RESULT_DISCONNECT;
1433         }
1434
1435         pci_set_master(pdev);
1436         pci_restore_state(pdev);
1437         pci_save_state(pdev);
1438
1439         if (wait_vital(pdev)) {
1440                 mlx5_core_err(dev, "%s: wait_vital timed out\n", __func__);
1441                 return PCI_ERS_RESULT_DISCONNECT;
1442         }
1443
1444         return PCI_ERS_RESULT_RECOVERED;
1445 }
1446
1447 static void mlx5_pci_resume(struct pci_dev *pdev)
1448 {
1449         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1450         int err;
1451
1452         mlx5_core_info(dev, "%s was called\n", __func__);
1453
1454         err = mlx5_load_one(dev, false);
1455         if (err)
1456                 mlx5_core_err(dev, "%s: mlx5_load_one failed with error code: %d\n",
1457                               __func__, err);
1458         else
1459                 mlx5_core_info(dev, "%s: device recovered\n", __func__);
1460 }
1461
1462 static const struct pci_error_handlers mlx5_err_handler = {
1463         .error_detected = mlx5_pci_err_detected,
1464         .slot_reset     = mlx5_pci_slot_reset,
1465         .resume         = mlx5_pci_resume
1466 };
1467
1468 static int mlx5_try_fast_unload(struct mlx5_core_dev *dev)
1469 {
1470         bool fast_teardown = false, force_teardown = false;
1471         int ret = 1;
1472
1473         fast_teardown = MLX5_CAP_GEN(dev, fast_teardown);
1474         force_teardown = MLX5_CAP_GEN(dev, force_teardown);
1475
1476         mlx5_core_dbg(dev, "force teardown firmware support=%d\n", force_teardown);
1477         mlx5_core_dbg(dev, "fast teardown firmware support=%d\n", fast_teardown);
1478
1479         if (!fast_teardown && !force_teardown)
1480                 return -EOPNOTSUPP;
1481
1482         if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
1483                 mlx5_core_dbg(dev, "Device in internal error state, giving up\n");
1484                 return -EAGAIN;
1485         }
1486
1487         /* Panic tear down fw command will stop the PCI bus communication
1488          * with the HCA, so the health polll is no longer needed.
1489          */
1490         mlx5_drain_health_wq(dev);
1491         mlx5_stop_health_poll(dev, false);
1492
1493         ret = mlx5_cmd_fast_teardown_hca(dev);
1494         if (!ret)
1495                 goto succeed;
1496
1497         ret = mlx5_cmd_force_teardown_hca(dev);
1498         if (!ret)
1499                 goto succeed;
1500
1501         mlx5_core_dbg(dev, "Firmware couldn't do fast unload error: %d\n", ret);
1502         mlx5_start_health_poll(dev);
1503         return ret;
1504
1505 succeed:
1506         mlx5_enter_error_state(dev, true);
1507
1508         /* Some platforms requiring freeing the IRQ's in the shutdown
1509          * flow. If they aren't freed they can't be allocated after
1510          * kexec. There is no need to cleanup the mlx5_core software
1511          * contexts.
1512          */
1513         mlx5_core_eq_free_irqs(dev);
1514
1515         return 0;
1516 }
1517
1518 static void shutdown(struct pci_dev *pdev)
1519 {
1520         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1521         int err;
1522
1523         mlx5_core_info(dev, "Shutdown was called\n");
1524         err = mlx5_try_fast_unload(dev);
1525         if (err)
1526                 mlx5_unload_one(dev, false);
1527         mlx5_pci_disable_device(dev);
1528 }
1529
1530 static const struct pci_device_id mlx5_core_pci_table[] = {
1531         { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTIB) },
1532         { PCI_VDEVICE(MELLANOX, 0x1012), MLX5_PCI_DEV_IS_VF},   /* Connect-IB VF */
1533         { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4) },
1534         { PCI_VDEVICE(MELLANOX, 0x1014), MLX5_PCI_DEV_IS_VF},   /* ConnectX-4 VF */
1535         { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4_LX) },
1536         { PCI_VDEVICE(MELLANOX, 0x1016), MLX5_PCI_DEV_IS_VF},   /* ConnectX-4LX VF */
1537         { PCI_VDEVICE(MELLANOX, 0x1017) },                      /* ConnectX-5, PCIe 3.0 */
1538         { PCI_VDEVICE(MELLANOX, 0x1018), MLX5_PCI_DEV_IS_VF},   /* ConnectX-5 VF */
1539         { PCI_VDEVICE(MELLANOX, 0x1019) },                      /* ConnectX-5 Ex */
1540         { PCI_VDEVICE(MELLANOX, 0x101a), MLX5_PCI_DEV_IS_VF},   /* ConnectX-5 Ex VF */
1541         { PCI_VDEVICE(MELLANOX, 0x101b) },                      /* ConnectX-6 */
1542         { PCI_VDEVICE(MELLANOX, 0x101c), MLX5_PCI_DEV_IS_VF},   /* ConnectX-6 VF */
1543         { PCI_VDEVICE(MELLANOX, 0x101d) },                      /* ConnectX-6 Dx */
1544         { PCI_VDEVICE(MELLANOX, 0x101e), MLX5_PCI_DEV_IS_VF},   /* ConnectX Family mlx5Gen Virtual Function */
1545         { PCI_VDEVICE(MELLANOX, 0x101f) },                      /* ConnectX-6 LX */
1546         { PCI_VDEVICE(MELLANOX, 0x1021) },                      /* ConnectX-7 */
1547         { PCI_VDEVICE(MELLANOX, 0xa2d2) },                      /* BlueField integrated ConnectX-5 network controller */
1548         { PCI_VDEVICE(MELLANOX, 0xa2d3), MLX5_PCI_DEV_IS_VF},   /* BlueField integrated ConnectX-5 network controller VF */
1549         { PCI_VDEVICE(MELLANOX, 0xa2d6) },                      /* BlueField-2 integrated ConnectX-6 Dx network controller */
1550         { 0, }
1551 };
1552
1553 MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
1554
1555 void mlx5_disable_device(struct mlx5_core_dev *dev)
1556 {
1557         mlx5_error_sw_reset(dev);
1558         mlx5_unload_one(dev, false);
1559 }
1560
1561 void mlx5_recover_device(struct mlx5_core_dev *dev)
1562 {
1563         mlx5_pci_disable_device(dev);
1564         if (mlx5_pci_slot_reset(dev->pdev) == PCI_ERS_RESULT_RECOVERED)
1565                 mlx5_pci_resume(dev->pdev);
1566 }
1567
1568 static struct pci_driver mlx5_core_driver = {
1569         .name           = DRIVER_NAME,
1570         .id_table       = mlx5_core_pci_table,
1571         .probe          = init_one,
1572         .remove         = remove_one,
1573         .shutdown       = shutdown,
1574         .err_handler    = &mlx5_err_handler,
1575         .sriov_configure   = mlx5_core_sriov_configure,
1576 };
1577
1578 static void mlx5_core_verify_params(void)
1579 {
1580         if (prof_sel >= ARRAY_SIZE(profile)) {
1581                 pr_warn("mlx5_core: WARNING: Invalid module parameter prof_sel %d, valid range 0-%zu, changing back to default(%d)\n",
1582                         prof_sel,
1583                         ARRAY_SIZE(profile) - 1,
1584                         MLX5_DEFAULT_PROF);
1585                 prof_sel = MLX5_DEFAULT_PROF;
1586         }
1587 }
1588
1589 static int __init init(void)
1590 {
1591         int err;
1592
1593         get_random_bytes(&sw_owner_id, sizeof(sw_owner_id));
1594
1595         mlx5_core_verify_params();
1596         mlx5_accel_ipsec_build_fs_cmds();
1597         mlx5_register_debugfs();
1598
1599         err = pci_register_driver(&mlx5_core_driver);
1600         if (err)
1601                 goto err_debug;
1602
1603 #ifdef CONFIG_MLX5_CORE_EN
1604         mlx5e_init();
1605 #endif
1606
1607         return 0;
1608
1609 err_debug:
1610         mlx5_unregister_debugfs();
1611         return err;
1612 }
1613
1614 static void __exit cleanup(void)
1615 {
1616 #ifdef CONFIG_MLX5_CORE_EN
1617         mlx5e_cleanup();
1618 #endif
1619         pci_unregister_driver(&mlx5_core_driver);
1620         mlx5_unregister_debugfs();
1621 }
1622
1623 module_init(init);
1624 module_exit(cleanup);