nfp_net_repr.c 10.5 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
/*
 * 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>
35
#include <linux/io-64-nonatomic-hi-lo.h>
36 37
#include <linux/lockdep.h>
#include <net/dst_metadata.h>
S
Simon Horman 已提交
38
#include <net/switchdev.h>
39 40

#include "nfpcore/nfp_cpp.h"
41
#include "nfpcore/nfp_nsp.h"
42 43
#include "nfp_app.h"
#include "nfp_main.h"
44
#include "nfp_net_ctrl.h"
45 46 47
#include "nfp_net_repr.h"
#include "nfp_port.h"

48 49 50 51 52 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
static void
nfp_repr_inc_tx_stats(struct net_device *netdev, unsigned int len,
		      int tx_status)
{
	struct nfp_repr *repr = netdev_priv(netdev);
	struct nfp_repr_pcpu_stats *stats;

	if (unlikely(tx_status != NET_XMIT_SUCCESS &&
		     tx_status != NET_XMIT_CN)) {
		this_cpu_inc(repr->stats->tx_drops);
		return;
	}

	stats = this_cpu_ptr(repr->stats);
	u64_stats_update_begin(&stats->syncp);
	stats->tx_packets++;
	stats->tx_bytes += len;
	u64_stats_update_end(&stats->syncp);
}

void nfp_repr_inc_rx_stats(struct net_device *netdev, unsigned int len)
{
	struct nfp_repr *repr = netdev_priv(netdev);
	struct nfp_repr_pcpu_stats *stats;

	stats = this_cpu_ptr(repr->stats);
	u64_stats_update_begin(&stats->syncp);
	stats->rx_packets++;
	stats->rx_bytes += len;
	u64_stats_update_end(&stats->syncp);
}

static void
81
nfp_repr_phy_port_get_stats64(struct nfp_port *port,
82 83
			      struct rtnl_link_stats64 *stats)
{
84
	u8 __iomem *mem = port->eth_stats;
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 135 136 137

	/* TX and RX stats are flipped as we are returning the stats as seen
	 * at the switch port corresponding to the phys port.
	 */
	stats->tx_packets = readq(mem + NFP_MAC_STATS_RX_FRAMES_RECEIVED_OK);
	stats->tx_bytes = readq(mem + NFP_MAC_STATS_RX_IN_OCTETS);
	stats->tx_dropped = readq(mem + NFP_MAC_STATS_RX_IN_ERRORS);

	stats->rx_packets = readq(mem + NFP_MAC_STATS_TX_FRAMES_TRANSMITTED_OK);
	stats->rx_bytes = readq(mem + NFP_MAC_STATS_TX_OUT_OCTETS);
	stats->rx_dropped = readq(mem + NFP_MAC_STATS_TX_OUT_ERRORS);
}

static void
nfp_repr_vf_get_stats64(const struct nfp_app *app, u8 vf,
			struct rtnl_link_stats64 *stats)
{
	u8 __iomem *mem;

	mem = app->pf->vf_cfg_mem + vf * NFP_NET_CFG_BAR_SZ;

	/* TX and RX stats are flipped as we are returning the stats as seen
	 * at the switch port corresponding to the VF.
	 */
	stats->tx_packets = readq(mem + NFP_NET_CFG_STATS_RX_FRAMES);
	stats->tx_bytes = readq(mem + NFP_NET_CFG_STATS_RX_OCTETS);
	stats->tx_dropped = readq(mem + NFP_NET_CFG_STATS_RX_DISCARDS);

	stats->rx_packets = readq(mem + NFP_NET_CFG_STATS_TX_FRAMES);
	stats->rx_bytes = readq(mem + NFP_NET_CFG_STATS_TX_OCTETS);
	stats->rx_dropped = readq(mem + NFP_NET_CFG_STATS_TX_DISCARDS);
}

static void
nfp_repr_pf_get_stats64(const struct nfp_app *app, u8 pf,
			struct rtnl_link_stats64 *stats)
{
	u8 __iomem *mem;

	if (pf)
		return;

	mem = nfp_cpp_area_iomem(app->pf->data_vnic_bar);

	stats->tx_packets = readq(mem + NFP_NET_CFG_STATS_RX_FRAMES);
	stats->tx_bytes = readq(mem + NFP_NET_CFG_STATS_RX_OCTETS);
	stats->tx_dropped = readq(mem + NFP_NET_CFG_STATS_RX_DISCARDS);

	stats->rx_packets = readq(mem + NFP_NET_CFG_STATS_TX_FRAMES);
	stats->rx_bytes = readq(mem + NFP_NET_CFG_STATS_TX_OCTETS);
	stats->rx_dropped = readq(mem + NFP_NET_CFG_STATS_TX_DISCARDS);
}

138
static void
139
nfp_repr_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
140
{
141 142 143 144 145 146 147 148
	struct nfp_repr *repr = netdev_priv(netdev);
	struct nfp_app *app = repr->app;

	if (WARN_ON(!repr->port))
		return;

	switch (repr->port->type) {
	case NFP_PORT_PHYS_PORT:
149
		if (!__nfp_port_get_eth_port(repr->port))
150
			break;
151
		nfp_repr_phy_port_get_stats64(repr->port, stats);
152
		break;
153 154
	case NFP_PORT_PF_PORT:
		nfp_repr_pf_get_stats64(app, repr->port->pf_id, stats);
155
		break;
156 157
	case NFP_PORT_VF_PORT:
		nfp_repr_vf_get_stats64(app, repr->port->vf_id, stats);
158 159 160 161 162
	default:
		break;
	}
}

163
static bool
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 202 203 204 205
nfp_repr_has_offload_stats(const struct net_device *dev, int attr_id)
{
	switch (attr_id) {
	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
		return true;
	}

	return false;
}

static int
nfp_repr_get_host_stats64(const struct net_device *netdev,
			  struct rtnl_link_stats64 *stats)
{
	struct nfp_repr *repr = netdev_priv(netdev);
	int i;

	for_each_possible_cpu(i) {
		u64 tbytes, tpkts, tdrops, rbytes, rpkts;
		struct nfp_repr_pcpu_stats *repr_stats;
		unsigned int start;

		repr_stats = per_cpu_ptr(repr->stats, i);
		do {
			start = u64_stats_fetch_begin_irq(&repr_stats->syncp);
			tbytes = repr_stats->tx_bytes;
			tpkts = repr_stats->tx_packets;
			tdrops = repr_stats->tx_drops;
			rbytes = repr_stats->rx_bytes;
			rpkts = repr_stats->rx_packets;
		} while (u64_stats_fetch_retry_irq(&repr_stats->syncp, start));

		stats->tx_bytes += tbytes;
		stats->tx_packets += tpkts;
		stats->tx_dropped += tdrops;
		stats->rx_bytes += rbytes;
		stats->rx_packets += rpkts;
	}

	return 0;
}

206 207 208
static int
nfp_repr_get_offload_stats(int attr_id, const struct net_device *dev,
			   void *stats)
209 210 211 212 213 214 215 216 217
{
	switch (attr_id) {
	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
		return nfp_repr_get_host_stats64(dev, stats);
	}

	return -EINVAL;
}

218
static netdev_tx_t nfp_repr_xmit(struct sk_buff *skb, struct net_device *netdev)
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
{
	struct nfp_repr *repr = netdev_priv(netdev);
	unsigned int len = skb->len;
	int ret;

	skb_dst_drop(skb);
	dst_hold((struct dst_entry *)repr->dst);
	skb_dst_set(skb, (struct dst_entry *)repr->dst);
	skb->dev = repr->dst->u.port_info.lower_dev;

	ret = dev_queue_xmit(skb);
	nfp_repr_inc_tx_stats(netdev, len, ret);

	return ret;
}

235 236 237
static int nfp_repr_stop(struct net_device *netdev)
{
	struct nfp_repr *repr = netdev_priv(netdev);
238
	int err;
239

240 241 242 243 244 245
	err = nfp_app_repr_stop(repr->app, repr);
	if (err)
		return err;

	nfp_port_configure(netdev, false);
	return 0;
246 247 248 249 250
}

static int nfp_repr_open(struct net_device *netdev)
{
	struct nfp_repr *repr = netdev_priv(netdev);
251 252 253 254 255 256 257 258 259 260 261
	int err;

	err = nfp_port_configure(netdev, true);
	if (err)
		return err;

	err = nfp_app_repr_open(repr->app, repr);
	if (err)
		goto err_port_disable;

	return 0;
262

263 264 265
err_port_disable:
	nfp_port_configure(netdev, false);
	return err;
266 267
}

268
const struct net_device_ops nfp_repr_netdev_ops = {
269 270 271 272 273 274
	.ndo_open		= nfp_repr_open,
	.ndo_stop		= nfp_repr_stop,
	.ndo_start_xmit		= nfp_repr_xmit,
	.ndo_get_stats64	= nfp_repr_get_stats64,
	.ndo_has_offload_stats	= nfp_repr_has_offload_stats,
	.ndo_get_offload_stats	= nfp_repr_get_offload_stats,
275
	.ndo_get_phys_port_name	= nfp_port_get_phys_port_name,
276
	.ndo_setup_tc		= nfp_port_setup_tc,
277 278
};

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
static void nfp_repr_clean(struct nfp_repr *repr)
{
	unregister_netdev(repr->netdev);
	dst_release((struct dst_entry *)repr->dst);
	nfp_port_free(repr->port);
}

static struct lock_class_key nfp_repr_netdev_xmit_lock_key;
static struct lock_class_key nfp_repr_netdev_addr_lock_key;

static void nfp_repr_set_lockdep_class_one(struct net_device *dev,
					   struct netdev_queue *txq,
					   void *_unused)
{
	lockdep_set_class(&txq->_xmit_lock, &nfp_repr_netdev_xmit_lock_key);
}

static void nfp_repr_set_lockdep_class(struct net_device *dev)
{
	lockdep_set_class(&dev->addr_list_lock, &nfp_repr_netdev_addr_lock_key);
	netdev_for_each_tx_queue(dev, nfp_repr_set_lockdep_class_one, NULL);
}

int nfp_repr_init(struct nfp_app *app, struct net_device *netdev,
303 304
		  u32 cmsg_port_id, struct nfp_port *port,
		  struct net_device *pf_netdev)
305 306 307 308 309 310 311 312 313 314 315 316 317
{
	struct nfp_repr *repr = netdev_priv(netdev);
	int err;

	nfp_repr_set_lockdep_class(netdev);

	repr->port = port;
	repr->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX, GFP_KERNEL);
	if (!repr->dst)
		return -ENOMEM;
	repr->dst->u.port_info.port_id = cmsg_port_id;
	repr->dst->u.port_info.lower_dev = pf_netdev;

318
	netdev->netdev_ops = &nfp_repr_netdev_ops;
319 320
	netdev->ethtool_ops = &nfp_port_ethtool_ops;

S
Simon Horman 已提交
321
	SWITCHDEV_SET_OPS(netdev, &nfp_port_switchdev_ops);
322

323 324 325 326 327
	if (nfp_app_has_tc(app)) {
		netdev->features |= NETIF_F_HW_TC;
		netdev->hw_features |= NETIF_F_HW_TC;
	}

328 329 330 331 332 333 334 335 336 337 338
	err = register_netdev(netdev);
	if (err)
		goto err_clean;

	return 0;

err_clean:
	dst_release((struct dst_entry *)repr->dst);
	return err;
}

339 340 341 342 343 344
static void nfp_repr_free(struct nfp_repr *repr)
{
	free_percpu(repr->stats);
	free_netdev(repr->netdev);
}

345 346 347 348 349 350 351 352 353 354 355 356 357
struct net_device *nfp_repr_alloc(struct nfp_app *app)
{
	struct net_device *netdev;
	struct nfp_repr *repr;

	netdev = alloc_etherdev(sizeof(*repr));
	if (!netdev)
		return NULL;

	repr = netdev_priv(netdev);
	repr->netdev = netdev;
	repr->app = app;

358 359 360 361
	repr->stats = netdev_alloc_pcpu_stats(struct nfp_repr_pcpu_stats);
	if (!repr->stats)
		goto err_free_netdev;

362
	return netdev;
363 364 365 366

err_free_netdev:
	free_netdev(netdev);
	return NULL;
367 368 369 370 371 372 373
}

static void nfp_repr_clean_and_free(struct nfp_repr *repr)
{
	nfp_info(repr->app->cpp, "Destroying Representor(%s)\n",
		 repr->netdev->name);
	nfp_repr_clean(repr);
374
	nfp_repr_free(repr);
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
}

void nfp_reprs_clean_and_free(struct nfp_reprs *reprs)
{
	unsigned int i;

	for (i = 0; i < reprs->num_reprs; i++)
		if (reprs->reprs[i])
			nfp_repr_clean_and_free(netdev_priv(reprs->reprs[i]));

	kfree(reprs);
}

void
nfp_reprs_clean_and_free_by_type(struct nfp_app *app,
				 enum nfp_repr_type type)
{
	struct nfp_reprs *reprs;

	reprs = nfp_app_reprs_set(app, type, NULL);
	if (!reprs)
		return;

	synchronize_rcu();
	nfp_reprs_clean_and_free(reprs);
}

struct nfp_reprs *nfp_reprs_alloc(unsigned int num_reprs)
{
	struct nfp_reprs *reprs;

	reprs = kzalloc(sizeof(*reprs) +
			num_reprs * sizeof(struct net_device *), GFP_KERNEL);
	if (!reprs)
		return NULL;
	reprs->num_reprs = num_reprs;

	return reprs;
}