nfp_net_main.c 19.3 KB
Newer Older
J
Jakub Kicinski 已提交
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 44 45 46 47 48 49
/*
 * 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_net_main.c
 * Netronome network device driver: Main entry point
 * 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/etherdevice.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/pci_regs.h>
#include <linux/msi.h>
#include <linux/random.h>
J
Jakub Kicinski 已提交
50
#include <linux/rtnetlink.h>
J
Jakub Kicinski 已提交
51 52 53 54

#include "nfpcore/nfp.h"
#include "nfpcore/nfp_cpp.h"
#include "nfpcore/nfp_nffw.h"
55
#include "nfpcore/nfp_nsp.h"
J
Jakub Kicinski 已提交
56
#include "nfpcore/nfp6000_pcie.h"
57
#include "nfp_app.h"
J
Jakub Kicinski 已提交
58 59 60
#include "nfp_net_ctrl.h"
#include "nfp_net.h"
#include "nfp_main.h"
J
Jakub Kicinski 已提交
61
#include "nfp_port.h"
J
Jakub Kicinski 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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

#define NFP_PF_CSR_SLICE_SIZE	(32 * 1024)

static int nfp_is_ready(struct nfp_cpp *cpp)
{
	const char *cp;
	long state;
	int err;

	cp = nfp_hwinfo_lookup(cpp, "board.state");
	if (!cp)
		return 0;

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

	return state == 15;
}

/**
 * nfp_net_map_area() - Help function to map an area
 * @cpp:    NFP CPP handler
 * @name:   Name for the area
 * @target: CPP target
 * @addr:   CPP address
 * @size:   Size of the area
 * @area:   Area handle (returned).
 *
 * This function is primarily to simplify the code in the main probe
 * function. To undo the effect of this functions call
 * @nfp_cpp_area_release_free(*area);
 *
 * Return: Pointer to memory mapped area or ERR_PTR
 */
static u8 __iomem *nfp_net_map_area(struct nfp_cpp *cpp,
				    const char *name, int isl, int target,
				    unsigned long long addr, unsigned long size,
				    struct nfp_cpp_area **area)
{
	u8 __iomem *res;
	u32 dest;
	int err;

	dest = NFP_CPP_ISLAND_ID(target, NFP_CPP_ACTION_RW, 0, isl);

	*area = nfp_cpp_area_alloc_with_name(cpp, dest, name, addr, size);
	if (!*area) {
		err = -EIO;
		goto err_area;
	}

	err = nfp_cpp_area_acquire(*area);
	if (err < 0)
		goto err_acquire;

	res = nfp_cpp_area_iomem(*area);
	if (!res) {
		err = -EIO;
		goto err_map;
	}

	return res;

err_map:
	nfp_cpp_area_release(*area);
err_acquire:
	nfp_cpp_area_free(*area);
err_area:
	return (u8 __iomem *)ERR_PTR(err);
}

134 135 136 137 138 139 140 141 142
/**
 * nfp_net_get_mac_addr() - Get the MAC address.
 * @nn:       NFP Network structure
 * @cpp:      NFP CPP handle
 * @id:	      NFP port id
 *
 * First try to get the MAC address from NSP ETH table. If that
 * fails try HWInfo.  As a last resort generate a random address.
 */
J
Jakub Kicinski 已提交
143
static void
144
nfp_net_get_mac_addr(struct nfp_net *nn, struct nfp_cpp *cpp, unsigned int id)
J
Jakub Kicinski 已提交
145
{
J
Jakub Kicinski 已提交
146
	struct nfp_eth_table_port *eth_port;
147
	struct nfp_net_dp *dp = &nn->dp;
J
Jakub Kicinski 已提交
148 149 150 151
	u8 mac_addr[ETH_ALEN];
	const char *mac_str;
	char name[32];

J
Jakub Kicinski 已提交
152 153 154 155
	eth_port = __nfp_port_get_eth_port(nn->port);
	if (eth_port) {
		ether_addr_copy(dp->netdev->dev_addr, eth_port->mac_addr);
		ether_addr_copy(dp->netdev->perm_addr, eth_port->mac_addr);
156 157 158
		return;
	}

J
Jakub Kicinski 已提交
159 160 161 162
	snprintf(name, sizeof(name), "eth%d.mac", id);

	mac_str = nfp_hwinfo_lookup(cpp, name);
	if (!mac_str) {
163 164
		dev_warn(dp->dev, "Can't lookup MAC address. Generate\n");
		eth_hw_addr_random(dp->netdev);
J
Jakub Kicinski 已提交
165 166 167 168 169 170
		return;
	}

	if (sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
		   &mac_addr[0], &mac_addr[1], &mac_addr[2],
		   &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6) {
171
		dev_warn(dp->dev,
J
Jakub Kicinski 已提交
172
			 "Can't parse MAC address (%s). Generate.\n", mac_str);
173
		eth_hw_addr_random(dp->netdev);
J
Jakub Kicinski 已提交
174 175 176
		return;
	}

177 178
	ether_addr_copy(dp->netdev->dev_addr, mac_addr);
	ether_addr_copy(dp->netdev->perm_addr, mac_addr);
J
Jakub Kicinski 已提交
179 180
}

181
static struct nfp_eth_table_port *
182
nfp_net_find_port(struct nfp_eth_table *eth_tbl, unsigned int id)
J
Jakub Kicinski 已提交
183 184 185
{
	int i;

186 187 188
	for (i = 0; eth_tbl && i < eth_tbl->count; i++)
		if (eth_tbl->ports[i].eth_index == id)
			return &eth_tbl->ports[i];
189

190
	return NULL;
J
Jakub Kicinski 已提交
191 192 193 194 195 196 197 198
}

static unsigned int nfp_net_pf_get_num_ports(struct nfp_pf *pf)
{
	char name[256];
	int err = 0;
	u64 val;

199 200
	snprintf(name, sizeof(name), "nfd_cfg_pf%u_num_ports",
		 nfp_cppcore_pcie_unit(pf->cpp));
J
Jakub Kicinski 已提交
201 202

	val = nfp_rtsym_read_le(pf->cpp, name, &err);
J
Jakub Kicinski 已提交
203
	/* Default to one port/vNIC */
J
Jakub Kicinski 已提交
204 205
	if (err) {
		if (err != -ENOENT)
J
Jakub Kicinski 已提交
206
			nfp_err(pf->cpp, "Unable to read adapter vNIC count\n");
J
Jakub Kicinski 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
		val = 1;
	}

	return val;
}

static unsigned int
nfp_net_pf_total_qcs(struct nfp_pf *pf, void __iomem *ctrl_bar,
		     unsigned int stride, u32 start_off, u32 num_off)
{
	unsigned int i, min_qc, max_qc;

	min_qc = readl(ctrl_bar + start_off);
	max_qc = min_qc;

J
Jakub Kicinski 已提交
222
	for (i = 0; i < pf->max_data_vnics; i++) {
J
Jakub Kicinski 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		/* To make our lives simpler only accept configuration where
		 * queues are allocated to PFs in order (queues of PFn all have
		 * indexes lower than PFn+1).
		 */
		if (max_qc > readl(ctrl_bar + start_off))
			return 0;

		max_qc = readl(ctrl_bar + start_off);
		max_qc += readl(ctrl_bar + num_off) * stride;
		ctrl_bar += NFP_PF_CSR_SLICE_SIZE;
	}

	return max_qc - min_qc;
}

static u8 __iomem *nfp_net_pf_map_ctrl_bar(struct nfp_pf *pf)
{
	const struct nfp_rtsym *ctrl_sym;
	u8 __iomem *ctrl_bar;
	char pf_symbol[256];

244 245
	snprintf(pf_symbol, sizeof(pf_symbol), "_pf%u_net_bar0",
		 nfp_cppcore_pcie_unit(pf->cpp));
J
Jakub Kicinski 已提交
246 247 248 249 250 251 252 253

	ctrl_sym = nfp_rtsym_lookup(pf->cpp, pf_symbol);
	if (!ctrl_sym) {
		dev_err(&pf->pdev->dev,
			"Failed to find PF BAR0 symbol %s\n", pf_symbol);
		return NULL;
	}

J
Jakub Kicinski 已提交
254
	if (ctrl_sym->size < pf->max_data_vnics * NFP_PF_CSR_SLICE_SIZE) {
J
Jakub Kicinski 已提交
255
		dev_err(&pf->pdev->dev,
J
Jakub Kicinski 已提交
256 257
			"PF BAR0 too small to contain %d vNICs\n",
			pf->max_data_vnics);
J
Jakub Kicinski 已提交
258 259 260 261 262 263
		return NULL;
	}

	ctrl_bar = nfp_net_map_area(pf->cpp, "net.ctrl",
				    ctrl_sym->domain, ctrl_sym->target,
				    ctrl_sym->addr, ctrl_sym->size,
J
Jakub Kicinski 已提交
264
				    &pf->data_vnic_bar);
J
Jakub Kicinski 已提交
265 266 267 268 269 270 271 272 273
	if (IS_ERR(ctrl_bar)) {
		dev_err(&pf->pdev->dev, "Failed to map PF BAR0: %ld\n",
			PTR_ERR(ctrl_bar));
		return NULL;
	}

	return ctrl_bar;
}

274 275
static void nfp_net_pf_free_vnic(struct nfp_pf *pf, struct nfp_net *nn)
{
J
Jakub Kicinski 已提交
276
	nfp_port_free(nn->port);
277 278 279 280 281
	list_del(&nn->vnic_list);
	pf->num_vnics--;
	nfp_net_free(nn);
}

J
Jakub Kicinski 已提交
282
static void nfp_net_pf_free_vnics(struct nfp_pf *pf)
J
Jakub Kicinski 已提交
283 284 285
{
	struct nfp_net *nn;

J
Jakub Kicinski 已提交
286 287
	while (!list_empty(&pf->vnics)) {
		nn = list_first_entry(&pf->vnics, struct nfp_net, vnic_list);
288
		nfp_net_pf_free_vnic(pf, nn);
J
Jakub Kicinski 已提交
289 290 291 292
	}
}

static struct nfp_net *
J
Jakub Kicinski 已提交
293 294 295
nfp_net_pf_alloc_vnic(struct nfp_pf *pf, void __iomem *ctrl_bar,
		      void __iomem *tx_bar, void __iomem *rx_bar,
		      int stride, struct nfp_net_fw_version *fw_ver,
296
		      unsigned int eth_id)
J
Jakub Kicinski 已提交
297
{
J
Jakub Kicinski 已提交
298
	struct nfp_eth_table_port *eth_port;
J
Jakub Kicinski 已提交
299 300 301 302 303 304
	u32 n_tx_rings, n_rx_rings;
	struct nfp_net *nn;

	n_tx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_TXRINGS);
	n_rx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_RXRINGS);

J
Jakub Kicinski 已提交
305
	/* Allocate and initialise the vNIC */
306
	nn = nfp_net_alloc(pf->pdev, n_tx_rings, n_rx_rings);
J
Jakub Kicinski 已提交
307 308 309
	if (IS_ERR(nn))
		return nn;

310
	nn->app = pf->app;
J
Jakub Kicinski 已提交
311
	nn->fw_ver = *fw_ver;
312
	nn->dp.ctrl_bar = ctrl_bar;
J
Jakub Kicinski 已提交
313 314
	nn->tx_bar = tx_bar;
	nn->rx_bar = rx_bar;
315
	nn->dp.is_vf = 0;
J
Jakub Kicinski 已提交
316 317
	nn->stride_rx = stride;
	nn->stride_tx = stride;
J
Jakub Kicinski 已提交
318 319 320 321 322 323 324 325 326 327 328 329

	eth_port = nfp_net_find_port(pf->eth_tbl, eth_id);
	if (eth_port) {
		nn->port = nfp_port_alloc(pf->app, NFP_PORT_PHYS_PORT,
					  nn->dp.netdev);
		if (IS_ERR(nn->port)) {
			nfp_net_free(nn);
			return ERR_CAST(nn->port);
		}
		nn->port->eth_id = eth_id;
		nn->port->eth_port = eth_port;
	}
330 331 332

	pf->num_vnics++;
	list_add_tail(&nn->vnic_list, &pf->vnics);
J
Jakub Kicinski 已提交
333 334 335 336 337

	return nn;
}

static int
J
Jakub Kicinski 已提交
338
nfp_net_pf_init_vnic(struct nfp_pf *pf, struct nfp_net *nn, unsigned int id)
J
Jakub Kicinski 已提交
339 340 341 342
{
	int err;

	/* Get MAC address */
343
	nfp_net_get_mac_addr(nn, pf->cpp, id);
J
Jakub Kicinski 已提交
344 345 346 347 348 349 350

	/* Get ME clock frequency from ctrl BAR
	 * XXX for now frequency is hardcoded until we figure out how
	 * to get the value from nfp-hwinfo into ctrl bar
	 */
	nn->me_freq_mhz = 1200;

351
	err = nfp_net_init(nn);
J
Jakub Kicinski 已提交
352 353 354
	if (err)
		return err;

J
Jakub Kicinski 已提交
355
	nfp_net_debugfs_vnic_add(nn, pf->ddir, id);
J
Jakub Kicinski 已提交
356

357 358 359 360 361 362
	if (nn->port) {
		err = nfp_devlink_port_register(pf->app, nn->port);
		if (err)
			goto err_dfs_clean;
	}

J
Jakub Kicinski 已提交
363 364 365
	nfp_net_info(nn);

	return 0;
366 367 368 369 370

err_dfs_clean:
	nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
	nfp_net_clean(nn);
	return err;
J
Jakub Kicinski 已提交
371 372 373
}

static int
J
Jakub Kicinski 已提交
374 375 376
nfp_net_pf_alloc_vnics(struct nfp_pf *pf, void __iomem *ctrl_bar,
		       void __iomem *tx_bar, void __iomem *rx_bar,
		       int stride, struct nfp_net_fw_version *fw_ver)
J
Jakub Kicinski 已提交
377 378 379 380 381 382
{
	u32 prev_tx_base, prev_rx_base, tgt_tx_base, tgt_rx_base;
	struct nfp_net *nn;
	unsigned int i;
	int err;

383 384 385 386 387 388
	if (pf->eth_tbl && pf->max_data_vnics != pf->eth_tbl->count) {
		nfp_err(pf->cpp, "ETH entries don't match vNICs (%d vs %d)\n",
			pf->max_data_vnics, pf->eth_tbl->count);
		return -EINVAL;
	}

J
Jakub Kicinski 已提交
389 390 391
	prev_tx_base = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
	prev_rx_base = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);

J
Jakub Kicinski 已提交
392
	for (i = 0; i < pf->max_data_vnics; i++) {
J
Jakub Kicinski 已提交
393 394 395 396 397 398 399
		tgt_tx_base = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
		tgt_rx_base = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
		tx_bar += (tgt_tx_base - prev_tx_base) * NFP_QCP_QUEUE_ADDR_SZ;
		rx_bar += (tgt_rx_base - prev_rx_base) * NFP_QCP_QUEUE_ADDR_SZ;
		prev_tx_base = tgt_tx_base;
		prev_rx_base = tgt_rx_base;

400 401 402 403 404
		nn = nfp_net_pf_alloc_vnic(pf, ctrl_bar, tx_bar, rx_bar,
					   stride, fw_ver, i);
		if (IS_ERR(nn)) {
			err = PTR_ERR(nn);
			goto err_free_prev;
J
Jakub Kicinski 已提交
405 406 407
		}

		ctrl_bar += NFP_PF_CSR_SLICE_SIZE;
408 409

		/* Check if vNIC has external port associated and cfg is OK */
J
Jakub Kicinski 已提交
410
		if (pf->eth_tbl && !nn->port) {
411 412 413 414
			nfp_err(pf->cpp, "NSP port entries don't match vNICs (no entry for port #%d)\n", i);
			err = -EINVAL;
			goto err_free_prev;
		}
J
Jakub Kicinski 已提交
415
		if (nn->port && nn->port->eth_port->override_changed) {
416 417 418 419
			nfp_warn(pf->cpp, "Config changed for port #%d, reboot required before port will be operational\n", i);
			nfp_net_pf_free_vnic(pf, nn);
			continue;
		}
J
Jakub Kicinski 已提交
420 421
	}

J
Jakub Kicinski 已提交
422
	if (list_empty(&pf->vnics))
423 424
		return -ENODEV;

J
Jakub Kicinski 已提交
425 426 427
	return 0;

err_free_prev:
J
Jakub Kicinski 已提交
428
	nfp_net_pf_free_vnics(pf);
J
Jakub Kicinski 已提交
429 430 431
	return err;
}

432 433
static void nfp_net_pf_clean_vnic(struct nfp_pf *pf, struct nfp_net *nn)
{
434 435
	if (nn->port)
		nfp_devlink_port_unregister(nn->port);
436 437 438 439
	nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
	nfp_net_clean(nn);
}

J
Jakub Kicinski 已提交
440
static int
J
Jakub Kicinski 已提交
441 442 443 444
nfp_net_pf_spawn_vnics(struct nfp_pf *pf,
		       void __iomem *ctrl_bar, void __iomem *tx_bar,
		       void __iomem *rx_bar, int stride,
		       struct nfp_net_fw_version *fw_ver)
J
Jakub Kicinski 已提交
445
{
J
Jakub Kicinski 已提交
446
	unsigned int id, wanted_irqs, num_irqs, vnics_left, irqs_left;
J
Jakub Kicinski 已提交
447 448 449
	struct nfp_net *nn;
	int err;

J
Jakub Kicinski 已提交
450 451 452
	/* Allocate the vnics and do basic init */
	err = nfp_net_pf_alloc_vnics(pf, ctrl_bar, tx_bar, rx_bar,
				     stride, fw_ver);
J
Jakub Kicinski 已提交
453 454 455 456 457
	if (err)
		return err;

	/* Get MSI-X vectors */
	wanted_irqs = 0;
J
Jakub Kicinski 已提交
458
	list_for_each_entry(nn, &pf->vnics, vnic_list)
459
		wanted_irqs += NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs;
J
Jakub Kicinski 已提交
460 461 462 463 464 465 466 467
	pf->irq_entries = kcalloc(wanted_irqs, sizeof(*pf->irq_entries),
				  GFP_KERNEL);
	if (!pf->irq_entries) {
		err = -ENOMEM;
		goto err_nn_free;
	}

	num_irqs = nfp_net_irqs_alloc(pf->pdev, pf->irq_entries,
J
Jakub Kicinski 已提交
468
				      NFP_NET_MIN_VNIC_IRQS * pf->num_vnics,
J
Jakub Kicinski 已提交
469 470 471 472 473 474 475
				      wanted_irqs);
	if (!num_irqs) {
		nn_warn(nn, "Unable to allocate MSI-X Vectors. Exiting\n");
		err = -ENOMEM;
		goto err_vec_free;
	}

J
Jakub Kicinski 已提交
476
	/* Distribute IRQs to vNICs */
J
Jakub Kicinski 已提交
477
	irqs_left = num_irqs;
J
Jakub Kicinski 已提交
478 479
	vnics_left = pf->num_vnics;
	list_for_each_entry(nn, &pf->vnics, vnic_list) {
J
Jakub Kicinski 已提交
480 481
		unsigned int n;

J
Jakub Kicinski 已提交
482
		n = DIV_ROUND_UP(irqs_left, vnics_left);
J
Jakub Kicinski 已提交
483 484 485
		nfp_net_irqs_assign(nn, &pf->irq_entries[num_irqs - irqs_left],
				    n);
		irqs_left -= n;
J
Jakub Kicinski 已提交
486
		vnics_left--;
J
Jakub Kicinski 已提交
487 488
	}

J
Jakub Kicinski 已提交
489
	/* Finish vNIC init and register */
J
Jakub Kicinski 已提交
490
	id = 0;
J
Jakub Kicinski 已提交
491 492
	list_for_each_entry(nn, &pf->vnics, vnic_list) {
		err = nfp_net_pf_init_vnic(pf, nn, id);
J
Jakub Kicinski 已提交
493 494 495 496 497 498 499 500 501
		if (err)
			goto err_prev_deinit;

		id++;
	}

	return 0;

err_prev_deinit:
502 503
	list_for_each_entry_continue_reverse(nn, &pf->vnics, vnic_list)
		nfp_net_pf_clean_vnic(pf, nn);
J
Jakub Kicinski 已提交
504 505 506 507
	nfp_net_irqs_disable(pf->pdev);
err_vec_free:
	kfree(pf->irq_entries);
err_nn_free:
J
Jakub Kicinski 已提交
508
	nfp_net_pf_free_vnics(pf);
J
Jakub Kicinski 已提交
509 510 511
	return err;
}

512 513 514 515 516 517 518 519 520 521
static int nfp_net_pf_app_init(struct nfp_pf *pf)
{
	pf->app = nfp_app_alloc(pf);

	return PTR_ERR_OR_ZERO(pf->app);
}

static void nfp_net_pf_app_clean(struct nfp_pf *pf)
{
	nfp_app_free(pf->app);
S
Simon Horman 已提交
522
	pf->app = NULL;
523 524
}

J
Jakub Kicinski 已提交
525 526 527 528 529 530 531
static void nfp_net_pci_remove_finish(struct nfp_pf *pf)
{
	nfp_net_debugfs_dir_clean(&pf->ddir);

	nfp_net_irqs_disable(pf->pdev);
	kfree(pf->irq_entries);

532 533
	nfp_net_pf_app_clean(pf);

J
Jakub Kicinski 已提交
534 535
	nfp_cpp_area_release_free(pf->rx_area);
	nfp_cpp_area_release_free(pf->tx_area);
J
Jakub Kicinski 已提交
536
	nfp_cpp_area_release_free(pf->data_vnic_bar);
J
Jakub Kicinski 已提交
537 538
}

J
Jakub Kicinski 已提交
539 540 541 542 543 544 545 546 547 548
static int
nfp_net_eth_port_update(struct nfp_cpp *cpp, struct nfp_port *port,
			struct nfp_eth_table *eth_table)
{
	struct nfp_eth_table_port *eth_port;

	ASSERT_RTNL();

	eth_port = nfp_net_find_port(eth_table, port->eth_id);
	if (!eth_port) {
549
		set_bit(NFP_PORT_CHANGED, &port->flags);
J
Jakub Kicinski 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563
		nfp_warn(cpp, "Warning: port #%d not present after reconfig\n",
			 port->eth_id);
		return -EIO;
	}
	if (eth_port->override_changed) {
		nfp_warn(cpp, "Port #%d config changed, unregistering. Reboot required before port will be operational again.\n", port->eth_id);
		port->type = NFP_PORT_INVALID;
	}

	memcpy(port->eth_port, eth_port, sizeof(*eth_port));

	return 0;
}

J
Jakub Kicinski 已提交
564
static void nfp_net_refresh_vnics(struct work_struct *work)
J
Jakub Kicinski 已提交
565 566 567
{
	struct nfp_pf *pf = container_of(work, struct nfp_pf,
					 port_refresh_work);
568
	struct nfp_eth_table *eth_table;
J
Jakub Kicinski 已提交
569
	struct nfp_net *nn, *next;
570
	struct nfp_port *port;
J
Jakub Kicinski 已提交
571

J
Jakub Kicinski 已提交
572
	mutex_lock(&pf->lock);
J
Jakub Kicinski 已提交
573 574

	/* Check for nfp_net_pci_remove() racing against us */
J
Jakub Kicinski 已提交
575
	if (list_empty(&pf->vnics))
J
Jakub Kicinski 已提交
576 577
		goto out;

578 579
	/* Update state of all ports */
	rtnl_lock();
580 581
	list_for_each_entry(port, &pf->ports, port_list)
		clear_bit(NFP_PORT_CHANGED, &port->flags);
582 583 584

	eth_table = nfp_eth_read_ports(pf->cpp);
	if (!eth_table) {
585 586 587
		list_for_each_entry(port, &pf->ports, port_list)
			if (__nfp_port_get_eth_port(port))
				set_bit(NFP_PORT_CHANGED, &port->flags);
588
		rtnl_unlock();
589 590 591 592
		nfp_err(pf->cpp, "Error refreshing port config!\n");
		goto out;
	}

593 594 595
	list_for_each_entry(port, &pf->ports, port_list)
		if (__nfp_port_get_eth_port(port))
			nfp_net_eth_port_update(pf->cpp, port, eth_table);
596 597
	rtnl_unlock();

J
Jakub Kicinski 已提交
598
	kfree(eth_table);
599

600
	/* Shoot off the ports which became invalid */
J
Jakub Kicinski 已提交
601
	list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list) {
J
Jakub Kicinski 已提交
602
		if (!nn->port || nn->port->type != NFP_PORT_INVALID)
J
Jakub Kicinski 已提交
603 604
			continue;

605
		nfp_net_pf_clean_vnic(pf, nn);
606
		nfp_net_pf_free_vnic(pf, nn);
J
Jakub Kicinski 已提交
607 608
	}

J
Jakub Kicinski 已提交
609
	if (list_empty(&pf->vnics))
J
Jakub Kicinski 已提交
610 611
		nfp_net_pci_remove_finish(pf);
out:
J
Jakub Kicinski 已提交
612
	mutex_unlock(&pf->lock);
J
Jakub Kicinski 已提交
613 614
}

J
Jakub Kicinski 已提交
615
void nfp_net_refresh_port_table(struct nfp_port *port)
J
Jakub Kicinski 已提交
616
{
J
Jakub Kicinski 已提交
617
	struct nfp_pf *pf = port->app->pf;
J
Jakub Kicinski 已提交
618

619 620
	set_bit(NFP_PORT_CHANGED, &port->flags);

621 622
	schedule_work(&pf->port_refresh_work);
}
J
Jakub Kicinski 已提交
623

J
Jakub Kicinski 已提交
624
int nfp_net_refresh_eth_port(struct nfp_port *port)
625
{
J
Jakub Kicinski 已提交
626
	struct nfp_cpp *cpp = port->app->cpp;
627
	struct nfp_eth_table *eth_table;
J
Jakub Kicinski 已提交
628
	int ret;
J
Jakub Kicinski 已提交
629

630 631
	clear_bit(NFP_PORT_CHANGED, &port->flags);

J
Jakub Kicinski 已提交
632
	eth_table = nfp_eth_read_ports(cpp);
633
	if (!eth_table) {
634
		set_bit(NFP_PORT_CHANGED, &port->flags);
J
Jakub Kicinski 已提交
635
		nfp_err(cpp, "Error refreshing port state table!\n");
636 637
		return -EIO;
	}
J
Jakub Kicinski 已提交
638

J
Jakub Kicinski 已提交
639
	ret = nfp_net_eth_port_update(cpp, port, eth_table);
J
Jakub Kicinski 已提交
640

641
	kfree(eth_table);
J
Jakub Kicinski 已提交
642

J
Jakub Kicinski 已提交
643
	return ret;
J
Jakub Kicinski 已提交
644 645
}

J
Jakub Kicinski 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658
/*
 * PCI device functions
 */
int nfp_net_pci_probe(struct nfp_pf *pf)
{
	u8 __iomem *ctrl_bar, *tx_bar, *rx_bar;
	u32 total_tx_qcs, total_rx_qcs;
	struct nfp_net_fw_version fw_ver;
	u32 tx_area_sz, rx_area_sz;
	u32 start_q;
	int stride;
	int err;

J
Jakub Kicinski 已提交
659
	INIT_WORK(&pf->port_refresh_work, nfp_net_refresh_vnics);
660

J
Jakub Kicinski 已提交
661 662 663 664 665 666
	/* Verify that the board has completed initialization */
	if (!nfp_is_ready(pf->cpp)) {
		nfp_err(pf->cpp, "NFP is not ready for NIC operation.\n");
		return -EINVAL;
	}

J
Jakub Kicinski 已提交
667 668
	mutex_lock(&pf->lock);
	pf->max_data_vnics = nfp_net_pf_get_num_ports(pf);
J
Jakub Kicinski 已提交
669 670

	ctrl_bar = nfp_net_pf_map_ctrl_bar(pf);
671 672 673 674
	if (!ctrl_bar) {
		err = pf->fw_loaded ? -EINVAL : -EPROBE_DEFER;
		goto err_unlock;
	}
J
Jakub Kicinski 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740

	nfp_net_get_fw_version(&fw_ver, ctrl_bar);
	if (fw_ver.resv || fw_ver.class != NFP_NET_CFG_VERSION_CLASS_GENERIC) {
		nfp_err(pf->cpp, "Unknown Firmware ABI %d.%d.%d.%d\n",
			fw_ver.resv, fw_ver.class, fw_ver.major, fw_ver.minor);
		err = -EINVAL;
		goto err_ctrl_unmap;
	}

	/* Determine stride */
	if (nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 1)) {
		stride = 2;
		nfp_warn(pf->cpp, "OBSOLETE Firmware detected - VF isolation not available\n");
	} else {
		switch (fw_ver.major) {
		case 1 ... 4:
			stride = 4;
			break;
		default:
			nfp_err(pf->cpp, "Unsupported Firmware ABI %d.%d.%d.%d\n",
				fw_ver.resv, fw_ver.class,
				fw_ver.major, fw_ver.minor);
			err = -EINVAL;
			goto err_ctrl_unmap;
		}
	}

	/* Find how many QC structs need to be mapped */
	total_tx_qcs = nfp_net_pf_total_qcs(pf, ctrl_bar, stride,
					    NFP_NET_CFG_START_TXQ,
					    NFP_NET_CFG_MAX_TXRINGS);
	total_rx_qcs = nfp_net_pf_total_qcs(pf, ctrl_bar, stride,
					    NFP_NET_CFG_START_RXQ,
					    NFP_NET_CFG_MAX_RXRINGS);
	if (!total_tx_qcs || !total_rx_qcs) {
		nfp_err(pf->cpp, "Invalid PF QC configuration [%d,%d]\n",
			total_tx_qcs, total_rx_qcs);
		err = -EINVAL;
		goto err_ctrl_unmap;
	}

	tx_area_sz = NFP_QCP_QUEUE_ADDR_SZ * total_tx_qcs;
	rx_area_sz = NFP_QCP_QUEUE_ADDR_SZ * total_rx_qcs;

	/* Map TX queues */
	start_q = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
	tx_bar = nfp_net_map_area(pf->cpp, "net.tx", 0, 0,
				  NFP_PCIE_QUEUE(start_q),
				  tx_area_sz, &pf->tx_area);
	if (IS_ERR(tx_bar)) {
		nfp_err(pf->cpp, "Failed to map TX area.\n");
		err = PTR_ERR(tx_bar);
		goto err_ctrl_unmap;
	}

	/* Map RX queues */
	start_q = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
	rx_bar = nfp_net_map_area(pf->cpp, "net.rx", 0, 0,
				  NFP_PCIE_QUEUE(start_q),
				  rx_area_sz, &pf->rx_area);
	if (IS_ERR(rx_bar)) {
		nfp_err(pf->cpp, "Failed to map RX area.\n");
		err = PTR_ERR(rx_bar);
		goto err_unmap_tx;
	}

741 742 743 744
	err = nfp_net_pf_app_init(pf);
	if (err)
		goto err_unmap_rx;

J
Jakub Kicinski 已提交
745 746
	pf->ddir = nfp_net_debugfs_device_add(pf->pdev);

J
Jakub Kicinski 已提交
747 748
	err = nfp_net_pf_spawn_vnics(pf, ctrl_bar, tx_bar, rx_bar,
				     stride, &fw_ver);
J
Jakub Kicinski 已提交
749 750 751
	if (err)
		goto err_clean_ddir;

J
Jakub Kicinski 已提交
752
	mutex_unlock(&pf->lock);
753

J
Jakub Kicinski 已提交
754 755 756 757
	return 0;

err_clean_ddir:
	nfp_net_debugfs_dir_clean(&pf->ddir);
758 759
	nfp_net_pf_app_clean(pf);
err_unmap_rx:
J
Jakub Kicinski 已提交
760 761 762 763
	nfp_cpp_area_release_free(pf->rx_area);
err_unmap_tx:
	nfp_cpp_area_release_free(pf->tx_area);
err_ctrl_unmap:
J
Jakub Kicinski 已提交
764
	nfp_cpp_area_release_free(pf->data_vnic_bar);
765
err_unlock:
J
Jakub Kicinski 已提交
766
	mutex_unlock(&pf->lock);
J
Jakub Kicinski 已提交
767 768 769 770 771 772 773
	return err;
}

void nfp_net_pci_remove(struct nfp_pf *pf)
{
	struct nfp_net *nn;

J
Jakub Kicinski 已提交
774 775
	mutex_lock(&pf->lock);
	if (list_empty(&pf->vnics))
776 777
		goto out;

778 779
	list_for_each_entry(nn, &pf->vnics, vnic_list)
		nfp_net_pf_clean_vnic(pf, nn);
J
Jakub Kicinski 已提交
780

J
Jakub Kicinski 已提交
781
	nfp_net_pf_free_vnics(pf);
J
Jakub Kicinski 已提交
782

J
Jakub Kicinski 已提交
783
	nfp_net_pci_remove_finish(pf);
784
out:
J
Jakub Kicinski 已提交
785
	mutex_unlock(&pf->lock);
J
Jakub Kicinski 已提交
786 787

	cancel_work_sync(&pf->port_refresh_work);
J
Jakub Kicinski 已提交
788
}