nfp_net_main.c 20.7 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
/*
 * 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>
46
#include <linux/lockdep.h>
J
Jakub Kicinski 已提交
47 48 49 50
#include <linux/pci.h>
#include <linux/pci_regs.h>
#include <linux/msi.h>
#include <linux/random.h>
J
Jakub Kicinski 已提交
51
#include <linux/rtnetlink.h>
J
Jakub Kicinski 已提交
52 53 54 55

#include "nfpcore/nfp.h"
#include "nfpcore/nfp_cpp.h"
#include "nfpcore/nfp_nffw.h"
56
#include "nfpcore/nfp_nsp.h"
J
Jakub Kicinski 已提交
57
#include "nfpcore/nfp6000_pcie.h"
58
#include "nfp_app.h"
J
Jakub Kicinski 已提交
59 60 61
#include "nfp_net_ctrl.h"
#include "nfp_net.h"
#include "nfp_main.h"
J
Jakub Kicinski 已提交
62
#include "nfp_port.h"
J
Jakub Kicinski 已提交
63 64 65

#define NFP_PF_CSR_SLICE_SIZE	(32 * 1024)

66
static int nfp_is_ready(struct nfp_pf *pf)
J
Jakub Kicinski 已提交
67 68 69 70 71
{
	const char *cp;
	long state;
	int err;

72
	cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
J
Jakub Kicinski 已提交
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 134
	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);
}

135 136
/**
 * nfp_net_get_mac_addr() - Get the MAC address.
137
 * @pf:       NFP PF handle
138
 * @port:     NFP port structure
139 140 141 142 143
 * @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 已提交
144
void
145
nfp_net_get_mac_addr(struct nfp_pf *pf, struct nfp_port *port, unsigned int id)
J
Jakub Kicinski 已提交
146
{
J
Jakub Kicinski 已提交
147
	struct nfp_eth_table_port *eth_port;
J
Jakub Kicinski 已提交
148 149 150 151
	u8 mac_addr[ETH_ALEN];
	const char *mac_str;
	char name[32];

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

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

161
	mac_str = nfp_hwinfo_lookup(pf->hwinfo, name);
J
Jakub Kicinski 已提交
162
	if (!mac_str) {
163 164
		nfp_warn(pf->cpp, "Can't lookup MAC address. Generate\n");
		eth_hw_addr_random(port->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 172 173
		nfp_warn(pf->cpp, "Can't parse MAC address (%s). Generate.\n",
			 mac_str);
		eth_hw_addr_random(port->netdev);
J
Jakub Kicinski 已提交
174 175 176
		return;
	}

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

J
Jakub Kicinski 已提交
181
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
static int
nfp_net_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format,
			       unsigned int default_val)
J
Jakub Kicinski 已提交
196 197 198 199 200
{
	char name[256];
	int err = 0;
	u64 val;

201
	snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp));
J
Jakub Kicinski 已提交
202

203
	val = nfp_rtsym_read_le(pf->rtbl, name, &err);
J
Jakub Kicinski 已提交
204
	if (err) {
205 206 207 208
		if (err == -ENOENT)
			return default_val;
		nfp_err(pf->cpp, "Unable to read symbol %s\n", name);
		return err;
J
Jakub Kicinski 已提交
209 210 211 212 213
	}

	return val;
}

214 215 216 217 218
static int nfp_net_pf_get_num_ports(struct nfp_pf *pf)
{
	return nfp_net_pf_rtsym_read_optional(pf, "nfd_cfg_pf%u_num_ports", 1);
}

J
Jakub Kicinski 已提交
219 220 221 222 223 224
static int nfp_net_pf_get_app_id(struct nfp_pf *pf)
{
	return nfp_net_pf_rtsym_read_optional(pf, "_pf%u_net_app_id",
					      NFP_APP_CORE_NIC);
}

225 226 227
static u8 __iomem *
nfp_net_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt,
		     unsigned int min_size, struct nfp_cpp_area **area)
J
Jakub Kicinski 已提交
228
{
229
	const struct nfp_rtsym *sym;
J
Jakub Kicinski 已提交
230
	char pf_symbol[256];
231
	u8 __iomem *mem;
J
Jakub Kicinski 已提交
232

233
	snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt,
234
		 nfp_cppcore_pcie_unit(pf->cpp));
J
Jakub Kicinski 已提交
235

236
	sym = nfp_rtsym_lookup(pf->rtbl, pf_symbol);
237
	if (!sym)
238
		return (u8 __iomem *)ERR_PTR(-ENOENT);
J
Jakub Kicinski 已提交
239

240 241 242
	if (sym->size < min_size) {
		nfp_err(pf->cpp, "PF symbol %s too small\n", pf_symbol);
		return (u8 __iomem *)ERR_PTR(-EINVAL);
J
Jakub Kicinski 已提交
243 244
	}

245 246 247 248 249 250
	mem = nfp_net_map_area(pf->cpp, name, sym->domain, sym->target,
			       sym->addr, sym->size, area);
	if (IS_ERR(mem)) {
		nfp_err(pf->cpp, "Failed to map PF symbol %s: %ld\n",
			pf_symbol, PTR_ERR(mem));
		return mem;
J
Jakub Kicinski 已提交
251 252
	}

253
	return mem;
J
Jakub Kicinski 已提交
254 255
}

256 257
static void nfp_net_pf_free_vnic(struct nfp_pf *pf, struct nfp_net *nn)
{
J
Jakub Kicinski 已提交
258
	nfp_port_free(nn->port);
259 260 261 262 263
	list_del(&nn->vnic_list);
	pf->num_vnics--;
	nfp_net_free(nn);
}

J
Jakub Kicinski 已提交
264
static void nfp_net_pf_free_vnics(struct nfp_pf *pf)
J
Jakub Kicinski 已提交
265
{
266
	struct nfp_net *nn, *next;
J
Jakub Kicinski 已提交
267

268 269 270
	list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list)
		if (nfp_net_is_data_vnic(nn))
			nfp_net_pf_free_vnic(pf, nn);
J
Jakub Kicinski 已提交
271 272 273
}

static struct nfp_net *
274
nfp_net_pf_alloc_vnic(struct nfp_pf *pf, bool needs_netdev,
275
		      void __iomem *ctrl_bar, void __iomem *qc_bar,
276
		      int stride, unsigned int eth_id)
J
Jakub Kicinski 已提交
277
{
278
	u32 tx_base, rx_base, n_tx_rings, n_rx_rings;
J
Jakub Kicinski 已提交
279
	struct nfp_net *nn;
J
Jakub Kicinski 已提交
280
	int err;
J
Jakub Kicinski 已提交
281

282 283
	tx_base = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
	rx_base = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
J
Jakub Kicinski 已提交
284 285 286
	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 已提交
287
	/* Allocate and initialise the vNIC */
288
	nn = nfp_net_alloc(pf->pdev, needs_netdev, n_tx_rings, n_rx_rings);
J
Jakub Kicinski 已提交
289 290 291
	if (IS_ERR(nn))
		return nn;

292
	nn->app = pf->app;
293
	nfp_net_get_fw_version(&nn->fw_ver, ctrl_bar);
294
	nn->dp.ctrl_bar = ctrl_bar;
295 296
	nn->tx_bar = qc_bar + tx_base * NFP_QCP_QUEUE_ADDR_SZ;
	nn->rx_bar = qc_bar + rx_base * NFP_QCP_QUEUE_ADDR_SZ;
297
	nn->dp.is_vf = 0;
J
Jakub Kicinski 已提交
298 299
	nn->stride_rx = stride;
	nn->stride_tx = stride;
J
Jakub Kicinski 已提交
300

301 302 303 304 305 306
	if (needs_netdev) {
		err = nfp_app_vnic_init(pf->app, nn, eth_id);
		if (err) {
			nfp_net_free(nn);
			return ERR_PTR(err);
		}
J
Jakub Kicinski 已提交
307
	}
308 309 310

	pf->num_vnics++;
	list_add_tail(&nn->vnic_list, &pf->vnics);
J
Jakub Kicinski 已提交
311 312 313 314 315

	return nn;
}

static int
J
Jakub Kicinski 已提交
316
nfp_net_pf_init_vnic(struct nfp_pf *pf, struct nfp_net *nn, unsigned int id)
J
Jakub Kicinski 已提交
317 318 319 320 321 322 323 324 325
{
	int err;

	/* 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;

326
	err = nfp_net_init(nn);
J
Jakub Kicinski 已提交
327 328 329
	if (err)
		return err;

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

332 333 334 335 336 337
	if (nn->port) {
		err = nfp_devlink_port_register(pf->app, nn->port);
		if (err)
			goto err_dfs_clean;
	}

J
Jakub Kicinski 已提交
338 339 340
	nfp_net_info(nn);

	return 0;
341 342 343 344 345

err_dfs_clean:
	nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
	nfp_net_clean(nn);
	return err;
J
Jakub Kicinski 已提交
346 347 348
}

static int
J
Jakub Kicinski 已提交
349
nfp_net_pf_alloc_vnics(struct nfp_pf *pf, void __iomem *ctrl_bar,
350
		       void __iomem *qc_bar, int stride)
J
Jakub Kicinski 已提交
351 352 353 354 355
{
	struct nfp_net *nn;
	unsigned int i;
	int err;

J
Jakub Kicinski 已提交
356
	for (i = 0; i < pf->max_data_vnics; i++) {
357
		nn = nfp_net_pf_alloc_vnic(pf, true, ctrl_bar, qc_bar,
358
					   stride, i);
359 360 361
		if (IS_ERR(nn)) {
			err = PTR_ERR(nn);
			goto err_free_prev;
J
Jakub Kicinski 已提交
362 363 364
		}

		ctrl_bar += NFP_PF_CSR_SLICE_SIZE;
365

J
Jakub Kicinski 已提交
366 367
		/* Kill the vNIC if app init marked it as invalid */
		if (nn->port && nn->port->type == NFP_PORT_INVALID) {
368 369 370
			nfp_net_pf_free_vnic(pf, nn);
			continue;
		}
J
Jakub Kicinski 已提交
371 372
	}

J
Jakub Kicinski 已提交
373
	if (list_empty(&pf->vnics))
374 375
		return -ENODEV;

J
Jakub Kicinski 已提交
376 377 378
	return 0;

err_free_prev:
J
Jakub Kicinski 已提交
379
	nfp_net_pf_free_vnics(pf);
J
Jakub Kicinski 已提交
380 381 382
	return err;
}

383 384
static void nfp_net_pf_clean_vnic(struct nfp_pf *pf, struct nfp_net *nn)
{
385 386
	if (nn->port)
		nfp_devlink_port_unregister(nn->port);
387 388
	nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
	nfp_net_clean(nn);
389
	nfp_app_vnic_clean(pf->app, nn);
390 391
}

392
static int nfp_net_pf_alloc_irqs(struct nfp_pf *pf)
J
Jakub Kicinski 已提交
393
{
394
	unsigned int wanted_irqs, num_irqs, vnics_left, irqs_left;
J
Jakub Kicinski 已提交
395 396 397 398
	struct nfp_net *nn;

	/* Get MSI-X vectors */
	wanted_irqs = 0;
J
Jakub Kicinski 已提交
399
	list_for_each_entry(nn, &pf->vnics, vnic_list)
400
		wanted_irqs += NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs;
J
Jakub Kicinski 已提交
401 402
	pf->irq_entries = kcalloc(wanted_irqs, sizeof(*pf->irq_entries),
				  GFP_KERNEL);
403 404
	if (!pf->irq_entries)
		return -ENOMEM;
J
Jakub Kicinski 已提交
405 406

	num_irqs = nfp_net_irqs_alloc(pf->pdev, pf->irq_entries,
J
Jakub Kicinski 已提交
407
				      NFP_NET_MIN_VNIC_IRQS * pf->num_vnics,
J
Jakub Kicinski 已提交
408 409
				      wanted_irqs);
	if (!num_irqs) {
410 411 412
		nfp_warn(pf->cpp, "Unable to allocate MSI-X vectors\n");
		kfree(pf->irq_entries);
		return -ENOMEM;
J
Jakub Kicinski 已提交
413 414
	}

J
Jakub Kicinski 已提交
415
	/* Distribute IRQs to vNICs */
J
Jakub Kicinski 已提交
416
	irqs_left = num_irqs;
J
Jakub Kicinski 已提交
417 418
	vnics_left = pf->num_vnics;
	list_for_each_entry(nn, &pf->vnics, vnic_list) {
J
Jakub Kicinski 已提交
419 420
		unsigned int n;

421 422
		n = min(NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs,
			DIV_ROUND_UP(irqs_left, vnics_left));
J
Jakub Kicinski 已提交
423 424 425
		nfp_net_irqs_assign(nn, &pf->irq_entries[num_irqs - irqs_left],
				    n);
		irqs_left -= n;
J
Jakub Kicinski 已提交
426
		vnics_left--;
J
Jakub Kicinski 已提交
427 428
	}

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	return 0;
}

static void nfp_net_pf_free_irqs(struct nfp_pf *pf)
{
	nfp_net_irqs_disable(pf->pdev);
	kfree(pf->irq_entries);
}

static int nfp_net_pf_init_vnics(struct nfp_pf *pf)
{
	struct nfp_net *nn;
	unsigned int id;
	int err;

J
Jakub Kicinski 已提交
444
	/* Finish vNIC init and register */
J
Jakub Kicinski 已提交
445
	id = 0;
J
Jakub Kicinski 已提交
446
	list_for_each_entry(nn, &pf->vnics, vnic_list) {
447 448
		if (!nfp_net_is_data_vnic(nn))
			continue;
J
Jakub Kicinski 已提交
449
		err = nfp_net_pf_init_vnic(pf, nn, id);
J
Jakub Kicinski 已提交
450 451 452 453 454 455 456 457 458
		if (err)
			goto err_prev_deinit;

		id++;
	}

	return 0;

err_prev_deinit:
459
	list_for_each_entry_continue_reverse(nn, &pf->vnics, vnic_list)
460 461
		if (nfp_net_is_data_vnic(nn))
			nfp_net_pf_clean_vnic(pf, nn);
J
Jakub Kicinski 已提交
462 463 464
	return err;
}

465 466
static int
nfp_net_pf_app_init(struct nfp_pf *pf, u8 __iomem *qc_bar, unsigned int stride)
467
{
468
	u8 __iomem *ctrl_bar;
J
Jakub Kicinski 已提交
469 470 471 472 473 474 475 476 477 478
	int err;

	pf->app = nfp_app_alloc(pf, nfp_net_pf_get_app_id(pf));
	if (IS_ERR(pf->app))
		return PTR_ERR(pf->app);

	err = nfp_app_init(pf->app);
	if (err)
		goto err_free;

479 480 481 482 483 484 485
	if (!nfp_app_needs_ctrl_vnic(pf->app))
		return 0;

	ctrl_bar = nfp_net_pf_map_rtsym(pf, "net.ctrl", "_pf%u_net_ctrl_bar",
					NFP_PF_CSR_SLICE_SIZE,
					&pf->ctrl_vnic_bar);
	if (IS_ERR(ctrl_bar)) {
486
		nfp_err(pf->cpp, "Failed to find data vNIC memory symbol\n");
487 488 489 490 491 492 493 494 495 496 497
		err = PTR_ERR(ctrl_bar);
		goto err_free;
	}

	pf->ctrl_vnic =	nfp_net_pf_alloc_vnic(pf, false, ctrl_bar, qc_bar,
					      stride, 0);
	if (IS_ERR(pf->ctrl_vnic)) {
		err = PTR_ERR(pf->ctrl_vnic);
		goto err_unmap;
	}

J
Jakub Kicinski 已提交
498
	return 0;
499

500 501
err_unmap:
	nfp_cpp_area_release_free(pf->ctrl_vnic_bar);
J
Jakub Kicinski 已提交
502 503 504
err_free:
	nfp_app_free(pf->app);
	return err;
505 506 507 508
}

static void nfp_net_pf_app_clean(struct nfp_pf *pf)
{
509 510 511 512
	if (pf->ctrl_vnic) {
		nfp_net_pf_free_vnic(pf, pf->ctrl_vnic);
		nfp_cpp_area_release_free(pf->ctrl_vnic_bar);
	}
513
	nfp_app_free(pf->app);
S
Simon Horman 已提交
514
	pf->app = NULL;
515 516
}

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
static int nfp_net_pf_app_start_ctrl(struct nfp_pf *pf)
{
	int err;

	if (!pf->ctrl_vnic)
		return 0;
	err = nfp_net_pf_init_vnic(pf, pf->ctrl_vnic, 0);
	if (err)
		return err;

	err = nfp_ctrl_open(pf->ctrl_vnic);
	if (err)
		goto err_clean_ctrl;

	return 0;

err_clean_ctrl:
	nfp_net_pf_clean_vnic(pf, pf->ctrl_vnic);
	return err;
}

static void nfp_net_pf_app_stop_ctrl(struct nfp_pf *pf)
{
	if (!pf->ctrl_vnic)
		return;
	nfp_ctrl_close(pf->ctrl_vnic);
	nfp_net_pf_clean_vnic(pf, pf->ctrl_vnic);
}

static int nfp_net_pf_app_start(struct nfp_pf *pf)
{
	int err;

	err = nfp_net_pf_app_start_ctrl(pf);
	if (err)
		return err;

	err = nfp_app_start(pf->app, pf->ctrl_vnic);
	if (err)
		goto err_ctrl_stop;

	return 0;

err_ctrl_stop:
	nfp_net_pf_app_stop_ctrl(pf);
	return err;
}

static void nfp_net_pf_app_stop(struct nfp_pf *pf)
{
	nfp_app_stop(pf->app);
	nfp_net_pf_app_stop_ctrl(pf);
}

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 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
static void nfp_net_pci_unmap_mem(struct nfp_pf *pf)
{
	if (pf->vf_cfg_bar)
		nfp_cpp_area_release_free(pf->vf_cfg_bar);
	if (pf->mac_stats_bar)
		nfp_cpp_area_release_free(pf->mac_stats_bar);
	nfp_cpp_area_release_free(pf->qc_area);
	nfp_cpp_area_release_free(pf->data_vnic_bar);
}

static int nfp_net_pci_map_mem(struct nfp_pf *pf)
{
	u32 ctrl_bar_sz;
	u8 __iomem *mem;
	int err;

	ctrl_bar_sz = pf->max_data_vnics * NFP_PF_CSR_SLICE_SIZE;
	mem = nfp_net_pf_map_rtsym(pf, "net.ctrl", "_pf%d_net_bar0",
				   ctrl_bar_sz, &pf->data_vnic_bar);
	if (IS_ERR(mem)) {
		nfp_err(pf->cpp, "Failed to find data vNIC memory symbol\n");
		err = PTR_ERR(mem);
		if (!pf->fw_loaded && err == -ENOENT)
			err = -EPROBE_DEFER;
		return err;
	}

	pf->mac_stats_mem = nfp_net_pf_map_rtsym(pf, "net.macstats",
						 "_mac_stats",
						 NFP_MAC_STATS_SIZE *
						 (pf->eth_tbl->max_index + 1),
						 &pf->mac_stats_bar);
	if (IS_ERR(pf->mac_stats_mem)) {
		if (PTR_ERR(pf->mac_stats_mem) != -ENOENT) {
			err = PTR_ERR(pf->mac_stats_mem);
			goto err_unmap_ctrl;
		}
		pf->mac_stats_mem = NULL;
	}

	pf->vf_cfg_mem = nfp_net_pf_map_rtsym(pf, "net.vfcfg",
					      "_pf%d_net_vf_bar",
					      NFP_NET_CFG_BAR_SZ *
					      pf->limit_vfs, &pf->vf_cfg_bar);
	if (IS_ERR(pf->vf_cfg_mem)) {
		if (PTR_ERR(pf->vf_cfg_mem) != -ENOENT) {
			err = PTR_ERR(pf->vf_cfg_mem);
			goto err_unmap_mac_stats;
		}
		pf->vf_cfg_mem = NULL;
	}

	mem = nfp_net_map_area(pf->cpp, "net.qc", 0, 0,
			       NFP_PCIE_QUEUE(0), NFP_QCP_QUEUE_AREA_SZ,
			       &pf->qc_area);
	if (IS_ERR(mem)) {
		nfp_err(pf->cpp, "Failed to map Queue Controller area.\n");
		err = PTR_ERR(mem);
		goto err_unmap_vf_cfg;
	}

	return 0;

err_unmap_vf_cfg:
	if (pf->vf_cfg_bar)
		nfp_cpp_area_release_free(pf->vf_cfg_bar);
err_unmap_mac_stats:
	if (pf->mac_stats_bar)
		nfp_cpp_area_release_free(pf->mac_stats_bar);
err_unmap_ctrl:
	nfp_cpp_area_release_free(pf->data_vnic_bar);
	return err;
}

J
Jakub Kicinski 已提交
645 646
static void nfp_net_pci_remove_finish(struct nfp_pf *pf)
{
647 648
	nfp_net_pf_app_stop(pf);
	/* stop app first, to avoid double free of ctrl vNIC's ddir */
J
Jakub Kicinski 已提交
649 650
	nfp_net_debugfs_dir_clean(&pf->ddir);

651
	nfp_net_pf_free_irqs(pf);
652
	nfp_net_pf_app_clean(pf);
653
	nfp_net_pci_unmap_mem(pf);
J
Jakub Kicinski 已提交
654 655
}

J
Jakub Kicinski 已提交
656 657 658 659 660 661 662 663 664 665
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) {
666
		set_bit(NFP_PORT_CHANGED, &port->flags);
J
Jakub Kicinski 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680
		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;
}

681
int nfp_net_refresh_port_table_sync(struct nfp_pf *pf)
J
Jakub Kicinski 已提交
682
{
683
	struct nfp_eth_table *eth_table;
J
Jakub Kicinski 已提交
684
	struct nfp_net *nn, *next;
685
	struct nfp_port *port;
J
Jakub Kicinski 已提交
686

687
	lockdep_assert_held(&pf->lock);
J
Jakub Kicinski 已提交
688 689

	/* Check for nfp_net_pci_remove() racing against us */
J
Jakub Kicinski 已提交
690
	if (list_empty(&pf->vnics))
691
		return 0;
J
Jakub Kicinski 已提交
692

693 694
	/* Update state of all ports */
	rtnl_lock();
695 696
	list_for_each_entry(port, &pf->ports, port_list)
		clear_bit(NFP_PORT_CHANGED, &port->flags);
697 698 699

	eth_table = nfp_eth_read_ports(pf->cpp);
	if (!eth_table) {
700 701 702
		list_for_each_entry(port, &pf->ports, port_list)
			if (__nfp_port_get_eth_port(port))
				set_bit(NFP_PORT_CHANGED, &port->flags);
703
		rtnl_unlock();
704
		nfp_err(pf->cpp, "Error refreshing port config!\n");
705
		return -EIO;
706 707
	}

708 709 710
	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);
711 712
	rtnl_unlock();

J
Jakub Kicinski 已提交
713
	kfree(eth_table);
714

715
	/* Shoot off the ports which became invalid */
J
Jakub Kicinski 已提交
716
	list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list) {
J
Jakub Kicinski 已提交
717
		if (!nn->port || nn->port->type != NFP_PORT_INVALID)
J
Jakub Kicinski 已提交
718 719
			continue;

720
		nfp_net_pf_clean_vnic(pf, nn);
721
		nfp_net_pf_free_vnic(pf, nn);
J
Jakub Kicinski 已提交
722 723
	}

J
Jakub Kicinski 已提交
724
	if (list_empty(&pf->vnics))
J
Jakub Kicinski 已提交
725
		nfp_net_pci_remove_finish(pf);
726 727 728 729 730 731 732 733 734 735 736

	return 0;
}

static void nfp_net_refresh_vnics(struct work_struct *work)
{
	struct nfp_pf *pf = container_of(work, struct nfp_pf,
					 port_refresh_work);

	mutex_lock(&pf->lock);
	nfp_net_refresh_port_table_sync(pf);
J
Jakub Kicinski 已提交
737
	mutex_unlock(&pf->lock);
J
Jakub Kicinski 已提交
738 739
}

J
Jakub Kicinski 已提交
740
void nfp_net_refresh_port_table(struct nfp_port *port)
J
Jakub Kicinski 已提交
741
{
J
Jakub Kicinski 已提交
742
	struct nfp_pf *pf = port->app->pf;
J
Jakub Kicinski 已提交
743

744 745
	set_bit(NFP_PORT_CHANGED, &port->flags);

746 747
	schedule_work(&pf->port_refresh_work);
}
J
Jakub Kicinski 已提交
748

J
Jakub Kicinski 已提交
749
int nfp_net_refresh_eth_port(struct nfp_port *port)
750
{
J
Jakub Kicinski 已提交
751
	struct nfp_cpp *cpp = port->app->cpp;
752
	struct nfp_eth_table *eth_table;
J
Jakub Kicinski 已提交
753
	int ret;
J
Jakub Kicinski 已提交
754

755 756
	clear_bit(NFP_PORT_CHANGED, &port->flags);

J
Jakub Kicinski 已提交
757
	eth_table = nfp_eth_read_ports(cpp);
758
	if (!eth_table) {
759
		set_bit(NFP_PORT_CHANGED, &port->flags);
J
Jakub Kicinski 已提交
760
		nfp_err(cpp, "Error refreshing port state table!\n");
761 762
		return -EIO;
	}
J
Jakub Kicinski 已提交
763

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

766
	kfree(eth_table);
J
Jakub Kicinski 已提交
767

J
Jakub Kicinski 已提交
768
	return ret;
J
Jakub Kicinski 已提交
769 770
}

J
Jakub Kicinski 已提交
771 772 773 774 775 776
/*
 * PCI device functions
 */
int nfp_net_pci_probe(struct nfp_pf *pf)
{
	struct nfp_net_fw_version fw_ver;
777
	u8 __iomem *ctrl_bar, *qc_bar;
J
Jakub Kicinski 已提交
778 779 780
	int stride;
	int err;

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

J
Jakub Kicinski 已提交
783
	/* Verify that the board has completed initialization */
784
	if (!nfp_is_ready(pf)) {
J
Jakub Kicinski 已提交
785 786 787 788
		nfp_err(pf->cpp, "NFP is not ready for NIC operation.\n");
		return -EINVAL;
	}

J
Jakub Kicinski 已提交
789 790
	mutex_lock(&pf->lock);
	pf->max_data_vnics = nfp_net_pf_get_num_ports(pf);
791 792 793 794
	if ((int)pf->max_data_vnics < 0) {
		err = pf->max_data_vnics;
		goto err_unlock;
	}
J
Jakub Kicinski 已提交
795

796 797
	err = nfp_net_pci_map_mem(pf);
	if (err)
798
		goto err_unlock;
799 800 801 802 803 804

	ctrl_bar = nfp_cpp_area_iomem(pf->data_vnic_bar);
	qc_bar = nfp_cpp_area_iomem(pf->qc_area);
	if (!ctrl_bar || !qc_bar) {
		err = -EIO;
		goto err_unmap;
805
	}
J
Jakub Kicinski 已提交
806 807 808 809 810 811

	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;
812
		goto err_unmap;
J
Jakub Kicinski 已提交
813 814 815 816 817 818 819 820
	}

	/* 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) {
821
		case 1 ... 5:
J
Jakub Kicinski 已提交
822 823 824 825 826 827 828
			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;
829
			goto err_unmap;
J
Jakub Kicinski 已提交
830 831 832
		}
	}

833
	err = nfp_net_pf_app_init(pf, qc_bar, stride);
834
	if (err)
835
		goto err_unmap;
836

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

839 840
	/* Allocate the vnics and do basic init */
	err = nfp_net_pf_alloc_vnics(pf, ctrl_bar, qc_bar, stride);
J
Jakub Kicinski 已提交
841 842 843
	if (err)
		goto err_clean_ddir;

844 845 846 847
	err = nfp_net_pf_alloc_irqs(pf);
	if (err)
		goto err_free_vnics;

848
	err = nfp_net_pf_app_start(pf);
849 850 851
	if (err)
		goto err_free_irqs;

852 853 854 855
	err = nfp_net_pf_init_vnics(pf);
	if (err)
		goto err_stop_app;

J
Jakub Kicinski 已提交
856
	mutex_unlock(&pf->lock);
857

J
Jakub Kicinski 已提交
858 859
	return 0;

860 861
err_stop_app:
	nfp_net_pf_app_stop(pf);
862 863 864 865
err_free_irqs:
	nfp_net_pf_free_irqs(pf);
err_free_vnics:
	nfp_net_pf_free_vnics(pf);
J
Jakub Kicinski 已提交
866 867
err_clean_ddir:
	nfp_net_debugfs_dir_clean(&pf->ddir);
868
	nfp_net_pf_app_clean(pf);
869 870
err_unmap:
	nfp_net_pci_unmap_mem(pf);
871
err_unlock:
J
Jakub Kicinski 已提交
872
	mutex_unlock(&pf->lock);
873
	cancel_work_sync(&pf->port_refresh_work);
J
Jakub Kicinski 已提交
874 875 876 877 878 879 880
	return err;
}

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

J
Jakub Kicinski 已提交
881 882
	mutex_lock(&pf->lock);
	if (list_empty(&pf->vnics))
883 884
		goto out;

885
	list_for_each_entry(nn, &pf->vnics, vnic_list)
886 887
		if (nfp_net_is_data_vnic(nn))
			nfp_net_pf_clean_vnic(pf, nn);
J
Jakub Kicinski 已提交
888

J
Jakub Kicinski 已提交
889
	nfp_net_pf_free_vnics(pf);
J
Jakub Kicinski 已提交
890

J
Jakub Kicinski 已提交
891
	nfp_net_pci_remove_finish(pf);
892
out:
J
Jakub Kicinski 已提交
893
	mutex_unlock(&pf->lock);
J
Jakub Kicinski 已提交
894 895

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