chip.c 90.2 KB
Newer Older
1
/*
2 3
 * Marvell 88e6xxx Ethernet switch single-chip support
 *
4 5
 * Copyright (c) 2008 Marvell Semiconductor
 *
6 7 8
 * Copyright (c) 2015 CMC Electronics, Inc.
 *	Added support for VLAN Table Unit operations
 *
9 10
 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
 *
11 12 13 14 15 16
 * 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.
 */

17
#include <linux/delay.h>
18
#include <linux/etherdevice.h>
19
#include <linux/ethtool.h>
20
#include <linux/if_bridge.h>
21
#include <linux/jiffies.h>
22
#include <linux/list.h>
23
#include <linux/mdio.h>
24
#include <linux/module.h>
25
#include <linux/of_device.h>
26
#include <linux/of_mdio.h>
27
#include <linux/netdevice.h>
28
#include <linux/gpio/consumer.h>
29
#include <linux/phy.h>
30
#include <net/dsa.h>
31
#include <net/switchdev.h>
32

33
#include "mv88e6xxx.h"
34
#include "global2.h"
35

36
static void assert_reg_lock(struct mv88e6xxx_chip *chip)
37
{
38 39
	if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
		dev_err(chip->dev, "Switch registers lock not held!\n");
40 41 42 43
		dump_stack();
	}
}

44 45 46 47 48 49 50 51 52 53
/* The switch ADDR[4:1] configuration pins define the chip SMI device address
 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
 *
 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
 * is the only device connected to the SMI master. In this mode it responds to
 * all 32 possible SMI addresses, and thus maps directly the internal devices.
 *
 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
 * multiple devices to share the SMI interface. In this mode it responds to only
 * 2 registers, used to indirectly access the internal SMI devices.
54
 */
55

56
static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
57 58
			      int addr, int reg, u16 *val)
{
59
	if (!chip->smi_ops)
60 61
		return -EOPNOTSUPP;

62
	return chip->smi_ops->read(chip, addr, reg, val);
63 64
}

65
static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
66 67
			       int addr, int reg, u16 val)
{
68
	if (!chip->smi_ops)
69 70
		return -EOPNOTSUPP;

71
	return chip->smi_ops->write(chip, addr, reg, val);
72 73
}

74
static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
75 76 77 78
					  int addr, int reg, u16 *val)
{
	int ret;

79
	ret = mdiobus_read_nested(chip->bus, addr, reg);
80 81 82 83 84 85 86 87
	if (ret < 0)
		return ret;

	*val = ret & 0xffff;

	return 0;
}

88
static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
89 90 91 92
					   int addr, int reg, u16 val)
{
	int ret;

93
	ret = mdiobus_write_nested(chip->bus, addr, reg, val);
94 95 96 97 98 99 100 101 102 103 104
	if (ret < 0)
		return ret;

	return 0;
}

static const struct mv88e6xxx_ops mv88e6xxx_smi_single_chip_ops = {
	.read = mv88e6xxx_smi_single_chip_read,
	.write = mv88e6xxx_smi_single_chip_write,
};

105
static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
106 107 108 109 110
{
	int ret;
	int i;

	for (i = 0; i < 16; i++) {
111
		ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
112 113 114
		if (ret < 0)
			return ret;

115
		if ((ret & SMI_CMD_BUSY) == 0)
116 117 118 119 120 121
			return 0;
	}

	return -ETIMEDOUT;
}

122
static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
123
					 int addr, int reg, u16 *val)
124 125 126
{
	int ret;

127
	/* Wait for the bus to become free. */
128
	ret = mv88e6xxx_smi_multi_chip_wait(chip);
129 130 131
	if (ret < 0)
		return ret;

132
	/* Transmit the read command. */
133
	ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
134
				   SMI_CMD_OP_22_READ | (addr << 5) | reg);
135 136 137
	if (ret < 0)
		return ret;

138
	/* Wait for the read command to complete. */
139
	ret = mv88e6xxx_smi_multi_chip_wait(chip);
140 141 142
	if (ret < 0)
		return ret;

143
	/* Read the data. */
144
	ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
145 146 147
	if (ret < 0)
		return ret;

148
	*val = ret & 0xffff;
149

150
	return 0;
151 152
}

153
static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
154
					  int addr, int reg, u16 val)
155 156 157
{
	int ret;

158
	/* Wait for the bus to become free. */
159
	ret = mv88e6xxx_smi_multi_chip_wait(chip);
160 161 162
	if (ret < 0)
		return ret;

163
	/* Transmit the data to write. */
164
	ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
165 166 167
	if (ret < 0)
		return ret;

168
	/* Transmit the write command. */
169
	ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
170
				   SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
171 172 173
	if (ret < 0)
		return ret;

174
	/* Wait for the write command to complete. */
175
	ret = mv88e6xxx_smi_multi_chip_wait(chip);
176 177 178 179 180 181
	if (ret < 0)
		return ret;

	return 0;
}

182 183 184 185 186
static const struct mv88e6xxx_ops mv88e6xxx_smi_multi_chip_ops = {
	.read = mv88e6xxx_smi_multi_chip_read,
	.write = mv88e6xxx_smi_multi_chip_write,
};

187
int mv88e6xxx_read(struct mv88e6xxx_chip *chip, int addr, int reg, u16 *val)
188 189 190
{
	int err;

191
	assert_reg_lock(chip);
192

193
	err = mv88e6xxx_smi_read(chip, addr, reg, val);
194 195 196
	if (err)
		return err;

197
	dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
198 199 200 201 202
		addr, reg, *val);

	return 0;
}

203
int mv88e6xxx_write(struct mv88e6xxx_chip *chip, int addr, int reg, u16 val)
204
{
205 206
	int err;

207
	assert_reg_lock(chip);
208

209
	err = mv88e6xxx_smi_write(chip, addr, reg, val);
210 211 212
	if (err)
		return err;

213
	dev_dbg(chip->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
214 215
		addr, reg, val);

216 217 218
	return 0;
}

219 220
static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port, int reg,
			       u16 *val)
221 222 223 224 225 226
{
	int addr = chip->info->port_base_addr + port;

	return mv88e6xxx_read(chip, addr, reg, val);
}

227 228
static int mv88e6xxx_port_write(struct mv88e6xxx_chip *chip, int port, int reg,
				u16 val)
229 230 231 232 233 234
{
	int addr = chip->info->port_base_addr + port;

	return mv88e6xxx_write(chip, addr, reg, val);
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
static int mv88e6xxx_phy_read(struct mv88e6xxx_chip *chip, int phy,
			      int reg, u16 *val)
{
	int addr = phy; /* PHY devices addresses start at 0x0 */

	if (!chip->phy_ops)
		return -EOPNOTSUPP;

	return chip->phy_ops->read(chip, addr, reg, val);
}

static int mv88e6xxx_phy_write(struct mv88e6xxx_chip *chip, int phy,
			       int reg, u16 val)
{
	int addr = phy; /* PHY devices addresses start at 0x0 */

	if (!chip->phy_ops)
		return -EOPNOTSUPP;

	return chip->phy_ops->write(chip, addr, reg, val);
}

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 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 316 317 318 319 320 321 322 323 324
static int mv88e6xxx_phy_page_get(struct mv88e6xxx_chip *chip, int phy, u8 page)
{
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_PHY_PAGE))
		return -EOPNOTSUPP;

	return mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
}

static void mv88e6xxx_phy_page_put(struct mv88e6xxx_chip *chip, int phy)
{
	int err;

	/* Restore PHY page Copper 0x0 for access via the registered MDIO bus */
	err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, PHY_PAGE_COPPER);
	if (unlikely(err)) {
		dev_err(chip->dev, "failed to restore PHY %d page Copper (%d)\n",
			phy, err);
	}
}

static int mv88e6xxx_phy_page_read(struct mv88e6xxx_chip *chip, int phy,
				   u8 page, int reg, u16 *val)
{
	int err;

	/* There is no paging for registers 22 */
	if (reg == PHY_PAGE)
		return -EINVAL;

	err = mv88e6xxx_phy_page_get(chip, phy, page);
	if (!err) {
		err = mv88e6xxx_phy_read(chip, phy, reg, val);
		mv88e6xxx_phy_page_put(chip, phy);
	}

	return err;
}

static int mv88e6xxx_phy_page_write(struct mv88e6xxx_chip *chip, int phy,
				    u8 page, int reg, u16 val)
{
	int err;

	/* There is no paging for registers 22 */
	if (reg == PHY_PAGE)
		return -EINVAL;

	err = mv88e6xxx_phy_page_get(chip, phy, page);
	if (!err) {
		err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
		mv88e6xxx_phy_page_put(chip, phy);
	}

	return err;
}

static int mv88e6xxx_serdes_read(struct mv88e6xxx_chip *chip, int reg, u16 *val)
{
	return mv88e6xxx_phy_page_read(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
				       reg, val);
}

static int mv88e6xxx_serdes_write(struct mv88e6xxx_chip *chip, int reg, u16 val)
{
	return mv88e6xxx_phy_page_write(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
					reg, val);
}

325
int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int addr, int reg, u16 mask)
326
{
327
	int i;
328

329
	for (i = 0; i < 16; i++) {
330 331 332 333 334 335 336 337 338 339 340 341 342
		u16 val;
		int err;

		err = mv88e6xxx_read(chip, addr, reg, &val);
		if (err)
			return err;

		if (!(val & mask))
			return 0;

		usleep_range(1000, 2000);
	}

343
	dev_err(chip->dev, "Timeout while waiting for switch\n");
344 345 346
	return -ETIMEDOUT;
}

347
/* Indirect write to single pointer-data register with an Update bit */
348
int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg, u16 update)
349 350
{
	u16 val;
351
	int err;
352 353

	/* Wait until the previous operation is completed */
354 355 356
	err = mv88e6xxx_wait(chip, addr, reg, BIT(15));
	if (err)
		return err;
357 358 359 360 361 362 363

	/* Set the Update bit to trigger a write operation */
	val = BIT(15) | update;

	return mv88e6xxx_write(chip, addr, reg, val);
}

364
static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
365 366 367 368
{
	u16 val;
	int err;

369
	err = mv88e6xxx_read(chip, addr, reg, &val);
370 371 372 373 374 375
	if (err)
		return err;

	return val;
}

376
static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
377 378
				int reg, u16 val)
{
379
	return mv88e6xxx_write(chip, addr, reg, val);
380 381
}

382
static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
383 384
{
	int ret;
385
	int i;
386

387
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
388 389 390
	if (ret < 0)
		return ret;

391
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
392
				   ret & ~GLOBAL_CONTROL_PPU_ENABLE);
393 394
	if (ret)
		return ret;
395

396
	for (i = 0; i < 16; i++) {
397
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
398 399 400
		if (ret < 0)
			return ret;

401
		usleep_range(1000, 2000);
402 403
		if ((ret & GLOBAL_STATUS_PPU_MASK) !=
		    GLOBAL_STATUS_PPU_POLLING)
404
			return 0;
405 406 407 408 409
	}

	return -ETIMEDOUT;
}

410
static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
411
{
412
	int ret, err, i;
413

414
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
415 416 417
	if (ret < 0)
		return ret;

418
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
419
				   ret | GLOBAL_CONTROL_PPU_ENABLE);
420 421
	if (err)
		return err;
422

423
	for (i = 0; i < 16; i++) {
424
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
425 426 427
		if (ret < 0)
			return ret;

428
		usleep_range(1000, 2000);
429 430
		if ((ret & GLOBAL_STATUS_PPU_MASK) ==
		    GLOBAL_STATUS_PPU_POLLING)
431
			return 0;
432 433 434 435 436 437 438
	}

	return -ETIMEDOUT;
}

static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
{
439
	struct mv88e6xxx_chip *chip;
440

441
	chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
442

443
	mutex_lock(&chip->reg_lock);
444

445 446 447 448
	if (mutex_trylock(&chip->ppu_mutex)) {
		if (mv88e6xxx_ppu_enable(chip) == 0)
			chip->ppu_disabled = 0;
		mutex_unlock(&chip->ppu_mutex);
449
	}
450

451
	mutex_unlock(&chip->reg_lock);
452 453 454 455
}

static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
{
456
	struct mv88e6xxx_chip *chip = (void *)_ps;
457

458
	schedule_work(&chip->ppu_work);
459 460
}

461
static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
462 463 464
{
	int ret;

465
	mutex_lock(&chip->ppu_mutex);
466

467
	/* If the PHY polling unit is enabled, disable it so that
468 469 470 471
	 * we can access the PHY registers.  If it was already
	 * disabled, cancel the timer that is going to re-enable
	 * it.
	 */
472 473
	if (!chip->ppu_disabled) {
		ret = mv88e6xxx_ppu_disable(chip);
474
		if (ret < 0) {
475
			mutex_unlock(&chip->ppu_mutex);
476 477
			return ret;
		}
478
		chip->ppu_disabled = 1;
479
	} else {
480
		del_timer(&chip->ppu_timer);
481
		ret = 0;
482 483 484 485 486
	}

	return ret;
}

487
static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
488
{
489
	/* Schedule a timer to re-enable the PHY polling unit. */
490 491
	mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
	mutex_unlock(&chip->ppu_mutex);
492 493
}

494
static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
495
{
496 497 498 499 500
	mutex_init(&chip->ppu_mutex);
	INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
	init_timer(&chip->ppu_timer);
	chip->ppu_timer.data = (unsigned long)chip;
	chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
501 502
}

503 504 505 506 507
static void mv88e6xxx_ppu_state_destroy(struct mv88e6xxx_chip *chip)
{
	del_timer_sync(&chip->ppu_timer);
}

508 509
static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
				  int reg, u16 *val)
510
{
511
	int err;
512

513 514 515
	err = mv88e6xxx_ppu_access_get(chip);
	if (!err) {
		err = mv88e6xxx_read(chip, addr, reg, val);
516
		mv88e6xxx_ppu_access_put(chip);
517 518
	}

519
	return err;
520 521
}

522 523
static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
				   int reg, u16 val)
524
{
525
	int err;
526

527 528 529
	err = mv88e6xxx_ppu_access_get(chip);
	if (!err) {
		err = mv88e6xxx_write(chip, addr, reg, val);
530
		mv88e6xxx_ppu_access_put(chip);
531 532
	}

533
	return err;
534 535
}

536 537 538 539 540
static const struct mv88e6xxx_ops mv88e6xxx_phy_ppu_ops = {
	.read = mv88e6xxx_phy_ppu_read,
	.write = mv88e6xxx_phy_ppu_write,
};

541
static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
542
{
543
	return chip->info->family == MV88E6XXX_FAMILY_6065;
544 545
}

546
static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
547
{
548
	return chip->info->family == MV88E6XXX_FAMILY_6095;
549 550
}

551
static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
552
{
553
	return chip->info->family == MV88E6XXX_FAMILY_6097;
554 555
}

556
static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
557
{
558
	return chip->info->family == MV88E6XXX_FAMILY_6165;
559 560
}

561
static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
562
{
563
	return chip->info->family == MV88E6XXX_FAMILY_6185;
564 565
}

566
static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
567
{
568
	return chip->info->family == MV88E6XXX_FAMILY_6320;
569 570
}

571
static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
572
{
573
	return chip->info->family == MV88E6XXX_FAMILY_6351;
574 575
}

576
static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
577
{
578
	return chip->info->family == MV88E6XXX_FAMILY_6352;
579 580
}

581
static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
582
{
583
	return chip->info->num_databases;
584 585
}

586
static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
587 588
{
	/* Does the device have dedicated FID registers for ATU and VTU ops? */
589 590
	if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
	    mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
591 592 593 594 595
		return true;

	return false;
}

596 597 598 599
/* We expect the switch to perform auto negotiation if there is a real
 * phy. However, in the case of a fixed link phy, we force the port
 * settings from the fixed link settings.
 */
600 601
static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
				  struct phy_device *phydev)
602
{
V
Vivien Didelot 已提交
603
	struct mv88e6xxx_chip *chip = ds->priv;
604 605
	u16 reg;
	int err;
606 607 608 609

	if (!phy_is_pseudo_fixed_link(phydev))
		return;

610
	mutex_lock(&chip->reg_lock);
611

612 613
	err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
	if (err)
614 615
		goto out;

616 617 618 619 620
	reg &= ~(PORT_PCS_CTRL_LINK_UP |
		 PORT_PCS_CTRL_FORCE_LINK |
		 PORT_PCS_CTRL_DUPLEX_FULL |
		 PORT_PCS_CTRL_FORCE_DUPLEX |
		 PORT_PCS_CTRL_UNFORCED);
621 622 623

	reg |= PORT_PCS_CTRL_FORCE_LINK;
	if (phydev->link)
624
		reg |= PORT_PCS_CTRL_LINK_UP;
625

626
	if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
		goto out;

	switch (phydev->speed) {
	case SPEED_1000:
		reg |= PORT_PCS_CTRL_1000;
		break;
	case SPEED_100:
		reg |= PORT_PCS_CTRL_100;
		break;
	case SPEED_10:
		reg |= PORT_PCS_CTRL_10;
		break;
	default:
		pr_info("Unknown speed");
		goto out;
	}

	reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
	if (phydev->duplex == DUPLEX_FULL)
		reg |= PORT_PCS_CTRL_DUPLEX_FULL;

648 649
	if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
	    (port >= chip->info->num_ports - 2)) {
650 651 652 653 654 655 656 657
		if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
			reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
		if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
			reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
		if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
			reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
				PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
	}
658
	mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
659 660

out:
661
	mutex_unlock(&chip->reg_lock);
662 663
}

664
static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
665 666 667 668 669
{
	int ret;
	int i;

	for (i = 0; i < 10; i++) {
670
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
671
		if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
672 673 674 675 676 677
			return 0;
	}

	return -ETIMEDOUT;
}

678
static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
679 680 681
{
	int ret;

682
	if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
683 684
		port = (port + 1) << 5;

685
	/* Snapshot the hardware statistics counters for this port. */
686
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
687 688 689 690
				   GLOBAL_STATS_OP_CAPTURE_PORT |
				   GLOBAL_STATS_OP_HIST_RX_TX | port);
	if (ret < 0)
		return ret;
691

692
	/* Wait for the snapshotting to complete. */
693
	ret = _mv88e6xxx_stats_wait(chip);
694 695 696 697 698 699
	if (ret < 0)
		return ret;

	return 0;
}

700
static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
701
				  int stat, u32 *val)
702 703 704 705 706 707
{
	u32 _val;
	int ret;

	*val = 0;

708
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
709 710
				   GLOBAL_STATS_OP_READ_CAPTURED |
				   GLOBAL_STATS_OP_HIST_RX_TX | stat);
711 712 713
	if (ret < 0)
		return;

714
	ret = _mv88e6xxx_stats_wait(chip);
715 716 717
	if (ret < 0)
		return;

718
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
719 720 721 722 723
	if (ret < 0)
		return;

	_val = ret << 16;

724
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
725 726 727 728 729 730
	if (ret < 0)
		return;

	*val = _val | ret;
}

731
static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
	{ "in_good_octets",	8, 0x00, BANK0, },
	{ "in_bad_octets",	4, 0x02, BANK0, },
	{ "in_unicast",		4, 0x04, BANK0, },
	{ "in_broadcasts",	4, 0x06, BANK0, },
	{ "in_multicasts",	4, 0x07, BANK0, },
	{ "in_pause",		4, 0x16, BANK0, },
	{ "in_undersize",	4, 0x18, BANK0, },
	{ "in_fragments",	4, 0x19, BANK0, },
	{ "in_oversize",	4, 0x1a, BANK0, },
	{ "in_jabber",		4, 0x1b, BANK0, },
	{ "in_rx_error",	4, 0x1c, BANK0, },
	{ "in_fcs_error",	4, 0x1d, BANK0, },
	{ "out_octets",		8, 0x0e, BANK0, },
	{ "out_unicast",	4, 0x10, BANK0, },
	{ "out_broadcasts",	4, 0x13, BANK0, },
	{ "out_multicasts",	4, 0x12, BANK0, },
	{ "out_pause",		4, 0x15, BANK0, },
	{ "excessive",		4, 0x11, BANK0, },
	{ "collisions",		4, 0x1e, BANK0, },
	{ "deferred",		4, 0x05, BANK0, },
	{ "single",		4, 0x14, BANK0, },
	{ "multiple",		4, 0x17, BANK0, },
	{ "out_fcs_error",	4, 0x03, BANK0, },
	{ "late",		4, 0x1f, BANK0, },
	{ "hist_64bytes",	4, 0x08, BANK0, },
	{ "hist_65_127bytes",	4, 0x09, BANK0, },
	{ "hist_128_255bytes",	4, 0x0a, BANK0, },
	{ "hist_256_511bytes",	4, 0x0b, BANK0, },
	{ "hist_512_1023bytes", 4, 0x0c, BANK0, },
	{ "hist_1024_max_bytes", 4, 0x0d, BANK0, },
	{ "sw_in_discards",	4, 0x10, PORT, },
	{ "sw_in_filtered",	2, 0x12, PORT, },
	{ "sw_out_filtered",	2, 0x13, PORT, },
	{ "in_discards",	4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "in_filtered",	4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "in_accepted",	4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "in_bad_accepted",	4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "tcam_counter_0",	4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "tcam_counter_1",	4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "tcam_counter_2",	4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "tcam_counter_3",	4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "in_da_unknown",	4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "in_management",	4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "out_queue_0",	4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "out_queue_1",	4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "out_queue_2",	4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "out_queue_3",	4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "out_queue_4",	4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "out_queue_5",	4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "out_queue_6",	4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "out_queue_7",	4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "out_cut_through",	4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "out_octets_a",	4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "out_octets_b",	4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
	{ "out_management",	4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
791 792
};

793
static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
794
			       struct mv88e6xxx_hw_stat *stat)
795
{
796 797
	switch (stat->type) {
	case BANK0:
798
		return true;
799
	case BANK1:
800
		return mv88e6xxx_6320_family(chip);
801
	case PORT:
802 803 804 805 806 807
		return mv88e6xxx_6095_family(chip) ||
			mv88e6xxx_6185_family(chip) ||
			mv88e6xxx_6097_family(chip) ||
			mv88e6xxx_6165_family(chip) ||
			mv88e6xxx_6351_family(chip) ||
			mv88e6xxx_6352_family(chip);
808
	}
809
	return false;
810 811
}

812
static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
813
					    struct mv88e6xxx_hw_stat *s,
814 815 816 817
					    int port)
{
	u32 low;
	u32 high = 0;
818 819
	int err;
	u16 reg;
820 821
	u64 value;

822 823
	switch (s->type) {
	case PORT:
824 825
		err = mv88e6xxx_port_read(chip, port, s->reg, &reg);
		if (err)
826 827
			return UINT64_MAX;

828
		low = reg;
829
		if (s->sizeof_stat == 4) {
830 831
			err = mv88e6xxx_port_read(chip, port, s->reg + 1, &reg);
			if (err)
832
				return UINT64_MAX;
833
			high = reg;
834
		}
835 836 837
		break;
	case BANK0:
	case BANK1:
838
		_mv88e6xxx_stats_read(chip, s->reg, &low);
839
		if (s->sizeof_stat == 8)
840
			_mv88e6xxx_stats_read(chip, s->reg + 1, &high);
841 842 843 844 845
	}
	value = (((u64)high) << 16) | low;
	return value;
}

846 847
static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
				  uint8_t *data)
848
{
V
Vivien Didelot 已提交
849
	struct mv88e6xxx_chip *chip = ds->priv;
850 851
	struct mv88e6xxx_hw_stat *stat;
	int i, j;
852

853 854
	for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
		stat = &mv88e6xxx_hw_stats[i];
855
		if (mv88e6xxx_has_stat(chip, stat)) {
856 857 858 859
			memcpy(data + j * ETH_GSTRING_LEN, stat->string,
			       ETH_GSTRING_LEN);
			j++;
		}
860
	}
861 862
}

863
static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
864
{
V
Vivien Didelot 已提交
865
	struct mv88e6xxx_chip *chip = ds->priv;
866 867 868 869 870
	struct mv88e6xxx_hw_stat *stat;
	int i, j;

	for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
		stat = &mv88e6xxx_hw_stats[i];
871
		if (mv88e6xxx_has_stat(chip, stat))
872 873 874
			j++;
	}
	return j;
875 876
}

877 878
static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
					uint64_t *data)
879
{
V
Vivien Didelot 已提交
880
	struct mv88e6xxx_chip *chip = ds->priv;
881 882 883 884
	struct mv88e6xxx_hw_stat *stat;
	int ret;
	int i, j;

885
	mutex_lock(&chip->reg_lock);
886

887
	ret = _mv88e6xxx_stats_snapshot(chip, port);
888
	if (ret < 0) {
889
		mutex_unlock(&chip->reg_lock);
890 891 892 893
		return;
	}
	for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
		stat = &mv88e6xxx_hw_stats[i];
894 895
		if (mv88e6xxx_has_stat(chip, stat)) {
			data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
896 897 898 899
			j++;
		}
	}

900
	mutex_unlock(&chip->reg_lock);
901 902
}

903
static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
904 905 906 907
{
	return 32 * sizeof(u16);
}

908 909
static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
			       struct ethtool_regs *regs, void *_p)
910
{
V
Vivien Didelot 已提交
911
	struct mv88e6xxx_chip *chip = ds->priv;
912 913
	int err;
	u16 reg;
914 915 916 917 918 919 920
	u16 *p = _p;
	int i;

	regs->version = 0;

	memset(p, 0xff, 32 * sizeof(u16));

921
	mutex_lock(&chip->reg_lock);
922

923 924
	for (i = 0; i < 32; i++) {

925 926 927
		err = mv88e6xxx_port_read(chip, port, i, &reg);
		if (!err)
			p[i] = reg;
928
	}
929

930
	mutex_unlock(&chip->reg_lock);
931 932
}

933
static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
934
{
935 936
	return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
			      GLOBAL_ATU_OP_BUSY);
937 938
}

939 940
static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
			     struct ethtool_eee *e)
941
{
V
Vivien Didelot 已提交
942
	struct mv88e6xxx_chip *chip = ds->priv;
943 944
	u16 reg;
	int err;
945

946
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
947 948
		return -EOPNOTSUPP;

949
	mutex_lock(&chip->reg_lock);
950

951 952
	err = mv88e6xxx_phy_read(chip, port, 16, &reg);
	if (err)
953
		goto out;
954 955 956 957

	e->eee_enabled = !!(reg & 0x0200);
	e->tx_lpi_enabled = !!(reg & 0x0100);

958
	err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
959
	if (err)
960
		goto out;
961

962
	e->eee_active = !!(reg & PORT_STATUS_EEE);
963
out:
964
	mutex_unlock(&chip->reg_lock);
965 966

	return err;
967 968
}

969 970
static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
			     struct phy_device *phydev, struct ethtool_eee *e)
971
{
V
Vivien Didelot 已提交
972
	struct mv88e6xxx_chip *chip = ds->priv;
973 974
	u16 reg;
	int err;
975

976
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
977 978
		return -EOPNOTSUPP;

979
	mutex_lock(&chip->reg_lock);
980

981 982
	err = mv88e6xxx_phy_read(chip, port, 16, &reg);
	if (err)
983 984
		goto out;

985
	reg &= ~0x0300;
986 987 988 989 990
	if (e->eee_enabled)
		reg |= 0x0200;
	if (e->tx_lpi_enabled)
		reg |= 0x0100;

991
	err = mv88e6xxx_phy_write(chip, port, 16, reg);
992
out:
993
	mutex_unlock(&chip->reg_lock);
994

995
	return err;
996 997
}

998
static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
999 1000 1001
{
	int ret;

1002 1003 1004
	if (mv88e6xxx_has_fid_reg(chip)) {
		ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
					   fid);
1005 1006
		if (ret < 0)
			return ret;
1007
	} else if (mv88e6xxx_num_databases(chip) == 256) {
1008
		/* ATU DBNum[7:4] are located in ATU Control 15:12 */
1009
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
1010 1011 1012
		if (ret < 0)
			return ret;

1013
		ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
1014 1015 1016 1017 1018 1019 1020
					   (ret & 0xfff) |
					   ((fid << 8) & 0xf000));
		if (ret < 0)
			return ret;

		/* ATU DBNum[3:0] are located in ATU Operation 3:0 */
		cmd |= fid & 0xf;
1021 1022
	}

1023
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
1024 1025 1026
	if (ret < 0)
		return ret;

1027
	return _mv88e6xxx_atu_wait(chip);
1028 1029
}

1030
static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
				     struct mv88e6xxx_atu_entry *entry)
{
	u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;

	if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
		unsigned int mask, shift;

		if (entry->trunk) {
			data |= GLOBAL_ATU_DATA_TRUNK;
			mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
			shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
		} else {
			mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
			shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
		}

		data |= (entry->portv_trunkid << shift) & mask;
	}

1050
	return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
1051 1052
}

1053
static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
1054 1055
				     struct mv88e6xxx_atu_entry *entry,
				     bool static_too)
1056
{
1057 1058
	int op;
	int err;
1059

1060
	err = _mv88e6xxx_atu_wait(chip);
1061 1062
	if (err)
		return err;
1063

1064
	err = _mv88e6xxx_atu_data_write(chip, entry);
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	if (err)
		return err;

	if (entry->fid) {
		op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
			GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
	} else {
		op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
			GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
	}

1076
	return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
1077 1078
}

1079
static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
1080
				u16 fid, bool static_too)
1081 1082 1083 1084 1085
{
	struct mv88e6xxx_atu_entry entry = {
		.fid = fid,
		.state = 0, /* EntryState bits must be 0 */
	};
1086

1087
	return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1088 1089
}

1090
static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
1091
			       int from_port, int to_port, bool static_too)
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
{
	struct mv88e6xxx_atu_entry entry = {
		.trunk = false,
		.fid = fid,
	};

	/* EntryState bits must be 0xF */
	entry.state = GLOBAL_ATU_DATA_STATE_MASK;

	/* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
	entry.portv_trunkid = (to_port & 0x0f) << 4;
	entry.portv_trunkid |= from_port & 0x0f;

1105
	return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1106 1107
}

1108
static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
1109
				 int port, bool static_too)
1110 1111
{
	/* Destination port 0xF means remove the entries */
1112
	return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
1113 1114
}

1115 1116 1117 1118 1119 1120 1121
static const char * const mv88e6xxx_port_state_names[] = {
	[PORT_CONTROL_STATE_DISABLED] = "Disabled",
	[PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
	[PORT_CONTROL_STATE_LEARNING] = "Learning",
	[PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
};

1122
static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
1123
				 u8 state)
1124
{
1125
	struct dsa_switch *ds = chip->ds;
1126 1127
	u16 reg;
	int err;
1128 1129
	u8 oldstate;

1130 1131 1132
	err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, &reg);
	if (err)
		return err;
1133

1134
	oldstate = reg & PORT_CONTROL_STATE_MASK;
1135

1136 1137
	reg &= ~PORT_CONTROL_STATE_MASK;
	reg |= state;
1138

1139 1140 1141
	err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
	if (err)
		return err;
1142

1143 1144 1145
	netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
		   mv88e6xxx_port_state_names[state],
		   mv88e6xxx_port_state_names[oldstate]);
1146

1147
	return 0;
1148 1149
}

1150
static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
1151
{
1152 1153 1154
	struct net_device *bridge = chip->ports[port].bridge_dev;
	const u16 mask = (1 << chip->info->num_ports) - 1;
	struct dsa_switch *ds = chip->ds;
1155
	u16 output_ports = 0;
1156 1157
	u16 reg;
	int err;
1158 1159 1160 1161 1162 1163
	int i;

	/* allow CPU port or DSA link(s) to send frames to every port */
	if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
		output_ports = mask;
	} else {
1164
		for (i = 0; i < chip->info->num_ports; ++i) {
1165
			/* allow sending frames to every group member */
1166
			if (bridge && chip->ports[i].bridge_dev == bridge)
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
				output_ports |= BIT(i);

			/* allow sending frames to CPU port and DSA link(s) */
			if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
				output_ports |= BIT(i);
		}
	}

	/* prevent frames from going back out of the port they came in on */
	output_ports &= ~BIT(port);
1177

1178 1179 1180
	err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
	if (err)
		return err;
1181

1182 1183
	reg &= ~mask;
	reg |= output_ports & mask;
1184

1185
	return mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
1186 1187
}

1188 1189
static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
					 u8 state)
1190
{
V
Vivien Didelot 已提交
1191
	struct mv88e6xxx_chip *chip = ds->priv;
1192
	int stp_state;
1193
	int err;
1194 1195 1196

	switch (state) {
	case BR_STATE_DISABLED:
1197
		stp_state = PORT_CONTROL_STATE_DISABLED;
1198 1199 1200
		break;
	case BR_STATE_BLOCKING:
	case BR_STATE_LISTENING:
1201
		stp_state = PORT_CONTROL_STATE_BLOCKING;
1202 1203
		break;
	case BR_STATE_LEARNING:
1204
		stp_state = PORT_CONTROL_STATE_LEARNING;
1205 1206 1207
		break;
	case BR_STATE_FORWARDING:
	default:
1208
		stp_state = PORT_CONTROL_STATE_FORWARDING;
1209 1210 1211
		break;
	}

1212 1213 1214
	mutex_lock(&chip->reg_lock);
	err = _mv88e6xxx_port_state(chip, port, stp_state);
	mutex_unlock(&chip->reg_lock);
1215 1216

	if (err)
1217 1218
		netdev_err(ds->ports[port].netdev,
			   "failed to update state to %s\n",
1219
			   mv88e6xxx_port_state_names[stp_state]);
1220 1221
}

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
{
	struct mv88e6xxx_chip *chip = ds->priv;
	int err;

	mutex_lock(&chip->reg_lock);
	err = _mv88e6xxx_atu_remove(chip, 0, port, false);
	mutex_unlock(&chip->reg_lock);

	if (err)
		netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
}

1235
static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
1236
				u16 *new, u16 *old)
1237
{
1238
	struct dsa_switch *ds = chip->ds;
1239 1240
	u16 pvid, reg;
	int err;
1241

1242 1243 1244
	err = mv88e6xxx_port_read(chip, port, PORT_DEFAULT_VLAN, &reg);
	if (err)
		return err;
1245

1246
	pvid = reg & PORT_DEFAULT_VLAN_MASK;
1247 1248

	if (new) {
1249 1250
		reg &= ~PORT_DEFAULT_VLAN_MASK;
		reg |= *new & PORT_DEFAULT_VLAN_MASK;
1251

1252 1253 1254
		err = mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, reg);
		if (err)
			return err;
1255

1256 1257
		netdev_dbg(ds->ports[port].netdev,
			   "DefaultVID %d (was %d)\n", *new, pvid);
1258 1259 1260 1261
	}

	if (old)
		*old = pvid;
1262 1263 1264 1265

	return 0;
}

1266
static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
1267
				    int port, u16 *pvid)
1268
{
1269
	return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
1270 1271
}

1272
static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
1273
				    int port, u16 pvid)
1274
{
1275
	return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
1276 1277
}

1278
static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
1279
{
1280 1281
	return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
			      GLOBAL_VTU_OP_BUSY);
1282 1283
}

1284
static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
1285 1286 1287
{
	int ret;

1288
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
1289 1290 1291
	if (ret < 0)
		return ret;

1292
	return _mv88e6xxx_vtu_wait(chip);
1293 1294
}

1295
static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
1296 1297 1298
{
	int ret;

1299
	ret = _mv88e6xxx_vtu_wait(chip);
1300 1301 1302
	if (ret < 0)
		return ret;

1303
	return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
1304 1305
}

1306
static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
1307 1308 1309 1310 1311 1312 1313 1314
					struct mv88e6xxx_vtu_stu_entry *entry,
					unsigned int nibble_offset)
{
	u16 regs[3];
	int i;
	int ret;

	for (i = 0; i < 3; ++i) {
1315
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1316 1317 1318 1319 1320 1321 1322
					  GLOBAL_VTU_DATA_0_3 + i);
		if (ret < 0)
			return ret;

		regs[i] = ret;
	}

1323
	for (i = 0; i < chip->info->num_ports; ++i) {
1324 1325 1326 1327 1328 1329 1330 1331 1332
		unsigned int shift = (i % 4) * 4 + nibble_offset;
		u16 reg = regs[i / 4];

		entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
	}

	return 0;
}

1333
static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
1334 1335
				   struct mv88e6xxx_vtu_stu_entry *entry)
{
1336
	return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
1337 1338
}

1339
static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
1340 1341
				   struct mv88e6xxx_vtu_stu_entry *entry)
{
1342
	return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
1343 1344
}

1345
static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
1346 1347 1348 1349 1350 1351 1352
					 struct mv88e6xxx_vtu_stu_entry *entry,
					 unsigned int nibble_offset)
{
	u16 regs[3] = { 0 };
	int i;
	int ret;

1353
	for (i = 0; i < chip->info->num_ports; ++i) {
1354 1355 1356 1357 1358 1359 1360
		unsigned int shift = (i % 4) * 4 + nibble_offset;
		u8 data = entry->data[i];

		regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
	}

	for (i = 0; i < 3; ++i) {
1361
		ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
1362 1363 1364 1365 1366 1367 1368 1369
					   GLOBAL_VTU_DATA_0_3 + i, regs[i]);
		if (ret < 0)
			return ret;
	}

	return 0;
}

1370
static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
1371 1372
				    struct mv88e6xxx_vtu_stu_entry *entry)
{
1373
	return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
1374 1375
}

1376
static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
1377 1378
				    struct mv88e6xxx_vtu_stu_entry *entry)
{
1379
	return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
1380 1381
}

1382
static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
1383
{
1384
	return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
1385 1386 1387
				    vid & GLOBAL_VTU_VID_MASK);
}

1388
static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
1389 1390 1391 1392 1393
				  struct mv88e6xxx_vtu_stu_entry *entry)
{
	struct mv88e6xxx_vtu_stu_entry next = { 0 };
	int ret;

1394
	ret = _mv88e6xxx_vtu_wait(chip);
1395 1396 1397
	if (ret < 0)
		return ret;

1398
	ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1399 1400 1401
	if (ret < 0)
		return ret;

1402
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1403 1404 1405 1406 1407 1408 1409
	if (ret < 0)
		return ret;

	next.vid = ret & GLOBAL_VTU_VID_MASK;
	next.valid = !!(ret & GLOBAL_VTU_VID_VALID);

	if (next.valid) {
1410
		ret = mv88e6xxx_vtu_data_read(chip, &next);
1411 1412 1413
		if (ret < 0)
			return ret;

1414 1415
		if (mv88e6xxx_has_fid_reg(chip)) {
			ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1416 1417 1418 1419 1420
						  GLOBAL_VTU_FID);
			if (ret < 0)
				return ret;

			next.fid = ret & GLOBAL_VTU_FID_MASK;
1421
		} else if (mv88e6xxx_num_databases(chip) == 256) {
1422 1423 1424
			/* VTU DBNum[7:4] are located in VTU Operation 11:8, and
			 * VTU DBNum[3:0] are located in VTU Operation 3:0
			 */
1425
			ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1426 1427 1428 1429 1430 1431
						  GLOBAL_VTU_OP);
			if (ret < 0)
				return ret;

			next.fid = (ret & 0xf00) >> 4;
			next.fid |= ret & 0xf;
1432
		}
1433

1434 1435
		if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
			ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
						  GLOBAL_VTU_SID);
			if (ret < 0)
				return ret;

			next.sid = ret & GLOBAL_VTU_SID_MASK;
		}
	}

	*entry = next;
	return 0;
}

1448 1449 1450
static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
				    struct switchdev_obj_port_vlan *vlan,
				    int (*cb)(struct switchdev_obj *obj))
1451
{
V
Vivien Didelot 已提交
1452
	struct mv88e6xxx_chip *chip = ds->priv;
1453 1454 1455 1456
	struct mv88e6xxx_vtu_stu_entry next;
	u16 pvid;
	int err;

1457
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1458 1459
		return -EOPNOTSUPP;

1460
	mutex_lock(&chip->reg_lock);
1461

1462
	err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
1463 1464 1465
	if (err)
		goto unlock;

1466
	err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1467 1468 1469 1470
	if (err)
		goto unlock;

	do {
1471
		err = _mv88e6xxx_vtu_getnext(chip, &next);
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
		if (err)
			break;

		if (!next.valid)
			break;

		if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
			continue;

		/* reinit and dump this VLAN obj */
1482 1483
		vlan->vid_begin = next.vid;
		vlan->vid_end = next.vid;
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
		vlan->flags = 0;

		if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
			vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;

		if (next.vid == pvid)
			vlan->flags |= BRIDGE_VLAN_INFO_PVID;

		err = cb(&vlan->obj);
		if (err)
			break;
	} while (next.vid < GLOBAL_VTU_VID_MASK);

unlock:
1498
	mutex_unlock(&chip->reg_lock);
1499 1500 1501 1502

	return err;
}

1503
static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
1504 1505
				    struct mv88e6xxx_vtu_stu_entry *entry)
{
1506
	u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
1507 1508 1509
	u16 reg = 0;
	int ret;

1510
	ret = _mv88e6xxx_vtu_wait(chip);
1511 1512 1513 1514 1515 1516 1517
	if (ret < 0)
		return ret;

	if (!entry->valid)
		goto loadpurge;

	/* Write port member tags */
1518
	ret = mv88e6xxx_vtu_data_write(chip, entry);
1519 1520 1521
	if (ret < 0)
		return ret;

1522
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1523
		reg = entry->sid & GLOBAL_VTU_SID_MASK;
1524 1525
		ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
					   reg);
1526 1527
		if (ret < 0)
			return ret;
1528
	}
1529

1530
	if (mv88e6xxx_has_fid_reg(chip)) {
1531
		reg = entry->fid & GLOBAL_VTU_FID_MASK;
1532 1533
		ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
					   reg);
1534 1535
		if (ret < 0)
			return ret;
1536
	} else if (mv88e6xxx_num_databases(chip) == 256) {
1537 1538 1539 1540 1541
		/* VTU DBNum[7:4] are located in VTU Operation 11:8, and
		 * VTU DBNum[3:0] are located in VTU Operation 3:0
		 */
		op |= (entry->fid & 0xf0) << 8;
		op |= entry->fid & 0xf;
1542 1543 1544 1545 1546
	}

	reg = GLOBAL_VTU_VID_VALID;
loadpurge:
	reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1547
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1548 1549 1550
	if (ret < 0)
		return ret;

1551
	return _mv88e6xxx_vtu_cmd(chip, op);
1552 1553
}

1554
static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
1555 1556 1557 1558 1559
				  struct mv88e6xxx_vtu_stu_entry *entry)
{
	struct mv88e6xxx_vtu_stu_entry next = { 0 };
	int ret;

1560
	ret = _mv88e6xxx_vtu_wait(chip);
1561 1562 1563
	if (ret < 0)
		return ret;

1564
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1565 1566 1567 1568
				   sid & GLOBAL_VTU_SID_MASK);
	if (ret < 0)
		return ret;

1569
	ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1570 1571 1572
	if (ret < 0)
		return ret;

1573
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
1574 1575 1576 1577 1578
	if (ret < 0)
		return ret;

	next.sid = ret & GLOBAL_VTU_SID_MASK;

1579
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1580 1581 1582 1583 1584 1585
	if (ret < 0)
		return ret;

	next.valid = !!(ret & GLOBAL_VTU_VID_VALID);

	if (next.valid) {
1586
		ret = mv88e6xxx_stu_data_read(chip, &next);
1587 1588 1589 1590 1591 1592 1593 1594
		if (ret < 0)
			return ret;
	}

	*entry = next;
	return 0;
}

1595
static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
1596 1597 1598 1599 1600
				    struct mv88e6xxx_vtu_stu_entry *entry)
{
	u16 reg = 0;
	int ret;

1601
	ret = _mv88e6xxx_vtu_wait(chip);
1602 1603 1604 1605 1606 1607 1608
	if (ret < 0)
		return ret;

	if (!entry->valid)
		goto loadpurge;

	/* Write port states */
1609
	ret = mv88e6xxx_stu_data_write(chip, entry);
1610 1611 1612 1613 1614
	if (ret < 0)
		return ret;

	reg = GLOBAL_VTU_VID_VALID;
loadpurge:
1615
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1616 1617 1618 1619
	if (ret < 0)
		return ret;

	reg = entry->sid & GLOBAL_VTU_SID_MASK;
1620
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1621 1622 1623
	if (ret < 0)
		return ret;

1624
	return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1625 1626
}

1627
static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
1628
			       u16 *new, u16 *old)
1629
{
1630
	struct dsa_switch *ds = chip->ds;
1631
	u16 upper_mask;
1632
	u16 fid;
1633 1634
	u16 reg;
	int err;
1635

1636
	if (mv88e6xxx_num_databases(chip) == 4096)
1637
		upper_mask = 0xff;
1638
	else if (mv88e6xxx_num_databases(chip) == 256)
1639
		upper_mask = 0xf;
1640 1641 1642
	else
		return -EOPNOTSUPP;

1643
	/* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1644 1645 1646
	err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
	if (err)
		return err;
1647

1648
	fid = (reg & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1649 1650

	if (new) {
1651 1652
		reg &= ~PORT_BASE_VLAN_FID_3_0_MASK;
		reg |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1653

1654 1655 1656
		err = mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
		if (err)
			return err;
1657 1658 1659
	}

	/* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1660 1661 1662
	err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_1, &reg);
	if (err)
		return err;
1663

1664
	fid |= (reg & upper_mask) << 4;
1665 1666

	if (new) {
1667 1668
		reg &= ~upper_mask;
		reg |= (*new >> 4) & upper_mask;
1669

1670 1671 1672
		err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, reg);
		if (err)
			return err;
1673

1674 1675
		netdev_dbg(ds->ports[port].netdev,
			   "FID %d (was %d)\n", *new, fid);
1676 1677 1678 1679 1680 1681 1682 1683
	}

	if (old)
		*old = fid;

	return 0;
}

1684
static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
1685
				   int port, u16 *fid)
1686
{
1687
	return _mv88e6xxx_port_fid(chip, port, NULL, fid);
1688 1689
}

1690
static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
1691
				   int port, u16 fid)
1692
{
1693
	return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
1694 1695
}

1696
static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
1697 1698 1699
{
	DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
	struct mv88e6xxx_vtu_stu_entry vlan;
1700
	int i, err;
1701 1702 1703

	bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);

1704
	/* Set every FID bit used by the (un)bridged ports */
1705 1706
	for (i = 0; i < chip->info->num_ports; ++i) {
		err = _mv88e6xxx_port_fid_get(chip, i, fid);
1707 1708 1709 1710 1711 1712
		if (err)
			return err;

		set_bit(*fid, fid_bitmap);
	}

1713
	/* Set every FID bit used by the VLAN entries */
1714
	err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1715 1716 1717 1718
	if (err)
		return err;

	do {
1719
		err = _mv88e6xxx_vtu_getnext(chip, &vlan);
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
		if (err)
			return err;

		if (!vlan.valid)
			break;

		set_bit(vlan.fid, fid_bitmap);
	} while (vlan.vid < GLOBAL_VTU_VID_MASK);

	/* The reset value 0x000 is used to indicate that multiple address
	 * databases are not needed. Return the next positive available.
	 */
	*fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
1733
	if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
1734 1735 1736
		return -ENOSPC;

	/* Clear the database */
1737
	return _mv88e6xxx_atu_flush(chip, *fid, true);
1738 1739
}

1740
static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
1741
			      struct mv88e6xxx_vtu_stu_entry *entry)
1742
{
1743
	struct dsa_switch *ds = chip->ds;
1744 1745 1746 1747
	struct mv88e6xxx_vtu_stu_entry vlan = {
		.valid = true,
		.vid = vid,
	};
1748 1749
	int i, err;

1750
	err = _mv88e6xxx_fid_new(chip, &vlan.fid);
1751 1752
	if (err)
		return err;
1753

1754
	/* exclude all ports except the CPU and DSA ports */
1755
	for (i = 0; i < chip->info->num_ports; ++i)
1756 1757 1758
		vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
			? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
			: GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1759

1760 1761
	if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
	    mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
1762 1763 1764 1765 1766 1767 1768
		struct mv88e6xxx_vtu_stu_entry vstp;

		/* Adding a VTU entry requires a valid STU entry. As VSTP is not
		 * implemented, only one STU entry is needed to cover all VTU
		 * entries. Thus, validate the SID 0.
		 */
		vlan.sid = 0;
1769
		err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
1770 1771 1772 1773 1774 1775 1776 1777
		if (err)
			return err;

		if (vstp.sid != vlan.sid || !vstp.valid) {
			memset(&vstp, 0, sizeof(vstp));
			vstp.valid = true;
			vstp.sid = vlan.sid;

1778
			err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
1779 1780 1781 1782 1783 1784 1785 1786 1787
			if (err)
				return err;
		}
	}

	*entry = vlan;
	return 0;
}

1788
static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
1789 1790 1791 1792 1793 1794 1795
			      struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
{
	int err;

	if (!vid)
		return -EINVAL;

1796
	err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
1797 1798 1799
	if (err)
		return err;

1800
	err = _mv88e6xxx_vtu_getnext(chip, entry);
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
	if (err)
		return err;

	if (entry->vid != vid || !entry->valid) {
		if (!creat)
			return -EOPNOTSUPP;
		/* -ENOENT would've been more appropriate, but switchdev expects
		 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
		 */

1811
		err = _mv88e6xxx_vtu_new(chip, vid, entry);
1812 1813 1814 1815 1816
	}

	return err;
}

1817 1818 1819
static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
					u16 vid_begin, u16 vid_end)
{
V
Vivien Didelot 已提交
1820
	struct mv88e6xxx_chip *chip = ds->priv;
1821 1822 1823 1824 1825 1826
	struct mv88e6xxx_vtu_stu_entry vlan;
	int i, err;

	if (!vid_begin)
		return -EOPNOTSUPP;

1827
	mutex_lock(&chip->reg_lock);
1828

1829
	err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
1830 1831 1832 1833
	if (err)
		goto unlock;

	do {
1834
		err = _mv88e6xxx_vtu_getnext(chip, &vlan);
1835 1836 1837 1838 1839 1840 1841 1842 1843
		if (err)
			goto unlock;

		if (!vlan.valid)
			break;

		if (vlan.vid > vid_end)
			break;

1844
		for (i = 0; i < chip->info->num_ports; ++i) {
1845 1846 1847 1848 1849 1850 1851
			if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
				continue;

			if (vlan.data[i] ==
			    GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
				continue;

1852 1853
			if (chip->ports[i].bridge_dev ==
			    chip->ports[port].bridge_dev)
1854 1855
				break; /* same bridge, check next VLAN */

1856
			netdev_warn(ds->ports[port].netdev,
1857 1858
				    "hardware VLAN %d already used by %s\n",
				    vlan.vid,
1859
				    netdev_name(chip->ports[i].bridge_dev));
1860 1861 1862 1863 1864 1865
			err = -EOPNOTSUPP;
			goto unlock;
		}
	} while (vlan.vid < vid_end);

unlock:
1866
	mutex_unlock(&chip->reg_lock);
1867 1868 1869 1870

	return err;
}

1871 1872 1873 1874 1875 1876 1877
static const char * const mv88e6xxx_port_8021q_mode_names[] = {
	[PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
	[PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
	[PORT_CONTROL_2_8021Q_CHECK] = "Check",
	[PORT_CONTROL_2_8021Q_SECURE] = "Secure",
};

1878 1879
static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
					 bool vlan_filtering)
1880
{
V
Vivien Didelot 已提交
1881
	struct mv88e6xxx_chip *chip = ds->priv;
1882 1883
	u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
		PORT_CONTROL_2_8021Q_DISABLED;
1884 1885
	u16 reg;
	int err;
1886

1887
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1888 1889
		return -EOPNOTSUPP;

1890
	mutex_lock(&chip->reg_lock);
1891

1892 1893
	err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_2, &reg);
	if (err)
1894 1895
		goto unlock;

1896
	old = reg & PORT_CONTROL_2_8021Q_MASK;
1897

1898
	if (new != old) {
1899 1900
		reg &= ~PORT_CONTROL_2_8021Q_MASK;
		reg |= new & PORT_CONTROL_2_8021Q_MASK;
1901

1902 1903
		err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
		if (err)
1904 1905
			goto unlock;

1906
		netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
1907 1908 1909
			   mv88e6xxx_port_8021q_mode_names[new],
			   mv88e6xxx_port_8021q_mode_names[old]);
	}
1910

1911
	err = 0;
1912
unlock:
1913
	mutex_unlock(&chip->reg_lock);
1914

1915
	return err;
1916 1917
}

1918 1919 1920 1921
static int
mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
			    const struct switchdev_obj_port_vlan *vlan,
			    struct switchdev_trans *trans)
1922
{
V
Vivien Didelot 已提交
1923
	struct mv88e6xxx_chip *chip = ds->priv;
1924 1925
	int err;

1926
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1927 1928
		return -EOPNOTSUPP;

1929 1930 1931 1932 1933 1934 1935 1936
	/* If the requested port doesn't belong to the same bridge as the VLAN
	 * members, do not support it (yet) and fallback to software VLAN.
	 */
	err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
					   vlan->vid_end);
	if (err)
		return err;

1937 1938 1939 1940 1941 1942
	/* We don't need any dynamic resource from the kernel (yet),
	 * so skip the prepare phase.
	 */
	return 0;
}

1943
static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
1944
				    u16 vid, bool untagged)
1945 1946 1947 1948
{
	struct mv88e6xxx_vtu_stu_entry vlan;
	int err;

1949
	err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
1950
	if (err)
1951
		return err;
1952 1953 1954 1955 1956

	vlan.data[port] = untagged ?
		GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
		GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;

1957
	return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
1958 1959
}

1960 1961 1962
static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
				    const struct switchdev_obj_port_vlan *vlan,
				    struct switchdev_trans *trans)
1963
{
V
Vivien Didelot 已提交
1964
	struct mv88e6xxx_chip *chip = ds->priv;
1965 1966 1967 1968
	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
	u16 vid;

1969
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1970 1971
		return;

1972
	mutex_lock(&chip->reg_lock);
1973

1974
	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
1975
		if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
1976 1977
			netdev_err(ds->ports[port].netdev,
				   "failed to add VLAN %d%c\n",
1978
				   vid, untagged ? 'u' : 't');
1979

1980
	if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
1981
		netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
1982
			   vlan->vid_end);
1983

1984
	mutex_unlock(&chip->reg_lock);
1985 1986
}

1987
static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
1988
				    int port, u16 vid)
1989
{
1990
	struct dsa_switch *ds = chip->ds;
1991 1992 1993
	struct mv88e6xxx_vtu_stu_entry vlan;
	int i, err;

1994
	err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
1995
	if (err)
1996
		return err;
1997

1998 1999
	/* Tell switchdev if this VLAN is handled in software */
	if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2000
		return -EOPNOTSUPP;
2001 2002 2003 2004

	vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;

	/* keep the VLAN unless all ports are excluded */
2005
	vlan.valid = false;
2006
	for (i = 0; i < chip->info->num_ports; ++i) {
2007
		if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
2008 2009 2010
			continue;

		if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
2011
			vlan.valid = true;
2012 2013 2014 2015
			break;
		}
	}

2016
	err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
2017 2018 2019
	if (err)
		return err;

2020
	return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
2021 2022
}

2023 2024
static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
				   const struct switchdev_obj_port_vlan *vlan)
2025
{
V
Vivien Didelot 已提交
2026
	struct mv88e6xxx_chip *chip = ds->priv;
2027 2028 2029
	u16 pvid, vid;
	int err = 0;

2030
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2031 2032
		return -EOPNOTSUPP;

2033
	mutex_lock(&chip->reg_lock);
2034

2035
	err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
2036 2037 2038
	if (err)
		goto unlock;

2039
	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
2040
		err = _mv88e6xxx_port_vlan_del(chip, port, vid);
2041 2042 2043 2044
		if (err)
			goto unlock;

		if (vid == pvid) {
2045
			err = _mv88e6xxx_port_pvid_set(chip, port, 0);
2046 2047 2048 2049 2050
			if (err)
				goto unlock;
		}
	}

2051
unlock:
2052
	mutex_unlock(&chip->reg_lock);
2053 2054 2055 2056

	return err;
}

2057
static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
2058
				    const unsigned char *addr)
2059 2060 2061 2062
{
	int i, ret;

	for (i = 0; i < 3; i++) {
2063
		ret = _mv88e6xxx_reg_write(
2064
			chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
2065
			(addr[i * 2] << 8) | addr[i * 2 + 1]);
2066 2067 2068 2069 2070 2071 2072
		if (ret < 0)
			return ret;
	}

	return 0;
}

2073
static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
2074
				   unsigned char *addr)
2075 2076 2077 2078
{
	int i, ret;

	for (i = 0; i < 3; i++) {
2079
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
2080
					  GLOBAL_ATU_MAC_01 + i);
2081 2082 2083 2084 2085 2086 2087 2088 2089
		if (ret < 0)
			return ret;
		addr[i * 2] = ret >> 8;
		addr[i * 2 + 1] = ret & 0xff;
	}

	return 0;
}

2090
static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
2091
			       struct mv88e6xxx_atu_entry *entry)
2092
{
2093 2094
	int ret;

2095
	ret = _mv88e6xxx_atu_wait(chip);
2096 2097 2098
	if (ret < 0)
		return ret;

2099
	ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
2100 2101 2102
	if (ret < 0)
		return ret;

2103
	ret = _mv88e6xxx_atu_data_write(chip, entry);
2104
	if (ret < 0)
2105 2106
		return ret;

2107
	return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
2108
}
2109

2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
				  struct mv88e6xxx_atu_entry *entry);

static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
			     const u8 *addr, struct mv88e6xxx_atu_entry *entry)
{
	struct mv88e6xxx_atu_entry next;
	int err;

	eth_broadcast_addr(next.mac);

	err = _mv88e6xxx_atu_mac_write(chip, next.mac);
	if (err)
		return err;

	do {
		err = _mv88e6xxx_atu_getnext(chip, fid, &next);
		if (err)
			return err;

		if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
			break;

		if (ether_addr_equal(next.mac, addr)) {
			*entry = next;
			return 0;
		}
	} while (!is_broadcast_ether_addr(next.mac));

	memset(entry, 0, sizeof(*entry));
	entry->fid = fid;
	ether_addr_copy(entry->mac, addr);

	return 0;
}

2146 2147 2148
static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
					const unsigned char *addr, u16 vid,
					u8 state)
2149
{
2150
	struct mv88e6xxx_vtu_stu_entry vlan;
2151
	struct mv88e6xxx_atu_entry entry;
2152 2153
	int err;

2154 2155
	/* Null VLAN ID corresponds to the port private database */
	if (vid == 0)
2156
		err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
2157
	else
2158
		err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
2159 2160
	if (err)
		return err;
2161

2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
	err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
	if (err)
		return err;

	/* Purge the ATU entry only if no port is using it anymore */
	if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
		entry.portv_trunkid &= ~BIT(port);
		if (!entry.portv_trunkid)
			entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
	} else {
		entry.portv_trunkid |= BIT(port);
		entry.state = state;
2174 2175
	}

2176
	return _mv88e6xxx_atu_load(chip, &entry);
2177 2178
}

2179 2180 2181
static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
				      const struct switchdev_obj_port_fdb *fdb,
				      struct switchdev_trans *trans)
V
Vivien Didelot 已提交
2182 2183 2184 2185 2186 2187 2188
{
	/* We don't need any dynamic resource from the kernel (yet),
	 * so skip the prepare phase.
	 */
	return 0;
}

2189 2190 2191
static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
				   const struct switchdev_obj_port_fdb *fdb,
				   struct switchdev_trans *trans)
2192
{
V
Vivien Didelot 已提交
2193
	struct mv88e6xxx_chip *chip = ds->priv;
2194

2195
	mutex_lock(&chip->reg_lock);
2196 2197 2198
	if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
					 GLOBAL_ATU_DATA_STATE_UC_STATIC))
		netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
2199
	mutex_unlock(&chip->reg_lock);
2200 2201
}

2202 2203
static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
				  const struct switchdev_obj_port_fdb *fdb)
2204
{
V
Vivien Didelot 已提交
2205
	struct mv88e6xxx_chip *chip = ds->priv;
2206
	int err;
2207

2208
	mutex_lock(&chip->reg_lock);
2209 2210
	err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
					   GLOBAL_ATU_DATA_STATE_UNUSED);
2211
	mutex_unlock(&chip->reg_lock);
2212

2213
	return err;
2214 2215
}

2216
static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2217
				  struct mv88e6xxx_atu_entry *entry)
2218
{
2219 2220 2221 2222
	struct mv88e6xxx_atu_entry next = { 0 };
	int ret;

	next.fid = fid;
2223

2224
	ret = _mv88e6xxx_atu_wait(chip);
2225 2226
	if (ret < 0)
		return ret;
2227

2228
	ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2229 2230
	if (ret < 0)
		return ret;
2231

2232
	ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
2233 2234
	if (ret < 0)
		return ret;
2235

2236
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
2237 2238
	if (ret < 0)
		return ret;
2239

2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
	next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
	if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
		unsigned int mask, shift;

		if (ret & GLOBAL_ATU_DATA_TRUNK) {
			next.trunk = true;
			mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
			shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
		} else {
			next.trunk = false;
			mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
			shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
		}

		next.portv_trunkid = (ret & mask) >> shift;
	}
2256

2257
	*entry = next;
2258 2259 2260
	return 0;
}

2261 2262 2263 2264
static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
				      u16 fid, u16 vid, int port,
				      struct switchdev_obj *obj,
				      int (*cb)(struct switchdev_obj *obj))
2265 2266 2267 2268 2269 2270
{
	struct mv88e6xxx_atu_entry addr = {
		.mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
	};
	int err;

2271
	err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
2272 2273 2274 2275
	if (err)
		return err;

	do {
2276
		err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
2277
		if (err)
2278
			return err;
2279 2280 2281 2282

		if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
			break;

2283 2284 2285 2286 2287
		if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
			continue;

		if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
			struct switchdev_obj_port_fdb *fdb;
2288

2289 2290 2291 2292
			if (!is_unicast_ether_addr(addr.mac))
				continue;

			fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
2293 2294
			fdb->vid = vid;
			ether_addr_copy(fdb->addr, addr.mac);
2295 2296 2297 2298
			if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
				fdb->ndm_state = NUD_NOARP;
			else
				fdb->ndm_state = NUD_REACHABLE;
2299 2300 2301 2302 2303 2304 2305 2306 2307
		} else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
			struct switchdev_obj_port_mdb *mdb;

			if (!is_multicast_ether_addr(addr.mac))
				continue;

			mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
			mdb->vid = vid;
			ether_addr_copy(mdb->addr, addr.mac);
2308 2309
		} else {
			return -EOPNOTSUPP;
2310
		}
2311 2312 2313 2314

		err = cb(obj);
		if (err)
			return err;
2315 2316 2317 2318 2319
	} while (!is_broadcast_ether_addr(addr.mac));

	return err;
}

2320 2321 2322
static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
				  struct switchdev_obj *obj,
				  int (*cb)(struct switchdev_obj *obj))
2323 2324 2325 2326
{
	struct mv88e6xxx_vtu_stu_entry vlan = {
		.vid = GLOBAL_VTU_VID_MASK, /* all ones */
	};
2327
	u16 fid;
2328 2329
	int err;

2330
	/* Dump port's default Filtering Information Database (VLAN ID 0) */
2331
	err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2332
	if (err)
2333
		return err;
2334

2335
	err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2336
	if (err)
2337
		return err;
2338

2339
	/* Dump VLANs' Filtering Information Databases */
2340
	err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2341
	if (err)
2342
		return err;
2343 2344

	do {
2345
		err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2346
		if (err)
2347
			return err;
2348 2349 2350 2351

		if (!vlan.valid)
			break;

2352 2353
		err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
						 obj, cb);
2354
		if (err)
2355
			return err;
2356 2357
	} while (vlan.vid < GLOBAL_VTU_VID_MASK);

2358 2359 2360 2361 2362 2363 2364
	return err;
}

static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
				   struct switchdev_obj_port_fdb *fdb,
				   int (*cb)(struct switchdev_obj *obj))
{
V
Vivien Didelot 已提交
2365
	struct mv88e6xxx_chip *chip = ds->priv;
2366 2367 2368 2369
	int err;

	mutex_lock(&chip->reg_lock);
	err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
2370
	mutex_unlock(&chip->reg_lock);
2371 2372 2373 2374

	return err;
}

2375 2376
static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
				      struct net_device *bridge)
2377
{
V
Vivien Didelot 已提交
2378
	struct mv88e6xxx_chip *chip = ds->priv;
2379
	int i, err = 0;
2380

2381
	mutex_lock(&chip->reg_lock);
2382

2383
	/* Assign the bridge and remap each port's VLANTable */
2384
	chip->ports[port].bridge_dev = bridge;
2385

2386 2387 2388
	for (i = 0; i < chip->info->num_ports; ++i) {
		if (chip->ports[i].bridge_dev == bridge) {
			err = _mv88e6xxx_port_based_vlan_map(chip, i);
2389 2390 2391 2392 2393
			if (err)
				break;
		}
	}

2394
	mutex_unlock(&chip->reg_lock);
2395

2396
	return err;
2397 2398
}

2399
static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
2400
{
V
Vivien Didelot 已提交
2401
	struct mv88e6xxx_chip *chip = ds->priv;
2402
	struct net_device *bridge = chip->ports[port].bridge_dev;
2403
	int i;
2404

2405
	mutex_lock(&chip->reg_lock);
2406

2407
	/* Unassign the bridge and remap each port's VLANTable */
2408
	chip->ports[port].bridge_dev = NULL;
2409

2410 2411 2412
	for (i = 0; i < chip->info->num_ports; ++i)
		if (i == port || chip->ports[i].bridge_dev == bridge)
			if (_mv88e6xxx_port_based_vlan_map(chip, i))
2413 2414
				netdev_warn(ds->ports[i].netdev,
					    "failed to remap\n");
2415

2416
	mutex_unlock(&chip->reg_lock);
2417 2418
}

2419
static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
2420
{
2421
	bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
2422
	u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2423
	struct gpio_desc *gpiod = chip->reset;
2424
	unsigned long timeout;
2425 2426
	int err, ret;
	u16 reg;
2427 2428 2429
	int i;

	/* Set all ports to the disabled state. */
2430
	for (i = 0; i < chip->info->num_ports; i++) {
2431 2432 2433
		err = mv88e6xxx_port_read(chip, i, PORT_CONTROL, &reg);
		if (err)
			return err;
2434

2435 2436 2437 2438
		err = mv88e6xxx_port_write(chip, i, PORT_CONTROL,
					   reg & 0xfffc);
		if (err)
			return err;
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
	}

	/* Wait for transmit queues to drain. */
	usleep_range(2000, 4000);

	/* If there is a gpio connected to the reset pin, toggle it */
	if (gpiod) {
		gpiod_set_value_cansleep(gpiod, 1);
		usleep_range(10000, 20000);
		gpiod_set_value_cansleep(gpiod, 0);
		usleep_range(10000, 20000);
	}

	/* Reset the switch. Keep the PPU active if requested. The PPU
	 * needs to be active to support indirect phy register access
	 * through global registers 0x18 and 0x19.
	 */
	if (ppu_active)
2457
		err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
2458
	else
2459 2460 2461
		err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
	if (err)
		return err;
2462 2463 2464 2465

	/* Wait up to one second for reset to complete. */
	timeout = jiffies + 1 * HZ;
	while (time_before(jiffies, timeout)) {
2466
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
2467 2468 2469 2470 2471 2472 2473 2474
		if (ret < 0)
			return ret;

		if ((ret & is_reset) == is_reset)
			break;
		usleep_range(1000, 2000);
	}
	if (time_after(jiffies, timeout))
2475
		err = -ETIMEDOUT;
2476
	else
2477
		err = 0;
2478

2479
	return err;
2480 2481
}

2482
static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
2483
{
2484 2485
	u16 val;
	int err;
2486

2487 2488 2489 2490
	/* Clear Power Down bit */
	err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
	if (err)
		return err;
2491

2492 2493 2494
	if (val & BMCR_PDOWN) {
		val &= ~BMCR_PDOWN;
		err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
2495 2496
	}

2497
	return err;
2498 2499
}

2500
static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
2501
{
2502
	struct dsa_switch *ds = chip->ds;
2503
	int err;
2504
	u16 reg;
2505

2506 2507 2508 2509
	if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
	    mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
	    mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
	    mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
2510 2511 2512 2513 2514 2515
		/* MAC Forcing register: don't force link, speed,
		 * duplex or flow control state to any particular
		 * values on physical ports, but force the CPU port
		 * and all DSA ports to their maximum bandwidth and
		 * full duplex.
		 */
2516
		err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
2517
		if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
2518
			reg &= ~PORT_PCS_CTRL_UNFORCED;
2519 2520 2521 2522
			reg |= PORT_PCS_CTRL_FORCE_LINK |
				PORT_PCS_CTRL_LINK_UP |
				PORT_PCS_CTRL_DUPLEX_FULL |
				PORT_PCS_CTRL_FORCE_DUPLEX;
2523
			if (mv88e6xxx_6065_family(chip))
2524 2525 2526 2527 2528 2529 2530
				reg |= PORT_PCS_CTRL_100;
			else
				reg |= PORT_PCS_CTRL_1000;
		} else {
			reg |= PORT_PCS_CTRL_UNFORCED;
		}

2531 2532 2533
		err = mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
		if (err)
			return err;
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550
	}

	/* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
	 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
	 * tunneling, determine priority by looking at 802.1p and IP
	 * priority fields (IP prio has precedence), and set STP state
	 * to Forwarding.
	 *
	 * If this is the CPU link, use DSA or EDSA tagging depending
	 * on which tagging mode was configured.
	 *
	 * If this is a link to another switch, use DSA tagging mode.
	 *
	 * If this is the upstream port for this switch, enable
	 * forwarding of unknown unicasts and multicasts.
	 */
	reg = 0;
2551 2552 2553 2554
	if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
	    mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
	    mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
	    mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
2555 2556 2557 2558
		reg = PORT_CONTROL_IGMP_MLD_SNOOP |
		PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
		PORT_CONTROL_STATE_FORWARDING;
	if (dsa_is_cpu_port(ds, port)) {
2559
		if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
2560
			reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2561
				PORT_CONTROL_FORWARD_UNKNOWN_MC;
2562 2563
		else
			reg |= PORT_CONTROL_DSA_TAG;
2564 2565
		reg |= PORT_CONTROL_EGRESS_ADD_TAG |
			PORT_CONTROL_FORWARD_UNKNOWN;
2566
	}
2567
	if (dsa_is_dsa_port(ds, port)) {
2568 2569
		if (mv88e6xxx_6095_family(chip) ||
		    mv88e6xxx_6185_family(chip))
2570
			reg |= PORT_CONTROL_DSA_TAG;
2571 2572 2573 2574 2575
		if (mv88e6xxx_6352_family(chip) ||
		    mv88e6xxx_6351_family(chip) ||
		    mv88e6xxx_6165_family(chip) ||
		    mv88e6xxx_6097_family(chip) ||
		    mv88e6xxx_6320_family(chip)) {
2576
			reg |= PORT_CONTROL_FRAME_MODE_DSA;
2577 2578
		}

2579 2580 2581 2582 2583
		if (port == dsa_upstream_port(ds))
			reg |= PORT_CONTROL_FORWARD_UNKNOWN |
				PORT_CONTROL_FORWARD_UNKNOWN_MC;
	}
	if (reg) {
2584 2585 2586
		err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
		if (err)
			return err;
2587 2588
	}

2589 2590 2591
	/* If this port is connected to a SerDes, make sure the SerDes is not
	 * powered down.
	 */
2592
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
2593 2594 2595 2596 2597 2598 2599 2600 2601 2602
		err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
		if (err)
			return err;
		reg &= PORT_STATUS_CMODE_MASK;
		if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
		    (reg == PORT_STATUS_CMODE_1000BASE_X) ||
		    (reg == PORT_STATUS_CMODE_SGMII)) {
			err = mv88e6xxx_serdes_power_on(chip);
			if (err < 0)
				return err;
2603 2604 2605
		}
	}

2606
	/* Port Control 2: don't force a good FCS, set the maximum frame size to
2607
	 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
2608 2609 2610
	 * untagged frames on this port, do a destination address lookup on all
	 * received packets as usual, disable ARP mirroring and don't send a
	 * copy of all transmitted/received frames on this port to the CPU.
2611 2612
	 */
	reg = 0;
2613 2614 2615 2616
	if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
	    mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
	    mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
	    mv88e6xxx_6185_family(chip))
2617 2618
		reg = PORT_CONTROL_2_MAP_DA;

2619 2620
	if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
	    mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
2621 2622
		reg |= PORT_CONTROL_2_JUMBO_10240;

2623
	if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
2624 2625 2626 2627 2628 2629 2630 2631 2632
		/* Set the upstream port this port should use */
		reg |= dsa_upstream_port(ds);
		/* enable forwarding of unknown multicast addresses to
		 * the upstream port
		 */
		if (port == dsa_upstream_port(ds))
			reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
	}

2633
	reg |= PORT_CONTROL_2_8021Q_DISABLED;
2634

2635
	if (reg) {
2636 2637 2638
		err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
		if (err)
			return err;
2639 2640 2641 2642 2643 2644 2645
	}

	/* Port Association Vector: when learning source addresses
	 * of packets, add the address to the address database using
	 * a port bitmap that has only the bit for this port set and
	 * the other bits clear.
	 */
2646
	reg = 1 << port;
2647 2648
	/* Disable learning for CPU port */
	if (dsa_is_cpu_port(ds, port))
2649
		reg = 0;
2650

2651 2652 2653
	err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
	if (err)
		return err;
2654 2655

	/* Egress rate control 2: disable egress rate control. */
2656 2657 2658
	err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
	if (err)
		return err;
2659

2660 2661 2662
	if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
	    mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
	    mv88e6xxx_6320_family(chip)) {
2663 2664 2665 2666
		/* Do not limit the period of time that this port can
		 * be paused for by the remote end or the period of
		 * time that this port can pause the remote end.
		 */
2667 2668 2669
		err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
		if (err)
			return err;
2670 2671 2672 2673 2674

		/* Port ATU control: disable limiting the number of
		 * address database entries that this port is allowed
		 * to use.
		 */
2675 2676
		err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
					   0x0000);
2677 2678 2679
		/* Priority Override: disable DA, SA and VTU priority
		 * override.
		 */
2680 2681 2682 2683
		err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
					   0x0000);
		if (err)
			return err;
2684 2685 2686 2687

		/* Port Ethertype: use the Ethertype DSA Ethertype
		 * value.
		 */
2688
		if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
2689 2690 2691 2692
			err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
						   ETH_P_EDSA);
			if (err)
				return err;
2693 2694
		}

2695 2696 2697
		/* Tag Remap: use an identity 802.1p prio -> switch
		 * prio mapping.
		 */
2698 2699 2700 2701
		err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
					   0x3210);
		if (err)
			return err;
2702 2703 2704 2705

		/* Tag Remap 2: use an identity 802.1p prio -> switch
		 * prio mapping.
		 */
2706 2707 2708 2709
		err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
					   0x7654);
		if (err)
			return err;
2710 2711
	}

2712
	/* Rate Control: disable ingress rate limiting. */
2713 2714 2715
	if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
	    mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
	    mv88e6xxx_6320_family(chip)) {
2716 2717 2718 2719
		err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
					   0x0001);
		if (err)
			return err;
2720
	} else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
2721 2722 2723 2724
		err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
					   0x0000);
		if (err)
			return err;
2725 2726
	}

2727 2728
	/* Port Control 1: disable trunking, disable sending
	 * learning messages to this port.
2729
	 */
2730 2731 2732
	err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
	if (err)
		return err;
2733

2734
	/* Port based VLAN map: give each port the same default address
2735 2736
	 * database, and allow bidirectional communication between the
	 * CPU and DSA port(s), and the other ports.
2737
	 */
2738 2739 2740
	err = _mv88e6xxx_port_fid_set(chip, port, 0);
	if (err)
		return err;
2741

2742 2743 2744
	err = _mv88e6xxx_port_based_vlan_map(chip, port);
	if (err)
		return err;
2745 2746 2747 2748

	/* Default VLAN ID and priority: don't set a default VLAN
	 * ID, and set the default packet priority to zero.
	 */
2749
	return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
2750 2751
}

2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769
static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
{
	int err;

	err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
			      (addr[0] << 8) | addr[1]);
	if (err)
		return err;

	err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
			      (addr[2] << 8) | addr[3]);
	if (err)
		return err;

	return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
			       (addr[4] << 8) | addr[5]);
}

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
static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
				     unsigned int msecs)
{
	const unsigned int coeff = chip->info->age_time_coeff;
	const unsigned int min = 0x01 * coeff;
	const unsigned int max = 0xff * coeff;
	u8 age_time;
	u16 val;
	int err;

	if (msecs < min || msecs > max)
		return -ERANGE;

	/* Round to nearest multiple of coeff */
	age_time = (msecs + coeff / 2) / coeff;

	err = mv88e6xxx_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, &val);
	if (err)
		return err;

	/* AgeTime is 11:4 bits */
	val &= ~0xff0;
	val |= age_time << 4;

	return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, val);
}

2797 2798 2799
static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
				     unsigned int ageing_time)
{
V
Vivien Didelot 已提交
2800
	struct mv88e6xxx_chip *chip = ds->priv;
2801 2802 2803 2804 2805 2806 2807 2808 2809
	int err;

	mutex_lock(&chip->reg_lock);
	err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
	mutex_unlock(&chip->reg_lock);

	return err;
}

2810
static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
2811
{
2812
	struct dsa_switch *ds = chip->ds;
2813
	u32 upstream_port = dsa_upstream_port(ds);
2814
	u16 reg;
2815
	int err;
2816

2817 2818 2819 2820
	/* Enable the PHY Polling Unit if present, don't discard any packets,
	 * and mask all interrupt sources.
	 */
	reg = 0;
2821 2822
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
	    mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
2823 2824
		reg |= GLOBAL_CONTROL_PPU_ENABLE;

2825
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
2826 2827 2828
	if (err)
		return err;

2829 2830 2831 2832 2833 2834
	/* Configure the upstream port, and configure it as the port to which
	 * ingress and egress and ARP monitor frames are to be sent.
	 */
	reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
		upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
		upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
2835 2836
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
				   reg);
2837 2838 2839
	if (err)
		return err;

2840
	/* Disable remote management, and set the switch's DSA device number. */
2841
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
2842 2843 2844 2845 2846
				   GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
				   (ds->index & 0x1f));
	if (err)
		return err;

2847 2848 2849 2850 2851
	/* Clear all the VTU and STU entries */
	err = _mv88e6xxx_vtu_stu_flush(chip);
	if (err < 0)
		return err;

2852 2853 2854 2855
	/* Set the default address aging time to 5 minutes, and
	 * enable address learn messages to be sent to all message
	 * ports.
	 */
2856 2857
	err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
			      GLOBAL_ATU_CONTROL_LEARN2ALL);
2858
	if (err)
2859
		return err;
2860

2861 2862
	err = mv88e6xxx_g1_set_age_time(chip, 300000);
	if (err)
2863 2864 2865 2866 2867 2868 2869
		return err;

	/* Clear all ATU entries */
	err = _mv88e6xxx_atu_flush(chip, 0, true);
	if (err)
		return err;

2870
	/* Configure the IP ToS mapping registers. */
2871
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2872
	if (err)
2873
		return err;
2874
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2875
	if (err)
2876
		return err;
2877
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2878
	if (err)
2879
		return err;
2880
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2881
	if (err)
2882
		return err;
2883
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2884
	if (err)
2885
		return err;
2886
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2887
	if (err)
2888
		return err;
2889
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2890
	if (err)
2891
		return err;
2892
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2893
	if (err)
2894
		return err;
2895 2896

	/* Configure the IEEE 802.1p priority mapping register. */
2897
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2898
	if (err)
2899
		return err;
2900

2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914
	/* Clear the statistics counters for all ports */
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
				   GLOBAL_STATS_OP_FLUSH_ALL);
	if (err)
		return err;

	/* Wait for the flush to complete. */
	err = _mv88e6xxx_stats_wait(chip);
	if (err)
		return err;

	return 0;
}

2915
static int mv88e6xxx_setup(struct dsa_switch *ds)
2916
{
V
Vivien Didelot 已提交
2917
	struct mv88e6xxx_chip *chip = ds->priv;
2918
	int err;
2919 2920
	int i;

2921 2922
	chip->ds = ds;
	ds->slave_mii_bus = chip->mdio_bus;
2923

2924
	mutex_lock(&chip->reg_lock);
2925

2926
	err = mv88e6xxx_switch_reset(chip);
2927 2928 2929
	if (err)
		goto unlock;

2930 2931 2932 2933 2934 2935 2936 2937 2938
	/* Setup Switch Port Registers */
	for (i = 0; i < chip->info->num_ports; i++) {
		err = mv88e6xxx_setup_port(chip, i);
		if (err)
			goto unlock;
	}

	/* Setup Switch Global 1 Registers */
	err = mv88e6xxx_g1_setup(chip);
2939 2940 2941
	if (err)
		goto unlock;

2942 2943 2944
	/* Setup Switch Global 2 Registers */
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
		err = mv88e6xxx_g2_setup(chip);
2945 2946 2947
		if (err)
			goto unlock;
	}
2948

2949
unlock:
2950
	mutex_unlock(&chip->reg_lock);
2951

2952
	return err;
2953 2954
}

2955 2956
static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
{
V
Vivien Didelot 已提交
2957
	struct mv88e6xxx_chip *chip = ds->priv;
2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
	int err;

	mutex_lock(&chip->reg_lock);

	/* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
		err = mv88e6xxx_g2_set_switch_mac(chip, addr);
	else
		err = mv88e6xxx_g1_set_switch_mac(chip, addr);

	mutex_unlock(&chip->reg_lock);

	return err;
}

2973
static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
2974
{
2975
	struct mv88e6xxx_chip *chip = bus->priv;
2976 2977
	u16 val;
	int err;
2978

2979
	if (phy >= chip->info->num_ports)
2980
		return 0xffff;
2981

2982
	mutex_lock(&chip->reg_lock);
2983
	err = mv88e6xxx_phy_read(chip, phy, reg, &val);
2984
	mutex_unlock(&chip->reg_lock);
2985 2986

	return err ? err : val;
2987 2988
}

2989
static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
2990
{
2991
	struct mv88e6xxx_chip *chip = bus->priv;
2992
	int err;
2993

2994
	if (phy >= chip->info->num_ports)
2995
		return 0xffff;
2996

2997
	mutex_lock(&chip->reg_lock);
2998
	err = mv88e6xxx_phy_write(chip, phy, reg, val);
2999
	mutex_unlock(&chip->reg_lock);
3000 3001

	return err;
3002 3003
}

3004
static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
3005 3006 3007 3008 3009 3010 3011
				   struct device_node *np)
{
	static int index;
	struct mii_bus *bus;
	int err;

	if (np)
3012
		chip->mdio_np = of_get_child_by_name(np, "mdio");
3013

3014
	bus = devm_mdiobus_alloc(chip->dev);
3015 3016 3017
	if (!bus)
		return -ENOMEM;

3018
	bus->priv = (void *)chip;
3019 3020 3021 3022 3023 3024 3025 3026 3027 3028
	if (np) {
		bus->name = np->full_name;
		snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
	} else {
		bus->name = "mv88e6xxx SMI";
		snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
	}

	bus->read = mv88e6xxx_mdio_read;
	bus->write = mv88e6xxx_mdio_write;
3029
	bus->parent = chip->dev;
3030

3031 3032
	if (chip->mdio_np)
		err = of_mdiobus_register(bus, chip->mdio_np);
3033 3034 3035
	else
		err = mdiobus_register(bus);
	if (err) {
3036
		dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
3037 3038
		goto out;
	}
3039
	chip->mdio_bus = bus;
3040 3041 3042 3043

	return 0;

out:
3044 3045
	if (chip->mdio_np)
		of_node_put(chip->mdio_np);
3046 3047 3048 3049

	return err;
}

3050
static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
3051 3052

{
3053
	struct mii_bus *bus = chip->mdio_bus;
3054 3055 3056

	mdiobus_unregister(bus);

3057 3058
	if (chip->mdio_np)
		of_node_put(chip->mdio_np);
3059 3060
}

3061 3062 3063 3064
#ifdef CONFIG_NET_DSA_HWMON

static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
{
V
Vivien Didelot 已提交
3065
	struct mv88e6xxx_chip *chip = ds->priv;
3066
	u16 val;
3067 3068 3069 3070
	int ret;

	*temp = 0;

3071
	mutex_lock(&chip->reg_lock);
3072

3073
	ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
3074 3075 3076 3077
	if (ret < 0)
		goto error;

	/* Enable temperature sensor */
3078
	ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3079 3080 3081
	if (ret < 0)
		goto error;

3082
	ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
3083 3084 3085 3086 3087 3088
	if (ret < 0)
		goto error;

	/* Wait for temperature to stabilize */
	usleep_range(10000, 12000);

3089 3090
	ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
	if (ret < 0)
3091 3092 3093
		goto error;

	/* Disable temperature sensor */
3094
	ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
3095 3096 3097 3098 3099 3100
	if (ret < 0)
		goto error;

	*temp = ((val & 0x1f) - 5) * 5;

error:
3101
	mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
3102
	mutex_unlock(&chip->reg_lock);
3103 3104 3105 3106 3107
	return ret;
}

static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
{
V
Vivien Didelot 已提交
3108
	struct mv88e6xxx_chip *chip = ds->priv;
3109
	int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3110
	u16 val;
3111 3112 3113 3114
	int ret;

	*temp = 0;

3115 3116 3117
	mutex_lock(&chip->reg_lock);
	ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
	mutex_unlock(&chip->reg_lock);
3118 3119 3120
	if (ret < 0)
		return ret;

3121
	*temp = (val & 0xff) - 25;
3122 3123 3124 3125

	return 0;
}

3126
static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3127
{
V
Vivien Didelot 已提交
3128
	struct mv88e6xxx_chip *chip = ds->priv;
3129

3130
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
3131 3132
		return -EOPNOTSUPP;

3133
	if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
3134 3135 3136 3137 3138
		return mv88e63xx_get_temp(ds, temp);

	return mv88e61xx_get_temp(ds, temp);
}

3139
static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3140
{
V
Vivien Didelot 已提交
3141
	struct mv88e6xxx_chip *chip = ds->priv;
3142
	int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3143
	u16 val;
3144 3145
	int ret;

3146
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3147 3148 3149 3150
		return -EOPNOTSUPP;

	*temp = 0;

3151 3152 3153
	mutex_lock(&chip->reg_lock);
	ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
	mutex_unlock(&chip->reg_lock);
3154 3155 3156
	if (ret < 0)
		return ret;

3157
	*temp = (((val >> 8) & 0x1f) * 5) - 25;
3158 3159 3160 3161

	return 0;
}

3162
static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3163
{
V
Vivien Didelot 已提交
3164
	struct mv88e6xxx_chip *chip = ds->priv;
3165
	int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3166 3167
	u16 val;
	int err;
3168

3169
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3170 3171
		return -EOPNOTSUPP;

3172 3173 3174 3175
	mutex_lock(&chip->reg_lock);
	err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
	if (err)
		goto unlock;
3176
	temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3177 3178 3179 3180 3181 3182
	err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
				       (val & 0xe0ff) | (temp << 8));
unlock:
	mutex_unlock(&chip->reg_lock);

	return err;
3183 3184
}

3185
static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3186
{
V
Vivien Didelot 已提交
3187
	struct mv88e6xxx_chip *chip = ds->priv;
3188
	int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3189
	u16 val;
3190 3191
	int ret;

3192
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3193 3194 3195 3196
		return -EOPNOTSUPP;

	*alarm = false;

3197 3198 3199
	mutex_lock(&chip->reg_lock);
	ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
	mutex_unlock(&chip->reg_lock);
3200 3201 3202
	if (ret < 0)
		return ret;

3203
	*alarm = !!(val & 0x40);
3204 3205 3206 3207 3208

	return 0;
}
#endif /* CONFIG_NET_DSA_HWMON */

3209 3210
static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
{
V
Vivien Didelot 已提交
3211
	struct mv88e6xxx_chip *chip = ds->priv;
3212 3213 3214 3215 3216 3217 3218

	return chip->eeprom_len;
}

static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
				struct ethtool_eeprom *eeprom, u8 *data)
{
V
Vivien Didelot 已提交
3219
	struct mv88e6xxx_chip *chip = ds->priv;
3220 3221 3222 3223 3224
	int err;

	mutex_lock(&chip->reg_lock);

	if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3225
		err = mv88e6xxx_g2_get_eeprom16(chip, eeprom, data);
3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241
	else
		err = -EOPNOTSUPP;

	mutex_unlock(&chip->reg_lock);

	if (err)
		return err;

	eeprom->magic = 0xc3ec4951;

	return 0;
}

static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
				struct ethtool_eeprom *eeprom, u8 *data)
{
V
Vivien Didelot 已提交
3242
	struct mv88e6xxx_chip *chip = ds->priv;
3243 3244 3245 3246 3247 3248 3249 3250
	int err;

	if (eeprom->magic != 0xc3ec4951)
		return -EINVAL;

	mutex_lock(&chip->reg_lock);

	if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3251
		err = mv88e6xxx_g2_set_eeprom16(chip, eeprom, data);
3252 3253 3254 3255 3256 3257 3258 3259
	else
		err = -EOPNOTSUPP;

	mutex_unlock(&chip->reg_lock);

	return err;
}

3260 3261 3262 3263 3264 3265 3266
static const struct mv88e6xxx_info mv88e6xxx_table[] = {
	[MV88E6085] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
		.family = MV88E6XXX_FAMILY_6097,
		.name = "Marvell 88E6085",
		.num_databases = 4096,
		.num_ports = 10,
3267
		.port_base_addr = 0x10,
3268
		.age_time_coeff = 15000,
3269 3270 3271 3272 3273 3274 3275 3276 3277
		.flags = MV88E6XXX_FLAGS_FAMILY_6097,
	},

	[MV88E6095] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
		.family = MV88E6XXX_FAMILY_6095,
		.name = "Marvell 88E6095/88E6095F",
		.num_databases = 256,
		.num_ports = 11,
3278
		.port_base_addr = 0x10,
3279
		.age_time_coeff = 15000,
3280 3281 3282 3283 3284 3285 3286 3287 3288
		.flags = MV88E6XXX_FLAGS_FAMILY_6095,
	},

	[MV88E6123] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
		.family = MV88E6XXX_FAMILY_6165,
		.name = "Marvell 88E6123",
		.num_databases = 4096,
		.num_ports = 3,
3289
		.port_base_addr = 0x10,
3290
		.age_time_coeff = 15000,
3291 3292 3293 3294 3295 3296 3297 3298 3299
		.flags = MV88E6XXX_FLAGS_FAMILY_6165,
	},

	[MV88E6131] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
		.family = MV88E6XXX_FAMILY_6185,
		.name = "Marvell 88E6131",
		.num_databases = 256,
		.num_ports = 8,
3300
		.port_base_addr = 0x10,
3301
		.age_time_coeff = 15000,
3302 3303 3304 3305 3306 3307 3308 3309 3310
		.flags = MV88E6XXX_FLAGS_FAMILY_6185,
	},

	[MV88E6161] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
		.family = MV88E6XXX_FAMILY_6165,
		.name = "Marvell 88E6161",
		.num_databases = 4096,
		.num_ports = 6,
3311
		.port_base_addr = 0x10,
3312
		.age_time_coeff = 15000,
3313 3314 3315 3316 3317 3318 3319 3320 3321
		.flags = MV88E6XXX_FLAGS_FAMILY_6165,
	},

	[MV88E6165] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
		.family = MV88E6XXX_FAMILY_6165,
		.name = "Marvell 88E6165",
		.num_databases = 4096,
		.num_ports = 6,
3322
		.port_base_addr = 0x10,
3323
		.age_time_coeff = 15000,
3324 3325 3326 3327 3328 3329 3330 3331 3332
		.flags = MV88E6XXX_FLAGS_FAMILY_6165,
	},

	[MV88E6171] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
		.family = MV88E6XXX_FAMILY_6351,
		.name = "Marvell 88E6171",
		.num_databases = 4096,
		.num_ports = 7,
3333
		.port_base_addr = 0x10,
3334
		.age_time_coeff = 15000,
3335 3336 3337 3338 3339 3340 3341 3342 3343
		.flags = MV88E6XXX_FLAGS_FAMILY_6351,
	},

	[MV88E6172] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
		.family = MV88E6XXX_FAMILY_6352,
		.name = "Marvell 88E6172",
		.num_databases = 4096,
		.num_ports = 7,
3344
		.port_base_addr = 0x10,
3345
		.age_time_coeff = 15000,
3346 3347 3348 3349 3350 3351 3352 3353 3354
		.flags = MV88E6XXX_FLAGS_FAMILY_6352,
	},

	[MV88E6175] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
		.family = MV88E6XXX_FAMILY_6351,
		.name = "Marvell 88E6175",
		.num_databases = 4096,
		.num_ports = 7,
3355
		.port_base_addr = 0x10,
3356
		.age_time_coeff = 15000,
3357 3358 3359 3360 3361 3362 3363 3364 3365
		.flags = MV88E6XXX_FLAGS_FAMILY_6351,
	},

	[MV88E6176] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
		.family = MV88E6XXX_FAMILY_6352,
		.name = "Marvell 88E6176",
		.num_databases = 4096,
		.num_ports = 7,
3366
		.port_base_addr = 0x10,
3367
		.age_time_coeff = 15000,
3368 3369 3370 3371 3372 3373 3374 3375 3376
		.flags = MV88E6XXX_FLAGS_FAMILY_6352,
	},

	[MV88E6185] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
		.family = MV88E6XXX_FAMILY_6185,
		.name = "Marvell 88E6185",
		.num_databases = 256,
		.num_ports = 10,
3377
		.port_base_addr = 0x10,
3378
		.age_time_coeff = 15000,
3379 3380 3381 3382 3383 3384 3385 3386 3387
		.flags = MV88E6XXX_FLAGS_FAMILY_6185,
	},

	[MV88E6240] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
		.family = MV88E6XXX_FAMILY_6352,
		.name = "Marvell 88E6240",
		.num_databases = 4096,
		.num_ports = 7,
3388
		.port_base_addr = 0x10,
3389
		.age_time_coeff = 15000,
3390 3391 3392 3393 3394 3395 3396 3397 3398
		.flags = MV88E6XXX_FLAGS_FAMILY_6352,
	},

	[MV88E6320] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
		.family = MV88E6XXX_FAMILY_6320,
		.name = "Marvell 88E6320",
		.num_databases = 4096,
		.num_ports = 7,
3399
		.port_base_addr = 0x10,
3400
		.age_time_coeff = 15000,
3401 3402 3403 3404 3405 3406 3407 3408 3409
		.flags = MV88E6XXX_FLAGS_FAMILY_6320,
	},

	[MV88E6321] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
		.family = MV88E6XXX_FAMILY_6320,
		.name = "Marvell 88E6321",
		.num_databases = 4096,
		.num_ports = 7,
3410
		.port_base_addr = 0x10,
3411
		.age_time_coeff = 15000,
3412 3413 3414 3415 3416 3417 3418 3419 3420
		.flags = MV88E6XXX_FLAGS_FAMILY_6320,
	},

	[MV88E6350] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
		.family = MV88E6XXX_FAMILY_6351,
		.name = "Marvell 88E6350",
		.num_databases = 4096,
		.num_ports = 7,
3421
		.port_base_addr = 0x10,
3422
		.age_time_coeff = 15000,
3423 3424 3425 3426 3427 3428 3429 3430 3431
		.flags = MV88E6XXX_FLAGS_FAMILY_6351,
	},

	[MV88E6351] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
		.family = MV88E6XXX_FAMILY_6351,
		.name = "Marvell 88E6351",
		.num_databases = 4096,
		.num_ports = 7,
3432
		.port_base_addr = 0x10,
3433
		.age_time_coeff = 15000,
3434 3435 3436 3437 3438 3439 3440 3441 3442
		.flags = MV88E6XXX_FLAGS_FAMILY_6351,
	},

	[MV88E6352] = {
		.prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
		.family = MV88E6XXX_FAMILY_6352,
		.name = "Marvell 88E6352",
		.num_databases = 4096,
		.num_ports = 7,
3443
		.port_base_addr = 0x10,
3444
		.age_time_coeff = 15000,
3445 3446 3447 3448
		.flags = MV88E6XXX_FLAGS_FAMILY_6352,
	},
};

3449
static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
3450
{
3451
	int i;
3452

3453 3454 3455
	for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
		if (mv88e6xxx_table[i].prod_num == prod_num)
			return &mv88e6xxx_table[i];
3456 3457 3458 3459

	return NULL;
}

3460
static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
3461 3462
{
	const struct mv88e6xxx_info *info;
3463 3464 3465
	unsigned int prod_num, rev;
	u16 id;
	int err;
3466

3467 3468 3469 3470 3471
	mutex_lock(&chip->reg_lock);
	err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
	mutex_unlock(&chip->reg_lock);
	if (err)
		return err;
3472 3473 3474 3475 3476 3477 3478 3479

	prod_num = (id & 0xfff0) >> 4;
	rev = id & 0x000f;

	info = mv88e6xxx_lookup_info(prod_num);
	if (!info)
		return -ENODEV;

3480
	/* Update the compatible info with the probed one */
3481
	chip->info = info;
3482

3483 3484 3485 3486
	err = mv88e6xxx_g2_require(chip);
	if (err)
		return err;

3487 3488
	dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
		 chip->info->prod_num, chip->info->name, rev);
3489 3490 3491 3492

	return 0;
}

3493
static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
3494
{
3495
	struct mv88e6xxx_chip *chip;
3496

3497 3498
	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
	if (!chip)
3499 3500
		return NULL;

3501
	chip->dev = dev;
3502

3503
	mutex_init(&chip->reg_lock);
3504

3505
	return chip;
3506 3507
}

3508 3509 3510 3511 3512
static const struct mv88e6xxx_ops mv88e6xxx_g2_smi_phy_ops = {
	.read = mv88e6xxx_g2_smi_phy_read,
	.write = mv88e6xxx_g2_smi_phy_write,
};

3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529
static const struct mv88e6xxx_ops mv88e6xxx_phy_ops = {
	.read = mv88e6xxx_read,
	.write = mv88e6xxx_write,
};

static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
{
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SMI_PHY)) {
		chip->phy_ops = &mv88e6xxx_g2_smi_phy_ops;
	} else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
		chip->phy_ops = &mv88e6xxx_phy_ppu_ops;
		mv88e6xxx_ppu_state_init(chip);
	} else {
		chip->phy_ops = &mv88e6xxx_phy_ops;
	}
}

3530 3531 3532 3533 3534 3535 3536
static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
{
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
		mv88e6xxx_ppu_state_destroy(chip);
	}
}

3537
static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
3538 3539 3540 3541 3542 3543
			      struct mii_bus *bus, int sw_addr)
{
	/* ADDR[0] pin is unavailable externally and considered zero */
	if (sw_addr & 0x1)
		return -EINVAL;

3544
	if (sw_addr == 0)
3545
		chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
3546
	else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
3547
		chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
3548 3549 3550
	else
		return -EINVAL;

3551 3552
	chip->bus = bus;
	chip->sw_addr = sw_addr;
3553 3554 3555 3556

	return 0;
}

3557 3558
static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
{
V
Vivien Didelot 已提交
3559
	struct mv88e6xxx_chip *chip = ds->priv;
3560 3561 3562 3563 3564

	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
		return DSA_TAG_PROTO_EDSA;

	return DSA_TAG_PROTO_DSA;
3565 3566
}

3567 3568 3569
static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
				       struct device *host_dev, int sw_addr,
				       void **priv)
3570
{
3571
	struct mv88e6xxx_chip *chip;
3572
	struct mii_bus *bus;
3573
	int err;
3574

3575
	bus = dsa_host_dev_to_mii_bus(host_dev);
3576 3577 3578
	if (!bus)
		return NULL;

3579 3580
	chip = mv88e6xxx_alloc_chip(dsa_dev);
	if (!chip)
3581 3582
		return NULL;

3583
	/* Legacy SMI probing will only support chips similar to 88E6085 */
3584
	chip->info = &mv88e6xxx_table[MV88E6085];
3585

3586
	err = mv88e6xxx_smi_init(chip, bus, sw_addr);
3587 3588 3589
	if (err)
		goto free;

3590
	err = mv88e6xxx_detect(chip);
3591
	if (err)
3592
		goto free;
3593

3594 3595
	mv88e6xxx_phy_init(chip);

3596
	err = mv88e6xxx_mdio_register(chip, NULL);
3597
	if (err)
3598
		goto free;
3599

3600
	*priv = chip;
3601

3602
	return chip->info->name;
3603
free:
3604
	devm_kfree(dsa_dev, chip);
3605 3606

	return NULL;
3607 3608
}

3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623
static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
				      const struct switchdev_obj_port_mdb *mdb,
				      struct switchdev_trans *trans)
{
	/* We don't need any dynamic resource from the kernel (yet),
	 * so skip the prepare phase.
	 */

	return 0;
}

static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
				   const struct switchdev_obj_port_mdb *mdb,
				   struct switchdev_trans *trans)
{
V
Vivien Didelot 已提交
3624
	struct mv88e6xxx_chip *chip = ds->priv;
3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635

	mutex_lock(&chip->reg_lock);
	if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
					 GLOBAL_ATU_DATA_STATE_MC_STATIC))
		netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
	mutex_unlock(&chip->reg_lock);
}

static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
				  const struct switchdev_obj_port_mdb *mdb)
{
V
Vivien Didelot 已提交
3636
	struct mv88e6xxx_chip *chip = ds->priv;
3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650
	int err;

	mutex_lock(&chip->reg_lock);
	err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
					   GLOBAL_ATU_DATA_STATE_UNUSED);
	mutex_unlock(&chip->reg_lock);

	return err;
}

static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
				   struct switchdev_obj_port_mdb *mdb,
				   int (*cb)(struct switchdev_obj *obj))
{
V
Vivien Didelot 已提交
3651
	struct mv88e6xxx_chip *chip = ds->priv;
3652 3653 3654 3655 3656 3657 3658 3659 3660
	int err;

	mutex_lock(&chip->reg_lock);
	err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
	mutex_unlock(&chip->reg_lock);

	return err;
}

3661
static struct dsa_switch_ops mv88e6xxx_switch_ops = {
3662
	.probe			= mv88e6xxx_drv_probe,
3663
	.get_tag_protocol	= mv88e6xxx_get_tag_protocol,
3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677
	.setup			= mv88e6xxx_setup,
	.set_addr		= mv88e6xxx_set_addr,
	.adjust_link		= mv88e6xxx_adjust_link,
	.get_strings		= mv88e6xxx_get_strings,
	.get_ethtool_stats	= mv88e6xxx_get_ethtool_stats,
	.get_sset_count		= mv88e6xxx_get_sset_count,
	.set_eee		= mv88e6xxx_set_eee,
	.get_eee		= mv88e6xxx_get_eee,
#ifdef CONFIG_NET_DSA_HWMON
	.get_temp		= mv88e6xxx_get_temp,
	.get_temp_limit		= mv88e6xxx_get_temp_limit,
	.set_temp_limit		= mv88e6xxx_set_temp_limit,
	.get_temp_alarm		= mv88e6xxx_get_temp_alarm,
#endif
3678
	.get_eeprom_len		= mv88e6xxx_get_eeprom_len,
3679 3680 3681 3682
	.get_eeprom		= mv88e6xxx_get_eeprom,
	.set_eeprom		= mv88e6xxx_set_eeprom,
	.get_regs_len		= mv88e6xxx_get_regs_len,
	.get_regs		= mv88e6xxx_get_regs,
3683
	.set_ageing_time	= mv88e6xxx_set_ageing_time,
3684 3685 3686
	.port_bridge_join	= mv88e6xxx_port_bridge_join,
	.port_bridge_leave	= mv88e6xxx_port_bridge_leave,
	.port_stp_state_set	= mv88e6xxx_port_stp_state_set,
3687
	.port_fast_age		= mv88e6xxx_port_fast_age,
3688 3689 3690 3691 3692 3693 3694 3695 3696
	.port_vlan_filtering	= mv88e6xxx_port_vlan_filtering,
	.port_vlan_prepare	= mv88e6xxx_port_vlan_prepare,
	.port_vlan_add		= mv88e6xxx_port_vlan_add,
	.port_vlan_del		= mv88e6xxx_port_vlan_del,
	.port_vlan_dump		= mv88e6xxx_port_vlan_dump,
	.port_fdb_prepare       = mv88e6xxx_port_fdb_prepare,
	.port_fdb_add           = mv88e6xxx_port_fdb_add,
	.port_fdb_del           = mv88e6xxx_port_fdb_del,
	.port_fdb_dump          = mv88e6xxx_port_fdb_dump,
3697 3698 3699 3700
	.port_mdb_prepare       = mv88e6xxx_port_mdb_prepare,
	.port_mdb_add           = mv88e6xxx_port_mdb_add,
	.port_mdb_del           = mv88e6xxx_port_mdb_del,
	.port_mdb_dump          = mv88e6xxx_port_mdb_dump,
3701 3702
};

3703
static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
3704 3705
				     struct device_node *np)
{
3706
	struct device *dev = chip->dev;
3707 3708 3709 3710 3711 3712 3713
	struct dsa_switch *ds;

	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
	if (!ds)
		return -ENOMEM;

	ds->dev = dev;
3714
	ds->priv = chip;
3715
	ds->ops = &mv88e6xxx_switch_ops;
3716 3717 3718 3719 3720 3721

	dev_set_drvdata(dev, ds);

	return dsa_register_switch(ds, np);
}

3722
static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
3723
{
3724
	dsa_unregister_switch(chip->ds);
3725 3726
}

3727
static int mv88e6xxx_probe(struct mdio_device *mdiodev)
3728
{
3729
	struct device *dev = &mdiodev->dev;
3730
	struct device_node *np = dev->of_node;
3731
	const struct mv88e6xxx_info *compat_info;
3732
	struct mv88e6xxx_chip *chip;
3733
	u32 eeprom_len;
3734
	int err;
3735

3736 3737 3738 3739
	compat_info = of_device_get_match_data(dev);
	if (!compat_info)
		return -EINVAL;

3740 3741
	chip = mv88e6xxx_alloc_chip(dev);
	if (!chip)
3742 3743
		return -ENOMEM;

3744
	chip->info = compat_info;
3745

3746
	err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
3747 3748
	if (err)
		return err;
3749

3750
	err = mv88e6xxx_detect(chip);
3751 3752
	if (err)
		return err;
3753

3754 3755
	mv88e6xxx_phy_init(chip);

3756 3757 3758
	chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
	if (IS_ERR(chip->reset))
		return PTR_ERR(chip->reset);
3759

3760
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
3761
	    !of_property_read_u32(np, "eeprom-length", &eeprom_len))
3762
		chip->eeprom_len = eeprom_len;
3763

3764
	err = mv88e6xxx_mdio_register(chip, np);
3765 3766 3767
	if (err)
		return err;

3768
	err = mv88e6xxx_register_switch(chip, np);
3769
	if (err) {
3770
		mv88e6xxx_mdio_unregister(chip);
3771 3772 3773
		return err;
	}

3774 3775
	return 0;
}
3776 3777 3778 3779

static void mv88e6xxx_remove(struct mdio_device *mdiodev)
{
	struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
V
Vivien Didelot 已提交
3780
	struct mv88e6xxx_chip *chip = ds->priv;
3781

3782
	mv88e6xxx_phy_destroy(chip);
3783 3784
	mv88e6xxx_unregister_switch(chip);
	mv88e6xxx_mdio_unregister(chip);
3785 3786 3787
}

static const struct of_device_id mv88e6xxx_of_match[] = {
3788 3789 3790 3791
	{
		.compatible = "marvell,mv88e6085",
		.data = &mv88e6xxx_table[MV88E6085],
	},
3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807
	{ /* sentinel */ },
};

MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);

static struct mdio_driver mv88e6xxx_driver = {
	.probe	= mv88e6xxx_probe,
	.remove = mv88e6xxx_remove,
	.mdiodrv.driver = {
		.name = "mv88e6085",
		.of_match_table = mv88e6xxx_of_match,
	},
};

static int __init mv88e6xxx_init(void)
{
3808
	register_switch_driver(&mv88e6xxx_switch_ops);
3809 3810
	return mdio_driver_register(&mv88e6xxx_driver);
}
3811 3812 3813 3814
module_init(mv88e6xxx_init);

static void __exit mv88e6xxx_cleanup(void)
{
3815
	mdio_driver_unregister(&mv88e6xxx_driver);
3816
	unregister_switch_driver(&mv88e6xxx_switch_ops);
3817 3818
}
module_exit(mv88e6xxx_cleanup);
3819 3820 3821 3822

MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
MODULE_LICENSE("GPL");