nfp_main.c 18.9 KB
Newer Older
1 2
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/* Copyright (C) 2015-2018 Netronome Systems, Inc. */
3 4 5 6 7 8 9 10 11 12 13

/*
 * nfp_main.c
 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
 *          Alejandro Lucero <alejandro.lucero@netronome.com>
 *          Jason McMullan <jason.mcmullan@netronome.com>
 *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
 */

#include <linux/kernel.h>
#include <linux/module.h>
14
#include <linux/mutex.h>
15 16 17
#include <linux/pci.h>
#include <linux/firmware.h>
#include <linux/vermagic.h>
C
Carl Heymann 已提交
18
#include <linux/vmalloc.h>
S
Simon Horman 已提交
19
#include <net/devlink.h>
20

J
Jakub Kicinski 已提交
21 22
#include "nfpcore/nfp.h"
#include "nfpcore/nfp_cpp.h"
23
#include "nfpcore/nfp_nffw.h"
24
#include "nfpcore/nfp_nsp.h"
J
Jakub Kicinski 已提交
25 26 27

#include "nfpcore/nfp6000_pcie.h"

28
#include "nfp_abi.h"
S
Simon Horman 已提交
29
#include "nfp_app.h"
30 31 32 33 34 35
#include "nfp_main.h"
#include "nfp_net.h"

static const char nfp_driver_name[] = "nfp";
const char nfp_driver_version[] = VERMAGIC_STRING;

J
Jakub Kicinski 已提交
36
static const struct pci_device_id nfp_pci_device_ids[] = {
37
	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
J
Jakub Kicinski 已提交
38 39 40
	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
	  PCI_ANY_ID, 0,
	},
J
Jakub Kicinski 已提交
41 42 43 44
	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP5000,
	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
	  PCI_ANY_ID, 0,
	},
45
	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
J
Jakub Kicinski 已提交
46 47 48 49 50 51 52
	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
	  PCI_ANY_ID, 0,
	},
	{ 0, } /* Required last entry. */
};
MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
int nfp_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format,
			       unsigned int default_val)
{
	char name[256];
	int err = 0;
	u64 val;

	snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp));

	val = nfp_rtsym_read_le(pf->rtbl, name, &err);
	if (err) {
		if (err == -ENOENT)
			return default_val;
		nfp_err(pf->cpp, "Unable to read symbol %s\n", name);
		return err;
	}

	return val;
}

u8 __iomem *
nfp_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt,
		 unsigned int min_size, struct nfp_cpp_area **area)
{
	char pf_symbol[256];

	snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt,
		 nfp_cppcore_pcie_unit(pf->cpp));

	return nfp_rtsym_map(pf->rtbl, pf_symbol, name, min_size, area);
}

85 86 87 88 89 90 91 92 93 94 95 96
/* Callers should hold the devlink instance lock */
int nfp_mbox_cmd(struct nfp_pf *pf, u32 cmd, void *in_data, u64 in_length,
		 void *out_data, u64 out_length)
{
	unsigned long err_at;
	u64 max_data_sz;
	u32 val = 0;
	int n, err;

	if (!pf->mbox)
		return -EOPNOTSUPP;

97
	max_data_sz = nfp_rtsym_size(pf->mbox) - NFP_MBOX_SYM_MIN_SIZE;
98 99

	/* Check if cmd field is clear */
100
	err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
101 102 103 104 105 106 107
	if (err || val) {
		nfp_warn(pf->cpp, "failed to issue command (%u): %u, err: %d\n",
			 cmd, val, err);
		return err ?: -EBUSY;
	}

	in_length = min(in_length, max_data_sz);
108 109
	n = nfp_rtsym_write(pf->cpp, pf->mbox, NFP_MBOX_DATA, in_data,
			    in_length);
110 111 112
	if (n != in_length)
		return -EIO;
	/* Write data_len and wipe reserved */
113
	err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, in_length);
114 115 116 117
	if (err)
		return err;

	/* Read back for ordering */
118
	err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
119 120 121 122
	if (err)
		return err;

	/* Write cmd and wipe return value */
123
	err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_CMD, cmd);
124 125 126 127 128 129
	if (err)
		return err;

	err_at = jiffies + 5 * HZ;
	while (true) {
		/* Wait for command to go to 0 (NFP_MBOX_NO_CMD) */
130
		err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
131 132 133 134 135 136 137 138 139 140 141 142
		if (err)
			return err;
		if (!val)
			break;

		if (time_is_before_eq_jiffies(err_at))
			return -ETIMEDOUT;

		msleep(5);
	}

	/* Copy output if any (could be error info, do it before reading ret) */
143
	err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
144 145 146 147
	if (err)
		return err;

	out_length = min_t(u32, val, min(out_length, max_data_sz));
148 149
	n = nfp_rtsym_read(pf->cpp, pf->mbox, NFP_MBOX_DATA,
			   out_data, out_length);
150 151 152 153
	if (n != out_length)
		return -EIO;

	/* Check if there is an error */
154
	err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_RET, &val);
155 156 157 158 159 160 161 162
	if (err)
		return err;
	if (val)
		return -val;

	return out_length;
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
static bool nfp_board_ready(struct nfp_pf *pf)
{
	const char *cp;
	long state;
	int err;

	cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
	if (!cp)
		return false;

	err = kstrtol(cp, 0, &state);
	if (err < 0)
		return false;

	return state == 15;
}

static int nfp_pf_board_state_wait(struct nfp_pf *pf)
{
	const unsigned long wait_until = jiffies + 10 * HZ;

	while (!nfp_board_ready(pf)) {
		if (time_is_before_eq_jiffies(wait_until)) {
			nfp_err(pf->cpp, "NFP board initialization timeout\n");
			return -EINVAL;
		}

		nfp_info(pf->cpp, "waiting for board initialization\n");
		if (msleep_interruptible(500))
			return -ERESTARTSYS;

		/* Refresh cached information */
		kfree(pf->hwinfo);
		pf->hwinfo = nfp_hwinfo_read(pf->cpp);
	}

	return 0;
}

J
Jakub Kicinski 已提交
202
static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
203 204 205
{
	int err;

206
	pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
207 208 209 210 211
	if (err) {
		/* For backwards compatibility if symbol not found allow all */
		pf->limit_vfs = ~0;
		if (err == -ENOENT)
			return 0;
212

213 214 215
		nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
		return err;
	}
J
Jakub Kicinski 已提交
216

217 218 219 220
	err = pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
	if (err)
		nfp_warn(pf->cpp, "Failed to set VF count in sysfs: %d\n", err);
	return 0;
221 222
}

J
Jakub Kicinski 已提交
223 224 225 226 227 228
static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
{
#ifdef CONFIG_PCI_IOV
	struct nfp_pf *pf = pci_get_drvdata(pdev);
	int err;

229 230 231
	if (num_vfs > pf->limit_vfs) {
		nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
			 pf->limit_vfs);
232
		return -EINVAL;
S
Simon Horman 已提交
233 234
	}

235
	err = pci_enable_sriov(pdev, num_vfs);
S
Simon Horman 已提交
236
	if (err) {
237
		dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
238
		return err;
239 240
	}

241 242
	mutex_lock(&pf->lock);

243
	err = nfp_app_sriov_enable(pf->app, num_vfs);
J
Jakub Kicinski 已提交
244
	if (err) {
245 246 247 248
		dev_warn(&pdev->dev,
			 "App specific PCI SR-IOV configuration failed: %d\n",
			 err);
		goto err_sriov_disable;
J
Jakub Kicinski 已提交
249 250 251 252 253 254
	}

	pf->num_vfs = num_vfs;

	dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);

S
Simon Horman 已提交
255
	mutex_unlock(&pf->lock);
J
Jakub Kicinski 已提交
256
	return num_vfs;
S
Simon Horman 已提交
257

258
err_sriov_disable:
S
Simon Horman 已提交
259
	mutex_unlock(&pf->lock);
260
	pci_disable_sriov(pdev);
S
Simon Horman 已提交
261
	return err;
J
Jakub Kicinski 已提交
262 263 264 265
#endif
	return 0;
}

266
static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
J
Jakub Kicinski 已提交
267 268 269 270
{
#ifdef CONFIG_PCI_IOV
	struct nfp_pf *pf = pci_get_drvdata(pdev);

271 272
	mutex_lock(&pf->lock);

J
Jakub Kicinski 已提交
273 274 275 276 277 278
	/* If the VFs are assigned we cannot shut down SR-IOV without
	 * causing issues, so just leave the hardware available but
	 * disabled
	 */
	if (pci_vfs_assigned(pdev)) {
		dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
279
		mutex_unlock(&pf->lock);
J
Jakub Kicinski 已提交
280 281 282
		return -EPERM;
	}

S
Simon Horman 已提交
283 284
	nfp_app_sriov_disable(pf->app);

J
Jakub Kicinski 已提交
285 286
	pf->num_vfs = 0;

287 288
	mutex_unlock(&pf->lock);

J
Jakub Kicinski 已提交
289 290
	pci_disable_sriov(pdev);
	dev_dbg(&pdev->dev, "Removed VFs.\n");
291 292
#endif
	return 0;
S
Simon Horman 已提交
293 294
}

J
Jakub Kicinski 已提交
295 296
static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
{
297 298 299
	if (!pci_get_drvdata(pdev))
		return -ENOENT;

J
Jakub Kicinski 已提交
300 301 302 303 304 305
	if (num_vfs == 0)
		return nfp_pcie_sriov_disable(pdev);
	else
		return nfp_pcie_sriov_enable(pdev, num_vfs);
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
int nfp_flash_update_common(struct nfp_pf *pf, const char *path,
			    struct netlink_ext_ack *extack)
{
	struct device *dev = &pf->pdev->dev;
	const struct firmware *fw;
	struct nfp_nsp *nsp;
	int err;

	nsp = nfp_nsp_open(pf->cpp);
	if (IS_ERR(nsp)) {
		err = PTR_ERR(nsp);
		if (extack)
			NL_SET_ERR_MSG_MOD(extack, "can't access NSP");
		else
			dev_err(dev, "Failed to access the NSP: %d\n", err);
		return err;
	}

	err = request_firmware_direct(&fw, path, dev);
	if (err) {
		NL_SET_ERR_MSG_MOD(extack,
				   "unable to read flash file from disk");
		goto exit_close_nsp;
	}

	dev_info(dev, "Please be patient while writing flash image: %s\n",
		 path);

	err = nfp_nsp_write_flash(nsp, fw);
	if (err < 0)
		goto exit_release_fw;
	dev_info(dev, "Finished writing flash image\n");
	err = 0;

exit_release_fw:
	release_firmware(fw);
exit_close_nsp:
	nfp_nsp_close(nsp);
	return err;
}

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
static const struct firmware *
nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
{
	const struct firmware *fw = NULL;
	int err;

	err = request_firmware_direct(&fw, name, &pdev->dev);
	nfp_info(pf->cpp, "  %s: %s\n",
		 name, err ? "not found" : "found, loading...");
	if (err)
		return NULL;

	return fw;
}

J
Jakub Kicinski 已提交
362 363 364 365 366 367 368 369 370 371 372
/**
 * nfp_net_fw_find() - Find the correct firmware image for netdev mode
 * @pdev:	PCI Device structure
 * @pf:		NFP PF Device structure
 *
 * Return: firmware if found and requested successfully.
 */
static const struct firmware *
nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
{
	struct nfp_eth_table_port *port;
373
	const struct firmware *fw;
J
Jakub Kicinski 已提交
374 375
	const char *fw_model;
	char fw_name[256];
376 377
	const u8 *serial;
	u16 interface;
378 379 380
	int spc, i, j;

	nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
J
Jakub Kicinski 已提交
381

382 383 384 385 386
	/* First try to find a firmware image specific for this device */
	interface = nfp_cpp_interface(pf->cpp);
	nfp_cpp_serial(pf->cpp, &serial);
	sprintf(fw_name, "netronome/serial-%pMF-%02hhx-%02hhx.nffw",
		serial, interface >> 8, interface & 0xff);
387 388 389
	fw = nfp_net_fw_request(pdev, pf, fw_name);
	if (fw)
		return fw;
390 391 392

	/* Then try the PCI name */
	sprintf(fw_name, "netronome/pci-%s.nffw", pci_name(pdev));
393 394 395
	fw = nfp_net_fw_request(pdev, pf, fw_name);
	if (fw)
		return fw;
396 397

	/* Finally try the card type and media */
J
Jakub Kicinski 已提交
398 399 400 401 402
	if (!pf->eth_tbl) {
		dev_err(&pdev->dev, "Error: can't identify media config\n");
		return NULL;
	}

403
	fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
J
Jakub Kicinski 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	if (!fw_model) {
		dev_err(&pdev->dev, "Error: can't read part number\n");
		return NULL;
	}

	spc = ARRAY_SIZE(fw_name);
	spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);

	for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
		port = &pf->eth_tbl->ports[i];
		j = 1;
		while (i + j < pf->eth_tbl->count &&
		       port->speed == port[j].speed)
			j++;

		spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
				"_%dx%d", j, port->speed / 1000);
	}

	if (spc <= 0)
		return NULL;

	spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
	if (spc <= 0)
		return NULL;

430
	return nfp_net_fw_request(pdev, pf, fw_name);
J
Jakub Kicinski 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
}

/**
 * nfp_net_fw_load() - Load the firmware image
 * @pdev:       PCI Device structure
 * @pf:		NFP PF Device structure
 * @nsp:	NFP SP handle
 *
 * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
 */
static int
nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
{
	const struct firmware *fw;
	u16 interface;
	int err;

	interface = nfp_cpp_interface(pf->cpp);
	if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
		/* Only Unit 0 should reset or load firmware */
		dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
		return 0;
	}

	fw = nfp_net_fw_find(pdev, pf);
J
Jakub Kicinski 已提交
456 457 458
	if (!fw) {
		if (nfp_nsp_has_stored_fw_load(nsp))
			nfp_nsp_load_stored_fw(nsp);
J
Jakub Kicinski 已提交
459
		return 0;
J
Jakub Kicinski 已提交
460
	}
J
Jakub Kicinski 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

	dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
	err = nfp_nsp_device_soft_reset(nsp);
	if (err < 0) {
		dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
			err);
		goto exit_release_fw;
	}

	err = nfp_nsp_load_fw(nsp, fw);
	if (err < 0) {
		dev_err(&pdev->dev, "FW loading failed: %d\n", err);
		goto exit_release_fw;
	}

	dev_info(&pdev->dev, "Finished loading FW image\n");

exit_release_fw:
	release_firmware(fw);

	return err < 0 ? err : 1;
}

J
Jakub Kicinski 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
static void
nfp_nsp_init_ports(struct pci_dev *pdev, struct nfp_pf *pf,
		   struct nfp_nsp *nsp)
{
	bool needs_reinit = false;
	int i;

	pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
	if (!pf->eth_tbl)
		return;

	if (!nfp_nsp_has_mac_reinit(nsp))
		return;

	for (i = 0; i < pf->eth_tbl->count; i++)
		needs_reinit |= pf->eth_tbl->ports[i].override_changed;
	if (!needs_reinit)
		return;

	kfree(pf->eth_tbl);
	if (nfp_nsp_mac_reinit(nsp))
		dev_warn(&pdev->dev, "MAC reinit failed\n");

	pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
}

510 511 512 513 514
static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
{
	struct nfp_nsp *nsp;
	int err;

515 516 517 518
	err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
	if (err)
		return err;

519 520 521 522 523 524 525 526 527 528 529
	nsp = nfp_nsp_open(pf->cpp);
	if (IS_ERR(nsp)) {
		err = PTR_ERR(nsp);
		dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
		return err;
	}

	err = nfp_nsp_wait(nsp);
	if (err < 0)
		goto exit_close_nsp;

J
Jakub Kicinski 已提交
530
	nfp_nsp_init_ports(pdev, pf, nsp);
531

D
David Brunecz 已提交
532 533 534
	pf->nspi = __nfp_nsp_identify(nsp);
	if (pf->nspi)
		dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
535

536 537
	err = nfp_fw_load(pdev, pf, nsp);
	if (err < 0) {
538
		kfree(pf->nspi);
539 540 541 542 543 544 545 546 547 548 549 550 551 552
		kfree(pf->eth_tbl);
		dev_err(&pdev->dev, "Failed to load FW\n");
		goto exit_close_nsp;
	}

	pf->fw_loaded = !!err;
	err = 0;

exit_close_nsp:
	nfp_nsp_close(nsp);

	return err;
}

J
Jakub Kicinski 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
static void nfp_fw_unload(struct nfp_pf *pf)
{
	struct nfp_nsp *nsp;
	int err;

	nsp = nfp_nsp_open(pf->cpp);
	if (IS_ERR(nsp)) {
		nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
		return;
	}

	err = nfp_nsp_device_soft_reset(nsp);
	if (err < 0)
		dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
	else
		dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");

	nfp_nsp_close(nsp);
}

573 574 575 576 577 578 579 580 581 582
static int nfp_pf_find_rtsyms(struct nfp_pf *pf)
{
	char pf_symbol[256];
	unsigned int pf_id;

	pf_id = nfp_cppcore_pcie_unit(pf->cpp);

	/* Optional per-PCI PF mailbox */
	snprintf(pf_symbol, sizeof(pf_symbol), NFP_MBOX_SYM_NAME, pf_id);
	pf->mbox = nfp_rtsym_lookup(pf->rtbl, pf_symbol);
583
	if (pf->mbox && nfp_rtsym_size(pf->mbox) < NFP_MBOX_SYM_MIN_SIZE) {
584
		nfp_err(pf->cpp, "PF mailbox symbol too small: %llu < %d\n",
585
			nfp_rtsym_size(pf->mbox), NFP_MBOX_SYM_MIN_SIZE);
586 587 588 589 590 591
		return -EINVAL;
	}

	return 0;
}

J
Jakub Kicinski 已提交
592 593 594
static int nfp_pci_probe(struct pci_dev *pdev,
			 const struct pci_device_id *pci_id)
{
S
Simon Horman 已提交
595
	struct devlink *devlink;
J
Jakub Kicinski 已提交
596 597 598
	struct nfp_pf *pf;
	int err;

599 600 601 602
	if (pdev->vendor == PCI_VENDOR_ID_NETRONOME &&
	    pdev->device == PCI_DEVICE_ID_NETRONOME_NFP6000_VF)
		dev_warn(&pdev->dev, "Binding NFP VF device to the NFP PF driver, the VF driver is called 'nfp_netvf'\n");

J
Jakub Kicinski 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
	err = pci_enable_device(pdev);
	if (err < 0)
		return err;

	pci_set_master(pdev);

	err = dma_set_mask_and_coherent(&pdev->dev,
					DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
	if (err)
		goto err_pci_disable;

	err = pci_request_regions(pdev, nfp_driver_name);
	if (err < 0) {
		dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
		goto err_pci_disable;
	}

S
Simon Horman 已提交
620 621
	devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
	if (!devlink) {
J
Jakub Kicinski 已提交
622 623 624
		err = -ENOMEM;
		goto err_rel_regions;
	}
S
Simon Horman 已提交
625
	pf = devlink_priv(devlink);
J
Jakub Kicinski 已提交
626
	INIT_LIST_HEAD(&pf->vnics);
627
	INIT_LIST_HEAD(&pf->ports);
628
	mutex_init(&pf->lock);
J
Jakub Kicinski 已提交
629 630 631
	pci_set_drvdata(pdev, pf);
	pf->pdev = pdev;

632 633 634 635 636 637
	pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
	if (!pf->wq) {
		err = -ENOMEM;
		goto err_pci_priv_unset;
	}

J
Jakub Kicinski 已提交
638 639 640 641 642 643 644 645
	pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
	if (IS_ERR_OR_NULL(pf->cpp)) {
		err = PTR_ERR(pf->cpp);
		if (err >= 0)
			err = -ENOMEM;
		goto err_disable_msix;
	}

J
Jakub Kicinski 已提交
646 647 648 649
	err = nfp_resource_table_init(pf->cpp);
	if (err)
		goto err_cpp_free;

650 651
	pf->hwinfo = nfp_hwinfo_read(pf->cpp);

652
	dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
653 654 655 656 657
		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
		 nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
658

659 660 661 662
	err = nfp_pf_board_state_wait(pf);
	if (err)
		goto err_hwinfo_free;

S
Simon Horman 已提交
663 664
	err = nfp_nsp_init(pdev, pf);
	if (err)
665
		goto err_hwinfo_free;
S
Simon Horman 已提交
666

J
Jakub Kicinski 已提交
667 668
	pf->mip = nfp_mip_open(pf->cpp);
	pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
669

670 671 672 673
	err = nfp_pf_find_rtsyms(pf);
	if (err)
		goto err_fw_unload;

C
Carl Heymann 已提交
674 675 676
	pf->dump_flag = NFP_DUMP_NSP_DIAG;
	pf->dumpspec = nfp_net_dump_load_dumpspec(pf->cpp, pf->rtbl);

J
Jakub Kicinski 已提交
677 678 679
	err = nfp_pcie_sriov_read_nfd_limit(pf);
	if (err)
		goto err_fw_unload;
680

681 682 683 684 685
	pf->num_vfs = pci_num_vf(pdev);
	if (pf->num_vfs > pf->limit_vfs) {
		dev_err(&pdev->dev,
			"Error: %d VFs already enabled, but loaded FW can only support %d\n",
			pf->num_vfs, pf->limit_vfs);
686
		err = -EINVAL;
687 688 689
		goto err_fw_unload;
	}

J
Jakub Kicinski 已提交
690 691
	err = nfp_net_pci_probe(pf);
	if (err)
J
Jakub Kicinski 已提交
692
		goto err_fw_unload;
J
Jakub Kicinski 已提交
693

D
David Brunecz 已提交
694 695 696 697 698 699
	err = nfp_hwmon_register(pf);
	if (err) {
		dev_err(&pdev->dev, "Failed to register hwmon info\n");
		goto err_net_remove;
	}

J
Jakub Kicinski 已提交
700 701
	return 0;

D
David Brunecz 已提交
702 703
err_net_remove:
	nfp_net_pci_remove(pf);
J
Jakub Kicinski 已提交
704
err_fw_unload:
705
	kfree(pf->rtbl);
J
Jakub Kicinski 已提交
706
	nfp_mip_close(pf->mip);
J
Jakub Kicinski 已提交
707 708 709
	if (pf->fw_loaded)
		nfp_fw_unload(pf);
	kfree(pf->eth_tbl);
D
David Brunecz 已提交
710
	kfree(pf->nspi);
C
Carl Heymann 已提交
711
	vfree(pf->dumpspec);
712 713
err_hwinfo_free:
	kfree(pf->hwinfo);
J
Jakub Kicinski 已提交
714
err_cpp_free:
J
Jakub Kicinski 已提交
715 716
	nfp_cpp_free(pf->cpp);
err_disable_msix:
717 718
	destroy_workqueue(pf->wq);
err_pci_priv_unset:
J
Jakub Kicinski 已提交
719
	pci_set_drvdata(pdev, NULL);
720
	mutex_destroy(&pf->lock);
S
Simon Horman 已提交
721
	devlink_free(devlink);
J
Jakub Kicinski 已提交
722 723 724 725 726 727 728 729
err_rel_regions:
	pci_release_regions(pdev);
err_pci_disable:
	pci_disable_device(pdev);

	return err;
}

730
static void __nfp_pci_shutdown(struct pci_dev *pdev, bool unload_fw)
J
Jakub Kicinski 已提交
731
{
732 733 734 735 736
	struct nfp_pf *pf;

	pf = pci_get_drvdata(pdev);
	if (!pf)
		return;
S
Simon Horman 已提交
737

D
David Brunecz 已提交
738 739
	nfp_hwmon_unregister(pf);

J
Jakub Kicinski 已提交
740 741
	nfp_pcie_sriov_disable(pdev);

742
	nfp_net_pci_remove(pf);
S
Simon Horman 已提交
743

C
Carl Heymann 已提交
744
	vfree(pf->dumpspec);
745
	kfree(pf->rtbl);
J
Jakub Kicinski 已提交
746
	nfp_mip_close(pf->mip);
747
	if (unload_fw && pf->fw_loaded)
J
Jakub Kicinski 已提交
748 749
		nfp_fw_unload(pf);

750
	destroy_workqueue(pf->wq);
J
Jakub Kicinski 已提交
751
	pci_set_drvdata(pdev, NULL);
752
	kfree(pf->hwinfo);
J
Jakub Kicinski 已提交
753 754 755
	nfp_cpp_free(pf->cpp);

	kfree(pf->eth_tbl);
D
David Brunecz 已提交
756
	kfree(pf->nspi);
757
	mutex_destroy(&pf->lock);
758
	devlink_free(priv_to_devlink(pf));
J
Jakub Kicinski 已提交
759 760 761 762
	pci_release_regions(pdev);
	pci_disable_device(pdev);
}

763 764 765 766 767 768 769 770 771 772
static void nfp_pci_remove(struct pci_dev *pdev)
{
	__nfp_pci_shutdown(pdev, true);
}

static void nfp_pci_shutdown(struct pci_dev *pdev)
{
	__nfp_pci_shutdown(pdev, false);
}

J
Jakub Kicinski 已提交
773 774 775 776 777
static struct pci_driver nfp_pci_driver = {
	.name			= nfp_driver_name,
	.id_table		= nfp_pci_device_ids,
	.probe			= nfp_pci_probe,
	.remove			= nfp_pci_remove,
778
	.shutdown		= nfp_pci_shutdown,
J
Jakub Kicinski 已提交
779 780 781
	.sriov_configure	= nfp_pcie_sriov_configure,
};

782 783 784 785 786 787 788 789 790
static int __init nfp_main_init(void)
{
	int err;

	pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
		nfp_driver_name);

	nfp_net_debugfs_create();

J
Jakub Kicinski 已提交
791 792 793 794
	err = pci_register_driver(&nfp_pci_driver);
	if (err < 0)
		goto err_destroy_debugfs;

795 796
	err = pci_register_driver(&nfp_netvf_pci_driver);
	if (err)
J
Jakub Kicinski 已提交
797
		goto err_unreg_pf;
798 799 800

	return err;

J
Jakub Kicinski 已提交
801 802
err_unreg_pf:
	pci_unregister_driver(&nfp_pci_driver);
803 804 805 806 807 808 809 810
err_destroy_debugfs:
	nfp_net_debugfs_destroy();
	return err;
}

static void __exit nfp_main_exit(void)
{
	pci_unregister_driver(&nfp_netvf_pci_driver);
J
Jakub Kicinski 已提交
811
	pci_unregister_driver(&nfp_pci_driver);
812 813 814 815 816 817
	nfp_net_debugfs_destroy();
}

module_init(nfp_main_init);
module_exit(nfp_main_exit);

818 819
MODULE_FIRMWARE("netronome/nic_AMDA0058-0011_2x40.nffw");
MODULE_FIRMWARE("netronome/nic_AMDA0058-0012_2x40.nffw");
J
Jakub Kicinski 已提交
820 821 822 823 824 825 826 827
MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
828
MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_1x10_1x25.nffw");
J
Jakub Kicinski 已提交
829

830 831 832
MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");
J
Jakub Kicinski 已提交
833
MODULE_VERSION(UTS_RELEASE);