nfp_main.c 12.8 KB
Newer Older
1 2 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 33 34 35 36 37 38 39 40 41 42 43
/*
 * Copyright (C) 2015-2017 Netronome Systems, Inc.
 *
 * This software is dual licensed under the GNU General License Version 2,
 * June 1991 as shown in the file COPYING in the top-level directory of this
 * source tree or the BSD 2-Clause License provided below.  You have the
 * option to license this software under the complete terms of either license.
 *
 * The BSD 2-Clause License:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      1. Redistributions of source code must retain the above
 *         copyright notice, this list of conditions and the following
 *         disclaimer.
 *
 *      2. 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.
 */

/*
 * 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>
44
#include <linux/mutex.h>
45 46 47
#include <linux/pci.h>
#include <linux/firmware.h>
#include <linux/vermagic.h>
S
Simon Horman 已提交
48
#include <net/devlink.h>
49

J
Jakub Kicinski 已提交
50 51
#include "nfpcore/nfp.h"
#include "nfpcore/nfp_cpp.h"
52
#include "nfpcore/nfp_nffw.h"
53
#include "nfpcore/nfp_nsp.h"
J
Jakub Kicinski 已提交
54 55 56

#include "nfpcore/nfp6000_pcie.h"

S
Simon Horman 已提交
57
#include "nfp_app.h"
58 59 60 61 62 63
#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 已提交
64
static const struct pci_device_id nfp_pci_device_ids[] = {
65
	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
J
Jakub Kicinski 已提交
66 67 68
	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
	  PCI_ANY_ID, 0,
	},
69
	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
J
Jakub Kicinski 已提交
70 71 72 73 74 75 76
	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
	  PCI_ANY_ID, 0,
	},
	{ 0, } /* Required last entry. */
};
MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);

J
Jakub Kicinski 已提交
77
static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
78 79 80
{
	int err;

81
	pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
82
	if (!err)
J
Jakub Kicinski 已提交
83
		return pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
84 85

	pf->limit_vfs = ~0;
J
Jakub Kicinski 已提交
86
	pci_sriov_set_totalvfs(pf->pdev, 0); /* 0 is unset */
87
	/* Allow any setting for backwards compatibility if symbol not found */
J
Jakub Kicinski 已提交
88 89 90 91 92
	if (err == -ENOENT)
		return 0;

	nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
	return err;
93 94
}

J
Jakub Kicinski 已提交
95 96 97 98 99 100
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;

S
Simon Horman 已提交
101 102
	mutex_lock(&pf->lock);

103 104 105
	if (num_vfs > pf->limit_vfs) {
		nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
			 pf->limit_vfs);
S
Simon Horman 已提交
106 107 108 109
		err = -EINVAL;
		goto err_unlock;
	}

110
	err = pci_enable_sriov(pdev, num_vfs);
S
Simon Horman 已提交
111
	if (err) {
112
		dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
S
Simon Horman 已提交
113
		goto err_unlock;
114 115
	}

116
	err = nfp_app_sriov_enable(pf->app, num_vfs);
J
Jakub Kicinski 已提交
117
	if (err) {
118 119 120 121
		dev_warn(&pdev->dev,
			 "App specific PCI SR-IOV configuration failed: %d\n",
			 err);
		goto err_sriov_disable;
J
Jakub Kicinski 已提交
122 123 124 125 126 127
	}

	pf->num_vfs = num_vfs;

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

S
Simon Horman 已提交
128
	mutex_unlock(&pf->lock);
J
Jakub Kicinski 已提交
129
	return num_vfs;
S
Simon Horman 已提交
130

131 132
err_sriov_disable:
	pci_disable_sriov(pdev);
S
Simon Horman 已提交
133 134 135
err_unlock:
	mutex_unlock(&pf->lock);
	return err;
J
Jakub Kicinski 已提交
136 137 138 139
#endif
	return 0;
}

140
static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
J
Jakub Kicinski 已提交
141 142 143 144
{
#ifdef CONFIG_PCI_IOV
	struct nfp_pf *pf = pci_get_drvdata(pdev);

145 146
	mutex_lock(&pf->lock);

J
Jakub Kicinski 已提交
147 148 149 150 151 152
	/* 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");
153
		mutex_unlock(&pf->lock);
J
Jakub Kicinski 已提交
154 155 156
		return -EPERM;
	}

S
Simon Horman 已提交
157 158
	nfp_app_sriov_disable(pf->app);

J
Jakub Kicinski 已提交
159 160 161 162 163
	pf->num_vfs = 0;

	pci_disable_sriov(pdev);
	dev_dbg(&pdev->dev, "Removed VFs.\n");

S
Simon Horman 已提交
164
	mutex_unlock(&pf->lock);
165 166
#endif
	return 0;
S
Simon Horman 已提交
167 168
}

J
Jakub Kicinski 已提交
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
static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
{
	if (num_vfs == 0)
		return nfp_pcie_sriov_disable(pdev);
	else
		return nfp_pcie_sriov_enable(pdev, num_vfs);
}

/**
 * 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)
{
	const struct firmware *fw = NULL;
	struct nfp_eth_table_port *port;
	const char *fw_model;
	char fw_name[256];
	int spc, err = 0;
	int i, j;

	if (!pf->eth_tbl) {
		dev_err(&pdev->dev, "Error: can't identify media config\n");
		return NULL;
	}

199
	fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
J
Jakub Kicinski 已提交
200 201 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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	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;

	err = request_firmware(&fw, fw_name, &pdev->dev);
	if (err)
		return NULL;

	dev_info(&pdev->dev, "Loading FW image: %s\n", fw_name);

	return fw;
}

/**
 * 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);
	if (!fw)
		return 0;

	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;
}

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
{
	struct nfp_nsp *nsp;
	int err;

	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;

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

D
David Brunecz 已提交
302 303 304
	pf->nspi = __nfp_nsp_identify(nsp);
	if (pf->nspi)
		dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
305

306 307
	err = nfp_fw_load(pdev, pf, nsp);
	if (err < 0) {
308
		kfree(pf->nspi);
309 310 311 312 313 314 315 316 317 318 319 320 321 322
		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 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
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);
}

static int nfp_pci_probe(struct pci_dev *pdev,
			 const struct pci_device_id *pci_id)
{
S
Simon Horman 已提交
346
	struct devlink *devlink;
J
Jakub Kicinski 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
	struct nfp_pf *pf;
	int err;

	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 已提交
367 368
	devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
	if (!devlink) {
J
Jakub Kicinski 已提交
369 370 371
		err = -ENOMEM;
		goto err_rel_regions;
	}
S
Simon Horman 已提交
372
	pf = devlink_priv(devlink);
J
Jakub Kicinski 已提交
373
	INIT_LIST_HEAD(&pf->vnics);
374
	INIT_LIST_HEAD(&pf->ports);
375
	mutex_init(&pf->lock);
J
Jakub Kicinski 已提交
376 377 378 379 380 381 382 383 384 385 386
	pci_set_drvdata(pdev, pf);
	pf->pdev = pdev;

	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;
	}

387 388
	pf->hwinfo = nfp_hwinfo_read(pf->cpp);

389
	dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
390 391 392 393 394
		 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"));
395

S
Simon Horman 已提交
396
	err = devlink_register(devlink, &pdev->dev);
397
	if (err)
398
		goto err_hwinfo_free;
J
Jakub Kicinski 已提交
399

S
Simon Horman 已提交
400 401 402 403
	err = nfp_nsp_init(pdev, pf);
	if (err)
		goto err_devlink_unreg;

J
Jakub Kicinski 已提交
404 405
	pf->mip = nfp_mip_open(pf->cpp);
	pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
406

J
Jakub Kicinski 已提交
407 408 409
	err = nfp_pcie_sriov_read_nfd_limit(pf);
	if (err)
		goto err_fw_unload;
410

411 412 413 414 415 416 417 418
	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);
		goto err_fw_unload;
	}

J
Jakub Kicinski 已提交
419 420
	err = nfp_net_pci_probe(pf);
	if (err)
J
Jakub Kicinski 已提交
421
		goto err_sriov_unlimit;
J
Jakub Kicinski 已提交
422

D
David Brunecz 已提交
423 424 425 426 427 428
	err = nfp_hwmon_register(pf);
	if (err) {
		dev_err(&pdev->dev, "Failed to register hwmon info\n");
		goto err_net_remove;
	}

J
Jakub Kicinski 已提交
429 430
	return 0;

D
David Brunecz 已提交
431 432
err_net_remove:
	nfp_net_pci_remove(pf);
J
Jakub Kicinski 已提交
433 434
err_sriov_unlimit:
	pci_sriov_set_totalvfs(pf->pdev, 0);
J
Jakub Kicinski 已提交
435
err_fw_unload:
436
	kfree(pf->rtbl);
J
Jakub Kicinski 已提交
437
	nfp_mip_close(pf->mip);
J
Jakub Kicinski 已提交
438 439 440
	if (pf->fw_loaded)
		nfp_fw_unload(pf);
	kfree(pf->eth_tbl);
D
David Brunecz 已提交
441
	kfree(pf->nspi);
S
Simon Horman 已提交
442 443
err_devlink_unreg:
	devlink_unregister(devlink);
444 445
err_hwinfo_free:
	kfree(pf->hwinfo);
J
Jakub Kicinski 已提交
446 447 448
	nfp_cpp_free(pf->cpp);
err_disable_msix:
	pci_set_drvdata(pdev, NULL);
449
	mutex_destroy(&pf->lock);
S
Simon Horman 已提交
450
	devlink_free(devlink);
J
Jakub Kicinski 已提交
451 452 453 454 455 456 457 458 459 460 461
err_rel_regions:
	pci_release_regions(pdev);
err_pci_disable:
	pci_disable_device(pdev);

	return err;
}

static void nfp_pci_remove(struct pci_dev *pdev)
{
	struct nfp_pf *pf = pci_get_drvdata(pdev);
S
Simon Horman 已提交
462 463
	struct devlink *devlink;

D
David Brunecz 已提交
464 465
	nfp_hwmon_unregister(pf);

S
Simon Horman 已提交
466
	devlink = priv_to_devlink(pf);
J
Jakub Kicinski 已提交
467

468 469
	nfp_net_pci_remove(pf);

J
Jakub Kicinski 已提交
470
	nfp_pcie_sriov_disable(pdev);
J
Jakub Kicinski 已提交
471
	pci_sriov_set_totalvfs(pf->pdev, 0);
J
Jakub Kicinski 已提交
472

S
Simon Horman 已提交
473 474
	devlink_unregister(devlink);

475
	kfree(pf->rtbl);
J
Jakub Kicinski 已提交
476
	nfp_mip_close(pf->mip);
J
Jakub Kicinski 已提交
477 478 479 480
	if (pf->fw_loaded)
		nfp_fw_unload(pf);

	pci_set_drvdata(pdev, NULL);
481
	kfree(pf->hwinfo);
J
Jakub Kicinski 已提交
482 483 484
	nfp_cpp_free(pf->cpp);

	kfree(pf->eth_tbl);
D
David Brunecz 已提交
485
	kfree(pf->nspi);
486
	mutex_destroy(&pf->lock);
S
Simon Horman 已提交
487
	devlink_free(devlink);
J
Jakub Kicinski 已提交
488 489 490 491 492 493 494 495 496 497 498 499
	pci_release_regions(pdev);
	pci_disable_device(pdev);
}

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,
	.sriov_configure	= nfp_pcie_sriov_configure,
};

500 501 502 503 504 505 506 507 508
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 已提交
509 510 511 512
	err = pci_register_driver(&nfp_pci_driver);
	if (err < 0)
		goto err_destroy_debugfs;

513 514
	err = pci_register_driver(&nfp_netvf_pci_driver);
	if (err)
J
Jakub Kicinski 已提交
515
		goto err_unreg_pf;
516 517 518

	return err;

J
Jakub Kicinski 已提交
519 520
err_unreg_pf:
	pci_unregister_driver(&nfp_pci_driver);
521 522 523 524 525 526 527 528
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 已提交
529
	pci_unregister_driver(&nfp_pci_driver);
530 531 532 533 534 535
	nfp_net_debugfs_destroy();
}

module_init(nfp_main_init);
module_exit(nfp_main_exit);

J
Jakub Kicinski 已提交
536 537 538 539 540 541 542 543 544
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");

545 546 547
MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");