main.c 10.9 KB
Newer Older
S
Simon Horman 已提交
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
/*
 * Copyright (C) 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.
 */

#include <linux/etherdevice.h>
#include <linux/pci.h>
#include <linux/skbuff.h>
37
#include <linux/vmalloc.h>
S
Simon Horman 已提交
38 39 40
#include <net/devlink.h>
#include <net/dst_metadata.h>

41
#include "main.h"
S
Simon Horman 已提交
42
#include "../nfpcore/nfp_cpp.h"
43
#include "../nfpcore/nfp_nffw.h"
S
Simon Horman 已提交
44 45 46 47 48 49 50 51
#include "../nfpcore/nfp_nsp.h"
#include "../nfp_app.h"
#include "../nfp_main.h"
#include "../nfp_net.h"
#include "../nfp_net_repr.h"
#include "../nfp_port.h"
#include "./cmsg.h"

52 53
#define NFP_FLOWER_ALLOWED_VER 0x0001000000010000UL

S
Simon Horman 已提交
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static const char *nfp_flower_extra_cap(struct nfp_app *app, struct nfp_net *nn)
{
	return "FLOWER";
}

static enum devlink_eswitch_mode eswitch_mode_get(struct nfp_app *app)
{
	return DEVLINK_ESWITCH_MODE_SWITCHDEV;
}

static enum nfp_repr_type
nfp_flower_repr_get_type_and_port(struct nfp_app *app, u32 port_id, u8 *port)
{
	switch (FIELD_GET(NFP_FLOWER_CMSG_PORT_TYPE, port_id)) {
	case NFP_FLOWER_CMSG_PORT_TYPE_PHYS_PORT:
		*port = FIELD_GET(NFP_FLOWER_CMSG_PORT_PHYS_PORT_NUM,
				  port_id);
		return NFP_REPR_TYPE_PHYS_PORT;

	case NFP_FLOWER_CMSG_PORT_TYPE_PCIE_PORT:
		*port = FIELD_GET(NFP_FLOWER_CMSG_PORT_VNIC, port_id);
		if (FIELD_GET(NFP_FLOWER_CMSG_PORT_VNIC_TYPE, port_id) ==
		    NFP_FLOWER_CMSG_PORT_VNIC_TYPE_PF)
			return NFP_REPR_TYPE_PF;
		else
			return NFP_REPR_TYPE_VF;
	}

	return NFP_FLOWER_CMSG_PORT_TYPE_UNSPEC;
}

static struct net_device *
nfp_flower_repr_get(struct nfp_app *app, u32 port_id)
{
	enum nfp_repr_type repr_type;
	struct nfp_reprs *reprs;
	u8 port = 0;

	repr_type = nfp_flower_repr_get_type_and_port(app, port_id, &port);

	reprs = rcu_dereference(app->reprs[repr_type]);
	if (!reprs)
		return NULL;

	if (port >= reprs->num_reprs)
		return NULL;

	return reprs->reprs[port];
}

104 105
static int
nfp_flower_repr_netdev_open(struct nfp_app *app, struct nfp_repr *repr)
S
Simon Horman 已提交
106 107 108
{
	int err;

109
	err = nfp_flower_cmsg_portmod(repr, true);
S
Simon Horman 已提交
110 111 112
	if (err)
		return err;

113 114
	netif_carrier_on(repr->netdev);
	netif_tx_wake_all_queues(repr->netdev);
S
Simon Horman 已提交
115 116 117 118

	return 0;
}

119 120
static int
nfp_flower_repr_netdev_stop(struct nfp_app *app, struct nfp_repr *repr)
S
Simon Horman 已提交
121
{
122 123
	netif_carrier_off(repr->netdev);
	netif_tx_disable(repr->netdev);
S
Simon Horman 已提交
124

125
	return nfp_flower_cmsg_portmod(repr, false);
S
Simon Horman 已提交
126 127
}

128 129 130 131 132 133 134 135 136 137 138 139 140
static void nfp_flower_sriov_disable(struct nfp_app *app)
{
	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_VF);
}

static int
nfp_flower_spawn_vnic_reprs(struct nfp_app *app,
			    enum nfp_flower_cmsg_port_vnic_type vnic_type,
			    enum nfp_repr_type repr_type, unsigned int cnt)
{
	u8 nfp_pcie = nfp_cppcore_pcie_unit(app->pf->cpp);
	struct nfp_flower_priv *priv = app->priv;
	struct nfp_reprs *reprs, *old_reprs;
141
	enum nfp_port_type port_type;
142 143 144
	const u8 queue = 0;
	int i, err;

145 146 147
	port_type = repr_type == NFP_REPR_TYPE_PF ? NFP_PORT_PF_PORT :
						    NFP_PORT_VF_PORT;

148 149 150 151 152
	reprs = nfp_reprs_alloc(cnt);
	if (!reprs)
		return -ENOMEM;

	for (i = 0; i < cnt; i++) {
153
		struct nfp_port *port;
154 155 156 157 158 159 160 161
		u32 port_id;

		reprs->reprs[i] = nfp_repr_alloc(app);
		if (!reprs->reprs[i]) {
			err = -ENOMEM;
			goto err_reprs_clean;
		}

162 163 164
		/* For now we only support 1 PF */
		WARN_ON(repr_type == NFP_REPR_TYPE_PF && i);

165 166 167
		port = nfp_port_alloc(app, port_type, reprs->reprs[i]);
		if (repr_type == NFP_REPR_TYPE_PF) {
			port->pf_id = i;
168
			port->vnic = priv->nn->dp.ctrl_bar;
169
		} else {
170
			port->pf_id = 0;
171
			port->vf_id = i;
172 173
			port->vnic =
				app->pf->vf_cfg_mem + i * NFP_NET_CFG_BAR_SZ;
174 175
		}

176 177 178 179 180
		eth_hw_addr_random(reprs->reprs[i]);

		port_id = nfp_flower_cmsg_pcie_port(nfp_pcie, vnic_type,
						    i, queue);
		err = nfp_repr_init(app, reprs->reprs[i],
181 182 183
				    port_id, port, priv->nn->dp.netdev);
		if (err) {
			nfp_port_free(port);
184
			goto err_reprs_clean;
185
		}
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

		nfp_info(app->cpp, "%s%d Representor(%s) created\n",
			 repr_type == NFP_REPR_TYPE_PF ? "PF" : "VF", i,
			 reprs->reprs[i]->name);
	}

	old_reprs = nfp_app_reprs_set(app, repr_type, reprs);
	if (IS_ERR(old_reprs)) {
		err = PTR_ERR(old_reprs);
		goto err_reprs_clean;
	}

	return 0;
err_reprs_clean:
	nfp_reprs_clean_and_free(reprs);
	return err;
}

static int nfp_flower_sriov_enable(struct nfp_app *app, int num_vfs)
{
	return nfp_flower_spawn_vnic_reprs(app,
					   NFP_FLOWER_CMSG_PORT_VNIC_TYPE_VF,
					   NFP_REPR_TYPE_VF, num_vfs);
}

S
Simon Horman 已提交
211 212
static void nfp_flower_stop(struct nfp_app *app)
{
213
	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PF);
S
Simon Horman 已提交
214
	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
215

S
Simon Horman 已提交
216 217
}

218 219
static int
nfp_flower_spawn_phy_reprs(struct nfp_app *app, struct nfp_flower_priv *priv)
S
Simon Horman 已提交
220 221 222
{
	struct nfp_eth_table *eth_tbl = app->pf->eth_tbl;
	struct nfp_reprs *reprs, *old_reprs;
223
	struct sk_buff *ctrl_skb;
S
Simon Horman 已提交
224 225 226
	unsigned int i;
	int err;

227 228
	ctrl_skb = nfp_flower_cmsg_mac_repr_start(app, eth_tbl->count);
	if (!ctrl_skb)
S
Simon Horman 已提交
229 230
		return -ENOMEM;

231 232 233 234 235 236
	reprs = nfp_reprs_alloc(eth_tbl->max_index + 1);
	if (!reprs) {
		err = -ENOMEM;
		goto err_free_ctrl_skb;
	}

S
Simon Horman 已提交
237
	for (i = 0; i < eth_tbl->count; i++) {
238
		unsigned int phys_port = eth_tbl->ports[i].index;
S
Simon Horman 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
		struct nfp_port *port;
		u32 cmsg_port_id;

		reprs->reprs[phys_port] = nfp_repr_alloc(app);
		if (!reprs->reprs[phys_port]) {
			err = -ENOMEM;
			goto err_reprs_clean;
		}

		port = nfp_port_alloc(app, NFP_PORT_PHYS_PORT,
				      reprs->reprs[phys_port]);
		if (IS_ERR(port)) {
			err = PTR_ERR(port);
			goto err_reprs_clean;
		}
		err = nfp_port_init_phy_port(app->pf, app, port, i);
		if (err) {
			nfp_port_free(port);
			goto err_reprs_clean;
		}

		SET_NETDEV_DEV(reprs->reprs[phys_port], &priv->nn->pdev->dev);
261
		nfp_net_get_mac_addr(app->pf, port);
S
Simon Horman 已提交
262 263 264 265 266 267 268 269 270

		cmsg_port_id = nfp_flower_cmsg_phys_port(phys_port);
		err = nfp_repr_init(app, reprs->reprs[phys_port],
				    cmsg_port_id, port, priv->nn->dp.netdev);
		if (err) {
			nfp_port_free(port);
			goto err_reprs_clean;
		}

271 272 273 274 275
		nfp_flower_cmsg_mac_repr_add(ctrl_skb, i,
					     eth_tbl->ports[i].nbi,
					     eth_tbl->ports[i].base,
					     phys_port);

S
Simon Horman 已提交
276 277 278 279 280 281 282 283 284 285
		nfp_info(app->cpp, "Phys Port %d Representor(%s) created\n",
			 phys_port, reprs->reprs[phys_port]->name);
	}

	old_reprs = nfp_app_reprs_set(app, NFP_REPR_TYPE_PHYS_PORT, reprs);
	if (IS_ERR(old_reprs)) {
		err = PTR_ERR(old_reprs);
		goto err_reprs_clean;
	}

286 287 288 289 290 291 292 293 294
	/* The MAC_REPR control message should be sent after the MAC
	 * representors are registered using nfp_app_reprs_set().  This is
	 * because the firmware may respond with control messages for the
	 * MAC representors, f.e. to provide the driver with information
	 * about their state, and without registration the driver will drop
	 * any such messages.
	 */
	nfp_ctrl_tx(app->ctrl, ctrl_skb);

S
Simon Horman 已提交
295 296 297
	return 0;
err_reprs_clean:
	nfp_reprs_clean_and_free(reprs);
298 299
err_free_ctrl_skb:
	kfree_skb(ctrl_skb);
S
Simon Horman 已提交
300 301 302
	return err;
}

303 304 305 306 307 308 309 310 311 312 313 314 315
static int nfp_flower_start(struct nfp_app *app)
{
	int err;

	err = nfp_flower_spawn_phy_reprs(app, app->priv);
	if (err)
		return err;

	return nfp_flower_spawn_vnic_reprs(app,
					   NFP_FLOWER_CMSG_PORT_VNIC_TYPE_PF,
					   NFP_REPR_TYPE_PF, 1);
}

316 317
static int nfp_flower_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
				 unsigned int id)
S
Simon Horman 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
{
	if (id > 0) {
		nfp_warn(app->cpp, "FlowerNIC doesn't support more than one data vNIC\n");
		goto err_invalid_port;
	}

	eth_hw_addr_random(nn->dp.netdev);
	netif_keep_dst(nn->dp.netdev);

	return 0;

err_invalid_port:
	nn->port = nfp_port_alloc(app, NFP_PORT_INVALID, nn->dp.netdev);
	return PTR_ERR_OR_ZERO(nn->port);
}

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
static void nfp_flower_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
{
	struct nfp_flower_priv *priv = app->priv;

	priv->nn = NULL;
}

static int nfp_flower_vnic_init(struct nfp_app *app, struct nfp_net *nn)
{
	struct nfp_flower_priv *priv = app->priv;

	priv->nn = nn;

	return 0;
}

S
Simon Horman 已提交
350 351 352
static int nfp_flower_init(struct nfp_app *app)
{
	const struct nfp_pf *pf = app->pf;
353
	struct nfp_flower_priv *app_priv;
354 355
	u64 version;
	int err;
S
Simon Horman 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

	if (!pf->eth_tbl) {
		nfp_warn(app->cpp, "FlowerNIC requires eth table\n");
		return -EINVAL;
	}

	if (!pf->mac_stats_bar) {
		nfp_warn(app->cpp, "FlowerNIC requires mac_stats BAR\n");
		return -EINVAL;
	}

	if (!pf->vf_cfg_bar) {
		nfp_warn(app->cpp, "FlowerNIC requires vf_cfg BAR\n");
		return -EINVAL;
	}

372 373 374 375 376 377 378 379 380 381 382 383
	version = nfp_rtsym_read_le(app->pf->rtbl, "hw_flower_version", &err);
	if (err) {
		nfp_warn(app->cpp, "FlowerNIC requires hw_flower_version memory symbol\n");
		return err;
	}

	/* We need to ensure hardware has enough flower capabilities. */
	if (version != NFP_FLOWER_ALLOWED_VER) {
		nfp_warn(app->cpp, "FlowerNIC: unsupported firmware version\n");
		return -EINVAL;
	}

384 385
	app_priv = vzalloc(sizeof(struct nfp_flower_priv));
	if (!app_priv)
386 387
		return -ENOMEM;

388
	app->priv = app_priv;
389
	app_priv->app = app;
390 391 392
	skb_queue_head_init(&app_priv->cmsg_skbs);
	INIT_WORK(&app_priv->cmsg_work, nfp_flower_cmsg_process_rx);

393 394 395 396
	err = nfp_flower_metadata_init(app);
	if (err)
		goto err_free_app_priv;

S
Simon Horman 已提交
397
	return 0;
398 399 400 401

err_free_app_priv:
	vfree(app->priv);
	return err;
S
Simon Horman 已提交
402 403
}

404 405
static void nfp_flower_clean(struct nfp_app *app)
{
406 407 408 409 410
	struct nfp_flower_priv *app_priv = app->priv;

	skb_queue_purge(&app_priv->cmsg_skbs);
	flush_work(&app_priv->cmsg_work);

411
	nfp_flower_metadata_cleanup(app);
412
	vfree(app->priv);
413 414 415
	app->priv = NULL;
}

S
Simon Horman 已提交
416 417 418 419 420 421 422 423
const struct nfp_app_type app_flower = {
	.id		= NFP_APP_FLOWER_NIC,
	.name		= "flower",
	.ctrl_has_meta	= true,

	.extra_cap	= nfp_flower_extra_cap,

	.init		= nfp_flower_init,
424
	.clean		= nfp_flower_clean,
S
Simon Horman 已提交
425

426
	.vnic_alloc	= nfp_flower_vnic_alloc,
S
Simon Horman 已提交
427
	.vnic_init	= nfp_flower_vnic_init,
428
	.vnic_clean	= nfp_flower_vnic_clean,
S
Simon Horman 已提交
429

430 431 432
	.repr_open	= nfp_flower_repr_netdev_open,
	.repr_stop	= nfp_flower_repr_netdev_stop,

S
Simon Horman 已提交
433 434 435 436 437
	.start		= nfp_flower_start,
	.stop		= nfp_flower_stop,

	.ctrl_msg_rx	= nfp_flower_cmsg_rx,

438 439 440
	.sriov_enable	= nfp_flower_sriov_enable,
	.sriov_disable	= nfp_flower_sriov_disable,

S
Simon Horman 已提交
441 442
	.eswitch_mode_get  = eswitch_mode_get,
	.repr_get	= nfp_flower_repr_get,
443 444

	.setup_tc	= nfp_flower_setup_tc,
S
Simon Horman 已提交
445
};