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