main.c 39.1 KB
Newer Older
1
/*
2
 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

33
#include <linux/highmem.h>
34 35 36 37 38 39 40
#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/pci.h>
#include <linux/dma-mapping.h>
#include <linux/slab.h>
#include <linux/io-mapping.h>
41
#include <linux/interrupt.h>
42
#include <linux/delay.h>
43 44 45 46 47
#include <linux/mlx5/driver.h>
#include <linux/mlx5/cq.h>
#include <linux/mlx5/qp.h>
#include <linux/mlx5/srq.h>
#include <linux/debugfs.h>
48
#include <linux/kmod.h>
49
#include <linux/mlx5/mlx5_ifc.h>
50
#include <linux/mlx5/vport.h>
51 52 53
#ifdef CONFIG_RFS_ACCEL
#include <linux/cpu_rmap.h>
#endif
O
Or Gerlitz 已提交
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
#include "lib/clock.h"
63 64

MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
65
MODULE_DESCRIPTION("Mellanox Connect-IB, ConnectX-4 core driver");
66 67 68
MODULE_LICENSE("Dual BSD/GPL");
MODULE_VERSION(DRIVER_VERSION);

K
Kamal Heib 已提交
69 70
unsigned int mlx5_core_debug_mask;
module_param_named(debug_mask, mlx5_core_debug_mask, uint, 0644);
71 72
MODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");

73
#define MLX5_DEFAULT_PROF	2
K
Kamal Heib 已提交
74 75
static unsigned int prof_sel = MLX5_DEFAULT_PROF;
module_param_named(prof_sel, prof_sel, uint, 0444);
76 77
MODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");

78 79 80 81 82
enum {
	MLX5_ATOMIC_REQ_MODE_BE = 0x0,
	MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS = 0x1,
};

83 84 85 86 87 88 89 90 91 92 93
static struct mlx5_profile profile[] = {
	[0] = {
		.mask           = 0,
	},
	[1] = {
		.mask		= MLX5_PROF_MASK_QP_SIZE,
		.log_max_qp	= 12,
	},
	[2] = {
		.mask		= MLX5_PROF_MASK_QP_SIZE |
				  MLX5_PROF_MASK_MR_CACHE,
94
		.log_max_qp	= 18,
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
		.mr_cache[0]	= {
			.size	= 500,
			.limit	= 250
		},
		.mr_cache[1]	= {
			.size	= 500,
			.limit	= 250
		},
		.mr_cache[2]	= {
			.size	= 500,
			.limit	= 250
		},
		.mr_cache[3]	= {
			.size	= 500,
			.limit	= 250
		},
		.mr_cache[4]	= {
			.size	= 500,
			.limit	= 250
		},
		.mr_cache[5]	= {
			.size	= 500,
			.limit	= 250
		},
		.mr_cache[6]	= {
			.size	= 500,
			.limit	= 250
		},
		.mr_cache[7]	= {
			.size	= 500,
			.limit	= 250
		},
		.mr_cache[8]	= {
			.size	= 500,
			.limit	= 250
		},
		.mr_cache[9]	= {
			.size	= 500,
			.limit	= 250
		},
		.mr_cache[10]	= {
			.size	= 500,
			.limit	= 250
		},
		.mr_cache[11]	= {
			.size	= 500,
			.limit	= 250
		},
		.mr_cache[12]	= {
			.size	= 64,
			.limit	= 32
		},
		.mr_cache[13]	= {
			.size	= 32,
			.limit	= 16
		},
		.mr_cache[14]	= {
			.size	= 16,
			.limit	= 8
		},
		.mr_cache[15]	= {
			.size	= 8,
			.limit	= 4
		},
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
		.mr_cache[16]	= {
			.size	= 8,
			.limit	= 4
		},
		.mr_cache[17]	= {
			.size	= 8,
			.limit	= 4
		},
		.mr_cache[18]	= {
			.size	= 8,
			.limit	= 4
		},
		.mr_cache[19]	= {
			.size	= 4,
			.limit	= 2
		},
		.mr_cache[20]	= {
			.size	= 4,
			.limit	= 2
		},
179 180
	},
};
181

182 183 184
#define FW_INIT_TIMEOUT_MILI		2000
#define FW_INIT_WAIT_MS			2
#define FW_PRE_INIT_TIMEOUT_MILI	10000
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

static int wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili)
{
	unsigned long end = jiffies + msecs_to_jiffies(max_wait_mili);
	int err = 0;

	while (fw_initializing(dev)) {
		if (time_after(jiffies, end)) {
			err = -EBUSY;
			break;
		}
		msleep(FW_INIT_WAIT_MS);
	}

	return err;
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
static void mlx5_set_driver_version(struct mlx5_core_dev *dev)
{
	int driver_ver_sz = MLX5_FLD_SZ_BYTES(set_driver_version_in,
					      driver_version);
	u8 in[MLX5_ST_SZ_BYTES(set_driver_version_in)] = {0};
	u8 out[MLX5_ST_SZ_BYTES(set_driver_version_out)] = {0};
	int remaining_size = driver_ver_sz;
	char *string;

	if (!MLX5_CAP_GEN(dev, driver_version))
		return;

	string = MLX5_ADDR_OF(set_driver_version_in, in, driver_version);

	strncpy(string, "Linux", remaining_size);

	remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
	strncat(string, ",", remaining_size);

	remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
	strncat(string, DRIVER_NAME, remaining_size);

	remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
	strncat(string, ",", remaining_size);

	remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
	strncat(string, DRIVER_VERSION, remaining_size);

	/*Send the command*/
	MLX5_SET(set_driver_version_in, in, opcode,
		 MLX5_CMD_OP_SET_DRIVER_VERSION);

	mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
}

237 238 239 240 241 242
static int set_dma_caps(struct pci_dev *pdev)
{
	int err;

	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
	if (err) {
J
Joe Perches 已提交
243
		dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
244 245
		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
		if (err) {
J
Joe Perches 已提交
246
			dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
247 248 249 250 251 252 253
			return err;
		}
	}

	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
	if (err) {
		dev_warn(&pdev->dev,
J
Joe Perches 已提交
254
			 "Warning: couldn't set 64-bit consistent PCI DMA mask\n");
255 256 257
		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
		if (err) {
			dev_err(&pdev->dev,
J
Joe Perches 已提交
258
				"Can't set consistent PCI DMA mask, aborting\n");
259 260 261 262 263 264 265 266
			return err;
		}
	}

	dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
	return err;
}

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
static int mlx5_pci_enable_device(struct mlx5_core_dev *dev)
{
	struct pci_dev *pdev = dev->pdev;
	int err = 0;

	mutex_lock(&dev->pci_status_mutex);
	if (dev->pci_status == MLX5_PCI_STATUS_DISABLED) {
		err = pci_enable_device(pdev);
		if (!err)
			dev->pci_status = MLX5_PCI_STATUS_ENABLED;
	}
	mutex_unlock(&dev->pci_status_mutex);

	return err;
}

static void mlx5_pci_disable_device(struct mlx5_core_dev *dev)
{
	struct pci_dev *pdev = dev->pdev;

	mutex_lock(&dev->pci_status_mutex);
	if (dev->pci_status == MLX5_PCI_STATUS_ENABLED) {
		pci_disable_device(pdev);
		dev->pci_status = MLX5_PCI_STATUS_DISABLED;
	}
	mutex_unlock(&dev->pci_status_mutex);
}

295 296 297 298 299
static int request_bar(struct pci_dev *pdev)
{
	int err = 0;

	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
J
Joe Perches 已提交
300
		dev_err(&pdev->dev, "Missing registers BAR, aborting\n");
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
		return -ENODEV;
	}

	err = pci_request_regions(pdev, DRIVER_NAME);
	if (err)
		dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");

	return err;
}

static void release_bar(struct pci_dev *pdev)
{
	pci_release_regions(pdev);
}

316
static int mlx5_alloc_irq_vectors(struct mlx5_core_dev *dev)
317
{
318 319
	struct mlx5_priv *priv = &dev->priv;
	struct mlx5_eq_table *table = &priv->eq_table;
320
	int num_eqs = 1 << MLX5_CAP_GEN(dev, log_max_eq);
321 322
	int nvec;

323 324
	nvec = MLX5_CAP_GEN(dev, num_ports) * num_online_cpus() +
	       MLX5_EQ_VEC_COMP_BASE;
325 326 327 328
	nvec = min_t(int, nvec, num_eqs);
	if (nvec <= MLX5_EQ_VEC_COMP_BASE)
		return -ENOMEM;

329
	priv->irq_info = kcalloc(nvec, sizeof(*priv->irq_info), GFP_KERNEL);
330
	if (!priv->irq_info)
331
		goto err_free_msix;
332

333
	nvec = pci_alloc_irq_vectors(dev->pdev,
334
			MLX5_EQ_VEC_COMP_BASE + 1, nvec,
335
			PCI_IRQ_MSIX);
336 337
	if (nvec < 0)
		return nvec;
338

339
	table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
340 341

	return 0;
342 343 344 345

err_free_msix:
	kfree(priv->irq_info);
	return -ENOMEM;
346 347
}

348
static void mlx5_free_irq_vectors(struct mlx5_core_dev *dev)
349
{
350
	struct mlx5_priv *priv = &dev->priv;
351

352
	pci_free_irq_vectors(dev->pdev);
353
	kfree(priv->irq_info);
354 355
}

356
struct mlx5_reg_host_endianness {
357 358 359 360
	u8	he;
	u8      rsvd[15];
};

361 362 363
#define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos))

enum {
364 365
	MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) |
				MLX5_DEV_CAP_FLAG_DCT,
366 367
};

368
static u16 to_fw_pkey_sz(struct mlx5_core_dev *dev, u32 size)
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
{
	switch (size) {
	case 128:
		return 0;
	case 256:
		return 1;
	case 512:
		return 2;
	case 1024:
		return 3;
	case 2048:
		return 4;
	case 4096:
		return 5;
	default:
384
		mlx5_core_warn(dev, "invalid pkey table size %d\n", size);
385 386 387 388
		return 0;
	}
}

389 390 391
static int mlx5_core_get_caps_mode(struct mlx5_core_dev *dev,
				   enum mlx5_cap_type cap_type,
				   enum mlx5_cap_mode cap_mode)
392
{
393 394
	u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
	int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
395 396
	void *out, *hca_caps;
	u16 opmod = (cap_type << 1) | (cap_mode & 0x01);
397 398
	int err;

399 400
	memset(in, 0, sizeof(in));
	out = kzalloc(out_sz, GFP_KERNEL);
401
	if (!out)
402
		return -ENOMEM;
403

404 405 406
	MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
	MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
	err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
407
	if (err) {
408 409 410
		mlx5_core_warn(dev,
			       "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n",
			       cap_type, cap_mode, err);
411 412
		goto query_ex;
	}
413

414 415 416 417
	hca_caps =  MLX5_ADDR_OF(query_hca_cap_out, out, capability);

	switch (cap_mode) {
	case HCA_CAP_OPMOD_GET_MAX:
418
		memcpy(dev->caps.hca_max[cap_type], hca_caps,
419 420 421
		       MLX5_UN_SZ_BYTES(hca_cap_union));
		break;
	case HCA_CAP_OPMOD_GET_CUR:
422
		memcpy(dev->caps.hca_cur[cap_type], hca_caps,
423 424 425 426 427 428 429 430 431
		       MLX5_UN_SZ_BYTES(hca_cap_union));
		break;
	default:
		mlx5_core_warn(dev,
			       "Tried to query dev cap type(%x) with wrong opmode(%x)\n",
			       cap_type, cap_mode);
		err = -EINVAL;
		break;
	}
432 433 434 435 436
query_ex:
	kfree(out);
	return err;
}

437 438 439 440 441 442 443 444 445 446
int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type)
{
	int ret;

	ret = mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_CUR);
	if (ret)
		return ret;
	return mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_MAX);
}

447
static int set_caps(struct mlx5_core_dev *dev, void *in, int in_sz, int opmod)
448
{
449
	u32 out[MLX5_ST_SZ_DW(set_hca_cap_out)] = {0};
450

451
	MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
452
	MLX5_SET(set_hca_cap_in, in, op_mod, opmod << 1);
453
	return mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
454 455
}

456 457 458 459 460 461 462 463 464
static int handle_hca_cap_atomic(struct mlx5_core_dev *dev)
{
	void *set_ctx;
	void *set_hca_cap;
	int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
	int req_endianness;
	int err;

	if (MLX5_CAP_GEN(dev, atomic)) {
465
		err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC);
466 467 468 469 470 471 472 473
		if (err)
			return err;
	} else {
		return 0;
	}

	req_endianness =
		MLX5_CAP_ATOMIC(dev,
474
				supported_atomic_req_8B_endianness_mode_1);
475 476 477 478 479 480 481 482 483 484 485

	if (req_endianness != MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS)
		return 0;

	set_ctx = kzalloc(set_sz, GFP_KERNEL);
	if (!set_ctx)
		return -ENOMEM;

	set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);

	/* Set requestor to host endianness */
486
	MLX5_SET(atomic_caps, set_hca_cap, atomic_req_8B_endianness_mode,
487 488 489 490 491 492 493 494
		 MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS);

	err = set_caps(dev, set_ctx, set_sz, MLX5_SET_HCA_CAP_OP_MOD_ATOMIC);

	kfree(set_ctx);
	return err;
}

495 496
static int handle_hca_cap(struct mlx5_core_dev *dev)
{
497
	void *set_ctx = NULL;
498 499
	struct mlx5_profile *prof = dev->profile;
	int err = -ENOMEM;
500
	int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
501
	void *set_hca_cap;
502

503
	set_ctx = kzalloc(set_sz, GFP_KERNEL);
504
	if (!set_ctx)
505 506
		goto query_ex;

507
	err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL);
508 509 510
	if (err)
		goto query_ex;

511 512
	set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
				   capability);
513
	memcpy(set_hca_cap, dev->caps.hca_cur[MLX5_CAP_GENERAL],
514 515 516
	       MLX5_ST_SZ_BYTES(cmd_hca_cap));

	mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
517
		      mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
518
		      128);
519
	/* we limit the size of the pkey table to 128 entries for now */
520
	MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
521
		 to_fw_pkey_sz(dev, 128));
522

523 524 525 526 527 528 529
	/* Check log_max_qp from HCA caps to set in current profile */
	if (MLX5_CAP_GEN_MAX(dev, log_max_qp) < profile[prof_sel].log_max_qp) {
		mlx5_core_warn(dev, "log_max_qp value in current profile is %d, changing it to HCA capability limit (%d)\n",
			       profile[prof_sel].log_max_qp,
			       MLX5_CAP_GEN_MAX(dev, log_max_qp));
		profile[prof_sel].log_max_qp = MLX5_CAP_GEN_MAX(dev, log_max_qp);
	}
530
	if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
531 532
		MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
			 prof->log_max_qp);
533

534 535
	/* disable cmdif checksum */
	MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
536

537 538 539 540
	/* Enable 4K UAR only when HCA supports it and page size is bigger
	 * than 4K.
	 */
	if (MLX5_CAP_GEN_MAX(dev, uar_4k) && PAGE_SIZE > 4096)
541 542
		MLX5_SET(cmd_hca_cap, set_hca_cap, uar_4k, 1);

543 544
	MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12);

545 546 547 548 549 550
	if (MLX5_CAP_GEN_MAX(dev, cache_line_128byte))
		MLX5_SET(cmd_hca_cap,
			 set_hca_cap,
			 cache_line_128byte,
			 cache_line_size() == 128 ? 1 : 0);

551 552
	err = set_caps(dev, set_ctx, set_sz,
		       MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE);
553

554 555 556 557 558 559 560
query_ex:
	kfree(set_ctx);
	return err;
}

static int set_hca_ctrl(struct mlx5_core_dev *dev)
{
561 562
	struct mlx5_reg_host_endianness he_in;
	struct mlx5_reg_host_endianness he_out;
563 564
	int err;

E
Eli Cohen 已提交
565 566 567
	if (!mlx5_core_is_pf(dev))
		return 0;

568 569 570 571 572 573 574 575
	memset(&he_in, 0, sizeof(he_in));
	he_in.he = MLX5_SET_HOST_ENDIANNESS;
	err = mlx5_core_access_reg(dev, &he_in,  sizeof(he_in),
					&he_out, sizeof(he_out),
					MLX5_REG_HOST_ENDIANNESS, 0, 1);
	return err;
}

576 577 578 579 580
static int mlx5_core_set_hca_defaults(struct mlx5_core_dev *dev)
{
	int ret = 0;

	/* Disable local_lb by default */
581
	if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH)
582 583 584 585 586
		ret = mlx5_nic_vport_update_local_lb(dev, false);

	return ret;
}

587
int mlx5_core_enable_hca(struct mlx5_core_dev *dev, u16 func_id)
588
{
589 590
	u32 out[MLX5_ST_SZ_DW(enable_hca_out)] = {0};
	u32 in[MLX5_ST_SZ_DW(enable_hca_in)]   = {0};
591

592 593
	MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA);
	MLX5_SET(enable_hca_in, in, function_id, func_id);
594
	return mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
595 596
}

597
int mlx5_core_disable_hca(struct mlx5_core_dev *dev, u16 func_id)
598
{
599 600
	u32 out[MLX5_ST_SZ_DW(disable_hca_out)] = {0};
	u32 in[MLX5_ST_SZ_DW(disable_hca_in)]   = {0};
601

602 603
	MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA);
	MLX5_SET(disable_hca_in, in, function_id, func_id);
604
	return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
605 606
}

607
u64 mlx5_read_internal_timer(struct mlx5_core_dev *dev)
608 609 610 611 612 613 614 615 616
{
	u32 timer_h, timer_h1, timer_l;

	timer_h = ioread32be(&dev->iseg->internal_timer_h);
	timer_l = ioread32be(&dev->iseg->internal_timer_l);
	timer_h1 = ioread32be(&dev->iseg->internal_timer_h);
	if (timer_h != timer_h1) /* wrap around */
		timer_l = ioread32be(&dev->iseg->internal_timer_l);

617
	return (u64)timer_l | (u64)timer_h1 << 32;
618 619
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
static int mlx5_irq_set_affinity_hint(struct mlx5_core_dev *mdev, int i)
{
	struct mlx5_priv *priv  = &mdev->priv;
	int irq = pci_irq_vector(mdev->pdev, MLX5_EQ_VEC_COMP_BASE + i);

	if (!zalloc_cpumask_var(&priv->irq_info[i].mask, GFP_KERNEL)) {
		mlx5_core_warn(mdev, "zalloc_cpumask_var failed");
		return -ENOMEM;
	}

	cpumask_set_cpu(cpumask_local_spread(i, priv->numa_node),
			priv->irq_info[i].mask);

	if (IS_ENABLED(CONFIG_SMP) &&
	    irq_set_affinity_hint(irq, priv->irq_info[i].mask))
		mlx5_core_warn(mdev, "irq_set_affinity_hint failed, irq 0x%.4x", irq);

	return 0;
}

static void mlx5_irq_clear_affinity_hint(struct mlx5_core_dev *mdev, int i)
{
	struct mlx5_priv *priv  = &mdev->priv;
	int irq = pci_irq_vector(mdev->pdev, MLX5_EQ_VEC_COMP_BASE + i);

	irq_set_affinity_hint(irq, NULL);
	free_cpumask_var(priv->irq_info[i].mask);
}

static int mlx5_irq_set_affinity_hints(struct mlx5_core_dev *mdev)
{
	int err;
	int i;

	for (i = 0; i < mdev->priv.eq_table.num_comp_vectors; i++) {
		err = mlx5_irq_set_affinity_hint(mdev, i);
		if (err)
			goto err_out;
	}

	return 0;

err_out:
	for (i--; i >= 0; i--)
		mlx5_irq_clear_affinity_hint(mdev, i);

	return err;
}

static void mlx5_irq_clear_affinity_hints(struct mlx5_core_dev *mdev)
{
	int i;

	for (i = 0; i < mdev->priv.eq_table.num_comp_vectors; i++)
		mlx5_irq_clear_affinity_hint(mdev, i);
}

677 678
int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn,
		    unsigned int *irqn)
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
{
	struct mlx5_eq_table *table = &dev->priv.eq_table;
	struct mlx5_eq *eq, *n;
	int err = -ENOENT;

	spin_lock(&table->lock);
	list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
		if (eq->index == vector) {
			*eqn = eq->eqn;
			*irqn = eq->irqn;
			err = 0;
			break;
		}
	}
	spin_unlock(&table->lock);

	return err;
}
EXPORT_SYMBOL(mlx5_vector2eqn);

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
struct mlx5_eq *mlx5_eqn2eq(struct mlx5_core_dev *dev, int eqn)
{
	struct mlx5_eq_table *table = &dev->priv.eq_table;
	struct mlx5_eq *eq;

	spin_lock(&table->lock);
	list_for_each_entry(eq, &table->comp_eqs_list, list)
		if (eq->eqn == eqn) {
			spin_unlock(&table->lock);
			return eq;
		}

	spin_unlock(&table->lock);

	return ERR_PTR(-ENOENT);
}

716 717 718 719 720
static void free_comp_eqs(struct mlx5_core_dev *dev)
{
	struct mlx5_eq_table *table = &dev->priv.eq_table;
	struct mlx5_eq *eq, *n;

721 722 723 724 725 726
#ifdef CONFIG_RFS_ACCEL
	if (dev->rmap) {
		free_irq_cpu_rmap(dev->rmap);
		dev->rmap = NULL;
	}
#endif
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
	spin_lock(&table->lock);
	list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
		list_del(&eq->list);
		spin_unlock(&table->lock);
		if (mlx5_destroy_unmap_eq(dev, eq))
			mlx5_core_warn(dev, "failed to destroy EQ 0x%x\n",
				       eq->eqn);
		kfree(eq);
		spin_lock(&table->lock);
	}
	spin_unlock(&table->lock);
}

static int alloc_comp_eqs(struct mlx5_core_dev *dev)
{
	struct mlx5_eq_table *table = &dev->priv.eq_table;
743
	char name[MLX5_MAX_IRQ_NAME];
744 745 746 747 748 749 750 751 752
	struct mlx5_eq *eq;
	int ncomp_vec;
	int nent;
	int err;
	int i;

	INIT_LIST_HEAD(&table->comp_eqs_list);
	ncomp_vec = table->num_comp_vectors;
	nent = MLX5_COMP_EQ_SIZE;
753 754 755 756 757
#ifdef CONFIG_RFS_ACCEL
	dev->rmap = alloc_irq_cpu_rmap(ncomp_vec);
	if (!dev->rmap)
		return -ENOMEM;
#endif
758 759 760 761 762 763 764
	for (i = 0; i < ncomp_vec; i++) {
		eq = kzalloc(sizeof(*eq), GFP_KERNEL);
		if (!eq) {
			err = -ENOMEM;
			goto clean;
		}

765
#ifdef CONFIG_RFS_ACCEL
766 767
		irq_cpu_rmap_add(dev->rmap, pci_irq_vector(dev->pdev,
				 MLX5_EQ_VEC_COMP_BASE + i));
768
#endif
769
		snprintf(name, MLX5_MAX_IRQ_NAME, "mlx5_comp%d", i);
770 771
		err = mlx5_create_map_eq(dev, eq,
					 i + MLX5_EQ_VEC_COMP_BASE, nent, 0,
772
					 name, MLX5_EQ_TYPE_COMP);
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
		if (err) {
			kfree(eq);
			goto clean;
		}
		mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->eqn);
		eq->index = i;
		spin_lock(&table->lock);
		list_add_tail(&eq->list, &table->comp_eqs_list);
		spin_unlock(&table->lock);
	}

	return 0;

clean:
	free_comp_eqs(dev);
	return err;
}

791 792
static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
{
793 794
	u32 query_in[MLX5_ST_SZ_DW(query_issi_in)]   = {0};
	u32 query_out[MLX5_ST_SZ_DW(query_issi_out)] = {0};
795
	u32 sup_issi;
796
	int err;
797 798

	MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
799 800
	err = mlx5_cmd_exec(dev, query_in, sizeof(query_in),
			    query_out, sizeof(query_out));
801
	if (err) {
802 803 804 805
		u32 syndrome;
		u8 status;

		mlx5_cmd_mbox_status(query_out, &status, &syndrome);
K
Kamal Heib 已提交
806 807 808 809
		if (!status || syndrome == MLX5_DRIVER_SYND) {
			mlx5_core_err(dev, "Failed to query ISSI err(%d) status(%d) synd(%d)\n",
				      err, status, syndrome);
			return err;
810 811
		}

K
Kamal Heib 已提交
812 813 814
		mlx5_core_warn(dev, "Query ISSI is not supported by FW, ISSI is 0\n");
		dev->issi = 0;
		return 0;
815 816 817 818 819
	}

	sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);

	if (sup_issi & (1 << 1)) {
820 821
		u32 set_in[MLX5_ST_SZ_DW(set_issi_in)]   = {0};
		u32 set_out[MLX5_ST_SZ_DW(set_issi_out)] = {0};
822 823 824

		MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
		MLX5_SET(set_issi_in, set_in, current_issi, 1);
825 826
		err = mlx5_cmd_exec(dev, set_in, sizeof(set_in),
				    set_out, sizeof(set_out));
827
		if (err) {
K
Kamal Heib 已提交
828 829
			mlx5_core_err(dev, "Failed to set ISSI to 1 err(%d)\n",
				      err);
830 831 832 833 834 835
			return err;
		}

		dev->issi = 1;

		return 0;
836
	} else if (sup_issi & (1 << 0) || !sup_issi) {
837 838 839
		return 0;
	}

840
	return -EOPNOTSUPP;
841 842
}

843 844 845 846
static int mlx5_pci_init(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
{
	struct pci_dev *pdev = dev->pdev;
	int err = 0;
847 848 849 850 851 852 853 854 855

	pci_set_drvdata(dev->pdev, dev);
	strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
	priv->name[MLX5_MAX_NAME_LEN - 1] = 0;

	mutex_init(&priv->pgdir_mutex);
	INIT_LIST_HEAD(&priv->pgdir_list);
	spin_lock_init(&priv->mkey_lock);

856 857 858 859
	mutex_init(&priv->alloc_mutex);

	priv->numa_node = dev_to_node(&dev->pdev->dev);

860 861 862 863
	priv->dbg_root = debugfs_create_dir(dev_name(&pdev->dev), mlx5_debugfs_root);
	if (!priv->dbg_root)
		return -ENOMEM;

864
	err = mlx5_pci_enable_device(dev);
865
	if (err) {
J
Joe Perches 已提交
866
		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
867 868 869 870 871
		goto err_dbg;
	}

	err = request_bar(pdev);
	if (err) {
J
Joe Perches 已提交
872
		dev_err(&pdev->dev, "error requesting BARs, aborting\n");
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
		goto err_disable;
	}

	pci_set_master(pdev);

	err = set_dma_caps(pdev);
	if (err) {
		dev_err(&pdev->dev, "Failed setting DMA capabilities mask, aborting\n");
		goto err_clr_master;
	}

	dev->iseg_base = pci_resource_start(dev->pdev, 0);
	dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
	if (!dev->iseg) {
		err = -ENOMEM;
		dev_err(&pdev->dev, "Failed mapping initialization segment, aborting\n");
		goto err_clr_master;
	}
891 892 893 894 895 896 897

	return 0;

err_clr_master:
	pci_clear_master(dev->pdev);
	release_bar(dev->pdev);
err_disable:
898
	mlx5_pci_disable_device(dev);
899 900 901 902 903 904 905 906 907 908 909

err_dbg:
	debugfs_remove(priv->dbg_root);
	return err;
}

static void mlx5_pci_close(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
{
	iounmap(dev->iseg);
	pci_clear_master(dev->pdev);
	release_bar(dev->pdev);
910
	mlx5_pci_disable_device(dev);
911 912 913
	debugfs_remove(priv->dbg_root);
}

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
static int mlx5_init_once(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
{
	struct pci_dev *pdev = dev->pdev;
	int err;

	err = mlx5_query_board_id(dev);
	if (err) {
		dev_err(&pdev->dev, "query board id failed\n");
		goto out;
	}

	err = mlx5_eq_init(dev);
	if (err) {
		dev_err(&pdev->dev, "failed to initialize eq\n");
		goto out;
	}

	err = mlx5_init_cq_table(dev);
	if (err) {
		dev_err(&pdev->dev, "failed to initialize cq table\n");
		goto err_eq_cleanup;
	}

	mlx5_init_qp_table(dev);

	mlx5_init_srq_table(dev);

	mlx5_init_mkey_table(dev);

943 944
	mlx5_init_reserved_gids(dev);

945 946
	mlx5_init_clock(dev);

947 948 949 950 951 952
	err = mlx5_init_rl_table(dev);
	if (err) {
		dev_err(&pdev->dev, "Failed to init rate limiting\n");
		goto err_tables_cleanup;
	}

953 954 955 956 957 958
	err = mlx5_mpfs_init(dev);
	if (err) {
		dev_err(&pdev->dev, "Failed to init l2 table %d\n", err);
		goto err_rl_cleanup;
	}

959 960 961
	err = mlx5_eswitch_init(dev);
	if (err) {
		dev_err(&pdev->dev, "Failed to init eswitch %d\n", err);
962
		goto err_mpfs_cleanup;
963 964 965 966 967 968 969 970
	}

	err = mlx5_sriov_init(dev);
	if (err) {
		dev_err(&pdev->dev, "Failed to init sriov %d\n", err);
		goto err_eswitch_cleanup;
	}

971 972 973 974 975 976
	err = mlx5_fpga_init(dev);
	if (err) {
		dev_err(&pdev->dev, "Failed to init fpga device %d\n", err);
		goto err_sriov_cleanup;
	}

977 978
	return 0;

979 980
err_sriov_cleanup:
	mlx5_sriov_cleanup(dev);
981 982
err_eswitch_cleanup:
	mlx5_eswitch_cleanup(dev->priv.eswitch);
983 984
err_mpfs_cleanup:
	mlx5_mpfs_cleanup(dev);
985 986
err_rl_cleanup:
	mlx5_cleanup_rl_table(dev);
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
err_tables_cleanup:
	mlx5_cleanup_mkey_table(dev);
	mlx5_cleanup_srq_table(dev);
	mlx5_cleanup_qp_table(dev);
	mlx5_cleanup_cq_table(dev);

err_eq_cleanup:
	mlx5_eq_cleanup(dev);

out:
	return err;
}

static void mlx5_cleanup_once(struct mlx5_core_dev *dev)
{
1002
	mlx5_fpga_cleanup(dev);
1003 1004
	mlx5_sriov_cleanup(dev);
	mlx5_eswitch_cleanup(dev->priv.eswitch);
1005
	mlx5_mpfs_cleanup(dev);
1006
	mlx5_cleanup_rl_table(dev);
1007
	mlx5_cleanup_clock(dev);
1008
	mlx5_cleanup_reserved_gids(dev);
1009 1010 1011 1012 1013 1014 1015 1016 1017
	mlx5_cleanup_mkey_table(dev);
	mlx5_cleanup_srq_table(dev);
	mlx5_cleanup_qp_table(dev);
	mlx5_cleanup_cq_table(dev);
	mlx5_eq_cleanup(dev);
}

static int mlx5_load_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
			 bool boot)
1018 1019 1020 1021
{
	struct pci_dev *pdev = dev->pdev;
	int err;

1022
	mutex_lock(&dev->intf_state_mutex);
1023
	if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1024 1025 1026 1027 1028
		dev_warn(&dev->pdev->dev, "%s: interface is up, NOP\n",
			 __func__);
		goto out;
	}

1029 1030 1031
	dev_info(&pdev->dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
		 fw_rev_min(dev), fw_rev_sub(dev));

1032 1033 1034 1035 1036
	/* on load removing any previous indication of internal error, device is
	 * up
	 */
	dev->state = MLX5_DEVICE_STATE_UP;

1037 1038 1039 1040 1041 1042
	/* wait for firmware to accept initialization segments configurations
	 */
	err = wait_fw_init(dev, FW_PRE_INIT_TIMEOUT_MILI);
	if (err) {
		dev_err(&dev->pdev->dev, "Firmware over %d MS in pre-initializing state, aborting\n",
			FW_PRE_INIT_TIMEOUT_MILI);
1043
		goto out_err;
1044 1045
	}

1046 1047 1048
	err = mlx5_cmd_init(dev);
	if (err) {
		dev_err(&pdev->dev, "Failed initializing command interface, aborting\n");
1049
		goto out_err;
1050 1051
	}

1052 1053 1054 1055
	err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI);
	if (err) {
		dev_err(&dev->pdev->dev, "Firmware over %d MS in initializing state, aborting\n",
			FW_INIT_TIMEOUT_MILI);
1056
		goto err_cmd_cleanup;
1057 1058
	}

1059
	err = mlx5_core_enable_hca(dev, 0);
1060 1061
	if (err) {
		dev_err(&pdev->dev, "enable hca failed\n");
1062
		goto err_cmd_cleanup;
1063 1064
	}

1065 1066 1067 1068 1069 1070
	err = mlx5_core_set_issi(dev);
	if (err) {
		dev_err(&pdev->dev, "failed to set issi\n");
		goto err_disable_hca;
	}

1071 1072 1073 1074 1075 1076
	err = mlx5_satisfy_startup_pages(dev, 1);
	if (err) {
		dev_err(&pdev->dev, "failed to allocate boot pages\n");
		goto err_disable_hca;
	}

1077 1078 1079
	err = set_hca_ctrl(dev);
	if (err) {
		dev_err(&pdev->dev, "set_hca_ctrl failed\n");
1080
		goto reclaim_boot_pages;
1081 1082 1083 1084 1085
	}

	err = handle_hca_cap(dev);
	if (err) {
		dev_err(&pdev->dev, "handle_hca_cap failed\n");
1086
		goto reclaim_boot_pages;
1087 1088
	}

1089 1090 1091 1092
	err = handle_hca_cap_atomic(dev);
	if (err) {
		dev_err(&pdev->dev, "handle_hca_cap_atomic failed\n");
		goto reclaim_boot_pages;
1093 1094
	}

1095
	err = mlx5_satisfy_startup_pages(dev, 0);
1096
	if (err) {
1097 1098
		dev_err(&pdev->dev, "failed to allocate init pages\n");
		goto reclaim_boot_pages;
1099 1100 1101 1102 1103
	}

	err = mlx5_pagealloc_start(dev);
	if (err) {
		dev_err(&pdev->dev, "mlx5_pagealloc_start failed\n");
1104
		goto reclaim_boot_pages;
1105 1106 1107 1108 1109 1110 1111 1112
	}

	err = mlx5_cmd_init_hca(dev);
	if (err) {
		dev_err(&pdev->dev, "init hca failed\n");
		goto err_pagealloc_stop;
	}

1113 1114
	mlx5_set_driver_version(dev);

1115 1116
	mlx5_start_health_poll(dev);

1117 1118 1119 1120 1121 1122
	err = mlx5_query_hca_caps(dev);
	if (err) {
		dev_err(&pdev->dev, "query hca failed\n");
		goto err_stop_poll;
	}

1123 1124
	if (boot && mlx5_init_once(dev, priv)) {
		dev_err(&pdev->dev, "sw objs init failed\n");
1125 1126 1127
		goto err_stop_poll;
	}

1128
	err = mlx5_alloc_irq_vectors(dev);
1129
	if (err) {
1130
		dev_err(&pdev->dev, "alloc irq vectors failed\n");
1131
		goto err_cleanup_once;
1132 1133
	}

1134 1135
	dev->priv.uar = mlx5_get_uars_page(dev);
	if (!dev->priv.uar) {
1136
		dev_err(&pdev->dev, "Failed allocating uar, aborting\n");
1137
		goto err_disable_msix;
1138 1139 1140 1141 1142
	}

	err = mlx5_start_eqs(dev);
	if (err) {
		dev_err(&pdev->dev, "Failed to start pages and async EQs\n");
1143
		goto err_put_uars;
1144 1145
	}

1146 1147 1148 1149 1150 1151
	err = alloc_comp_eqs(dev);
	if (err) {
		dev_err(&pdev->dev, "Failed to alloc completion EQs\n");
		goto err_stop_eqs;
	}

1152 1153 1154 1155 1156 1157
	err = mlx5_irq_set_affinity_hints(dev);
	if (err) {
		dev_err(&pdev->dev, "Failed to alloc affinity hint cpumask\n");
		goto err_affinity_hints;
	}

1158
	err = mlx5_init_fs(dev);
1159
	if (err) {
1160
		dev_err(&pdev->dev, "Failed to init flow steering\n");
1161
		goto err_fs;
1162
	}
1163

1164
	err = mlx5_core_set_hca_defaults(dev);
1165
	if (err) {
1166
		dev_err(&pdev->dev, "Failed to set hca defaults\n");
1167 1168
		goto err_fs;
	}
1169

1170
	err = mlx5_sriov_attach(dev);
E
Eli Cohen 已提交
1171 1172 1173 1174 1175
	if (err) {
		dev_err(&pdev->dev, "sriov init failed %d\n", err);
		goto err_sriov;
	}

1176 1177 1178
	err = mlx5_fpga_device_start(dev);
	if (err) {
		dev_err(&pdev->dev, "fpga device start failed %d\n", err);
1179
		goto err_fpga_start;
1180
	}
1181 1182 1183 1184 1185
	err = mlx5_accel_ipsec_init(dev);
	if (err) {
		dev_err(&pdev->dev, "IPSec device start failed %d\n", err);
		goto err_ipsec_start;
	}
1186

1187 1188 1189 1190 1191 1192 1193 1194
	if (mlx5_device_registered(dev)) {
		mlx5_attach_device(dev);
	} else {
		err = mlx5_register_device(dev);
		if (err) {
			dev_err(&pdev->dev, "mlx5_register_device failed %d\n", err);
			goto err_reg_dev;
		}
1195 1196
	}

1197
	set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1198 1199 1200
out:
	mutex_unlock(&dev->intf_state_mutex);

1201 1202
	return 0;

1203
err_reg_dev:
1204 1205
	mlx5_accel_ipsec_cleanup(dev);
err_ipsec_start:
1206 1207 1208
	mlx5_fpga_device_stop(dev);

err_fpga_start:
1209
	mlx5_sriov_detach(dev);
E
Eli Cohen 已提交
1210

1211
err_sriov:
1212
	mlx5_cleanup_fs(dev);
1213

1214
err_fs:
1215 1216 1217
	mlx5_irq_clear_affinity_hints(dev);

err_affinity_hints:
1218 1219
	free_comp_eqs(dev);

1220 1221 1222
err_stop_eqs:
	mlx5_stop_eqs(dev);

1223
err_put_uars:
1224
	mlx5_put_uars_page(dev, priv->uar);
1225

1226
err_disable_msix:
1227
	mlx5_free_irq_vectors(dev);
1228

1229 1230 1231 1232
err_cleanup_once:
	if (boot)
		mlx5_cleanup_once(dev);

1233 1234
err_stop_poll:
	mlx5_stop_health_poll(dev);
1235 1236
	if (mlx5_cmd_teardown_hca(dev)) {
		dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1237
		goto out_err;
1238
	}
1239 1240 1241 1242

err_pagealloc_stop:
	mlx5_pagealloc_stop(dev);

1243
reclaim_boot_pages:
1244 1245
	mlx5_reclaim_startup_pages(dev);

1246
err_disable_hca:
1247
	mlx5_core_disable_hca(dev, 0);
1248

1249
err_cmd_cleanup:
1250 1251
	mlx5_cmd_cleanup(dev);

1252 1253 1254 1255
out_err:
	dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
	mutex_unlock(&dev->intf_state_mutex);

1256 1257 1258
	return err;
}

1259 1260
static int mlx5_unload_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
			   bool cleanup)
1261
{
1262
	int err = 0;
1263

1264
	if (cleanup)
1265
		mlx5_drain_health_recovery(dev);
1266

1267
	mutex_lock(&dev->intf_state_mutex);
1268
	if (!test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1269 1270
		dev_warn(&dev->pdev->dev, "%s: interface is down, NOP\n",
			 __func__);
1271 1272
		if (cleanup)
			mlx5_cleanup_once(dev);
1273 1274
		goto out;
	}
1275

1276 1277
	clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);

1278 1279 1280
	if (mlx5_device_registered(dev))
		mlx5_detach_device(dev);

1281
	mlx5_accel_ipsec_cleanup(dev);
1282 1283
	mlx5_fpga_device_stop(dev);

1284
	mlx5_sriov_detach(dev);
1285
	mlx5_cleanup_fs(dev);
1286
	mlx5_irq_clear_affinity_hints(dev);
1287
	free_comp_eqs(dev);
1288
	mlx5_stop_eqs(dev);
1289
	mlx5_put_uars_page(dev, priv->uar);
1290
	mlx5_free_irq_vectors(dev);
1291 1292
	if (cleanup)
		mlx5_cleanup_once(dev);
1293
	mlx5_stop_health_poll(dev);
1294 1295
	err = mlx5_cmd_teardown_hca(dev);
	if (err) {
1296
		dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1297
		goto out;
1298
	}
1299 1300
	mlx5_pagealloc_stop(dev);
	mlx5_reclaim_startup_pages(dev);
1301
	mlx5_core_disable_hca(dev, 0);
1302
	mlx5_cmd_cleanup(dev);
1303

1304
out:
1305
	mutex_unlock(&dev->intf_state_mutex);
1306
	return err;
1307
}
1308

1309 1310 1311 1312 1313 1314
struct mlx5_core_event_handler {
	void (*event)(struct mlx5_core_dev *dev,
		      enum mlx5_dev_event event,
		      void *data);
};

O
Or Gerlitz 已提交
1315
static const struct devlink_ops mlx5_devlink_ops = {
1316
#ifdef CONFIG_MLX5_ESWITCH
O
Or Gerlitz 已提交
1317 1318
	.eswitch_mode_set = mlx5_devlink_eswitch_mode_set,
	.eswitch_mode_get = mlx5_devlink_eswitch_mode_get,
1319 1320
	.eswitch_inline_mode_set = mlx5_devlink_eswitch_inline_mode_set,
	.eswitch_inline_mode_get = mlx5_devlink_eswitch_inline_mode_get,
1321 1322
	.eswitch_encap_mode_set = mlx5_devlink_eswitch_encap_mode_set,
	.eswitch_encap_mode_get = mlx5_devlink_eswitch_encap_mode_get,
O
Or Gerlitz 已提交
1323 1324
#endif
};
1325

1326
#define MLX5_IB_MOD "mlx5_ib"
1327 1328 1329 1330
static int init_one(struct pci_dev *pdev,
		    const struct pci_device_id *id)
{
	struct mlx5_core_dev *dev;
O
Or Gerlitz 已提交
1331
	struct devlink *devlink;
1332 1333 1334
	struct mlx5_priv *priv;
	int err;

O
Or Gerlitz 已提交
1335 1336
	devlink = devlink_alloc(&mlx5_devlink_ops, sizeof(*dev));
	if (!devlink) {
1337 1338 1339
		dev_err(&pdev->dev, "kzalloc failed\n");
		return -ENOMEM;
	}
O
Or Gerlitz 已提交
1340 1341

	dev = devlink_priv(devlink);
1342
	priv = &dev->priv;
E
Eli Cohen 已提交
1343
	priv->pci_dev_data = id->driver_data;
1344 1345 1346

	pci_set_drvdata(pdev, dev);

1347 1348
	dev->pdev = pdev;
	dev->event = mlx5_core_event;
1349 1350
	dev->profile = &profile[prof_sel];

E
Eli Cohen 已提交
1351 1352
	INIT_LIST_HEAD(&priv->ctx_list);
	spin_lock_init(&priv->ctx_lock);
1353 1354
	mutex_init(&dev->pci_status_mutex);
	mutex_init(&dev->intf_state_mutex);
1355

1356 1357 1358
	INIT_LIST_HEAD(&priv->waiting_events_list);
	priv->is_accum_events = false;

1359 1360 1361 1362 1363 1364 1365 1366
#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
	err = init_srcu_struct(&priv->pfault_srcu);
	if (err) {
		dev_err(&pdev->dev, "init_srcu_struct failed with error code %d\n",
			err);
		goto clean_dev;
	}
#endif
1367 1368 1369 1370 1371
	mutex_init(&priv->bfregs.reg_head.lock);
	mutex_init(&priv->bfregs.wc_head.lock);
	INIT_LIST_HEAD(&priv->bfregs.reg_head.list);
	INIT_LIST_HEAD(&priv->bfregs.wc_head.list);

1372
	err = mlx5_pci_init(dev, priv);
1373
	if (err) {
1374
		dev_err(&pdev->dev, "mlx5_pci_init failed with error code %d\n", err);
1375
		goto clean_srcu;
1376 1377
	}

1378 1379 1380 1381 1382 1383
	err = mlx5_health_init(dev);
	if (err) {
		dev_err(&pdev->dev, "mlx5_health_init failed with error code %d\n", err);
		goto close_pci;
	}

1384 1385 1386
	mlx5_pagealloc_init(dev);

	err = mlx5_load_one(dev, priv, true);
1387
	if (err) {
1388
		dev_err(&pdev->dev, "mlx5_load_one failed with error code %d\n", err);
1389
		goto clean_health;
1390
	}
1391

1392
	request_module_nowait(MLX5_IB_MOD);
1393

O
Or Gerlitz 已提交
1394 1395 1396 1397
	err = devlink_register(devlink, &pdev->dev);
	if (err)
		goto clean_load;

1398
	pci_save_state(pdev);
1399 1400
	return 0;

O
Or Gerlitz 已提交
1401
clean_load:
1402
	mlx5_unload_one(dev, priv, true);
1403
clean_health:
1404
	mlx5_pagealloc_cleanup(dev);
1405
	mlx5_health_cleanup(dev);
1406 1407
close_pci:
	mlx5_pci_close(dev, priv);
1408 1409 1410
clean_srcu:
#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
	cleanup_srcu_struct(&priv->pfault_srcu);
1411
clean_dev:
1412
#endif
O
Or Gerlitz 已提交
1413
	devlink_free(devlink);
1414

1415 1416
	return err;
}
1417

1418 1419 1420
static void remove_one(struct pci_dev *pdev)
{
	struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
O
Or Gerlitz 已提交
1421
	struct devlink *devlink = priv_to_devlink(dev);
1422
	struct mlx5_priv *priv = &dev->priv;
1423

O
Or Gerlitz 已提交
1424
	devlink_unregister(devlink);
1425 1426
	mlx5_unregister_device(dev);

1427
	if (mlx5_unload_one(dev, priv, true)) {
1428
		dev_err(&dev->pdev->dev, "mlx5_unload_one failed\n");
1429
		mlx5_health_cleanup(dev);
1430 1431
		return;
	}
1432

1433
	mlx5_pagealloc_cleanup(dev);
1434
	mlx5_health_cleanup(dev);
1435
	mlx5_pci_close(dev, priv);
1436 1437 1438
#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
	cleanup_srcu_struct(&priv->pfault_srcu);
#endif
O
Or Gerlitz 已提交
1439
	devlink_free(devlink);
1440 1441
}

1442 1443 1444 1445 1446 1447 1448
static pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev,
					      pci_channel_state_t state)
{
	struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
	struct mlx5_priv *priv = &dev->priv;

	dev_info(&pdev->dev, "%s was called\n", __func__);
1449

1450
	mlx5_enter_error_state(dev, false);
1451
	mlx5_unload_one(dev, priv, false);
1452
	/* In case of kernel call drain the health wq */
1453
	if (state) {
1454
		mlx5_drain_health_wq(dev);
1455 1456 1457
		mlx5_pci_disable_device(dev);
	}

1458 1459 1460 1461
	return state == pci_channel_io_perm_failure ?
		PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
}

1462 1463
/* wait for the device to show vital signs by waiting
 * for the health counter to start counting.
1464
 */
1465
static int wait_vital(struct pci_dev *pdev)
1466 1467 1468 1469
{
	struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
	struct mlx5_core_health *health = &dev->priv.health;
	const int niter = 100;
1470
	u32 last_count = 0;
1471 1472 1473 1474 1475 1476
	u32 count;
	int i;

	for (i = 0; i < niter; i++) {
		count = ioread32be(health->health_counter);
		if (count && count != 0xffffffff) {
1477 1478 1479 1480 1481
			if (last_count && last_count != count) {
				dev_info(&pdev->dev, "Counter value 0x%x after %d iterations\n", count, i);
				return 0;
			}
			last_count = count;
1482 1483 1484 1485
		}
		msleep(50);
	}

1486
	return -ETIMEDOUT;
1487 1488
}

1489
static pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev)
1490 1491 1492 1493 1494 1495
{
	struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
	int err;

	dev_info(&pdev->dev, "%s was called\n", __func__);

1496
	err = mlx5_pci_enable_device(dev);
1497
	if (err) {
1498 1499 1500 1501 1502 1503 1504
		dev_err(&pdev->dev, "%s: mlx5_pci_enable_device failed with error code: %d\n"
			, __func__, err);
		return PCI_ERS_RESULT_DISCONNECT;
	}

	pci_set_master(pdev);
	pci_restore_state(pdev);
1505
	pci_save_state(pdev);
1506 1507

	if (wait_vital(pdev)) {
1508
		dev_err(&pdev->dev, "%s: wait_vital timed out\n", __func__);
1509
		return PCI_ERS_RESULT_DISCONNECT;
1510
	}
1511

1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
	return PCI_ERS_RESULT_RECOVERED;
}

static void mlx5_pci_resume(struct pci_dev *pdev)
{
	struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
	struct mlx5_priv *priv = &dev->priv;
	int err;

	dev_info(&pdev->dev, "%s was called\n", __func__);

1523
	err = mlx5_load_one(dev, priv, false);
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
	if (err)
		dev_err(&pdev->dev, "%s: mlx5_load_one failed with error code: %d\n"
			, __func__, err);
	else
		dev_info(&pdev->dev, "%s: device recovered\n", __func__);
}

static const struct pci_error_handlers mlx5_err_handler = {
	.error_detected = mlx5_pci_err_detected,
	.slot_reset	= mlx5_pci_slot_reset,
	.resume		= mlx5_pci_resume
};

1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
static int mlx5_try_fast_unload(struct mlx5_core_dev *dev)
{
	int ret;

	if (!MLX5_CAP_GEN(dev, force_teardown)) {
		mlx5_core_dbg(dev, "force teardown is not supported in the firmware\n");
		return -EOPNOTSUPP;
	}

	if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
		mlx5_core_dbg(dev, "Device in internal error state, giving up\n");
		return -EAGAIN;
	}

1551 1552 1553 1554 1555 1556
	/* Panic tear down fw command will stop the PCI bus communication
	 * with the HCA, so the health polll is no longer needed.
	 */
	mlx5_drain_health_wq(dev);
	mlx5_stop_health_poll(dev);

1557 1558 1559
	ret = mlx5_cmd_force_teardown_hca(dev);
	if (ret) {
		mlx5_core_dbg(dev, "Firmware couldn't do fast unload error: %d\n", ret);
1560
		mlx5_start_health_poll(dev);
1561 1562 1563 1564 1565 1566 1567 1568
		return ret;
	}

	mlx5_enter_error_state(dev, true);

	return 0;
}

1569 1570 1571 1572
static void shutdown(struct pci_dev *pdev)
{
	struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
	struct mlx5_priv *priv = &dev->priv;
1573
	int err;
1574 1575

	dev_info(&pdev->dev, "Shutdown was called\n");
1576 1577 1578
	err = mlx5_try_fast_unload(dev);
	if (err)
		mlx5_unload_one(dev, priv, false);
1579 1580 1581
	mlx5_pci_disable_device(dev);
}

1582
static const struct pci_device_id mlx5_core_pci_table[] = {
M
Myron Stowe 已提交
1583
	{ PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTIB) },
E
Eli Cohen 已提交
1584
	{ PCI_VDEVICE(MELLANOX, 0x1012), MLX5_PCI_DEV_IS_VF},	/* Connect-IB VF */
M
Myron Stowe 已提交
1585
	{ PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4) },
E
Eli Cohen 已提交
1586
	{ PCI_VDEVICE(MELLANOX, 0x1014), MLX5_PCI_DEV_IS_VF},	/* ConnectX-4 VF */
M
Myron Stowe 已提交
1587
	{ PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4_LX) },
E
Eli Cohen 已提交
1588
	{ PCI_VDEVICE(MELLANOX, 0x1016), MLX5_PCI_DEV_IS_VF},	/* ConnectX-4LX VF */
1589
	{ PCI_VDEVICE(MELLANOX, 0x1017) },			/* ConnectX-5, PCIe 3.0 */
1590
	{ PCI_VDEVICE(MELLANOX, 0x1018), MLX5_PCI_DEV_IS_VF},	/* ConnectX-5 VF */
1591 1592 1593 1594
	{ PCI_VDEVICE(MELLANOX, 0x1019) },			/* ConnectX-5 Ex */
	{ PCI_VDEVICE(MELLANOX, 0x101a), MLX5_PCI_DEV_IS_VF},	/* ConnectX-5 Ex VF */
	{ PCI_VDEVICE(MELLANOX, 0x101b) },			/* ConnectX-6 */
	{ PCI_VDEVICE(MELLANOX, 0x101c), MLX5_PCI_DEV_IS_VF},	/* ConnectX-6 VF */
1595 1596
	{ PCI_VDEVICE(MELLANOX, 0xa2d2) },			/* BlueField integrated ConnectX-5 network controller */
	{ PCI_VDEVICE(MELLANOX, 0xa2d3), MLX5_PCI_DEV_IS_VF},	/* BlueField integrated ConnectX-5 network controller VF */
1597 1598 1599 1600 1601
	{ 0, }
};

MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);

1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
void mlx5_disable_device(struct mlx5_core_dev *dev)
{
	mlx5_pci_err_detected(dev->pdev, 0);
}

void mlx5_recover_device(struct mlx5_core_dev *dev)
{
	mlx5_pci_disable_device(dev);
	if (mlx5_pci_slot_reset(dev->pdev) == PCI_ERS_RESULT_RECOVERED)
		mlx5_pci_resume(dev->pdev);
}

1614 1615 1616 1617
static struct pci_driver mlx5_core_driver = {
	.name           = DRIVER_NAME,
	.id_table       = mlx5_core_pci_table,
	.probe          = init_one,
1618
	.remove         = remove_one,
1619
	.shutdown	= shutdown,
E
Eli Cohen 已提交
1620 1621
	.err_handler	= &mlx5_err_handler,
	.sriov_configure   = mlx5_core_sriov_configure,
1622
};
1623

K
Kamal Heib 已提交
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
static void mlx5_core_verify_params(void)
{
	if (prof_sel >= ARRAY_SIZE(profile)) {
		pr_warn("mlx5_core: WARNING: Invalid module parameter prof_sel %d, valid range 0-%zu, changing back to default(%d)\n",
			prof_sel,
			ARRAY_SIZE(profile) - 1,
			MLX5_DEFAULT_PROF);
		prof_sel = MLX5_DEFAULT_PROF;
	}
}

1635 1636 1637 1638
static int __init init(void)
{
	int err;

K
Kamal Heib 已提交
1639
	mlx5_core_verify_params();
1640 1641
	mlx5_register_debugfs();

1642 1643
	err = pci_register_driver(&mlx5_core_driver);
	if (err)
1644
		goto err_debug;
1645

1646 1647 1648 1649
#ifdef CONFIG_MLX5_CORE_EN
	mlx5e_init();
#endif

1650 1651 1652 1653 1654 1655 1656 1657 1658
	return 0;

err_debug:
	mlx5_unregister_debugfs();
	return err;
}

static void __exit cleanup(void)
{
1659 1660 1661
#ifdef CONFIG_MLX5_CORE_EN
	mlx5e_cleanup();
#endif
1662
	pci_unregister_driver(&mlx5_core_driver);
1663 1664 1665 1666 1667
	mlx5_unregister_debugfs();
}

module_init(init);
module_exit(cleanup);