rocker_main.c 76.8 KB
Newer Older
1 2
/*
 * drivers/net/ethernet/rocker/rocker.c - Rocker switch device driver
3
 * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * 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>
#include <linux/sort.h>
#include <linux/random.h>
#include <linux/netdevice.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>
28
#include <linux/if_bridge.h>
29
#include <linux/bitops.h>
30
#include <linux/ctype.h>
31 32
#include <net/switchdev.h>
#include <net/rtnetlink.h>
33 34
#include <net/netevent.h>
#include <net/arp.h>
35
#include <linux/io-64-nonatomic-lo-hi.h>
36 37
#include <generated/utsrelease.h>

38
#include "rocker_hw.h"
39 40
#include "rocker.h"
#include "rocker_tlv.h"
41 42 43 44 45 46 47 48 49 50 51

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, }
};

struct rocker_wait {
	wait_queue_head_t wait;
	bool done;
52
	bool nowait;
53 54 55 56 57
};

static void rocker_wait_reset(struct rocker_wait *wait)
{
	wait->done = false;
58
	wait->nowait = false;
59 60 61 62 63 64 65 66
}

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

67
static struct rocker_wait *rocker_wait_create(void)
68 69 70
{
	struct rocker_wait *wait;

71
	wait = kzalloc(sizeof(*wait), GFP_KERNEL);
72 73 74 75 76
	if (!wait)
		return NULL;
	return wait;
}

77
static void rocker_wait_destroy(struct rocker_wait *wait)
78
{
79
	kfree(wait);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

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);
}

97
static u32 rocker_msix_vector(const struct rocker *rocker, unsigned int vector)
98 99 100 101
{
	return rocker->msix_entries[vector].vector;
}

102
static u32 rocker_msix_tx_vector(const struct rocker_port *rocker_port)
103 104 105 106 107
{
	return rocker_msix_vector(rocker_port->rocker,
				  ROCKER_MSIX_VEC_TX(rocker_port->port_number));
}

108
static u32 rocker_msix_rx_vector(const struct rocker_port *rocker_port)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
{
	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
 *****************************/

127
static int rocker_reg_test(const struct rocker *rocker)
128
{
129
	const struct pci_dev *pdev = rocker->pdev;
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 155 156
	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;
}

157 158 159 160
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)
161
{
162
	const struct pci_dev *pdev = rocker->pdev;
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	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

186
static int rocker_dma_test_offset(const struct rocker *rocker,
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
				  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;
}

246 247
static int rocker_dma_test(const struct rocker *rocker,
			   struct rocker_wait *wait)
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
{
	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;
}

269
static int rocker_basic_hw_test(const struct rocker *rocker)
270
{
271
	const struct pci_dev *pdev = rocker->pdev;
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	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;
}

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

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

316
static int rocker_desc_err(const struct rocker_desc_info *desc_info)
317
{
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	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;
342 343
}

344
static void rocker_desc_gen_clear(const struct rocker_desc_info *desc_info)
345 346 347 348
{
	desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN;
}

349
static bool rocker_desc_gen(const struct rocker_desc_info *desc_info)
350 351 352 353 354 355
{
	u32 comp_err = desc_info->desc->comp_err;

	return comp_err & ROCKER_DMA_DESC_COMP_ERR_GEN ? true : false;
}

356 357
static void *
rocker_desc_cookie_ptr_get(const struct rocker_desc_info *desc_info)
358
{
359
	return (void *)(uintptr_t)desc_info->desc->cookie;
360 361
}

362
static void rocker_desc_cookie_ptr_set(const struct rocker_desc_info *desc_info,
363 364
				       void *ptr)
{
365
	desc_info->desc->cookie = (uintptr_t) ptr;
366 367 368
}

static struct rocker_desc_info *
369
rocker_desc_head_get(const struct rocker_dma_ring_info *info)
370 371 372 373 374 375 376 377 378 379 380
{
	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;
}

381
static void rocker_desc_commit(const struct rocker_desc_info *desc_info)
382 383 384 385 386
{
	desc_info->desc->buf_size = desc_info->data_size;
	desc_info->desc->tlv_size = desc_info->tlv_size;
}

387
static void rocker_desc_head_set(const struct rocker *rocker,
388
				 struct rocker_dma_ring_info *info,
389
				 const struct rocker_desc_info *desc_info)
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
{
	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;
}

414 415
static void rocker_dma_ring_credits_set(const struct rocker *rocker,
					const struct rocker_dma_ring_info *info,
416 417 418 419 420 421 422 423 424 425 426 427
					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));
}

428
static int rocker_dma_ring_create(const struct rocker *rocker,
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
				  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;
}

464 465
static void rocker_dma_ring_destroy(const struct rocker *rocker,
				    const struct rocker_dma_ring_info *info)
466 467 468 469 470 471 472 473 474
{
	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);
}

475
static void rocker_dma_ring_pass_to_producer(const struct rocker *rocker,
476 477 478 479 480 481 482 483 484 485 486 487 488 489
					     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]);
}

490 491
static int rocker_dma_ring_bufs_alloc(const struct rocker *rocker,
				      const struct rocker_dma_ring_info *info,
492 493 494 495 496 497 498 499 500 501 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
				      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--) {
528
		const struct rocker_desc_info *desc_info = &info->desc_info[i];
529 530 531 532 533 534 535 536

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

537 538
static void rocker_dma_ring_bufs_free(const struct rocker *rocker,
				      const struct rocker_dma_ring_info *info,
539 540 541 542 543 544
				      int direction)
{
	struct pci_dev *pdev = rocker->pdev;
	int i;

	for (i = 0; i < info->size; i++) {
545
		const struct rocker_desc_info *desc_info = &info->desc_info[i];
546 547 548 549 550 551 552 553 554 555
		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);
	}
}

556 557 558 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
static int rocker_dma_cmd_ring_wait_alloc(struct rocker_desc_info *desc_info)
{
	struct rocker_wait *wait;

	wait = rocker_wait_create();
	if (!wait)
		return -ENOMEM;
	rocker_desc_cookie_ptr_set(desc_info, wait);
	return 0;
}

static void
rocker_dma_cmd_ring_wait_free(const struct rocker_desc_info *desc_info)
{
	struct rocker_wait *wait = rocker_desc_cookie_ptr_get(desc_info);

	rocker_wait_destroy(wait);
}

static int rocker_dma_cmd_ring_waits_alloc(const struct rocker *rocker)
{
	const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring;
	int i;
	int err;

	for (i = 0; i < cmd_ring->size; i++) {
		err = rocker_dma_cmd_ring_wait_alloc(&cmd_ring->desc_info[i]);
		if (err)
			goto rollback;
	}
	return 0;

rollback:
	for (i--; i >= 0; i--)
		rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]);
	return err;
}

static void rocker_dma_cmd_ring_waits_free(const struct rocker *rocker)
{
	const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring;
	int i;

	for (i = 0; i < cmd_ring->size; i++)
		rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]);
}

603 604
static int rocker_dma_rings_init(struct rocker *rocker)
{
605
	const struct pci_dev *pdev = rocker->pdev;
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
	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;
	}

625 626 627 628 629 630
	err = rocker_dma_cmd_ring_waits_alloc(rocker);
	if (err) {
		dev_err(&pdev->dev, "failed to alloc command dma ring waits\n");
		goto err_dma_cmd_ring_waits_alloc;
	}

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
	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);
653 654
err_dma_cmd_ring_waits_alloc:
	rocker_dma_cmd_ring_waits_free(rocker);
655 656 657 658 659 660 661 662 663 664
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);
665
	rocker_dma_cmd_ring_waits_free(rocker);
666 667 668 669 670
	rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
				  PCI_DMA_BIDIRECTIONAL);
	rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
}

671
static int rocker_dma_rx_ring_skb_map(const struct rocker_port *rocker_port,
672 673 674
				      struct rocker_desc_info *desc_info,
				      struct sk_buff *skb, size_t buf_len)
{
675
	const struct rocker *rocker = rocker_port->rocker;
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
	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;
}

695
static size_t rocker_port_rx_buf_len(const struct rocker_port *rocker_port)
696 697 698 699
{
	return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
}

700
static int rocker_dma_rx_ring_skb_alloc(const struct rocker_port *rocker_port,
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
					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;
717
	err = rocker_dma_rx_ring_skb_map(rocker_port, desc_info, skb, buf_len);
718 719 720 721 722 723 724 725
	if (err) {
		dev_kfree_skb_any(skb);
		return err;
	}
	rocker_desc_cookie_ptr_set(desc_info, skb);
	return 0;
}

726 727
static void rocker_dma_rx_ring_skb_unmap(const struct rocker *rocker,
					 const struct rocker_tlv **attrs)
728 729 730 731 732 733 734 735 736 737 738 739 740
{
	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);
}

741 742
static void rocker_dma_rx_ring_skb_free(const struct rocker *rocker,
					const struct rocker_desc_info *desc_info)
743
{
744
	const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
745 746 747 748 749 750 751 752 753
	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);
}

754
static int rocker_dma_rx_ring_skbs_alloc(const struct rocker_port *rocker_port)
755
{
756
	const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
757
	const struct rocker *rocker = rocker_port->rocker;
758 759 760 761
	int i;
	int err;

	for (i = 0; i < rx_ring->size; i++) {
762
		err = rocker_dma_rx_ring_skb_alloc(rocker_port,
763 764 765 766 767 768 769 770 771 772 773 774
						   &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;
}

775
static void rocker_dma_rx_ring_skbs_free(const struct rocker_port *rocker_port)
776
{
777
	const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
778
	const struct rocker *rocker = rocker_port->rocker;
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
	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;
	}

824
	err = rocker_dma_rx_ring_skbs_alloc(rocker_port);
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
	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;

850
	rocker_dma_rx_ring_skbs_free(rocker_port);
851 852 853 854 855 856 857 858
	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);
}

859 860
static void rocker_port_set_enable(const struct rocker_port *rocker_port,
				   bool enable)
861 862 863 864
{
	u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE);

	if (enable)
865
		val |= 1ULL << rocker_port->pport;
866
	else
867
		val &= ~(1ULL << rocker_port->pport);
868 869 870 871 872 873 874 875 876 877
	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;
878
	const struct rocker_desc_info *desc_info;
879 880 881 882 883 884
	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);
885 886 887 888 889
		if (wait->nowait) {
			rocker_desc_gen_clear(desc_info);
		} else {
			rocker_wait_wake_up(wait);
		}
890 891 892 893 894 895 896 897
		credits++;
	}
	spin_unlock(&rocker->cmd_ring_lock);
	rocker_dma_ring_credits_set(rocker, &rocker->cmd_ring, credits);

	return IRQ_HANDLED;
}

898
static void rocker_port_link_up(const struct rocker_port *rocker_port)
899 900 901 902 903
{
	netif_carrier_on(rocker_port->dev);
	netdev_info(rocker_port->dev, "Link is up\n");
}

904
static void rocker_port_link_down(const struct rocker_port *rocker_port)
905 906 907 908 909
{
	netif_carrier_off(rocker_port->dev);
	netdev_info(rocker_port->dev, "Link is down\n");
}

910
static int rocker_event_link_change(const struct rocker *rocker,
911 912
				    const struct rocker_tlv *info)
{
913
	const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1];
914 915 916 917 918
	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 已提交
919
	if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] ||
920 921 922
	    !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP])
		return -EIO;
	port_number =
S
Scott Feldman 已提交
923
		rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1;
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
	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;
}

940 941 942
static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
					      const unsigned char *addr,
					      __be16 vlan_id);
943

944
static int rocker_event_mac_vlan_seen(const struct rocker *rocker,
945 946
				      const struct rocker_tlv *info)
{
947
	const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1];
948 949
	unsigned int port_number;
	struct rocker_port *rocker_port;
950
	const unsigned char *addr;
951 952 953
	__be16 vlan_id;

	rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info);
S
Scott Feldman 已提交
954
	if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] ||
955 956 957 958
	    !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] ||
	    !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID])
		return -EIO;
	port_number =
S
Scott Feldman 已提交
959
		rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1;
960
	addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]);
961
	vlan_id = rocker_tlv_get_be16(attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]);
962 963 964 965 966

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

	rocker_port = rocker->ports[port_number];
967
	return rocker_world_port_ev_mac_vlan_seen(rocker_port, addr, vlan_id);
968
}
969

970 971
static int rocker_event_process(const struct rocker *rocker,
				const struct rocker_desc_info *desc_info)
972
{
973 974
	const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1];
	const struct rocker_tlv *info;
975 976 977 978 979 980 981 982 983 984 985 986 987
	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);
988 989
	case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN:
		return rocker_event_mac_vlan_seen(rocker, info);
990 991 992 993 994 995 996 997
	}

	return -EOPNOTSUPP;
}

static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id)
{
	struct rocker *rocker = dev_id;
998 999
	const struct pci_dev *pdev = rocker->pdev;
	const struct rocker_desc_info *desc_info;
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 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
	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
 ********************/

1043 1044 1045
int rocker_cmd_exec(struct rocker_port *rocker_port, bool nowait,
		    rocker_cmd_prep_cb_t prepare, void *prepare_priv,
		    rocker_cmd_proc_cb_t process, void *process_priv)
1046
{
1047
	struct rocker *rocker = rocker_port->rocker;
1048 1049
	struct rocker_desc_info *desc_info;
	struct rocker_wait *wait;
1050
	unsigned long lock_flags;
1051 1052
	int err;

1053
	spin_lock_irqsave(&rocker->cmd_ring_lock, lock_flags);
1054

1055 1056
	desc_info = rocker_desc_head_get(&rocker->cmd_ring);
	if (!desc_info) {
1057
		spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1058
		return -EAGAIN;
1059
	}
1060

1061 1062 1063 1064
	wait = rocker_desc_cookie_ptr_get(desc_info);
	rocker_wait_init(wait);
	wait->nowait = nowait;

1065
	err = prepare(rocker_port, desc_info, prepare_priv);
1066
	if (err) {
1067
		spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1068
		return err;
1069
	}
1070

1071
	rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info);
1072

1073 1074 1075 1076
	spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);

	if (nowait)
		return 0;
1077

1078 1079
	if (!rocker_wait_event_timeout(wait, HZ / 10))
		return -EIO;
1080 1081 1082 1083 1084 1085

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

	if (process)
1086
		err = process(rocker_port, desc_info, process_priv);
1087 1088 1089 1090 1091 1092

	rocker_desc_gen_clear(desc_info);
	return err;
}

static int
1093
rocker_cmd_get_port_settings_prep(const struct rocker_port *rocker_port,
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
				  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 已提交
1105 1106
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
			       rocker_port->pport))
1107 1108 1109 1110 1111 1112
		return -EMSGSIZE;
	rocker_tlv_nest_end(desc_info, cmd_info);
	return 0;
}

static int
1113
rocker_cmd_get_port_settings_ethtool_proc(const struct rocker_port *rocker_port,
1114
					  const struct rocker_desc_info *desc_info,
1115 1116 1117
					  void *priv)
{
	struct ethtool_cmd *ecmd = priv;
1118 1119
	const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
	const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
	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
1151
rocker_cmd_get_port_settings_macaddr_proc(const struct rocker_port *rocker_port,
1152
					  const struct rocker_desc_info *desc_info,
1153 1154 1155
					  void *priv)
{
	unsigned char *macaddr = priv;
1156 1157 1158
	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;
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

	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;
}

1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
static int
rocker_cmd_get_port_settings_mode_proc(const struct rocker_port *rocker_port,
				       const struct rocker_desc_info *desc_info,
				       void *priv)
{
	u8 *p_mode = priv;
	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;

	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_MODE];
	if (!attr)
		return -EIO;

	*p_mode = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MODE]);
	return 0;
}

1201 1202 1203 1204 1205 1206
struct port_name {
	char *buf;
	size_t len;
};

static int
1207
rocker_cmd_get_port_settings_phys_name_proc(const struct rocker_port *rocker_port,
1208
					    const struct rocker_desc_info *desc_info,
1209 1210
					    void *priv)
{
1211 1212
	const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
	const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1213
	struct port_name *name = priv;
1214
	const struct rocker_tlv *attr;
1215
	size_t i, j, len;
1216
	const char *str;
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246

	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;
}

1247
static int
1248
rocker_cmd_set_port_settings_ethtool_prep(const struct rocker_port *rocker_port,
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
					  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 已提交
1261 1262
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
			       rocker_port->pport))
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
		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
1278
rocker_cmd_set_port_settings_macaddr_prep(const struct rocker_port *rocker_port,
1279 1280 1281
					  struct rocker_desc_info *desc_info,
					  void *priv)
{
1282
	const unsigned char *macaddr = priv;
1283 1284 1285 1286 1287 1288 1289 1290
	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 已提交
1291 1292
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
			       rocker_port->pport))
1293 1294 1295 1296 1297 1298 1299 1300
		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 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
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;
}

1325
static int
1326
rocker_cmd_set_port_learning_prep(const struct rocker_port *rocker_port,
1327 1328 1329
				  struct rocker_desc_info *desc_info,
				  void *priv)
{
1330
	bool learning = *(bool *)priv;
1331 1332 1333 1334 1335 1336 1337 1338
	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 已提交
1339 1340
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
			       rocker_port->pport))
1341 1342
		return -EMSGSIZE;
	if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING,
1343
			      learning))
1344 1345 1346 1347 1348
		return -EMSGSIZE;
	rocker_tlv_nest_end(desc_info, cmd_info);
	return 0;
}

1349 1350 1351
static int rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port,
						struct ethtool_cmd *ecmd)
{
1352
	return rocker_cmd_exec(rocker_port, false,
1353 1354
			       rocker_cmd_get_port_settings_prep, NULL,
			       rocker_cmd_get_port_settings_ethtool_proc,
1355
			       ecmd);
1356 1357 1358 1359 1360
}

static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port,
						unsigned char *macaddr)
{
1361
	return rocker_cmd_exec(rocker_port, false,
1362 1363
			       rocker_cmd_get_port_settings_prep, NULL,
			       rocker_cmd_get_port_settings_macaddr_proc,
1364
			       macaddr);
1365 1366
}

1367 1368 1369
static int rocker_cmd_get_port_settings_mode(struct rocker_port *rocker_port,
					     u8 *p_mode)
{
1370
	return rocker_cmd_exec(rocker_port, false,
1371 1372 1373 1374
			       rocker_cmd_get_port_settings_prep, NULL,
			       rocker_cmd_get_port_settings_mode_proc, p_mode);
}

1375 1376 1377
static int rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port,
						struct ethtool_cmd *ecmd)
{
1378
	return rocker_cmd_exec(rocker_port, false,
1379
			       rocker_cmd_set_port_settings_ethtool_prep,
1380
			       ecmd, NULL, NULL);
1381 1382 1383 1384 1385
}

static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port,
						unsigned char *macaddr)
{
1386
	return rocker_cmd_exec(rocker_port, false,
1387
			       rocker_cmd_set_port_settings_macaddr_prep,
1388
			       macaddr, NULL, NULL);
1389 1390
}

S
Scott Feldman 已提交
1391 1392 1393
static int rocker_cmd_set_port_settings_mtu(struct rocker_port *rocker_port,
					    int mtu)
{
1394
	return rocker_cmd_exec(rocker_port, false,
S
Scott Feldman 已提交
1395 1396 1397 1398
			       rocker_cmd_set_port_settings_mtu_prep,
			       &mtu, NULL, NULL);
}

1399 1400
int rocker_port_set_learning(struct rocker_port *rocker_port,
			     bool learning)
1401
{
1402
	return rocker_cmd_exec(rocker_port, false,
1403
			       rocker_cmd_set_port_learning_prep,
1404
			       &learning, NULL, NULL);
1405 1406
}

1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
/**********************
 * Worlds manipulation
 **********************/

static struct rocker_world_ops *rocker_world_ops[] = {
	&rocker_ofdpa_ops,
};

#define ROCKER_WORLD_OPS_LEN ARRAY_SIZE(rocker_world_ops)

static struct rocker_world_ops *rocker_world_ops_find(u8 mode)
{
	int i;

	for (i = 0; i < ROCKER_WORLD_OPS_LEN; i++)
		if (rocker_world_ops[i]->mode == mode)
			return rocker_world_ops[i];
	return NULL;
}

static int rocker_world_init(struct rocker *rocker, u8 mode)
{
	struct rocker_world_ops *wops;
	int err;

	wops = rocker_world_ops_find(mode);
	if (!wops) {
		dev_err(&rocker->pdev->dev, "port mode \"%d\" is not supported\n",
			mode);
		return -EINVAL;
	}
	rocker->wops = wops;
	rocker->wpriv = kzalloc(wops->priv_size, GFP_KERNEL);
	if (!rocker->wpriv)
		return -ENOMEM;
	if (!wops->init)
		return 0;
	err = wops->init(rocker);
	if (err)
		kfree(rocker->wpriv);
	return err;
}

static void rocker_world_fini(struct rocker *rocker)
{
	struct rocker_world_ops *wops = rocker->wops;

	if (!wops || !wops->fini)
		return;
	wops->fini(rocker);
	kfree(rocker->wpriv);
}

static int rocker_world_check_init(struct rocker_port *rocker_port)
{
	struct rocker *rocker = rocker_port->rocker;
	u8 mode;
	int err;

	err = rocker_cmd_get_port_settings_mode(rocker_port, &mode);
	if (err) {
		dev_err(&rocker->pdev->dev, "failed to get port mode\n");
		return err;
	}
	if (rocker->wops) {
		if (rocker->wops->mode != mode) {
			dev_err(&rocker->pdev->dev, "hardware has ports in different worlds, which is not supported\n");
			return err;
		}
		return 0;
	}
	return rocker_world_init(rocker, mode);
}

static int rocker_world_port_pre_init(struct rocker_port *rocker_port)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;
	int err;

	rocker_port->wpriv = kzalloc(wops->port_priv_size, GFP_KERNEL);
	if (!rocker_port->wpriv)
		return -ENOMEM;
	if (!wops->port_pre_init)
		return 0;
	err = wops->port_pre_init(rocker_port);
	if (err)
		kfree(rocker_port->wpriv);
	return 0;
}

static int rocker_world_port_init(struct rocker_port *rocker_port)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_init)
		return 0;
	return wops->port_init(rocker_port);
}

static void rocker_world_port_fini(struct rocker_port *rocker_port)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_fini)
		return;
	wops->port_fini(rocker_port);
}

static void rocker_world_port_post_fini(struct rocker_port *rocker_port)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_post_fini)
		return;
	wops->port_post_fini(rocker_port);
	kfree(rocker_port->wpriv);
}

static int rocker_world_port_open(struct rocker_port *rocker_port)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_open)
		return 0;
	return wops->port_open(rocker_port);
}

static void rocker_world_port_stop(struct rocker_port *rocker_port)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_stop)
		return;
	wops->port_stop(rocker_port);
}

static int rocker_world_port_attr_stp_state_set(struct rocker_port *rocker_port,
						u8 state,
						struct switchdev_trans *trans)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_attr_stp_state_set)
1550
		return -EOPNOTSUPP;
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
	return wops->port_attr_stp_state_set(rocker_port, state, trans);
}

static int
rocker_world_port_attr_bridge_flags_set(struct rocker_port *rocker_port,
					unsigned long brport_flags,
					struct switchdev_trans *trans)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_attr_bridge_flags_set)
1562
		return -EOPNOTSUPP;
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
	return wops->port_attr_bridge_flags_set(rocker_port, brport_flags,
						trans);
}

static int
rocker_world_port_attr_bridge_flags_get(const struct rocker_port *rocker_port,
					unsigned long *p_brport_flags)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_attr_bridge_flags_get)
1574
		return -EOPNOTSUPP;
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
	return wops->port_attr_bridge_flags_get(rocker_port, p_brport_flags);
}

static int
rocker_world_port_attr_bridge_ageing_time_set(struct rocker_port *rocker_port,
					      u32 ageing_time,
					      struct switchdev_trans *trans)

{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_attr_bridge_ageing_time_set)
1587
		return -EOPNOTSUPP;
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
	return wops->port_attr_bridge_ageing_time_set(rocker_port, ageing_time,
						      trans);
}

static int
rocker_world_port_obj_vlan_add(struct rocker_port *rocker_port,
			       const struct switchdev_obj_port_vlan *vlan,
			       struct switchdev_trans *trans)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_obj_vlan_add)
1600
		return -EOPNOTSUPP;
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
	return wops->port_obj_vlan_add(rocker_port, vlan, trans);
}

static int
rocker_world_port_obj_vlan_del(struct rocker_port *rocker_port,
			       const struct switchdev_obj_port_vlan *vlan)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_obj_vlan_del)
1611
		return -EOPNOTSUPP;
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
	return wops->port_obj_vlan_del(rocker_port, vlan);
}

static int
rocker_world_port_obj_vlan_dump(const struct rocker_port *rocker_port,
				struct switchdev_obj_port_vlan *vlan,
				switchdev_obj_dump_cb_t *cb)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_obj_vlan_dump)
1623
		return -EOPNOTSUPP;
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
	return wops->port_obj_vlan_dump(rocker_port, vlan, cb);
}

static int
rocker_world_port_obj_fdb_add(struct rocker_port *rocker_port,
			      const struct switchdev_obj_port_fdb *fdb,
			      struct switchdev_trans *trans)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_obj_fdb_add)
1635
		return -EOPNOTSUPP;
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
	return wops->port_obj_fdb_add(rocker_port, fdb, trans);
}

static int
rocker_world_port_obj_fdb_del(struct rocker_port *rocker_port,
			      const struct switchdev_obj_port_fdb *fdb)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_obj_fdb_del)
1646
		return -EOPNOTSUPP;
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
	return wops->port_obj_fdb_del(rocker_port, fdb);
}

static int
rocker_world_port_obj_fdb_dump(const struct rocker_port *rocker_port,
			       struct switchdev_obj_port_fdb *fdb,
			       switchdev_obj_dump_cb_t *cb)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_obj_fdb_dump)
1658
		return -EOPNOTSUPP;
1659 1660 1661 1662 1663 1664 1665 1666 1667
	return wops->port_obj_fdb_dump(rocker_port, fdb, cb);
}

static int rocker_world_port_master_linked(struct rocker_port *rocker_port,
					   struct net_device *master)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_master_linked)
1668
		return -EOPNOTSUPP;
1669 1670 1671 1672 1673 1674 1675 1676 1677
	return wops->port_master_linked(rocker_port, master);
}

static int rocker_world_port_master_unlinked(struct rocker_port *rocker_port,
					     struct net_device *master)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_master_unlinked)
1678
		return -EOPNOTSUPP;
1679 1680 1681 1682 1683 1684 1685 1686 1687
	return wops->port_master_unlinked(rocker_port, master);
}

static int rocker_world_port_neigh_update(struct rocker_port *rocker_port,
					  struct neighbour *n)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_neigh_update)
1688
		return -EOPNOTSUPP;
1689 1690 1691 1692 1693 1694 1695 1696 1697
	return wops->port_neigh_update(rocker_port, n);
}

static int rocker_world_port_neigh_destroy(struct rocker_port *rocker_port,
					   struct neighbour *n)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_neigh_destroy)
1698
		return -EOPNOTSUPP;
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
	return wops->port_neigh_destroy(rocker_port, n);
}

static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
					      const unsigned char *addr,
					      __be16 vlan_id)
{
	struct rocker_world_ops *wops = rocker_port->rocker->wops;

	if (!wops->port_ev_mac_vlan_seen)
1709
		return -EOPNOTSUPP;
1710 1711 1712
	return wops->port_ev_mac_vlan_seen(rocker_port, addr, vlan_id);
}

1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
static int rocker_world_fib4_add(struct rocker *rocker,
				 const struct fib_entry_notifier_info *fen_info)
{
	struct rocker_world_ops *wops = rocker->wops;

	if (!wops->fib4_add)
		return 0;
	return wops->fib4_add(rocker, fen_info);
}

static int rocker_world_fib4_del(struct rocker *rocker,
				 const struct fib_entry_notifier_info *fen_info)
{
	struct rocker_world_ops *wops = rocker->wops;

	if (!wops->fib4_del)
		return 0;
	return wops->fib4_del(rocker, fen_info);
}

static void rocker_world_fib4_abort(struct rocker *rocker)
{
	struct rocker_world_ops *wops = rocker->wops;

	if (wops->fib4_abort)
		wops->fib4_abort(rocker);
}

1741 1742 1743 1744 1745
/*****************
 * Net device ops
 *****************/

static int rocker_port_open(struct net_device *dev)
1746
{
1747 1748
	struct rocker_port *rocker_port = netdev_priv(dev);
	int err;
1749

1750 1751 1752
	err = rocker_port_dma_rings_init(rocker_port);
	if (err)
		return err;
1753

1754 1755 1756 1757 1758 1759 1760
	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;
	}
1761

1762 1763 1764 1765 1766 1767 1768
	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;
	}
1769

1770 1771 1772 1773 1774
	err = rocker_world_port_open(rocker_port);
	if (err) {
		netdev_err(rocker_port->dev, "cannot open port in world\n");
		goto err_world_port_open;
	}
1775

1776 1777 1778 1779 1780
	napi_enable(&rocker_port->napi_tx);
	napi_enable(&rocker_port->napi_rx);
	if (!dev->proto_down)
		rocker_port_set_enable(rocker_port, true);
	netif_start_queue(dev);
1781
	return 0;
1782 1783 1784 1785 1786 1787 1788 1789

err_world_port_open:
	free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
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;
1790 1791
}

1792
static int rocker_port_stop(struct net_device *dev)
1793
{
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
	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);
	rocker_world_port_stop(rocker_port);
	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);
1804 1805 1806 1807

	return 0;
}

1808 1809
static void rocker_tx_desc_frags_unmap(const struct rocker_port *rocker_port,
				       const struct rocker_desc_info *desc_info)
1810
{
1811 1812 1813 1814 1815
	const struct rocker *rocker = rocker_port->rocker;
	struct pci_dev *pdev = rocker->pdev;
	const struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1];
	struct rocker_tlv *attr;
	int rem;
1816

1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
	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) {
		const struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1];
		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);
	}
1836 1837
}

1838 1839 1840
static int rocker_tx_desc_frag_map_put(const struct rocker_port *rocker_port,
				       struct rocker_desc_info *desc_info,
				       char *buf, size_t buf_len)
1841
{
1842 1843 1844 1845
	const struct rocker *rocker = rocker_port->rocker;
	struct pci_dev *pdev = rocker->pdev;
	dma_addr_t dma_handle;
	struct rocker_tlv *frag;
1846

1847 1848 1849 1850 1851
	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;
1852
	}
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
	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);
1863
	return 0;
1864 1865 1866 1867 1868 1869

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

1872
static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev)
1873
{
1874 1875 1876 1877 1878 1879
	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;
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896

	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;
1897 1898 1899 1900 1901
	if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX) {
		err = skb_linearize(skb);
		if (err)
			goto unmap_frags;
	}
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928

	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);
1929 1930
	dev->stats.tx_dropped++;

1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
	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 已提交
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
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;
}

1978 1979 1980 1981 1982 1983 1984
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;

1985
	err = rocker_cmd_exec(rocker_port, false,
1986 1987
			      rocker_cmd_get_port_settings_prep, NULL,
			      rocker_cmd_get_port_settings_phys_name_proc,
1988
			      &name);
1989 1990 1991 1992

	return err ? -EOPNOTSUPP : 0;
}

1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
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;
}

2004 2005
static void rocker_port_neigh_destroy(struct net_device *dev,
				      struct neighbour *n)
2006 2007
{
	struct rocker_port *rocker_port = netdev_priv(n->dev);
2008
	int err;
2009

2010 2011 2012 2013
	err = rocker_world_port_neigh_destroy(rocker_port, n);
	if (err)
		netdev_warn(rocker_port->dev, "failed to handle neigh destroy (err %d)\n",
			    err);
2014 2015
}

S
Scott Feldman 已提交
2016 2017 2018 2019 2020
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 已提交
2021
	.ndo_change_mtu			= rocker_port_change_mtu,
2022
	.ndo_bridge_getlink		= switchdev_port_bridge_getlink,
2023
	.ndo_bridge_setlink		= switchdev_port_bridge_setlink,
2024
	.ndo_bridge_dellink		= switchdev_port_bridge_dellink,
2025 2026 2027
	.ndo_fdb_add			= switchdev_port_fdb_add,
	.ndo_fdb_del			= switchdev_port_fdb_del,
	.ndo_fdb_dump			= switchdev_port_fdb_dump,
2028
	.ndo_get_phys_port_name		= rocker_port_get_phys_port_name,
2029
	.ndo_change_proto_down		= rocker_port_change_proto_down,
2030
	.ndo_neigh_destroy		= rocker_port_neigh_destroy,
S
Scott Feldman 已提交
2031 2032 2033 2034 2035 2036
};

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

2037 2038
static int rocker_port_attr_get(struct net_device *dev,
				struct switchdev_attr *attr)
2039
{
2040 2041
	const struct rocker_port *rocker_port = netdev_priv(dev);
	const struct rocker *rocker = rocker_port->rocker;
2042
	int err = 0;
2043

2044
	switch (attr->id) {
2045
	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
2046 2047
		attr->u.ppid.id_len = sizeof(rocker->hw.id);
		memcpy(&attr->u.ppid.id, &rocker->hw.id, attr->u.ppid.id_len);
2048
		break;
2049
	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
2050 2051
		err = rocker_world_port_attr_bridge_flags_get(rocker_port,
							      &attr->u.brport_flags);
2052
		break;
2053 2054 2055 2056
	default:
		return -EOPNOTSUPP;
	}

2057
	return err;
2058 2059
}

2060
static int rocker_port_attr_set(struct net_device *dev,
2061
				const struct switchdev_attr *attr,
2062
				struct switchdev_trans *trans)
2063 2064 2065 2066 2067
{
	struct rocker_port *rocker_port = netdev_priv(dev);
	int err = 0;

	switch (attr->id) {
2068
	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
2069 2070 2071
		err = rocker_world_port_attr_stp_state_set(rocker_port,
							   attr->u.stp_state,
							   trans);
2072
		break;
2073
	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
2074 2075 2076
		err = rocker_world_port_attr_bridge_flags_set(rocker_port,
							      attr->u.brport_flags,
							      trans);
2077
		break;
2078
	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
2079 2080 2081
		err = rocker_world_port_attr_bridge_ageing_time_set(rocker_port,
								    attr->u.ageing_time,
								    trans);
2082
		break;
2083 2084 2085 2086 2087 2088
	default:
		err = -EOPNOTSUPP;
		break;
	}

	return err;
2089 2090
}

2091
static int rocker_port_obj_add(struct net_device *dev,
2092
			       const struct switchdev_obj *obj,
2093
			       struct switchdev_trans *trans)
2094 2095 2096 2097
{
	struct rocker_port *rocker_port = netdev_priv(dev);
	int err = 0;

2098
	switch (obj->id) {
2099
	case SWITCHDEV_OBJ_ID_PORT_VLAN:
2100 2101 2102
		err = rocker_world_port_obj_vlan_add(rocker_port,
						     SWITCHDEV_OBJ_PORT_VLAN(obj),
						     trans);
2103
		break;
2104
	case SWITCHDEV_OBJ_ID_PORT_FDB:
2105 2106 2107
		err = rocker_world_port_obj_fdb_add(rocker_port,
						    SWITCHDEV_OBJ_PORT_FDB(obj),
						    trans);
2108
		break;
2109 2110 2111 2112 2113 2114 2115 2116 2117
	default:
		err = -EOPNOTSUPP;
		break;
	}

	return err;
}

static int rocker_port_obj_del(struct net_device *dev,
2118
			       const struct switchdev_obj *obj)
2119 2120 2121 2122
{
	struct rocker_port *rocker_port = netdev_priv(dev);
	int err = 0;

2123
	switch (obj->id) {
2124
	case SWITCHDEV_OBJ_ID_PORT_VLAN:
2125 2126
		err = rocker_world_port_obj_vlan_del(rocker_port,
						     SWITCHDEV_OBJ_PORT_VLAN(obj));
2127
		break;
2128
	case SWITCHDEV_OBJ_ID_PORT_FDB:
2129 2130
		err = rocker_world_port_obj_fdb_del(rocker_port,
						    SWITCHDEV_OBJ_PORT_FDB(obj));
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
		break;
	default:
		err = -EOPNOTSUPP;
		break;
	}

	return err;
}

static int rocker_port_obj_dump(struct net_device *dev,
2141 2142
				struct switchdev_obj *obj,
				switchdev_obj_dump_cb_t *cb)
2143
{
2144
	const struct rocker_port *rocker_port = netdev_priv(dev);
2145 2146
	int err = 0;

2147
	switch (obj->id) {
2148
	case SWITCHDEV_OBJ_ID_PORT_FDB:
2149 2150 2151
		err = rocker_world_port_obj_fdb_dump(rocker_port,
						     SWITCHDEV_OBJ_PORT_FDB(obj),
						     cb);
2152
		break;
2153
	case SWITCHDEV_OBJ_ID_PORT_VLAN:
2154 2155 2156
		err = rocker_world_port_obj_vlan_dump(rocker_port,
						      SWITCHDEV_OBJ_PORT_VLAN(obj),
						      cb);
2157
		break;
2158 2159 2160 2161 2162 2163 2164 2165
	default:
		err = -EOPNOTSUPP;
		break;
	}

	return err;
}

J
Jiri Pirko 已提交
2166
static const struct switchdev_ops rocker_port_switchdev_ops = {
2167
	.switchdev_port_attr_get	= rocker_port_attr_get,
2168
	.switchdev_port_attr_set	= rocker_port_attr_set,
2169 2170
	.switchdev_port_obj_add		= rocker_port_obj_add,
	.switchdev_port_obj_del		= rocker_port_obj_del,
2171
	.switchdev_port_obj_dump	= rocker_port_obj_dump,
2172 2173
};

2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198
static int rocker_router_fib_event(struct notifier_block *nb,
				   unsigned long event, void *ptr)
{
	struct rocker *rocker = container_of(nb, struct rocker, fib_nb);
	struct fib_entry_notifier_info *fen_info = ptr;
	int err;

	switch (event) {
	case FIB_EVENT_ENTRY_ADD:
		err = rocker_world_fib4_add(rocker, fen_info);
		if (err)
			rocker_world_fib4_abort(rocker);
		else
		break;
	case FIB_EVENT_ENTRY_DEL:
		rocker_world_fib4_del(rocker, fen_info);
		break;
	case FIB_EVENT_RULE_ADD: /* fall through */
	case FIB_EVENT_RULE_DEL:
		rocker_world_fib4_abort(rocker);
		break;
	}
	return NOTIFY_DONE;
}

2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
/********************
 * 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));
}

2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259
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
2260
rocker_cmd_get_port_stats_prep(const struct rocker_port *rocker_port,
2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
			       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 已提交
2274 2275
	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT,
			       rocker_port->pport))
2276 2277 2278 2279 2280 2281 2282 2283
		return -EMSGSIZE;

	rocker_tlv_nest_end(desc_info, cmd_stats);

	return 0;
}

static int
2284
rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port *rocker_port,
2285
				       const struct rocker_desc_info *desc_info,
2286 2287
				       void *priv)
{
2288 2289 2290
	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 已提交
2291
	u32 pport;
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
	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 已提交
2303
	if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT])
2304 2305
		return -EIO;

S
Scott Feldman 已提交
2306 2307
	pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]);
	if (pport != rocker_port->pport)
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
		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)
{
2324
	return rocker_cmd_exec(rocker_port, false,
2325 2326
			       rocker_cmd_get_port_stats_prep, NULL,
			       rocker_cmd_get_port_stats_ethtool_proc,
2327
			       priv);
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
}

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;
	}
}

2353 2354 2355 2356 2357
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,
2358 2359 2360
	.get_strings		= rocker_port_get_strings,
	.get_ethtool_stats	= rocker_port_get_stats,
	.get_sset_count		= rocker_port_get_sset_count,
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
};

/*****************
 * 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);
2375 2376
	const struct rocker *rocker = rocker_port->rocker;
	const struct rocker_desc_info *desc_info;
2377 2378 2379 2380 2381
	u32 credits = 0;
	int err;

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

2384 2385 2386 2387 2388
		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);
2389 2390 2391 2392 2393

		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 已提交
2394
		} else {
2395
			rocker_port->dev->stats.tx_errors++;
S
Scott Feldman 已提交
2396
		}
2397 2398

		dev_kfree_skb_any(skb);
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410
		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;
}

2411 2412
static int rocker_port_rx_proc(const struct rocker *rocker,
			       const struct rocker_port *rocker_port,
2413 2414
			       struct rocker_desc_info *desc_info)
{
2415
	const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
2416 2417
	struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
	size_t rx_len;
2418
	u16 rx_flags = 0;
2419 2420 2421 2422 2423 2424 2425

	if (!skb)
		return -ENOENT;

	rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
	if (!attrs[ROCKER_TLV_RX_FRAG_LEN])
		return -EINVAL;
2426 2427
	if (attrs[ROCKER_TLV_RX_FLAGS])
		rx_flags = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FLAGS]);
2428 2429 2430 2431 2432 2433

	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);
2434

2435
	if (rx_flags & ROCKER_RX_FLAGS_FWD_OFFLOAD)
2436
		skb->offload_fwd_mark = 1;
2437

2438 2439 2440
	rocker_port->dev->stats.rx_packets++;
	rocker_port->dev->stats.rx_bytes += skb->len;

2441 2442
	netif_receive_skb(skb);

2443
	return rocker_dma_rx_ring_skb_alloc(rocker_port, desc_info);
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453
}

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);
2454
	const struct rocker *rocker = rocker_port->rocker;
2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473
	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);
		}
2474 2475 2476
		if (err)
			rocker_port->dev->stats.rx_errors++;

2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
		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
 *****************/

2494
static void rocker_carrier_init(const struct rocker_port *rocker_port)
2495
{
2496
	const struct rocker *rocker = rocker_port->rocker;
2497 2498 2499
	u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS);
	bool link_up;

S
Scott Feldman 已提交
2500
	link_up = link_status & (1 << rocker_port->pport);
2501 2502 2503 2504 2505 2506
	if (link_up)
		netif_carrier_on(rocker_port->dev);
	else
		netif_carrier_off(rocker_port->dev);
}

2507
static void rocker_remove_ports(struct rocker *rocker)
2508
{
2509
	struct rocker_port *rocker_port;
2510 2511
	int i;

2512 2513
	for (i = 0; i < rocker->port_count; i++) {
		rocker_port = rocker->ports[i];
2514 2515
		if (!rocker_port)
			continue;
2516
		rocker_world_port_fini(rocker_port);
2517
		unregister_netdev(rocker_port->dev);
2518
		rocker_world_port_post_fini(rocker_port);
2519
		free_netdev(rocker_port->dev);
2520
	}
2521
	rocker_world_fini(rocker);
2522 2523 2524
	kfree(rocker->ports);
}

2525
static void rocker_port_dev_addr_init(struct rocker_port *rocker_port)
2526
{
2527
	const struct rocker *rocker = rocker_port->rocker;
2528
	const struct pci_dev *pdev = rocker->pdev;
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540
	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)
{
2541
	const struct pci_dev *pdev = rocker->pdev;
2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552
	struct rocker_port *rocker_port;
	struct net_device *dev;
	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 已提交
2553
	rocker_port->pport = port_number + 1;
2554

2555 2556 2557 2558 2559 2560
	err = rocker_world_check_init(rocker_port);
	if (err) {
		dev_err(&pdev->dev, "world init failed\n");
		goto err_world_check_init;
	}

2561
	rocker_port_dev_addr_init(rocker_port);
2562 2563
	dev->netdev_ops = &rocker_port_netdev_ops;
	dev->ethtool_ops = &rocker_port_ethtool_ops;
J
Jiri Pirko 已提交
2564
	dev->switchdev_ops = &rocker_port_switchdev_ops;
E
Eric Dumazet 已提交
2565
	netif_tx_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx,
2566
			  NAPI_POLL_WEIGHT);
2567 2568 2569 2570
	netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx,
		       NAPI_POLL_WEIGHT);
	rocker_carrier_init(rocker_port);

2571
	dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_SG;
2572

2573 2574 2575 2576 2577
	err = rocker_world_port_pre_init(rocker_port);
	if (err) {
		dev_err(&pdev->dev, "port world pre-init failed\n");
		goto err_world_port_pre_init;
	}
2578 2579 2580 2581 2582 2583 2584
	err = register_netdev(dev);
	if (err) {
		dev_err(&pdev->dev, "register_netdev failed\n");
		goto err_register_netdev;
	}
	rocker->ports[port_number] = rocker_port;

2585 2586 2587 2588 2589 2590
	err = rocker_world_port_init(rocker_port);
	if (err) {
		dev_err(&pdev->dev, "port world init failed\n");
		goto err_world_port_init;
	}

2591 2592
	return 0;

2593
err_world_port_init:
2594
	rocker->ports[port_number] = NULL;
2595
	unregister_netdev(dev);
2596
err_register_netdev:
2597 2598 2599
	rocker_world_port_post_fini(rocker_port);
err_world_port_pre_init:
err_world_check_init:
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
	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;
2611
	rocker->ports = kzalloc(alloc_size, GFP_KERNEL);
2612 2613
	if (!rocker->ports)
		return -ENOMEM;
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
	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;
}

2660
static void rocker_msix_fini(const struct rocker *rocker)
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703
{
	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");
2704
		err = -EINVAL;
2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763
		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);

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

2764 2765 2766
	rocker->fib_nb.notifier_call = rocker_router_fib_event;
	register_fib_notifier(&rocker->fib_nb);

2767 2768
	dev_info(&pdev->dev, "Rocker switch with id %*phN\n",
		 (int)sizeof(rocker->hw.id), &rocker->hw.id);
2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797

	return 0;

err_probe_ports:
	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);

2798
	unregister_fib_notifier(&rocker->fib_nb);
2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817
	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,
};

2818 2819 2820 2821
/************************************
 * Net device notifier event handler
 ************************************/

2822
static bool rocker_port_dev_check(const struct net_device *dev)
2823 2824 2825 2826
{
	return dev->netdev_ops == &rocker_port_netdev_ops;
}

2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857
static bool rocker_port_dev_check_under(const struct net_device *dev,
					struct rocker *rocker)
{
	struct rocker_port *rocker_port;

	if (!rocker_port_dev_check(dev))
		return false;

	rocker_port = netdev_priv(dev);
	if (rocker_port->rocker != rocker)
		return false;

	return true;
}

struct rocker_port *rocker_port_dev_lower_find(struct net_device *dev,
					       struct rocker *rocker)
{
	struct net_device *lower_dev;
	struct list_head *iter;

	if (rocker_port_dev_check_under(dev, rocker))
		return netdev_priv(dev);

	netdev_for_each_all_lower_dev(dev, lower_dev, iter) {
		if (rocker_port_dev_check_under(lower_dev, rocker))
			return netdev_priv(lower_dev);
	}
	return NULL;
}

2858 2859 2860
static int rocker_netdevice_event(struct notifier_block *unused,
				  unsigned long event, void *ptr)
{
J
Jiri Pirko 已提交
2861 2862 2863
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
	struct netdev_notifier_changeupper_info *info;
	struct rocker_port *rocker_port;
2864 2865
	int err;

J
Jiri Pirko 已提交
2866 2867 2868
	if (!rocker_port_dev_check(dev))
		return NOTIFY_DONE;

2869 2870
	switch (event) {
	case NETDEV_CHANGEUPPER:
J
Jiri Pirko 已提交
2871 2872 2873 2874 2875
		info = ptr;
		if (!info->master)
			goto out;
		rocker_port = netdev_priv(dev);
		if (info->linking) {
2876 2877 2878 2879 2880
			err = rocker_world_port_master_linked(rocker_port,
							      info->upper_dev);
			if (err)
				netdev_warn(dev, "failed to reflect master linked (err %d)\n",
					    err);
J
Jiri Pirko 已提交
2881
		} else {
2882 2883 2884 2885 2886
			err = rocker_world_port_master_unlinked(rocker_port,
								info->upper_dev);
			if (err)
				netdev_warn(dev, "failed to reflect master unlinked (err %d)\n",
					    err);
J
Jiri Pirko 已提交
2887
		}
2888
	}
J
Jiri Pirko 已提交
2889
out:
2890 2891 2892 2893 2894 2895 2896
	return NOTIFY_DONE;
}

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

2897 2898 2899 2900 2901 2902 2903
/************************************
 * Net event notifier event handler
 ************************************/

static int rocker_netevent_event(struct notifier_block *unused,
				 unsigned long event, void *ptr)
{
2904
	struct rocker_port *rocker_port;
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915
	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;
2916 2917 2918 2919 2920
		rocker_port = netdev_priv(dev);
		err = rocker_world_port_neigh_update(rocker_port, n);
		if (err)
			netdev_warn(dev, "failed to handle neigh update (err %d)\n",
				    err);
2921 2922 2923 2924 2925 2926 2927 2928 2929 2930
		break;
	}

	return NOTIFY_DONE;
}

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

2931 2932 2933 2934 2935 2936
/***********************
 * Module init and exit
 ***********************/

static int __init rocker_module_init(void)
{
2937 2938 2939
	int err;

	register_netdevice_notifier(&rocker_netdevice_nb);
2940
	register_netevent_notifier(&rocker_netevent_nb);
2941 2942 2943 2944 2945 2946
	err = pci_register_driver(&rocker_pci_driver);
	if (err)
		goto err_pci_register_driver;
	return 0;

err_pci_register_driver:
2947
	unregister_netevent_notifier(&rocker_netevent_nb);
2948 2949
	unregister_netdevice_notifier(&rocker_netdevice_nb);
	return err;
2950 2951 2952 2953
}

static void __exit rocker_module_exit(void)
{
2954
	unregister_netevent_notifier(&rocker_netevent_nb);
2955
	unregister_netdevice_notifier(&rocker_netdevice_nb);
2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966
	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);