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