rocker.c 147.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * drivers/net/ethernet/rocker/rocker.c - Rocker switch device driver
 * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
 * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/spinlock.h>
19
#include <linux/hashtable.h>
20 21 22 23 24 25 26 27 28 29 30
#include <linux/crc32.h>
#include <linux/sort.h>
#include <linux/random.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <linux/skbuff.h>
#include <linux/socket.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
31
#include <linux/if_bridge.h>
32
#include <linux/bitops.h>
33
#include <linux/ctype.h>
34 35
#include <net/switchdev.h>
#include <net/rtnetlink.h>
36 37 38
#include <net/ip_fib.h>
#include <net/netevent.h>
#include <net/arp.h>
39 40 41 42 43 44 45 46 47 48 49 50
#include <asm-generic/io-64-nonatomic-lo-hi.h>
#include <generated/utsrelease.h>

#include "rocker.h"

static const char rocker_driver_name[] = "rocker";

static const struct pci_device_id rocker_pci_id_table[] = {
	{PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_ROCKER), 0},
	{0, }
};

51 52 53 54 55
struct rocker_flow_tbl_key {
	u32 priority;
	enum rocker_of_dpa_table_id tbl_id;
	union {
		struct {
S
Scott Feldman 已提交
56 57
			u32 in_pport;
			u32 in_pport_mask;
58 59 60
			enum rocker_of_dpa_table_id goto_tbl;
		} ig_port;
		struct {
S
Scott Feldman 已提交
61
			u32 in_pport;
62 63 64 65 66 67 68
			__be16 vlan_id;
			__be16 vlan_id_mask;
			enum rocker_of_dpa_table_id goto_tbl;
			bool untagged;
			__be16 new_vlan_id;
		} vlan;
		struct {
S
Scott Feldman 已提交
69 70
			u32 in_pport;
			u32 in_pport_mask;
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
			__be16 eth_type;
			u8 eth_dst[ETH_ALEN];
			u8 eth_dst_mask[ETH_ALEN];
			__be16 vlan_id;
			__be16 vlan_id_mask;
			enum rocker_of_dpa_table_id goto_tbl;
			bool copy_to_cpu;
		} term_mac;
		struct {
			__be16 eth_type;
			__be32 dst4;
			__be32 dst4_mask;
			enum rocker_of_dpa_table_id goto_tbl;
			u32 group_id;
		} ucast_routing;
		struct {
			u8 eth_dst[ETH_ALEN];
			u8 eth_dst_mask[ETH_ALEN];
			int has_eth_dst;
			int has_eth_dst_mask;
			__be16 vlan_id;
			u32 tunnel_id;
			enum rocker_of_dpa_table_id goto_tbl;
			u32 group_id;
			bool copy_to_cpu;
		} bridge;
		struct {
S
Scott Feldman 已提交
98 99
			u32 in_pport;
			u32 in_pport_mask;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
			u8 eth_src[ETH_ALEN];
			u8 eth_src_mask[ETH_ALEN];
			u8 eth_dst[ETH_ALEN];
			u8 eth_dst_mask[ETH_ALEN];
			__be16 eth_type;
			__be16 vlan_id;
			__be16 vlan_id_mask;
			u8 ip_proto;
			u8 ip_proto_mask;
			u8 ip_tos;
			u8 ip_tos_mask;
			u32 group_id;
		} acl;
	};
};

struct rocker_flow_tbl_entry {
	struct hlist_node entry;
118
	u32 cmd;
119 120
	u64 cookie;
	struct rocker_flow_tbl_key key;
121
	size_t key_len;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	u32 key_crc32; /* key */
};

struct rocker_group_tbl_entry {
	struct hlist_node entry;
	u32 cmd;
	u32 group_id; /* key */
	u16 group_count;
	u32 *group_ids;
	union {
		struct {
			u8 pop_vlan;
		} l2_interface;
		struct {
			u8 eth_src[ETH_ALEN];
			u8 eth_dst[ETH_ALEN];
			__be16 vlan_id;
			u32 group_id;
		} l2_rewrite;
		struct {
			u8 eth_src[ETH_ALEN];
			u8 eth_dst[ETH_ALEN];
			__be16 vlan_id;
			bool ttl_check;
			u32 group_id;
		} l3_unicast;
	};
};

struct rocker_fdb_tbl_entry {
	struct hlist_node entry;
	u32 key_crc32; /* key */
	bool learned;
155
	unsigned long touched;
156
	struct rocker_fdb_tbl_key {
157
		struct rocker_port *rocker_port;
158 159 160 161 162 163 164 165 166 167 168 169
		u8 addr[ETH_ALEN];
		__be16 vlan_id;
	} key;
};

struct rocker_internal_vlan_tbl_entry {
	struct hlist_node entry;
	int ifindex; /* key */
	u32 ref_count;
	__be16 vlan_id;
};

170 171 172 173 174 175 176 177 178 179
struct rocker_neigh_tbl_entry {
	struct hlist_node entry;
	__be32 ip_addr; /* key */
	struct net_device *dev;
	u32 ref_count;
	u32 index;
	u8 eth_dst[ETH_ALEN];
	bool ttl_check;
};

180 181 182 183 184
struct rocker_desc_info {
	char *data; /* mapped */
	size_t data_size;
	size_t tlv_size;
	struct rocker_desc *desc;
S
Scott Feldman 已提交
185
	dma_addr_t mapaddr;
186 187 188 189 190 191 192 193 194 195 196 197 198 199
};

struct rocker_dma_ring_info {
	size_t size;
	u32 head;
	u32 tail;
	struct rocker_desc *desc; /* mapped */
	dma_addr_t mapaddr;
	struct rocker_desc_info *desc_info;
	unsigned int type;
};

struct rocker;

200 201 202 203 204 205
enum {
	ROCKER_CTRL_LINK_LOCAL_MCAST,
	ROCKER_CTRL_LOCAL_ARP,
	ROCKER_CTRL_IPV4_MCAST,
	ROCKER_CTRL_IPV6_MCAST,
	ROCKER_CTRL_DFLT_BRIDGING,
206
	ROCKER_CTRL_DFLT_OVS,
207 208 209 210 211 212 213 214
	ROCKER_CTRL_MAX,
};

#define ROCKER_INTERNAL_VLAN_ID_BASE	0x0f00
#define ROCKER_N_INTERNAL_VLANS		255
#define ROCKER_VLAN_BITMAP_LEN		BITS_TO_LONGS(VLAN_N_VID)
#define ROCKER_INTERNAL_VLAN_BITMAP_LEN	BITS_TO_LONGS(ROCKER_N_INTERNAL_VLANS)

215 216
struct rocker_port {
	struct net_device *dev;
217
	struct net_device *bridge_dev;
218 219
	struct rocker *rocker;
	unsigned int port_number;
S
Scott Feldman 已提交
220
	u32 pport;
221
	__be16 internal_vlan_id;
222
	int stp_state;
223
	u32 brport_flags;
224
	unsigned long ageing_time;
225 226
	bool ctrls[ROCKER_CTRL_MAX];
	unsigned long vlan_bitmap[ROCKER_VLAN_BITMAP_LEN];
227 228 229 230
	struct napi_struct napi_tx;
	struct napi_struct napi_rx;
	struct rocker_dma_ring_info tx_ring;
	struct rocker_dma_ring_info rx_ring;
231
	struct list_head trans_mem;
232 233 234 235 236 237 238 239 240 241 242
};

struct rocker {
	struct pci_dev *pdev;
	u8 __iomem *hw_addr;
	struct msix_entry *msix_entries;
	unsigned int port_count;
	struct rocker_port **ports;
	struct {
		u64 id;
	} hw;
S
Scott Feldman 已提交
243
	spinlock_t cmd_ring_lock;		/* for cmd ring accesses */
244 245
	struct rocker_dma_ring_info cmd_ring;
	struct rocker_dma_ring_info event_ring;
246
	DECLARE_HASHTABLE(flow_tbl, 16);
S
Scott Feldman 已提交
247
	spinlock_t flow_tbl_lock;		/* for flow tbl accesses */
248 249
	u64 flow_tbl_next_cookie;
	DECLARE_HASHTABLE(group_tbl, 16);
S
Scott Feldman 已提交
250
	spinlock_t group_tbl_lock;		/* for group tbl accesses */
S
Scott Feldman 已提交
251
	struct timer_list fdb_cleanup_timer;
252
	DECLARE_HASHTABLE(fdb_tbl, 16);
S
Scott Feldman 已提交
253
	spinlock_t fdb_tbl_lock;		/* for fdb tbl accesses */
254 255
	unsigned long internal_vlan_bitmap[ROCKER_INTERNAL_VLAN_BITMAP_LEN];
	DECLARE_HASHTABLE(internal_vlan_tbl, 8);
S
Scott Feldman 已提交
256
	spinlock_t internal_vlan_tbl_lock;	/* for vlan tbl accesses */
257
	DECLARE_HASHTABLE(neigh_tbl, 16);
S
Scott Feldman 已提交
258
	spinlock_t neigh_tbl_lock;		/* for neigh tbl accesses */
259
	u32 neigh_tbl_next_index;
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
};

static const u8 zero_mac[ETH_ALEN]   = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const u8 ff_mac[ETH_ALEN]     = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
static const u8 ll_mac[ETH_ALEN]     = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
static const u8 ll_mask[ETH_ALEN]    = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
static const u8 mcast_mac[ETH_ALEN]  = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const u8 ipv4_mcast[ETH_ALEN] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
static const u8 ipv4_mask[ETH_ALEN]  = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 };
static const u8 ipv6_mcast[ETH_ALEN] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
static const u8 ipv6_mask[ETH_ALEN]  = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };

/* Rocker priority levels for flow table entries.  Higher
 * priority match takes precedence over lower priority match.
 */

enum {
	ROCKER_PRIORITY_UNKNOWN = 0,
	ROCKER_PRIORITY_IG_PORT = 1,
	ROCKER_PRIORITY_VLAN = 1,
	ROCKER_PRIORITY_TERM_MAC_UCAST = 0,
	ROCKER_PRIORITY_TERM_MAC_MCAST = 1,
	ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_EXACT = 1,
	ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_WILD = 2,
	ROCKER_PRIORITY_BRIDGING_VLAN = 3,
	ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_EXACT = 1,
	ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_WILD = 2,
	ROCKER_PRIORITY_BRIDGING_TENANT = 3,
	ROCKER_PRIORITY_ACL_CTRL = 3,
	ROCKER_PRIORITY_ACL_NORMAL = 2,
	ROCKER_PRIORITY_ACL_DFLT = 1,
291 292
};

293 294 295 296 297 298 299 300 301
static bool rocker_vlan_id_is_internal(__be16 vlan_id)
{
	u16 start = ROCKER_INTERNAL_VLAN_ID_BASE;
	u16 end = 0xffe;
	u16 _vlan_id = ntohs(vlan_id);

	return (_vlan_id >= start && _vlan_id <= end);
}

302
static __be16 rocker_port_vid_to_vlan(const struct rocker_port *rocker_port,
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
				      u16 vid, bool *pop_vlan)
{
	__be16 vlan_id;

	if (pop_vlan)
		*pop_vlan = false;
	vlan_id = htons(vid);
	if (!vlan_id) {
		vlan_id = rocker_port->internal_vlan_id;
		if (pop_vlan)
			*pop_vlan = true;
	}

	return vlan_id;
}

319
static u16 rocker_port_vlan_to_vid(const struct rocker_port *rocker_port,
320 321 322 323 324 325 326 327
				   __be16 vlan_id)
{
	if (rocker_vlan_id_is_internal(vlan_id))
		return 0;

	return ntohs(vlan_id);
}

328
static bool rocker_port_is_bridged(const struct rocker_port *rocker_port)
329
{
330 331
	return rocker_port->bridge_dev &&
	       netif_is_bridge_master(rocker_port->bridge_dev);
332 333 334 335
}

static bool rocker_port_is_ovsed(const struct rocker_port *rocker_port)
{
336 337
	return rocker_port->bridge_dev &&
	       netif_is_ovs_master(rocker_port->bridge_dev);
338 339
}

340 341 342 343 344
#define ROCKER_OP_FLAG_REMOVE		BIT(0)
#define ROCKER_OP_FLAG_NOWAIT		BIT(1)
#define ROCKER_OP_FLAG_LEARNED		BIT(2)
#define ROCKER_OP_FLAG_REFRESH		BIT(3)

345
static void *__rocker_port_mem_alloc(struct rocker_port *rocker_port,
346
				     enum switchdev_trans_ph trans_ph, int flags,
347
				     size_t size)
348 349
{
	struct list_head *elem = NULL;
350 351
	gfp_t gfp_flags = (flags & ROCKER_OP_FLAG_NOWAIT) ?
			  GFP_ATOMIC : GFP_KERNEL;
352 353 354 355 356 357 358 359 360 361

	/* If in transaction prepare phase, allocate the memory
	 * and enqueue it on a per-port list.  If in transaction
	 * commit phase, dequeue the memory from the per-port list
	 * rather than re-allocating the memory.  The idea is the
	 * driver code paths for prepare and commit are identical
	 * so the memory allocated in the prepare phase is the
	 * memory used in the commit phase.
	 */

362
	switch (trans_ph) {
363
	case SWITCHDEV_TRANS_PREPARE:
364
		elem = kzalloc(size + sizeof(*elem), gfp_flags);
365 366 367 368 369 370 371 372 373 374
		if (!elem)
			return NULL;
		list_add_tail(elem, &rocker_port->trans_mem);
		break;
	case SWITCHDEV_TRANS_COMMIT:
		BUG_ON(list_empty(&rocker_port->trans_mem));
		elem = rocker_port->trans_mem.next;
		list_del_init(elem);
		break;
	case SWITCHDEV_TRANS_NONE:
375
		elem = kzalloc(size + sizeof(*elem), gfp_flags);
376 377 378 379 380 381 382 383 384 385 386
		if (elem)
			INIT_LIST_HEAD(elem);
		break;
	default:
		break;
	}

	return elem ? elem + 1 : NULL;
}

static void *rocker_port_kzalloc(struct rocker_port *rocker_port,
387
				 enum switchdev_trans_ph trans_ph, int flags,
388
				 size_t size)
389
{
390
	return __rocker_port_mem_alloc(rocker_port, trans_ph, flags, size);
391 392 393
}

static void *rocker_port_kcalloc(struct rocker_port *rocker_port,
394
				 enum switchdev_trans_ph trans_ph, int flags,
395
				 size_t n, size_t size)
396
{
397
	return __rocker_port_mem_alloc(rocker_port, trans_ph, flags, n * size);
398 399
}

400
static void rocker_port_kfree(enum switchdev_trans_ph trans_ph, const void *mem)
401 402 403 404 405 406 407 408
{
	struct list_head *elem;

	/* Frees are ignored if in transaction prepare phase.  The
	 * memory remains on the per-port list until freed in the
	 * commit phase.
	 */

409
	if (trans_ph == SWITCHDEV_TRANS_PREPARE)
410 411 412 413 414 415 416
		return;

	elem = (struct list_head *)mem - 1;
	BUG_ON(!list_empty(elem));
	kfree(elem);
}

417 418 419
struct rocker_wait {
	wait_queue_head_t wait;
	bool done;
420
	bool nowait;
421 422 423 424 425
};

static void rocker_wait_reset(struct rocker_wait *wait)
{
	wait->done = false;
426
	wait->nowait = false;
427 428 429 430 431 432 433 434
}

static void rocker_wait_init(struct rocker_wait *wait)
{
	init_waitqueue_head(&wait->wait);
	rocker_wait_reset(wait);
}

435
static struct rocker_wait *rocker_wait_create(struct rocker_port *rocker_port,
436
					      enum switchdev_trans_ph trans_ph,
437
					      int flags)
438 439 440
{
	struct rocker_wait *wait;

441
	wait = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*wait));
442 443 444 445 446 447
	if (!wait)
		return NULL;
	rocker_wait_init(wait);
	return wait;
}

448
static void rocker_wait_destroy(enum switchdev_trans_ph trans_ph,
449
				struct rocker_wait *wait)
450
{
451
	rocker_port_kfree(trans_ph, wait);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
}

static bool rocker_wait_event_timeout(struct rocker_wait *wait,
				      unsigned long timeout)
{
	wait_event_timeout(wait->wait, wait->done, HZ / 10);
	if (!wait->done)
		return false;
	return true;
}

static void rocker_wait_wake_up(struct rocker_wait *wait)
{
	wait->done = true;
	wake_up(&wait->wait);
}

469
static u32 rocker_msix_vector(const struct rocker *rocker, unsigned int vector)
470 471 472 473
{
	return rocker->msix_entries[vector].vector;
}

474
static u32 rocker_msix_tx_vector(const struct rocker_port *rocker_port)
475 476 477 478 479
{
	return rocker_msix_vector(rocker_port->rocker,
				  ROCKER_MSIX_VEC_TX(rocker_port->port_number));
}

480
static u32 rocker_msix_rx_vector(const struct rocker_port *rocker_port)
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
{
	return rocker_msix_vector(rocker_port->rocker,
				  ROCKER_MSIX_VEC_RX(rocker_port->port_number));
}

#define rocker_write32(rocker, reg, val)	\
	writel((val), (rocker)->hw_addr + (ROCKER_ ## reg))
#define rocker_read32(rocker, reg)	\
	readl((rocker)->hw_addr + (ROCKER_ ## reg))
#define rocker_write64(rocker, reg, val)	\
	writeq((val), (rocker)->hw_addr + (ROCKER_ ## reg))
#define rocker_read64(rocker, reg)	\
	readq((rocker)->hw_addr + (ROCKER_ ## reg))

/*****************************
 * HW basic testing functions
 *****************************/

499
static int rocker_reg_test(const struct rocker *rocker)
500
{
501
	const struct pci_dev *pdev = rocker->pdev;
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
	u64 test_reg;
	u64 rnd;

	rnd = prandom_u32();
	rnd >>= 1;
	rocker_write32(rocker, TEST_REG, rnd);
	test_reg = rocker_read32(rocker, TEST_REG);
	if (test_reg != rnd * 2) {
		dev_err(&pdev->dev, "unexpected 32bit register value %08llx, expected %08llx\n",
			test_reg, rnd * 2);
		return -EIO;
	}

	rnd = prandom_u32();
	rnd <<= 31;
	rnd |= prandom_u32();
	rocker_write64(rocker, TEST_REG64, rnd);
	test_reg = rocker_read64(rocker, TEST_REG64);
	if (test_reg != rnd * 2) {
		dev_err(&pdev->dev, "unexpected 64bit register value %16llx, expected %16llx\n",
			test_reg, rnd * 2);
		return -EIO;
	}

	return 0;
}

529 530 531 532
static int rocker_dma_test_one(const struct rocker *rocker,
			       struct rocker_wait *wait, u32 test_type,
			       dma_addr_t dma_handle, const unsigned char *buf,
			       const unsigned char *expect, size_t size)
533
{
534
	const struct pci_dev *pdev = rocker->pdev;
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
	int i;

	rocker_wait_reset(wait);
	rocker_write32(rocker, TEST_DMA_CTRL, test_type);

	if (!rocker_wait_event_timeout(wait, HZ / 10)) {
		dev_err(&pdev->dev, "no interrupt received within a timeout\n");
		return -EIO;
	}

	for (i = 0; i < size; i++) {
		if (buf[i] != expect[i]) {
			dev_err(&pdev->dev, "unexpected memory content %02x at byte %x\n, %02x expected",
				buf[i], i, expect[i]);
			return -EIO;
		}
	}
	return 0;
}

#define ROCKER_TEST_DMA_BUF_SIZE (PAGE_SIZE * 4)
#define ROCKER_TEST_DMA_FILL_PATTERN 0x96

558
static int rocker_dma_test_offset(const struct rocker *rocker,
559 560 561 562 563 564 565 566 567 568 569 570 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
				  struct rocker_wait *wait, int offset)
{
	struct pci_dev *pdev = rocker->pdev;
	unsigned char *alloc;
	unsigned char *buf;
	unsigned char *expect;
	dma_addr_t dma_handle;
	int i;
	int err;

	alloc = kzalloc(ROCKER_TEST_DMA_BUF_SIZE * 2 + offset,
			GFP_KERNEL | GFP_DMA);
	if (!alloc)
		return -ENOMEM;
	buf = alloc + offset;
	expect = buf + ROCKER_TEST_DMA_BUF_SIZE;

	dma_handle = pci_map_single(pdev, buf, ROCKER_TEST_DMA_BUF_SIZE,
				    PCI_DMA_BIDIRECTIONAL);
	if (pci_dma_mapping_error(pdev, dma_handle)) {
		err = -EIO;
		goto free_alloc;
	}

	rocker_write64(rocker, TEST_DMA_ADDR, dma_handle);
	rocker_write32(rocker, TEST_DMA_SIZE, ROCKER_TEST_DMA_BUF_SIZE);

	memset(expect, ROCKER_TEST_DMA_FILL_PATTERN, ROCKER_TEST_DMA_BUF_SIZE);
	err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_FILL,
				  dma_handle, buf, expect,
				  ROCKER_TEST_DMA_BUF_SIZE);
	if (err)
		goto unmap;

	memset(expect, 0, ROCKER_TEST_DMA_BUF_SIZE);
	err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_CLEAR,
				  dma_handle, buf, expect,
				  ROCKER_TEST_DMA_BUF_SIZE);
	if (err)
		goto unmap;

	prandom_bytes(buf, ROCKER_TEST_DMA_BUF_SIZE);
	for (i = 0; i < ROCKER_TEST_DMA_BUF_SIZE; i++)
		expect[i] = ~buf[i];
	err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_INVERT,
				  dma_handle, buf, expect,
				  ROCKER_TEST_DMA_BUF_SIZE);
	if (err)
		goto unmap;

unmap:
	pci_unmap_single(pdev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE,
			 PCI_DMA_BIDIRECTIONAL);
free_alloc:
	kfree(alloc);

	return err;
}

618 619
static int rocker_dma_test(const struct rocker *rocker,
			   struct rocker_wait *wait)
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
{
	int i;
	int err;

	for (i = 0; i < 8; i++) {
		err = rocker_dma_test_offset(rocker, wait, i);
		if (err)
			return err;
	}
	return 0;
}

static irqreturn_t rocker_test_irq_handler(int irq, void *dev_id)
{
	struct rocker_wait *wait = dev_id;

	rocker_wait_wake_up(wait);

	return IRQ_HANDLED;
}

641
static int rocker_basic_hw_test(const struct rocker *rocker)
642
{
643
	const struct pci_dev *pdev = rocker->pdev;
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 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 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
	struct rocker_wait wait;
	int err;

	err = rocker_reg_test(rocker);
	if (err) {
		dev_err(&pdev->dev, "reg test failed\n");
		return err;
	}

	err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST),
			  rocker_test_irq_handler, 0,
			  rocker_driver_name, &wait);
	if (err) {
		dev_err(&pdev->dev, "cannot assign test irq\n");
		return err;
	}

	rocker_wait_init(&wait);
	rocker_write32(rocker, TEST_IRQ, ROCKER_MSIX_VEC_TEST);

	if (!rocker_wait_event_timeout(&wait, HZ / 10)) {
		dev_err(&pdev->dev, "no interrupt received within a timeout\n");
		err = -EIO;
		goto free_irq;
	}

	err = rocker_dma_test(rocker, &wait);
	if (err)
		dev_err(&pdev->dev, "dma test failed\n");

free_irq:
	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), &wait);
	return err;
}

/******
 * TLV
 ******/

#define ROCKER_TLV_ALIGNTO 8U
#define ROCKER_TLV_ALIGN(len) \
	(((len) + ROCKER_TLV_ALIGNTO - 1) & ~(ROCKER_TLV_ALIGNTO - 1))
#define ROCKER_TLV_HDRLEN ROCKER_TLV_ALIGN(sizeof(struct rocker_tlv))

/*  <------- ROCKER_TLV_HDRLEN -------> <--- ROCKER_TLV_ALIGN(payload) --->
 * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+
 * |             Header          | Pad |           Payload           | Pad |
 * |      (struct rocker_tlv)    | ing |                             | ing |
 * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+
 *  <--------------------------- tlv->len -------------------------->
 */

static struct rocker_tlv *rocker_tlv_next(const struct rocker_tlv *tlv,
					  int *remaining)
{
	int totlen = ROCKER_TLV_ALIGN(tlv->len);

	*remaining -= totlen;
	return (struct rocker_tlv *) ((char *) tlv + totlen);
}

static int rocker_tlv_ok(const struct rocker_tlv *tlv, int remaining)
{
	return remaining >= (int) ROCKER_TLV_HDRLEN &&
	       tlv->len >= ROCKER_TLV_HDRLEN &&
	       tlv->len <= remaining;
}

#define rocker_tlv_for_each(pos, head, len, rem)	\
	for (pos = head, rem = len;			\
	     rocker_tlv_ok(pos, rem);			\
	     pos = rocker_tlv_next(pos, &(rem)))

#define rocker_tlv_for_each_nested(pos, tlv, rem)	\
	rocker_tlv_for_each(pos, rocker_tlv_data(tlv),	\
			    rocker_tlv_len(tlv), rem)

static int rocker_tlv_attr_size(int payload)
{
	return ROCKER_TLV_HDRLEN + payload;
}

static int rocker_tlv_total_size(int payload)
{
	return ROCKER_TLV_ALIGN(rocker_tlv_attr_size(payload));
}

static int rocker_tlv_padlen(int payload)
{
	return rocker_tlv_total_size(payload) - rocker_tlv_attr_size(payload);
}

static int rocker_tlv_type(const struct rocker_tlv *tlv)
{
	return tlv->type;
}

static void *rocker_tlv_data(const struct rocker_tlv *tlv)
{
	return (char *) tlv + ROCKER_TLV_HDRLEN;
}

static int rocker_tlv_len(const struct rocker_tlv *tlv)
{
	return tlv->len - ROCKER_TLV_HDRLEN;
}

static u8 rocker_tlv_get_u8(const struct rocker_tlv *tlv)
{
	return *(u8 *) rocker_tlv_data(tlv);
}

static u16 rocker_tlv_get_u16(const struct rocker_tlv *tlv)
{
	return *(u16 *) rocker_tlv_data(tlv);
}

761 762 763 764 765
static __be16 rocker_tlv_get_be16(const struct rocker_tlv *tlv)
{
	return *(__be16 *) rocker_tlv_data(tlv);
}

766 767 768 769 770 771 772 773 774 775
static u32 rocker_tlv_get_u32(const struct rocker_tlv *tlv)
{
	return *(u32 *) rocker_tlv_data(tlv);
}

static u64 rocker_tlv_get_u64(const struct rocker_tlv *tlv)
{
	return *(u64 *) rocker_tlv_data(tlv);
}

776
static void rocker_tlv_parse(const struct rocker_tlv **tb, int maxtype,
777 778 779 780 781 782 783 784 785 786 787 788
			     const char *buf, int buf_len)
{
	const struct rocker_tlv *tlv;
	const struct rocker_tlv *head = (const struct rocker_tlv *) buf;
	int rem;

	memset(tb, 0, sizeof(struct rocker_tlv *) * (maxtype + 1));

	rocker_tlv_for_each(tlv, head, buf_len, rem) {
		u32 type = rocker_tlv_type(tlv);

		if (type > 0 && type <= maxtype)
789
			tb[type] = tlv;
790 791 792
	}
}

793
static void rocker_tlv_parse_nested(const struct rocker_tlv **tb, int maxtype,
794 795 796 797 798 799
				    const struct rocker_tlv *tlv)
{
	rocker_tlv_parse(tb, maxtype, rocker_tlv_data(tlv),
			 rocker_tlv_len(tlv));
}

800 801
static void rocker_tlv_parse_desc(const struct rocker_tlv **tb, int maxtype,
				  const struct rocker_desc_info *desc_info)
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
{
	rocker_tlv_parse(tb, maxtype, desc_info->data,
			 desc_info->desc->tlv_size);
}

static struct rocker_tlv *rocker_tlv_start(struct rocker_desc_info *desc_info)
{
	return (struct rocker_tlv *) ((char *) desc_info->data +
					       desc_info->tlv_size);
}

static int rocker_tlv_put(struct rocker_desc_info *desc_info,
			  int attrtype, int attrlen, const void *data)
{
	int tail_room = desc_info->data_size - desc_info->tlv_size;
	int total_size = rocker_tlv_total_size(attrlen);
	struct rocker_tlv *tlv;

	if (unlikely(tail_room < total_size))
		return -EMSGSIZE;

	tlv = rocker_tlv_start(desc_info);
	desc_info->tlv_size += total_size;
	tlv->type = attrtype;
	tlv->len = rocker_tlv_attr_size(attrlen);
	memcpy(rocker_tlv_data(tlv), data, attrlen);
	memset((char *) tlv + tlv->len, 0, rocker_tlv_padlen(attrlen));
	return 0;
}

static int rocker_tlv_put_u8(struct rocker_desc_info *desc_info,
			     int attrtype, u8 value)
{
	return rocker_tlv_put(desc_info, attrtype, sizeof(u8), &value);
}

static int rocker_tlv_put_u16(struct rocker_desc_info *desc_info,
			      int attrtype, u16 value)
{
	return rocker_tlv_put(desc_info, attrtype, sizeof(u16), &value);
}

844 845 846 847 848 849
static int rocker_tlv_put_be16(struct rocker_desc_info *desc_info,
			       int attrtype, __be16 value)
{
	return rocker_tlv_put(desc_info, attrtype, sizeof(__be16), &value);
}

850 851 852 853 854 855
static int rocker_tlv_put_u32(struct rocker_desc_info *desc_info,
			      int attrtype, u32 value)
{
	return rocker_tlv_put(desc_info, attrtype, sizeof(u32), &value);
}

856 857 858 859 860 861
static int rocker_tlv_put_be32(struct rocker_desc_info *desc_info,
			       int attrtype, __be32 value)
{
	return rocker_tlv_put(desc_info, attrtype, sizeof(__be32), &value);
}

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
static int rocker_tlv_put_u64(struct rocker_desc_info *desc_info,
			      int attrtype, u64 value)
{
	return rocker_tlv_put(desc_info, attrtype, sizeof(u64), &value);
}

static struct rocker_tlv *
rocker_tlv_nest_start(struct rocker_desc_info *desc_info, int attrtype)
{
	struct rocker_tlv *start = rocker_tlv_start(desc_info);

	if (rocker_tlv_put(desc_info, attrtype, 0, NULL) < 0)
		return NULL;

	return start;
}

static void rocker_tlv_nest_end(struct rocker_desc_info *desc_info,
				struct rocker_tlv *start)
{
	start->len = (char *) rocker_tlv_start(desc_info) - (char *) start;
}

static void rocker_tlv_nest_cancel(struct rocker_desc_info *desc_info,
886
				   const struct rocker_tlv *start)
887
{
888
	desc_info->tlv_size = (const char *) start - desc_info->data;
889 890 891 892 893 894 895 896 897 898 899
}

/******************************************
 * DMA rings and descriptors manipulations
 ******************************************/

static u32 __pos_inc(u32 pos, size_t limit)
{
	return ++pos == limit ? 0 : pos;
}

900
static int rocker_desc_err(const struct rocker_desc_info *desc_info)
901
{
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
	int err = desc_info->desc->comp_err & ~ROCKER_DMA_DESC_COMP_ERR_GEN;

	switch (err) {
	case ROCKER_OK:
		return 0;
	case -ROCKER_ENOENT:
		return -ENOENT;
	case -ROCKER_ENXIO:
		return -ENXIO;
	case -ROCKER_ENOMEM:
		return -ENOMEM;
	case -ROCKER_EEXIST:
		return -EEXIST;
	case -ROCKER_EINVAL:
		return -EINVAL;
	case -ROCKER_EMSGSIZE:
		return -EMSGSIZE;
	case -ROCKER_ENOTSUP:
		return -EOPNOTSUPP;
	case -ROCKER_ENOBUFS:
		return -ENOBUFS;
	}

	return -EINVAL;
926 927
}

928
static void rocker_desc_gen_clear(const struct rocker_desc_info *desc_info)
929 930 931 932
{
	desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN;
}

933
static bool rocker_desc_gen(const struct rocker_desc_info *desc_info)
934 935 936 937 938 939
{
	u32 comp_err = desc_info->desc->comp_err;

	return comp_err & ROCKER_DMA_DESC_COMP_ERR_GEN ? true : false;
}

940
static void *rocker_desc_cookie_ptr_get(const struct rocker_desc_info *desc_info)
941
{
942
	return (void *)(uintptr_t)desc_info->desc->cookie;
943 944
}

945
static void rocker_desc_cookie_ptr_set(const struct rocker_desc_info *desc_info,
946 947
				       void *ptr)
{
948
	desc_info->desc->cookie = (uintptr_t) ptr;
949 950 951
}

static struct rocker_desc_info *
952
rocker_desc_head_get(const struct rocker_dma_ring_info *info)
953 954 955 956 957 958 959 960 961 962 963
{
	static struct rocker_desc_info *desc_info;
	u32 head = __pos_inc(info->head, info->size);

	desc_info = &info->desc_info[info->head];
	if (head == info->tail)
		return NULL; /* ring full */
	desc_info->tlv_size = 0;
	return desc_info;
}

964
static void rocker_desc_commit(const struct rocker_desc_info *desc_info)
965 966 967 968 969
{
	desc_info->desc->buf_size = desc_info->data_size;
	desc_info->desc->tlv_size = desc_info->tlv_size;
}

970
static void rocker_desc_head_set(const struct rocker *rocker,
971
				 struct rocker_dma_ring_info *info,
972
				 const struct rocker_desc_info *desc_info)
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
{
	u32 head = __pos_inc(info->head, info->size);

	BUG_ON(head == info->tail);
	rocker_desc_commit(desc_info);
	info->head = head;
	rocker_write32(rocker, DMA_DESC_HEAD(info->type), head);
}

static struct rocker_desc_info *
rocker_desc_tail_get(struct rocker_dma_ring_info *info)
{
	static struct rocker_desc_info *desc_info;

	if (info->tail == info->head)
		return NULL; /* nothing to be done between head and tail */
	desc_info = &info->desc_info[info->tail];
	if (!rocker_desc_gen(desc_info))
		return NULL; /* gen bit not set, desc is not ready yet */
	info->tail = __pos_inc(info->tail, info->size);
	desc_info->tlv_size = desc_info->desc->tlv_size;
	return desc_info;
}

997 998
static void rocker_dma_ring_credits_set(const struct rocker *rocker,
					const struct rocker_dma_ring_info *info,
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
					u32 credits)
{
	if (credits)
		rocker_write32(rocker, DMA_DESC_CREDITS(info->type), credits);
}

static unsigned long rocker_dma_ring_size_fix(size_t size)
{
	return max(ROCKER_DMA_SIZE_MIN,
		   min(roundup_pow_of_two(size), ROCKER_DMA_SIZE_MAX));
}

1011
static int rocker_dma_ring_create(const struct rocker *rocker,
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
				  unsigned int type,
				  size_t size,
				  struct rocker_dma_ring_info *info)
{
	int i;

	BUG_ON(size != rocker_dma_ring_size_fix(size));
	info->size = size;
	info->type = type;
	info->head = 0;
	info->tail = 0;
	info->desc_info = kcalloc(info->size, sizeof(*info->desc_info),
				  GFP_KERNEL);
	if (!info->desc_info)
		return -ENOMEM;

	info->desc = pci_alloc_consistent(rocker->pdev,
					  info->size * sizeof(*info->desc),
					  &info->mapaddr);
	if (!info->desc) {
		kfree(info->desc_info);
		return -ENOMEM;
	}

	for (i = 0; i < info->size; i++)
		info->desc_info[i].desc = &info->desc[i];

	rocker_write32(rocker, DMA_DESC_CTRL(info->type),
		       ROCKER_DMA_DESC_CTRL_RESET);
	rocker_write64(rocker, DMA_DESC_ADDR(info->type), info->mapaddr);
	rocker_write32(rocker, DMA_DESC_SIZE(info->type), info->size);

	return 0;
}

1047 1048
static void rocker_dma_ring_destroy(const struct rocker *rocker,
				    const struct rocker_dma_ring_info *info)
1049 1050 1051 1052 1053 1054 1055 1056 1057
{
	rocker_write64(rocker, DMA_DESC_ADDR(info->type), 0);

	pci_free_consistent(rocker->pdev,
			    info->size * sizeof(struct rocker_desc),
			    info->desc, info->mapaddr);
	kfree(info->desc_info);
}

1058
static void rocker_dma_ring_pass_to_producer(const struct rocker *rocker,
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
					     struct rocker_dma_ring_info *info)
{
	int i;

	BUG_ON(info->head || info->tail);

	/* When ring is consumer, we need to advance head for each desc.
	 * That tells hw that the desc is ready to be used by it.
	 */
	for (i = 0; i < info->size - 1; i++)
		rocker_desc_head_set(rocker, info, &info->desc_info[i]);
	rocker_desc_commit(&info->desc_info[i]);
}

1073 1074
static int rocker_dma_ring_bufs_alloc(const struct rocker *rocker,
				      const struct rocker_dma_ring_info *info,
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
				      int direction, size_t buf_size)
{
	struct pci_dev *pdev = rocker->pdev;
	int i;
	int err;

	for (i = 0; i < info->size; i++) {
		struct rocker_desc_info *desc_info = &info->desc_info[i];
		struct rocker_desc *desc = &info->desc[i];
		dma_addr_t dma_handle;
		char *buf;

		buf = kzalloc(buf_size, GFP_KERNEL | GFP_DMA);
		if (!buf) {
			err = -ENOMEM;
			goto rollback;
		}

		dma_handle = pci_map_single(pdev, buf, buf_size, direction);
		if (pci_dma_mapping_error(pdev, dma_handle)) {
			kfree(buf);
			err = -EIO;
			goto rollback;
		}

		desc_info->data = buf;
		desc_info->data_size = buf_size;
		dma_unmap_addr_set(desc_info, mapaddr, dma_handle);

		desc->buf_addr = dma_handle;
		desc->buf_size = buf_size;
	}
	return 0;

rollback:
	for (i--; i >= 0; i--) {
1111
		const struct rocker_desc_info *desc_info = &info->desc_info[i];
1112 1113 1114 1115 1116 1117 1118 1119

		pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
				 desc_info->data_size, direction);
		kfree(desc_info->data);
	}
	return err;
}

1120 1121
static void rocker_dma_ring_bufs_free(const struct rocker *rocker,
				      const struct rocker_dma_ring_info *info,
1122 1123 1124 1125 1126 1127
				      int direction)
{
	struct pci_dev *pdev = rocker->pdev;
	int i;

	for (i = 0; i < info->size; i++) {
1128
		const struct rocker_desc_info *desc_info = &info->desc_info[i];
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
		struct rocker_desc *desc = &info->desc[i];

		desc->buf_addr = 0;
		desc->buf_size = 0;
		pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
				 desc_info->data_size, direction);
		kfree(desc_info->data);
	}
}

static int rocker_dma_rings_init(struct rocker *rocker)
{
1141
	const struct pci_dev *pdev = rocker->pdev;
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
	int err;

	err = rocker_dma_ring_create(rocker, ROCKER_DMA_CMD,
				     ROCKER_DMA_CMD_DEFAULT_SIZE,
				     &rocker->cmd_ring);
	if (err) {
		dev_err(&pdev->dev, "failed to create command dma ring\n");
		return err;
	}

	spin_lock_init(&rocker->cmd_ring_lock);

	err = rocker_dma_ring_bufs_alloc(rocker, &rocker->cmd_ring,
					 PCI_DMA_BIDIRECTIONAL, PAGE_SIZE);
	if (err) {
		dev_err(&pdev->dev, "failed to alloc command dma ring buffers\n");
		goto err_dma_cmd_ring_bufs_alloc;
	}

	err = rocker_dma_ring_create(rocker, ROCKER_DMA_EVENT,
				     ROCKER_DMA_EVENT_DEFAULT_SIZE,
				     &rocker->event_ring);
	if (err) {
		dev_err(&pdev->dev, "failed to create event dma ring\n");
		goto err_dma_event_ring_create;
	}

	err = rocker_dma_ring_bufs_alloc(rocker, &rocker->event_ring,
					 PCI_DMA_FROMDEVICE, PAGE_SIZE);
	if (err) {
		dev_err(&pdev->dev, "failed to alloc event dma ring buffers\n");
		goto err_dma_event_ring_bufs_alloc;
	}
	rocker_dma_ring_pass_to_producer(rocker, &rocker->event_ring);
	return 0;

err_dma_event_ring_bufs_alloc:
	rocker_dma_ring_destroy(rocker, &rocker->event_ring);
err_dma_event_ring_create:
	rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
				  PCI_DMA_BIDIRECTIONAL);
err_dma_cmd_ring_bufs_alloc:
	rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
	return err;
}

static void rocker_dma_rings_fini(struct rocker *rocker)
{
	rocker_dma_ring_bufs_free(rocker, &rocker->event_ring,
				  PCI_DMA_BIDIRECTIONAL);
	rocker_dma_ring_destroy(rocker, &rocker->event_ring);
	rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
				  PCI_DMA_BIDIRECTIONAL);
	rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
}

1198
static int rocker_dma_rx_ring_skb_map(const struct rocker_port *rocker_port,
1199 1200 1201
				      struct rocker_desc_info *desc_info,
				      struct sk_buff *skb, size_t buf_len)
{
1202
	const struct rocker *rocker = rocker_port->rocker;
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
	struct pci_dev *pdev = rocker->pdev;
	dma_addr_t dma_handle;

	dma_handle = pci_map_single(pdev, skb->data, buf_len,
				    PCI_DMA_FROMDEVICE);
	if (pci_dma_mapping_error(pdev, dma_handle))
		return -EIO;
	if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_RX_FRAG_ADDR, dma_handle))
		goto tlv_put_failure;
	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_RX_FRAG_MAX_LEN, buf_len))
		goto tlv_put_failure;
	return 0;

tlv_put_failure:
	pci_unmap_single(pdev, dma_handle, buf_len, PCI_DMA_FROMDEVICE);
	desc_info->tlv_size = 0;
	return -EMSGSIZE;
}

1222
static size_t rocker_port_rx_buf_len(const struct rocker_port *rocker_port)
1223 1224 1225 1226
{
	return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
}

1227
static int rocker_dma_rx_ring_skb_alloc(const struct rocker_port *rocker_port,
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
					struct rocker_desc_info *desc_info)
{
	struct net_device *dev = rocker_port->dev;
	struct sk_buff *skb;
	size_t buf_len = rocker_port_rx_buf_len(rocker_port);
	int err;

	/* Ensure that hw will see tlv_size zero in case of an error.
	 * That tells hw to use another descriptor.
	 */
	rocker_desc_cookie_ptr_set(desc_info, NULL);
	desc_info->tlv_size = 0;

	skb = netdev_alloc_skb_ip_align(dev, buf_len);
	if (!skb)
		return -ENOMEM;
1244
	err = rocker_dma_rx_ring_skb_map(rocker_port, desc_info, skb, buf_len);
1245 1246 1247 1248 1249 1250 1251 1252
	if (err) {
		dev_kfree_skb_any(skb);
		return err;
	}
	rocker_desc_cookie_ptr_set(desc_info, skb);
	return 0;
}

1253 1254
static void rocker_dma_rx_ring_skb_unmap(const struct rocker *rocker,
					 const struct rocker_tlv **attrs)
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
{
	struct pci_dev *pdev = rocker->pdev;
	dma_addr_t dma_handle;
	size_t len;

	if (!attrs[ROCKER_TLV_RX_FRAG_ADDR] ||
	    !attrs[ROCKER_TLV_RX_FRAG_MAX_LEN])
		return;
	dma_handle = rocker_tlv_get_u64(attrs[ROCKER_TLV_RX_FRAG_ADDR]);
	len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]);
	pci_unmap_single(pdev, dma_handle, len, PCI_DMA_FROMDEVICE);
}

1268 1269
static void rocker_dma_rx_ring_skb_free(const struct rocker *rocker,
					const struct rocker_desc_info *desc_info)
1270
{
1271
	const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
1272 1273 1274 1275 1276 1277 1278 1279 1280
	struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);

	if (!skb)
		return;
	rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
	rocker_dma_rx_ring_skb_unmap(rocker, attrs);
	dev_kfree_skb_any(skb);
}

1281
static int rocker_dma_rx_ring_skbs_alloc(const struct rocker_port *rocker_port)
1282
{
1283
	const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
1284
	const struct rocker *rocker = rocker_port->rocker;
1285 1286 1287 1288
	int i;
	int err;

	for (i = 0; i < rx_ring->size; i++) {
1289
		err = rocker_dma_rx_ring_skb_alloc(rocker_port,
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
						   &rx_ring->desc_info[i]);
		if (err)
			goto rollback;
	}
	return 0;

rollback:
	for (i--; i >= 0; i--)
		rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
	return err;
}

1302
static void rocker_dma_rx_ring_skbs_free(const struct rocker_port *rocker_port)
1303
{
1304
	const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
1305
	const struct rocker *rocker = rocker_port->rocker;
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
	int i;

	for (i = 0; i < rx_ring->size; i++)
		rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
}

static int rocker_port_dma_rings_init(struct rocker_port *rocker_port)
{
	struct rocker *rocker = rocker_port->rocker;
	int err;

	err = rocker_dma_ring_create(rocker,
				     ROCKER_DMA_TX(rocker_port->port_number),
				     ROCKER_DMA_TX_DEFAULT_SIZE,
				     &rocker_port->tx_ring);
	if (err) {
		netdev_err(rocker_port->dev, "failed to create tx dma ring\n");
		return err;
	}

	err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->tx_ring,
					 PCI_DMA_TODEVICE,
					 ROCKER_DMA_TX_DESC_SIZE);
	if (err) {
		netdev_err(rocker_port->dev, "failed to alloc tx dma ring buffers\n");
		goto err_dma_tx_ring_bufs_alloc;
	}

	err = rocker_dma_ring_create(rocker,
				     ROCKER_DMA_RX(rocker_port->port_number),
				     ROCKER_DMA_RX_DEFAULT_SIZE,
				     &rocker_port->rx_ring);
	if (err) {
		netdev_err(rocker_port->dev, "failed to create rx dma ring\n");
		goto err_dma_rx_ring_create;
	}

	err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->rx_ring,
					 PCI_DMA_BIDIRECTIONAL,
					 ROCKER_DMA_RX_DESC_SIZE);
	if (err) {
		netdev_err(rocker_port->dev, "failed to alloc rx dma ring buffers\n");
		goto err_dma_rx_ring_bufs_alloc;
	}

1351
	err = rocker_dma_rx_ring_skbs_alloc(rocker_port);
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
	if (err) {
		netdev_err(rocker_port->dev, "failed to alloc rx dma ring skbs\n");
		goto err_dma_rx_ring_skbs_alloc;
	}
	rocker_dma_ring_pass_to_producer(rocker, &rocker_port->rx_ring);

	return 0;

err_dma_rx_ring_skbs_alloc:
	rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
				  PCI_DMA_BIDIRECTIONAL);
err_dma_rx_ring_bufs_alloc:
	rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
err_dma_rx_ring_create:
	rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
				  PCI_DMA_TODEVICE);
err_dma_tx_ring_bufs_alloc:
	rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
	return err;
}

static void rocker_port_dma_rings_fini(struct rocker_port *rocker_port)
{
	struct rocker *rocker = rocker_port->rocker;

1377
	rocker_dma_rx_ring_skbs_free(rocker_port);
1378 1379 1380 1381 1382 1383 1384 1385
	rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
				  PCI_DMA_BIDIRECTIONAL);
	rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
	rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
				  PCI_DMA_TODEVICE);
	rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
}

1386 1387
static void rocker_port_set_enable(const struct rocker_port *rocker_port,
				   bool enable)
1388 1389 1390 1391
{
	u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE);

	if (enable)
1392
		val |= 1ULL << rocker_port->pport;
1393
	else
1394
		val &= ~(1ULL << rocker_port->pport);
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
	rocker_write64(rocker_port->rocker, PORT_PHYS_ENABLE, val);
}

/********************************
 * Interrupt handler and helpers
 ********************************/

static irqreturn_t rocker_cmd_irq_handler(int irq, void *dev_id)
{
	struct rocker *rocker = dev_id;
1405
	const struct rocker_desc_info *desc_info;
1406 1407 1408 1409 1410 1411
	struct rocker_wait *wait;
	u32 credits = 0;

	spin_lock(&rocker->cmd_ring_lock);
	while ((desc_info = rocker_desc_tail_get(&rocker->cmd_ring))) {
		wait = rocker_desc_cookie_ptr_get(desc_info);
1412 1413 1414 1415 1416 1417
		if (wait->nowait) {
			rocker_desc_gen_clear(desc_info);
			rocker_wait_destroy(SWITCHDEV_TRANS_NONE, wait);
		} else {
			rocker_wait_wake_up(wait);
		}
1418 1419 1420 1421 1422 1423 1424 1425
		credits++;
	}
	spin_unlock(&rocker->cmd_ring_lock);
	rocker_dma_ring_credits_set(rocker, &rocker->cmd_ring, credits);

	return IRQ_HANDLED;
}

1426
static void rocker_port_link_up(const struct rocker_port *rocker_port)
1427 1428 1429 1430 1431
{
	netif_carrier_on(rocker_port->dev);
	netdev_info(rocker_port->dev, "Link is up\n");
}

1432
static void rocker_port_link_down(const struct rocker_port *rocker_port)
1433 1434 1435 1436 1437
{
	netif_carrier_off(rocker_port->dev);
	netdev_info(rocker_port->dev, "Link is down\n");
}

1438
static int rocker_event_link_change(const struct rocker *rocker,
1439 1440
				    const struct rocker_tlv *info)
{
1441
	const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1];
1442 1443 1444 1445 1446
	unsigned int port_number;
	bool link_up;
	struct rocker_port *rocker_port;

	rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_LINK_CHANGED_MAX, info);
S
Scott Feldman 已提交
1447
	if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] ||
1448 1449 1450
	    !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP])
		return -EIO;
	port_number =
S
Scott Feldman 已提交
1451
		rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1;
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
	link_up = rocker_tlv_get_u8(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]);

	if (port_number >= rocker->port_count)
		return -EINVAL;

	rocker_port = rocker->ports[port_number];
	if (netif_carrier_ok(rocker_port->dev) != link_up) {
		if (link_up)
			rocker_port_link_up(rocker_port);
		else
			rocker_port_link_down(rocker_port);
	}

	return 0;
}

1468
static int rocker_port_fdb(struct rocker_port *rocker_port,
1469
			   enum switchdev_trans_ph trans_ph,
1470 1471 1472
			   const unsigned char *addr,
			   __be16 vlan_id, int flags);

1473
static int rocker_event_mac_vlan_seen(const struct rocker *rocker,
1474 1475
				      const struct rocker_tlv *info)
{
1476
	const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1];
1477 1478
	unsigned int port_number;
	struct rocker_port *rocker_port;
1479
	const unsigned char *addr;
1480
	int flags = ROCKER_OP_FLAG_NOWAIT | ROCKER_OP_FLAG_LEARNED;
1481 1482 1483
	__be16 vlan_id;

	rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info);
S
Scott Feldman 已提交
1484
	if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] ||
1485 1486 1487 1488
	    !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] ||
	    !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID])
		return -EIO;
	port_number =
S
Scott Feldman 已提交
1489
		rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1;
1490
	addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]);
1491
	vlan_id = rocker_tlv_get_be16(attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]);
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

	if (port_number >= rocker->port_count)
		return -EINVAL;

	rocker_port = rocker->ports[port_number];

	if (rocker_port->stp_state != BR_STATE_LEARNING &&
	    rocker_port->stp_state != BR_STATE_FORWARDING)
		return 0;

1502 1503
	return rocker_port_fdb(rocker_port, SWITCHDEV_TRANS_NONE,
			       addr, vlan_id, flags);
1504
}
1505

1506 1507
static int rocker_event_process(const struct rocker *rocker,
				const struct rocker_desc_info *desc_info)
1508
{
1509 1510
	const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1];
	const struct rocker_tlv *info;
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
	u16 type;

	rocker_tlv_parse_desc(attrs, ROCKER_TLV_EVENT_MAX, desc_info);
	if (!attrs[ROCKER_TLV_EVENT_TYPE] ||
	    !attrs[ROCKER_TLV_EVENT_INFO])
		return -EIO;

	type = rocker_tlv_get_u16(attrs[ROCKER_TLV_EVENT_TYPE]);
	info = attrs[ROCKER_TLV_EVENT_INFO];

	switch (type) {
	case ROCKER_TLV_EVENT_TYPE_LINK_CHANGED:
		return rocker_event_link_change(rocker, info);
1524 1525
	case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN:
		return rocker_event_mac_vlan_seen(rocker, info);
1526 1527 1528 1529 1530 1531 1532 1533
	}

	return -EOPNOTSUPP;
}

static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id)
{
	struct rocker *rocker = dev_id;
1534 1535
	const struct pci_dev *pdev = rocker->pdev;
	const struct rocker_desc_info *desc_info;
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
	u32 credits = 0;
	int err;

	while ((desc_info = rocker_desc_tail_get(&rocker->event_ring))) {
		err = rocker_desc_err(desc_info);
		if (err) {
			dev_err(&pdev->dev, "event desc received with err %d\n",
				err);
		} else {
			err = rocker_event_process(rocker, desc_info);
			if (err)
				dev_err(&pdev->dev, "event processing failed with err %d\n",
					err);
		}
		rocker_desc_gen_clear(desc_info);
		rocker_desc_head_set(rocker, &rocker->event_ring, desc_info);
		credits++;
	}
	rocker_dma_ring_credits_set(rocker, &rocker->event_ring, credits);

	return IRQ_HANDLED;
}

static irqreturn_t rocker_tx_irq_handler(int irq, void *dev_id)
{
	struct rocker_port *rocker_port = dev_id;

	napi_schedule(&rocker_port->napi_tx);
	return IRQ_HANDLED;
}

static irqreturn_t rocker_rx_irq_handler(int irq, void *dev_id)
{
	struct rocker_port *rocker_port = dev_id;

	napi_schedule(&rocker_port->napi_rx);
	return IRQ_HANDLED;
}

/********************
 * Command interface
 ********************/

1579
typedef int (*rocker_cmd_prep_cb_t)(const struct rocker_port *rocker_port,
1580 1581 1582
				    struct rocker_desc_info *desc_info,
				    void *priv);

1583
typedef int (*rocker_cmd_proc_cb_t)(const struct rocker_port *rocker_port,
1584 1585
				    const struct rocker_desc_info *desc_info,
				    void *priv);
1586

1587
static int rocker_cmd_exec(struct rocker_port *rocker_port,
1588
			   enum switchdev_trans_ph trans_ph, int flags,
1589 1590
			   rocker_cmd_prep_cb_t prepare, void *prepare_priv,
			   rocker_cmd_proc_cb_t process, void *process_priv)
1591
{
1592
	struct rocker *rocker = rocker_port->rocker;
1593 1594
	struct rocker_desc_info *desc_info;
	struct rocker_wait *wait;
1595 1596
	bool nowait = !!(flags & ROCKER_OP_FLAG_NOWAIT);
	unsigned long lock_flags;
1597 1598
	int err;

1599
	wait = rocker_wait_create(rocker_port, trans_ph, flags);
1600 1601
	if (!wait)
		return -ENOMEM;
1602
	wait->nowait = nowait;
1603

1604
	spin_lock_irqsave(&rocker->cmd_ring_lock, lock_flags);
1605

1606 1607
	desc_info = rocker_desc_head_get(&rocker->cmd_ring);
	if (!desc_info) {
1608
		spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1609 1610 1611
		err = -EAGAIN;
		goto out;
	}
1612

1613
	err = prepare(rocker_port, desc_info, prepare_priv);
1614
	if (err) {
1615
		spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1616 1617
		goto out;
	}
1618

1619 1620
	rocker_desc_cookie_ptr_set(desc_info, wait);

1621
	if (trans_ph != SWITCHDEV_TRANS_PREPARE)
1622
		rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info);
1623

1624 1625 1626 1627
	spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);

	if (nowait)
		return 0;
1628

1629
	if (trans_ph != SWITCHDEV_TRANS_PREPARE)
1630 1631
		if (!rocker_wait_event_timeout(wait, HZ / 10))
			return -EIO;
1632 1633 1634 1635 1636 1637

	err = rocker_desc_err(desc_info);
	if (err)
		return err;

	if (process)
1638
		err = process(rocker_port, desc_info, process_priv);
1639 1640 1641

	rocker_desc_gen_clear(desc_info);
out:
1642
	rocker_wait_destroy(trans_ph, wait);
1643 1644 1645 1646
	return err;
}

static int
1647
rocker_cmd_get_port_settings_prep(const struct rocker_port *rocker_port,
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
				  struct rocker_desc_info *desc_info,
				  void *priv)
{
	struct rocker_tlv *cmd_info;

	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
			       ROCKER_TLV_CMD_TYPE_GET_PORT_SETTINGS))
		return -EMSGSIZE;
	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
	if (!cmd_info)
		return -EMSGSIZE;
S
Scott Feldman 已提交
1659 1660
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
			       rocker_port->pport))
1661 1662 1663 1664 1665 1666
		return -EMSGSIZE;
	rocker_tlv_nest_end(desc_info, cmd_info);
	return 0;
}

static int
1667
rocker_cmd_get_port_settings_ethtool_proc(const struct rocker_port *rocker_port,
1668
					  const struct rocker_desc_info *desc_info,
1669 1670 1671
					  void *priv)
{
	struct ethtool_cmd *ecmd = priv;
1672 1673
	const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
	const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
	u32 speed;
	u8 duplex;
	u8 autoneg;

	rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
	if (!attrs[ROCKER_TLV_CMD_INFO])
		return -EIO;

	rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
				attrs[ROCKER_TLV_CMD_INFO]);
	if (!info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED] ||
	    !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX] ||
	    !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG])
		return -EIO;

	speed = rocker_tlv_get_u32(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED]);
	duplex = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX]);
	autoneg = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]);

	ecmd->transceiver = XCVR_INTERNAL;
	ecmd->supported = SUPPORTED_TP;
	ecmd->phy_address = 0xff;
	ecmd->port = PORT_TP;
	ethtool_cmd_speed_set(ecmd, speed);
	ecmd->duplex = duplex ? DUPLEX_FULL : DUPLEX_HALF;
	ecmd->autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;

	return 0;
}

static int
1705
rocker_cmd_get_port_settings_macaddr_proc(const struct rocker_port *rocker_port,
1706
					  const struct rocker_desc_info *desc_info,
1707 1708 1709
					  void *priv)
{
	unsigned char *macaddr = priv;
1710 1711 1712
	const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
	const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
	const struct rocker_tlv *attr;
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730

	rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
	if (!attrs[ROCKER_TLV_CMD_INFO])
		return -EIO;

	rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
				attrs[ROCKER_TLV_CMD_INFO]);
	attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR];
	if (!attr)
		return -EIO;

	if (rocker_tlv_len(attr) != ETH_ALEN)
		return -EINVAL;

	ether_addr_copy(macaddr, rocker_tlv_data(attr));
	return 0;
}

1731 1732 1733 1734 1735 1736
struct port_name {
	char *buf;
	size_t len;
};

static int
1737
rocker_cmd_get_port_settings_phys_name_proc(const struct rocker_port *rocker_port,
1738
					    const struct rocker_desc_info *desc_info,
1739 1740
					    void *priv)
{
1741 1742
	const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
	const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1743
	struct port_name *name = priv;
1744
	const struct rocker_tlv *attr;
1745
	size_t i, j, len;
1746
	const char *str;
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776

	rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
	if (!attrs[ROCKER_TLV_CMD_INFO])
		return -EIO;

	rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
				attrs[ROCKER_TLV_CMD_INFO]);
	attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_PHYS_NAME];
	if (!attr)
		return -EIO;

	len = min_t(size_t, rocker_tlv_len(attr), name->len);
	str = rocker_tlv_data(attr);

	/* make sure name only contains alphanumeric characters */
	for (i = j = 0; i < len; ++i) {
		if (isalnum(str[i])) {
			name->buf[j] = str[i];
			j++;
		}
	}

	if (j == 0)
		return -EIO;

	name->buf[j] = '\0';

	return 0;
}

1777
static int
1778
rocker_cmd_set_port_settings_ethtool_prep(const struct rocker_port *rocker_port,
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
					  struct rocker_desc_info *desc_info,
					  void *priv)
{
	struct ethtool_cmd *ecmd = priv;
	struct rocker_tlv *cmd_info;

	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
			       ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
		return -EMSGSIZE;
	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
	if (!cmd_info)
		return -EMSGSIZE;
S
Scott Feldman 已提交
1791 1792
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
			       rocker_port->pport))
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
		return -EMSGSIZE;
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_SPEED,
			       ethtool_cmd_speed(ecmd)))
		return -EMSGSIZE;
	if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX,
			      ecmd->duplex))
		return -EMSGSIZE;
	if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG,
			      ecmd->autoneg))
		return -EMSGSIZE;
	rocker_tlv_nest_end(desc_info, cmd_info);
	return 0;
}

static int
1808
rocker_cmd_set_port_settings_macaddr_prep(const struct rocker_port *rocker_port,
1809 1810 1811
					  struct rocker_desc_info *desc_info,
					  void *priv)
{
1812
	const unsigned char *macaddr = priv;
1813 1814 1815 1816 1817 1818 1819 1820
	struct rocker_tlv *cmd_info;

	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
			       ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
		return -EMSGSIZE;
	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
	if (!cmd_info)
		return -EMSGSIZE;
S
Scott Feldman 已提交
1821 1822
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
			       rocker_port->pport))
1823 1824 1825 1826 1827 1828 1829 1830
		return -EMSGSIZE;
	if (rocker_tlv_put(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR,
			   ETH_ALEN, macaddr))
		return -EMSGSIZE;
	rocker_tlv_nest_end(desc_info, cmd_info);
	return 0;
}

S
Scott Feldman 已提交
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
static int
rocker_cmd_set_port_settings_mtu_prep(const struct rocker_port *rocker_port,
				      struct rocker_desc_info *desc_info,
				      void *priv)
{
	int mtu = *(int *)priv;
	struct rocker_tlv *cmd_info;

	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
			       ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
		return -EMSGSIZE;
	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
	if (!cmd_info)
		return -EMSGSIZE;
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
			       rocker_port->pport))
		return -EMSGSIZE;
	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MTU,
			       mtu))
		return -EMSGSIZE;
	rocker_tlv_nest_end(desc_info, cmd_info);
	return 0;
}

1855
static int
1856
rocker_cmd_set_port_learning_prep(const struct rocker_port *rocker_port,
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
				  struct rocker_desc_info *desc_info,
				  void *priv)
{
	struct rocker_tlv *cmd_info;

	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
			       ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
		return -EMSGSIZE;
	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
	if (!cmd_info)
		return -EMSGSIZE;
S
Scott Feldman 已提交
1868 1869
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
			       rocker_port->pport))
1870 1871 1872 1873 1874 1875 1876 1877
		return -EMSGSIZE;
	if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING,
			      !!(rocker_port->brport_flags & BR_LEARNING)))
		return -EMSGSIZE;
	rocker_tlv_nest_end(desc_info, cmd_info);
	return 0;
}

1878 1879 1880
static int rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port,
						struct ethtool_cmd *ecmd)
{
1881
	return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
1882 1883
			       rocker_cmd_get_port_settings_prep, NULL,
			       rocker_cmd_get_port_settings_ethtool_proc,
1884
			       ecmd);
1885 1886 1887 1888 1889
}

static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port,
						unsigned char *macaddr)
{
1890
	return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
1891 1892
			       rocker_cmd_get_port_settings_prep, NULL,
			       rocker_cmd_get_port_settings_macaddr_proc,
1893
			       macaddr);
1894 1895 1896 1897 1898
}

static int rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port,
						struct ethtool_cmd *ecmd)
{
1899
	return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
1900
			       rocker_cmd_set_port_settings_ethtool_prep,
1901
			       ecmd, NULL, NULL);
1902 1903 1904 1905 1906
}

static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port,
						unsigned char *macaddr)
{
1907
	return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
1908
			       rocker_cmd_set_port_settings_macaddr_prep,
1909
			       macaddr, NULL, NULL);
1910 1911
}

S
Scott Feldman 已提交
1912 1913 1914 1915 1916 1917 1918 1919
static int rocker_cmd_set_port_settings_mtu(struct rocker_port *rocker_port,
					    int mtu)
{
	return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
			       rocker_cmd_set_port_settings_mtu_prep,
			       &mtu, NULL, NULL);
}

1920
static int rocker_port_set_learning(struct rocker_port *rocker_port,
1921
				    enum switchdev_trans_ph trans_ph)
1922
{
1923
	return rocker_cmd_exec(rocker_port, trans_ph, 0,
1924
			       rocker_cmd_set_port_learning_prep,
1925
			       NULL, NULL, NULL);
1926 1927
}

1928 1929 1930
static int
rocker_cmd_flow_tbl_add_ig_port(struct rocker_desc_info *desc_info,
				const struct rocker_flow_tbl_entry *entry)
1931
{
S
Scott Feldman 已提交
1932 1933
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
			       entry->key.ig_port.in_pport))
1934
		return -EMSGSIZE;
S
Scott Feldman 已提交
1935 1936
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
			       entry->key.ig_port.in_pport_mask))
1937 1938 1939 1940 1941 1942 1943 1944
		return -EMSGSIZE;
	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
			       entry->key.ig_port.goto_tbl))
		return -EMSGSIZE;

	return 0;
}

1945 1946 1947
static int
rocker_cmd_flow_tbl_add_vlan(struct rocker_desc_info *desc_info,
			     const struct rocker_flow_tbl_entry *entry)
1948
{
S
Scott Feldman 已提交
1949 1950
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
			       entry->key.vlan.in_pport))
1951
		return -EMSGSIZE;
1952 1953
	if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
				entry->key.vlan.vlan_id))
1954
		return -EMSGSIZE;
1955 1956
	if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
				entry->key.vlan.vlan_id_mask))
1957 1958 1959 1960 1961
		return -EMSGSIZE;
	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
			       entry->key.vlan.goto_tbl))
		return -EMSGSIZE;
	if (entry->key.vlan.untagged &&
1962 1963
	    rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_NEW_VLAN_ID,
				entry->key.vlan.new_vlan_id))
1964 1965 1966 1967 1968
		return -EMSGSIZE;

	return 0;
}

1969 1970 1971
static int
rocker_cmd_flow_tbl_add_term_mac(struct rocker_desc_info *desc_info,
				 const struct rocker_flow_tbl_entry *entry)
1972
{
S
Scott Feldman 已提交
1973 1974
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
			       entry->key.term_mac.in_pport))
1975
		return -EMSGSIZE;
S
Scott Feldman 已提交
1976 1977
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
			       entry->key.term_mac.in_pport_mask))
1978
		return -EMSGSIZE;
1979 1980
	if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
				entry->key.term_mac.eth_type))
1981 1982 1983 1984 1985 1986 1987
		return -EMSGSIZE;
	if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
			   ETH_ALEN, entry->key.term_mac.eth_dst))
		return -EMSGSIZE;
	if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
			   ETH_ALEN, entry->key.term_mac.eth_dst_mask))
		return -EMSGSIZE;
1988 1989
	if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
				entry->key.term_mac.vlan_id))
1990
		return -EMSGSIZE;
1991 1992
	if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
				entry->key.term_mac.vlan_id_mask))
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
		return -EMSGSIZE;
	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
			       entry->key.term_mac.goto_tbl))
		return -EMSGSIZE;
	if (entry->key.term_mac.copy_to_cpu &&
	    rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION,
			      entry->key.term_mac.copy_to_cpu))
		return -EMSGSIZE;

	return 0;
}

static int
rocker_cmd_flow_tbl_add_ucast_routing(struct rocker_desc_info *desc_info,
2007
				      const struct rocker_flow_tbl_entry *entry)
2008
{
2009 2010
	if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
				entry->key.ucast_routing.eth_type))
2011
		return -EMSGSIZE;
2012 2013
	if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP,
				entry->key.ucast_routing.dst4))
2014
		return -EMSGSIZE;
2015 2016
	if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP_MASK,
				entry->key.ucast_routing.dst4_mask))
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
		return -EMSGSIZE;
	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
			       entry->key.ucast_routing.goto_tbl))
		return -EMSGSIZE;
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
			       entry->key.ucast_routing.group_id))
		return -EMSGSIZE;

	return 0;
}

2028 2029 2030
static int
rocker_cmd_flow_tbl_add_bridge(struct rocker_desc_info *desc_info,
			       const struct rocker_flow_tbl_entry *entry)
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
{
	if (entry->key.bridge.has_eth_dst &&
	    rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
			   ETH_ALEN, entry->key.bridge.eth_dst))
		return -EMSGSIZE;
	if (entry->key.bridge.has_eth_dst_mask &&
	    rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
			   ETH_ALEN, entry->key.bridge.eth_dst_mask))
		return -EMSGSIZE;
	if (entry->key.bridge.vlan_id &&
2041 2042
	    rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
				entry->key.bridge.vlan_id))
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
		return -EMSGSIZE;
	if (entry->key.bridge.tunnel_id &&
	    rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_TUNNEL_ID,
			       entry->key.bridge.tunnel_id))
		return -EMSGSIZE;
	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
			       entry->key.bridge.goto_tbl))
		return -EMSGSIZE;
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
			       entry->key.bridge.group_id))
		return -EMSGSIZE;
	if (entry->key.bridge.copy_to_cpu &&
	    rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION,
			      entry->key.bridge.copy_to_cpu))
		return -EMSGSIZE;

	return 0;
}

2062 2063 2064
static int
rocker_cmd_flow_tbl_add_acl(struct rocker_desc_info *desc_info,
			    const struct rocker_flow_tbl_entry *entry)
2065
{
S
Scott Feldman 已提交
2066 2067
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
			       entry->key.acl.in_pport))
2068
		return -EMSGSIZE;
S
Scott Feldman 已提交
2069 2070
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
			       entry->key.acl.in_pport_mask))
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
		return -EMSGSIZE;
	if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
			   ETH_ALEN, entry->key.acl.eth_src))
		return -EMSGSIZE;
	if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC_MASK,
			   ETH_ALEN, entry->key.acl.eth_src_mask))
		return -EMSGSIZE;
	if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
			   ETH_ALEN, entry->key.acl.eth_dst))
		return -EMSGSIZE;
	if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
			   ETH_ALEN, entry->key.acl.eth_dst_mask))
		return -EMSGSIZE;
2084 2085
	if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
				entry->key.acl.eth_type))
2086
		return -EMSGSIZE;
2087 2088
	if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
				entry->key.acl.vlan_id))
2089
		return -EMSGSIZE;
2090 2091
	if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
				entry->key.acl.vlan_id_mask))
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
		return -EMSGSIZE;

	switch (ntohs(entry->key.acl.eth_type)) {
	case ETH_P_IP:
	case ETH_P_IPV6:
		if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_PROTO,
				      entry->key.acl.ip_proto))
			return -EMSGSIZE;
		if (rocker_tlv_put_u8(desc_info,
				      ROCKER_TLV_OF_DPA_IP_PROTO_MASK,
				      entry->key.acl.ip_proto_mask))
			return -EMSGSIZE;
		if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_DSCP,
				      entry->key.acl.ip_tos & 0x3f))
			return -EMSGSIZE;
		if (rocker_tlv_put_u8(desc_info,
				      ROCKER_TLV_OF_DPA_IP_DSCP_MASK,
				      entry->key.acl.ip_tos_mask & 0x3f))
			return -EMSGSIZE;
		if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_ECN,
				      (entry->key.acl.ip_tos & 0xc0) >> 6))
			return -EMSGSIZE;
		if (rocker_tlv_put_u8(desc_info,
				      ROCKER_TLV_OF_DPA_IP_ECN_MASK,
				      (entry->key.acl.ip_tos_mask & 0xc0) >> 6))
			return -EMSGSIZE;
		break;
	}

	if (entry->key.acl.group_id != ROCKER_GROUP_NONE &&
	    rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
			       entry->key.acl.group_id))
		return -EMSGSIZE;

	return 0;
}

2129
static int rocker_cmd_flow_tbl_add(const struct rocker_port *rocker_port,
2130 2131 2132
				   struct rocker_desc_info *desc_info,
				   void *priv)
{
2133
	const struct rocker_flow_tbl_entry *entry = priv;
2134 2135 2136
	struct rocker_tlv *cmd_info;
	int err = 0;

2137
	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
		return -EMSGSIZE;
	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
	if (!cmd_info)
		return -EMSGSIZE;
	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_TABLE_ID,
			       entry->key.tbl_id))
		return -EMSGSIZE;
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_PRIORITY,
			       entry->key.priority))
		return -EMSGSIZE;
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_HARDTIME, 0))
		return -EMSGSIZE;
	if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE,
			       entry->cookie))
		return -EMSGSIZE;

	switch (entry->key.tbl_id) {
	case ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT:
		err = rocker_cmd_flow_tbl_add_ig_port(desc_info, entry);
		break;
	case ROCKER_OF_DPA_TABLE_ID_VLAN:
		err = rocker_cmd_flow_tbl_add_vlan(desc_info, entry);
		break;
	case ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC:
		err = rocker_cmd_flow_tbl_add_term_mac(desc_info, entry);
		break;
	case ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING:
		err = rocker_cmd_flow_tbl_add_ucast_routing(desc_info, entry);
		break;
	case ROCKER_OF_DPA_TABLE_ID_BRIDGING:
		err = rocker_cmd_flow_tbl_add_bridge(desc_info, entry);
		break;
	case ROCKER_OF_DPA_TABLE_ID_ACL_POLICY:
		err = rocker_cmd_flow_tbl_add_acl(desc_info, entry);
		break;
	default:
		err = -ENOTSUPP;
		break;
	}

	if (err)
		return err;

	rocker_tlv_nest_end(desc_info, cmd_info);

	return 0;
}

2186
static int rocker_cmd_flow_tbl_del(const struct rocker_port *rocker_port,
2187 2188 2189 2190 2191 2192
				   struct rocker_desc_info *desc_info,
				   void *priv)
{
	const struct rocker_flow_tbl_entry *entry = priv;
	struct rocker_tlv *cmd_info;

2193
	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209
		return -EMSGSIZE;
	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
	if (!cmd_info)
		return -EMSGSIZE;
	if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE,
			       entry->cookie))
		return -EMSGSIZE;
	rocker_tlv_nest_end(desc_info, cmd_info);

	return 0;
}

static int
rocker_cmd_group_tbl_add_l2_interface(struct rocker_desc_info *desc_info,
				      struct rocker_group_tbl_entry *entry)
{
S
Scott Feldman 已提交
2210
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_OUT_PPORT,
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
			       ROCKER_GROUP_PORT_GET(entry->group_id)))
		return -EMSGSIZE;
	if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_POP_VLAN,
			      entry->l2_interface.pop_vlan))
		return -EMSGSIZE;

	return 0;
}

static int
rocker_cmd_group_tbl_add_l2_rewrite(struct rocker_desc_info *desc_info,
2222
				    const struct rocker_group_tbl_entry *entry)
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
{
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER,
			       entry->l2_rewrite.group_id))
		return -EMSGSIZE;
	if (!is_zero_ether_addr(entry->l2_rewrite.eth_src) &&
	    rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
			   ETH_ALEN, entry->l2_rewrite.eth_src))
		return -EMSGSIZE;
	if (!is_zero_ether_addr(entry->l2_rewrite.eth_dst) &&
	    rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
			   ETH_ALEN, entry->l2_rewrite.eth_dst))
		return -EMSGSIZE;
	if (entry->l2_rewrite.vlan_id &&
2236 2237
	    rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
				entry->l2_rewrite.vlan_id))
2238 2239 2240 2241 2242 2243 2244
		return -EMSGSIZE;

	return 0;
}

static int
rocker_cmd_group_tbl_add_group_ids(struct rocker_desc_info *desc_info,
2245
				   const struct rocker_group_tbl_entry *entry)
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
{
	int i;
	struct rocker_tlv *group_ids;

	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GROUP_COUNT,
			       entry->group_count))
		return -EMSGSIZE;

	group_ids = rocker_tlv_nest_start(desc_info,
					  ROCKER_TLV_OF_DPA_GROUP_IDS);
	if (!group_ids)
		return -EMSGSIZE;

	for (i = 0; i < entry->group_count; i++)
		/* Note TLV array is 1-based */
		if (rocker_tlv_put_u32(desc_info, i + 1, entry->group_ids[i]))
			return -EMSGSIZE;

	rocker_tlv_nest_end(desc_info, group_ids);

	return 0;
}

static int
rocker_cmd_group_tbl_add_l3_unicast(struct rocker_desc_info *desc_info,
2271
				    const struct rocker_group_tbl_entry *entry)
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
{
	if (!is_zero_ether_addr(entry->l3_unicast.eth_src) &&
	    rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
			   ETH_ALEN, entry->l3_unicast.eth_src))
		return -EMSGSIZE;
	if (!is_zero_ether_addr(entry->l3_unicast.eth_dst) &&
	    rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
			   ETH_ALEN, entry->l3_unicast.eth_dst))
		return -EMSGSIZE;
	if (entry->l3_unicast.vlan_id &&
2282 2283
	    rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
				entry->l3_unicast.vlan_id))
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
		return -EMSGSIZE;
	if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_TTL_CHECK,
			      entry->l3_unicast.ttl_check))
		return -EMSGSIZE;
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER,
			       entry->l3_unicast.group_id))
		return -EMSGSIZE;

	return 0;
}

2295
static int rocker_cmd_group_tbl_add(const struct rocker_port *rocker_port,
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
				    struct rocker_desc_info *desc_info,
				    void *priv)
{
	struct rocker_group_tbl_entry *entry = priv;
	struct rocker_tlv *cmd_info;
	int err = 0;

	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
		return -EMSGSIZE;
	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
	if (!cmd_info)
		return -EMSGSIZE;

	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
			       entry->group_id))
		return -EMSGSIZE;

	switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) {
	case ROCKER_OF_DPA_GROUP_TYPE_L2_INTERFACE:
		err = rocker_cmd_group_tbl_add_l2_interface(desc_info, entry);
		break;
	case ROCKER_OF_DPA_GROUP_TYPE_L2_REWRITE:
		err = rocker_cmd_group_tbl_add_l2_rewrite(desc_info, entry);
		break;
	case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD:
	case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST:
		err = rocker_cmd_group_tbl_add_group_ids(desc_info, entry);
		break;
	case ROCKER_OF_DPA_GROUP_TYPE_L3_UCAST:
		err = rocker_cmd_group_tbl_add_l3_unicast(desc_info, entry);
		break;
	default:
		err = -ENOTSUPP;
		break;
	}

	if (err)
		return err;

	rocker_tlv_nest_end(desc_info, cmd_info);

	return 0;
}

2340
static int rocker_cmd_group_tbl_del(const struct rocker_port *rocker_port,
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
				    struct rocker_desc_info *desc_info,
				    void *priv)
{
	const struct rocker_group_tbl_entry *entry = priv;
	struct rocker_tlv *cmd_info;

	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
		return -EMSGSIZE;
	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
	if (!cmd_info)
		return -EMSGSIZE;
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
			       entry->group_id))
		return -EMSGSIZE;
	rocker_tlv_nest_end(desc_info, cmd_info);

	return 0;
}

2360 2361 2362
/***************************************************
 * Flow, group, FDB, internal VLAN and neigh tables
 ***************************************************/
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377

static int rocker_init_tbls(struct rocker *rocker)
{
	hash_init(rocker->flow_tbl);
	spin_lock_init(&rocker->flow_tbl_lock);

	hash_init(rocker->group_tbl);
	spin_lock_init(&rocker->group_tbl_lock);

	hash_init(rocker->fdb_tbl);
	spin_lock_init(&rocker->fdb_tbl_lock);

	hash_init(rocker->internal_vlan_tbl);
	spin_lock_init(&rocker->internal_vlan_tbl_lock);

2378 2379 2380
	hash_init(rocker->neigh_tbl);
	spin_lock_init(&rocker->neigh_tbl_lock);

2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
	return 0;
}

static void rocker_free_tbls(struct rocker *rocker)
{
	unsigned long flags;
	struct rocker_flow_tbl_entry *flow_entry;
	struct rocker_group_tbl_entry *group_entry;
	struct rocker_fdb_tbl_entry *fdb_entry;
	struct rocker_internal_vlan_tbl_entry *internal_vlan_entry;
2391
	struct rocker_neigh_tbl_entry *neigh_entry;
2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414
	struct hlist_node *tmp;
	int bkt;

	spin_lock_irqsave(&rocker->flow_tbl_lock, flags);
	hash_for_each_safe(rocker->flow_tbl, bkt, tmp, flow_entry, entry)
		hash_del(&flow_entry->entry);
	spin_unlock_irqrestore(&rocker->flow_tbl_lock, flags);

	spin_lock_irqsave(&rocker->group_tbl_lock, flags);
	hash_for_each_safe(rocker->group_tbl, bkt, tmp, group_entry, entry)
		hash_del(&group_entry->entry);
	spin_unlock_irqrestore(&rocker->group_tbl_lock, flags);

	spin_lock_irqsave(&rocker->fdb_tbl_lock, flags);
	hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, fdb_entry, entry)
		hash_del(&fdb_entry->entry);
	spin_unlock_irqrestore(&rocker->fdb_tbl_lock, flags);

	spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, flags);
	hash_for_each_safe(rocker->internal_vlan_tbl, bkt,
			   tmp, internal_vlan_entry, entry)
		hash_del(&internal_vlan_entry->entry);
	spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, flags);
2415 2416 2417 2418 2419

	spin_lock_irqsave(&rocker->neigh_tbl_lock, flags);
	hash_for_each_safe(rocker->neigh_tbl, bkt, tmp, neigh_entry, entry)
		hash_del(&neigh_entry->entry);
	spin_unlock_irqrestore(&rocker->neigh_tbl_lock, flags);
2420 2421 2422
}

static struct rocker_flow_tbl_entry *
2423 2424
rocker_flow_tbl_find(const struct rocker *rocker,
		     const struct rocker_flow_tbl_entry *match)
2425 2426
{
	struct rocker_flow_tbl_entry *found;
2427
	size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
2428 2429 2430

	hash_for_each_possible(rocker->flow_tbl, found,
			       entry, match->key_crc32) {
2431
		if (memcmp(&found->key, &match->key, key_len) == 0)
2432 2433 2434 2435 2436 2437 2438
			return found;
	}

	return NULL;
}

static int rocker_flow_tbl_add(struct rocker_port *rocker_port,
2439
			       enum switchdev_trans_ph trans_ph, int flags,
2440
			       struct rocker_flow_tbl_entry *match)
2441 2442 2443
{
	struct rocker *rocker = rocker_port->rocker;
	struct rocker_flow_tbl_entry *found;
2444
	size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
2445
	unsigned long lock_flags;
2446

2447
	match->key_crc32 = crc32(~0, &match->key, key_len);
2448

2449
	spin_lock_irqsave(&rocker->flow_tbl_lock, lock_flags);
2450 2451 2452 2453

	found = rocker_flow_tbl_find(rocker, match);

	if (found) {
2454
		match->cookie = found->cookie;
2455
		if (trans_ph != SWITCHDEV_TRANS_PREPARE)
2456
			hash_del(&found->entry);
2457
		rocker_port_kfree(trans_ph, found);
2458 2459
		found = match;
		found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_MOD;
2460 2461 2462
	} else {
		found = match;
		found->cookie = rocker->flow_tbl_next_cookie++;
2463
		found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_ADD;
2464 2465
	}

2466
	if (trans_ph != SWITCHDEV_TRANS_PREPARE)
2467
		hash_add(rocker->flow_tbl, &found->entry, found->key_crc32);
2468

2469
	spin_unlock_irqrestore(&rocker->flow_tbl_lock, lock_flags);
2470

2471
	return rocker_cmd_exec(rocker_port, trans_ph, flags,
2472
			       rocker_cmd_flow_tbl_add, found, NULL, NULL);
2473 2474 2475
}

static int rocker_flow_tbl_del(struct rocker_port *rocker_port,
2476
			       enum switchdev_trans_ph trans_ph, int flags,
2477
			       struct rocker_flow_tbl_entry *match)
2478 2479 2480
{
	struct rocker *rocker = rocker_port->rocker;
	struct rocker_flow_tbl_entry *found;
2481
	size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
2482
	unsigned long lock_flags;
2483 2484
	int err = 0;

2485
	match->key_crc32 = crc32(~0, &match->key, key_len);
2486

2487
	spin_lock_irqsave(&rocker->flow_tbl_lock, lock_flags);
2488 2489 2490 2491

	found = rocker_flow_tbl_find(rocker, match);

	if (found) {
2492
		if (trans_ph != SWITCHDEV_TRANS_PREPARE)
2493
			hash_del(&found->entry);
2494
		found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_DEL;
2495 2496
	}

2497
	spin_unlock_irqrestore(&rocker->flow_tbl_lock, lock_flags);
2498

2499
	rocker_port_kfree(trans_ph, match);
2500

2501
	if (found) {
2502
		err = rocker_cmd_exec(rocker_port, trans_ph, flags,
2503
				      rocker_cmd_flow_tbl_del,
2504
				      found, NULL, NULL);
2505
		rocker_port_kfree(trans_ph, found);
2506 2507 2508 2509 2510 2511
	}

	return err;
}

static int rocker_flow_tbl_do(struct rocker_port *rocker_port,
2512
			      enum switchdev_trans_ph trans_ph, int flags,
2513
			      struct rocker_flow_tbl_entry *entry)
2514 2515
{
	if (flags & ROCKER_OP_FLAG_REMOVE)
2516
		return rocker_flow_tbl_del(rocker_port, trans_ph, flags, entry);
2517
	else
2518
		return rocker_flow_tbl_add(rocker_port, trans_ph, flags, entry);
2519 2520 2521
}

static int rocker_flow_tbl_ig_port(struct rocker_port *rocker_port,
2522
				   enum switchdev_trans_ph trans_ph, int flags,
2523
				   u32 in_pport, u32 in_pport_mask,
2524 2525 2526 2527
				   enum rocker_of_dpa_table_id goto_tbl)
{
	struct rocker_flow_tbl_entry *entry;

2528
	entry = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*entry));
2529 2530 2531 2532 2533
	if (!entry)
		return -ENOMEM;

	entry->key.priority = ROCKER_PRIORITY_IG_PORT;
	entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT;
S
Scott Feldman 已提交
2534 2535
	entry->key.ig_port.in_pport = in_pport;
	entry->key.ig_port.in_pport_mask = in_pport_mask;
2536 2537
	entry->key.ig_port.goto_tbl = goto_tbl;

2538
	return rocker_flow_tbl_do(rocker_port, trans_ph, flags, entry);
2539 2540 2541
}

static int rocker_flow_tbl_vlan(struct rocker_port *rocker_port,
2542
				enum switchdev_trans_ph trans_ph, int flags,
2543 2544
				u32 in_pport, __be16 vlan_id,
				__be16 vlan_id_mask,
2545 2546 2547 2548 2549
				enum rocker_of_dpa_table_id goto_tbl,
				bool untagged, __be16 new_vlan_id)
{
	struct rocker_flow_tbl_entry *entry;

2550
	entry = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*entry));
2551 2552 2553 2554 2555
	if (!entry)
		return -ENOMEM;

	entry->key.priority = ROCKER_PRIORITY_VLAN;
	entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_VLAN;
S
Scott Feldman 已提交
2556
	entry->key.vlan.in_pport = in_pport;
2557 2558 2559 2560 2561 2562 2563
	entry->key.vlan.vlan_id = vlan_id;
	entry->key.vlan.vlan_id_mask = vlan_id_mask;
	entry->key.vlan.goto_tbl = goto_tbl;

	entry->key.vlan.untagged = untagged;
	entry->key.vlan.new_vlan_id = new_vlan_id;

2564
	return rocker_flow_tbl_do(rocker_port, trans_ph, flags, entry);
2565 2566 2567
}

static int rocker_flow_tbl_term_mac(struct rocker_port *rocker_port,
2568
				    enum switchdev_trans_ph trans_ph,
S
Scott Feldman 已提交
2569
				    u32 in_pport, u32 in_pport_mask,
2570 2571 2572 2573 2574 2575 2576
				    __be16 eth_type, const u8 *eth_dst,
				    const u8 *eth_dst_mask, __be16 vlan_id,
				    __be16 vlan_id_mask, bool copy_to_cpu,
				    int flags)
{
	struct rocker_flow_tbl_entry *entry;

2577
	entry = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*entry));
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591
	if (!entry)
		return -ENOMEM;

	if (is_multicast_ether_addr(eth_dst)) {
		entry->key.priority = ROCKER_PRIORITY_TERM_MAC_MCAST;
		entry->key.term_mac.goto_tbl =
			 ROCKER_OF_DPA_TABLE_ID_MULTICAST_ROUTING;
	} else {
		entry->key.priority = ROCKER_PRIORITY_TERM_MAC_UCAST;
		entry->key.term_mac.goto_tbl =
			 ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING;
	}

	entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC;
S
Scott Feldman 已提交
2592 2593
	entry->key.term_mac.in_pport = in_pport;
	entry->key.term_mac.in_pport_mask = in_pport_mask;
2594 2595 2596 2597 2598 2599 2600
	entry->key.term_mac.eth_type = eth_type;
	ether_addr_copy(entry->key.term_mac.eth_dst, eth_dst);
	ether_addr_copy(entry->key.term_mac.eth_dst_mask, eth_dst_mask);
	entry->key.term_mac.vlan_id = vlan_id;
	entry->key.term_mac.vlan_id_mask = vlan_id_mask;
	entry->key.term_mac.copy_to_cpu = copy_to_cpu;

2601
	return rocker_flow_tbl_do(rocker_port, trans_ph, flags, entry);
2602 2603 2604
}

static int rocker_flow_tbl_bridge(struct rocker_port *rocker_port,
2605
				  enum switchdev_trans_ph trans_ph, int flags,
2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616
				  const u8 *eth_dst, const u8 *eth_dst_mask,
				  __be16 vlan_id, u32 tunnel_id,
				  enum rocker_of_dpa_table_id goto_tbl,
				  u32 group_id, bool copy_to_cpu)
{
	struct rocker_flow_tbl_entry *entry;
	u32 priority;
	bool vlan_bridging = !!vlan_id;
	bool dflt = !eth_dst || (eth_dst && eth_dst_mask);
	bool wild = false;

2617
	entry = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*entry));
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629
	if (!entry)
		return -ENOMEM;

	entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_BRIDGING;

	if (eth_dst) {
		entry->key.bridge.has_eth_dst = 1;
		ether_addr_copy(entry->key.bridge.eth_dst, eth_dst);
	}
	if (eth_dst_mask) {
		entry->key.bridge.has_eth_dst_mask = 1;
		ether_addr_copy(entry->key.bridge.eth_dst_mask, eth_dst_mask);
S
Simon Horman 已提交
2630
		if (!ether_addr_equal(eth_dst_mask, ff_mac))
2631 2632 2633 2634
			wild = true;
	}

	priority = ROCKER_PRIORITY_UNKNOWN;
2635
	if (vlan_bridging && dflt && wild)
2636
		priority = ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_WILD;
2637
	else if (vlan_bridging && dflt && !wild)
2638
		priority = ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_EXACT;
2639
	else if (vlan_bridging && !dflt)
2640
		priority = ROCKER_PRIORITY_BRIDGING_VLAN;
2641
	else if (!vlan_bridging && dflt && wild)
2642
		priority = ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_WILD;
2643
	else if (!vlan_bridging && dflt && !wild)
2644
		priority = ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_EXACT;
2645
	else if (!vlan_bridging && !dflt)
2646 2647 2648 2649 2650 2651 2652 2653 2654
		priority = ROCKER_PRIORITY_BRIDGING_TENANT;

	entry->key.priority = priority;
	entry->key.bridge.vlan_id = vlan_id;
	entry->key.bridge.tunnel_id = tunnel_id;
	entry->key.bridge.goto_tbl = goto_tbl;
	entry->key.bridge.group_id = group_id;
	entry->key.bridge.copy_to_cpu = copy_to_cpu;

2655
	return rocker_flow_tbl_do(rocker_port, trans_ph, flags, entry);
2656 2657
}

2658
static int rocker_flow_tbl_ucast4_routing(struct rocker_port *rocker_port,
2659
					  enum switchdev_trans_ph trans_ph,
2660 2661 2662 2663 2664 2665 2666
					  __be16 eth_type, __be32 dst,
					  __be32 dst_mask, u32 priority,
					  enum rocker_of_dpa_table_id goto_tbl,
					  u32 group_id, int flags)
{
	struct rocker_flow_tbl_entry *entry;

2667
	entry = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*entry));
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
	if (!entry)
		return -ENOMEM;

	entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING;
	entry->key.priority = priority;
	entry->key.ucast_routing.eth_type = eth_type;
	entry->key.ucast_routing.dst4 = dst;
	entry->key.ucast_routing.dst4_mask = dst_mask;
	entry->key.ucast_routing.goto_tbl = goto_tbl;
	entry->key.ucast_routing.group_id = group_id;
	entry->key_len = offsetof(struct rocker_flow_tbl_key,
				  ucast_routing.group_id);

2681
	return rocker_flow_tbl_do(rocker_port, trans_ph, flags, entry);
2682 2683
}

2684
static int rocker_flow_tbl_acl(struct rocker_port *rocker_port,
2685
			       enum switchdev_trans_ph trans_ph, int flags,
2686
			       u32 in_pport, u32 in_pport_mask,
2687 2688
			       const u8 *eth_src, const u8 *eth_src_mask,
			       const u8 *eth_dst, const u8 *eth_dst_mask,
2689 2690 2691
			       __be16 eth_type, __be16 vlan_id,
			       __be16 vlan_id_mask, u8 ip_proto,
			       u8 ip_proto_mask, u8 ip_tos, u8 ip_tos_mask,
2692 2693 2694 2695 2696
			       u32 group_id)
{
	u32 priority;
	struct rocker_flow_tbl_entry *entry;

2697
	entry = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*entry));
2698 2699 2700 2701 2702
	if (!entry)
		return -ENOMEM;

	priority = ROCKER_PRIORITY_ACL_NORMAL;
	if (eth_dst && eth_dst_mask) {
S
Simon Horman 已提交
2703
		if (ether_addr_equal(eth_dst_mask, mcast_mac))
2704 2705 2706 2707 2708 2709 2710
			priority = ROCKER_PRIORITY_ACL_DFLT;
		else if (is_link_local_ether_addr(eth_dst))
			priority = ROCKER_PRIORITY_ACL_CTRL;
	}

	entry->key.priority = priority;
	entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
S
Scott Feldman 已提交
2711 2712
	entry->key.acl.in_pport = in_pport;
	entry->key.acl.in_pport_mask = in_pport_mask;
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731

	if (eth_src)
		ether_addr_copy(entry->key.acl.eth_src, eth_src);
	if (eth_src_mask)
		ether_addr_copy(entry->key.acl.eth_src_mask, eth_src_mask);
	if (eth_dst)
		ether_addr_copy(entry->key.acl.eth_dst, eth_dst);
	if (eth_dst_mask)
		ether_addr_copy(entry->key.acl.eth_dst_mask, eth_dst_mask);

	entry->key.acl.eth_type = eth_type;
	entry->key.acl.vlan_id = vlan_id;
	entry->key.acl.vlan_id_mask = vlan_id_mask;
	entry->key.acl.ip_proto = ip_proto;
	entry->key.acl.ip_proto_mask = ip_proto_mask;
	entry->key.acl.ip_tos = ip_tos;
	entry->key.acl.ip_tos_mask = ip_tos_mask;
	entry->key.acl.group_id = group_id;

2732
	return rocker_flow_tbl_do(rocker_port, trans_ph, flags, entry);
2733 2734 2735
}

static struct rocker_group_tbl_entry *
2736 2737
rocker_group_tbl_find(const struct rocker *rocker,
		      const struct rocker_group_tbl_entry *match)
2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
{
	struct rocker_group_tbl_entry *found;

	hash_for_each_possible(rocker->group_tbl, found,
			       entry, match->group_id) {
		if (found->group_id == match->group_id)
			return found;
	}

	return NULL;
}

2750
static void rocker_group_tbl_entry_free(enum switchdev_trans_ph trans_ph,
2751
					struct rocker_group_tbl_entry *entry)
2752 2753 2754 2755
{
	switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) {
	case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD:
	case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST:
2756
		rocker_port_kfree(trans_ph, entry->group_ids);
2757 2758 2759 2760
		break;
	default:
		break;
	}
2761
	rocker_port_kfree(trans_ph, entry);
2762 2763 2764
}

static int rocker_group_tbl_add(struct rocker_port *rocker_port,
2765
				enum switchdev_trans_ph trans_ph, int flags,
2766
				struct rocker_group_tbl_entry *match)
2767 2768 2769
{
	struct rocker *rocker = rocker_port->rocker;
	struct rocker_group_tbl_entry *found;
2770
	unsigned long lock_flags;
2771

2772
	spin_lock_irqsave(&rocker->group_tbl_lock, lock_flags);
2773 2774 2775 2776

	found = rocker_group_tbl_find(rocker, match);

	if (found) {
2777
		if (trans_ph != SWITCHDEV_TRANS_PREPARE)
2778
			hash_del(&found->entry);
2779
		rocker_group_tbl_entry_free(trans_ph, found);
2780 2781 2782 2783 2784 2785 2786
		found = match;
		found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_MOD;
	} else {
		found = match;
		found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_ADD;
	}

2787
	if (trans_ph != SWITCHDEV_TRANS_PREPARE)
2788
		hash_add(rocker->group_tbl, &found->entry, found->group_id);
2789

2790
	spin_unlock_irqrestore(&rocker->group_tbl_lock, lock_flags);
2791

2792
	return rocker_cmd_exec(rocker_port, trans_ph, flags,
2793
			       rocker_cmd_group_tbl_add, found, NULL, NULL);
2794 2795 2796
}

static int rocker_group_tbl_del(struct rocker_port *rocker_port,
2797
				enum switchdev_trans_ph trans_ph, int flags,
2798
				struct rocker_group_tbl_entry *match)
2799 2800 2801
{
	struct rocker *rocker = rocker_port->rocker;
	struct rocker_group_tbl_entry *found;
2802
	unsigned long lock_flags;
2803 2804
	int err = 0;

2805
	spin_lock_irqsave(&rocker->group_tbl_lock, lock_flags);
2806 2807 2808 2809

	found = rocker_group_tbl_find(rocker, match);

	if (found) {
2810
		if (trans_ph != SWITCHDEV_TRANS_PREPARE)
2811
			hash_del(&found->entry);
2812 2813 2814
		found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_DEL;
	}

2815
	spin_unlock_irqrestore(&rocker->group_tbl_lock, lock_flags);
2816

2817
	rocker_group_tbl_entry_free(trans_ph, match);
2818 2819

	if (found) {
2820
		err = rocker_cmd_exec(rocker_port, trans_ph, flags,
2821
				      rocker_cmd_group_tbl_del,
2822
				      found, NULL, NULL);
2823
		rocker_group_tbl_entry_free(trans_ph, found);
2824 2825 2826 2827 2828 2829
	}

	return err;
}

static int rocker_group_tbl_do(struct rocker_port *rocker_port,
2830
			       enum switchdev_trans_ph trans_ph, int flags,
2831
			       struct rocker_group_tbl_entry *entry)
2832 2833
{
	if (flags & ROCKER_OP_FLAG_REMOVE)
2834
		return rocker_group_tbl_del(rocker_port, trans_ph, flags, entry);
2835
	else
2836
		return rocker_group_tbl_add(rocker_port, trans_ph, flags, entry);
2837 2838 2839
}

static int rocker_group_l2_interface(struct rocker_port *rocker_port,
2840
				     enum switchdev_trans_ph trans_ph, int flags,
2841 2842
				     __be16 vlan_id, u32 out_pport,
				     int pop_vlan)
2843 2844 2845
{
	struct rocker_group_tbl_entry *entry;

2846
	entry = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*entry));
2847 2848 2849
	if (!entry)
		return -ENOMEM;

S
Scott Feldman 已提交
2850
	entry->group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
2851 2852
	entry->l2_interface.pop_vlan = pop_vlan;

2853
	return rocker_group_tbl_do(rocker_port, trans_ph, flags, entry);
2854 2855 2856
}

static int rocker_group_l2_fan_out(struct rocker_port *rocker_port,
2857
				   enum switchdev_trans_ph trans_ph,
2858
				   int flags, u8 group_count,
2859
				   const u32 *group_ids, u32 group_id)
2860 2861 2862
{
	struct rocker_group_tbl_entry *entry;

2863
	entry = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*entry));
2864 2865 2866 2867 2868 2869
	if (!entry)
		return -ENOMEM;

	entry->group_id = group_id;
	entry->group_count = group_count;

2870
	entry->group_ids = rocker_port_kcalloc(rocker_port, trans_ph, flags,
2871
					       group_count, sizeof(u32));
2872
	if (!entry->group_ids) {
2873
		rocker_port_kfree(trans_ph, entry);
2874 2875 2876 2877
		return -ENOMEM;
	}
	memcpy(entry->group_ids, group_ids, group_count * sizeof(u32));

2878
	return rocker_group_tbl_do(rocker_port, trans_ph, flags, entry);
2879 2880 2881
}

static int rocker_group_l2_flood(struct rocker_port *rocker_port,
2882
				 enum switchdev_trans_ph trans_ph, int flags,
2883
				 __be16 vlan_id, u8 group_count,
2884
				 const u32 *group_ids, u32 group_id)
2885
{
2886
	return rocker_group_l2_fan_out(rocker_port, trans_ph, flags,
2887 2888 2889 2890
				       group_count, group_ids,
				       group_id);
}

2891
static int rocker_group_l3_unicast(struct rocker_port *rocker_port,
2892
				   enum switchdev_trans_ph trans_ph, int flags,
2893
				   u32 index, const u8 *src_mac, const u8 *dst_mac,
2894
				   __be16 vlan_id, bool ttl_check, u32 pport)
2895 2896 2897
{
	struct rocker_group_tbl_entry *entry;

2898
	entry = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*entry));
2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
	if (!entry)
		return -ENOMEM;

	entry->group_id = ROCKER_GROUP_L3_UNICAST(index);
	if (src_mac)
		ether_addr_copy(entry->l3_unicast.eth_src, src_mac);
	if (dst_mac)
		ether_addr_copy(entry->l3_unicast.eth_dst, dst_mac);
	entry->l3_unicast.vlan_id = vlan_id;
	entry->l3_unicast.ttl_check = ttl_check;
	entry->l3_unicast.group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, pport);

2911
	return rocker_group_tbl_do(rocker_port, trans_ph, flags, entry);
2912 2913 2914
}

static struct rocker_neigh_tbl_entry *
2915
rocker_neigh_tbl_find(const struct rocker *rocker, __be32 ip_addr)
2916 2917 2918
{
	struct rocker_neigh_tbl_entry *found;

2919 2920
	hash_for_each_possible(rocker->neigh_tbl, found,
			       entry, be32_to_cpu(ip_addr))
2921 2922 2923 2924 2925 2926 2927
		if (found->ip_addr == ip_addr)
			return found;

	return NULL;
}

static void _rocker_neigh_add(struct rocker *rocker,
2928
			      enum switchdev_trans_ph trans_ph,
2929 2930
			      struct rocker_neigh_tbl_entry *entry)
{
2931
	if (trans_ph != SWITCHDEV_TRANS_COMMIT)
2932
		entry->index = rocker->neigh_tbl_next_index++;
2933
	if (trans_ph == SWITCHDEV_TRANS_PREPARE)
2934
		return;
2935
	entry->ref_count++;
2936 2937
	hash_add(rocker->neigh_tbl, &entry->entry,
		 be32_to_cpu(entry->ip_addr));
2938 2939
}

2940
static void _rocker_neigh_del(enum switchdev_trans_ph trans_ph,
2941 2942
			      struct rocker_neigh_tbl_entry *entry)
{
2943
	if (trans_ph == SWITCHDEV_TRANS_PREPARE)
2944
		return;
2945 2946
	if (--entry->ref_count == 0) {
		hash_del(&entry->entry);
2947
		rocker_port_kfree(trans_ph, entry);
2948 2949 2950
	}
}

2951
static void _rocker_neigh_update(struct rocker_neigh_tbl_entry *entry,
2952
				 enum switchdev_trans_ph trans_ph,
2953
				 const u8 *eth_dst, bool ttl_check)
2954 2955 2956 2957
{
	if (eth_dst) {
		ether_addr_copy(entry->eth_dst, eth_dst);
		entry->ttl_check = ttl_check;
2958
	} else if (trans_ph != SWITCHDEV_TRANS_PREPARE) {
2959 2960 2961 2962 2963
		entry->ref_count++;
	}
}

static int rocker_port_ipv4_neigh(struct rocker_port *rocker_port,
2964
				  enum switchdev_trans_ph trans_ph,
2965
				  int flags, __be32 ip_addr, const u8 *eth_dst)
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980
{
	struct rocker *rocker = rocker_port->rocker;
	struct rocker_neigh_tbl_entry *entry;
	struct rocker_neigh_tbl_entry *found;
	unsigned long lock_flags;
	__be16 eth_type = htons(ETH_P_IP);
	enum rocker_of_dpa_table_id goto_tbl =
		ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
	u32 group_id;
	u32 priority = 0;
	bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
	bool updating;
	bool removing;
	int err = 0;

2981
	entry = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*entry));
2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997
	if (!entry)
		return -ENOMEM;

	spin_lock_irqsave(&rocker->neigh_tbl_lock, lock_flags);

	found = rocker_neigh_tbl_find(rocker, ip_addr);

	updating = found && adding;
	removing = found && !adding;
	adding = !found && adding;

	if (adding) {
		entry->ip_addr = ip_addr;
		entry->dev = rocker_port->dev;
		ether_addr_copy(entry->eth_dst, eth_dst);
		entry->ttl_check = true;
2998
		_rocker_neigh_add(rocker, trans_ph, entry);
2999 3000
	} else if (removing) {
		memcpy(entry, found, sizeof(*entry));
3001
		_rocker_neigh_del(trans_ph, found);
3002
	} else if (updating) {
3003
		_rocker_neigh_update(found, trans_ph, eth_dst, true);
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019
		memcpy(entry, found, sizeof(*entry));
	} else {
		err = -ENOENT;
	}

	spin_unlock_irqrestore(&rocker->neigh_tbl_lock, lock_flags);

	if (err)
		goto err_out;

	/* For each active neighbor, we have an L3 unicast group and
	 * a /32 route to the neighbor, which uses the L3 unicast
	 * group.  The L3 unicast group can also be referred to by
	 * other routes' nexthops.
	 */

3020
	err = rocker_group_l3_unicast(rocker_port, trans_ph, flags,
3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035
				      entry->index,
				      rocker_port->dev->dev_addr,
				      entry->eth_dst,
				      rocker_port->internal_vlan_id,
				      entry->ttl_check,
				      rocker_port->pport);
	if (err) {
		netdev_err(rocker_port->dev,
			   "Error (%d) L3 unicast group index %d\n",
			   err, entry->index);
		goto err_out;
	}

	if (adding || removing) {
		group_id = ROCKER_GROUP_L3_UNICAST(entry->index);
3036
		err = rocker_flow_tbl_ucast4_routing(rocker_port, trans_ph,
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049
						     eth_type, ip_addr,
						     inet_make_mask(32),
						     priority, goto_tbl,
						     group_id, flags);

		if (err)
			netdev_err(rocker_port->dev,
				   "Error (%d) /32 unicast route %pI4 group 0x%08x\n",
				   err, &entry->ip_addr, group_id);
	}

err_out:
	if (!adding)
3050
		rocker_port_kfree(trans_ph, entry);
3051 3052 3053 3054 3055

	return err;
}

static int rocker_port_ipv4_resolve(struct rocker_port *rocker_port,
3056
				    enum switchdev_trans_ph trans_ph, __be32 ip_addr)
3057 3058
{
	struct net_device *dev = rocker_port->dev;
3059
	struct neighbour *n = __ipv4_neigh_lookup(dev, (__force u32)ip_addr);
3060 3061
	int err = 0;

Y
Ying Xue 已提交
3062
	if (!n) {
3063
		n = neigh_create(&arp_tbl, &ip_addr, dev);
Y
Ying Xue 已提交
3064 3065 3066
		if (IS_ERR(n))
			return IS_ERR(n);
	}
3067 3068 3069 3070 3071 3072 3073

	/* If the neigh is already resolved, then go ahead and
	 * install the entry, otherwise start the ARP process to
	 * resolve the neigh.
	 */

	if (n->nud_state & NUD_VALID)
3074
		err = rocker_port_ipv4_neigh(rocker_port, trans_ph, 0,
3075
					     ip_addr, n->ha);
3076 3077 3078
	else
		neigh_event_send(n, NULL);

Y
Ying Xue 已提交
3079
	neigh_release(n);
3080 3081 3082
	return err;
}

3083
static int rocker_port_ipv4_nh(struct rocker_port *rocker_port,
3084
			       enum switchdev_trans_ph trans_ph, int flags,
3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096
			       __be32 ip_addr, u32 *index)
{
	struct rocker *rocker = rocker_port->rocker;
	struct rocker_neigh_tbl_entry *entry;
	struct rocker_neigh_tbl_entry *found;
	unsigned long lock_flags;
	bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
	bool updating;
	bool removing;
	bool resolved = true;
	int err = 0;

3097
	entry = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*entry));
3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
	if (!entry)
		return -ENOMEM;

	spin_lock_irqsave(&rocker->neigh_tbl_lock, lock_flags);

	found = rocker_neigh_tbl_find(rocker, ip_addr);
	if (found)
		*index = found->index;

	updating = found && adding;
	removing = found && !adding;
	adding = !found && adding;

	if (adding) {
		entry->ip_addr = ip_addr;
		entry->dev = rocker_port->dev;
3114
		_rocker_neigh_add(rocker, trans_ph, entry);
3115 3116 3117
		*index = entry->index;
		resolved = false;
	} else if (removing) {
3118
		_rocker_neigh_del(trans_ph, found);
3119
	} else if (updating) {
3120
		_rocker_neigh_update(found, trans_ph, NULL, false);
3121 3122 3123 3124 3125 3126 3127 3128
		resolved = !is_zero_ether_addr(found->eth_dst);
	} else {
		err = -ENOENT;
	}

	spin_unlock_irqrestore(&rocker->neigh_tbl_lock, lock_flags);

	if (!adding)
3129
		rocker_port_kfree(trans_ph, entry);
3130 3131 3132 3133 3134 3135 3136

	if (err)
		return err;

	/* Resolved means neigh ip_addr is resolved to neigh mac. */

	if (!resolved)
3137
		err = rocker_port_ipv4_resolve(rocker_port, trans_ph, ip_addr);
3138 3139 3140 3141

	return err;
}

3142
static int rocker_port_vlan_flood_group(struct rocker_port *rocker_port,
3143
					enum switchdev_trans_ph trans_ph,
3144 3145 3146
					int flags, __be16 vlan_id)
{
	struct rocker_port *p;
3147
	const struct rocker *rocker = rocker_port->rocker;
3148
	u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0);
3149
	u32 *group_ids;
3150
	u8 group_count = 0;
3151
	int err = 0;
3152 3153
	int i;

3154
	group_ids = rocker_port_kcalloc(rocker_port, trans_ph, flags,
3155
					rocker->port_count, sizeof(u32));
3156 3157 3158
	if (!group_ids)
		return -ENOMEM;

3159 3160 3161 3162 3163 3164 3165
	/* Adjust the flood group for this VLAN.  The flood group
	 * references an L2 interface group for each port in this
	 * VLAN.
	 */

	for (i = 0; i < rocker->port_count; i++) {
		p = rocker->ports[i];
3166 3167
		if (!p)
			continue;
3168 3169 3170 3171
		if (!rocker_port_is_bridged(p))
			continue;
		if (test_bit(ntohs(vlan_id), p->vlan_bitmap)) {
			group_ids[group_count++] =
S
Scott Feldman 已提交
3172
				ROCKER_GROUP_L2_INTERFACE(vlan_id, p->pport);
3173 3174 3175 3176 3177
		}
	}

	/* If there are no bridged ports in this VLAN, we're done */
	if (group_count == 0)
3178
		goto no_ports_in_vlan;
3179

3180
	err = rocker_group_l2_flood(rocker_port, trans_ph, flags, vlan_id,
3181
				    group_count, group_ids, group_id);
3182 3183 3184 3185
	if (err)
		netdev_err(rocker_port->dev,
			   "Error (%d) port VLAN l2 flood group\n", err);

3186
no_ports_in_vlan:
3187
	rocker_port_kfree(trans_ph, group_ids);
3188 3189 3190 3191
	return err;
}

static int rocker_port_vlan_l2_groups(struct rocker_port *rocker_port,
3192
				      enum switchdev_trans_ph trans_ph, int flags,
3193
				      __be16 vlan_id, bool pop_vlan)
3194
{
3195
	const struct rocker *rocker = rocker_port->rocker;
3196 3197
	struct rocker_port *p;
	bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
S
Scott Feldman 已提交
3198
	u32 out_pport;
3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
	int ref = 0;
	int err;
	int i;

	/* An L2 interface group for this port in this VLAN, but
	 * only when port STP state is LEARNING|FORWARDING.
	 */

	if (rocker_port->stp_state == BR_STATE_LEARNING ||
	    rocker_port->stp_state == BR_STATE_FORWARDING) {
S
Scott Feldman 已提交
3209
		out_pport = rocker_port->pport;
3210
		err = rocker_group_l2_interface(rocker_port, trans_ph, flags,
3211
						vlan_id, out_pport, pop_vlan);
3212 3213
		if (err) {
			netdev_err(rocker_port->dev,
S
Scott Feldman 已提交
3214 3215
				   "Error (%d) port VLAN l2 group for pport %d\n",
				   err, out_pport);
3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226
			return err;
		}
	}

	/* An L2 interface group for this VLAN to CPU port.
	 * Add when first port joins this VLAN and destroy when
	 * last port leaves this VLAN.
	 */

	for (i = 0; i < rocker->port_count; i++) {
		p = rocker->ports[i];
3227
		if (p && test_bit(ntohs(vlan_id), p->vlan_bitmap))
3228 3229 3230 3231 3232 3233
			ref++;
	}

	if ((!adding || ref != 1) && (adding || ref != 0))
		return 0;

S
Scott Feldman 已提交
3234
	out_pport = 0;
3235
	err = rocker_group_l2_interface(rocker_port, trans_ph, flags,
3236
					vlan_id, out_pport, pop_vlan);
3237 3238 3239 3240 3241 3242 3243 3244 3245
	if (err) {
		netdev_err(rocker_port->dev,
			   "Error (%d) port VLAN l2 group for CPU port\n", err);
		return err;
	}

	return 0;
}

3246 3247 3248
static struct rocker_ctrl {
	const u8 *eth_dst;
	const u8 *eth_dst_mask;
3249
	__be16 eth_type;
3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288
	bool acl;
	bool bridge;
	bool term;
	bool copy_to_cpu;
} rocker_ctrls[] = {
	[ROCKER_CTRL_LINK_LOCAL_MCAST] = {
		/* pass link local multicast pkts up to CPU for filtering */
		.eth_dst = ll_mac,
		.eth_dst_mask = ll_mask,
		.acl = true,
	},
	[ROCKER_CTRL_LOCAL_ARP] = {
		/* pass local ARP pkts up to CPU */
		.eth_dst = zero_mac,
		.eth_dst_mask = zero_mac,
		.eth_type = htons(ETH_P_ARP),
		.acl = true,
	},
	[ROCKER_CTRL_IPV4_MCAST] = {
		/* pass IPv4 mcast pkts up to CPU, RFC 1112 */
		.eth_dst = ipv4_mcast,
		.eth_dst_mask = ipv4_mask,
		.eth_type = htons(ETH_P_IP),
		.term  = true,
		.copy_to_cpu = true,
	},
	[ROCKER_CTRL_IPV6_MCAST] = {
		/* pass IPv6 mcast pkts up to CPU, RFC 2464 */
		.eth_dst = ipv6_mcast,
		.eth_dst_mask = ipv6_mask,
		.eth_type = htons(ETH_P_IPV6),
		.term  = true,
		.copy_to_cpu = true,
	},
	[ROCKER_CTRL_DFLT_BRIDGING] = {
		/* flood any pkts on vlan */
		.bridge = true,
		.copy_to_cpu = true,
	},
3289 3290 3291 3292 3293 3294
	[ROCKER_CTRL_DFLT_OVS] = {
		/* pass all pkts up to CPU */
		.eth_dst = zero_mac,
		.eth_dst_mask = zero_mac,
		.acl = true,
	},
3295 3296 3297
};

static int rocker_port_ctrl_vlan_acl(struct rocker_port *rocker_port,
3298
				     enum switchdev_trans_ph trans_ph, int flags,
3299
				     const struct rocker_ctrl *ctrl, __be16 vlan_id)
3300
{
S
Scott Feldman 已提交
3301 3302 3303
	u32 in_pport = rocker_port->pport;
	u32 in_pport_mask = 0xffffffff;
	u32 out_pport = 0;
3304 3305
	const u8 *eth_src = NULL;
	const u8 *eth_src_mask = NULL;
3306 3307 3308 3309 3310
	__be16 vlan_id_mask = htons(0xffff);
	u8 ip_proto = 0;
	u8 ip_proto_mask = 0;
	u8 ip_tos = 0;
	u8 ip_tos_mask = 0;
S
Scott Feldman 已提交
3311
	u32 group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
3312 3313
	int err;

3314
	err = rocker_flow_tbl_acl(rocker_port, trans_ph, flags,
S
Scott Feldman 已提交
3315
				  in_pport, in_pport_mask,
3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329
				  eth_src, eth_src_mask,
				  ctrl->eth_dst, ctrl->eth_dst_mask,
				  ctrl->eth_type,
				  vlan_id, vlan_id_mask,
				  ip_proto, ip_proto_mask,
				  ip_tos, ip_tos_mask,
				  group_id);

	if (err)
		netdev_err(rocker_port->dev, "Error (%d) ctrl ACL\n", err);

	return err;
}

3330
static int rocker_port_ctrl_vlan_bridge(struct rocker_port *rocker_port,
3331
					enum switchdev_trans_ph trans_ph, int flags,
3332
					const struct rocker_ctrl *ctrl,
3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343
					__be16 vlan_id)
{
	enum rocker_of_dpa_table_id goto_tbl =
		ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
	u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0);
	u32 tunnel_id = 0;
	int err;

	if (!rocker_port_is_bridged(rocker_port))
		return 0;

3344
	err = rocker_flow_tbl_bridge(rocker_port, trans_ph, flags,
3345 3346 3347 3348 3349 3350 3351 3352 3353 3354
				     ctrl->eth_dst, ctrl->eth_dst_mask,
				     vlan_id, tunnel_id,
				     goto_tbl, group_id, ctrl->copy_to_cpu);

	if (err)
		netdev_err(rocker_port->dev, "Error (%d) ctrl FLOOD\n", err);

	return err;
}

3355
static int rocker_port_ctrl_vlan_term(struct rocker_port *rocker_port,
3356
				      enum switchdev_trans_ph trans_ph, int flags,
3357
				      const struct rocker_ctrl *ctrl, __be16 vlan_id)
3358
{
S
Scott Feldman 已提交
3359
	u32 in_pport_mask = 0xffffffff;
3360 3361 3362 3363 3364 3365
	__be16 vlan_id_mask = htons(0xffff);
	int err;

	if (ntohs(vlan_id) == 0)
		vlan_id = rocker_port->internal_vlan_id;

3366
	err = rocker_flow_tbl_term_mac(rocker_port, trans_ph,
S
Scott Feldman 已提交
3367
				       rocker_port->pport, in_pport_mask,
3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378
				       ctrl->eth_type, ctrl->eth_dst,
				       ctrl->eth_dst_mask, vlan_id,
				       vlan_id_mask, ctrl->copy_to_cpu,
				       flags);

	if (err)
		netdev_err(rocker_port->dev, "Error (%d) ctrl term\n", err);

	return err;
}

3379
static int rocker_port_ctrl_vlan(struct rocker_port *rocker_port,
3380
				 enum switchdev_trans_ph trans_ph, int flags,
3381
				 const struct rocker_ctrl *ctrl, __be16 vlan_id)
3382 3383
{
	if (ctrl->acl)
3384
		return rocker_port_ctrl_vlan_acl(rocker_port, trans_ph, flags,
3385
						 ctrl, vlan_id);
3386
	if (ctrl->bridge)
3387
		return rocker_port_ctrl_vlan_bridge(rocker_port, trans_ph, flags,
3388
						    ctrl, vlan_id);
3389 3390

	if (ctrl->term)
3391
		return rocker_port_ctrl_vlan_term(rocker_port, trans_ph, flags,
3392 3393 3394 3395 3396 3397
						  ctrl, vlan_id);

	return -EOPNOTSUPP;
}

static int rocker_port_ctrl_vlan_add(struct rocker_port *rocker_port,
3398
				     enum switchdev_trans_ph trans_ph, int flags,
3399
				     __be16 vlan_id)
3400 3401 3402 3403 3404 3405
{
	int err = 0;
	int i;

	for (i = 0; i < ROCKER_CTRL_MAX; i++) {
		if (rocker_port->ctrls[i]) {
3406
			err = rocker_port_ctrl_vlan(rocker_port, trans_ph, flags,
3407 3408 3409 3410 3411 3412 3413 3414 3415
						    &rocker_ctrls[i], vlan_id);
			if (err)
				return err;
		}
	}

	return err;
}

3416
static int rocker_port_ctrl(struct rocker_port *rocker_port,
3417
			    enum switchdev_trans_ph trans_ph, int flags,
3418
			    const struct rocker_ctrl *ctrl)
3419 3420 3421 3422 3423 3424 3425
{
	u16 vid;
	int err = 0;

	for (vid = 1; vid < VLAN_N_VID; vid++) {
		if (!test_bit(vid, rocker_port->vlan_bitmap))
			continue;
3426
		err = rocker_port_ctrl_vlan(rocker_port, trans_ph, flags,
3427 3428 3429 3430 3431 3432 3433 3434
					    ctrl, htons(vid));
		if (err)
			break;
	}

	return err;
}

3435
static int rocker_port_vlan(struct rocker_port *rocker_port,
3436
			    enum switchdev_trans_ph trans_ph, int flags, u16 vid)
3437 3438 3439
{
	enum rocker_of_dpa_table_id goto_tbl =
		ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC;
S
Scott Feldman 已提交
3440
	u32 in_pport = rocker_port->pport;
3441 3442 3443 3444 3445 3446 3447 3448 3449
	__be16 vlan_id = htons(vid);
	__be16 vlan_id_mask = htons(0xffff);
	__be16 internal_vlan_id;
	bool untagged;
	bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
	int err;

	internal_vlan_id = rocker_port_vid_to_vlan(rocker_port, vid, &untagged);

3450 3451
	if (adding && test_bit(ntohs(internal_vlan_id),
			       rocker_port->vlan_bitmap))
3452
			return 0; /* already added */
3453 3454
	else if (!adding && !test_bit(ntohs(internal_vlan_id),
				      rocker_port->vlan_bitmap))
3455 3456
			return 0; /* already removed */

3457 3458
	change_bit(ntohs(internal_vlan_id), rocker_port->vlan_bitmap);

3459
	if (adding) {
3460
		err = rocker_port_ctrl_vlan_add(rocker_port, trans_ph, flags,
3461 3462 3463 3464
						internal_vlan_id);
		if (err) {
			netdev_err(rocker_port->dev,
				   "Error (%d) port ctrl vlan add\n", err);
3465
			goto err_out;
3466 3467 3468
		}
	}

3469
	err = rocker_port_vlan_l2_groups(rocker_port, trans_ph, flags,
3470 3471 3472 3473
					 internal_vlan_id, untagged);
	if (err) {
		netdev_err(rocker_port->dev,
			   "Error (%d) port VLAN l2 groups\n", err);
3474
		goto err_out;
3475 3476
	}

3477
	err = rocker_port_vlan_flood_group(rocker_port, trans_ph, flags,
3478 3479 3480 3481
					   internal_vlan_id);
	if (err) {
		netdev_err(rocker_port->dev,
			   "Error (%d) port VLAN l2 flood group\n", err);
3482
		goto err_out;
3483 3484
	}

3485
	err = rocker_flow_tbl_vlan(rocker_port, trans_ph, flags,
S
Scott Feldman 已提交
3486
				   in_pport, vlan_id, vlan_id_mask,
3487 3488 3489 3490 3491
				   goto_tbl, untagged, internal_vlan_id);
	if (err)
		netdev_err(rocker_port->dev,
			   "Error (%d) port VLAN table\n", err);

3492
err_out:
3493
	if (trans_ph == SWITCHDEV_TRANS_PREPARE)
3494 3495
		change_bit(ntohs(internal_vlan_id), rocker_port->vlan_bitmap);

3496 3497 3498
	return err;
}

3499
static int rocker_port_ig_tbl(struct rocker_port *rocker_port,
3500
			      enum switchdev_trans_ph trans_ph, int flags)
3501 3502
{
	enum rocker_of_dpa_table_id goto_tbl;
S
Scott Feldman 已提交
3503 3504
	u32 in_pport;
	u32 in_pport_mask;
3505 3506 3507 3508 3509 3510
	int err;

	/* Normal Ethernet Frames.  Matches pkts from any local physical
	 * ports.  Goto VLAN tbl.
	 */

S
Scott Feldman 已提交
3511 3512
	in_pport = 0;
	in_pport_mask = 0xffff0000;
3513 3514
	goto_tbl = ROCKER_OF_DPA_TABLE_ID_VLAN;

3515
	err = rocker_flow_tbl_ig_port(rocker_port, trans_ph, flags,
S
Scott Feldman 已提交
3516
				      in_pport, in_pport_mask,
3517 3518 3519 3520 3521 3522 3523 3524
				      goto_tbl);
	if (err)
		netdev_err(rocker_port->dev,
			   "Error (%d) ingress port table entry\n", err);

	return err;
}

3525 3526
struct rocker_fdb_learn_work {
	struct work_struct work;
3527
	struct rocker_port *rocker_port;
3528
	enum switchdev_trans_ph trans_ph;
3529 3530 3531 3532 3533 3534 3535
	int flags;
	u8 addr[ETH_ALEN];
	u16 vid;
};

static void rocker_port_fdb_learn_work(struct work_struct *work)
{
3536
	const struct rocker_fdb_learn_work *lw =
3537 3538 3539
		container_of(work, struct rocker_fdb_learn_work, work);
	bool removing = (lw->flags & ROCKER_OP_FLAG_REMOVE);
	bool learned = (lw->flags & ROCKER_OP_FLAG_LEARNED);
3540
	struct switchdev_notifier_fdb_info info;
3541 3542 3543

	info.addr = lw->addr;
	info.vid = lw->vid;
3544

3545
	if (learned && removing)
3546
		call_switchdev_notifiers(SWITCHDEV_FDB_DEL,
3547
					 lw->rocker_port->dev, &info.info);
3548
	else if (learned && !removing)
3549
		call_switchdev_notifiers(SWITCHDEV_FDB_ADD,
3550
					 lw->rocker_port->dev, &info.info);
3551

3552
	rocker_port_kfree(lw->trans_ph, work);
3553 3554 3555
}

static int rocker_port_fdb_learn(struct rocker_port *rocker_port,
3556
				 enum switchdev_trans_ph trans_ph, int flags,
3557
				 const u8 *addr, __be16 vlan_id)
3558 3559 3560 3561
{
	struct rocker_fdb_learn_work *lw;
	enum rocker_of_dpa_table_id goto_tbl =
		ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
S
Scott Feldman 已提交
3562
	u32 out_pport = rocker_port->pport;
3563 3564
	u32 tunnel_id = 0;
	u32 group_id = ROCKER_GROUP_NONE;
3565
	bool syncing = !!(rocker_port->brport_flags & BR_LEARNING_SYNC);
3566 3567 3568 3569
	bool copy_to_cpu = false;
	int err;

	if (rocker_port_is_bridged(rocker_port))
S
Scott Feldman 已提交
3570
		group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
3571 3572

	if (!(flags & ROCKER_OP_FLAG_REFRESH)) {
3573
		err = rocker_flow_tbl_bridge(rocker_port, trans_ph, flags, addr,
3574
					     NULL, vlan_id, tunnel_id, goto_tbl,
3575 3576 3577 3578 3579
					     group_id, copy_to_cpu);
		if (err)
			return err;
	}

3580 3581 3582
	if (!syncing)
		return 0;

3583 3584 3585
	if (!rocker_port_is_bridged(rocker_port))
		return 0;

3586
	lw = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*lw));
3587 3588 3589 3590 3591
	if (!lw)
		return -ENOMEM;

	INIT_WORK(&lw->work, rocker_port_fdb_learn_work);

3592
	lw->rocker_port = rocker_port;
3593
	lw->trans_ph = trans_ph;
3594 3595 3596 3597
	lw->flags = flags;
	ether_addr_copy(lw->addr, addr);
	lw->vid = rocker_port_vlan_to_vid(rocker_port, vlan_id);

3598 3599
	if (trans_ph == SWITCHDEV_TRANS_PREPARE)
		rocker_port_kfree(trans_ph, lw);
3600 3601
	else
		schedule_work(&lw->work);
3602 3603 3604 3605 3606

	return 0;
}

static struct rocker_fdb_tbl_entry *
3607 3608
rocker_fdb_tbl_find(const struct rocker *rocker,
		    const struct rocker_fdb_tbl_entry *match)
3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619
{
	struct rocker_fdb_tbl_entry *found;

	hash_for_each_possible(rocker->fdb_tbl, found, entry, match->key_crc32)
		if (memcmp(&found->key, &match->key, sizeof(found->key)) == 0)
			return found;

	return NULL;
}

static int rocker_port_fdb(struct rocker_port *rocker_port,
3620
			   enum switchdev_trans_ph trans_ph,
3621 3622 3623 3624 3625 3626 3627 3628 3629
			   const unsigned char *addr,
			   __be16 vlan_id, int flags)
{
	struct rocker *rocker = rocker_port->rocker;
	struct rocker_fdb_tbl_entry *fdb;
	struct rocker_fdb_tbl_entry *found;
	bool removing = (flags & ROCKER_OP_FLAG_REMOVE);
	unsigned long lock_flags;

3630
	fdb = rocker_port_kzalloc(rocker_port, trans_ph, flags, sizeof(*fdb));
3631 3632 3633 3634
	if (!fdb)
		return -ENOMEM;

	fdb->learned = (flags & ROCKER_OP_FLAG_LEARNED);
3635
	fdb->touched = jiffies;
3636
	fdb->key.rocker_port = rocker_port;
3637 3638 3639 3640 3641 3642 3643 3644
	ether_addr_copy(fdb->key.addr, addr);
	fdb->key.vlan_id = vlan_id;
	fdb->key_crc32 = crc32(~0, &fdb->key, sizeof(fdb->key));

	spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);

	found = rocker_fdb_tbl_find(rocker, fdb);

3645 3646 3647
	if (found) {
		found->touched = jiffies;
		if (removing) {
3648 3649
			rocker_port_kfree(trans_ph, fdb);
			if (trans_ph != SWITCHDEV_TRANS_PREPARE)
3650 3651 3652
				hash_del(&found->entry);
		}
	} else if (!removing) {
3653
		if (trans_ph != SWITCHDEV_TRANS_PREPARE)
3654 3655
			hash_add(rocker->fdb_tbl, &fdb->entry,
				 fdb->key_crc32);
3656 3657 3658 3659 3660 3661
	}

	spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);

	/* Check if adding and already exists, or removing and can't find */
	if (!found != !removing) {
3662
		rocker_port_kfree(trans_ph, fdb);
3663 3664 3665 3666 3667 3668
		if (!found && removing)
			return 0;
		/* Refreshing existing to update aging timers */
		flags |= ROCKER_OP_FLAG_REFRESH;
	}

3669
	return rocker_port_fdb_learn(rocker_port, trans_ph, flags, addr, vlan_id);
3670 3671
}

3672
static int rocker_port_fdb_flush(struct rocker_port *rocker_port,
3673
				 enum switchdev_trans_ph trans_ph, int flags)
3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685
{
	struct rocker *rocker = rocker_port->rocker;
	struct rocker_fdb_tbl_entry *found;
	unsigned long lock_flags;
	struct hlist_node *tmp;
	int bkt;
	int err = 0;

	if (rocker_port->stp_state == BR_STATE_LEARNING ||
	    rocker_port->stp_state == BR_STATE_FORWARDING)
		return 0;

3686 3687
	flags |= ROCKER_OP_FLAG_REMOVE;

3688 3689 3690
	spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);

	hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, found, entry) {
3691
		if (found->key.rocker_port != rocker_port)
3692 3693 3694
			continue;
		if (!found->learned)
			continue;
3695
		err = rocker_port_fdb_learn(rocker_port, trans_ph, flags,
3696 3697 3698 3699
					    found->key.addr,
					    found->key.vlan_id);
		if (err)
			goto err_out;
3700
		if (trans_ph != SWITCHDEV_TRANS_PREPARE)
3701
			hash_del(&found->entry);
3702 3703 3704 3705 3706 3707 3708 3709
	}

err_out:
	spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);

	return err;
}

S
Scott Feldman 已提交
3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744
static void rocker_fdb_cleanup(unsigned long data)
{
	struct rocker *rocker = (struct rocker *)data;
	struct rocker_port *rocker_port;
	struct rocker_fdb_tbl_entry *entry;
	struct hlist_node *tmp;
	unsigned long next_timer = jiffies + BR_MIN_AGEING_TIME;
	unsigned long expires;
	unsigned long lock_flags;
	int flags = ROCKER_OP_FLAG_NOWAIT | ROCKER_OP_FLAG_REMOVE |
		    ROCKER_OP_FLAG_LEARNED;
	int bkt;

	spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);

	hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, entry, entry) {
		if (!entry->learned)
			continue;
		rocker_port = entry->key.rocker_port;
		expires = entry->touched + rocker_port->ageing_time;
		if (time_before_eq(expires, jiffies)) {
			rocker_port_fdb_learn(rocker_port, SWITCHDEV_TRANS_NONE,
					      flags, entry->key.addr,
					      entry->key.vlan_id);
			hash_del(&entry->entry);
		} else if (time_before(expires, next_timer)) {
			next_timer = expires;
		}
	}

	spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);

	mod_timer(&rocker->fdb_cleanup_timer, round_jiffies_up(next_timer));
}

3745
static int rocker_port_router_mac(struct rocker_port *rocker_port,
3746
				  enum switchdev_trans_ph trans_ph, int flags,
3747
				  __be16 vlan_id)
3748
{
S
Scott Feldman 已提交
3749
	u32 in_pport_mask = 0xffffffff;
3750 3751 3752 3753 3754 3755 3756 3757 3758 3759
	__be16 eth_type;
	const u8 *dst_mac_mask = ff_mac;
	__be16 vlan_id_mask = htons(0xffff);
	bool copy_to_cpu = false;
	int err;

	if (ntohs(vlan_id) == 0)
		vlan_id = rocker_port->internal_vlan_id;

	eth_type = htons(ETH_P_IP);
3760
	err = rocker_flow_tbl_term_mac(rocker_port, trans_ph,
S
Scott Feldman 已提交
3761
				       rocker_port->pport, in_pport_mask,
3762 3763 3764 3765 3766 3767 3768
				       eth_type, rocker_port->dev->dev_addr,
				       dst_mac_mask, vlan_id, vlan_id_mask,
				       copy_to_cpu, flags);
	if (err)
		return err;

	eth_type = htons(ETH_P_IPV6);
3769
	err = rocker_flow_tbl_term_mac(rocker_port, trans_ph,
S
Scott Feldman 已提交
3770
				       rocker_port->pport, in_pport_mask,
3771 3772 3773 3774 3775 3776 3777
				       eth_type, rocker_port->dev->dev_addr,
				       dst_mac_mask, vlan_id, vlan_id_mask,
				       copy_to_cpu, flags);

	return err;
}

3778
static int rocker_port_fwding(struct rocker_port *rocker_port,
3779
			      enum switchdev_trans_ph trans_ph, int flags)
3780 3781
{
	bool pop_vlan;
S
Scott Feldman 已提交
3782
	u32 out_pport;
3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797
	__be16 vlan_id;
	u16 vid;
	int err;

	/* Port will be forwarding-enabled if its STP state is LEARNING
	 * or FORWARDING.  Traffic from CPU can still egress, regardless of
	 * port STP state.  Use L2 interface group on port VLANs as a way
	 * to toggle port forwarding: if forwarding is disabled, L2
	 * interface group will not exist.
	 */

	if (rocker_port->stp_state != BR_STATE_LEARNING &&
	    rocker_port->stp_state != BR_STATE_FORWARDING)
		flags |= ROCKER_OP_FLAG_REMOVE;

S
Scott Feldman 已提交
3798
	out_pport = rocker_port->pport;
3799 3800 3801 3802 3803
	for (vid = 1; vid < VLAN_N_VID; vid++) {
		if (!test_bit(vid, rocker_port->vlan_bitmap))
			continue;
		vlan_id = htons(vid);
		pop_vlan = rocker_vlan_id_is_internal(vlan_id);
3804
		err = rocker_group_l2_interface(rocker_port, trans_ph, flags,
3805
						vlan_id, out_pport, pop_vlan);
3806 3807
		if (err) {
			netdev_err(rocker_port->dev,
S
Scott Feldman 已提交
3808 3809
				   "Error (%d) port VLAN l2 group for pport %d\n",
				   err, out_pport);
3810 3811 3812 3813 3814 3815 3816
			return err;
		}
	}

	return 0;
}

3817
static int rocker_port_stp_update(struct rocker_port *rocker_port,
3818
				  enum switchdev_trans_ph trans_ph, int flags,
3819
				  u8 state)
3820 3821
{
	bool want[ROCKER_CTRL_MAX] = { 0, };
3822 3823
	bool prev_ctrls[ROCKER_CTRL_MAX];
	u8 prev_state;
3824 3825 3826
	int err;
	int i;

3827
	if (trans_ph == SWITCHDEV_TRANS_PREPARE) {
3828 3829 3830 3831
		memcpy(prev_ctrls, rocker_port->ctrls, sizeof(prev_ctrls));
		prev_state = rocker_port->stp_state;
	}

3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846
	if (rocker_port->stp_state == state)
		return 0;

	rocker_port->stp_state = state;

	switch (state) {
	case BR_STATE_DISABLED:
		/* port is completely disabled */
		break;
	case BR_STATE_LISTENING:
	case BR_STATE_BLOCKING:
		want[ROCKER_CTRL_LINK_LOCAL_MCAST] = true;
		break;
	case BR_STATE_LEARNING:
	case BR_STATE_FORWARDING:
3847 3848
		if (!rocker_port_is_ovsed(rocker_port))
			want[ROCKER_CTRL_LINK_LOCAL_MCAST] = true;
3849 3850 3851 3852
		want[ROCKER_CTRL_IPV4_MCAST] = true;
		want[ROCKER_CTRL_IPV6_MCAST] = true;
		if (rocker_port_is_bridged(rocker_port))
			want[ROCKER_CTRL_DFLT_BRIDGING] = true;
3853 3854
		else if (rocker_port_is_ovsed(rocker_port))
			want[ROCKER_CTRL_DFLT_OVS] = true;
3855 3856 3857 3858 3859 3860 3861
		else
			want[ROCKER_CTRL_LOCAL_ARP] = true;
		break;
	}

	for (i = 0; i < ROCKER_CTRL_MAX; i++) {
		if (want[i] != rocker_port->ctrls[i]) {
3862 3863
			int ctrl_flags = flags |
					 (want[i] ? 0 : ROCKER_OP_FLAG_REMOVE);
3864
			err = rocker_port_ctrl(rocker_port, trans_ph, ctrl_flags,
3865 3866
					       &rocker_ctrls[i]);
			if (err)
3867
				goto err_out;
3868 3869 3870 3871
			rocker_port->ctrls[i] = want[i];
		}
	}

3872
	err = rocker_port_fdb_flush(rocker_port, trans_ph, flags);
3873
	if (err)
3874
		goto err_out;
3875

3876
	err = rocker_port_fwding(rocker_port, trans_ph, flags);
3877 3878

err_out:
3879
	if (trans_ph == SWITCHDEV_TRANS_PREPARE) {
3880 3881 3882 3883 3884
		memcpy(rocker_port->ctrls, prev_ctrls, sizeof(prev_ctrls));
		rocker_port->stp_state = prev_state;
	}

	return err;
3885 3886
}

3887
static int rocker_port_fwd_enable(struct rocker_port *rocker_port,
3888
				  enum switchdev_trans_ph trans_ph, int flags)
3889 3890 3891 3892 3893 3894
{
	if (rocker_port_is_bridged(rocker_port))
		/* bridge STP will enable port */
		return 0;

	/* port is not bridged, so simulate going to FORWARDING state */
3895
	return rocker_port_stp_update(rocker_port, trans_ph, flags,
3896
				      BR_STATE_FORWARDING);
3897 3898
}

3899
static int rocker_port_fwd_disable(struct rocker_port *rocker_port,
3900
				   enum switchdev_trans_ph trans_ph, int flags)
3901 3902 3903 3904 3905 3906
{
	if (rocker_port_is_bridged(rocker_port))
		/* bridge STP will disable port */
		return 0;

	/* port is not bridged, so simulate going to DISABLED state */
3907
	return rocker_port_stp_update(rocker_port, trans_ph, flags,
3908
				      BR_STATE_DISABLED);
3909 3910
}

3911
static struct rocker_internal_vlan_tbl_entry *
3912
rocker_internal_vlan_tbl_find(const struct rocker *rocker, int ifindex)
3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933
{
	struct rocker_internal_vlan_tbl_entry *found;

	hash_for_each_possible(rocker->internal_vlan_tbl, found,
			       entry, ifindex) {
		if (found->ifindex == ifindex)
			return found;
	}

	return NULL;
}

static __be16 rocker_port_internal_vlan_id_get(struct rocker_port *rocker_port,
					       int ifindex)
{
	struct rocker *rocker = rocker_port->rocker;
	struct rocker_internal_vlan_tbl_entry *entry;
	struct rocker_internal_vlan_tbl_entry *found;
	unsigned long lock_flags;
	int i;

3934
	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
3935 3936 3937 3938 3939 3940 3941 3942 3943
	if (!entry)
		return 0;

	entry->ifindex = ifindex;

	spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, lock_flags);

	found = rocker_internal_vlan_tbl_find(rocker, ifindex);
	if (found) {
3944
		kfree(entry);
3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966
		goto found;
	}

	found = entry;
	hash_add(rocker->internal_vlan_tbl, &found->entry, found->ifindex);

	for (i = 0; i < ROCKER_N_INTERNAL_VLANS; i++) {
		if (test_and_set_bit(i, rocker->internal_vlan_bitmap))
			continue;
		found->vlan_id = htons(ROCKER_INTERNAL_VLAN_ID_BASE + i);
		goto found;
	}

	netdev_err(rocker_port->dev, "Out of internal VLAN IDs\n");

found:
	found->ref_count++;
	spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, lock_flags);

	return found->vlan_id;
}

3967 3968 3969
static void
rocker_port_internal_vlan_id_put(const struct rocker_port *rocker_port,
				 int ifindex)
3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989
{
	struct rocker *rocker = rocker_port->rocker;
	struct rocker_internal_vlan_tbl_entry *found;
	unsigned long lock_flags;
	unsigned long bit;

	spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, lock_flags);

	found = rocker_internal_vlan_tbl_find(rocker, ifindex);
	if (!found) {
		netdev_err(rocker_port->dev,
			   "ifindex (%d) not found in internal VLAN tbl\n",
			   ifindex);
		goto not_found;
	}

	if (--found->ref_count <= 0) {
		bit = ntohs(found->vlan_id) - ROCKER_INTERNAL_VLAN_ID_BASE;
		clear_bit(bit, rocker->internal_vlan_bitmap);
		hash_del(&found->entry);
3990
		kfree(found);
3991 3992 3993 3994 3995 3996
	}

not_found:
	spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, lock_flags);
}

3997
static int rocker_port_fib_ipv4(struct rocker_port *rocker_port,
3998
				enum switchdev_trans_ph trans_ph, __be32 dst,
3999 4000
				int dst_len, const struct fib_info *fi,
				u32 tb_id, int flags)
4001
{
4002
	const struct fib_nh *nh;
4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021
	__be16 eth_type = htons(ETH_P_IP);
	__be32 dst_mask = inet_make_mask(dst_len);
	__be16 internal_vlan_id = rocker_port->internal_vlan_id;
	u32 priority = fi->fib_priority;
	enum rocker_of_dpa_table_id goto_tbl =
		ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
	u32 group_id;
	bool nh_on_port;
	bool has_gw;
	u32 index;
	int err;

	/* XXX support ECMP */

	nh = fi->fib_nh;
	nh_on_port = (fi->fib_dev == rocker_port->dev);
	has_gw = !!nh->nh_gw;

	if (has_gw && nh_on_port) {
4022
		err = rocker_port_ipv4_nh(rocker_port, trans_ph, flags,
4023 4024 4025 4026 4027 4028 4029 4030 4031 4032
					  nh->nh_gw, &index);
		if (err)
			return err;

		group_id = ROCKER_GROUP_L3_UNICAST(index);
	} else {
		/* Send to CPU for processing */
		group_id = ROCKER_GROUP_L2_INTERFACE(internal_vlan_id, 0);
	}

4033
	err = rocker_flow_tbl_ucast4_routing(rocker_port, trans_ph, eth_type, dst,
4034 4035 4036 4037 4038 4039 4040 4041 4042
					     dst_mask, priority, goto_tbl,
					     group_id, flags);
	if (err)
		netdev_err(rocker_port->dev, "Error (%d) IPv4 route %pI4\n",
			   err, &dst);

	return err;
}

4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071
/*****************
 * Net device ops
 *****************/

static int rocker_port_open(struct net_device *dev)
{
	struct rocker_port *rocker_port = netdev_priv(dev);
	int err;

	err = rocker_port_dma_rings_init(rocker_port);
	if (err)
		return err;

	err = request_irq(rocker_msix_tx_vector(rocker_port),
			  rocker_tx_irq_handler, 0,
			  rocker_driver_name, rocker_port);
	if (err) {
		netdev_err(rocker_port->dev, "cannot assign tx irq\n");
		goto err_request_tx_irq;
	}

	err = request_irq(rocker_msix_rx_vector(rocker_port),
			  rocker_rx_irq_handler, 0,
			  rocker_driver_name, rocker_port);
	if (err) {
		netdev_err(rocker_port->dev, "cannot assign rx irq\n");
		goto err_request_rx_irq;
	}

4072
	err = rocker_port_fwd_enable(rocker_port, SWITCHDEV_TRANS_NONE, 0);
4073
	if (err)
4074
		goto err_fwd_enable;
4075

4076 4077
	napi_enable(&rocker_port->napi_tx);
	napi_enable(&rocker_port->napi_rx);
4078 4079
	if (!dev->proto_down)
		rocker_port_set_enable(rocker_port, true);
4080 4081 4082
	netif_start_queue(dev);
	return 0;

4083
err_fwd_enable:
4084
	free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099
err_request_rx_irq:
	free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
err_request_tx_irq:
	rocker_port_dma_rings_fini(rocker_port);
	return err;
}

static int rocker_port_stop(struct net_device *dev)
{
	struct rocker_port *rocker_port = netdev_priv(dev);

	netif_stop_queue(dev);
	rocker_port_set_enable(rocker_port, false);
	napi_disable(&rocker_port->napi_rx);
	napi_disable(&rocker_port->napi_tx);
4100 4101
	rocker_port_fwd_disable(rocker_port, SWITCHDEV_TRANS_NONE,
				ROCKER_OP_FLAG_NOWAIT);
4102 4103 4104 4105 4106 4107 4108
	free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
	free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
	rocker_port_dma_rings_fini(rocker_port);

	return 0;
}

4109 4110
static void rocker_tx_desc_frags_unmap(const struct rocker_port *rocker_port,
				       const struct rocker_desc_info *desc_info)
4111
{
4112
	const struct rocker *rocker = rocker_port->rocker;
4113
	struct pci_dev *pdev = rocker->pdev;
4114
	const struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1];
4115 4116 4117 4118 4119 4120 4121
	struct rocker_tlv *attr;
	int rem;

	rocker_tlv_parse_desc(attrs, ROCKER_TLV_TX_MAX, desc_info);
	if (!attrs[ROCKER_TLV_TX_FRAGS])
		return;
	rocker_tlv_for_each_nested(attr, attrs[ROCKER_TLV_TX_FRAGS], rem) {
4122
		const struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1];
4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138
		dma_addr_t dma_handle;
		size_t len;

		if (rocker_tlv_type(attr) != ROCKER_TLV_TX_FRAG)
			continue;
		rocker_tlv_parse_nested(frag_attrs, ROCKER_TLV_TX_FRAG_ATTR_MAX,
					attr);
		if (!frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR] ||
		    !frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN])
			continue;
		dma_handle = rocker_tlv_get_u64(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR]);
		len = rocker_tlv_get_u16(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]);
		pci_unmap_single(pdev, dma_handle, len, DMA_TO_DEVICE);
	}
}

4139
static int rocker_tx_desc_frag_map_put(const struct rocker_port *rocker_port,
4140 4141 4142
				       struct rocker_desc_info *desc_info,
				       char *buf, size_t buf_len)
{
4143
	const struct rocker *rocker = rocker_port->rocker;
4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197
	struct pci_dev *pdev = rocker->pdev;
	dma_addr_t dma_handle;
	struct rocker_tlv *frag;

	dma_handle = pci_map_single(pdev, buf, buf_len, DMA_TO_DEVICE);
	if (unlikely(pci_dma_mapping_error(pdev, dma_handle))) {
		if (net_ratelimit())
			netdev_err(rocker_port->dev, "failed to dma map tx frag\n");
		return -EIO;
	}
	frag = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAG);
	if (!frag)
		goto unmap_frag;
	if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_TX_FRAG_ATTR_ADDR,
			       dma_handle))
		goto nest_cancel;
	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_TX_FRAG_ATTR_LEN,
			       buf_len))
		goto nest_cancel;
	rocker_tlv_nest_end(desc_info, frag);
	return 0;

nest_cancel:
	rocker_tlv_nest_cancel(desc_info, frag);
unmap_frag:
	pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE);
	return -EMSGSIZE;
}

static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct rocker_port *rocker_port = netdev_priv(dev);
	struct rocker *rocker = rocker_port->rocker;
	struct rocker_desc_info *desc_info;
	struct rocker_tlv *frags;
	int i;
	int err;

	desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
	if (unlikely(!desc_info)) {
		if (net_ratelimit())
			netdev_err(dev, "tx ring full when queue awake\n");
		return NETDEV_TX_BUSY;
	}

	rocker_desc_cookie_ptr_set(desc_info, skb);

	frags = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAGS);
	if (!frags)
		goto out;
	err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
					  skb->data, skb_headlen(skb));
	if (err)
		goto nest_cancel;
4198 4199 4200 4201 4202
	if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX) {
		err = skb_linearize(skb);
		if (err)
			goto unmap_frags;
	}
4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229

	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];

		err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
						  skb_frag_address(frag),
						  skb_frag_size(frag));
		if (err)
			goto unmap_frags;
	}
	rocker_tlv_nest_end(desc_info, frags);

	rocker_desc_gen_clear(desc_info);
	rocker_desc_head_set(rocker, &rocker_port->tx_ring, desc_info);

	desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
	if (!desc_info)
		netif_stop_queue(dev);

	return NETDEV_TX_OK;

unmap_frags:
	rocker_tx_desc_frags_unmap(rocker_port, desc_info);
nest_cancel:
	rocker_tlv_nest_cancel(desc_info, frags);
out:
	dev_kfree_skb(skb);
4230 4231
	dev->stats.tx_dropped++;

4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250
	return NETDEV_TX_OK;
}

static int rocker_port_set_mac_address(struct net_device *dev, void *p)
{
	struct sockaddr *addr = p;
	struct rocker_port *rocker_port = netdev_priv(dev);
	int err;

	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;

	err = rocker_cmd_set_port_settings_macaddr(rocker_port, addr->sa_data);
	if (err)
		return err;
	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
	return 0;
}

S
Scott Feldman 已提交
4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278
static int rocker_port_change_mtu(struct net_device *dev, int new_mtu)
{
	struct rocker_port *rocker_port = netdev_priv(dev);
	int running = netif_running(dev);
	int err;

#define ROCKER_PORT_MIN_MTU	68
#define ROCKER_PORT_MAX_MTU	9000

	if (new_mtu < ROCKER_PORT_MIN_MTU || new_mtu > ROCKER_PORT_MAX_MTU)
		return -EINVAL;

	if (running)
		rocker_port_stop(dev);

	netdev_info(dev, "MTU change from %d to %d\n", dev->mtu, new_mtu);
	dev->mtu = new_mtu;

	err = rocker_cmd_set_port_settings_mtu(rocker_port, new_mtu);
	if (err)
		return err;

	if (running)
		err = rocker_port_open(dev);

	return err;
}

4279 4280 4281 4282 4283 4284 4285
static int rocker_port_get_phys_port_name(struct net_device *dev,
					  char *buf, size_t len)
{
	struct rocker_port *rocker_port = netdev_priv(dev);
	struct port_name name = { .buf = buf, .len = len };
	int err;

4286
	err = rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
4287 4288
			      rocker_cmd_get_port_settings_prep, NULL,
			      rocker_cmd_get_port_settings_phys_name_proc,
4289
			      &name);
4290 4291 4292 4293

	return err ? -EOPNOTSUPP : 0;
}

4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304
static int rocker_port_change_proto_down(struct net_device *dev,
					 bool proto_down)
{
	struct rocker_port *rocker_port = netdev_priv(dev);

	if (rocker_port->dev->flags & IFF_UP)
		rocker_port_set_enable(rocker_port, !proto_down);
	rocker_port->dev->proto_down = proto_down;
	return 0;
}

4305 4306 4307 4308 4309 4310 4311 4312 4313 4314
static void rocker_port_neigh_destroy(struct neighbour *n)
{
	struct rocker_port *rocker_port = netdev_priv(n->dev);
	int flags = ROCKER_OP_FLAG_REMOVE | ROCKER_OP_FLAG_NOWAIT;
	__be32 ip_addr = *(__be32 *)n->primary_key;

	rocker_port_ipv4_neigh(rocker_port, SWITCHDEV_TRANS_NONE,
			       flags, ip_addr, n->ha);
}

S
Scott Feldman 已提交
4315 4316 4317 4318 4319
static const struct net_device_ops rocker_port_netdev_ops = {
	.ndo_open			= rocker_port_open,
	.ndo_stop			= rocker_port_stop,
	.ndo_start_xmit			= rocker_port_xmit,
	.ndo_set_mac_address		= rocker_port_set_mac_address,
S
Scott Feldman 已提交
4320
	.ndo_change_mtu			= rocker_port_change_mtu,
4321
	.ndo_bridge_getlink		= switchdev_port_bridge_getlink,
4322
	.ndo_bridge_setlink		= switchdev_port_bridge_setlink,
4323
	.ndo_bridge_dellink		= switchdev_port_bridge_dellink,
4324 4325 4326
	.ndo_fdb_add			= switchdev_port_fdb_add,
	.ndo_fdb_del			= switchdev_port_fdb_del,
	.ndo_fdb_dump			= switchdev_port_fdb_dump,
4327
	.ndo_get_phys_port_name		= rocker_port_get_phys_port_name,
4328
	.ndo_change_proto_down		= rocker_port_change_proto_down,
4329
	.ndo_neigh_destroy		= rocker_port_neigh_destroy,
S
Scott Feldman 已提交
4330 4331 4332 4333 4334 4335
};

/********************
 * swdev interface
 ********************/

4336 4337
static int rocker_port_attr_get(struct net_device *dev,
				struct switchdev_attr *attr)
4338
{
4339 4340
	const struct rocker_port *rocker_port = netdev_priv(dev);
	const struct rocker *rocker = rocker_port->rocker;
4341

4342 4343
	switch (attr->id) {
	case SWITCHDEV_ATTR_PORT_PARENT_ID:
4344 4345
		attr->u.ppid.id_len = sizeof(rocker->hw.id);
		memcpy(&attr->u.ppid.id, &rocker->hw.id, attr->u.ppid.id_len);
4346
		break;
4347
	case SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS:
4348
		attr->u.brport_flags = rocker_port->brport_flags;
4349
		break;
4350 4351 4352 4353
	default:
		return -EOPNOTSUPP;
	}

4354 4355 4356
	return 0;
}

4357
static void rocker_port_trans_abort(const struct rocker_port *rocker_port)
4358 4359 4360 4361 4362 4363 4364 4365 4366
{
	struct list_head *mem, *tmp;

	list_for_each_safe(mem, tmp, &rocker_port->trans_mem) {
		list_del(mem);
		kfree(mem);
	}
}

4367
static int rocker_port_brport_flags_set(struct rocker_port *rocker_port,
4368
					enum switchdev_trans_ph trans_ph,
4369 4370 4371 4372 4373 4374 4375 4376
					unsigned long brport_flags)
{
	unsigned long orig_flags;
	int err = 0;

	orig_flags = rocker_port->brport_flags;
	rocker_port->brport_flags = brport_flags;
	if ((orig_flags ^ rocker_port->brport_flags) & BR_LEARNING)
4377
		err = rocker_port_set_learning(rocker_port, trans_ph);
4378

4379
	if (trans_ph == SWITCHDEV_TRANS_PREPARE)
4380 4381 4382 4383 4384
		rocker_port->brport_flags = orig_flags;

	return err;
}

4385
static int rocker_port_attr_set(struct net_device *dev,
4386 4387
				struct switchdev_attr *attr,
				struct switchdev_trans *trans)
4388 4389 4390 4391
{
	struct rocker_port *rocker_port = netdev_priv(dev);
	int err = 0;

4392
	switch (attr->trans_ph) {
4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403
	case SWITCHDEV_TRANS_PREPARE:
		BUG_ON(!list_empty(&rocker_port->trans_mem));
		break;
	case SWITCHDEV_TRANS_ABORT:
		rocker_port_trans_abort(rocker_port);
		return 0;
	default:
		break;
	}

	switch (attr->id) {
4404
	case SWITCHDEV_ATTR_PORT_STP_STATE:
4405
		err = rocker_port_stp_update(rocker_port, attr->trans_ph,
4406
					     ROCKER_OP_FLAG_NOWAIT,
4407
					     attr->u.stp_state);
4408
		break;
4409
	case SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS:
4410
		err = rocker_port_brport_flags_set(rocker_port, attr->trans_ph,
4411
						   attr->u.brport_flags);
4412
		break;
4413 4414 4415 4416 4417 4418
	default:
		err = -EOPNOTSUPP;
		break;
	}

	return err;
4419 4420
}

4421
static int rocker_port_vlan_add(struct rocker_port *rocker_port,
4422
				enum switchdev_trans_ph trans_ph, u16 vid, u16 flags)
4423 4424 4425 4426 4427
{
	int err;

	/* XXX deal with flags for PVID and untagged */

4428
	err = rocker_port_vlan(rocker_port, trans_ph, 0, vid);
4429 4430 4431
	if (err)
		return err;

4432
	err = rocker_port_router_mac(rocker_port, trans_ph, 0, htons(vid));
4433
	if (err)
4434
		rocker_port_vlan(rocker_port, trans_ph,
4435 4436 4437
				 ROCKER_OP_FLAG_REMOVE, vid);

	return err;
4438 4439 4440
}

static int rocker_port_vlans_add(struct rocker_port *rocker_port,
4441
				 enum switchdev_trans_ph trans_ph,
4442
				 const struct switchdev_obj_vlan *vlan)
4443 4444 4445 4446
{
	u16 vid;
	int err;

4447
	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
4448
		err = rocker_port_vlan_add(rocker_port, trans_ph,
4449 4450 4451 4452 4453 4454 4455 4456
					   vid, vlan->flags);
		if (err)
			return err;
	}

	return 0;
}

4457
static int rocker_port_fdb_add(struct rocker_port *rocker_port,
4458
			       enum switchdev_trans_ph trans_ph,
4459
			       const struct switchdev_obj_fdb *fdb)
4460 4461 4462 4463 4464 4465 4466
{
	__be16 vlan_id = rocker_port_vid_to_vlan(rocker_port, fdb->vid, NULL);
	int flags = 0;

	if (!rocker_port_is_bridged(rocker_port))
		return -EINVAL;

4467
	return rocker_port_fdb(rocker_port, trans_ph, fdb->addr, vlan_id, flags);
4468 4469
}

4470
static int rocker_port_obj_add(struct net_device *dev,
4471 4472
			       struct switchdev_obj *obj,
			       struct switchdev_trans *trans)
4473 4474
{
	struct rocker_port *rocker_port = netdev_priv(dev);
4475
	const struct switchdev_obj_ipv4_fib *fib4;
4476 4477
	int err = 0;

4478
	switch (obj->trans_ph) {
4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490
	case SWITCHDEV_TRANS_PREPARE:
		BUG_ON(!list_empty(&rocker_port->trans_mem));
		break;
	case SWITCHDEV_TRANS_ABORT:
		rocker_port_trans_abort(rocker_port);
		return 0;
	default:
		break;
	}

	switch (obj->id) {
	case SWITCHDEV_OBJ_PORT_VLAN:
4491
		err = rocker_port_vlans_add(rocker_port, obj->trans_ph,
4492
					    &obj->u.vlan);
4493
		break;
4494
	case SWITCHDEV_OBJ_IPV4_FIB:
4495
		fib4 = &obj->u.ipv4_fib;
4496
		err = rocker_port_fib_ipv4(rocker_port, obj->trans_ph,
4497
					   htonl(fib4->dst), fib4->dst_len,
4498 4499
					   fib4->fi, fib4->tb_id, 0);
		break;
4500
	case SWITCHDEV_OBJ_PORT_FDB:
4501
		err = rocker_port_fdb_add(rocker_port, obj->trans_ph, &obj->u.fdb);
4502
		break;
4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525
	default:
		err = -EOPNOTSUPP;
		break;
	}

	return err;
}

static int rocker_port_vlan_del(struct rocker_port *rocker_port,
				u16 vid, u16 flags)
{
	int err;

	err = rocker_port_router_mac(rocker_port, SWITCHDEV_TRANS_NONE,
				     ROCKER_OP_FLAG_REMOVE, htons(vid));
	if (err)
		return err;

	return rocker_port_vlan(rocker_port, SWITCHDEV_TRANS_NONE,
				ROCKER_OP_FLAG_REMOVE, vid);
}

static int rocker_port_vlans_del(struct rocker_port *rocker_port,
4526
				 const struct switchdev_obj_vlan *vlan)
4527 4528 4529 4530
{
	u16 vid;
	int err;

4531
	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
4532 4533 4534 4535 4536 4537 4538 4539
		err = rocker_port_vlan_del(rocker_port, vid, vlan->flags);
		if (err)
			return err;
	}

	return 0;
}

4540
static int rocker_port_fdb_del(struct rocker_port *rocker_port,
4541
			       enum switchdev_trans_ph trans_ph,
4542
			       const struct switchdev_obj_fdb *fdb)
4543 4544
{
	__be16 vlan_id = rocker_port_vid_to_vlan(rocker_port, fdb->vid, NULL);
4545
	int flags = ROCKER_OP_FLAG_NOWAIT | ROCKER_OP_FLAG_REMOVE;
4546 4547 4548 4549

	if (!rocker_port_is_bridged(rocker_port))
		return -EINVAL;

4550
	return rocker_port_fdb(rocker_port, trans_ph, fdb->addr, vlan_id, flags);
4551 4552
}

4553 4554 4555 4556
static int rocker_port_obj_del(struct net_device *dev,
			       struct switchdev_obj *obj)
{
	struct rocker_port *rocker_port = netdev_priv(dev);
4557
	const struct switchdev_obj_ipv4_fib *fib4;
4558 4559 4560 4561
	int err = 0;

	switch (obj->id) {
	case SWITCHDEV_OBJ_PORT_VLAN:
4562
		err = rocker_port_vlans_del(rocker_port, &obj->u.vlan);
4563
		break;
4564
	case SWITCHDEV_OBJ_IPV4_FIB:
4565
		fib4 = &obj->u.ipv4_fib;
4566
		err = rocker_port_fib_ipv4(rocker_port, SWITCHDEV_TRANS_NONE,
4567 4568 4569
					   htonl(fib4->dst), fib4->dst_len,
					   fib4->fi, fib4->tb_id,
					   ROCKER_OP_FLAG_REMOVE);
4570
		break;
4571
	case SWITCHDEV_OBJ_PORT_FDB:
4572
		err = rocker_port_fdb_del(rocker_port, obj->trans_ph, &obj->u.fdb);
4573 4574 4575 4576 4577 4578 4579 4580 4581
		break;
	default:
		err = -EOPNOTSUPP;
		break;
	}

	return err;
}

4582
static int rocker_port_fdb_dump(const struct rocker_port *rocker_port,
4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594
				struct switchdev_obj *obj)
{
	struct rocker *rocker = rocker_port->rocker;
	struct switchdev_obj_fdb *fdb = &obj->u.fdb;
	struct rocker_fdb_tbl_entry *found;
	struct hlist_node *tmp;
	unsigned long lock_flags;
	int bkt;
	int err = 0;

	spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);
	hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, found, entry) {
4595
		if (found->key.rocker_port != rocker_port)
4596
			continue;
4597
		fdb->addr = found->key.addr;
4598
		fdb->ndm_state = NUD_REACHABLE;
4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609
		fdb->vid = rocker_port_vlan_to_vid(rocker_port,
						   found->key.vlan_id);
		err = obj->cb(rocker_port->dev, obj);
		if (err)
			break;
	}
	spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);

	return err;
}

4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631
static int rocker_port_vlan_dump(const struct rocker_port *rocker_port,
				 struct switchdev_obj *obj)
{
	struct switchdev_obj_vlan *vlan = &obj->u.vlan;
	u16 vid;
	int err = 0;

	for (vid = 1; vid < VLAN_N_VID; vid++) {
		if (!test_bit(vid, rocker_port->vlan_bitmap))
			continue;
		vlan->flags = 0;
		if (rocker_vlan_id_is_internal(htons(vid)))
			vlan->flags |= BRIDGE_VLAN_INFO_PVID;
		vlan->vid_begin = vlan->vid_end = vid;
		err = obj->cb(rocker_port->dev, obj);
		if (err)
			break;
	}

	return err;
}

4632 4633 4634
static int rocker_port_obj_dump(struct net_device *dev,
				struct switchdev_obj *obj)
{
4635
	const struct rocker_port *rocker_port = netdev_priv(dev);
4636 4637 4638 4639 4640 4641
	int err = 0;

	switch (obj->id) {
	case SWITCHDEV_OBJ_PORT_FDB:
		err = rocker_port_fdb_dump(rocker_port, obj);
		break;
4642 4643 4644
	case SWITCHDEV_OBJ_PORT_VLAN:
		err = rocker_port_vlan_dump(rocker_port, obj);
		break;
4645 4646 4647 4648 4649 4650 4651 4652
	default:
		err = -EOPNOTSUPP;
		break;
	}

	return err;
}

J
Jiri Pirko 已提交
4653
static const struct switchdev_ops rocker_port_switchdev_ops = {
4654
	.switchdev_port_attr_get	= rocker_port_attr_get,
4655
	.switchdev_port_attr_set	= rocker_port_attr_set,
4656 4657
	.switchdev_port_obj_add		= rocker_port_obj_add,
	.switchdev_port_obj_del		= rocker_port_obj_del,
4658
	.switchdev_port_obj_dump	= rocker_port_obj_dump,
4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687
};

/********************
 * ethtool interface
 ********************/

static int rocker_port_get_settings(struct net_device *dev,
				    struct ethtool_cmd *ecmd)
{
	struct rocker_port *rocker_port = netdev_priv(dev);

	return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd);
}

static int rocker_port_set_settings(struct net_device *dev,
				    struct ethtool_cmd *ecmd)
{
	struct rocker_port *rocker_port = netdev_priv(dev);

	return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd);
}

static void rocker_port_get_drvinfo(struct net_device *dev,
				    struct ethtool_drvinfo *drvinfo)
{
	strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver));
	strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
}

4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721
static struct rocker_port_stats {
	char str[ETH_GSTRING_LEN];
	int type;
} rocker_port_stats[] = {
	{ "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS,    },
	{ "rx_bytes",   ROCKER_TLV_CMD_PORT_STATS_RX_BYTES,   },
	{ "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, },
	{ "rx_errors",  ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS,  },

	{ "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS,    },
	{ "tx_bytes",   ROCKER_TLV_CMD_PORT_STATS_TX_BYTES,   },
	{ "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, },
	{ "tx_errors",  ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS,  },
};

#define ROCKER_PORT_STATS_LEN  ARRAY_SIZE(rocker_port_stats)

static void rocker_port_get_strings(struct net_device *netdev, u32 stringset,
				    u8 *data)
{
	u8 *p = data;
	int i;

	switch (stringset) {
	case ETH_SS_STATS:
		for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
			memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN);
			p += ETH_GSTRING_LEN;
		}
		break;
	}
}

static int
4722
rocker_cmd_get_port_stats_prep(const struct rocker_port *rocker_port,
4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735
			       struct rocker_desc_info *desc_info,
			       void *priv)
{
	struct rocker_tlv *cmd_stats;

	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
			       ROCKER_TLV_CMD_TYPE_GET_PORT_STATS))
		return -EMSGSIZE;

	cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
	if (!cmd_stats)
		return -EMSGSIZE;

S
Scott Feldman 已提交
4736 4737
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT,
			       rocker_port->pport))
4738 4739 4740 4741 4742 4743 4744 4745
		return -EMSGSIZE;

	rocker_tlv_nest_end(desc_info, cmd_stats);

	return 0;
}

static int
4746
rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port *rocker_port,
4747
				       const struct rocker_desc_info *desc_info,
4748 4749
				       void *priv)
{
4750 4751 4752
	const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
	const struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1];
	const struct rocker_tlv *pattr;
S
Scott Feldman 已提交
4753
	u32 pport;
4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764
	u64 *data = priv;
	int i;

	rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);

	if (!attrs[ROCKER_TLV_CMD_INFO])
		return -EIO;

	rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX,
				attrs[ROCKER_TLV_CMD_INFO]);

S
Scott Feldman 已提交
4765
	if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT])
4766 4767
		return -EIO;

S
Scott Feldman 已提交
4768 4769
	pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]);
	if (pport != rocker_port->pport)
4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785
		return -EIO;

	for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
		pattr = stats_attrs[rocker_port_stats[i].type];
		if (!pattr)
			continue;

		data[i] = rocker_tlv_get_u64(pattr);
	}

	return 0;
}

static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port,
					     void *priv)
{
4786
	return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
4787 4788
			       rocker_cmd_get_port_stats_prep, NULL,
			       rocker_cmd_get_port_stats_ethtool_proc,
4789
			       priv);
4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814
}

static void rocker_port_get_stats(struct net_device *dev,
				  struct ethtool_stats *stats, u64 *data)
{
	struct rocker_port *rocker_port = netdev_priv(dev);

	if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) {
		int i;

		for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i)
			data[i] = 0;
	}
}

static int rocker_port_get_sset_count(struct net_device *netdev, int sset)
{
	switch (sset) {
	case ETH_SS_STATS:
		return ROCKER_PORT_STATS_LEN;
	default:
		return -EOPNOTSUPP;
	}
}

4815 4816 4817 4818 4819
static const struct ethtool_ops rocker_port_ethtool_ops = {
	.get_settings		= rocker_port_get_settings,
	.set_settings		= rocker_port_set_settings,
	.get_drvinfo		= rocker_port_get_drvinfo,
	.get_link		= ethtool_op_get_link,
4820 4821 4822
	.get_strings		= rocker_port_get_strings,
	.get_ethtool_stats	= rocker_port_get_stats,
	.get_sset_count		= rocker_port_get_sset_count,
4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836
};

/*****************
 * NAPI interface
 *****************/

static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi)
{
	return container_of(napi, struct rocker_port, napi_tx);
}

static int rocker_port_poll_tx(struct napi_struct *napi, int budget)
{
	struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi);
4837 4838
	const struct rocker *rocker = rocker_port->rocker;
	const struct rocker_desc_info *desc_info;
4839 4840 4841 4842 4843
	u32 credits = 0;
	int err;

	/* Cleanup tx descriptors */
	while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) {
4844 4845
		struct sk_buff *skb;

4846 4847 4848 4849 4850
		err = rocker_desc_err(desc_info);
		if (err && net_ratelimit())
			netdev_err(rocker_port->dev, "tx desc received with err %d\n",
				   err);
		rocker_tx_desc_frags_unmap(rocker_port, desc_info);
4851 4852 4853 4854 4855

		skb = rocker_desc_cookie_ptr_get(desc_info);
		if (err == 0) {
			rocker_port->dev->stats.tx_packets++;
			rocker_port->dev->stats.tx_bytes += skb->len;
S
Scott Feldman 已提交
4856
		} else {
4857
			rocker_port->dev->stats.tx_errors++;
S
Scott Feldman 已提交
4858
		}
4859 4860

		dev_kfree_skb_any(skb);
4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872
		credits++;
	}

	if (credits && netif_queue_stopped(rocker_port->dev))
		netif_wake_queue(rocker_port->dev);

	napi_complete(napi);
	rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits);

	return 0;
}

4873 4874
static int rocker_port_rx_proc(const struct rocker *rocker,
			       const struct rocker_port *rocker_port,
4875 4876
			       struct rocker_desc_info *desc_info)
{
4877
	const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
4878 4879
	struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
	size_t rx_len;
4880
	u16 rx_flags = 0;
4881 4882 4883 4884 4885 4886 4887

	if (!skb)
		return -ENOENT;

	rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
	if (!attrs[ROCKER_TLV_RX_FRAG_LEN])
		return -EINVAL;
4888 4889
	if (attrs[ROCKER_TLV_RX_FLAGS])
		rx_flags = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FLAGS]);
4890 4891 4892 4893 4894 4895

	rocker_dma_rx_ring_skb_unmap(rocker, attrs);

	rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]);
	skb_put(skb, rx_len);
	skb->protocol = eth_type_trans(skb, rocker_port->dev);
4896

4897 4898 4899
	if (rx_flags & ROCKER_RX_FLAGS_FWD_OFFLOAD)
		skb->offload_fwd_mark = rocker_port->dev->offload_fwd_mark;

4900 4901 4902
	rocker_port->dev->stats.rx_packets++;
	rocker_port->dev->stats.rx_bytes += skb->len;

4903 4904
	netif_receive_skb(skb);

4905
	return rocker_dma_rx_ring_skb_alloc(rocker_port, desc_info);
4906 4907 4908 4909 4910 4911 4912 4913 4914 4915
}

static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi)
{
	return container_of(napi, struct rocker_port, napi_rx);
}

static int rocker_port_poll_rx(struct napi_struct *napi, int budget)
{
	struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi);
4916
	const struct rocker *rocker = rocker_port->rocker;
4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935
	struct rocker_desc_info *desc_info;
	u32 credits = 0;
	int err;

	/* Process rx descriptors */
	while (credits < budget &&
	       (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) {
		err = rocker_desc_err(desc_info);
		if (err) {
			if (net_ratelimit())
				netdev_err(rocker_port->dev, "rx desc received with err %d\n",
					   err);
		} else {
			err = rocker_port_rx_proc(rocker, rocker_port,
						  desc_info);
			if (err && net_ratelimit())
				netdev_err(rocker_port->dev, "rx processing failed with err %d\n",
					   err);
		}
4936 4937 4938
		if (err)
			rocker_port->dev->stats.rx_errors++;

4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955
		rocker_desc_gen_clear(desc_info);
		rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info);
		credits++;
	}

	if (credits < budget)
		napi_complete(napi);

	rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits);

	return credits;
}

/*****************
 * PCI driver ops
 *****************/

4956
static void rocker_carrier_init(const struct rocker_port *rocker_port)
4957
{
4958
	const struct rocker *rocker = rocker_port->rocker;
4959 4960 4961
	u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS);
	bool link_up;

S
Scott Feldman 已提交
4962
	link_up = link_status & (1 << rocker_port->pport);
4963 4964 4965 4966 4967 4968
	if (link_up)
		netif_carrier_on(rocker_port->dev);
	else
		netif_carrier_off(rocker_port->dev);
}

4969
static void rocker_remove_ports(const struct rocker *rocker)
4970
{
4971
	struct rocker_port *rocker_port;
4972 4973
	int i;

4974 4975
	for (i = 0; i < rocker->port_count; i++) {
		rocker_port = rocker->ports[i];
4976 4977
		if (!rocker_port)
			continue;
4978 4979
		rocker_port_ig_tbl(rocker_port, SWITCHDEV_TRANS_NONE,
				   ROCKER_OP_FLAG_REMOVE);
4980
		unregister_netdev(rocker_port->dev);
4981
		free_netdev(rocker_port->dev);
4982
	}
4983 4984 4985
	kfree(rocker->ports);
}

4986
static void rocker_port_dev_addr_init(struct rocker_port *rocker_port)
4987
{
4988
	const struct rocker *rocker = rocker_port->rocker;
4989
	const struct pci_dev *pdev = rocker->pdev;
4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001
	int err;

	err = rocker_cmd_get_port_settings_macaddr(rocker_port,
						   rocker_port->dev->dev_addr);
	if (err) {
		dev_warn(&pdev->dev, "failed to get mac address, using random\n");
		eth_hw_addr_random(rocker_port->dev);
	}
}

static int rocker_probe_port(struct rocker *rocker, unsigned int port_number)
{
5002
	const struct pci_dev *pdev = rocker->pdev;
5003 5004
	struct rocker_port *rocker_port;
	struct net_device *dev;
5005
	u16 untagged_vid = 0;
5006 5007 5008 5009 5010 5011 5012 5013 5014
	int err;

	dev = alloc_etherdev(sizeof(struct rocker_port));
	if (!dev)
		return -ENOMEM;
	rocker_port = netdev_priv(dev);
	rocker_port->dev = dev;
	rocker_port->rocker = rocker;
	rocker_port->port_number = port_number;
S
Scott Feldman 已提交
5015
	rocker_port->pport = port_number + 1;
5016
	rocker_port->brport_flags = BR_LEARNING | BR_LEARNING_SYNC;
5017
	rocker_port->ageing_time = BR_DEFAULT_AGEING_TIME;
5018
	INIT_LIST_HEAD(&rocker_port->trans_mem);
5019

5020
	rocker_port_dev_addr_init(rocker_port);
5021 5022
	dev->netdev_ops = &rocker_port_netdev_ops;
	dev->ethtool_ops = &rocker_port_ethtool_ops;
J
Jiri Pirko 已提交
5023
	dev->switchdev_ops = &rocker_port_switchdev_ops;
5024 5025 5026 5027 5028 5029
	netif_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx,
		       NAPI_POLL_WEIGHT);
	netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx,
		       NAPI_POLL_WEIGHT);
	rocker_carrier_init(rocker_port);

5030
	dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_SG;
5031 5032 5033 5034 5035 5036 5037 5038

	err = register_netdev(dev);
	if (err) {
		dev_err(&pdev->dev, "register_netdev failed\n");
		goto err_register_netdev;
	}
	rocker->ports[port_number] = rocker_port;

5039 5040
	switchdev_port_fwd_mark_set(rocker_port->dev, NULL, false);

5041
	rocker_port_set_learning(rocker_port, SWITCHDEV_TRANS_NONE);
5042

5043
	err = rocker_port_ig_tbl(rocker_port, SWITCHDEV_TRANS_NONE, 0);
5044
	if (err) {
5045
		netdev_err(rocker_port->dev, "install ig port table failed\n");
5046 5047 5048
		goto err_port_ig_tbl;
	}

5049 5050 5051 5052 5053 5054 5055 5056 5057 5058
	rocker_port->internal_vlan_id =
		rocker_port_internal_vlan_id_get(rocker_port, dev->ifindex);

	err = rocker_port_vlan_add(rocker_port, SWITCHDEV_TRANS_NONE,
				   untagged_vid, 0);
	if (err) {
		netdev_err(rocker_port->dev, "install untagged VLAN failed\n");
		goto err_untagged_vlan;
	}

5059 5060
	return 0;

5061 5062 5063
err_untagged_vlan:
	rocker_port_ig_tbl(rocker_port, SWITCHDEV_TRANS_NONE,
			   ROCKER_OP_FLAG_REMOVE);
5064
err_port_ig_tbl:
5065
	rocker->ports[port_number] = NULL;
5066
	unregister_netdev(dev);
5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078
err_register_netdev:
	free_netdev(dev);
	return err;
}

static int rocker_probe_ports(struct rocker *rocker)
{
	int i;
	size_t alloc_size;
	int err;

	alloc_size = sizeof(struct rocker_port *) * rocker->port_count;
5079
	rocker->ports = kzalloc(alloc_size, GFP_KERNEL);
5080 5081
	if (!rocker->ports)
		return -ENOMEM;
5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127
	for (i = 0; i < rocker->port_count; i++) {
		err = rocker_probe_port(rocker, i);
		if (err)
			goto remove_ports;
	}
	return 0;

remove_ports:
	rocker_remove_ports(rocker);
	return err;
}

static int rocker_msix_init(struct rocker *rocker)
{
	struct pci_dev *pdev = rocker->pdev;
	int msix_entries;
	int i;
	int err;

	msix_entries = pci_msix_vec_count(pdev);
	if (msix_entries < 0)
		return msix_entries;

	if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count))
		return -EINVAL;

	rocker->msix_entries = kmalloc_array(msix_entries,
					     sizeof(struct msix_entry),
					     GFP_KERNEL);
	if (!rocker->msix_entries)
		return -ENOMEM;

	for (i = 0; i < msix_entries; i++)
		rocker->msix_entries[i].entry = i;

	err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries);
	if (err < 0)
		goto err_enable_msix;

	return 0;

err_enable_msix:
	kfree(rocker->msix_entries);
	return err;
}

5128
static void rocker_msix_fini(const struct rocker *rocker)
5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171
{
	pci_disable_msix(rocker->pdev);
	kfree(rocker->msix_entries);
}

static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id)
{
	struct rocker *rocker;
	int err;

	rocker = kzalloc(sizeof(*rocker), GFP_KERNEL);
	if (!rocker)
		return -ENOMEM;

	err = pci_enable_device(pdev);
	if (err) {
		dev_err(&pdev->dev, "pci_enable_device failed\n");
		goto err_pci_enable_device;
	}

	err = pci_request_regions(pdev, rocker_driver_name);
	if (err) {
		dev_err(&pdev->dev, "pci_request_regions failed\n");
		goto err_pci_request_regions;
	}

	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
	if (!err) {
		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
		if (err) {
			dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
			goto err_pci_set_dma_mask;
		}
	} else {
		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
		if (err) {
			dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
			goto err_pci_set_dma_mask;
		}
	}

	if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) {
		dev_err(&pdev->dev, "invalid PCI region size\n");
5172
		err = -EINVAL;
5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225
		goto err_pci_resource_len_check;
	}

	rocker->hw_addr = ioremap(pci_resource_start(pdev, 0),
				  pci_resource_len(pdev, 0));
	if (!rocker->hw_addr) {
		dev_err(&pdev->dev, "ioremap failed\n");
		err = -EIO;
		goto err_ioremap;
	}
	pci_set_master(pdev);

	rocker->pdev = pdev;
	pci_set_drvdata(pdev, rocker);

	rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT);

	err = rocker_msix_init(rocker);
	if (err) {
		dev_err(&pdev->dev, "MSI-X init failed\n");
		goto err_msix_init;
	}

	err = rocker_basic_hw_test(rocker);
	if (err) {
		dev_err(&pdev->dev, "basic hw test failed\n");
		goto err_basic_hw_test;
	}

	rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);

	err = rocker_dma_rings_init(rocker);
	if (err)
		goto err_dma_rings_init;

	err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD),
			  rocker_cmd_irq_handler, 0,
			  rocker_driver_name, rocker);
	if (err) {
		dev_err(&pdev->dev, "cannot assign cmd irq\n");
		goto err_request_cmd_irq;
	}

	err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT),
			  rocker_event_irq_handler, 0,
			  rocker_driver_name, rocker);
	if (err) {
		dev_err(&pdev->dev, "cannot assign event irq\n");
		goto err_request_event_irq;
	}

	rocker->hw.id = rocker_read64(rocker, SWITCH_ID);

5226 5227 5228 5229 5230 5231
	err = rocker_init_tbls(rocker);
	if (err) {
		dev_err(&pdev->dev, "cannot init rocker tables\n");
		goto err_init_tbls;
	}

S
Scott Feldman 已提交
5232 5233 5234 5235
	setup_timer(&rocker->fdb_cleanup_timer, rocker_fdb_cleanup,
		    (unsigned long) rocker);
	mod_timer(&rocker->fdb_cleanup_timer, jiffies);

5236 5237 5238 5239 5240 5241
	err = rocker_probe_ports(rocker);
	if (err) {
		dev_err(&pdev->dev, "failed to probe ports\n");
		goto err_probe_ports;
	}

5242 5243
	dev_info(&pdev->dev, "Rocker switch with id %*phN\n",
		 (int)sizeof(rocker->hw.id), &rocker->hw.id);
5244 5245 5246 5247

	return 0;

err_probe_ports:
S
Scott Feldman 已提交
5248
	del_timer_sync(&rocker->fdb_cleanup_timer);
5249 5250
	rocker_free_tbls(rocker);
err_init_tbls:
5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275
	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
err_request_event_irq:
	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
err_request_cmd_irq:
	rocker_dma_rings_fini(rocker);
err_dma_rings_init:
err_basic_hw_test:
	rocker_msix_fini(rocker);
err_msix_init:
	iounmap(rocker->hw_addr);
err_ioremap:
err_pci_resource_len_check:
err_pci_set_dma_mask:
	pci_release_regions(pdev);
err_pci_request_regions:
	pci_disable_device(pdev);
err_pci_enable_device:
	kfree(rocker);
	return err;
}

static void rocker_remove(struct pci_dev *pdev)
{
	struct rocker *rocker = pci_get_drvdata(pdev);

S
Scott Feldman 已提交
5276
	del_timer_sync(&rocker->fdb_cleanup_timer);
5277
	rocker_free_tbls(rocker);
5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296
	rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
	rocker_remove_ports(rocker);
	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
	rocker_dma_rings_fini(rocker);
	rocker_msix_fini(rocker);
	iounmap(rocker->hw_addr);
	pci_release_regions(rocker->pdev);
	pci_disable_device(rocker->pdev);
	kfree(rocker);
}

static struct pci_driver rocker_pci_driver = {
	.name		= rocker_driver_name,
	.id_table	= rocker_pci_id_table,
	.probe		= rocker_probe,
	.remove		= rocker_remove,
};

5297 5298 5299 5300
/************************************
 * Net device notifier event handler
 ************************************/

5301
static bool rocker_port_dev_check(const struct net_device *dev)
5302 5303 5304 5305 5306 5307 5308
{
	return dev->netdev_ops == &rocker_port_netdev_ops;
}

static int rocker_port_bridge_join(struct rocker_port *rocker_port,
				   struct net_device *bridge)
{
5309
	u16 untagged_vid = 0;
5310 5311
	int err;

5312 5313 5314 5315 5316
	/* Port is joining bridge, so the internal VLAN for the
	 * port is going to change to the bridge internal VLAN.
	 * Let's remove untagged VLAN (vid=0) from port and
	 * re-add once internal VLAN has changed.
	 */
5317

5318
	err = rocker_port_vlan_del(rocker_port, untagged_vid, 0);
5319 5320
	if (err)
		return err;
5321 5322 5323

	rocker_port_internal_vlan_id_put(rocker_port,
					 rocker_port->dev->ifindex);
5324
	rocker_port->internal_vlan_id =
5325
		rocker_port_internal_vlan_id_get(rocker_port, bridge->ifindex);
5326 5327

	rocker_port->bridge_dev = bridge;
5328
	switchdev_port_fwd_mark_set(rocker_port->dev, bridge, true);
5329 5330 5331

	return rocker_port_vlan_add(rocker_port, SWITCHDEV_TRANS_NONE,
				    untagged_vid, 0);
5332 5333 5334 5335
}

static int rocker_port_bridge_leave(struct rocker_port *rocker_port)
{
5336
	u16 untagged_vid = 0;
5337 5338
	int err;

5339
	err = rocker_port_vlan_del(rocker_port, untagged_vid, 0);
5340 5341
	if (err)
		return err;
5342 5343 5344

	rocker_port_internal_vlan_id_put(rocker_port,
					 rocker_port->bridge_dev->ifindex);
5345 5346 5347
	rocker_port->internal_vlan_id =
		rocker_port_internal_vlan_id_get(rocker_port,
						 rocker_port->dev->ifindex);
5348

5349 5350
	switchdev_port_fwd_mark_set(rocker_port->dev, rocker_port->bridge_dev,
				    false);
5351 5352 5353 5354
	rocker_port->bridge_dev = NULL;

	err = rocker_port_vlan_add(rocker_port, SWITCHDEV_TRANS_NONE,
				   untagged_vid, 0);
5355 5356 5357 5358
	if (err)
		return err;

	if (rocker_port->dev->flags & IFF_UP)
5359 5360
		err = rocker_port_fwd_enable(rocker_port,
					     SWITCHDEV_TRANS_NONE, 0);
5361 5362 5363 5364

	return err;
}

5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380

static int rocker_port_ovs_changed(struct rocker_port *rocker_port,
				   struct net_device *master)
{
	int err;

	rocker_port->bridge_dev = master;

	err = rocker_port_fwd_disable(rocker_port, SWITCHDEV_TRANS_NONE, 0);
	if (err)
		return err;
	err = rocker_port_fwd_enable(rocker_port, SWITCHDEV_TRANS_NONE, 0);

	return err;
}

J
Jiri Pirko 已提交
5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393
static int rocker_port_master_linked(struct rocker_port *rocker_port,
				     struct net_device *master)
{
	int err = 0;

	if (netif_is_bridge_master(master))
		err = rocker_port_bridge_join(rocker_port, master);
	else if (netif_is_ovs_master(master))
		err = rocker_port_ovs_changed(rocker_port, master);
	return err;
}

static int rocker_port_master_unlinked(struct rocker_port *rocker_port)
5394 5395 5396
{
	int err = 0;

J
Jiri Pirko 已提交
5397
	if (rocker_port_is_bridged(rocker_port))
5398
		err = rocker_port_bridge_leave(rocker_port);
J
Jiri Pirko 已提交
5399
	else if (rocker_port_is_ovsed(rocker_port))
5400
		err = rocker_port_ovs_changed(rocker_port, NULL);
5401 5402 5403 5404 5405 5406
	return err;
}

static int rocker_netdevice_event(struct notifier_block *unused,
				  unsigned long event, void *ptr)
{
J
Jiri Pirko 已提交
5407 5408 5409
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
	struct netdev_notifier_changeupper_info *info;
	struct rocker_port *rocker_port;
5410 5411
	int err;

J
Jiri Pirko 已提交
5412 5413 5414
	if (!rocker_port_dev_check(dev))
		return NOTIFY_DONE;

5415 5416
	switch (event) {
	case NETDEV_CHANGEUPPER:
J
Jiri Pirko 已提交
5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432
		info = ptr;
		if (!info->master)
			goto out;
		rocker_port = netdev_priv(dev);
		if (info->linking) {
			err = rocker_port_master_linked(rocker_port,
							info->upper_dev);
			if (err)
				netdev_warn(dev, "failed to reflect master linked (err %d)\n",
					    err);
		} else {
			err = rocker_port_master_unlinked(rocker_port);
			if (err)
				netdev_warn(dev, "failed to reflect master unlinked (err %d)\n",
					    err);
		}
5433 5434
		break;
	}
J
Jiri Pirko 已提交
5435
out:
5436 5437 5438 5439 5440 5441 5442
	return NOTIFY_DONE;
}

static struct notifier_block rocker_netdevice_nb __read_mostly = {
	.notifier_call = rocker_netdevice_event,
};

5443 5444 5445 5446 5447 5448 5449
/************************************
 * Net event notifier event handler
 ************************************/

static int rocker_neigh_update(struct net_device *dev, struct neighbour *n)
{
	struct rocker_port *rocker_port = netdev_priv(dev);
5450 5451
	int flags = (n->nud_state & NUD_VALID ? 0 : ROCKER_OP_FLAG_REMOVE) |
		    ROCKER_OP_FLAG_NOWAIT;
5452 5453
	__be32 ip_addr = *(__be32 *)n->primary_key;

5454 5455
	return rocker_port_ipv4_neigh(rocker_port, SWITCHDEV_TRANS_NONE,
				      flags, ip_addr, n->ha);
5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486
}

static int rocker_netevent_event(struct notifier_block *unused,
				 unsigned long event, void *ptr)
{
	struct net_device *dev;
	struct neighbour *n = ptr;
	int err;

	switch (event) {
	case NETEVENT_NEIGH_UPDATE:
		if (n->tbl != &arp_tbl)
			return NOTIFY_DONE;
		dev = n->dev;
		if (!rocker_port_dev_check(dev))
			return NOTIFY_DONE;
		err = rocker_neigh_update(dev, n);
		if (err)
			netdev_warn(dev,
				    "failed to handle neigh update (err %d)\n",
				    err);
		break;
	}

	return NOTIFY_DONE;
}

static struct notifier_block rocker_netevent_nb __read_mostly = {
	.notifier_call = rocker_netevent_event,
};

5487 5488 5489 5490 5491 5492
/***********************
 * Module init and exit
 ***********************/

static int __init rocker_module_init(void)
{
5493 5494 5495
	int err;

	register_netdevice_notifier(&rocker_netdevice_nb);
5496
	register_netevent_notifier(&rocker_netevent_nb);
5497 5498 5499 5500 5501 5502
	err = pci_register_driver(&rocker_pci_driver);
	if (err)
		goto err_pci_register_driver;
	return 0;

err_pci_register_driver:
5503
	unregister_netevent_notifier(&rocker_netevent_nb);
5504 5505
	unregister_netdevice_notifier(&rocker_netdevice_nb);
	return err;
5506 5507 5508 5509
}

static void __exit rocker_module_exit(void)
{
5510
	unregister_netevent_notifier(&rocker_netevent_nb);
5511
	unregister_netdevice_notifier(&rocker_netdevice_nb);
5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522
	pci_unregister_driver(&rocker_pci_driver);
}

module_init(rocker_module_init);
module_exit(rocker_module_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>");
MODULE_DESCRIPTION("Rocker switch device driver");
MODULE_DEVICE_TABLE(pci, rocker_pci_id_table);