2 * Copyright (c) 2013-2019, Mellanox Technologies, Ltd. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <linux/kmod.h>
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/pci.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/slab.h>
34 #include <linux/io-mapping.h>
35 #include <linux/interrupt.h>
36 #include <linux/hardirq.h>
37 #include <dev/mlx5/driver.h>
38 #include <dev/mlx5/cq.h>
39 #include <dev/mlx5/qp.h>
40 #include <dev/mlx5/srq.h>
41 #include <dev/mlx5/mpfs.h>
42 #include <dev/mlx5/vport.h>
43 #include <linux/delay.h>
44 #include <dev/mlx5/mlx5_ifc.h>
45 #include <dev/mlx5/mlx5_fpga/core.h>
46 #include <dev/mlx5/mlx5_lib/mlx5.h>
47 #include "mlx5_core.h"
52 #include <dev/pci/pci_iov.h>
53 #include <sys/iov_schema.h>
56 static const char mlx5_version[] = "Mellanox Core driver "
57 DRIVER_VERSION " (" DRIVER_RELDATE ")";
58 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
59 MODULE_DESCRIPTION("Mellanox Connect-IB, ConnectX-4 core driver");
60 MODULE_LICENSE("Dual BSD/GPL");
61 MODULE_DEPEND(mlx5, linuxkpi, 1, 1, 1);
62 MODULE_DEPEND(mlx5, mlxfw, 1, 1, 1);
63 MODULE_DEPEND(mlx5, firmware, 1, 1, 1);
64 MODULE_VERSION(mlx5, 1);
66 SYSCTL_NODE(_hw, OID_AUTO, mlx5, CTLFLAG_RW, 0, "mlx5 hardware controls");
68 int mlx5_core_debug_mask;
69 SYSCTL_INT(_hw_mlx5, OID_AUTO, debug_mask, CTLFLAG_RWTUN,
70 &mlx5_core_debug_mask, 0,
71 "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
73 #define MLX5_DEFAULT_PROF 2
74 static int mlx5_prof_sel = MLX5_DEFAULT_PROF;
75 SYSCTL_INT(_hw_mlx5, OID_AUTO, prof_sel, CTLFLAG_RWTUN,
77 "profile selector. Valid range 0 - 2");
79 static int mlx5_fast_unload_enabled = 1;
80 SYSCTL_INT(_hw_mlx5, OID_AUTO, fast_unload_enabled, CTLFLAG_RWTUN,
81 &mlx5_fast_unload_enabled, 0,
82 "Set to enable fast unload. Clear to disable.");
84 #define NUMA_NO_NODE -1
86 static LIST_HEAD(intf_list);
87 static LIST_HEAD(dev_list);
88 static DEFINE_MUTEX(intf_mutex);
90 struct mlx5_device_context {
91 struct list_head list;
92 struct mlx5_interface *intf;
97 MLX5_ATOMIC_REQ_MODE_BE = 0x0,
98 MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS = 0x1,
101 static struct mlx5_profile profiles[] = {
106 .mask = MLX5_PROF_MASK_QP_SIZE,
110 .mask = MLX5_PROF_MASK_QP_SIZE |
111 MLX5_PROF_MASK_MR_CACHE,
175 .mask = MLX5_PROF_MASK_QP_SIZE,
181 static const char iov_mac_addr_name[] = "mac-addr";
184 static int set_dma_caps(struct pci_dev *pdev)
186 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
189 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
191 mlx5_core_warn(dev, "couldn't set 64-bit PCI DMA mask\n");
192 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
194 mlx5_core_err(dev, "Can't set PCI DMA mask, aborting\n");
199 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
201 mlx5_core_warn(dev, "couldn't set 64-bit consistent PCI DMA mask\n");
202 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
204 mlx5_core_err(dev, "Can't set consistent PCI DMA mask, aborting\n");
209 dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
213 int mlx5_pci_read_power_status(struct mlx5_core_dev *dev,
214 u16 *p_power, u8 *p_status)
216 u32 in[MLX5_ST_SZ_DW(mpein_reg)] = {};
217 u32 out[MLX5_ST_SZ_DW(mpein_reg)] = {};
220 err = mlx5_core_access_reg(dev, in, sizeof(in), out, sizeof(out),
221 MLX5_ACCESS_REG_SUMMARY_CTRL_ID_MPEIN, 0, 0);
223 *p_status = MLX5_GET(mpein_reg, out, pwr_status);
224 *p_power = MLX5_GET(mpein_reg, out, pci_power);
228 static int mlx5_pci_enable_device(struct mlx5_core_dev *dev)
230 struct pci_dev *pdev = dev->pdev;
233 mutex_lock(&dev->pci_status_mutex);
234 if (dev->pci_status == MLX5_PCI_STATUS_DISABLED) {
235 err = pci_enable_device(pdev);
237 dev->pci_status = MLX5_PCI_STATUS_ENABLED;
239 mutex_unlock(&dev->pci_status_mutex);
244 static void mlx5_pci_disable_device(struct mlx5_core_dev *dev)
246 struct pci_dev *pdev = dev->pdev;
248 mutex_lock(&dev->pci_status_mutex);
249 if (dev->pci_status == MLX5_PCI_STATUS_ENABLED) {
250 pci_disable_device(pdev);
251 dev->pci_status = MLX5_PCI_STATUS_DISABLED;
253 mutex_unlock(&dev->pci_status_mutex);
256 static int request_bar(struct pci_dev *pdev)
258 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
261 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
262 mlx5_core_err(dev, "Missing registers BAR, aborting\n");
266 err = pci_request_regions(pdev, DRIVER_NAME);
268 mlx5_core_err(dev, "Couldn't get PCI resources, aborting\n");
273 static void release_bar(struct pci_dev *pdev)
275 pci_release_regions(pdev);
278 static int mlx5_enable_msix(struct mlx5_core_dev *dev)
280 struct mlx5_priv *priv = &dev->priv;
281 struct mlx5_eq_table *table = &priv->eq_table;
282 int num_eqs = 1 << MLX5_CAP_GEN(dev, log_max_eq);
283 int limit = dev->msix_eqvec;
284 int nvec = MLX5_EQ_VEC_COMP_BASE;
290 nvec += MLX5_CAP_GEN(dev, num_ports) * num_online_cpus();
295 nvec = 256; /* limit of firmware API */
296 if (nvec <= MLX5_EQ_VEC_COMP_BASE)
299 priv->msix_arr = kzalloc(nvec * sizeof(*priv->msix_arr), GFP_KERNEL);
301 for (i = 0; i < nvec; i++)
302 priv->msix_arr[i].entry = i;
304 nvec = pci_enable_msix_range(dev->pdev, priv->msix_arr,
305 MLX5_EQ_VEC_COMP_BASE + 1, nvec);
309 table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
313 static void mlx5_disable_msix(struct mlx5_core_dev *dev)
315 struct mlx5_priv *priv = &dev->priv;
317 pci_disable_msix(dev->pdev);
318 kfree(priv->msix_arr);
321 struct mlx5_reg_host_endianess {
327 #define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos))
330 MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) |
331 MLX5_DEV_CAP_FLAG_DCT |
332 MLX5_DEV_CAP_FLAG_DRAIN_SIGERR,
335 static u16 to_fw_pkey_sz(struct mlx5_core_dev *dev, u32 size)
351 mlx5_core_warn(dev, "invalid pkey table size %d\n", size);
356 static int mlx5_core_get_caps_mode(struct mlx5_core_dev *dev,
357 enum mlx5_cap_type cap_type,
358 enum mlx5_cap_mode cap_mode)
360 u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
361 int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
362 void *out, *hca_caps;
363 u16 opmod = (cap_type << 1) | (cap_mode & 0x01);
366 memset(in, 0, sizeof(in));
367 out = kzalloc(out_sz, GFP_KERNEL);
369 MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
370 MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
371 err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
374 "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n",
375 cap_type, cap_mode, err);
379 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, out, capability);
382 case HCA_CAP_OPMOD_GET_MAX:
383 memcpy(dev->hca_caps_max[cap_type], hca_caps,
384 MLX5_UN_SZ_BYTES(hca_cap_union));
386 case HCA_CAP_OPMOD_GET_CUR:
387 memcpy(dev->hca_caps_cur[cap_type], hca_caps,
388 MLX5_UN_SZ_BYTES(hca_cap_union));
392 "Tried to query dev cap type(%x) with wrong opmode(%x)\n",
402 int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type)
406 ret = mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_CUR);
410 return mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_MAX);
413 static int set_caps(struct mlx5_core_dev *dev, void *in, int in_sz)
415 u32 out[MLX5_ST_SZ_DW(set_hca_cap_out)] = {0};
417 MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
419 return mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
422 static int handle_hca_cap(struct mlx5_core_dev *dev)
424 void *set_ctx = NULL;
425 struct mlx5_profile *prof = dev->profile;
427 int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
430 set_ctx = kzalloc(set_sz, GFP_KERNEL);
432 err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL);
436 set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
438 memcpy(set_hca_cap, dev->hca_caps_cur[MLX5_CAP_GENERAL],
439 MLX5_ST_SZ_BYTES(cmd_hca_cap));
441 mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
442 mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
444 /* we limit the size of the pkey table to 128 entries for now */
445 MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
446 to_fw_pkey_sz(dev, 128));
448 if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
449 MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
452 /* disable cmdif checksum */
453 MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
455 /* enable drain sigerr */
456 MLX5_SET(cmd_hca_cap, set_hca_cap, drain_sigerr, 1);
458 MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12);
460 err = set_caps(dev, set_ctx, set_sz);
467 static int handle_hca_cap_atomic(struct mlx5_core_dev *dev)
471 int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
475 if (MLX5_CAP_GEN(dev, atomic)) {
476 err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC);
485 supported_atomic_req_8B_endianess_mode_1);
487 if (req_endianness != MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS)
490 set_ctx = kzalloc(set_sz, GFP_KERNEL);
494 MLX5_SET(set_hca_cap_in, set_ctx, op_mod,
495 MLX5_SET_HCA_CAP_OP_MOD_ATOMIC << 1);
496 set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
498 /* Set requestor to host endianness */
499 MLX5_SET(atomic_caps, set_hca_cap, atomic_req_8B_endianess_mode,
500 MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS);
502 err = set_caps(dev, set_ctx, set_sz);
508 static int set_hca_ctrl(struct mlx5_core_dev *dev)
510 struct mlx5_reg_host_endianess he_in;
511 struct mlx5_reg_host_endianess he_out;
514 if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH &&
515 !MLX5_CAP_GEN(dev, roce))
518 memset(&he_in, 0, sizeof(he_in));
519 he_in.he = MLX5_SET_HOST_ENDIANNESS;
520 err = mlx5_core_access_reg(dev, &he_in, sizeof(he_in),
521 &he_out, sizeof(he_out),
522 MLX5_REG_HOST_ENDIANNESS, 0, 1);
526 static int mlx5_core_enable_hca(struct mlx5_core_dev *dev, u16 func_id)
528 u32 out[MLX5_ST_SZ_DW(enable_hca_out)] = {0};
529 u32 in[MLX5_ST_SZ_DW(enable_hca_in)] = {0};
531 MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA);
532 MLX5_SET(enable_hca_in, in, function_id, func_id);
533 return mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
536 static int mlx5_core_disable_hca(struct mlx5_core_dev *dev)
538 u32 out[MLX5_ST_SZ_DW(disable_hca_out)] = {0};
539 u32 in[MLX5_ST_SZ_DW(disable_hca_in)] = {0};
541 MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA);
542 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
545 static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
547 u32 query_in[MLX5_ST_SZ_DW(query_issi_in)] = {0};
548 u32 query_out[MLX5_ST_SZ_DW(query_issi_out)] = {0};
552 MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
554 err = mlx5_cmd_exec(dev, query_in, sizeof(query_in), query_out, sizeof(query_out));
559 mlx5_cmd_mbox_status(query_out, &status, &syndrome);
560 if (status == MLX5_CMD_STAT_BAD_OP_ERR) {
561 mlx5_core_dbg(dev, "Only ISSI 0 is supported\n");
565 mlx5_core_err(dev, "failed to query ISSI\n");
569 sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);
571 if (sup_issi & (1 << 1)) {
572 u32 set_in[MLX5_ST_SZ_DW(set_issi_in)] = {0};
573 u32 set_out[MLX5_ST_SZ_DW(set_issi_out)] = {0};
575 MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
576 MLX5_SET(set_issi_in, set_in, current_issi, 1);
578 err = mlx5_cmd_exec(dev, set_in, sizeof(set_in), set_out, sizeof(set_out));
580 mlx5_core_err(dev, "failed to set ISSI=1 err(%d)\n", err);
587 } else if (sup_issi & (1 << 0)) {
595 int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn, int *irqn)
597 struct mlx5_eq_table *table = &dev->priv.eq_table;
601 spin_lock(&table->lock);
602 list_for_each_entry(eq, &table->comp_eqs_list, list) {
603 if (eq->index == vector) {
610 spin_unlock(&table->lock);
614 EXPORT_SYMBOL(mlx5_vector2eqn);
616 static void free_comp_eqs(struct mlx5_core_dev *dev)
618 struct mlx5_eq_table *table = &dev->priv.eq_table;
619 struct mlx5_eq *eq, *n;
621 spin_lock(&table->lock);
622 list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
624 spin_unlock(&table->lock);
625 if (mlx5_destroy_unmap_eq(dev, eq))
626 mlx5_core_warn(dev, "failed to destroy EQ 0x%x\n",
629 spin_lock(&table->lock);
631 spin_unlock(&table->lock);
634 static int alloc_comp_eqs(struct mlx5_core_dev *dev)
636 struct mlx5_eq_table *table = &dev->priv.eq_table;
643 INIT_LIST_HEAD(&table->comp_eqs_list);
644 ncomp_vec = table->num_comp_vectors;
645 nent = MLX5_COMP_EQ_SIZE;
646 for (i = 0; i < ncomp_vec; i++) {
647 eq = kzalloc(sizeof(*eq), GFP_KERNEL);
649 err = mlx5_create_map_eq(dev, eq,
650 i + MLX5_EQ_VEC_COMP_BASE, nent, 0,
651 &dev->priv.uuari.uars[0]);
656 mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->eqn);
658 spin_lock(&table->lock);
659 list_add_tail(&eq->list, &table->comp_eqs_list);
660 spin_unlock(&table->lock);
670 static int map_bf_area(struct mlx5_core_dev *dev)
672 resource_size_t bf_start = pci_resource_start(dev->pdev, 0);
673 resource_size_t bf_len = pci_resource_len(dev->pdev, 0);
675 dev->priv.bf_mapping = io_mapping_create_wc(bf_start, bf_len);
677 return dev->priv.bf_mapping ? 0 : -ENOMEM;
680 static void unmap_bf_area(struct mlx5_core_dev *dev)
682 if (dev->priv.bf_mapping)
683 io_mapping_free(dev->priv.bf_mapping);
686 static inline int fw_initializing(struct mlx5_core_dev *dev)
688 return ioread32be(&dev->iseg->initializing) >> 31;
691 static int wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili,
694 int warn = jiffies + msecs_to_jiffies(warn_time_mili);
695 int end = jiffies + msecs_to_jiffies(max_wait_mili);
698 MPASS(max_wait_mili > warn_time_mili);
700 while (fw_initializing(dev) == 1) {
701 if (time_after(jiffies, end)) {
705 if (warn_time_mili && time_after(jiffies, warn)) {
707 "Waiting for FW initialization, timeout abort in %u s\n",
708 (unsigned int)(jiffies_to_msecs(end - warn) / 1000));
709 warn = jiffies + msecs_to_jiffies(warn_time_mili);
711 msleep(FW_INIT_WAIT_MS);
715 mlx5_core_dbg(dev, "Full initializing bit dword = 0x%x\n",
716 ioread32be(&dev->iseg->initializing));
721 static void mlx5_add_device(struct mlx5_interface *intf, struct mlx5_priv *priv)
723 struct mlx5_device_context *dev_ctx;
724 struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv);
726 dev_ctx = kzalloc(sizeof(*dev_ctx), GFP_KERNEL);
730 dev_ctx->intf = intf;
731 CURVNET_SET_QUIET(vnet0);
732 dev_ctx->context = intf->add(dev);
735 if (dev_ctx->context) {
736 spin_lock_irq(&priv->ctx_lock);
737 list_add_tail(&dev_ctx->list, &priv->ctx_list);
738 spin_unlock_irq(&priv->ctx_lock);
744 static void mlx5_remove_device(struct mlx5_interface *intf, struct mlx5_priv *priv)
746 struct mlx5_device_context *dev_ctx;
747 struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv);
749 list_for_each_entry(dev_ctx, &priv->ctx_list, list)
750 if (dev_ctx->intf == intf) {
751 spin_lock_irq(&priv->ctx_lock);
752 list_del(&dev_ctx->list);
753 spin_unlock_irq(&priv->ctx_lock);
755 intf->remove(dev, dev_ctx->context);
762 mlx5_register_device(struct mlx5_core_dev *dev)
764 struct mlx5_priv *priv = &dev->priv;
765 struct mlx5_interface *intf;
767 mutex_lock(&intf_mutex);
768 list_add_tail(&priv->dev_list, &dev_list);
769 list_for_each_entry(intf, &intf_list, list)
770 mlx5_add_device(intf, priv);
771 mutex_unlock(&intf_mutex);
777 mlx5_unregister_device(struct mlx5_core_dev *dev)
779 struct mlx5_priv *priv = &dev->priv;
780 struct mlx5_interface *intf;
782 mutex_lock(&intf_mutex);
783 list_for_each_entry(intf, &intf_list, list)
784 mlx5_remove_device(intf, priv);
785 list_del(&priv->dev_list);
786 mutex_unlock(&intf_mutex);
789 int mlx5_register_interface(struct mlx5_interface *intf)
791 struct mlx5_priv *priv;
793 if (!intf->add || !intf->remove)
796 mutex_lock(&intf_mutex);
797 list_add_tail(&intf->list, &intf_list);
798 list_for_each_entry(priv, &dev_list, dev_list)
799 mlx5_add_device(intf, priv);
800 mutex_unlock(&intf_mutex);
804 EXPORT_SYMBOL(mlx5_register_interface);
806 void mlx5_unregister_interface(struct mlx5_interface *intf)
808 struct mlx5_priv *priv;
810 mutex_lock(&intf_mutex);
811 list_for_each_entry(priv, &dev_list, dev_list)
812 mlx5_remove_device(intf, priv);
813 list_del(&intf->list);
814 mutex_unlock(&intf_mutex);
816 EXPORT_SYMBOL(mlx5_unregister_interface);
818 void *mlx5_get_protocol_dev(struct mlx5_core_dev *mdev, int protocol)
820 struct mlx5_priv *priv = &mdev->priv;
821 struct mlx5_device_context *dev_ctx;
825 spin_lock_irqsave(&priv->ctx_lock, flags);
827 list_for_each_entry(dev_ctx, &mdev->priv.ctx_list, list)
828 if ((dev_ctx->intf->protocol == protocol) &&
829 dev_ctx->intf->get_dev) {
830 result = dev_ctx->intf->get_dev(dev_ctx->context);
834 spin_unlock_irqrestore(&priv->ctx_lock, flags);
838 EXPORT_SYMBOL(mlx5_get_protocol_dev);
840 static int mlx5_auto_fw_update;
841 SYSCTL_INT(_hw_mlx5, OID_AUTO, auto_fw_update, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
842 &mlx5_auto_fw_update, 0,
843 "Allow automatic firmware update on driver start");
845 mlx5_firmware_update(struct mlx5_core_dev *dev)
847 const struct firmware *fw;
850 TUNABLE_INT_FETCH("hw.mlx5.auto_fw_update", &mlx5_auto_fw_update);
851 if (!mlx5_auto_fw_update)
853 fw = firmware_get("mlx5fw_mfa");
855 err = mlx5_firmware_flash(dev, fw);
856 firmware_put(fw, FIRMWARE_UNLOAD);
864 static int mlx5_pci_init(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
866 struct pci_dev *pdev = dev->pdev;
871 bsddev = pdev->dev.bsddev;
872 pci_set_drvdata(dev->pdev, dev);
873 strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
874 priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
876 mutex_init(&priv->pgdir_mutex);
877 INIT_LIST_HEAD(&priv->pgdir_list);
878 spin_lock_init(&priv->mkey_lock);
880 priv->numa_node = NUMA_NO_NODE;
882 err = mlx5_pci_enable_device(dev);
884 mlx5_core_err(dev, "Cannot enable PCI device, aborting\n");
888 err = request_bar(pdev);
890 mlx5_core_err(dev, "error requesting BARs, aborting\n");
894 pci_set_master(pdev);
896 err = set_dma_caps(pdev);
898 mlx5_core_err(dev, "Failed setting DMA capabilities mask, aborting\n");
902 dev->iseg_base = pci_resource_start(dev->pdev, 0);
903 dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
906 mlx5_core_err(dev, "Failed mapping initialization segment, aborting\n");
913 release_bar(dev->pdev);
915 mlx5_pci_disable_device(dev);
920 static void mlx5_pci_close(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
923 if (MLX5_CAP_GEN(dev, eswitch_flow_table))
924 pci_iov_detach(dev->pdev->dev.bsddev);
927 release_bar(dev->pdev);
928 mlx5_pci_disable_device(dev);
931 static int mlx5_init_once(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
935 err = mlx5_vsc_find_cap(dev);
937 mlx5_core_err(dev, "Unable to find vendor specific capabilities\n");
939 err = mlx5_query_hca_caps(dev);
941 mlx5_core_err(dev, "query hca failed\n");
945 err = mlx5_query_board_id(dev);
947 mlx5_core_err(dev, "query board id failed\n");
951 err = mlx5_eq_init(dev);
953 mlx5_core_err(dev, "failed to initialize eq\n");
957 MLX5_INIT_DOORBELL_LOCK(&priv->cq_uar_lock);
959 err = mlx5_init_cq_table(dev);
961 mlx5_core_err(dev, "failed to initialize cq table\n");
965 mlx5_init_qp_table(dev);
966 mlx5_init_srq_table(dev);
967 mlx5_init_mr_table(dev);
969 mlx5_init_reserved_gids(dev);
973 err = mlx5_init_rl_table(dev);
975 mlx5_core_err(dev, "Failed to init rate limiting\n");
976 goto err_tables_cleanup;
983 mlx5_cleanup_mr_table(dev);
984 mlx5_cleanup_srq_table(dev);
985 mlx5_cleanup_qp_table(dev);
986 mlx5_cleanup_cq_table(dev);
990 mlx5_eq_cleanup(dev);
996 static void mlx5_cleanup_once(struct mlx5_core_dev *dev)
999 mlx5_cleanup_rl_table(dev);
1001 mlx5_fpga_cleanup(dev);
1002 mlx5_cleanup_reserved_gids(dev);
1003 mlx5_cleanup_mr_table(dev);
1004 mlx5_cleanup_srq_table(dev);
1005 mlx5_cleanup_qp_table(dev);
1006 mlx5_cleanup_cq_table(dev);
1007 mlx5_eq_cleanup(dev);
1010 static int mlx5_load_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
1015 mutex_lock(&dev->intf_state_mutex);
1016 if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1017 mlx5_core_warn(dev, "interface is up, NOP\n");
1021 mlx5_core_dbg(dev, "firmware version: %d.%d.%d\n",
1022 fw_rev_maj(dev), fw_rev_min(dev), fw_rev_sub(dev));
1025 * On load removing any previous indication of internal error,
1028 dev->state = MLX5_DEVICE_STATE_UP;
1030 /* wait for firmware to accept initialization segments configurations
1032 err = wait_fw_init(dev, FW_PRE_INIT_TIMEOUT_MILI,
1033 FW_INIT_WARN_MESSAGE_INTERVAL);
1035 dev_err(&dev->pdev->dev,
1036 "Firmware over %d MS in pre-initializing state, aborting\n",
1037 FW_PRE_INIT_TIMEOUT_MILI);
1041 err = mlx5_cmd_init(dev);
1044 "Failed initializing command interface, aborting\n");
1048 err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI, 0);
1051 "Firmware over %d MS in initializing state, aborting\n",
1052 FW_INIT_TIMEOUT_MILI);
1053 goto err_cmd_cleanup;
1056 err = mlx5_core_enable_hca(dev, 0);
1058 mlx5_core_err(dev, "enable hca failed\n");
1059 goto err_cmd_cleanup;
1062 err = mlx5_core_set_issi(dev);
1064 mlx5_core_err(dev, "failed to set issi\n");
1065 goto err_disable_hca;
1068 err = mlx5_pagealloc_start(dev);
1070 mlx5_core_err(dev, "mlx5_pagealloc_start failed\n");
1071 goto err_disable_hca;
1074 err = mlx5_satisfy_startup_pages(dev, 1);
1076 mlx5_core_err(dev, "failed to allocate boot pages\n");
1077 goto err_pagealloc_stop;
1080 err = set_hca_ctrl(dev);
1082 mlx5_core_err(dev, "set_hca_ctrl failed\n");
1083 goto reclaim_boot_pages;
1086 err = handle_hca_cap(dev);
1088 mlx5_core_err(dev, "handle_hca_cap failed\n");
1089 goto reclaim_boot_pages;
1092 err = handle_hca_cap_atomic(dev);
1094 mlx5_core_err(dev, "handle_hca_cap_atomic failed\n");
1095 goto reclaim_boot_pages;
1098 err = mlx5_satisfy_startup_pages(dev, 0);
1100 mlx5_core_err(dev, "failed to allocate init pages\n");
1101 goto reclaim_boot_pages;
1104 err = mlx5_cmd_init_hca(dev);
1106 mlx5_core_err(dev, "init hca failed\n");
1107 goto reclaim_boot_pages;
1110 mlx5_start_health_poll(dev);
1112 if (boot && mlx5_init_once(dev, priv)) {
1113 mlx5_core_err(dev, "sw objs init failed\n");
1117 err = mlx5_enable_msix(dev);
1119 mlx5_core_err(dev, "enable msix failed\n");
1120 goto err_cleanup_once;
1123 err = mlx5_alloc_uuars(dev, &priv->uuari);
1125 mlx5_core_err(dev, "Failed allocating uar, aborting\n");
1126 goto err_disable_msix;
1129 err = mlx5_start_eqs(dev);
1131 mlx5_core_err(dev, "Failed to start pages and async EQs\n");
1135 err = alloc_comp_eqs(dev);
1137 mlx5_core_err(dev, "Failed to alloc completion EQs\n");
1141 if (map_bf_area(dev))
1142 mlx5_core_err(dev, "Failed to map blue flame area\n");
1144 err = mlx5_init_fs(dev);
1146 mlx5_core_err(dev, "flow steering init %d\n", err);
1147 goto err_free_comp_eqs;
1150 err = mlx5_mpfs_init(dev);
1152 mlx5_core_err(dev, "mpfs init failed %d\n", err);
1156 err = mlx5_fpga_device_start(dev);
1158 mlx5_core_err(dev, "fpga device start failed %d\n", err);
1162 err = mlx5_register_device(dev);
1164 mlx5_core_err(dev, "mlx5_register_device failed %d\n", err);
1168 set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1171 mutex_unlock(&dev->intf_state_mutex);
1175 mlx5_fpga_device_stop(dev);
1178 mlx5_mpfs_destroy(dev);
1181 mlx5_cleanup_fs(dev);
1191 mlx5_free_uuars(dev, &priv->uuari);
1194 mlx5_disable_msix(dev);
1198 mlx5_cleanup_once(dev);
1201 mlx5_stop_health_poll(dev, boot);
1202 if (mlx5_cmd_teardown_hca(dev)) {
1203 mlx5_core_err(dev, "tear_down_hca failed, skip cleanup\n");
1208 mlx5_reclaim_startup_pages(dev);
1211 mlx5_pagealloc_stop(dev);
1214 mlx5_core_disable_hca(dev);
1217 mlx5_cmd_cleanup(dev);
1220 dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
1221 mutex_unlock(&dev->intf_state_mutex);
1226 static int mlx5_unload_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
1232 mlx5_drain_health_recovery(dev);
1234 mutex_lock(&dev->intf_state_mutex);
1235 if (!test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1236 mlx5_core_warn(dev, "%s: interface is down, NOP\n", __func__);
1238 mlx5_cleanup_once(dev);
1242 mlx5_unregister_device(dev);
1244 mlx5_eswitch_cleanup(dev->priv.eswitch);
1245 mlx5_fpga_device_stop(dev);
1246 mlx5_mpfs_destroy(dev);
1247 mlx5_cleanup_fs(dev);
1249 mlx5_wait_for_reclaim_vfs_pages(dev);
1252 mlx5_free_uuars(dev, &priv->uuari);
1253 mlx5_disable_msix(dev);
1255 mlx5_cleanup_once(dev);
1256 mlx5_stop_health_poll(dev, cleanup);
1257 err = mlx5_cmd_teardown_hca(dev);
1259 mlx5_core_err(dev, "tear_down_hca failed, skip cleanup\n");
1262 mlx5_pagealloc_stop(dev);
1263 mlx5_reclaim_startup_pages(dev);
1264 mlx5_core_disable_hca(dev);
1265 mlx5_cmd_cleanup(dev);
1268 clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1269 mutex_unlock(&dev->intf_state_mutex);
1273 void mlx5_core_event(struct mlx5_core_dev *dev, enum mlx5_dev_event event,
1274 unsigned long param)
1276 struct mlx5_priv *priv = &dev->priv;
1277 struct mlx5_device_context *dev_ctx;
1278 unsigned long flags;
1280 spin_lock_irqsave(&priv->ctx_lock, flags);
1282 list_for_each_entry(dev_ctx, &priv->ctx_list, list)
1283 if (dev_ctx->intf->event)
1284 dev_ctx->intf->event(dev, dev_ctx->context, event, param);
1286 spin_unlock_irqrestore(&priv->ctx_lock, flags);
1289 struct mlx5_core_event_handler {
1290 void (*event)(struct mlx5_core_dev *dev,
1291 enum mlx5_dev_event event,
1295 #define MLX5_STATS_DESC(a, b, c, d, e, ...) d, e,
1297 #define MLX5_PORT_MODULE_ERROR_STATS(m) \
1298 m(+1, u64, power_budget_exceeded, "power_budget", "Module Power Budget Exceeded") \
1299 m(+1, u64, long_range, "long_range", "Module Long Range for non MLNX cable/module") \
1300 m(+1, u64, bus_stuck, "bus_stuck", "Module Bus stuck(I2C or data shorted)") \
1301 m(+1, u64, no_eeprom, "no_eeprom", "No EEPROM/retry timeout") \
1302 m(+1, u64, enforce_part_number, "enforce_part_number", "Module Enforce part number list") \
1303 m(+1, u64, unknown_id, "unknown_id", "Module Unknown identifier") \
1304 m(+1, u64, high_temp, "high_temp", "Module High Temperature") \
1305 m(+1, u64, cable_shorted, "cable_shorted", "Module Cable is shorted") \
1306 m(+1, u64, pmd_type_not_enabled, "pmd_type_not_enabled", "PMD type is not enabled") \
1307 m(+1, u64, laster_tec_failure, "laster_tec_failure", "Laster TEC failure") \
1308 m(+1, u64, high_current, "high_current", "High current") \
1309 m(+1, u64, high_voltage, "high_voltage", "High voltage") \
1310 m(+1, u64, pcie_sys_power_slot_exceeded, "pcie_sys_power_slot_exceeded", "PCIe system power slot Exceeded") \
1311 m(+1, u64, high_power, "high_power", "High power") \
1312 m(+1, u64, module_state_machine_fault, "module_state_machine_fault", "Module State Machine fault")
1314 static const char *mlx5_pme_err_desc[] = {
1315 MLX5_PORT_MODULE_ERROR_STATS(MLX5_STATS_DESC)
1318 static int init_one(struct pci_dev *pdev,
1319 const struct pci_device_id *id)
1321 struct mlx5_core_dev *dev;
1322 struct mlx5_priv *priv;
1323 device_t bsddev = pdev->dev.bsddev;
1325 nvlist_t *pf_schema, *vf_schema;
1326 int num_vfs, sriov_pos;
1329 struct sysctl_oid *pme_sysctl_node;
1330 struct sysctl_oid *pme_err_sysctl_node;
1331 struct sysctl_oid *cap_sysctl_node;
1332 struct sysctl_oid *current_cap_sysctl_node;
1333 struct sysctl_oid *max_cap_sysctl_node;
1335 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1338 priv->pci_dev_data = id->driver_data;
1340 if (mlx5_prof_sel < 0 || mlx5_prof_sel >= ARRAY_SIZE(profiles)) {
1341 device_printf(bsddev,
1342 "WARN: selected profile out of range, selecting default (%d)\n",
1344 mlx5_prof_sel = MLX5_DEFAULT_PROF;
1346 dev->profile = &profiles[mlx5_prof_sel];
1348 dev->event = mlx5_core_event;
1351 device_set_desc(bsddev, mlx5_version);
1353 sysctl_ctx_init(&dev->sysctl_ctx);
1354 SYSCTL_ADD_INT(&dev->sysctl_ctx,
1355 SYSCTL_CHILDREN(device_get_sysctl_tree(bsddev)),
1356 OID_AUTO, "msix_eqvec", CTLFLAG_RDTUN, &dev->msix_eqvec, 0,
1357 "Maximum number of MSIX event queue vectors, if set");
1358 SYSCTL_ADD_INT(&dev->sysctl_ctx,
1359 SYSCTL_CHILDREN(device_get_sysctl_tree(bsddev)),
1360 OID_AUTO, "power_status", CTLFLAG_RD, &dev->pwr_status, 0,
1361 "0:Invalid 1:Sufficient 2:Insufficient");
1362 SYSCTL_ADD_INT(&dev->sysctl_ctx,
1363 SYSCTL_CHILDREN(device_get_sysctl_tree(bsddev)),
1364 OID_AUTO, "power_value", CTLFLAG_RD, &dev->pwr_value, 0,
1365 "Current power value in Watts");
1367 pme_sysctl_node = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
1368 SYSCTL_CHILDREN(device_get_sysctl_tree(bsddev)),
1369 OID_AUTO, "pme_stats", CTLFLAG_RD, NULL,
1370 "Port module event statistics");
1371 if (pme_sysctl_node == NULL) {
1373 goto clean_sysctl_ctx;
1375 pme_err_sysctl_node = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
1376 SYSCTL_CHILDREN(pme_sysctl_node),
1377 OID_AUTO, "errors", CTLFLAG_RD, NULL,
1378 "Port module event error statistics");
1379 if (pme_err_sysctl_node == NULL) {
1381 goto clean_sysctl_ctx;
1383 SYSCTL_ADD_U64(&dev->sysctl_ctx,
1384 SYSCTL_CHILDREN(pme_sysctl_node), OID_AUTO,
1385 "module_plug", CTLFLAG_RD | CTLFLAG_MPSAFE,
1386 &dev->priv.pme_stats.status_counters[MLX5_MODULE_STATUS_PLUGGED_ENABLED],
1387 0, "Number of time module plugged");
1388 SYSCTL_ADD_U64(&dev->sysctl_ctx,
1389 SYSCTL_CHILDREN(pme_sysctl_node), OID_AUTO,
1390 "module_unplug", CTLFLAG_RD | CTLFLAG_MPSAFE,
1391 &dev->priv.pme_stats.status_counters[MLX5_MODULE_STATUS_UNPLUGGED],
1392 0, "Number of time module unplugged");
1393 for (i = 0 ; i < MLX5_MODULE_EVENT_ERROR_NUM; i++) {
1394 SYSCTL_ADD_U64(&dev->sysctl_ctx,
1395 SYSCTL_CHILDREN(pme_err_sysctl_node), OID_AUTO,
1396 mlx5_pme_err_desc[2 * i], CTLFLAG_RD | CTLFLAG_MPSAFE,
1397 &dev->priv.pme_stats.error_counters[i],
1398 0, mlx5_pme_err_desc[2 * i + 1]);
1401 cap_sysctl_node = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
1402 SYSCTL_CHILDREN(device_get_sysctl_tree(bsddev)),
1403 OID_AUTO, "caps", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1404 "hardware capabilities raw bitstrings");
1405 if (cap_sysctl_node == NULL) {
1407 goto clean_sysctl_ctx;
1409 current_cap_sysctl_node = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
1410 SYSCTL_CHILDREN(cap_sysctl_node),
1411 OID_AUTO, "current", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1413 if (current_cap_sysctl_node == NULL) {
1415 goto clean_sysctl_ctx;
1417 max_cap_sysctl_node = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
1418 SYSCTL_CHILDREN(cap_sysctl_node),
1419 OID_AUTO, "max", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1421 if (max_cap_sysctl_node == NULL) {
1423 goto clean_sysctl_ctx;
1425 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1426 SYSCTL_CHILDREN(current_cap_sysctl_node),
1427 OID_AUTO, "general", CTLFLAG_RD | CTLFLAG_MPSAFE,
1428 &dev->hca_caps_cur[MLX5_CAP_GENERAL],
1429 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1430 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1431 SYSCTL_CHILDREN(max_cap_sysctl_node),
1432 OID_AUTO, "general", CTLFLAG_RD | CTLFLAG_MPSAFE,
1433 &dev->hca_caps_max[MLX5_CAP_GENERAL],
1434 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1435 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1436 SYSCTL_CHILDREN(current_cap_sysctl_node),
1437 OID_AUTO, "ether", CTLFLAG_RD | CTLFLAG_MPSAFE,
1438 &dev->hca_caps_cur[MLX5_CAP_ETHERNET_OFFLOADS],
1439 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1440 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1441 SYSCTL_CHILDREN(max_cap_sysctl_node),
1442 OID_AUTO, "ether", CTLFLAG_RD | CTLFLAG_MPSAFE,
1443 &dev->hca_caps_max[MLX5_CAP_ETHERNET_OFFLOADS],
1444 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1445 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1446 SYSCTL_CHILDREN(current_cap_sysctl_node),
1447 OID_AUTO, "odp", CTLFLAG_RD | CTLFLAG_MPSAFE,
1448 &dev->hca_caps_cur[MLX5_CAP_ODP],
1449 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1450 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1451 SYSCTL_CHILDREN(max_cap_sysctl_node),
1452 OID_AUTO, "odp", CTLFLAG_RD | CTLFLAG_MPSAFE,
1453 &dev->hca_caps_max[MLX5_CAP_ODP],
1454 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1455 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1456 SYSCTL_CHILDREN(current_cap_sysctl_node),
1457 OID_AUTO, "atomic", CTLFLAG_RD | CTLFLAG_MPSAFE,
1458 &dev->hca_caps_cur[MLX5_CAP_ATOMIC],
1459 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1460 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1461 SYSCTL_CHILDREN(max_cap_sysctl_node),
1462 OID_AUTO, "atomic", CTLFLAG_RD | CTLFLAG_MPSAFE,
1463 &dev->hca_caps_max[MLX5_CAP_ATOMIC],
1464 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1465 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1466 SYSCTL_CHILDREN(current_cap_sysctl_node),
1467 OID_AUTO, "roce", CTLFLAG_RD | CTLFLAG_MPSAFE,
1468 &dev->hca_caps_cur[MLX5_CAP_ROCE],
1469 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1470 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1471 SYSCTL_CHILDREN(max_cap_sysctl_node),
1472 OID_AUTO, "roce", CTLFLAG_RD | CTLFLAG_MPSAFE,
1473 &dev->hca_caps_max[MLX5_CAP_ROCE],
1474 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1475 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1476 SYSCTL_CHILDREN(current_cap_sysctl_node),
1477 OID_AUTO, "ipoib", CTLFLAG_RD | CTLFLAG_MPSAFE,
1478 &dev->hca_caps_cur[MLX5_CAP_IPOIB_OFFLOADS],
1479 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1480 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1481 SYSCTL_CHILDREN(max_cap_sysctl_node),
1482 OID_AUTO, "ipoib", CTLFLAG_RD | CTLFLAG_MPSAFE,
1483 &dev->hca_caps_max[MLX5_CAP_IPOIB_OFFLOADS],
1484 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1485 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1486 SYSCTL_CHILDREN(current_cap_sysctl_node),
1487 OID_AUTO, "eoib", CTLFLAG_RD | CTLFLAG_MPSAFE,
1488 &dev->hca_caps_cur[MLX5_CAP_EOIB_OFFLOADS],
1489 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1490 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1491 SYSCTL_CHILDREN(max_cap_sysctl_node),
1492 OID_AUTO, "eoib", CTLFLAG_RD | CTLFLAG_MPSAFE,
1493 &dev->hca_caps_max[MLX5_CAP_EOIB_OFFLOADS],
1494 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1495 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1496 SYSCTL_CHILDREN(current_cap_sysctl_node),
1497 OID_AUTO, "flow_table", CTLFLAG_RD | CTLFLAG_MPSAFE,
1498 &dev->hca_caps_cur[MLX5_CAP_FLOW_TABLE],
1499 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1500 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1501 SYSCTL_CHILDREN(max_cap_sysctl_node),
1502 OID_AUTO, "flow_table", CTLFLAG_RD | CTLFLAG_MPSAFE,
1503 &dev->hca_caps_max[MLX5_CAP_FLOW_TABLE],
1504 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1505 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1506 SYSCTL_CHILDREN(current_cap_sysctl_node),
1507 OID_AUTO, "eswitch_flow_table", CTLFLAG_RD | CTLFLAG_MPSAFE,
1508 &dev->hca_caps_cur[MLX5_CAP_ESWITCH_FLOW_TABLE],
1509 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1510 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1511 SYSCTL_CHILDREN(max_cap_sysctl_node),
1512 OID_AUTO, "eswitch_flow_table", CTLFLAG_RD | CTLFLAG_MPSAFE,
1513 &dev->hca_caps_max[MLX5_CAP_ESWITCH_FLOW_TABLE],
1514 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1515 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1516 SYSCTL_CHILDREN(current_cap_sysctl_node),
1517 OID_AUTO, "eswitch", CTLFLAG_RD | CTLFLAG_MPSAFE,
1518 &dev->hca_caps_cur[MLX5_CAP_ESWITCH],
1519 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1520 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1521 SYSCTL_CHILDREN(max_cap_sysctl_node),
1522 OID_AUTO, "eswitch", CTLFLAG_RD | CTLFLAG_MPSAFE,
1523 &dev->hca_caps_max[MLX5_CAP_ESWITCH],
1524 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1525 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1526 SYSCTL_CHILDREN(current_cap_sysctl_node),
1527 OID_AUTO, "snapshot", CTLFLAG_RD | CTLFLAG_MPSAFE,
1528 &dev->hca_caps_cur[MLX5_CAP_SNAPSHOT],
1529 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1530 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1531 SYSCTL_CHILDREN(max_cap_sysctl_node),
1532 OID_AUTO, "snapshot", CTLFLAG_RD | CTLFLAG_MPSAFE,
1533 &dev->hca_caps_max[MLX5_CAP_SNAPSHOT],
1534 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1535 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1536 SYSCTL_CHILDREN(current_cap_sysctl_node),
1537 OID_AUTO, "vector_calc", CTLFLAG_RD | CTLFLAG_MPSAFE,
1538 &dev->hca_caps_cur[MLX5_CAP_VECTOR_CALC],
1539 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1540 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1541 SYSCTL_CHILDREN(max_cap_sysctl_node),
1542 OID_AUTO, "vector_calc", CTLFLAG_RD | CTLFLAG_MPSAFE,
1543 &dev->hca_caps_max[MLX5_CAP_VECTOR_CALC],
1544 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1545 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1546 SYSCTL_CHILDREN(current_cap_sysctl_node),
1547 OID_AUTO, "qos", CTLFLAG_RD | CTLFLAG_MPSAFE,
1548 &dev->hca_caps_cur[MLX5_CAP_QOS],
1549 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1550 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1551 SYSCTL_CHILDREN(max_cap_sysctl_node),
1552 OID_AUTO, "qos", CTLFLAG_RD | CTLFLAG_MPSAFE,
1553 &dev->hca_caps_max[MLX5_CAP_QOS],
1554 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1555 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1556 SYSCTL_CHILDREN(current_cap_sysctl_node),
1557 OID_AUTO, "debug", CTLFLAG_RD | CTLFLAG_MPSAFE,
1558 &dev->hca_caps_cur[MLX5_CAP_DEBUG],
1559 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1560 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1561 SYSCTL_CHILDREN(max_cap_sysctl_node),
1562 OID_AUTO, "debug", CTLFLAG_RD | CTLFLAG_MPSAFE,
1563 &dev->hca_caps_max[MLX5_CAP_DEBUG],
1564 MLX5_UN_SZ_DW(hca_cap_union) * sizeof(u32), "IU", "");
1565 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1566 SYSCTL_CHILDREN(cap_sysctl_node),
1567 OID_AUTO, "pcam", CTLFLAG_RD | CTLFLAG_MPSAFE,
1568 &dev->caps.pcam, sizeof(dev->caps.pcam), "IU", "");
1569 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1570 SYSCTL_CHILDREN(cap_sysctl_node),
1571 OID_AUTO, "mcam", CTLFLAG_RD | CTLFLAG_MPSAFE,
1572 &dev->caps.mcam, sizeof(dev->caps.mcam), "IU", "");
1573 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1574 SYSCTL_CHILDREN(cap_sysctl_node),
1575 OID_AUTO, "qcam", CTLFLAG_RD | CTLFLAG_MPSAFE,
1576 &dev->caps.qcam, sizeof(dev->caps.qcam), "IU", "");
1577 SYSCTL_ADD_OPAQUE(&dev->sysctl_ctx,
1578 SYSCTL_CHILDREN(cap_sysctl_node),
1579 OID_AUTO, "fpga", CTLFLAG_RD | CTLFLAG_MPSAFE,
1580 &dev->caps.fpga, sizeof(dev->caps.fpga), "IU", "");
1582 INIT_LIST_HEAD(&priv->ctx_list);
1583 spin_lock_init(&priv->ctx_lock);
1584 mutex_init(&dev->pci_status_mutex);
1585 mutex_init(&dev->intf_state_mutex);
1586 mtx_init(&dev->dump_lock, "mlx5dmp", NULL, MTX_DEF | MTX_NEW);
1587 err = mlx5_pci_init(dev, priv);
1589 mlx5_core_err(dev, "mlx5_pci_init failed %d\n", err);
1593 err = mlx5_health_init(dev);
1595 mlx5_core_err(dev, "mlx5_health_init failed %d\n", err);
1599 mlx5_pagealloc_init(dev);
1601 err = mlx5_load_one(dev, priv, true);
1603 mlx5_core_err(dev, "mlx5_load_one failed %d\n", err);
1607 mlx5_fwdump_prep(dev);
1609 mlx5_firmware_update(dev);
1612 if (MLX5_CAP_GEN(dev, vport_group_manager)) {
1613 if (pci_find_extcap(bsddev, PCIZ_SRIOV, &sriov_pos) == 0) {
1614 num_vfs = pci_read_config(bsddev, sriov_pos +
1615 PCIR_SRIOV_TOTAL_VFS, 2);
1617 mlx5_core_info(dev, "cannot find SR-IOV PCIe cap\n");
1620 err = mlx5_eswitch_init(dev, 1 + num_vfs);
1622 pf_schema = pci_iov_schema_alloc_node();
1623 vf_schema = pci_iov_schema_alloc_node();
1624 pci_iov_schema_add_unicast_mac(vf_schema,
1625 iov_mac_addr_name, 0, NULL);
1626 err = pci_iov_attach(bsddev, pf_schema, vf_schema);
1628 device_printf(bsddev,
1629 "Failed to initialize SR-IOV support, error %d\n",
1633 mlx5_core_err(dev, "eswitch init failed, error %d\n",
1639 pci_save_state(bsddev);
1643 mlx5_pagealloc_cleanup(dev);
1644 mlx5_health_cleanup(dev);
1646 mlx5_pci_close(dev, priv);
1648 mtx_destroy(&dev->dump_lock);
1650 sysctl_ctx_free(&dev->sysctl_ctx);
1655 static void remove_one(struct pci_dev *pdev)
1657 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1658 struct mlx5_priv *priv = &dev->priv;
1660 if (mlx5_unload_one(dev, priv, true)) {
1661 mlx5_core_err(dev, "mlx5_unload_one failed\n");
1662 mlx5_health_cleanup(dev);
1666 mlx5_pagealloc_cleanup(dev);
1667 mlx5_health_cleanup(dev);
1668 mlx5_fwdump_clean(dev);
1669 mlx5_pci_close(dev, priv);
1670 mtx_destroy(&dev->dump_lock);
1671 pci_set_drvdata(pdev, NULL);
1672 sysctl_ctx_free(&dev->sysctl_ctx);
1676 static pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev,
1677 pci_channel_state_t state)
1679 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1680 struct mlx5_priv *priv = &dev->priv;
1682 mlx5_core_info(dev, "%s was called\n", __func__);
1683 mlx5_enter_error_state(dev, false);
1684 mlx5_unload_one(dev, priv, false);
1687 mlx5_drain_health_wq(dev);
1688 mlx5_pci_disable_device(dev);
1691 return state == pci_channel_io_perm_failure ?
1692 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1695 static pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev)
1697 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1700 mlx5_core_info(dev,"%s was called\n", __func__);
1702 err = mlx5_pci_enable_device(dev);
1704 mlx5_core_err(dev, "mlx5_pci_enable_device failed with error code: %d\n"
1706 return PCI_ERS_RESULT_DISCONNECT;
1708 pci_set_master(pdev);
1709 pci_set_powerstate(pdev->dev.bsddev, PCI_POWERSTATE_D0);
1710 pci_restore_state(pdev->dev.bsddev);
1711 pci_save_state(pdev->dev.bsddev);
1713 return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
1716 /* wait for the device to show vital signs. For now we check
1717 * that we can read the device ID and that the health buffer
1718 * shows a non zero value which is different than 0xffffffff
1720 static void wait_vital(struct pci_dev *pdev)
1722 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1723 struct mlx5_core_health *health = &dev->priv.health;
1724 const int niter = 100;
1729 /* Wait for firmware to be ready after reset */
1731 for (i = 0; i < niter; i++) {
1732 if (pci_read_config_word(pdev, 2, &did)) {
1733 mlx5_core_warn(dev, "failed reading config word\n");
1736 if (did == pdev->device) {
1738 "device ID correctly read after %d iterations\n", i);
1744 mlx5_core_warn(dev, "could not read device ID\n");
1746 for (i = 0; i < niter; i++) {
1747 count = ioread32be(health->health_counter);
1748 if (count && count != 0xffffffff) {
1750 "Counter value 0x%x after %d iterations\n", count, i);
1757 mlx5_core_warn(dev, "could not read device ID\n");
1760 static void mlx5_pci_resume(struct pci_dev *pdev)
1762 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1763 struct mlx5_priv *priv = &dev->priv;
1766 mlx5_core_info(dev,"%s was called\n", __func__);
1770 err = mlx5_load_one(dev, priv, false);
1773 "mlx5_load_one failed with error code: %d\n" ,err);
1775 mlx5_core_info(dev,"device recovered\n");
1778 static const struct pci_error_handlers mlx5_err_handler = {
1779 .error_detected = mlx5_pci_err_detected,
1780 .slot_reset = mlx5_pci_slot_reset,
1781 .resume = mlx5_pci_resume
1786 mlx5_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *pf_config)
1788 struct pci_dev *pdev;
1789 struct mlx5_core_dev *core_dev;
1790 struct mlx5_priv *priv;
1793 pdev = device_get_softc(dev);
1794 core_dev = pci_get_drvdata(pdev);
1795 priv = &core_dev->priv;
1797 if (priv->eswitch == NULL)
1799 if (priv->eswitch->total_vports < num_vfs + 1)
1800 num_vfs = priv->eswitch->total_vports - 1;
1801 err = mlx5_eswitch_enable_sriov(priv->eswitch, num_vfs);
1806 mlx5_iov_uninit(device_t dev)
1808 struct pci_dev *pdev;
1809 struct mlx5_core_dev *core_dev;
1810 struct mlx5_priv *priv;
1812 pdev = device_get_softc(dev);
1813 core_dev = pci_get_drvdata(pdev);
1814 priv = &core_dev->priv;
1816 mlx5_eswitch_disable_sriov(priv->eswitch);
1820 mlx5_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *vf_config)
1822 struct pci_dev *pdev;
1823 struct mlx5_core_dev *core_dev;
1824 struct mlx5_priv *priv;
1829 pdev = device_get_softc(dev);
1830 core_dev = pci_get_drvdata(pdev);
1831 priv = &core_dev->priv;
1833 if (vfnum + 1 >= priv->eswitch->total_vports)
1836 if (nvlist_exists_binary(vf_config, iov_mac_addr_name)) {
1837 mac = nvlist_get_binary(vf_config, iov_mac_addr_name,
1839 error = -mlx5_eswitch_set_vport_mac(priv->eswitch,
1840 vfnum + 1, __DECONST(u8 *, mac));
1842 mlx5_core_err(core_dev,
1843 "setting MAC for VF %d failed, error %d\n",
1848 error = -mlx5_eswitch_set_vport_state(priv->eswitch, vfnum + 1,
1849 VPORT_STATE_FOLLOW);
1851 mlx5_core_err(core_dev,
1852 "upping vport for VF %d failed, error %d\n",
1855 error = -mlx5_core_enable_hca(core_dev, vfnum + 1);
1857 mlx5_core_err(core_dev, "enabling VF %d failed, error %d\n",
1864 static int mlx5_try_fast_unload(struct mlx5_core_dev *dev)
1866 bool fast_teardown, force_teardown;
1869 if (!mlx5_fast_unload_enabled) {
1870 mlx5_core_dbg(dev, "fast unload is disabled by user\n");
1874 fast_teardown = MLX5_CAP_GEN(dev, fast_teardown);
1875 force_teardown = MLX5_CAP_GEN(dev, force_teardown);
1877 mlx5_core_dbg(dev, "force teardown firmware support=%d\n", force_teardown);
1878 mlx5_core_dbg(dev, "fast teardown firmware support=%d\n", fast_teardown);
1880 if (!fast_teardown && !force_teardown)
1883 if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
1884 mlx5_core_dbg(dev, "Device in internal error state, giving up\n");
1888 /* Panic tear down fw command will stop the PCI bus communication
1889 * with the HCA, so the health polll is no longer needed.
1891 mlx5_drain_health_wq(dev);
1892 mlx5_stop_health_poll(dev, false);
1894 err = mlx5_cmd_fast_teardown_hca(dev);
1898 err = mlx5_cmd_force_teardown_hca(dev);
1902 mlx5_core_dbg(dev, "Firmware couldn't do fast unload error: %d\n", err);
1903 mlx5_start_health_poll(dev);
1906 mlx5_enter_error_state(dev, true);
1910 static void mlx5_shutdown_disable_interrupts(struct mlx5_core_dev *mdev)
1912 int nvec = mdev->priv.eq_table.num_comp_vectors + MLX5_EQ_VEC_COMP_BASE;
1915 mdev->priv.disable_irqs = 1;
1917 /* wait for all IRQ handlers to finish processing */
1918 for (x = 0; x != nvec; x++)
1919 synchronize_irq(mdev->priv.msix_arr[x].vector);
1922 static void shutdown_one(struct pci_dev *pdev)
1924 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1925 struct mlx5_priv *priv = &dev->priv;
1928 /* enter polling mode */
1929 mlx5_cmd_use_polling(dev);
1931 set_bit(MLX5_INTERFACE_STATE_TEARDOWN, &dev->intf_state);
1933 /* disable all interrupts */
1934 mlx5_shutdown_disable_interrupts(dev);
1936 err = mlx5_try_fast_unload(dev);
1938 mlx5_unload_one(dev, priv, false);
1939 mlx5_pci_disable_device(dev);
1942 static const struct pci_device_id mlx5_core_pci_table[] = {
1943 { PCI_VDEVICE(MELLANOX, 4113) }, /* Connect-IB */
1944 { PCI_VDEVICE(MELLANOX, 4114) }, /* Connect-IB VF */
1945 { PCI_VDEVICE(MELLANOX, 4115) }, /* ConnectX-4 */
1946 { PCI_VDEVICE(MELLANOX, 4116) }, /* ConnectX-4 VF */
1947 { PCI_VDEVICE(MELLANOX, 4117) }, /* ConnectX-4LX */
1948 { PCI_VDEVICE(MELLANOX, 4118) }, /* ConnectX-4LX VF */
1949 { PCI_VDEVICE(MELLANOX, 4119) }, /* ConnectX-5 */
1950 { PCI_VDEVICE(MELLANOX, 4120) }, /* ConnectX-5 VF */
1951 { PCI_VDEVICE(MELLANOX, 4121) },
1952 { PCI_VDEVICE(MELLANOX, 4122) },
1953 { PCI_VDEVICE(MELLANOX, 4123) },
1954 { PCI_VDEVICE(MELLANOX, 4124) },
1955 { PCI_VDEVICE(MELLANOX, 4125) },
1956 { PCI_VDEVICE(MELLANOX, 4126) },
1957 { PCI_VDEVICE(MELLANOX, 4127) },
1958 { PCI_VDEVICE(MELLANOX, 4128) },
1959 { PCI_VDEVICE(MELLANOX, 4129) },
1960 { PCI_VDEVICE(MELLANOX, 4130) },
1961 { PCI_VDEVICE(MELLANOX, 4131) },
1962 { PCI_VDEVICE(MELLANOX, 4132) },
1963 { PCI_VDEVICE(MELLANOX, 4133) },
1964 { PCI_VDEVICE(MELLANOX, 4134) },
1965 { PCI_VDEVICE(MELLANOX, 4135) },
1966 { PCI_VDEVICE(MELLANOX, 4136) },
1967 { PCI_VDEVICE(MELLANOX, 4137) },
1968 { PCI_VDEVICE(MELLANOX, 4138) },
1969 { PCI_VDEVICE(MELLANOX, 4139) },
1970 { PCI_VDEVICE(MELLANOX, 4140) },
1971 { PCI_VDEVICE(MELLANOX, 4141) },
1972 { PCI_VDEVICE(MELLANOX, 4142) },
1973 { PCI_VDEVICE(MELLANOX, 4143) },
1974 { PCI_VDEVICE(MELLANOX, 4144) },
1978 MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
1980 void mlx5_disable_device(struct mlx5_core_dev *dev)
1982 mlx5_pci_err_detected(dev->pdev, 0);
1985 void mlx5_recover_device(struct mlx5_core_dev *dev)
1987 mlx5_pci_disable_device(dev);
1988 if (mlx5_pci_slot_reset(dev->pdev) == PCI_ERS_RESULT_RECOVERED)
1989 mlx5_pci_resume(dev->pdev);
1992 struct pci_driver mlx5_core_driver = {
1993 .name = DRIVER_NAME,
1994 .id_table = mlx5_core_pci_table,
1995 .shutdown = shutdown_one,
1997 .remove = remove_one,
1998 .err_handler = &mlx5_err_handler,
2000 .bsd_iov_init = mlx5_iov_init,
2001 .bsd_iov_uninit = mlx5_iov_uninit,
2002 .bsd_iov_add_vf = mlx5_iov_add_vf,
2006 static int __init init(void)
2010 err = pci_register_driver(&mlx5_core_driver);
2014 err = mlx5_ctl_init();
2021 pci_unregister_driver(&mlx5_core_driver);
2027 static void __exit cleanup(void)
2030 pci_unregister_driver(&mlx5_core_driver);
2033 module_init_order(init, SI_ORDER_FIRST);
2034 module_exit_order(cleanup, SI_ORDER_FIRST);