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