2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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 #ifdef CONFIG_RFS_ACCEL
51 #include <linux/cpu_rmap.h>
53 #include <net/devlink.h>
54 #include "mlx5_core.h"
56 #ifdef CONFIG_MLX5_CORE_EN
60 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
61 MODULE_DESCRIPTION("Mellanox Connect-IB, ConnectX-4 core driver");
62 MODULE_LICENSE("Dual BSD/GPL");
63 MODULE_VERSION(DRIVER_VERSION);
65 unsigned int mlx5_core_debug_mask;
66 module_param_named(debug_mask, mlx5_core_debug_mask, uint, 0644);
67 MODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
69 #define MLX5_DEFAULT_PROF 2
70 static unsigned int prof_sel = MLX5_DEFAULT_PROF;
71 module_param_named(prof_sel, prof_sel, uint, 0444);
72 MODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");
75 MLX5_ATOMIC_REQ_MODE_BE = 0x0,
76 MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS = 0x1,
79 static struct mlx5_profile profile[] = {
84 .mask = MLX5_PROF_MASK_QP_SIZE,
88 .mask = MLX5_PROF_MASK_QP_SIZE |
89 MLX5_PROF_MASK_MR_CACHE,
158 #define FW_INIT_TIMEOUT_MILI 2000
159 #define FW_INIT_WAIT_MS 2
161 static int wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili)
163 unsigned long end = jiffies + msecs_to_jiffies(max_wait_mili);
166 while (fw_initializing(dev)) {
167 if (time_after(jiffies, end)) {
171 msleep(FW_INIT_WAIT_MS);
177 static int set_dma_caps(struct pci_dev *pdev)
181 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
183 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
184 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
186 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
191 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
194 "Warning: couldn't set 64-bit consistent PCI DMA mask\n");
195 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
198 "Can't set consistent PCI DMA mask, aborting\n");
203 dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
207 static int mlx5_pci_enable_device(struct mlx5_core_dev *dev)
209 struct pci_dev *pdev = dev->pdev;
212 mutex_lock(&dev->pci_status_mutex);
213 if (dev->pci_status == MLX5_PCI_STATUS_DISABLED) {
214 err = pci_enable_device(pdev);
216 dev->pci_status = MLX5_PCI_STATUS_ENABLED;
218 mutex_unlock(&dev->pci_status_mutex);
223 static void mlx5_pci_disable_device(struct mlx5_core_dev *dev)
225 struct pci_dev *pdev = dev->pdev;
227 mutex_lock(&dev->pci_status_mutex);
228 if (dev->pci_status == MLX5_PCI_STATUS_ENABLED) {
229 pci_disable_device(pdev);
230 dev->pci_status = MLX5_PCI_STATUS_DISABLED;
232 mutex_unlock(&dev->pci_status_mutex);
235 static int request_bar(struct pci_dev *pdev)
239 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
240 dev_err(&pdev->dev, "Missing registers BAR, aborting\n");
244 err = pci_request_regions(pdev, DRIVER_NAME);
246 dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
251 static void release_bar(struct pci_dev *pdev)
253 pci_release_regions(pdev);
256 static int mlx5_enable_msix(struct mlx5_core_dev *dev)
258 struct mlx5_priv *priv = &dev->priv;
259 struct mlx5_eq_table *table = &priv->eq_table;
260 int num_eqs = 1 << MLX5_CAP_GEN(dev, log_max_eq);
264 nvec = MLX5_CAP_GEN(dev, num_ports) * num_online_cpus() +
265 MLX5_EQ_VEC_COMP_BASE;
266 nvec = min_t(int, nvec, num_eqs);
267 if (nvec <= MLX5_EQ_VEC_COMP_BASE)
270 priv->msix_arr = kcalloc(nvec, sizeof(*priv->msix_arr), GFP_KERNEL);
272 priv->irq_info = kcalloc(nvec, sizeof(*priv->irq_info), GFP_KERNEL);
273 if (!priv->msix_arr || !priv->irq_info)
276 for (i = 0; i < nvec; i++)
277 priv->msix_arr[i].entry = i;
279 nvec = pci_enable_msix_range(dev->pdev, priv->msix_arr,
280 MLX5_EQ_VEC_COMP_BASE + 1, nvec);
284 table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
289 kfree(priv->irq_info);
290 kfree(priv->msix_arr);
294 static void mlx5_disable_msix(struct mlx5_core_dev *dev)
296 struct mlx5_priv *priv = &dev->priv;
298 pci_disable_msix(dev->pdev);
299 kfree(priv->irq_info);
300 kfree(priv->msix_arr);
303 struct mlx5_reg_host_endianess {
309 #define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos))
312 MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) |
313 MLX5_DEV_CAP_FLAG_DCT,
316 static u16 to_fw_pkey_sz(struct mlx5_core_dev *dev, u32 size)
332 mlx5_core_warn(dev, "invalid pkey table size %d\n", size);
337 static int mlx5_core_get_caps_mode(struct mlx5_core_dev *dev,
338 enum mlx5_cap_type cap_type,
339 enum mlx5_cap_mode cap_mode)
341 u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
342 int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
343 void *out, *hca_caps;
344 u16 opmod = (cap_type << 1) | (cap_mode & 0x01);
347 memset(in, 0, sizeof(in));
348 out = kzalloc(out_sz, GFP_KERNEL);
352 MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
353 MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
354 err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
357 "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n",
358 cap_type, cap_mode, err);
362 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, out, capability);
365 case HCA_CAP_OPMOD_GET_MAX:
366 memcpy(dev->hca_caps_max[cap_type], hca_caps,
367 MLX5_UN_SZ_BYTES(hca_cap_union));
369 case HCA_CAP_OPMOD_GET_CUR:
370 memcpy(dev->hca_caps_cur[cap_type], hca_caps,
371 MLX5_UN_SZ_BYTES(hca_cap_union));
375 "Tried to query dev cap type(%x) with wrong opmode(%x)\n",
385 int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type)
389 ret = mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_CUR);
392 return mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_MAX);
395 static int set_caps(struct mlx5_core_dev *dev, void *in, int in_sz, int opmod)
397 u32 out[MLX5_ST_SZ_DW(set_hca_cap_out)] = {0};
399 MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
400 MLX5_SET(set_hca_cap_in, in, op_mod, opmod << 1);
401 return mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
404 static int handle_hca_cap_atomic(struct mlx5_core_dev *dev)
408 int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
412 if (MLX5_CAP_GEN(dev, atomic)) {
413 err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC);
422 supported_atomic_req_8B_endianess_mode_1);
424 if (req_endianness != MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS)
427 set_ctx = kzalloc(set_sz, GFP_KERNEL);
431 set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
433 /* Set requestor to host endianness */
434 MLX5_SET(atomic_caps, set_hca_cap, atomic_req_8B_endianess_mode,
435 MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS);
437 err = set_caps(dev, set_ctx, set_sz, MLX5_SET_HCA_CAP_OP_MOD_ATOMIC);
443 static int handle_hca_cap(struct mlx5_core_dev *dev)
445 void *set_ctx = NULL;
446 struct mlx5_profile *prof = dev->profile;
448 int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
451 set_ctx = kzalloc(set_sz, GFP_KERNEL);
455 err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL);
459 set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
461 memcpy(set_hca_cap, dev->hca_caps_cur[MLX5_CAP_GENERAL],
462 MLX5_ST_SZ_BYTES(cmd_hca_cap));
464 mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
465 mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
467 /* we limit the size of the pkey table to 128 entries for now */
468 MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
469 to_fw_pkey_sz(dev, 128));
471 if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
472 MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
475 /* disable cmdif checksum */
476 MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
478 MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12);
480 err = set_caps(dev, set_ctx, set_sz,
481 MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE);
488 static int set_hca_ctrl(struct mlx5_core_dev *dev)
490 struct mlx5_reg_host_endianess he_in;
491 struct mlx5_reg_host_endianess he_out;
494 if (!mlx5_core_is_pf(dev))
497 memset(&he_in, 0, sizeof(he_in));
498 he_in.he = MLX5_SET_HOST_ENDIANNESS;
499 err = mlx5_core_access_reg(dev, &he_in, sizeof(he_in),
500 &he_out, sizeof(he_out),
501 MLX5_REG_HOST_ENDIANNESS, 0, 1);
505 int mlx5_core_enable_hca(struct mlx5_core_dev *dev, u16 func_id)
507 u32 out[MLX5_ST_SZ_DW(enable_hca_out)] = {0};
508 u32 in[MLX5_ST_SZ_DW(enable_hca_in)] = {0};
510 MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA);
511 MLX5_SET(enable_hca_in, in, function_id, func_id);
512 return mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
515 int mlx5_core_disable_hca(struct mlx5_core_dev *dev, u16 func_id)
517 u32 out[MLX5_ST_SZ_DW(disable_hca_out)] = {0};
518 u32 in[MLX5_ST_SZ_DW(disable_hca_in)] = {0};
520 MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA);
521 MLX5_SET(disable_hca_in, in, function_id, func_id);
522 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
525 cycle_t mlx5_read_internal_timer(struct mlx5_core_dev *dev)
527 u32 timer_h, timer_h1, timer_l;
529 timer_h = ioread32be(&dev->iseg->internal_timer_h);
530 timer_l = ioread32be(&dev->iseg->internal_timer_l);
531 timer_h1 = ioread32be(&dev->iseg->internal_timer_h);
532 if (timer_h != timer_h1) /* wrap around */
533 timer_l = ioread32be(&dev->iseg->internal_timer_l);
535 return (cycle_t)timer_l | (cycle_t)timer_h1 << 32;
538 static int mlx5_irq_set_affinity_hint(struct mlx5_core_dev *mdev, int i)
540 struct mlx5_priv *priv = &mdev->priv;
541 struct msix_entry *msix = priv->msix_arr;
542 int irq = msix[i + MLX5_EQ_VEC_COMP_BASE].vector;
543 int numa_node = priv->numa_node;
546 if (!zalloc_cpumask_var(&priv->irq_info[i].mask, GFP_KERNEL)) {
547 mlx5_core_warn(mdev, "zalloc_cpumask_var failed");
551 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
552 priv->irq_info[i].mask);
554 err = irq_set_affinity_hint(irq, priv->irq_info[i].mask);
556 mlx5_core_warn(mdev, "irq_set_affinity_hint failed,irq 0x%.4x",
564 free_cpumask_var(priv->irq_info[i].mask);
568 static void mlx5_irq_clear_affinity_hint(struct mlx5_core_dev *mdev, int i)
570 struct mlx5_priv *priv = &mdev->priv;
571 struct msix_entry *msix = priv->msix_arr;
572 int irq = msix[i + MLX5_EQ_VEC_COMP_BASE].vector;
574 irq_set_affinity_hint(irq, NULL);
575 free_cpumask_var(priv->irq_info[i].mask);
578 static int mlx5_irq_set_affinity_hints(struct mlx5_core_dev *mdev)
583 for (i = 0; i < mdev->priv.eq_table.num_comp_vectors; i++) {
584 err = mlx5_irq_set_affinity_hint(mdev, i);
592 for (i--; i >= 0; i--)
593 mlx5_irq_clear_affinity_hint(mdev, i);
598 static void mlx5_irq_clear_affinity_hints(struct mlx5_core_dev *mdev)
602 for (i = 0; i < mdev->priv.eq_table.num_comp_vectors; i++)
603 mlx5_irq_clear_affinity_hint(mdev, i);
606 int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn,
609 struct mlx5_eq_table *table = &dev->priv.eq_table;
610 struct mlx5_eq *eq, *n;
613 spin_lock(&table->lock);
614 list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
615 if (eq->index == vector) {
622 spin_unlock(&table->lock);
626 EXPORT_SYMBOL(mlx5_vector2eqn);
628 struct mlx5_eq *mlx5_eqn2eq(struct mlx5_core_dev *dev, int eqn)
630 struct mlx5_eq_table *table = &dev->priv.eq_table;
633 spin_lock(&table->lock);
634 list_for_each_entry(eq, &table->comp_eqs_list, list)
635 if (eq->eqn == eqn) {
636 spin_unlock(&table->lock);
640 spin_unlock(&table->lock);
642 return ERR_PTR(-ENOENT);
645 static void free_comp_eqs(struct mlx5_core_dev *dev)
647 struct mlx5_eq_table *table = &dev->priv.eq_table;
648 struct mlx5_eq *eq, *n;
650 #ifdef CONFIG_RFS_ACCEL
652 free_irq_cpu_rmap(dev->rmap);
656 spin_lock(&table->lock);
657 list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
659 spin_unlock(&table->lock);
660 if (mlx5_destroy_unmap_eq(dev, eq))
661 mlx5_core_warn(dev, "failed to destroy EQ 0x%x\n",
664 spin_lock(&table->lock);
666 spin_unlock(&table->lock);
669 static int alloc_comp_eqs(struct mlx5_core_dev *dev)
671 struct mlx5_eq_table *table = &dev->priv.eq_table;
672 char name[MLX5_MAX_IRQ_NAME];
679 INIT_LIST_HEAD(&table->comp_eqs_list);
680 ncomp_vec = table->num_comp_vectors;
681 nent = MLX5_COMP_EQ_SIZE;
682 #ifdef CONFIG_RFS_ACCEL
683 dev->rmap = alloc_irq_cpu_rmap(ncomp_vec);
687 for (i = 0; i < ncomp_vec; i++) {
688 eq = kzalloc(sizeof(*eq), GFP_KERNEL);
694 #ifdef CONFIG_RFS_ACCEL
695 irq_cpu_rmap_add(dev->rmap,
696 dev->priv.msix_arr[i + MLX5_EQ_VEC_COMP_BASE].vector);
698 snprintf(name, MLX5_MAX_IRQ_NAME, "mlx5_comp%d", i);
699 err = mlx5_create_map_eq(dev, eq,
700 i + MLX5_EQ_VEC_COMP_BASE, nent, 0,
701 name, &dev->priv.uuari.uars[0]);
706 mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->eqn);
708 spin_lock(&table->lock);
709 list_add_tail(&eq->list, &table->comp_eqs_list);
710 spin_unlock(&table->lock);
720 static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
722 u32 query_in[MLX5_ST_SZ_DW(query_issi_in)] = {0};
723 u32 query_out[MLX5_ST_SZ_DW(query_issi_out)] = {0};
727 MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
728 err = mlx5_cmd_exec(dev, query_in, sizeof(query_in),
729 query_out, sizeof(query_out));
734 mlx5_cmd_mbox_status(query_out, &status, &syndrome);
735 if (!status || syndrome == MLX5_DRIVER_SYND) {
736 mlx5_core_err(dev, "Failed to query ISSI err(%d) status(%d) synd(%d)\n",
737 err, status, syndrome);
741 mlx5_core_warn(dev, "Query ISSI is not supported by FW, ISSI is 0\n");
746 sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);
748 if (sup_issi & (1 << 1)) {
749 u32 set_in[MLX5_ST_SZ_DW(set_issi_in)] = {0};
750 u32 set_out[MLX5_ST_SZ_DW(set_issi_out)] = {0};
752 MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
753 MLX5_SET(set_issi_in, set_in, current_issi, 1);
754 err = mlx5_cmd_exec(dev, set_in, sizeof(set_in),
755 set_out, sizeof(set_out));
757 mlx5_core_err(dev, "Failed to set ISSI to 1 err(%d)\n",
765 } else if (sup_issi & (1 << 0) || !sup_issi) {
773 static int mlx5_pci_init(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
775 struct pci_dev *pdev = dev->pdev;
778 pci_set_drvdata(dev->pdev, dev);
779 strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
780 priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
782 mutex_init(&priv->pgdir_mutex);
783 INIT_LIST_HEAD(&priv->pgdir_list);
784 spin_lock_init(&priv->mkey_lock);
786 mutex_init(&priv->alloc_mutex);
788 priv->numa_node = dev_to_node(&dev->pdev->dev);
790 priv->dbg_root = debugfs_create_dir(dev_name(&pdev->dev), mlx5_debugfs_root);
794 err = mlx5_pci_enable_device(dev);
796 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
800 err = request_bar(pdev);
802 dev_err(&pdev->dev, "error requesting BARs, aborting\n");
806 pci_set_master(pdev);
808 err = set_dma_caps(pdev);
810 dev_err(&pdev->dev, "Failed setting DMA capabilities mask, aborting\n");
814 dev->iseg_base = pci_resource_start(dev->pdev, 0);
815 dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
818 dev_err(&pdev->dev, "Failed mapping initialization segment, aborting\n");
825 pci_clear_master(dev->pdev);
826 release_bar(dev->pdev);
828 mlx5_pci_disable_device(dev);
831 debugfs_remove(priv->dbg_root);
835 static void mlx5_pci_close(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
838 pci_clear_master(dev->pdev);
839 release_bar(dev->pdev);
840 mlx5_pci_disable_device(dev);
841 debugfs_remove(priv->dbg_root);
844 static int mlx5_init_once(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
846 struct pci_dev *pdev = dev->pdev;
849 err = mlx5_query_board_id(dev);
851 dev_err(&pdev->dev, "query board id failed\n");
855 err = mlx5_eq_init(dev);
857 dev_err(&pdev->dev, "failed to initialize eq\n");
861 MLX5_INIT_DOORBELL_LOCK(&priv->cq_uar_lock);
863 err = mlx5_init_cq_table(dev);
865 dev_err(&pdev->dev, "failed to initialize cq table\n");
869 mlx5_init_qp_table(dev);
871 mlx5_init_srq_table(dev);
873 mlx5_init_mkey_table(dev);
875 err = mlx5_init_rl_table(dev);
877 dev_err(&pdev->dev, "Failed to init rate limiting\n");
878 goto err_tables_cleanup;
881 #ifdef CONFIG_MLX5_CORE_EN
882 err = mlx5_eswitch_init(dev);
884 dev_err(&pdev->dev, "Failed to init eswitch %d\n", err);
889 err = mlx5_sriov_init(dev);
891 dev_err(&pdev->dev, "Failed to init sriov %d\n", err);
892 goto err_eswitch_cleanup;
898 #ifdef CONFIG_MLX5_CORE_EN
899 mlx5_eswitch_cleanup(dev->priv.eswitch);
903 mlx5_cleanup_rl_table(dev);
906 mlx5_cleanup_mkey_table(dev);
907 mlx5_cleanup_srq_table(dev);
908 mlx5_cleanup_qp_table(dev);
909 mlx5_cleanup_cq_table(dev);
912 mlx5_eq_cleanup(dev);
918 static void mlx5_cleanup_once(struct mlx5_core_dev *dev)
920 mlx5_sriov_cleanup(dev);
921 #ifdef CONFIG_MLX5_CORE_EN
922 mlx5_eswitch_cleanup(dev->priv.eswitch);
924 mlx5_cleanup_rl_table(dev);
925 mlx5_cleanup_mkey_table(dev);
926 mlx5_cleanup_srq_table(dev);
927 mlx5_cleanup_qp_table(dev);
928 mlx5_cleanup_cq_table(dev);
929 mlx5_eq_cleanup(dev);
932 static int mlx5_load_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
935 struct pci_dev *pdev = dev->pdev;
938 mutex_lock(&dev->intf_state_mutex);
939 if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
940 dev_warn(&dev->pdev->dev, "%s: interface is up, NOP\n",
945 dev_info(&pdev->dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
946 fw_rev_min(dev), fw_rev_sub(dev));
948 /* on load removing any previous indication of internal error, device is
951 dev->state = MLX5_DEVICE_STATE_UP;
953 err = mlx5_cmd_init(dev);
955 dev_err(&pdev->dev, "Failed initializing command interface, aborting\n");
959 err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI);
961 dev_err(&dev->pdev->dev, "Firmware over %d MS in initializing state, aborting\n",
962 FW_INIT_TIMEOUT_MILI);
966 err = mlx5_core_enable_hca(dev, 0);
968 dev_err(&pdev->dev, "enable hca failed\n");
969 goto err_cmd_cleanup;
972 err = mlx5_core_set_issi(dev);
974 dev_err(&pdev->dev, "failed to set issi\n");
975 goto err_disable_hca;
978 err = mlx5_satisfy_startup_pages(dev, 1);
980 dev_err(&pdev->dev, "failed to allocate boot pages\n");
981 goto err_disable_hca;
984 err = set_hca_ctrl(dev);
986 dev_err(&pdev->dev, "set_hca_ctrl failed\n");
987 goto reclaim_boot_pages;
990 err = handle_hca_cap(dev);
992 dev_err(&pdev->dev, "handle_hca_cap failed\n");
993 goto reclaim_boot_pages;
996 err = handle_hca_cap_atomic(dev);
998 dev_err(&pdev->dev, "handle_hca_cap_atomic failed\n");
999 goto reclaim_boot_pages;
1002 err = mlx5_satisfy_startup_pages(dev, 0);
1004 dev_err(&pdev->dev, "failed to allocate init pages\n");
1005 goto reclaim_boot_pages;
1008 err = mlx5_pagealloc_start(dev);
1010 dev_err(&pdev->dev, "mlx5_pagealloc_start failed\n");
1011 goto reclaim_boot_pages;
1014 err = mlx5_cmd_init_hca(dev);
1016 dev_err(&pdev->dev, "init hca failed\n");
1017 goto err_pagealloc_stop;
1020 mlx5_start_health_poll(dev);
1022 err = mlx5_query_hca_caps(dev);
1024 dev_err(&pdev->dev, "query hca failed\n");
1028 if (boot && mlx5_init_once(dev, priv)) {
1029 dev_err(&pdev->dev, "sw objs init failed\n");
1033 err = mlx5_enable_msix(dev);
1035 dev_err(&pdev->dev, "enable msix failed\n");
1036 goto err_cleanup_once;
1039 err = mlx5_alloc_uuars(dev, &priv->uuari);
1041 dev_err(&pdev->dev, "Failed allocating uar, aborting\n");
1042 goto err_disable_msix;
1045 err = mlx5_start_eqs(dev);
1047 dev_err(&pdev->dev, "Failed to start pages and async EQs\n");
1051 err = alloc_comp_eqs(dev);
1053 dev_err(&pdev->dev, "Failed to alloc completion EQs\n");
1057 err = mlx5_irq_set_affinity_hints(dev);
1059 dev_err(&pdev->dev, "Failed to alloc affinity hint cpumask\n");
1060 goto err_affinity_hints;
1063 err = mlx5_init_fs(dev);
1065 dev_err(&pdev->dev, "Failed to init flow steering\n");
1069 #ifdef CONFIG_MLX5_CORE_EN
1070 mlx5_eswitch_attach(dev->priv.eswitch);
1073 err = mlx5_sriov_attach(dev);
1075 dev_err(&pdev->dev, "sriov init failed %d\n", err);
1079 if (mlx5_device_registered(dev)) {
1080 mlx5_attach_device(dev);
1082 err = mlx5_register_device(dev);
1084 dev_err(&pdev->dev, "mlx5_register_device failed %d\n", err);
1089 clear_bit(MLX5_INTERFACE_STATE_DOWN, &dev->intf_state);
1090 set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1092 mutex_unlock(&dev->intf_state_mutex);
1097 mlx5_sriov_detach(dev);
1100 #ifdef CONFIG_MLX5_CORE_EN
1101 mlx5_eswitch_detach(dev->priv.eswitch);
1103 mlx5_cleanup_fs(dev);
1106 mlx5_irq_clear_affinity_hints(dev);
1115 mlx5_free_uuars(dev, &priv->uuari);
1118 mlx5_disable_msix(dev);
1122 mlx5_cleanup_once(dev);
1125 mlx5_stop_health_poll(dev);
1126 if (mlx5_cmd_teardown_hca(dev)) {
1127 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1132 mlx5_pagealloc_stop(dev);
1135 mlx5_reclaim_startup_pages(dev);
1138 mlx5_core_disable_hca(dev, 0);
1141 mlx5_cmd_cleanup(dev);
1144 dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
1145 mutex_unlock(&dev->intf_state_mutex);
1150 static int mlx5_unload_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
1155 mutex_lock(&dev->intf_state_mutex);
1156 if (test_bit(MLX5_INTERFACE_STATE_DOWN, &dev->intf_state)) {
1157 dev_warn(&dev->pdev->dev, "%s: interface is down, NOP\n",
1160 mlx5_cleanup_once(dev);
1164 if (mlx5_device_registered(dev))
1165 mlx5_detach_device(dev);
1167 mlx5_sriov_detach(dev);
1168 #ifdef CONFIG_MLX5_CORE_EN
1169 mlx5_eswitch_detach(dev->priv.eswitch);
1171 mlx5_cleanup_fs(dev);
1172 mlx5_irq_clear_affinity_hints(dev);
1175 mlx5_free_uuars(dev, &priv->uuari);
1176 mlx5_disable_msix(dev);
1178 mlx5_cleanup_once(dev);
1179 mlx5_stop_health_poll(dev);
1180 err = mlx5_cmd_teardown_hca(dev);
1182 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1185 mlx5_pagealloc_stop(dev);
1186 mlx5_reclaim_startup_pages(dev);
1187 mlx5_core_disable_hca(dev, 0);
1188 mlx5_cmd_cleanup(dev);
1191 clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1192 set_bit(MLX5_INTERFACE_STATE_DOWN, &dev->intf_state);
1193 mutex_unlock(&dev->intf_state_mutex);
1197 struct mlx5_core_event_handler {
1198 void (*event)(struct mlx5_core_dev *dev,
1199 enum mlx5_dev_event event,
1203 static const struct devlink_ops mlx5_devlink_ops = {
1204 #ifdef CONFIG_MLX5_CORE_EN
1205 .eswitch_mode_set = mlx5_devlink_eswitch_mode_set,
1206 .eswitch_mode_get = mlx5_devlink_eswitch_mode_get,
1210 #define MLX5_IB_MOD "mlx5_ib"
1211 static int init_one(struct pci_dev *pdev,
1212 const struct pci_device_id *id)
1214 struct mlx5_core_dev *dev;
1215 struct devlink *devlink;
1216 struct mlx5_priv *priv;
1219 devlink = devlink_alloc(&mlx5_devlink_ops, sizeof(*dev));
1221 dev_err(&pdev->dev, "kzalloc failed\n");
1225 dev = devlink_priv(devlink);
1227 priv->pci_dev_data = id->driver_data;
1229 pci_set_drvdata(pdev, dev);
1232 dev->event = mlx5_core_event;
1233 dev->profile = &profile[prof_sel];
1235 INIT_LIST_HEAD(&priv->ctx_list);
1236 spin_lock_init(&priv->ctx_lock);
1237 mutex_init(&dev->pci_status_mutex);
1238 mutex_init(&dev->intf_state_mutex);
1239 err = mlx5_pci_init(dev, priv);
1241 dev_err(&pdev->dev, "mlx5_pci_init failed with error code %d\n", err);
1245 err = mlx5_health_init(dev);
1247 dev_err(&pdev->dev, "mlx5_health_init failed with error code %d\n", err);
1251 mlx5_pagealloc_init(dev);
1253 err = mlx5_load_one(dev, priv, true);
1255 dev_err(&pdev->dev, "mlx5_load_one failed with error code %d\n", err);
1259 err = request_module_nowait(MLX5_IB_MOD);
1261 pr_info("failed request module on %s\n", MLX5_IB_MOD);
1263 err = devlink_register(devlink, &pdev->dev);
1270 mlx5_unload_one(dev, priv, true);
1272 mlx5_pagealloc_cleanup(dev);
1273 mlx5_health_cleanup(dev);
1275 mlx5_pci_close(dev, priv);
1277 pci_set_drvdata(pdev, NULL);
1278 devlink_free(devlink);
1283 static void remove_one(struct pci_dev *pdev)
1285 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1286 struct devlink *devlink = priv_to_devlink(dev);
1287 struct mlx5_priv *priv = &dev->priv;
1289 devlink_unregister(devlink);
1290 mlx5_unregister_device(dev);
1292 if (mlx5_unload_one(dev, priv, true)) {
1293 dev_err(&dev->pdev->dev, "mlx5_unload_one failed\n");
1294 mlx5_health_cleanup(dev);
1298 mlx5_pagealloc_cleanup(dev);
1299 mlx5_health_cleanup(dev);
1300 mlx5_pci_close(dev, priv);
1301 pci_set_drvdata(pdev, NULL);
1302 devlink_free(devlink);
1305 static pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev,
1306 pci_channel_state_t state)
1308 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1309 struct mlx5_priv *priv = &dev->priv;
1311 dev_info(&pdev->dev, "%s was called\n", __func__);
1313 mlx5_enter_error_state(dev);
1314 mlx5_unload_one(dev, priv, false);
1315 /* In case of kernel call save the pci state and drain health wq */
1317 pci_save_state(pdev);
1318 mlx5_drain_health_wq(dev);
1319 mlx5_pci_disable_device(dev);
1322 return state == pci_channel_io_perm_failure ?
1323 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1326 /* wait for the device to show vital signs by waiting
1327 * for the health counter to start counting.
1329 static int wait_vital(struct pci_dev *pdev)
1331 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1332 struct mlx5_core_health *health = &dev->priv.health;
1333 const int niter = 100;
1338 for (i = 0; i < niter; i++) {
1339 count = ioread32be(health->health_counter);
1340 if (count && count != 0xffffffff) {
1341 if (last_count && last_count != count) {
1342 dev_info(&pdev->dev, "Counter value 0x%x after %d iterations\n", count, i);
1353 static pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev)
1355 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1358 dev_info(&pdev->dev, "%s was called\n", __func__);
1360 err = mlx5_pci_enable_device(dev);
1362 dev_err(&pdev->dev, "%s: mlx5_pci_enable_device failed with error code: %d\n"
1364 return PCI_ERS_RESULT_DISCONNECT;
1367 pci_set_master(pdev);
1368 pci_restore_state(pdev);
1370 if (wait_vital(pdev)) {
1371 dev_err(&pdev->dev, "%s: wait_vital timed out\n", __func__);
1372 return PCI_ERS_RESULT_DISCONNECT;
1375 return PCI_ERS_RESULT_RECOVERED;
1378 static void mlx5_pci_resume(struct pci_dev *pdev)
1380 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1381 struct mlx5_priv *priv = &dev->priv;
1384 dev_info(&pdev->dev, "%s was called\n", __func__);
1386 err = mlx5_load_one(dev, priv, false);
1388 dev_err(&pdev->dev, "%s: mlx5_load_one failed with error code: %d\n"
1391 dev_info(&pdev->dev, "%s: device recovered\n", __func__);
1394 static const struct pci_error_handlers mlx5_err_handler = {
1395 .error_detected = mlx5_pci_err_detected,
1396 .slot_reset = mlx5_pci_slot_reset,
1397 .resume = mlx5_pci_resume
1400 static void shutdown(struct pci_dev *pdev)
1402 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1403 struct mlx5_priv *priv = &dev->priv;
1405 dev_info(&pdev->dev, "Shutdown was called\n");
1406 /* Notify mlx5 clients that the kernel is being shut down */
1407 set_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &dev->intf_state);
1408 mlx5_unload_one(dev, priv, false);
1409 mlx5_pci_disable_device(dev);
1412 static const struct pci_device_id mlx5_core_pci_table[] = {
1413 { PCI_VDEVICE(MELLANOX, 0x1011) }, /* Connect-IB */
1414 { PCI_VDEVICE(MELLANOX, 0x1012), MLX5_PCI_DEV_IS_VF}, /* Connect-IB VF */
1415 { PCI_VDEVICE(MELLANOX, 0x1013) }, /* ConnectX-4 */
1416 { PCI_VDEVICE(MELLANOX, 0x1014), MLX5_PCI_DEV_IS_VF}, /* ConnectX-4 VF */
1417 { PCI_VDEVICE(MELLANOX, 0x1015) }, /* ConnectX-4LX */
1418 { PCI_VDEVICE(MELLANOX, 0x1016), MLX5_PCI_DEV_IS_VF}, /* ConnectX-4LX VF */
1419 { PCI_VDEVICE(MELLANOX, 0x1017) }, /* ConnectX-5, PCIe 3.0 */
1420 { PCI_VDEVICE(MELLANOX, 0x1018), MLX5_PCI_DEV_IS_VF}, /* ConnectX-5 VF */
1421 { PCI_VDEVICE(MELLANOX, 0x1019) }, /* ConnectX-5, PCIe 4.0 */
1425 MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
1427 void mlx5_disable_device(struct mlx5_core_dev *dev)
1429 mlx5_pci_err_detected(dev->pdev, 0);
1432 void mlx5_recover_device(struct mlx5_core_dev *dev)
1434 mlx5_pci_disable_device(dev);
1435 if (mlx5_pci_slot_reset(dev->pdev) == PCI_ERS_RESULT_RECOVERED)
1436 mlx5_pci_resume(dev->pdev);
1439 static struct pci_driver mlx5_core_driver = {
1440 .name = DRIVER_NAME,
1441 .id_table = mlx5_core_pci_table,
1443 .remove = remove_one,
1444 .shutdown = shutdown,
1445 .err_handler = &mlx5_err_handler,
1446 .sriov_configure = mlx5_core_sriov_configure,
1449 static void mlx5_core_verify_params(void)
1451 if (prof_sel >= ARRAY_SIZE(profile)) {
1452 pr_warn("mlx5_core: WARNING: Invalid module parameter prof_sel %d, valid range 0-%zu, changing back to default(%d)\n",
1454 ARRAY_SIZE(profile) - 1,
1456 prof_sel = MLX5_DEFAULT_PROF;
1460 static int __init init(void)
1464 mlx5_core_verify_params();
1465 mlx5_register_debugfs();
1467 err = pci_register_driver(&mlx5_core_driver);
1471 #ifdef CONFIG_MLX5_CORE_EN
1478 mlx5_unregister_debugfs();
1482 static void __exit cleanup(void)
1484 #ifdef CONFIG_MLX5_CORE_EN
1487 pci_unregister_driver(&mlx5_core_driver);
1488 mlx5_unregister_debugfs();
1492 module_exit(cleanup);