mlx5_main.c revision 347847
1290650Shselasky/*- 2347819Shselasky * Copyright (c) 2013-2019, Mellanox Technologies, Ltd. All rights reserved. 3290650Shselasky * 4290650Shselasky * Redistribution and use in source and binary forms, with or without 5290650Shselasky * modification, are permitted provided that the following conditions 6290650Shselasky * are met: 7290650Shselasky * 1. Redistributions of source code must retain the above copyright 8290650Shselasky * notice, this list of conditions and the following disclaimer. 9290650Shselasky * 2. Redistributions in binary form must reproduce the above copyright 10290650Shselasky * notice, this list of conditions and the following disclaimer in the 11290650Shselasky * documentation and/or other materials provided with the distribution. 12290650Shselasky * 13290650Shselasky * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND 14290650Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15290650Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16290650Shselasky * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 17290650Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18290650Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19290650Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20290650Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21290650Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22290650Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23290650Shselasky * SUCH DAMAGE. 24290650Shselasky * 25290650Shselasky * $FreeBSD: stable/11/sys/dev/mlx5/mlx5_core/mlx5_main.c 347847 2019-05-16 17:59:43Z hselasky $ 26290650Shselasky */ 27290650Shselasky 28290650Shselasky#include <linux/kmod.h> 29290650Shselasky#include <linux/module.h> 30290650Shselasky#include <linux/errno.h> 31290650Shselasky#include <linux/pci.h> 32290650Shselasky#include <linux/dma-mapping.h> 33290650Shselasky#include <linux/slab.h> 34290650Shselasky#include <linux/io-mapping.h> 35290650Shselasky#include <linux/interrupt.h> 36347802Shselasky#include <linux/hardirq.h> 37290650Shselasky#include <dev/mlx5/driver.h> 38290650Shselasky#include <dev/mlx5/cq.h> 39290650Shselasky#include <dev/mlx5/qp.h> 40290650Shselasky#include <dev/mlx5/srq.h> 41290650Shselasky#include <linux/delay.h> 42290650Shselasky#include <dev/mlx5/mlx5_ifc.h> 43341958Shselasky#include <dev/mlx5/mlx5_fpga/core.h> 44341958Shselasky#include <dev/mlx5/mlx5_lib/mlx5.h> 45290650Shselasky#include "mlx5_core.h" 46329200Shselasky#include "fs_core.h" 47290650Shselasky 48341948Shselaskystatic const char mlx5_version[] = "Mellanox Core driver " 49341948Shselasky DRIVER_VERSION " (" DRIVER_RELDATE ")"; 50290650ShselaskyMODULE_AUTHOR("Eli Cohen <eli@mellanox.com>"); 51290650ShselaskyMODULE_DESCRIPTION("Mellanox Connect-IB, ConnectX-4 core driver"); 52290650ShselaskyMODULE_LICENSE("Dual BSD/GPL"); 53290650ShselaskyMODULE_DEPEND(mlx5, linuxkpi, 1, 1, 1); 54347839ShselaskyMODULE_DEPEND(mlx5, mlxfw, 1, 1, 1); 55347847ShselaskyMODULE_DEPEND(mlx5, firmware, 1, 1, 1); 56290650ShselaskyMODULE_VERSION(mlx5, 1); 57290650Shselasky 58347835ShselaskySYSCTL_NODE(_hw, OID_AUTO, mlx5, CTLFLAG_RW, 0, "mlx5 hardware controls"); 59347835Shselasky 60290650Shselaskyint mlx5_core_debug_mask; 61347835ShselaskySYSCTL_INT(_hw_mlx5, OID_AUTO, debug_mask, CTLFLAG_RWTUN, 62347835Shselasky &mlx5_core_debug_mask, 0, 63347835Shselasky "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0"); 64290650Shselasky 65290650Shselasky#define MLX5_DEFAULT_PROF 2 66347835Shselaskystatic int mlx5_prof_sel = MLX5_DEFAULT_PROF; 67347835ShselaskySYSCTL_INT(_hw_mlx5, OID_AUTO, prof_sel, CTLFLAG_RWTUN, 68347835Shselasky &mlx5_prof_sel, 0, 69347835Shselasky "profile selector. Valid range 0 - 2"); 70290650Shselasky 71347819Shselaskystatic int mlx5_fast_unload_enabled = 1; 72347819ShselaskySYSCTL_INT(_hw_mlx5, OID_AUTO, fast_unload_enabled, CTLFLAG_RWTUN, 73347819Shselasky &mlx5_fast_unload_enabled, 0, 74347819Shselasky "Set to enable fast unload. Clear to disable."); 75347819Shselasky 76290650Shselasky#define NUMA_NO_NODE -1 77290650Shselasky 78290650Shselaskystatic LIST_HEAD(intf_list); 79290650Shselaskystatic LIST_HEAD(dev_list); 80290650Shselaskystatic DEFINE_MUTEX(intf_mutex); 81290650Shselasky 82290650Shselaskystruct mlx5_device_context { 83290650Shselasky struct list_head list; 84290650Shselasky struct mlx5_interface *intf; 85290650Shselasky void *context; 86290650Shselasky}; 87290650Shselasky 88329209Shselaskyenum { 89329209Shselasky MLX5_ATOMIC_REQ_MODE_BE = 0x0, 90329209Shselasky MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS = 0x1, 91329209Shselasky}; 92329209Shselasky 93290650Shselaskystatic struct mlx5_profile profiles[] = { 94290650Shselasky [0] = { 95290650Shselasky .mask = 0, 96290650Shselasky }, 97290650Shselasky [1] = { 98290650Shselasky .mask = MLX5_PROF_MASK_QP_SIZE, 99290650Shselasky .log_max_qp = 12, 100290650Shselasky }, 101290650Shselasky [2] = { 102290650Shselasky .mask = MLX5_PROF_MASK_QP_SIZE | 103290650Shselasky MLX5_PROF_MASK_MR_CACHE, 104290650Shselasky .log_max_qp = 17, 105290650Shselasky .mr_cache[0] = { 106290650Shselasky .size = 500, 107290650Shselasky .limit = 250 108290650Shselasky }, 109290650Shselasky .mr_cache[1] = { 110290650Shselasky .size = 500, 111290650Shselasky .limit = 250 112290650Shselasky }, 113290650Shselasky .mr_cache[2] = { 114290650Shselasky .size = 500, 115290650Shselasky .limit = 250 116290650Shselasky }, 117290650Shselasky .mr_cache[3] = { 118290650Shselasky .size = 500, 119290650Shselasky .limit = 250 120290650Shselasky }, 121290650Shselasky .mr_cache[4] = { 122290650Shselasky .size = 500, 123290650Shselasky .limit = 250 124290650Shselasky }, 125290650Shselasky .mr_cache[5] = { 126290650Shselasky .size = 500, 127290650Shselasky .limit = 250 128290650Shselasky }, 129290650Shselasky .mr_cache[6] = { 130290650Shselasky .size = 500, 131290650Shselasky .limit = 250 132290650Shselasky }, 133290650Shselasky .mr_cache[7] = { 134290650Shselasky .size = 500, 135290650Shselasky .limit = 250 136290650Shselasky }, 137290650Shselasky .mr_cache[8] = { 138290650Shselasky .size = 500, 139290650Shselasky .limit = 250 140290650Shselasky }, 141290650Shselasky .mr_cache[9] = { 142290650Shselasky .size = 500, 143290650Shselasky .limit = 250 144290650Shselasky }, 145290650Shselasky .mr_cache[10] = { 146290650Shselasky .size = 500, 147290650Shselasky .limit = 250 148290650Shselasky }, 149290650Shselasky .mr_cache[11] = { 150290650Shselasky .size = 500, 151290650Shselasky .limit = 250 152290650Shselasky }, 153290650Shselasky .mr_cache[12] = { 154290650Shselasky .size = 64, 155290650Shselasky .limit = 32 156290650Shselasky }, 157290650Shselasky .mr_cache[13] = { 158290650Shselasky .size = 32, 159290650Shselasky .limit = 16 160290650Shselasky }, 161290650Shselasky .mr_cache[14] = { 162290650Shselasky .size = 16, 163290650Shselasky .limit = 8 164290650Shselasky }, 165290650Shselasky }, 166290650Shselasky [3] = { 167290650Shselasky .mask = MLX5_PROF_MASK_QP_SIZE, 168290650Shselasky .log_max_qp = 17, 169290650Shselasky }, 170290650Shselasky}; 171290650Shselasky 172290650Shselaskystatic int set_dma_caps(struct pci_dev *pdev) 173290650Shselasky{ 174290650Shselasky int err; 175290650Shselasky 176290650Shselasky err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 177290650Shselasky if (err) { 178290650Shselasky device_printf((&pdev->dev)->bsddev, "WARN: ""Warning: couldn't set 64-bit PCI DMA mask\n"); 179290650Shselasky err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 180290650Shselasky if (err) { 181290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""Can't set PCI DMA mask, aborting\n"); 182290650Shselasky return err; 183290650Shselasky } 184290650Shselasky } 185290650Shselasky 186290650Shselasky err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 187290650Shselasky if (err) { 188290650Shselasky device_printf((&pdev->dev)->bsddev, "WARN: ""Warning: couldn't set 64-bit consistent PCI DMA mask\n"); 189290650Shselasky err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 190290650Shselasky if (err) { 191290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""Can't set consistent PCI DMA mask, aborting\n"); 192290650Shselasky return err; 193290650Shselasky } 194290650Shselasky } 195290650Shselasky 196290650Shselasky dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024); 197290650Shselasky return err; 198290650Shselasky} 199290650Shselasky 200331580Shselaskystatic int mlx5_pci_enable_device(struct mlx5_core_dev *dev) 201331580Shselasky{ 202331580Shselasky struct pci_dev *pdev = dev->pdev; 203331580Shselasky int err = 0; 204331580Shselasky 205331580Shselasky mutex_lock(&dev->pci_status_mutex); 206331580Shselasky if (dev->pci_status == MLX5_PCI_STATUS_DISABLED) { 207331580Shselasky err = pci_enable_device(pdev); 208331580Shselasky if (!err) 209331580Shselasky dev->pci_status = MLX5_PCI_STATUS_ENABLED; 210331580Shselasky } 211331580Shselasky mutex_unlock(&dev->pci_status_mutex); 212331580Shselasky 213331580Shselasky return err; 214331580Shselasky} 215331580Shselasky 216331580Shselaskystatic void mlx5_pci_disable_device(struct mlx5_core_dev *dev) 217331580Shselasky{ 218331580Shselasky struct pci_dev *pdev = dev->pdev; 219331580Shselasky 220331580Shselasky mutex_lock(&dev->pci_status_mutex); 221331580Shselasky if (dev->pci_status == MLX5_PCI_STATUS_ENABLED) { 222331580Shselasky pci_disable_device(pdev); 223331580Shselasky dev->pci_status = MLX5_PCI_STATUS_DISABLED; 224331580Shselasky } 225331580Shselasky mutex_unlock(&dev->pci_status_mutex); 226331580Shselasky} 227331580Shselasky 228290650Shselaskystatic int request_bar(struct pci_dev *pdev) 229290650Shselasky{ 230290650Shselasky int err = 0; 231290650Shselasky 232290650Shselasky if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 233290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""Missing registers BAR, aborting\n"); 234290650Shselasky return -ENODEV; 235290650Shselasky } 236290650Shselasky 237290650Shselasky err = pci_request_regions(pdev, DRIVER_NAME); 238290650Shselasky if (err) 239290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""Couldn't get PCI resources, aborting\n"); 240290650Shselasky 241290650Shselasky return err; 242290650Shselasky} 243290650Shselasky 244290650Shselaskystatic void release_bar(struct pci_dev *pdev) 245290650Shselasky{ 246290650Shselasky pci_release_regions(pdev); 247290650Shselasky} 248290650Shselasky 249290650Shselaskystatic int mlx5_enable_msix(struct mlx5_core_dev *dev) 250290650Shselasky{ 251290650Shselasky struct mlx5_priv *priv = &dev->priv; 252290650Shselasky struct mlx5_eq_table *table = &priv->eq_table; 253290650Shselasky int num_eqs = 1 << MLX5_CAP_GEN(dev, log_max_eq); 254338554Shselasky int limit = dev->msix_eqvec; 255337112Shselasky int nvec = MLX5_EQ_VEC_COMP_BASE; 256290650Shselasky int i; 257290650Shselasky 258337112Shselasky if (limit > 0) 259337112Shselasky nvec += limit; 260337112Shselasky else 261337112Shselasky nvec += MLX5_CAP_GEN(dev, num_ports) * num_online_cpus(); 262337112Shselasky 263290650Shselasky nvec = min_t(int, nvec, num_eqs); 264290650Shselasky if (nvec <= MLX5_EQ_VEC_COMP_BASE) 265290650Shselasky return -ENOMEM; 266290650Shselasky 267290650Shselasky priv->msix_arr = kzalloc(nvec * sizeof(*priv->msix_arr), GFP_KERNEL); 268290650Shselasky 269290650Shselasky priv->irq_info = kzalloc(nvec * sizeof(*priv->irq_info), GFP_KERNEL); 270290650Shselasky 271290650Shselasky for (i = 0; i < nvec; i++) 272290650Shselasky priv->msix_arr[i].entry = i; 273290650Shselasky 274290650Shselasky nvec = pci_enable_msix_range(dev->pdev, priv->msix_arr, 275290650Shselasky MLX5_EQ_VEC_COMP_BASE + 1, nvec); 276290650Shselasky if (nvec < 0) 277290650Shselasky return nvec; 278290650Shselasky 279290650Shselasky table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE; 280290650Shselasky 281290650Shselasky return 0; 282290650Shselasky 283290650Shselasky} 284290650Shselasky 285290650Shselaskystatic void mlx5_disable_msix(struct mlx5_core_dev *dev) 286290650Shselasky{ 287290650Shselasky struct mlx5_priv *priv = &dev->priv; 288290650Shselasky 289290650Shselasky pci_disable_msix(dev->pdev); 290290650Shselasky kfree(priv->irq_info); 291290650Shselasky kfree(priv->msix_arr); 292290650Shselasky} 293290650Shselasky 294290650Shselaskystruct mlx5_reg_host_endianess { 295290650Shselasky u8 he; 296290650Shselasky u8 rsvd[15]; 297290650Shselasky}; 298290650Shselasky 299290650Shselasky 300290650Shselasky#define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos)) 301290650Shselasky 302290650Shselaskyenum { 303290650Shselasky MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) | 304306233Shselasky MLX5_DEV_CAP_FLAG_DCT | 305306233Shselasky MLX5_DEV_CAP_FLAG_DRAIN_SIGERR, 306290650Shselasky}; 307290650Shselasky 308290650Shselaskystatic u16 to_fw_pkey_sz(u32 size) 309290650Shselasky{ 310290650Shselasky switch (size) { 311290650Shselasky case 128: 312290650Shselasky return 0; 313290650Shselasky case 256: 314290650Shselasky return 1; 315290650Shselasky case 512: 316290650Shselasky return 2; 317290650Shselasky case 1024: 318290650Shselasky return 3; 319290650Shselasky case 2048: 320290650Shselasky return 4; 321290650Shselasky case 4096: 322290650Shselasky return 5; 323290650Shselasky default: 324290650Shselasky printf("mlx5_core: WARN: ""invalid pkey table size %d\n", size); 325290650Shselasky return 0; 326290650Shselasky } 327290650Shselasky} 328290650Shselasky 329331807Shselaskystatic int mlx5_core_get_caps_mode(struct mlx5_core_dev *dev, 330331807Shselasky enum mlx5_cap_type cap_type, 331331807Shselasky enum mlx5_cap_mode cap_mode) 332290650Shselasky{ 333290650Shselasky u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)]; 334290650Shselasky int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out); 335290650Shselasky void *out, *hca_caps; 336290650Shselasky u16 opmod = (cap_type << 1) | (cap_mode & 0x01); 337290650Shselasky int err; 338290650Shselasky 339290650Shselasky memset(in, 0, sizeof(in)); 340290650Shselasky out = kzalloc(out_sz, GFP_KERNEL); 341290650Shselasky 342290650Shselasky MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP); 343290650Shselasky MLX5_SET(query_hca_cap_in, in, op_mod, opmod); 344290650Shselasky err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz); 345290650Shselasky if (err) { 346290650Shselasky mlx5_core_warn(dev, 347290650Shselasky "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n", 348290650Shselasky cap_type, cap_mode, err); 349290650Shselasky goto query_ex; 350290650Shselasky } 351290650Shselasky 352290650Shselasky hca_caps = MLX5_ADDR_OF(query_hca_cap_out, out, capability); 353290650Shselasky 354290650Shselasky switch (cap_mode) { 355290650Shselasky case HCA_CAP_OPMOD_GET_MAX: 356290650Shselasky memcpy(dev->hca_caps_max[cap_type], hca_caps, 357290650Shselasky MLX5_UN_SZ_BYTES(hca_cap_union)); 358290650Shselasky break; 359290650Shselasky case HCA_CAP_OPMOD_GET_CUR: 360290650Shselasky memcpy(dev->hca_caps_cur[cap_type], hca_caps, 361290650Shselasky MLX5_UN_SZ_BYTES(hca_cap_union)); 362290650Shselasky break; 363290650Shselasky default: 364290650Shselasky mlx5_core_warn(dev, 365290650Shselasky "Tried to query dev cap type(%x) with wrong opmode(%x)\n", 366290650Shselasky cap_type, cap_mode); 367290650Shselasky err = -EINVAL; 368290650Shselasky break; 369290650Shselasky } 370290650Shselaskyquery_ex: 371290650Shselasky kfree(out); 372290650Shselasky return err; 373290650Shselasky} 374290650Shselasky 375331807Shselaskyint mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type) 376331807Shselasky{ 377331807Shselasky int ret; 378331807Shselasky 379331807Shselasky ret = mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_CUR); 380331807Shselasky if (ret) 381331807Shselasky return ret; 382331807Shselasky 383331807Shselasky return mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_MAX); 384331807Shselasky} 385331807Shselasky 386290650Shselaskystatic int set_caps(struct mlx5_core_dev *dev, void *in, int in_sz) 387290650Shselasky{ 388331807Shselasky u32 out[MLX5_ST_SZ_DW(set_hca_cap_out)] = {0}; 389290650Shselasky 390290650Shselasky MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP); 391290650Shselasky 392331807Shselasky return mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out)); 393290650Shselasky} 394290650Shselasky 395290650Shselaskystatic int handle_hca_cap(struct mlx5_core_dev *dev) 396290650Shselasky{ 397290650Shselasky void *set_ctx = NULL; 398290650Shselasky struct mlx5_profile *prof = dev->profile; 399290650Shselasky int err = -ENOMEM; 400290650Shselasky int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in); 401290650Shselasky void *set_hca_cap; 402290650Shselasky 403290650Shselasky set_ctx = kzalloc(set_sz, GFP_KERNEL); 404290650Shselasky 405331807Shselasky err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL); 406290650Shselasky if (err) 407290650Shselasky goto query_ex; 408290650Shselasky 409290650Shselasky set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, 410290650Shselasky capability); 411290650Shselasky memcpy(set_hca_cap, dev->hca_caps_cur[MLX5_CAP_GENERAL], 412290650Shselasky MLX5_ST_SZ_BYTES(cmd_hca_cap)); 413290650Shselasky 414290650Shselasky mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n", 415290650Shselasky mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)), 416290650Shselasky 128); 417290650Shselasky /* we limit the size of the pkey table to 128 entries for now */ 418290650Shselasky MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size, 419290650Shselasky to_fw_pkey_sz(128)); 420290650Shselasky 421290650Shselasky if (prof->mask & MLX5_PROF_MASK_QP_SIZE) 422290650Shselasky MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp, 423290650Shselasky prof->log_max_qp); 424290650Shselasky 425290650Shselasky /* disable cmdif checksum */ 426290650Shselasky MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0); 427290650Shselasky 428306233Shselasky /* enable drain sigerr */ 429306233Shselasky MLX5_SET(cmd_hca_cap, set_hca_cap, drain_sigerr, 1); 430306233Shselasky 431290650Shselasky MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12); 432290650Shselasky 433290650Shselasky err = set_caps(dev, set_ctx, set_sz); 434290650Shselasky 435290650Shselaskyquery_ex: 436290650Shselasky kfree(set_ctx); 437290650Shselasky return err; 438290650Shselasky} 439290650Shselasky 440329209Shselaskystatic int handle_hca_cap_atomic(struct mlx5_core_dev *dev) 441329209Shselasky{ 442329209Shselasky void *set_ctx; 443329209Shselasky void *set_hca_cap; 444329209Shselasky int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in); 445329209Shselasky int req_endianness; 446329209Shselasky int err; 447329209Shselasky 448329209Shselasky if (MLX5_CAP_GEN(dev, atomic)) { 449331807Shselasky err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC); 450329209Shselasky if (err) 451329209Shselasky return err; 452329209Shselasky } else { 453329209Shselasky return 0; 454329209Shselasky } 455329209Shselasky 456329209Shselasky req_endianness = 457329209Shselasky MLX5_CAP_ATOMIC(dev, 458329209Shselasky supported_atomic_req_8B_endianess_mode_1); 459329209Shselasky 460329209Shselasky if (req_endianness != MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS) 461329209Shselasky return 0; 462329209Shselasky 463329209Shselasky set_ctx = kzalloc(set_sz, GFP_KERNEL); 464329209Shselasky if (!set_ctx) 465329209Shselasky return -ENOMEM; 466329209Shselasky 467329209Shselasky MLX5_SET(set_hca_cap_in, set_ctx, op_mod, 468329209Shselasky MLX5_SET_HCA_CAP_OP_MOD_ATOMIC << 1); 469329209Shselasky set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability); 470329209Shselasky 471329209Shselasky /* Set requestor to host endianness */ 472329209Shselasky MLX5_SET(atomic_caps, set_hca_cap, atomic_req_8B_endianess_mode, 473329209Shselasky MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS); 474329209Shselasky 475329209Shselasky err = set_caps(dev, set_ctx, set_sz); 476329209Shselasky 477329209Shselasky kfree(set_ctx); 478329209Shselasky return err; 479329209Shselasky} 480329209Shselasky 481290650Shselaskystatic int set_hca_ctrl(struct mlx5_core_dev *dev) 482290650Shselasky{ 483290650Shselasky struct mlx5_reg_host_endianess he_in; 484290650Shselasky struct mlx5_reg_host_endianess he_out; 485290650Shselasky int err; 486290650Shselasky 487306233Shselasky if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH && 488306233Shselasky !MLX5_CAP_GEN(dev, roce)) 489306233Shselasky return 0; 490306233Shselasky 491290650Shselasky memset(&he_in, 0, sizeof(he_in)); 492290650Shselasky he_in.he = MLX5_SET_HOST_ENDIANNESS; 493290650Shselasky err = mlx5_core_access_reg(dev, &he_in, sizeof(he_in), 494290650Shselasky &he_out, sizeof(he_out), 495290650Shselasky MLX5_REG_HOST_ENDIANNESS, 0, 1); 496290650Shselasky return err; 497290650Shselasky} 498290650Shselasky 499290650Shselaskystatic int mlx5_core_enable_hca(struct mlx5_core_dev *dev) 500290650Shselasky{ 501331807Shselasky u32 out[MLX5_ST_SZ_DW(enable_hca_out)] = {0}; 502331807Shselasky u32 in[MLX5_ST_SZ_DW(enable_hca_in)] = {0}; 503290650Shselasky 504290650Shselasky MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA); 505331807Shselasky return mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out)); 506290650Shselasky} 507290650Shselasky 508290650Shselaskystatic int mlx5_core_disable_hca(struct mlx5_core_dev *dev) 509290650Shselasky{ 510331807Shselasky u32 out[MLX5_ST_SZ_DW(disable_hca_out)] = {0}; 511331807Shselasky u32 in[MLX5_ST_SZ_DW(disable_hca_in)] = {0}; 512290650Shselasky 513290650Shselasky MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA); 514331807Shselasky return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); 515290650Shselasky} 516290650Shselasky 517290650Shselaskystatic int mlx5_core_set_issi(struct mlx5_core_dev *dev) 518290650Shselasky{ 519331807Shselasky u32 query_in[MLX5_ST_SZ_DW(query_issi_in)] = {0}; 520331807Shselasky u32 query_out[MLX5_ST_SZ_DW(query_issi_out)] = {0}; 521331807Shselasky u32 sup_issi; 522290650Shselasky int err; 523290650Shselasky 524290650Shselasky MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI); 525290650Shselasky 526331807Shselasky err = mlx5_cmd_exec(dev, query_in, sizeof(query_in), query_out, sizeof(query_out)); 527290650Shselasky if (err) { 528331807Shselasky u32 syndrome; 529331807Shselasky u8 status; 530331807Shselasky 531331807Shselasky mlx5_cmd_mbox_status(query_out, &status, &syndrome); 532331807Shselasky if (status == MLX5_CMD_STAT_BAD_OP_ERR) { 533290650Shselasky pr_debug("Only ISSI 0 is supported\n"); 534290650Shselasky return 0; 535290650Shselasky } 536290650Shselasky 537290650Shselasky printf("mlx5_core: ERR: ""failed to query ISSI\n"); 538290650Shselasky return err; 539290650Shselasky } 540290650Shselasky 541290650Shselasky sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0); 542290650Shselasky 543290650Shselasky if (sup_issi & (1 << 1)) { 544331807Shselasky u32 set_in[MLX5_ST_SZ_DW(set_issi_in)] = {0}; 545331807Shselasky u32 set_out[MLX5_ST_SZ_DW(set_issi_out)] = {0}; 546290650Shselasky 547290650Shselasky MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI); 548290650Shselasky MLX5_SET(set_issi_in, set_in, current_issi, 1); 549290650Shselasky 550331807Shselasky err = mlx5_cmd_exec(dev, set_in, sizeof(set_in), set_out, sizeof(set_out)); 551290650Shselasky if (err) { 552331807Shselasky printf("mlx5_core: ERR: ""failed to set ISSI=1 err(%d)\n", err); 553290650Shselasky return err; 554290650Shselasky } 555290650Shselasky 556290650Shselasky dev->issi = 1; 557290650Shselasky 558290650Shselasky return 0; 559290650Shselasky } else if (sup_issi & (1 << 0)) { 560290650Shselasky return 0; 561290650Shselasky } 562290650Shselasky 563290650Shselasky return -ENOTSUPP; 564290650Shselasky} 565290650Shselasky 566290650Shselasky 567290650Shselaskyint mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn, int *irqn) 568290650Shselasky{ 569290650Shselasky struct mlx5_eq_table *table = &dev->priv.eq_table; 570290650Shselasky struct mlx5_eq *eq; 571290650Shselasky int err = -ENOENT; 572290650Shselasky 573290650Shselasky spin_lock(&table->lock); 574290650Shselasky list_for_each_entry(eq, &table->comp_eqs_list, list) { 575290650Shselasky if (eq->index == vector) { 576290650Shselasky *eqn = eq->eqn; 577290650Shselasky *irqn = eq->irqn; 578290650Shselasky err = 0; 579290650Shselasky break; 580290650Shselasky } 581290650Shselasky } 582290650Shselasky spin_unlock(&table->lock); 583290650Shselasky 584290650Shselasky return err; 585290650Shselasky} 586290650ShselaskyEXPORT_SYMBOL(mlx5_vector2eqn); 587290650Shselasky 588290650Shselaskyint mlx5_rename_eq(struct mlx5_core_dev *dev, int eq_ix, char *name) 589290650Shselasky{ 590290650Shselasky struct mlx5_priv *priv = &dev->priv; 591290650Shselasky struct mlx5_eq_table *table = &priv->eq_table; 592290650Shselasky struct mlx5_eq *eq; 593290650Shselasky int err = -ENOENT; 594290650Shselasky 595290650Shselasky spin_lock(&table->lock); 596290650Shselasky list_for_each_entry(eq, &table->comp_eqs_list, list) { 597290650Shselasky if (eq->index == eq_ix) { 598290650Shselasky int irq_ix = eq_ix + MLX5_EQ_VEC_COMP_BASE; 599290650Shselasky 600290650Shselasky snprintf(priv->irq_info[irq_ix].name, MLX5_MAX_IRQ_NAME, 601290650Shselasky "%s-%d", name, eq_ix); 602290650Shselasky 603290650Shselasky err = 0; 604290650Shselasky break; 605290650Shselasky } 606290650Shselasky } 607290650Shselasky spin_unlock(&table->lock); 608290650Shselasky 609290650Shselasky return err; 610290650Shselasky} 611290650Shselasky 612290650Shselaskystatic void free_comp_eqs(struct mlx5_core_dev *dev) 613290650Shselasky{ 614290650Shselasky struct mlx5_eq_table *table = &dev->priv.eq_table; 615290650Shselasky struct mlx5_eq *eq, *n; 616290650Shselasky 617290650Shselasky spin_lock(&table->lock); 618290650Shselasky list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) { 619290650Shselasky list_del(&eq->list); 620290650Shselasky spin_unlock(&table->lock); 621290650Shselasky if (mlx5_destroy_unmap_eq(dev, eq)) 622290650Shselasky mlx5_core_warn(dev, "failed to destroy EQ 0x%x\n", 623290650Shselasky eq->eqn); 624290650Shselasky kfree(eq); 625290650Shselasky spin_lock(&table->lock); 626290650Shselasky } 627290650Shselasky spin_unlock(&table->lock); 628290650Shselasky} 629290650Shselasky 630290650Shselaskystatic int alloc_comp_eqs(struct mlx5_core_dev *dev) 631290650Shselasky{ 632290650Shselasky struct mlx5_eq_table *table = &dev->priv.eq_table; 633290650Shselasky char name[MLX5_MAX_IRQ_NAME]; 634290650Shselasky struct mlx5_eq *eq; 635290650Shselasky int ncomp_vec; 636290650Shselasky int nent; 637290650Shselasky int err; 638290650Shselasky int i; 639290650Shselasky 640290650Shselasky INIT_LIST_HEAD(&table->comp_eqs_list); 641290650Shselasky ncomp_vec = table->num_comp_vectors; 642290650Shselasky nent = MLX5_COMP_EQ_SIZE; 643290650Shselasky for (i = 0; i < ncomp_vec; i++) { 644290650Shselasky eq = kzalloc(sizeof(*eq), GFP_KERNEL); 645290650Shselasky 646290650Shselasky snprintf(name, MLX5_MAX_IRQ_NAME, "mlx5_comp%d", i); 647290650Shselasky err = mlx5_create_map_eq(dev, eq, 648290650Shselasky i + MLX5_EQ_VEC_COMP_BASE, nent, 0, 649290650Shselasky name, &dev->priv.uuari.uars[0]); 650290650Shselasky if (err) { 651290650Shselasky kfree(eq); 652290650Shselasky goto clean; 653290650Shselasky } 654290650Shselasky mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->eqn); 655290650Shselasky eq->index = i; 656290650Shselasky spin_lock(&table->lock); 657290650Shselasky list_add_tail(&eq->list, &table->comp_eqs_list); 658290650Shselasky spin_unlock(&table->lock); 659290650Shselasky } 660290650Shselasky 661290650Shselasky return 0; 662290650Shselasky 663290650Shselaskyclean: 664290650Shselasky free_comp_eqs(dev); 665290650Shselasky return err; 666290650Shselasky} 667290650Shselasky 668290650Shselaskystatic int map_bf_area(struct mlx5_core_dev *dev) 669290650Shselasky{ 670290650Shselasky resource_size_t bf_start = pci_resource_start(dev->pdev, 0); 671290650Shselasky resource_size_t bf_len = pci_resource_len(dev->pdev, 0); 672290650Shselasky 673290650Shselasky dev->priv.bf_mapping = io_mapping_create_wc(bf_start, bf_len); 674290650Shselasky 675290650Shselasky return dev->priv.bf_mapping ? 0 : -ENOMEM; 676290650Shselasky} 677290650Shselasky 678290650Shselaskystatic void unmap_bf_area(struct mlx5_core_dev *dev) 679290650Shselasky{ 680290650Shselasky if (dev->priv.bf_mapping) 681290650Shselasky io_mapping_free(dev->priv.bf_mapping); 682290650Shselasky} 683290650Shselasky 684290650Shselaskystatic inline int fw_initializing(struct mlx5_core_dev *dev) 685290650Shselasky{ 686290650Shselasky return ioread32be(&dev->iseg->initializing) >> 31; 687290650Shselasky} 688290650Shselasky 689290650Shselaskystatic int wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili) 690290650Shselasky{ 691290650Shselasky u64 end = jiffies + msecs_to_jiffies(max_wait_mili); 692290650Shselasky int err = 0; 693290650Shselasky 694290650Shselasky while (fw_initializing(dev)) { 695290650Shselasky if (time_after(jiffies, end)) { 696290650Shselasky err = -EBUSY; 697290650Shselasky break; 698290650Shselasky } 699290650Shselasky msleep(FW_INIT_WAIT_MS); 700290650Shselasky } 701290650Shselasky 702290650Shselasky return err; 703290650Shselasky} 704290650Shselasky 705331580Shselaskystatic void mlx5_add_device(struct mlx5_interface *intf, struct mlx5_priv *priv) 706290650Shselasky{ 707331580Shselasky struct mlx5_device_context *dev_ctx; 708331580Shselasky struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv); 709331580Shselasky 710331580Shselasky dev_ctx = kzalloc(sizeof(*dev_ctx), GFP_KERNEL); 711331580Shselasky if (!dev_ctx) 712331580Shselasky return; 713331580Shselasky 714331580Shselasky dev_ctx->intf = intf; 715331580Shselasky CURVNET_SET_QUIET(vnet0); 716331580Shselasky dev_ctx->context = intf->add(dev); 717331580Shselasky CURVNET_RESTORE(); 718331580Shselasky 719331580Shselasky if (dev_ctx->context) { 720331580Shselasky spin_lock_irq(&priv->ctx_lock); 721331580Shselasky list_add_tail(&dev_ctx->list, &priv->ctx_list); 722331580Shselasky spin_unlock_irq(&priv->ctx_lock); 723331580Shselasky } else { 724331580Shselasky kfree(dev_ctx); 725331580Shselasky } 726331580Shselasky} 727331580Shselasky 728331580Shselaskystatic void mlx5_remove_device(struct mlx5_interface *intf, struct mlx5_priv *priv) 729331580Shselasky{ 730331580Shselasky struct mlx5_device_context *dev_ctx; 731331580Shselasky struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv); 732331580Shselasky 733331580Shselasky list_for_each_entry(dev_ctx, &priv->ctx_list, list) 734331580Shselasky if (dev_ctx->intf == intf) { 735331580Shselasky spin_lock_irq(&priv->ctx_lock); 736331580Shselasky list_del(&dev_ctx->list); 737331580Shselasky spin_unlock_irq(&priv->ctx_lock); 738331580Shselasky 739331580Shselasky intf->remove(dev, dev_ctx->context); 740331580Shselasky kfree(dev_ctx); 741331580Shselasky return; 742331580Shselasky } 743331580Shselasky} 744331580Shselasky 745341958Shselaskyint 746341958Shselaskymlx5_register_device(struct mlx5_core_dev *dev) 747331580Shselasky{ 748290650Shselasky struct mlx5_priv *priv = &dev->priv; 749331580Shselasky struct mlx5_interface *intf; 750290650Shselasky 751331580Shselasky mutex_lock(&intf_mutex); 752331580Shselasky list_add_tail(&priv->dev_list, &dev_list); 753331580Shselasky list_for_each_entry(intf, &intf_list, list) 754331580Shselasky mlx5_add_device(intf, priv); 755331580Shselasky mutex_unlock(&intf_mutex); 756331580Shselasky 757331580Shselasky return 0; 758331580Shselasky} 759331580Shselasky 760341958Shselaskyvoid 761341958Shselaskymlx5_unregister_device(struct mlx5_core_dev *dev) 762331580Shselasky{ 763331580Shselasky struct mlx5_priv *priv = &dev->priv; 764331580Shselasky struct mlx5_interface *intf; 765331580Shselasky 766331580Shselasky mutex_lock(&intf_mutex); 767331580Shselasky list_for_each_entry(intf, &intf_list, list) 768331580Shselasky mlx5_remove_device(intf, priv); 769331580Shselasky list_del(&priv->dev_list); 770331580Shselasky mutex_unlock(&intf_mutex); 771331580Shselasky} 772331580Shselasky 773331580Shselaskyint mlx5_register_interface(struct mlx5_interface *intf) 774331580Shselasky{ 775331580Shselasky struct mlx5_priv *priv; 776331580Shselasky 777331580Shselasky if (!intf->add || !intf->remove) 778331580Shselasky return -EINVAL; 779331580Shselasky 780331580Shselasky mutex_lock(&intf_mutex); 781331580Shselasky list_add_tail(&intf->list, &intf_list); 782331580Shselasky list_for_each_entry(priv, &dev_list, dev_list) 783331580Shselasky mlx5_add_device(intf, priv); 784331580Shselasky mutex_unlock(&intf_mutex); 785331580Shselasky 786331580Shselasky return 0; 787331580Shselasky} 788331580ShselaskyEXPORT_SYMBOL(mlx5_register_interface); 789331580Shselasky 790331580Shselaskyvoid mlx5_unregister_interface(struct mlx5_interface *intf) 791331580Shselasky{ 792331580Shselasky struct mlx5_priv *priv; 793331580Shselasky 794331580Shselasky mutex_lock(&intf_mutex); 795331580Shselasky list_for_each_entry(priv, &dev_list, dev_list) 796331580Shselasky mlx5_remove_device(intf, priv); 797331580Shselasky list_del(&intf->list); 798331580Shselasky mutex_unlock(&intf_mutex); 799331580Shselasky} 800331580ShselaskyEXPORT_SYMBOL(mlx5_unregister_interface); 801331580Shselasky 802331580Shselaskyvoid *mlx5_get_protocol_dev(struct mlx5_core_dev *mdev, int protocol) 803331580Shselasky{ 804331580Shselasky struct mlx5_priv *priv = &mdev->priv; 805331580Shselasky struct mlx5_device_context *dev_ctx; 806331580Shselasky unsigned long flags; 807331580Shselasky void *result = NULL; 808331580Shselasky 809331580Shselasky spin_lock_irqsave(&priv->ctx_lock, flags); 810331580Shselasky 811331580Shselasky list_for_each_entry(dev_ctx, &mdev->priv.ctx_list, list) 812331580Shselasky if ((dev_ctx->intf->protocol == protocol) && 813331580Shselasky dev_ctx->intf->get_dev) { 814331580Shselasky result = dev_ctx->intf->get_dev(dev_ctx->context); 815331580Shselasky break; 816331580Shselasky } 817331580Shselasky 818331580Shselasky spin_unlock_irqrestore(&priv->ctx_lock, flags); 819331580Shselasky 820331580Shselasky return result; 821331580Shselasky} 822331580ShselaskyEXPORT_SYMBOL(mlx5_get_protocol_dev); 823331580Shselasky 824347847Shselaskystatic int 825347847Shselaskymlx5_firmware_update(struct mlx5_core_dev *dev) 826347847Shselasky{ 827347847Shselasky const struct firmware *fw; 828347847Shselasky int err; 829347847Shselasky 830347847Shselasky fw = firmware_get("mlx5fw_mfa"); 831347847Shselasky if (fw) { 832347847Shselasky err = mlx5_firmware_flash(dev, fw); 833347847Shselasky firmware_put(fw, FIRMWARE_UNLOAD); 834347847Shselasky } 835347847Shselasky else 836347847Shselasky return (-ENOENT); 837347847Shselasky 838347847Shselasky return err; 839347847Shselasky} 840347847Shselasky 841331580Shselaskystatic int mlx5_pci_init(struct mlx5_core_dev *dev, struct mlx5_priv *priv) 842331580Shselasky{ 843331580Shselasky struct pci_dev *pdev = dev->pdev; 844331580Shselasky int err = 0; 845331580Shselasky 846290650Shselasky pci_set_drvdata(dev->pdev, dev); 847290650Shselasky strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN); 848290650Shselasky priv->name[MLX5_MAX_NAME_LEN - 1] = 0; 849290650Shselasky 850290650Shselasky mutex_init(&priv->pgdir_mutex); 851290650Shselasky INIT_LIST_HEAD(&priv->pgdir_list); 852290650Shselasky spin_lock_init(&priv->mkey_lock); 853290650Shselasky 854290650Shselasky priv->numa_node = NUMA_NO_NODE; 855290650Shselasky 856331580Shselasky err = mlx5_pci_enable_device(dev); 857290650Shselasky if (err) { 858290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""Cannot enable PCI device, aborting\n"); 859290650Shselasky goto err_dbg; 860290650Shselasky } 861290650Shselasky 862290650Shselasky err = request_bar(pdev); 863290650Shselasky if (err) { 864290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""error requesting BARs, aborting\n"); 865290650Shselasky goto err_disable; 866290650Shselasky } 867290650Shselasky 868290650Shselasky pci_set_master(pdev); 869290650Shselasky 870290650Shselasky err = set_dma_caps(pdev); 871290650Shselasky if (err) { 872290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""Failed setting DMA capabilities mask, aborting\n"); 873290650Shselasky goto err_clr_master; 874290650Shselasky } 875290650Shselasky 876329212Shselasky dev->iseg_base = pci_resource_start(dev->pdev, 0); 877329212Shselasky dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg)); 878290650Shselasky if (!dev->iseg) { 879290650Shselasky err = -ENOMEM; 880290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""Failed mapping initialization segment, aborting\n"); 881290650Shselasky goto err_clr_master; 882290650Shselasky } 883331580Shselasky 884337105Shselasky return 0; 885331585Shselasky 886331580Shselaskyerr_clr_master: 887331580Shselasky release_bar(dev->pdev); 888331580Shselaskyerr_disable: 889331580Shselasky mlx5_pci_disable_device(dev); 890331580Shselaskyerr_dbg: 891331580Shselasky return err; 892331580Shselasky} 893331580Shselasky 894331580Shselaskystatic void mlx5_pci_close(struct mlx5_core_dev *dev, struct mlx5_priv *priv) 895331580Shselasky{ 896331580Shselasky iounmap(dev->iseg); 897331580Shselasky release_bar(dev->pdev); 898331580Shselasky mlx5_pci_disable_device(dev); 899331580Shselasky} 900331580Shselasky 901331810Shselaskystatic int mlx5_init_once(struct mlx5_core_dev *dev, struct mlx5_priv *priv) 902331580Shselasky{ 903331580Shselasky struct pci_dev *pdev = dev->pdev; 904331580Shselasky int err; 905331580Shselasky 906337103Shselasky err = mlx5_vsc_find_cap(dev); 907337103Shselasky if (err) 908337103Shselasky dev_err(&pdev->dev, "Unable to find vendor specific capabilities\n"); 909331815Shselasky 910331810Shselasky err = mlx5_query_hca_caps(dev); 911331810Shselasky if (err) { 912331810Shselasky dev_err(&pdev->dev, "query hca failed\n"); 913331810Shselasky goto out; 914331810Shselasky } 915331810Shselasky 916331810Shselasky err = mlx5_query_board_id(dev); 917331810Shselasky if (err) { 918331810Shselasky dev_err(&pdev->dev, "query board id failed\n"); 919331810Shselasky goto out; 920331810Shselasky } 921331810Shselasky 922331810Shselasky err = mlx5_eq_init(dev); 923331810Shselasky if (err) { 924331810Shselasky dev_err(&pdev->dev, "failed to initialize eq\n"); 925331810Shselasky goto out; 926331810Shselasky } 927331810Shselasky 928331810Shselasky MLX5_INIT_DOORBELL_LOCK(&priv->cq_uar_lock); 929331810Shselasky 930331810Shselasky err = mlx5_init_cq_table(dev); 931331810Shselasky if (err) { 932331810Shselasky dev_err(&pdev->dev, "failed to initialize cq table\n"); 933331810Shselasky goto err_eq_cleanup; 934331810Shselasky } 935331810Shselasky 936331810Shselasky mlx5_init_qp_table(dev); 937331810Shselasky mlx5_init_srq_table(dev); 938331810Shselasky mlx5_init_mr_table(dev); 939331810Shselasky 940341958Shselasky mlx5_init_reserved_gids(dev); 941341958Shselasky mlx5_fpga_init(dev); 942341958Shselasky 943331810Shselasky return 0; 944331810Shselasky 945331810Shselaskyerr_eq_cleanup: 946331810Shselasky mlx5_eq_cleanup(dev); 947331810Shselasky 948331810Shselaskyout: 949331810Shselasky return err; 950331810Shselasky} 951331810Shselasky 952331810Shselaskystatic void mlx5_cleanup_once(struct mlx5_core_dev *dev) 953331810Shselasky{ 954341958Shselasky mlx5_fpga_cleanup(dev); 955341958Shselasky mlx5_cleanup_reserved_gids(dev); 956331810Shselasky mlx5_cleanup_mr_table(dev); 957331810Shselasky mlx5_cleanup_srq_table(dev); 958331810Shselasky mlx5_cleanup_qp_table(dev); 959331810Shselasky mlx5_cleanup_cq_table(dev); 960331810Shselasky mlx5_eq_cleanup(dev); 961331810Shselasky} 962331810Shselasky 963331810Shselaskystatic int mlx5_load_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv, 964331810Shselasky bool boot) 965331810Shselasky{ 966331810Shselasky struct pci_dev *pdev = dev->pdev; 967331810Shselasky int err; 968331810Shselasky 969331580Shselasky mutex_lock(&dev->intf_state_mutex); 970331580Shselasky if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) { 971331580Shselasky dev_warn(&dev->pdev->dev, "%s: interface is up, NOP\n", 972331580Shselasky __func__); 973331580Shselasky goto out; 974331580Shselasky } 975331580Shselasky 976290650Shselasky device_printf((&pdev->dev)->bsddev, "INFO: ""firmware version: %d.%d.%d\n", fw_rev_maj(dev), fw_rev_min(dev), fw_rev_sub(dev)); 977290650Shselasky 978306233Shselasky /* 979306233Shselasky * On load removing any previous indication of internal error, 980306233Shselasky * device is up 981306233Shselasky */ 982306233Shselasky dev->state = MLX5_DEVICE_STATE_UP; 983306233Shselasky 984290650Shselasky err = mlx5_cmd_init(dev); 985290650Shselasky if (err) { 986290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""Failed initializing command interface, aborting\n"); 987331580Shselasky goto out_err; 988290650Shselasky } 989290650Shselasky 990290650Shselasky err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI); 991290650Shselasky if (err) { 992290650Shselasky device_printf((&dev->pdev->dev)->bsddev, "ERR: ""Firmware over %d MS in initializing state, aborting\n", FW_INIT_TIMEOUT_MILI); 993290650Shselasky goto err_cmd_cleanup; 994290650Shselasky } 995290650Shselasky 996290650Shselasky err = mlx5_core_enable_hca(dev); 997290650Shselasky if (err) { 998290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""enable hca failed\n"); 999331810Shselasky goto err_cmd_cleanup; 1000290650Shselasky } 1001290650Shselasky 1002290650Shselasky err = mlx5_core_set_issi(dev); 1003290650Shselasky if (err) { 1004290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""failed to set issi\n"); 1005290650Shselasky goto err_disable_hca; 1006290650Shselasky } 1007290650Shselasky 1008290650Shselasky err = mlx5_pagealloc_start(dev); 1009290650Shselasky if (err) { 1010290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""mlx5_pagealloc_start failed\n"); 1011290650Shselasky goto err_disable_hca; 1012290650Shselasky } 1013290650Shselasky 1014290650Shselasky err = mlx5_satisfy_startup_pages(dev, 1); 1015290650Shselasky if (err) { 1016290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""failed to allocate boot pages\n"); 1017290650Shselasky goto err_pagealloc_stop; 1018290650Shselasky } 1019290650Shselasky 1020329209Shselasky err = set_hca_ctrl(dev); 1021329209Shselasky if (err) { 1022329209Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""set_hca_ctrl failed\n"); 1023329209Shselasky goto reclaim_boot_pages; 1024329209Shselasky } 1025329209Shselasky 1026306233Shselasky err = handle_hca_cap(dev); 1027290650Shselasky if (err) { 1028306233Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""handle_hca_cap failed\n"); 1029290650Shselasky goto reclaim_boot_pages; 1030290650Shselasky } 1031290650Shselasky 1032329209Shselasky err = handle_hca_cap_atomic(dev); 1033290650Shselasky if (err) { 1034329209Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""handle_hca_cap_atomic failed\n"); 1035290650Shselasky goto reclaim_boot_pages; 1036290650Shselasky } 1037290650Shselasky 1038290650Shselasky err = mlx5_satisfy_startup_pages(dev, 0); 1039290650Shselasky if (err) { 1040290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""failed to allocate init pages\n"); 1041290650Shselasky goto reclaim_boot_pages; 1042290650Shselasky } 1043290650Shselasky 1044290650Shselasky err = mlx5_cmd_init_hca(dev); 1045290650Shselasky if (err) { 1046290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""init hca failed\n"); 1047290650Shselasky goto reclaim_boot_pages; 1048290650Shselasky } 1049290650Shselasky 1050290650Shselasky mlx5_start_health_poll(dev); 1051290650Shselasky 1052331810Shselasky if (boot && mlx5_init_once(dev, priv)) { 1053331810Shselasky dev_err(&pdev->dev, "sw objs init failed\n"); 1054290650Shselasky goto err_stop_poll; 1055290650Shselasky } 1056290650Shselasky 1057290650Shselasky err = mlx5_enable_msix(dev); 1058290650Shselasky if (err) { 1059290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""enable msix failed\n"); 1060331810Shselasky goto err_cleanup_once; 1061290650Shselasky } 1062290650Shselasky 1063290650Shselasky err = mlx5_alloc_uuars(dev, &priv->uuari); 1064290650Shselasky if (err) { 1065290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""Failed allocating uar, aborting\n"); 1066331810Shselasky goto err_disable_msix; 1067290650Shselasky } 1068290650Shselasky 1069290650Shselasky err = mlx5_start_eqs(dev); 1070290650Shselasky if (err) { 1071290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""Failed to start pages and async EQs\n"); 1072290650Shselasky goto err_free_uar; 1073290650Shselasky } 1074290650Shselasky 1075290650Shselasky err = alloc_comp_eqs(dev); 1076290650Shselasky if (err) { 1077290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""Failed to alloc completion EQs\n"); 1078290650Shselasky goto err_stop_eqs; 1079290650Shselasky } 1080290650Shselasky 1081290650Shselasky if (map_bf_area(dev)) 1082290650Shselasky device_printf((&pdev->dev)->bsddev, "ERR: ""Failed to map blue flame area\n"); 1083290650Shselasky 1084329200Shselasky err = mlx5_init_fs(dev); 1085329200Shselasky if (err) { 1086329200Shselasky mlx5_core_err(dev, "flow steering init %d\n", err); 1087331810Shselasky goto err_free_comp_eqs; 1088329200Shselasky } 1089329200Shselasky 1090341958Shselasky err = mlx5_fpga_device_start(dev); 1091341958Shselasky if (err) { 1092341958Shselasky dev_err(&pdev->dev, "fpga device start failed %d\n", err); 1093341958Shselasky goto err_fpga_start; 1094341958Shselasky } 1095341958Shselasky 1096331580Shselasky err = mlx5_register_device(dev); 1097331580Shselasky if (err) { 1098331580Shselasky dev_err(&pdev->dev, "mlx5_register_device failed %d\n", err); 1099331810Shselasky goto err_fs; 1100331580Shselasky } 1101331580Shselasky 1102331580Shselasky set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state); 1103331580Shselasky 1104331580Shselaskyout: 1105331580Shselasky mutex_unlock(&dev->intf_state_mutex); 1106290650Shselasky return 0; 1107290650Shselasky 1108341958Shselaskyerr_fpga_start: 1109331810Shselaskyerr_fs: 1110331580Shselasky mlx5_cleanup_fs(dev); 1111331810Shselasky 1112331810Shselaskyerr_free_comp_eqs: 1113331810Shselasky free_comp_eqs(dev); 1114329200Shselasky unmap_bf_area(dev); 1115329200Shselasky 1116290650Shselaskyerr_stop_eqs: 1117290650Shselasky mlx5_stop_eqs(dev); 1118290650Shselasky 1119290650Shselaskyerr_free_uar: 1120290650Shselasky mlx5_free_uuars(dev, &priv->uuari); 1121290650Shselasky 1122331810Shselaskyerr_disable_msix: 1123290650Shselasky mlx5_disable_msix(dev); 1124290650Shselasky 1125331810Shselaskyerr_cleanup_once: 1126331810Shselasky if (boot) 1127331810Shselasky mlx5_cleanup_once(dev); 1128331810Shselasky 1129290650Shselaskyerr_stop_poll: 1130341934Shselasky mlx5_stop_health_poll(dev, boot); 1131290650Shselasky if (mlx5_cmd_teardown_hca(dev)) { 1132290650Shselasky device_printf((&dev->pdev->dev)->bsddev, "ERR: ""tear_down_hca failed, skip cleanup\n"); 1133331580Shselasky goto out_err; 1134290650Shselasky } 1135290650Shselasky 1136290650Shselaskyreclaim_boot_pages: 1137290650Shselasky mlx5_reclaim_startup_pages(dev); 1138290650Shselasky 1139290650Shselaskyerr_pagealloc_stop: 1140290650Shselasky mlx5_pagealloc_stop(dev); 1141290650Shselasky 1142290650Shselaskyerr_disable_hca: 1143290650Shselasky mlx5_core_disable_hca(dev); 1144290650Shselasky 1145290650Shselaskyerr_cmd_cleanup: 1146290650Shselasky mlx5_cmd_cleanup(dev); 1147290650Shselasky 1148331580Shselaskyout_err: 1149331580Shselasky dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR; 1150331580Shselasky mutex_unlock(&dev->intf_state_mutex); 1151290650Shselasky 1152290650Shselasky return err; 1153290650Shselasky} 1154290650Shselasky 1155331810Shselaskystatic int mlx5_unload_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv, 1156331810Shselasky bool cleanup) 1157290650Shselasky{ 1158331580Shselasky int err = 0; 1159290650Shselasky 1160331811Shselasky if (cleanup) 1161331811Shselasky mlx5_drain_health_recovery(dev); 1162331811Shselasky 1163331580Shselasky mutex_lock(&dev->intf_state_mutex); 1164347799Shselasky if (!test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) { 1165331580Shselasky dev_warn(&dev->pdev->dev, "%s: interface is down, NOP\n", __func__); 1166331810Shselasky if (cleanup) 1167331810Shselasky mlx5_cleanup_once(dev); 1168331580Shselasky goto out; 1169331580Shselasky } 1170331580Shselasky 1171331580Shselasky mlx5_unregister_device(dev); 1172331580Shselasky 1173341958Shselasky mlx5_fpga_device_stop(dev); 1174329200Shselasky mlx5_cleanup_fs(dev); 1175290650Shselasky unmap_bf_area(dev); 1176322144Shselasky mlx5_wait_for_reclaim_vfs_pages(dev); 1177290650Shselasky free_comp_eqs(dev); 1178290650Shselasky mlx5_stop_eqs(dev); 1179290650Shselasky mlx5_free_uuars(dev, &priv->uuari); 1180290650Shselasky mlx5_disable_msix(dev); 1181331810Shselasky if (cleanup) 1182331810Shselasky mlx5_cleanup_once(dev); 1183341934Shselasky mlx5_stop_health_poll(dev, cleanup); 1184331580Shselasky err = mlx5_cmd_teardown_hca(dev); 1185331580Shselasky if (err) { 1186290650Shselasky device_printf((&dev->pdev->dev)->bsddev, "ERR: ""tear_down_hca failed, skip cleanup\n"); 1187331580Shselasky goto out; 1188290650Shselasky } 1189290650Shselasky mlx5_pagealloc_stop(dev); 1190290650Shselasky mlx5_reclaim_startup_pages(dev); 1191290650Shselasky mlx5_core_disable_hca(dev); 1192290650Shselasky mlx5_cmd_cleanup(dev); 1193290650Shselasky 1194331580Shselaskyout: 1195331580Shselasky clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state); 1196331580Shselasky mutex_unlock(&dev->intf_state_mutex); 1197331580Shselasky return err; 1198290650Shselasky} 1199290650Shselasky 1200331580Shselaskyvoid mlx5_core_event(struct mlx5_core_dev *dev, enum mlx5_dev_event event, 1201331580Shselasky unsigned long param) 1202290650Shselasky{ 1203290650Shselasky struct mlx5_priv *priv = &dev->priv; 1204290650Shselasky struct mlx5_device_context *dev_ctx; 1205290650Shselasky unsigned long flags; 1206290650Shselasky 1207290650Shselasky spin_lock_irqsave(&priv->ctx_lock, flags); 1208290650Shselasky 1209290650Shselasky list_for_each_entry(dev_ctx, &priv->ctx_list, list) 1210290650Shselasky if (dev_ctx->intf->event) 1211290650Shselasky dev_ctx->intf->event(dev, dev_ctx->context, event, param); 1212290650Shselasky 1213290650Shselasky spin_unlock_irqrestore(&priv->ctx_lock, flags); 1214290650Shselasky} 1215290650Shselasky 1216290650Shselaskystruct mlx5_core_event_handler { 1217290650Shselasky void (*event)(struct mlx5_core_dev *dev, 1218290650Shselasky enum mlx5_dev_event event, 1219290650Shselasky void *data); 1220290650Shselasky}; 1221290650Shselasky 1222290650Shselaskystatic int init_one(struct pci_dev *pdev, 1223290650Shselasky const struct pci_device_id *id) 1224290650Shselasky{ 1225290650Shselasky struct mlx5_core_dev *dev; 1226290650Shselasky struct mlx5_priv *priv; 1227338554Shselasky device_t bsddev = pdev->dev.bsddev; 1228290650Shselasky int err; 1229290650Shselasky 1230290650Shselasky dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1231290650Shselasky priv = &dev->priv; 1232306233Shselasky if (id) 1233306233Shselasky priv->pci_dev_data = id->driver_data; 1234290650Shselasky 1235347835Shselasky if (mlx5_prof_sel < 0 || mlx5_prof_sel >= ARRAY_SIZE(profiles)) { 1236341930Shselasky device_printf(bsddev, "WARN: selected profile out of range, selecting default (%d)\n", MLX5_DEFAULT_PROF); 1237347835Shselasky mlx5_prof_sel = MLX5_DEFAULT_PROF; 1238290650Shselasky } 1239347835Shselasky dev->profile = &profiles[mlx5_prof_sel]; 1240331580Shselasky dev->pdev = pdev; 1241290650Shselasky dev->event = mlx5_core_event; 1242290650Shselasky 1243341948Shselasky /* Set desc */ 1244341948Shselasky device_set_desc(bsddev, mlx5_version); 1245341948Shselasky 1246338554Shselasky sysctl_ctx_init(&dev->sysctl_ctx); 1247338554Shselasky SYSCTL_ADD_INT(&dev->sysctl_ctx, 1248338554Shselasky SYSCTL_CHILDREN(device_get_sysctl_tree(bsddev)), 1249338554Shselasky OID_AUTO, "msix_eqvec", CTLFLAG_RDTUN, &dev->msix_eqvec, 0, 1250338554Shselasky "Maximum number of MSIX event queue vectors, if set"); 1251338554Shselasky 1252290650Shselasky INIT_LIST_HEAD(&priv->ctx_list); 1253290650Shselasky spin_lock_init(&priv->ctx_lock); 1254341930Shselasky mutex_init(&dev->pci_status_mutex); 1255341930Shselasky mutex_init(&dev->intf_state_mutex); 1256331580Shselasky err = mlx5_pci_init(dev, priv); 1257290650Shselasky if (err) { 1258341930Shselasky device_printf(bsddev, "ERR: mlx5_pci_init failed %d\n", err); 1259331580Shselasky goto clean_dev; 1260290650Shselasky } 1261290650Shselasky 1262341930Shselasky err = mlx5_health_init(dev); 1263341930Shselasky if (err) { 1264341930Shselasky device_printf(bsddev, "ERR: mlx5_health_init failed %d\n", err); 1265341930Shselasky goto close_pci; 1266341930Shselasky } 1267331580Shselasky 1268331810Shselasky mlx5_pagealloc_init(dev); 1269331810Shselasky 1270331810Shselasky err = mlx5_load_one(dev, priv, true); 1271290650Shselasky if (err) { 1272341930Shselasky device_printf(bsddev, "ERR: mlx5_load_one failed %d\n", err); 1273331580Shselasky goto clean_health; 1274290650Shselasky } 1275290650Shselasky 1276331914Shselasky mlx5_fwdump_prep(dev); 1277331914Shselasky 1278347847Shselasky mlx5_firmware_update(dev); 1279347847Shselasky 1280341930Shselasky pci_save_state(bsddev); 1281290650Shselasky return 0; 1282290650Shselasky 1283331580Shselaskyclean_health: 1284331810Shselasky mlx5_pagealloc_cleanup(dev); 1285341930Shselasky mlx5_health_cleanup(dev); 1286331580Shselaskyclose_pci: 1287341930Shselasky mlx5_pci_close(dev, priv); 1288331580Shselaskyclean_dev: 1289338554Shselasky sysctl_ctx_free(&dev->sysctl_ctx); 1290290650Shselasky kfree(dev); 1291290650Shselasky return err; 1292290650Shselasky} 1293290650Shselasky 1294290650Shselaskystatic void remove_one(struct pci_dev *pdev) 1295290650Shselasky{ 1296290650Shselasky struct mlx5_core_dev *dev = pci_get_drvdata(pdev); 1297331580Shselasky struct mlx5_priv *priv = &dev->priv; 1298290650Shselasky 1299331810Shselasky if (mlx5_unload_one(dev, priv, true)) { 1300331580Shselasky dev_err(&dev->pdev->dev, "mlx5_unload_one failed\n"); 1301331580Shselasky mlx5_health_cleanup(dev); 1302331580Shselasky return; 1303331580Shselasky } 1304331580Shselasky 1305331914Shselasky mlx5_fwdump_clean(dev); 1306331810Shselasky mlx5_pagealloc_cleanup(dev); 1307331580Shselasky mlx5_health_cleanup(dev); 1308331580Shselasky mlx5_pci_close(dev, priv); 1309331580Shselasky pci_set_drvdata(pdev, NULL); 1310338554Shselasky sysctl_ctx_free(&dev->sysctl_ctx); 1311290650Shselasky kfree(dev); 1312290650Shselasky} 1313290650Shselasky 1314331580Shselaskystatic pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev, 1315331580Shselasky pci_channel_state_t state) 1316331580Shselasky{ 1317331580Shselasky struct mlx5_core_dev *dev = pci_get_drvdata(pdev); 1318331580Shselasky struct mlx5_priv *priv = &dev->priv; 1319331580Shselasky 1320331580Shselasky dev_info(&pdev->dev, "%s was called\n", __func__); 1321331810Shselasky mlx5_enter_error_state(dev, false); 1322331810Shselasky mlx5_unload_one(dev, priv, false); 1323331914Shselasky 1324331582Shselasky if (state) { 1325331582Shselasky mlx5_drain_health_wq(dev); 1326331582Shselasky mlx5_pci_disable_device(dev); 1327331582Shselasky } 1328331582Shselasky 1329331580Shselasky return state == pci_channel_io_perm_failure ? 1330331580Shselasky PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET; 1331331580Shselasky} 1332331580Shselasky 1333331580Shselaskystatic pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev) 1334331580Shselasky{ 1335331580Shselasky struct mlx5_core_dev *dev = pci_get_drvdata(pdev); 1336331580Shselasky int err = 0; 1337331580Shselasky 1338331580Shselasky dev_info(&pdev->dev, "%s was called\n", __func__); 1339331580Shselasky 1340331580Shselasky err = mlx5_pci_enable_device(dev); 1341331580Shselasky if (err) { 1342331580Shselasky dev_err(&pdev->dev, "%s: mlx5_pci_enable_device failed with error code: %d\n" 1343331580Shselasky , __func__, err); 1344331580Shselasky return PCI_ERS_RESULT_DISCONNECT; 1345331580Shselasky } 1346331580Shselasky pci_set_master(pdev); 1347331580Shselasky pci_set_powerstate(pdev->dev.bsddev, PCI_POWERSTATE_D0); 1348331580Shselasky pci_restore_state(pdev->dev.bsddev); 1349331816Shselasky pci_save_state(pdev->dev.bsddev); 1350331580Shselasky 1351331580Shselasky return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED; 1352331580Shselasky} 1353331580Shselasky 1354331580Shselasky/* wait for the device to show vital signs. For now we check 1355331580Shselasky * that we can read the device ID and that the health buffer 1356331580Shselasky * shows a non zero value which is different than 0xffffffff 1357331580Shselasky */ 1358331580Shselaskystatic void wait_vital(struct pci_dev *pdev) 1359331580Shselasky{ 1360331580Shselasky struct mlx5_core_dev *dev = pci_get_drvdata(pdev); 1361331580Shselasky struct mlx5_core_health *health = &dev->priv.health; 1362331580Shselasky const int niter = 100; 1363331580Shselasky u32 count; 1364331580Shselasky u16 did; 1365331580Shselasky int i; 1366331580Shselasky 1367331580Shselasky /* Wait for firmware to be ready after reset */ 1368331580Shselasky msleep(1000); 1369331580Shselasky for (i = 0; i < niter; i++) { 1370331580Shselasky if (pci_read_config_word(pdev, 2, &did)) { 1371331580Shselasky dev_warn(&pdev->dev, "failed reading config word\n"); 1372331580Shselasky break; 1373331580Shselasky } 1374331580Shselasky if (did == pdev->device) { 1375331580Shselasky dev_info(&pdev->dev, "device ID correctly read after %d iterations\n", i); 1376331580Shselasky break; 1377331580Shselasky } 1378331580Shselasky msleep(50); 1379331580Shselasky } 1380331580Shselasky if (i == niter) 1381331580Shselasky dev_warn(&pdev->dev, "%s-%d: could not read device ID\n", __func__, __LINE__); 1382331580Shselasky 1383331580Shselasky for (i = 0; i < niter; i++) { 1384331580Shselasky count = ioread32be(health->health_counter); 1385331580Shselasky if (count && count != 0xffffffff) { 1386331580Shselasky dev_info(&pdev->dev, "Counter value 0x%x after %d iterations\n", count, i); 1387331580Shselasky break; 1388331580Shselasky } 1389331580Shselasky msleep(50); 1390331580Shselasky } 1391331580Shselasky 1392331580Shselasky if (i == niter) 1393331580Shselasky dev_warn(&pdev->dev, "%s-%d: could not read device ID\n", __func__, __LINE__); 1394331580Shselasky} 1395331580Shselasky 1396331580Shselaskystatic void mlx5_pci_resume(struct pci_dev *pdev) 1397331580Shselasky{ 1398331580Shselasky struct mlx5_core_dev *dev = pci_get_drvdata(pdev); 1399331580Shselasky struct mlx5_priv *priv = &dev->priv; 1400331580Shselasky int err; 1401331580Shselasky 1402331580Shselasky dev_info(&pdev->dev, "%s was called\n", __func__); 1403331580Shselasky 1404331580Shselasky wait_vital(pdev); 1405331580Shselasky 1406331810Shselasky err = mlx5_load_one(dev, priv, false); 1407331580Shselasky if (err) 1408331580Shselasky dev_err(&pdev->dev, "%s: mlx5_load_one failed with error code: %d\n" 1409331580Shselasky , __func__, err); 1410331580Shselasky else 1411331580Shselasky dev_info(&pdev->dev, "%s: device recovered\n", __func__); 1412331580Shselasky} 1413331580Shselasky 1414331580Shselaskystatic const struct pci_error_handlers mlx5_err_handler = { 1415331580Shselasky .error_detected = mlx5_pci_err_detected, 1416331580Shselasky .slot_reset = mlx5_pci_slot_reset, 1417331580Shselasky .resume = mlx5_pci_resume 1418331580Shselasky}; 1419331580Shselasky 1420331810Shselaskystatic int mlx5_try_fast_unload(struct mlx5_core_dev *dev) 1421331810Shselasky{ 1422347818Shselasky bool fast_teardown, force_teardown; 1423331810Shselasky int err; 1424331810Shselasky 1425347819Shselasky if (!mlx5_fast_unload_enabled) { 1426347819Shselasky mlx5_core_dbg(dev, "fast unload is disabled by user\n"); 1427347819Shselasky return -EOPNOTSUPP; 1428347819Shselasky } 1429347819Shselasky 1430347818Shselasky fast_teardown = MLX5_CAP_GEN(dev, fast_teardown); 1431347818Shselasky force_teardown = MLX5_CAP_GEN(dev, force_teardown); 1432347818Shselasky 1433347818Shselasky mlx5_core_dbg(dev, "force teardown firmware support=%d\n", force_teardown); 1434347818Shselasky mlx5_core_dbg(dev, "fast teardown firmware support=%d\n", fast_teardown); 1435347818Shselasky 1436347818Shselasky if (!fast_teardown && !force_teardown) 1437331810Shselasky return -EOPNOTSUPP; 1438331810Shselasky 1439331810Shselasky if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) { 1440331810Shselasky mlx5_core_dbg(dev, "Device in internal error state, giving up\n"); 1441331810Shselasky return -EAGAIN; 1442331810Shselasky } 1443331810Shselasky 1444341934Shselasky /* Panic tear down fw command will stop the PCI bus communication 1445341934Shselasky * with the HCA, so the health polll is no longer needed. 1446341934Shselasky */ 1447341934Shselasky mlx5_drain_health_wq(dev); 1448341934Shselasky mlx5_stop_health_poll(dev, false); 1449341934Shselasky 1450347818Shselasky err = mlx5_cmd_fast_teardown_hca(dev); 1451347818Shselasky if (!err) 1452347818Shselasky goto done; 1453347818Shselasky 1454331810Shselasky err = mlx5_cmd_force_teardown_hca(dev); 1455347818Shselasky if (!err) 1456347818Shselasky goto done; 1457331810Shselasky 1458347818Shselasky mlx5_core_dbg(dev, "Firmware couldn't do fast unload error: %d\n", err); 1459347818Shselasky mlx5_start_health_poll(dev); 1460347818Shselasky return err; 1461347818Shselaskydone: 1462331810Shselasky mlx5_enter_error_state(dev, true); 1463331810Shselasky return 0; 1464331810Shselasky} 1465331810Shselasky 1466347802Shselaskystatic void mlx5_disable_interrupts(struct mlx5_core_dev *mdev) 1467347802Shselasky{ 1468347802Shselasky int nvec = mdev->priv.eq_table.num_comp_vectors + MLX5_EQ_VEC_COMP_BASE; 1469347802Shselasky int x; 1470347802Shselasky 1471347802Shselasky mdev->priv.disable_irqs = 1; 1472347802Shselasky 1473347802Shselasky /* wait for all IRQ handlers to finish processing */ 1474347802Shselasky for (x = 0; x != nvec; x++) 1475347802Shselasky synchronize_irq(mdev->priv.msix_arr[x].vector); 1476347802Shselasky} 1477347802Shselasky 1478329211Shselaskystatic void shutdown_one(struct pci_dev *pdev) 1479329211Shselasky{ 1480331580Shselasky struct mlx5_core_dev *dev = pci_get_drvdata(pdev); 1481331580Shselasky struct mlx5_priv *priv = &dev->priv; 1482331810Shselasky int err; 1483331580Shselasky 1484347802Shselasky /* enter polling mode */ 1485347802Shselasky mlx5_cmd_use_polling(dev); 1486347802Shselasky 1487347802Shselasky /* disable all interrupts */ 1488347802Shselasky mlx5_disable_interrupts(dev); 1489347802Shselasky 1490331810Shselasky err = mlx5_try_fast_unload(dev); 1491331810Shselasky if (err) 1492331810Shselasky mlx5_unload_one(dev, priv, false); 1493331580Shselasky mlx5_pci_disable_device(dev); 1494329211Shselasky} 1495329211Shselasky 1496290650Shselaskystatic const struct pci_device_id mlx5_core_pci_table[] = { 1497290650Shselasky { PCI_VDEVICE(MELLANOX, 4113) }, /* Connect-IB */ 1498290650Shselasky { PCI_VDEVICE(MELLANOX, 4114) }, /* Connect-IB VF */ 1499290650Shselasky { PCI_VDEVICE(MELLANOX, 4115) }, /* ConnectX-4 */ 1500290650Shselasky { PCI_VDEVICE(MELLANOX, 4116) }, /* ConnectX-4 VF */ 1501290650Shselasky { PCI_VDEVICE(MELLANOX, 4117) }, /* ConnectX-4LX */ 1502290650Shselasky { PCI_VDEVICE(MELLANOX, 4118) }, /* ConnectX-4LX VF */ 1503306233Shselasky { PCI_VDEVICE(MELLANOX, 4119) }, /* ConnectX-5 */ 1504306233Shselasky { PCI_VDEVICE(MELLANOX, 4120) }, /* ConnectX-5 VF */ 1505290650Shselasky { PCI_VDEVICE(MELLANOX, 4121) }, 1506290650Shselasky { PCI_VDEVICE(MELLANOX, 4122) }, 1507290650Shselasky { PCI_VDEVICE(MELLANOX, 4123) }, 1508290650Shselasky { PCI_VDEVICE(MELLANOX, 4124) }, 1509290650Shselasky { PCI_VDEVICE(MELLANOX, 4125) }, 1510290650Shselasky { PCI_VDEVICE(MELLANOX, 4126) }, 1511290650Shselasky { PCI_VDEVICE(MELLANOX, 4127) }, 1512290650Shselasky { PCI_VDEVICE(MELLANOX, 4128) }, 1513290650Shselasky { PCI_VDEVICE(MELLANOX, 4129) }, 1514290650Shselasky { PCI_VDEVICE(MELLANOX, 4130) }, 1515290650Shselasky { PCI_VDEVICE(MELLANOX, 4131) }, 1516290650Shselasky { PCI_VDEVICE(MELLANOX, 4132) }, 1517290650Shselasky { PCI_VDEVICE(MELLANOX, 4133) }, 1518290650Shselasky { PCI_VDEVICE(MELLANOX, 4134) }, 1519290650Shselasky { PCI_VDEVICE(MELLANOX, 4135) }, 1520290650Shselasky { PCI_VDEVICE(MELLANOX, 4136) }, 1521290650Shselasky { PCI_VDEVICE(MELLANOX, 4137) }, 1522290650Shselasky { PCI_VDEVICE(MELLANOX, 4138) }, 1523290650Shselasky { PCI_VDEVICE(MELLANOX, 4139) }, 1524290650Shselasky { PCI_VDEVICE(MELLANOX, 4140) }, 1525290650Shselasky { PCI_VDEVICE(MELLANOX, 4141) }, 1526290650Shselasky { PCI_VDEVICE(MELLANOX, 4142) }, 1527290650Shselasky { PCI_VDEVICE(MELLANOX, 4143) }, 1528290650Shselasky { PCI_VDEVICE(MELLANOX, 4144) }, 1529290650Shselasky { 0, } 1530290650Shselasky}; 1531290650Shselasky 1532290650ShselaskyMODULE_DEVICE_TABLE(pci, mlx5_core_pci_table); 1533290650Shselasky 1534331809Shselaskyvoid mlx5_disable_device(struct mlx5_core_dev *dev) 1535331809Shselasky{ 1536331809Shselasky mlx5_pci_err_detected(dev->pdev, 0); 1537331809Shselasky} 1538331809Shselasky 1539331809Shselaskyvoid mlx5_recover_device(struct mlx5_core_dev *dev) 1540331809Shselasky{ 1541331809Shselasky mlx5_pci_disable_device(dev); 1542331809Shselasky if (mlx5_pci_slot_reset(dev->pdev) == PCI_ERS_RESULT_RECOVERED) 1543331809Shselasky mlx5_pci_resume(dev->pdev); 1544331809Shselasky} 1545331809Shselasky 1546331586Shselaskystruct pci_driver mlx5_core_driver = { 1547290650Shselasky .name = DRIVER_NAME, 1548290650Shselasky .id_table = mlx5_core_pci_table, 1549329211Shselasky .shutdown = shutdown_one, 1550290650Shselasky .probe = init_one, 1551331580Shselasky .remove = remove_one, 1552331580Shselasky .err_handler = &mlx5_err_handler 1553290650Shselasky}; 1554290650Shselasky 1555290650Shselaskystatic int __init init(void) 1556290650Shselasky{ 1557290650Shselasky int err; 1558290650Shselasky 1559290650Shselasky err = pci_register_driver(&mlx5_core_driver); 1560290650Shselasky if (err) 1561331580Shselasky goto err_debug; 1562290650Shselasky 1563331586Shselasky err = mlx5_fwdump_init(); 1564331586Shselasky if (err) 1565331586Shselasky goto err_fwdump; 1566331586Shselasky 1567331586Shselasky return 0; 1568331586Shselasky 1569331586Shselaskyerr_fwdump: 1570331586Shselasky pci_unregister_driver(&mlx5_core_driver); 1571290650Shselasky 1572290650Shselaskyerr_debug: 1573290650Shselasky return err; 1574290650Shselasky} 1575290650Shselasky 1576290650Shselaskystatic void __exit cleanup(void) 1577290650Shselasky{ 1578331586Shselasky mlx5_fwdump_fini(); 1579290650Shselasky pci_unregister_driver(&mlx5_core_driver); 1580290650Shselasky} 1581290650Shselasky 1582290650Shselaskymodule_init(init); 1583290650Shselaskymodule_exit(cleanup); 1584