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 #include <linux/mlx5/vport.h>
51 #ifdef CONFIG_RFS_ACCEL
52 #include <linux/cpu_rmap.h>
54 #include <net/devlink.h>
55 #include "mlx5_core.h"
60 #include "fpga/core.h"
61 #include "accel/ipsec.h"
62 #include "lib/clock.h"
64 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
65 MODULE_DESCRIPTION("Mellanox Connect-IB, ConnectX-4 core driver");
66 MODULE_LICENSE("Dual BSD/GPL");
67 MODULE_VERSION(DRIVER_VERSION);
69 unsigned int mlx5_core_debug_mask;
70 module_param_named(debug_mask, mlx5_core_debug_mask, uint, 0644);
71 MODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
73 #define MLX5_DEFAULT_PROF 2
74 static unsigned int prof_sel = MLX5_DEFAULT_PROF;
75 module_param_named(prof_sel, prof_sel, uint, 0444);
76 MODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");
79 MLX5_ATOMIC_REQ_MODE_BE = 0x0,
80 MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS = 0x1,
83 static struct mlx5_profile profile[] = {
88 .mask = MLX5_PROF_MASK_QP_SIZE,
92 .mask = MLX5_PROF_MASK_QP_SIZE |
93 MLX5_PROF_MASK_MR_CACHE,
182 #define FW_INIT_TIMEOUT_MILI 2000
183 #define FW_INIT_WAIT_MS 2
184 #define FW_PRE_INIT_TIMEOUT_MILI 10000
186 static int wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili)
188 unsigned long end = jiffies + msecs_to_jiffies(max_wait_mili);
191 while (fw_initializing(dev)) {
192 if (time_after(jiffies, end)) {
196 msleep(FW_INIT_WAIT_MS);
202 static void mlx5_set_driver_version(struct mlx5_core_dev *dev)
204 int driver_ver_sz = MLX5_FLD_SZ_BYTES(set_driver_version_in,
206 u8 in[MLX5_ST_SZ_BYTES(set_driver_version_in)] = {0};
207 u8 out[MLX5_ST_SZ_BYTES(set_driver_version_out)] = {0};
208 int remaining_size = driver_ver_sz;
211 if (!MLX5_CAP_GEN(dev, driver_version))
214 string = MLX5_ADDR_OF(set_driver_version_in, in, driver_version);
216 strncpy(string, "Linux", remaining_size);
218 remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
219 strncat(string, ",", remaining_size);
221 remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
222 strncat(string, DRIVER_NAME, remaining_size);
224 remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
225 strncat(string, ",", remaining_size);
227 remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
228 strncat(string, DRIVER_VERSION, remaining_size);
231 MLX5_SET(set_driver_version_in, in, opcode,
232 MLX5_CMD_OP_SET_DRIVER_VERSION);
234 mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
237 static int set_dma_caps(struct pci_dev *pdev)
241 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
243 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
244 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
246 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
251 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
254 "Warning: couldn't set 64-bit consistent PCI DMA mask\n");
255 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
258 "Can't set consistent PCI DMA mask, aborting\n");
263 dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
267 static int mlx5_pci_enable_device(struct mlx5_core_dev *dev)
269 struct pci_dev *pdev = dev->pdev;
272 mutex_lock(&dev->pci_status_mutex);
273 if (dev->pci_status == MLX5_PCI_STATUS_DISABLED) {
274 err = pci_enable_device(pdev);
276 dev->pci_status = MLX5_PCI_STATUS_ENABLED;
278 mutex_unlock(&dev->pci_status_mutex);
283 static void mlx5_pci_disable_device(struct mlx5_core_dev *dev)
285 struct pci_dev *pdev = dev->pdev;
287 mutex_lock(&dev->pci_status_mutex);
288 if (dev->pci_status == MLX5_PCI_STATUS_ENABLED) {
289 pci_disable_device(pdev);
290 dev->pci_status = MLX5_PCI_STATUS_DISABLED;
292 mutex_unlock(&dev->pci_status_mutex);
295 static int request_bar(struct pci_dev *pdev)
299 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
300 dev_err(&pdev->dev, "Missing registers BAR, aborting\n");
304 err = pci_request_regions(pdev, DRIVER_NAME);
306 dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
311 static void release_bar(struct pci_dev *pdev)
313 pci_release_regions(pdev);
316 static int mlx5_alloc_irq_vectors(struct mlx5_core_dev *dev)
318 struct mlx5_priv *priv = &dev->priv;
319 struct mlx5_eq_table *table = &priv->eq_table;
320 struct irq_affinity irqdesc = {
321 .pre_vectors = MLX5_EQ_VEC_COMP_BASE,
323 int num_eqs = 1 << MLX5_CAP_GEN(dev, log_max_eq);
326 nvec = MLX5_CAP_GEN(dev, num_ports) * num_online_cpus() +
327 MLX5_EQ_VEC_COMP_BASE;
328 nvec = min_t(int, nvec, num_eqs);
329 if (nvec <= MLX5_EQ_VEC_COMP_BASE)
332 priv->irq_info = kcalloc(nvec, sizeof(*priv->irq_info), GFP_KERNEL);
336 nvec = pci_alloc_irq_vectors_affinity(dev->pdev,
337 MLX5_EQ_VEC_COMP_BASE + 1, nvec,
338 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY,
343 table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
348 kfree(priv->irq_info);
352 static void mlx5_free_irq_vectors(struct mlx5_core_dev *dev)
354 struct mlx5_priv *priv = &dev->priv;
356 pci_free_irq_vectors(dev->pdev);
357 kfree(priv->irq_info);
360 struct mlx5_reg_host_endianness {
365 #define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos))
368 MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) |
369 MLX5_DEV_CAP_FLAG_DCT,
372 static u16 to_fw_pkey_sz(struct mlx5_core_dev *dev, u32 size)
388 mlx5_core_warn(dev, "invalid pkey table size %d\n", size);
393 static int mlx5_core_get_caps_mode(struct mlx5_core_dev *dev,
394 enum mlx5_cap_type cap_type,
395 enum mlx5_cap_mode cap_mode)
397 u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
398 int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
399 void *out, *hca_caps;
400 u16 opmod = (cap_type << 1) | (cap_mode & 0x01);
403 memset(in, 0, sizeof(in));
404 out = kzalloc(out_sz, GFP_KERNEL);
408 MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
409 MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
410 err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
413 "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n",
414 cap_type, cap_mode, err);
418 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, out, capability);
421 case HCA_CAP_OPMOD_GET_MAX:
422 memcpy(dev->caps.hca_max[cap_type], hca_caps,
423 MLX5_UN_SZ_BYTES(hca_cap_union));
425 case HCA_CAP_OPMOD_GET_CUR:
426 memcpy(dev->caps.hca_cur[cap_type], hca_caps,
427 MLX5_UN_SZ_BYTES(hca_cap_union));
431 "Tried to query dev cap type(%x) with wrong opmode(%x)\n",
441 int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type)
445 ret = mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_CUR);
448 return mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_MAX);
451 static int set_caps(struct mlx5_core_dev *dev, void *in, int in_sz, int opmod)
453 u32 out[MLX5_ST_SZ_DW(set_hca_cap_out)] = {0};
455 MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
456 MLX5_SET(set_hca_cap_in, in, op_mod, opmod << 1);
457 return mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
460 static int handle_hca_cap_atomic(struct mlx5_core_dev *dev)
464 int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
468 if (MLX5_CAP_GEN(dev, atomic)) {
469 err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC);
478 supported_atomic_req_8B_endianness_mode_1);
480 if (req_endianness != MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS)
483 set_ctx = kzalloc(set_sz, GFP_KERNEL);
487 set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
489 /* Set requestor to host endianness */
490 MLX5_SET(atomic_caps, set_hca_cap, atomic_req_8B_endianness_mode,
491 MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS);
493 err = set_caps(dev, set_ctx, set_sz, MLX5_SET_HCA_CAP_OP_MOD_ATOMIC);
499 static int handle_hca_cap(struct mlx5_core_dev *dev)
501 void *set_ctx = NULL;
502 struct mlx5_profile *prof = dev->profile;
504 int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
507 set_ctx = kzalloc(set_sz, GFP_KERNEL);
511 err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL);
515 set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
517 memcpy(set_hca_cap, dev->caps.hca_cur[MLX5_CAP_GENERAL],
518 MLX5_ST_SZ_BYTES(cmd_hca_cap));
520 mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
521 mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
523 /* we limit the size of the pkey table to 128 entries for now */
524 MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
525 to_fw_pkey_sz(dev, 128));
527 /* Check log_max_qp from HCA caps to set in current profile */
528 if (MLX5_CAP_GEN_MAX(dev, log_max_qp) < profile[prof_sel].log_max_qp) {
529 mlx5_core_warn(dev, "log_max_qp value in current profile is %d, changing it to HCA capability limit (%d)\n",
530 profile[prof_sel].log_max_qp,
531 MLX5_CAP_GEN_MAX(dev, log_max_qp));
532 profile[prof_sel].log_max_qp = MLX5_CAP_GEN_MAX(dev, log_max_qp);
534 if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
535 MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
538 /* disable cmdif checksum */
539 MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
541 /* Enable 4K UAR only when HCA supports it and page size is bigger
544 if (MLX5_CAP_GEN_MAX(dev, uar_4k) && PAGE_SIZE > 4096)
545 MLX5_SET(cmd_hca_cap, set_hca_cap, uar_4k, 1);
547 MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12);
549 if (MLX5_CAP_GEN_MAX(dev, cache_line_128byte))
550 MLX5_SET(cmd_hca_cap,
553 cache_line_size() == 128 ? 1 : 0);
555 err = set_caps(dev, set_ctx, set_sz,
556 MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE);
563 static int set_hca_ctrl(struct mlx5_core_dev *dev)
565 struct mlx5_reg_host_endianness he_in;
566 struct mlx5_reg_host_endianness he_out;
569 if (!mlx5_core_is_pf(dev))
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);
580 static int mlx5_core_set_hca_defaults(struct mlx5_core_dev *dev)
584 /* Disable local_lb by default */
585 if ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
586 MLX5_CAP_GEN(dev, disable_local_lb))
587 ret = mlx5_nic_vport_update_local_lb(dev, false);
592 int mlx5_core_enable_hca(struct mlx5_core_dev *dev, u16 func_id)
594 u32 out[MLX5_ST_SZ_DW(enable_hca_out)] = {0};
595 u32 in[MLX5_ST_SZ_DW(enable_hca_in)] = {0};
597 MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA);
598 MLX5_SET(enable_hca_in, in, function_id, func_id);
599 return mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
602 int mlx5_core_disable_hca(struct mlx5_core_dev *dev, u16 func_id)
604 u32 out[MLX5_ST_SZ_DW(disable_hca_out)] = {0};
605 u32 in[MLX5_ST_SZ_DW(disable_hca_in)] = {0};
607 MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA);
608 MLX5_SET(disable_hca_in, in, function_id, func_id);
609 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
612 u64 mlx5_read_internal_timer(struct mlx5_core_dev *dev)
614 u32 timer_h, timer_h1, timer_l;
616 timer_h = ioread32be(&dev->iseg->internal_timer_h);
617 timer_l = ioread32be(&dev->iseg->internal_timer_l);
618 timer_h1 = ioread32be(&dev->iseg->internal_timer_h);
619 if (timer_h != timer_h1) /* wrap around */
620 timer_l = ioread32be(&dev->iseg->internal_timer_l);
622 return (u64)timer_l | (u64)timer_h1 << 32;
625 int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn,
628 struct mlx5_eq_table *table = &dev->priv.eq_table;
629 struct mlx5_eq *eq, *n;
632 spin_lock(&table->lock);
633 list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
634 if (eq->index == vector) {
641 spin_unlock(&table->lock);
645 EXPORT_SYMBOL(mlx5_vector2eqn);
647 struct mlx5_eq *mlx5_eqn2eq(struct mlx5_core_dev *dev, int eqn)
649 struct mlx5_eq_table *table = &dev->priv.eq_table;
652 spin_lock(&table->lock);
653 list_for_each_entry(eq, &table->comp_eqs_list, list)
654 if (eq->eqn == eqn) {
655 spin_unlock(&table->lock);
659 spin_unlock(&table->lock);
661 return ERR_PTR(-ENOENT);
664 static void free_comp_eqs(struct mlx5_core_dev *dev)
666 struct mlx5_eq_table *table = &dev->priv.eq_table;
667 struct mlx5_eq *eq, *n;
669 #ifdef CONFIG_RFS_ACCEL
671 free_irq_cpu_rmap(dev->rmap);
675 spin_lock(&table->lock);
676 list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
678 spin_unlock(&table->lock);
679 if (mlx5_destroy_unmap_eq(dev, eq))
680 mlx5_core_warn(dev, "failed to destroy EQ 0x%x\n",
683 spin_lock(&table->lock);
685 spin_unlock(&table->lock);
688 static int alloc_comp_eqs(struct mlx5_core_dev *dev)
690 struct mlx5_eq_table *table = &dev->priv.eq_table;
691 char name[MLX5_MAX_IRQ_NAME];
698 INIT_LIST_HEAD(&table->comp_eqs_list);
699 ncomp_vec = table->num_comp_vectors;
700 nent = MLX5_COMP_EQ_SIZE;
701 #ifdef CONFIG_RFS_ACCEL
702 dev->rmap = alloc_irq_cpu_rmap(ncomp_vec);
706 for (i = 0; i < ncomp_vec; i++) {
707 eq = kzalloc(sizeof(*eq), GFP_KERNEL);
713 #ifdef CONFIG_RFS_ACCEL
714 irq_cpu_rmap_add(dev->rmap, pci_irq_vector(dev->pdev,
715 MLX5_EQ_VEC_COMP_BASE + i));
717 snprintf(name, MLX5_MAX_IRQ_NAME, "mlx5_comp%d", i);
718 err = mlx5_create_map_eq(dev, eq,
719 i + MLX5_EQ_VEC_COMP_BASE, nent, 0,
720 name, MLX5_EQ_TYPE_COMP);
725 mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->eqn);
727 spin_lock(&table->lock);
728 list_add_tail(&eq->list, &table->comp_eqs_list);
729 spin_unlock(&table->lock);
739 static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
741 u32 query_in[MLX5_ST_SZ_DW(query_issi_in)] = {0};
742 u32 query_out[MLX5_ST_SZ_DW(query_issi_out)] = {0};
746 MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
747 err = mlx5_cmd_exec(dev, query_in, sizeof(query_in),
748 query_out, sizeof(query_out));
753 mlx5_cmd_mbox_status(query_out, &status, &syndrome);
754 if (!status || syndrome == MLX5_DRIVER_SYND) {
755 mlx5_core_err(dev, "Failed to query ISSI err(%d) status(%d) synd(%d)\n",
756 err, status, syndrome);
760 mlx5_core_warn(dev, "Query ISSI is not supported by FW, ISSI is 0\n");
765 sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);
767 if (sup_issi & (1 << 1)) {
768 u32 set_in[MLX5_ST_SZ_DW(set_issi_in)] = {0};
769 u32 set_out[MLX5_ST_SZ_DW(set_issi_out)] = {0};
771 MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
772 MLX5_SET(set_issi_in, set_in, current_issi, 1);
773 err = mlx5_cmd_exec(dev, set_in, sizeof(set_in),
774 set_out, sizeof(set_out));
776 mlx5_core_err(dev, "Failed to set ISSI to 1 err(%d)\n",
784 } else if (sup_issi & (1 << 0) || !sup_issi) {
791 static int mlx5_pci_init(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
793 struct pci_dev *pdev = dev->pdev;
796 pci_set_drvdata(dev->pdev, dev);
797 strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
798 priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
800 mutex_init(&priv->pgdir_mutex);
801 INIT_LIST_HEAD(&priv->pgdir_list);
802 spin_lock_init(&priv->mkey_lock);
804 mutex_init(&priv->alloc_mutex);
806 priv->numa_node = dev_to_node(&dev->pdev->dev);
808 priv->dbg_root = debugfs_create_dir(dev_name(&pdev->dev), mlx5_debugfs_root);
812 err = mlx5_pci_enable_device(dev);
814 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
818 err = request_bar(pdev);
820 dev_err(&pdev->dev, "error requesting BARs, aborting\n");
824 pci_set_master(pdev);
826 err = set_dma_caps(pdev);
828 dev_err(&pdev->dev, "Failed setting DMA capabilities mask, aborting\n");
832 dev->iseg_base = pci_resource_start(dev->pdev, 0);
833 dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
836 dev_err(&pdev->dev, "Failed mapping initialization segment, aborting\n");
843 pci_clear_master(dev->pdev);
844 release_bar(dev->pdev);
846 mlx5_pci_disable_device(dev);
849 debugfs_remove(priv->dbg_root);
853 static void mlx5_pci_close(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
856 pci_clear_master(dev->pdev);
857 release_bar(dev->pdev);
858 mlx5_pci_disable_device(dev);
859 debugfs_remove(priv->dbg_root);
862 static int mlx5_init_once(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
864 struct pci_dev *pdev = dev->pdev;
867 err = mlx5_query_board_id(dev);
869 dev_err(&pdev->dev, "query board id failed\n");
873 err = mlx5_eq_init(dev);
875 dev_err(&pdev->dev, "failed to initialize eq\n");
879 err = mlx5_init_cq_table(dev);
881 dev_err(&pdev->dev, "failed to initialize cq table\n");
885 mlx5_init_qp_table(dev);
887 mlx5_init_srq_table(dev);
889 mlx5_init_mkey_table(dev);
891 mlx5_init_reserved_gids(dev);
893 mlx5_init_clock(dev);
895 err = mlx5_init_rl_table(dev);
897 dev_err(&pdev->dev, "Failed to init rate limiting\n");
898 goto err_tables_cleanup;
901 err = mlx5_mpfs_init(dev);
903 dev_err(&pdev->dev, "Failed to init l2 table %d\n", err);
907 err = mlx5_eswitch_init(dev);
909 dev_err(&pdev->dev, "Failed to init eswitch %d\n", err);
910 goto err_mpfs_cleanup;
913 err = mlx5_sriov_init(dev);
915 dev_err(&pdev->dev, "Failed to init sriov %d\n", err);
916 goto err_eswitch_cleanup;
919 err = mlx5_fpga_init(dev);
921 dev_err(&pdev->dev, "Failed to init fpga device %d\n", err);
922 goto err_sriov_cleanup;
928 mlx5_sriov_cleanup(dev);
930 mlx5_eswitch_cleanup(dev->priv.eswitch);
932 mlx5_mpfs_cleanup(dev);
934 mlx5_cleanup_rl_table(dev);
936 mlx5_cleanup_mkey_table(dev);
937 mlx5_cleanup_srq_table(dev);
938 mlx5_cleanup_qp_table(dev);
939 mlx5_cleanup_cq_table(dev);
942 mlx5_eq_cleanup(dev);
948 static void mlx5_cleanup_once(struct mlx5_core_dev *dev)
950 mlx5_fpga_cleanup(dev);
951 mlx5_sriov_cleanup(dev);
952 mlx5_eswitch_cleanup(dev->priv.eswitch);
953 mlx5_mpfs_cleanup(dev);
954 mlx5_cleanup_rl_table(dev);
955 mlx5_cleanup_clock(dev);
956 mlx5_cleanup_reserved_gids(dev);
957 mlx5_cleanup_mkey_table(dev);
958 mlx5_cleanup_srq_table(dev);
959 mlx5_cleanup_qp_table(dev);
960 mlx5_cleanup_cq_table(dev);
961 mlx5_eq_cleanup(dev);
964 static int mlx5_load_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
967 struct pci_dev *pdev = dev->pdev;
970 mutex_lock(&dev->intf_state_mutex);
971 if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
972 dev_warn(&dev->pdev->dev, "%s: interface is up, NOP\n",
977 dev_info(&pdev->dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
978 fw_rev_min(dev), fw_rev_sub(dev));
980 /* on load removing any previous indication of internal error, device is
983 dev->state = MLX5_DEVICE_STATE_UP;
985 /* wait for firmware to accept initialization segments configurations
987 err = wait_fw_init(dev, FW_PRE_INIT_TIMEOUT_MILI);
989 dev_err(&dev->pdev->dev, "Firmware over %d MS in pre-initializing state, aborting\n",
990 FW_PRE_INIT_TIMEOUT_MILI);
994 err = mlx5_cmd_init(dev);
996 dev_err(&pdev->dev, "Failed initializing command interface, aborting\n");
1000 err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI);
1002 dev_err(&dev->pdev->dev, "Firmware over %d MS in initializing state, aborting\n",
1003 FW_INIT_TIMEOUT_MILI);
1004 goto err_cmd_cleanup;
1007 err = mlx5_core_enable_hca(dev, 0);
1009 dev_err(&pdev->dev, "enable hca failed\n");
1010 goto err_cmd_cleanup;
1013 err = mlx5_core_set_issi(dev);
1015 dev_err(&pdev->dev, "failed to set issi\n");
1016 goto err_disable_hca;
1019 err = mlx5_satisfy_startup_pages(dev, 1);
1021 dev_err(&pdev->dev, "failed to allocate boot pages\n");
1022 goto err_disable_hca;
1025 err = set_hca_ctrl(dev);
1027 dev_err(&pdev->dev, "set_hca_ctrl failed\n");
1028 goto reclaim_boot_pages;
1031 err = handle_hca_cap(dev);
1033 dev_err(&pdev->dev, "handle_hca_cap failed\n");
1034 goto reclaim_boot_pages;
1037 err = handle_hca_cap_atomic(dev);
1039 dev_err(&pdev->dev, "handle_hca_cap_atomic failed\n");
1040 goto reclaim_boot_pages;
1043 err = mlx5_satisfy_startup_pages(dev, 0);
1045 dev_err(&pdev->dev, "failed to allocate init pages\n");
1046 goto reclaim_boot_pages;
1049 err = mlx5_pagealloc_start(dev);
1051 dev_err(&pdev->dev, "mlx5_pagealloc_start failed\n");
1052 goto reclaim_boot_pages;
1055 err = mlx5_cmd_init_hca(dev);
1057 dev_err(&pdev->dev, "init hca failed\n");
1058 goto err_pagealloc_stop;
1061 mlx5_set_driver_version(dev);
1063 mlx5_start_health_poll(dev);
1065 err = mlx5_query_hca_caps(dev);
1067 dev_err(&pdev->dev, "query hca failed\n");
1071 if (boot && mlx5_init_once(dev, priv)) {
1072 dev_err(&pdev->dev, "sw objs init failed\n");
1076 err = mlx5_alloc_irq_vectors(dev);
1078 dev_err(&pdev->dev, "alloc irq vectors failed\n");
1079 goto err_cleanup_once;
1082 dev->priv.uar = mlx5_get_uars_page(dev);
1083 if (!dev->priv.uar) {
1084 dev_err(&pdev->dev, "Failed allocating uar, aborting\n");
1085 goto err_disable_msix;
1088 err = mlx5_start_eqs(dev);
1090 dev_err(&pdev->dev, "Failed to start pages and async EQs\n");
1094 err = alloc_comp_eqs(dev);
1096 dev_err(&pdev->dev, "Failed to alloc completion EQs\n");
1100 err = mlx5_init_fs(dev);
1102 dev_err(&pdev->dev, "Failed to init flow steering\n");
1106 err = mlx5_core_set_hca_defaults(dev);
1108 dev_err(&pdev->dev, "Failed to set hca defaults\n");
1112 err = mlx5_sriov_attach(dev);
1114 dev_err(&pdev->dev, "sriov init failed %d\n", err);
1118 err = mlx5_fpga_device_start(dev);
1120 dev_err(&pdev->dev, "fpga device start failed %d\n", err);
1121 goto err_fpga_start;
1123 err = mlx5_accel_ipsec_init(dev);
1125 dev_err(&pdev->dev, "IPSec device start failed %d\n", err);
1126 goto err_ipsec_start;
1129 if (mlx5_device_registered(dev)) {
1130 mlx5_attach_device(dev);
1132 err = mlx5_register_device(dev);
1134 dev_err(&pdev->dev, "mlx5_register_device failed %d\n", err);
1139 set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1141 mutex_unlock(&dev->intf_state_mutex);
1146 mlx5_accel_ipsec_cleanup(dev);
1148 mlx5_fpga_device_stop(dev);
1151 mlx5_sriov_detach(dev);
1154 mlx5_cleanup_fs(dev);
1163 mlx5_put_uars_page(dev, priv->uar);
1166 mlx5_free_irq_vectors(dev);
1170 mlx5_cleanup_once(dev);
1173 mlx5_stop_health_poll(dev);
1174 if (mlx5_cmd_teardown_hca(dev)) {
1175 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1180 mlx5_pagealloc_stop(dev);
1183 mlx5_reclaim_startup_pages(dev);
1186 mlx5_core_disable_hca(dev, 0);
1189 mlx5_cmd_cleanup(dev);
1192 dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
1193 mutex_unlock(&dev->intf_state_mutex);
1198 static int mlx5_unload_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
1204 mlx5_drain_health_recovery(dev);
1206 mutex_lock(&dev->intf_state_mutex);
1207 if (!test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1208 dev_warn(&dev->pdev->dev, "%s: interface is down, NOP\n",
1211 mlx5_cleanup_once(dev);
1215 clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1217 if (mlx5_device_registered(dev))
1218 mlx5_detach_device(dev);
1220 mlx5_accel_ipsec_cleanup(dev);
1221 mlx5_fpga_device_stop(dev);
1223 mlx5_sriov_detach(dev);
1224 mlx5_cleanup_fs(dev);
1227 mlx5_put_uars_page(dev, priv->uar);
1228 mlx5_free_irq_vectors(dev);
1230 mlx5_cleanup_once(dev);
1231 mlx5_stop_health_poll(dev);
1232 err = mlx5_cmd_teardown_hca(dev);
1234 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1237 mlx5_pagealloc_stop(dev);
1238 mlx5_reclaim_startup_pages(dev);
1239 mlx5_core_disable_hca(dev, 0);
1240 mlx5_cmd_cleanup(dev);
1243 mutex_unlock(&dev->intf_state_mutex);
1247 struct mlx5_core_event_handler {
1248 void (*event)(struct mlx5_core_dev *dev,
1249 enum mlx5_dev_event event,
1253 static const struct devlink_ops mlx5_devlink_ops = {
1254 #ifdef CONFIG_MLX5_ESWITCH
1255 .eswitch_mode_set = mlx5_devlink_eswitch_mode_set,
1256 .eswitch_mode_get = mlx5_devlink_eswitch_mode_get,
1257 .eswitch_inline_mode_set = mlx5_devlink_eswitch_inline_mode_set,
1258 .eswitch_inline_mode_get = mlx5_devlink_eswitch_inline_mode_get,
1259 .eswitch_encap_mode_set = mlx5_devlink_eswitch_encap_mode_set,
1260 .eswitch_encap_mode_get = mlx5_devlink_eswitch_encap_mode_get,
1264 #define MLX5_IB_MOD "mlx5_ib"
1265 static int init_one(struct pci_dev *pdev,
1266 const struct pci_device_id *id)
1268 struct mlx5_core_dev *dev;
1269 struct devlink *devlink;
1270 struct mlx5_priv *priv;
1273 devlink = devlink_alloc(&mlx5_devlink_ops, sizeof(*dev));
1275 dev_err(&pdev->dev, "kzalloc failed\n");
1279 dev = devlink_priv(devlink);
1281 priv->pci_dev_data = id->driver_data;
1283 pci_set_drvdata(pdev, dev);
1286 dev->event = mlx5_core_event;
1287 dev->profile = &profile[prof_sel];
1289 INIT_LIST_HEAD(&priv->ctx_list);
1290 spin_lock_init(&priv->ctx_lock);
1291 mutex_init(&dev->pci_status_mutex);
1292 mutex_init(&dev->intf_state_mutex);
1294 INIT_LIST_HEAD(&priv->waiting_events_list);
1295 priv->is_accum_events = false;
1297 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1298 err = init_srcu_struct(&priv->pfault_srcu);
1300 dev_err(&pdev->dev, "init_srcu_struct failed with error code %d\n",
1305 mutex_init(&priv->bfregs.reg_head.lock);
1306 mutex_init(&priv->bfregs.wc_head.lock);
1307 INIT_LIST_HEAD(&priv->bfregs.reg_head.list);
1308 INIT_LIST_HEAD(&priv->bfregs.wc_head.list);
1310 err = mlx5_pci_init(dev, priv);
1312 dev_err(&pdev->dev, "mlx5_pci_init failed with error code %d\n", err);
1316 err = mlx5_health_init(dev);
1318 dev_err(&pdev->dev, "mlx5_health_init failed with error code %d\n", err);
1322 mlx5_pagealloc_init(dev);
1324 err = mlx5_load_one(dev, priv, true);
1326 dev_err(&pdev->dev, "mlx5_load_one failed with error code %d\n", err);
1330 request_module_nowait(MLX5_IB_MOD);
1332 err = devlink_register(devlink, &pdev->dev);
1336 pci_save_state(pdev);
1340 mlx5_unload_one(dev, priv, true);
1342 mlx5_pagealloc_cleanup(dev);
1343 mlx5_health_cleanup(dev);
1345 mlx5_pci_close(dev, priv);
1347 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1348 cleanup_srcu_struct(&priv->pfault_srcu);
1351 devlink_free(devlink);
1356 static void remove_one(struct pci_dev *pdev)
1358 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1359 struct devlink *devlink = priv_to_devlink(dev);
1360 struct mlx5_priv *priv = &dev->priv;
1362 devlink_unregister(devlink);
1363 mlx5_unregister_device(dev);
1365 if (mlx5_unload_one(dev, priv, true)) {
1366 dev_err(&dev->pdev->dev, "mlx5_unload_one failed\n");
1367 mlx5_health_cleanup(dev);
1371 mlx5_pagealloc_cleanup(dev);
1372 mlx5_health_cleanup(dev);
1373 mlx5_pci_close(dev, priv);
1374 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1375 cleanup_srcu_struct(&priv->pfault_srcu);
1377 devlink_free(devlink);
1380 static pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev,
1381 pci_channel_state_t state)
1383 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1384 struct mlx5_priv *priv = &dev->priv;
1386 dev_info(&pdev->dev, "%s was called\n", __func__);
1388 mlx5_enter_error_state(dev, false);
1389 mlx5_unload_one(dev, priv, false);
1390 /* In case of kernel call drain the health wq */
1392 mlx5_drain_health_wq(dev);
1393 mlx5_pci_disable_device(dev);
1396 return state == pci_channel_io_perm_failure ?
1397 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1400 /* wait for the device to show vital signs by waiting
1401 * for the health counter to start counting.
1403 static int wait_vital(struct pci_dev *pdev)
1405 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1406 struct mlx5_core_health *health = &dev->priv.health;
1407 const int niter = 100;
1412 for (i = 0; i < niter; i++) {
1413 count = ioread32be(health->health_counter);
1414 if (count && count != 0xffffffff) {
1415 if (last_count && last_count != count) {
1416 dev_info(&pdev->dev, "Counter value 0x%x after %d iterations\n", count, i);
1427 static pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev)
1429 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1432 dev_info(&pdev->dev, "%s was called\n", __func__);
1434 err = mlx5_pci_enable_device(dev);
1436 dev_err(&pdev->dev, "%s: mlx5_pci_enable_device failed with error code: %d\n"
1438 return PCI_ERS_RESULT_DISCONNECT;
1441 pci_set_master(pdev);
1442 pci_restore_state(pdev);
1443 pci_save_state(pdev);
1445 if (wait_vital(pdev)) {
1446 dev_err(&pdev->dev, "%s: wait_vital timed out\n", __func__);
1447 return PCI_ERS_RESULT_DISCONNECT;
1450 return PCI_ERS_RESULT_RECOVERED;
1453 static void mlx5_pci_resume(struct pci_dev *pdev)
1455 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1456 struct mlx5_priv *priv = &dev->priv;
1459 dev_info(&pdev->dev, "%s was called\n", __func__);
1461 err = mlx5_load_one(dev, priv, false);
1463 dev_err(&pdev->dev, "%s: mlx5_load_one failed with error code: %d\n"
1466 dev_info(&pdev->dev, "%s: device recovered\n", __func__);
1469 static const struct pci_error_handlers mlx5_err_handler = {
1470 .error_detected = mlx5_pci_err_detected,
1471 .slot_reset = mlx5_pci_slot_reset,
1472 .resume = mlx5_pci_resume
1475 static int mlx5_try_fast_unload(struct mlx5_core_dev *dev)
1479 if (!MLX5_CAP_GEN(dev, force_teardown)) {
1480 mlx5_core_dbg(dev, "force teardown is not supported in the firmware\n");
1484 if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
1485 mlx5_core_dbg(dev, "Device in internal error state, giving up\n");
1489 /* Panic tear down fw command will stop the PCI bus communication
1490 * with the HCA, so the health polll is no longer needed.
1492 mlx5_drain_health_wq(dev);
1493 mlx5_stop_health_poll(dev);
1495 ret = mlx5_cmd_force_teardown_hca(dev);
1497 mlx5_core_dbg(dev, "Firmware couldn't do fast unload error: %d\n", ret);
1498 mlx5_start_health_poll(dev);
1502 mlx5_enter_error_state(dev, true);
1507 static void shutdown(struct pci_dev *pdev)
1509 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1510 struct mlx5_priv *priv = &dev->priv;
1513 dev_info(&pdev->dev, "Shutdown was called\n");
1514 err = mlx5_try_fast_unload(dev);
1516 mlx5_unload_one(dev, priv, false);
1517 mlx5_pci_disable_device(dev);
1520 static const struct pci_device_id mlx5_core_pci_table[] = {
1521 { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTIB) },
1522 { PCI_VDEVICE(MELLANOX, 0x1012), MLX5_PCI_DEV_IS_VF}, /* Connect-IB VF */
1523 { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4) },
1524 { PCI_VDEVICE(MELLANOX, 0x1014), MLX5_PCI_DEV_IS_VF}, /* ConnectX-4 VF */
1525 { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4_LX) },
1526 { PCI_VDEVICE(MELLANOX, 0x1016), MLX5_PCI_DEV_IS_VF}, /* ConnectX-4LX VF */
1527 { PCI_VDEVICE(MELLANOX, 0x1017) }, /* ConnectX-5, PCIe 3.0 */
1528 { PCI_VDEVICE(MELLANOX, 0x1018), MLX5_PCI_DEV_IS_VF}, /* ConnectX-5 VF */
1529 { PCI_VDEVICE(MELLANOX, 0x1019) }, /* ConnectX-5 Ex */
1530 { PCI_VDEVICE(MELLANOX, 0x101a), MLX5_PCI_DEV_IS_VF}, /* ConnectX-5 Ex VF */
1531 { PCI_VDEVICE(MELLANOX, 0x101b) }, /* ConnectX-6 */
1532 { PCI_VDEVICE(MELLANOX, 0x101c), MLX5_PCI_DEV_IS_VF}, /* ConnectX-6 VF */
1533 { PCI_VDEVICE(MELLANOX, 0xa2d2) }, /* BlueField integrated ConnectX-5 network controller */
1534 { PCI_VDEVICE(MELLANOX, 0xa2d3), MLX5_PCI_DEV_IS_VF}, /* BlueField integrated ConnectX-5 network controller VF */
1538 MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
1540 void mlx5_disable_device(struct mlx5_core_dev *dev)
1542 mlx5_pci_err_detected(dev->pdev, 0);
1545 void mlx5_recover_device(struct mlx5_core_dev *dev)
1547 mlx5_pci_disable_device(dev);
1548 if (mlx5_pci_slot_reset(dev->pdev) == PCI_ERS_RESULT_RECOVERED)
1549 mlx5_pci_resume(dev->pdev);
1552 static struct pci_driver mlx5_core_driver = {
1553 .name = DRIVER_NAME,
1554 .id_table = mlx5_core_pci_table,
1556 .remove = remove_one,
1557 .shutdown = shutdown,
1558 .err_handler = &mlx5_err_handler,
1559 .sriov_configure = mlx5_core_sriov_configure,
1562 static void mlx5_core_verify_params(void)
1564 if (prof_sel >= ARRAY_SIZE(profile)) {
1565 pr_warn("mlx5_core: WARNING: Invalid module parameter prof_sel %d, valid range 0-%zu, changing back to default(%d)\n",
1567 ARRAY_SIZE(profile) - 1,
1569 prof_sel = MLX5_DEFAULT_PROF;
1573 static int __init init(void)
1577 mlx5_core_verify_params();
1578 mlx5_register_debugfs();
1580 err = pci_register_driver(&mlx5_core_driver);
1584 #ifdef CONFIG_MLX5_CORE_EN
1591 mlx5_unregister_debugfs();
1595 static void __exit cleanup(void)
1597 #ifdef CONFIG_MLX5_CORE_EN
1600 pci_unregister_driver(&mlx5_core_driver);
1601 mlx5_unregister_debugfs();
1605 module_exit(cleanup);