chip.c 93.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
static void assert_reg_lock(struct mv88e6xxx_chip *chip)
35
{
36 37
	if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
		dev_err(chip->dev, "Switch registers lock not held!\n");
38 39 40 41
		dump_stack();
	}
}

42 43 44 45 46 47 48 49 50 51
/* 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.
52
 */
53

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

60
	return chip->smi_ops->read(chip, addr, reg, val);
61 62
}

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

69
	return chip->smi_ops->write(chip, addr, reg, val);
70 71
}

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

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

	*val = ret & 0xffff;

	return 0;
}

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

91
	ret = mdiobus_write_nested(chip->bus, addr, reg, val);
92 93 94 95 96 97 98 99 100 101 102
	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,
};

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

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

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

	return -ETIMEDOUT;
}

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

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

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

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

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

146
	*val = ret & 0xffff;
147

148
	return 0;
149 150
}

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

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

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

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

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

	return 0;
}

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

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

190
	assert_reg_lock(chip);
191

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

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

	return 0;
}

202
static int mv88e6xxx_write(struct mv88e6xxx_chip *chip,
203
			   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
static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
220 221 222 223
{
	u16 val;
	int err;

224
	err = mv88e6xxx_read(chip, addr, reg, &val);
225 226 227 228 229 230
	if (err)
		return err;

	return val;
}

231
static int mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
232 233 234
{
	int ret;

235 236 237
	mutex_lock(&chip->reg_lock);
	ret = _mv88e6xxx_reg_read(chip, addr, reg);
	mutex_unlock(&chip->reg_lock);
238 239 240 241

	return ret;
}

242
static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
243 244
				int reg, u16 val)
{
245
	return mv88e6xxx_write(chip, addr, reg, val);
246 247
}

248
static int mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
249
			       int reg, u16 val)
250 251 252
{
	int ret;

253 254 255
	mutex_lock(&chip->reg_lock);
	ret = _mv88e6xxx_reg_write(chip, addr, reg, val);
	mutex_unlock(&chip->reg_lock);
256 257 258 259

	return ret;
}

260
static int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
261
{
262
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
263
	int err;
264

265
	err = mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
266 267 268 269
				  (addr[0] << 8) | addr[1]);
	if (err)
		return err;

270
	err = mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
271 272 273 274
				  (addr[2] << 8) | addr[3]);
	if (err)
		return err;

275
	return mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
276
				   (addr[4] << 8) | addr[5]);
277 278
}

279
static int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
280
{
281
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
282
	int ret;
283
	int i;
284 285 286 287

	for (i = 0; i < 6; i++) {
		int j;

288
		/* Write the MAC address byte. */
289
		ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
290 291 292 293
					  GLOBAL2_SWITCH_MAC_BUSY |
					  (i << 8) | addr[i]);
		if (ret)
			return ret;
294

295
		/* Wait for the write to complete. */
296
		for (j = 0; j < 16; j++) {
297
			ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2,
298 299 300 301
						 GLOBAL2_SWITCH_MAC);
			if (ret < 0)
				return ret;

302
			if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
303 304 305 306 307 308 309 310 311
				break;
		}
		if (j == 16)
			return -ETIMEDOUT;
	}

	return 0;
}

312
static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
313
{
314
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
315

316
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SWITCH_MAC))
317 318 319 320 321
		return mv88e6xxx_set_addr_indirect(ds, addr);
	else
		return mv88e6xxx_set_addr_direct(ds, addr);
}

322
static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_chip *chip,
323
				      int addr, int regnum)
324 325
{
	if (addr >= 0)
326
		return _mv88e6xxx_reg_read(chip, addr, regnum);
327 328 329
	return 0xffff;
}

330
static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_chip *chip,
331
				       int addr, int regnum, u16 val)
332 333
{
	if (addr >= 0)
334
		return _mv88e6xxx_reg_write(chip, addr, regnum, val);
335 336 337
	return 0;
}

338
static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
339 340
{
	int ret;
341
	unsigned long timeout;
342

343
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
344 345 346
	if (ret < 0)
		return ret;

347
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
348
				   ret & ~GLOBAL_CONTROL_PPU_ENABLE);
349 350
	if (ret)
		return ret;
351

352 353
	timeout = jiffies + 1 * HZ;
	while (time_before(jiffies, timeout)) {
354
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
355 356 357
		if (ret < 0)
			return ret;

358
		usleep_range(1000, 2000);
359 360
		if ((ret & GLOBAL_STATUS_PPU_MASK) !=
		    GLOBAL_STATUS_PPU_POLLING)
361
			return 0;
362 363 364 365 366
	}

	return -ETIMEDOUT;
}

367
static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
368
{
369
	int ret, err;
370
	unsigned long timeout;
371

372
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
373 374 375
	if (ret < 0)
		return ret;

376
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
377
				   ret | GLOBAL_CONTROL_PPU_ENABLE);
378 379
	if (err)
		return err;
380

381 382
	timeout = jiffies + 1 * HZ;
	while (time_before(jiffies, timeout)) {
383
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
384 385 386
		if (ret < 0)
			return ret;

387
		usleep_range(1000, 2000);
388 389
		if ((ret & GLOBAL_STATUS_PPU_MASK) ==
		    GLOBAL_STATUS_PPU_POLLING)
390
			return 0;
391 392 393 394 395 396 397
	}

	return -ETIMEDOUT;
}

static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
{
398
	struct mv88e6xxx_chip *chip;
399

400
	chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
401

402
	mutex_lock(&chip->reg_lock);
403

404 405 406 407
	if (mutex_trylock(&chip->ppu_mutex)) {
		if (mv88e6xxx_ppu_enable(chip) == 0)
			chip->ppu_disabled = 0;
		mutex_unlock(&chip->ppu_mutex);
408
	}
409

410
	mutex_unlock(&chip->reg_lock);
411 412 413 414
}

static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
{
415
	struct mv88e6xxx_chip *chip = (void *)_ps;
416

417
	schedule_work(&chip->ppu_work);
418 419
}

420
static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
421 422 423
{
	int ret;

424
	mutex_lock(&chip->ppu_mutex);
425

426
	/* If the PHY polling unit is enabled, disable it so that
427 428 429 430
	 * we can access the PHY registers.  If it was already
	 * disabled, cancel the timer that is going to re-enable
	 * it.
	 */
431 432
	if (!chip->ppu_disabled) {
		ret = mv88e6xxx_ppu_disable(chip);
433
		if (ret < 0) {
434
			mutex_unlock(&chip->ppu_mutex);
435 436
			return ret;
		}
437
		chip->ppu_disabled = 1;
438
	} else {
439
		del_timer(&chip->ppu_timer);
440
		ret = 0;
441 442 443 444 445
	}

	return ret;
}

446
static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
447
{
448
	/* Schedule a timer to re-enable the PHY polling unit. */
449 450
	mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
	mutex_unlock(&chip->ppu_mutex);
451 452
}

453
static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
454
{
455 456 457 458 459
	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;
460 461
}

462
static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_chip *chip, int addr,
463
				   int regnum)
464 465 466
{
	int ret;

467
	ret = mv88e6xxx_ppu_access_get(chip);
468
	if (ret >= 0) {
469 470
		ret = _mv88e6xxx_reg_read(chip, addr, regnum);
		mv88e6xxx_ppu_access_put(chip);
471 472 473 474 475
	}

	return ret;
}

476
static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_chip *chip, int addr,
477
				    int regnum, u16 val)
478 479 480
{
	int ret;

481
	ret = mv88e6xxx_ppu_access_get(chip);
482
	if (ret >= 0) {
483 484
		ret = _mv88e6xxx_reg_write(chip, addr, regnum, val);
		mv88e6xxx_ppu_access_put(chip);
485 486 487 488 489
	}

	return ret;
}

490
static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
491
{
492
	return chip->info->family == MV88E6XXX_FAMILY_6065;
493 494
}

495
static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
496
{
497
	return chip->info->family == MV88E6XXX_FAMILY_6095;
498 499
}

500
static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
501
{
502
	return chip->info->family == MV88E6XXX_FAMILY_6097;
503 504
}

505
static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
506
{
507
	return chip->info->family == MV88E6XXX_FAMILY_6165;
508 509
}

510
static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
511
{
512
	return chip->info->family == MV88E6XXX_FAMILY_6185;
513 514
}

515
static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
516
{
517
	return chip->info->family == MV88E6XXX_FAMILY_6320;
518 519
}

520
static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
521
{
522
	return chip->info->family == MV88E6XXX_FAMILY_6351;
523 524
}

525
static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
526
{
527
	return chip->info->family == MV88E6XXX_FAMILY_6352;
528 529
}

530
static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
531
{
532
	return chip->info->num_databases;
533 534
}

535
static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
536 537
{
	/* Does the device have dedicated FID registers for ATU and VTU ops? */
538 539
	if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
	    mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
540 541 542 543 544
		return true;

	return false;
}

545 546 547 548
/* 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.
 */
549 550
static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
				  struct phy_device *phydev)
551
{
552
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
553 554
	u32 reg;
	int ret;
555 556 557 558

	if (!phy_is_pseudo_fixed_link(phydev))
		return;

559
	mutex_lock(&chip->reg_lock);
560

561
	ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
562 563 564 565 566 567 568 569 570 571 572
	if (ret < 0)
		goto out;

	reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
		      PORT_PCS_CTRL_FORCE_LINK |
		      PORT_PCS_CTRL_DUPLEX_FULL |
		      PORT_PCS_CTRL_FORCE_DUPLEX |
		      PORT_PCS_CTRL_UNFORCED);

	reg |= PORT_PCS_CTRL_FORCE_LINK;
	if (phydev->link)
573
		reg |= PORT_PCS_CTRL_LINK_UP;
574

575
	if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
		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;

597 598
	if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
	    (port >= chip->info->num_ports - 2)) {
599 600 601 602 603 604 605 606
		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);
	}
607
	_mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
608 609

out:
610
	mutex_unlock(&chip->reg_lock);
611 612
}

613
static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
614 615 616 617 618
{
	int ret;
	int i;

	for (i = 0; i < 10; i++) {
619
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
620
		if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
621 622 623 624 625 626
			return 0;
	}

	return -ETIMEDOUT;
}

627
static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
628 629 630
{
	int ret;

631
	if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
632 633
		port = (port + 1) << 5;

634
	/* Snapshot the hardware statistics counters for this port. */
635
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
636 637 638 639
				   GLOBAL_STATS_OP_CAPTURE_PORT |
				   GLOBAL_STATS_OP_HIST_RX_TX | port);
	if (ret < 0)
		return ret;
640

641
	/* Wait for the snapshotting to complete. */
642
	ret = _mv88e6xxx_stats_wait(chip);
643 644 645 646 647 648
	if (ret < 0)
		return ret;

	return 0;
}

649
static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
650
				  int stat, u32 *val)
651 652 653 654 655 656
{
	u32 _val;
	int ret;

	*val = 0;

657
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
658 659
				   GLOBAL_STATS_OP_READ_CAPTURED |
				   GLOBAL_STATS_OP_HIST_RX_TX | stat);
660 661 662
	if (ret < 0)
		return;

663
	ret = _mv88e6xxx_stats_wait(chip);
664 665 666
	if (ret < 0)
		return;

667
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
668 669 670 671 672
	if (ret < 0)
		return;

	_val = ret << 16;

673
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
674 675 676 677 678 679
	if (ret < 0)
		return;

	*val = _val | ret;
}

680
static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
	{ "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, },
740 741
};

742
static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
743
			       struct mv88e6xxx_hw_stat *stat)
744
{
745 746
	switch (stat->type) {
	case BANK0:
747
		return true;
748
	case BANK1:
749
		return mv88e6xxx_6320_family(chip);
750
	case PORT:
751 752 753 754 755 756
		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);
757
	}
758
	return false;
759 760
}

761
static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
762
					    struct mv88e6xxx_hw_stat *s,
763 764 765 766 767 768 769
					    int port)
{
	u32 low;
	u32 high = 0;
	int ret;
	u64 value;

770 771
	switch (s->type) {
	case PORT:
772
		ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
773 774 775 776 777
		if (ret < 0)
			return UINT64_MAX;

		low = ret;
		if (s->sizeof_stat == 4) {
778
			ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
779
						  s->reg + 1);
780 781 782 783
			if (ret < 0)
				return UINT64_MAX;
			high = ret;
		}
784 785 786
		break;
	case BANK0:
	case BANK1:
787
		_mv88e6xxx_stats_read(chip, s->reg, &low);
788
		if (s->sizeof_stat == 8)
789
			_mv88e6xxx_stats_read(chip, s->reg + 1, &high);
790 791 792 793 794
	}
	value = (((u64)high) << 16) | low;
	return value;
}

795 796
static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
				  uint8_t *data)
797
{
798
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
799 800
	struct mv88e6xxx_hw_stat *stat;
	int i, j;
801

802 803
	for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
		stat = &mv88e6xxx_hw_stats[i];
804
		if (mv88e6xxx_has_stat(chip, stat)) {
805 806 807 808
			memcpy(data + j * ETH_GSTRING_LEN, stat->string,
			       ETH_GSTRING_LEN);
			j++;
		}
809
	}
810 811
}

812
static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
813
{
814
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
815 816 817 818 819
	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];
820
		if (mv88e6xxx_has_stat(chip, stat))
821 822 823
			j++;
	}
	return j;
824 825
}

826 827
static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
					uint64_t *data)
828
{
829
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
830 831 832 833
	struct mv88e6xxx_hw_stat *stat;
	int ret;
	int i, j;

834
	mutex_lock(&chip->reg_lock);
835

836
	ret = _mv88e6xxx_stats_snapshot(chip, port);
837
	if (ret < 0) {
838
		mutex_unlock(&chip->reg_lock);
839 840 841 842
		return;
	}
	for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
		stat = &mv88e6xxx_hw_stats[i];
843 844
		if (mv88e6xxx_has_stat(chip, stat)) {
			data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
845 846 847 848
			j++;
		}
	}

849
	mutex_unlock(&chip->reg_lock);
850 851
}

852
static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
853 854 855 856
{
	return 32 * sizeof(u16);
}

857 858
static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
			       struct ethtool_regs *regs, void *_p)
859
{
860
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
861 862 863 864 865 866 867
	u16 *p = _p;
	int i;

	regs->version = 0;

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

868
	mutex_lock(&chip->reg_lock);
869

870 871 872
	for (i = 0; i < 32; i++) {
		int ret;

873
		ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
874 875 876
		if (ret >= 0)
			p[i] = ret;
	}
877

878
	mutex_unlock(&chip->reg_lock);
879 880
}

881
static int _mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int reg, int offset,
882
			   u16 mask)
883 884 885 886 887 888
{
	unsigned long timeout = jiffies + HZ / 10;

	while (time_before(jiffies, timeout)) {
		int ret;

889
		ret = _mv88e6xxx_reg_read(chip, reg, offset);
890 891
		if (ret < 0)
			return ret;
892 893 894 895 896 897 898 899
		if (!(ret & mask))
			return 0;

		usleep_range(1000, 2000);
	}
	return -ETIMEDOUT;
}

900
static int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int reg,
901
			  int offset, u16 mask)
902 903 904
{
	int ret;

905 906 907
	mutex_lock(&chip->reg_lock);
	ret = _mv88e6xxx_wait(chip, reg, offset, mask);
	mutex_unlock(&chip->reg_lock);
908 909 910 911

	return ret;
}

912
static int mv88e6xxx_mdio_wait(struct mv88e6xxx_chip *chip)
913
{
914
	return _mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
915
			       GLOBAL2_SMI_OP_BUSY);
916 917
}

918
static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
919
{
920
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
921

922
	return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
923
			      GLOBAL2_EEPROM_OP_LOAD);
924 925
}

926
static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
927
{
928
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
929

930
	return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
931
			      GLOBAL2_EEPROM_OP_BUSY);
932 933
}

934 935
static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr)
{
936
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
937 938
	int ret;

939
	mutex_lock(&chip->eeprom_mutex);
940

941
	ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
942 943 944 945 946 947 948 949 950
				  GLOBAL2_EEPROM_OP_READ |
				  (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
	if (ret < 0)
		goto error;

	ret = mv88e6xxx_eeprom_busy_wait(ds);
	if (ret < 0)
		goto error;

951
	ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
952
error:
953
	mutex_unlock(&chip->eeprom_mutex);
954 955 956
	return ret;
}

957 958
static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
{
959
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
960

961 962
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
		return chip->eeprom_len;
963 964 965 966

	return 0;
}

967 968
static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
				struct ethtool_eeprom *eeprom, u8 *data)
969
{
970
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
971 972 973 974
	int offset;
	int len;
	int ret;

975
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
		return -EOPNOTSUPP;

	offset = eeprom->offset;
	len = eeprom->len;
	eeprom->len = 0;

	eeprom->magic = 0xc3ec4951;

	ret = mv88e6xxx_eeprom_load_wait(ds);
	if (ret < 0)
		return ret;

	if (offset & 1) {
		int word;

		word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
		if (word < 0)
			return word;

		*data++ = (word >> 8) & 0xff;

		offset++;
		len--;
		eeprom->len++;
	}

	while (len >= 2) {
		int word;

		word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
		if (word < 0)
			return word;

		*data++ = word & 0xff;
		*data++ = (word >> 8) & 0xff;

		offset += 2;
		len -= 2;
		eeprom->len += 2;
	}

	if (len) {
		int word;

		word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
		if (word < 0)
			return word;

		*data++ = word & 0xff;

		offset++;
		len--;
		eeprom->len++;
	}

	return 0;
}

static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds)
{
1036
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1037 1038
	int ret;

1039
	ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
	if (ret < 0)
		return ret;

	if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
		return -EROFS;

	return 0;
}

static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr,
				       u16 data)
{
1052
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1053 1054
	int ret;

1055
	mutex_lock(&chip->eeprom_mutex);
1056

1057
	ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
1058 1059 1060
	if (ret < 0)
		goto error;

1061
	ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
1062 1063 1064 1065 1066 1067 1068
				  GLOBAL2_EEPROM_OP_WRITE |
				  (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
	if (ret < 0)
		goto error;

	ret = mv88e6xxx_eeprom_busy_wait(ds);
error:
1069
	mutex_unlock(&chip->eeprom_mutex);
1070 1071 1072
	return ret;
}

1073 1074
static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
				struct ethtool_eeprom *eeprom, u8 *data)
1075
{
1076
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1077 1078 1079 1080
	int offset;
	int ret;
	int len;

1081
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
		return -EOPNOTSUPP;

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

	ret = mv88e6xxx_eeprom_is_readonly(ds);
	if (ret)
		return ret;

	offset = eeprom->offset;
	len = eeprom->len;
	eeprom->len = 0;

	ret = mv88e6xxx_eeprom_load_wait(ds);
	if (ret < 0)
		return ret;

	if (offset & 1) {
		int word;

		word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
		if (word < 0)
			return word;

		word = (*data++ << 8) | (word & 0xff);

		ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
		if (ret < 0)
			return ret;

		offset++;
		len--;
		eeprom->len++;
	}

	while (len >= 2) {
		int word;

		word = *data++;
		word |= *data++ << 8;

		ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
		if (ret < 0)
			return ret;

		offset += 2;
		len -= 2;
		eeprom->len += 2;
	}

	if (len) {
		int word;

		word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
		if (word < 0)
			return word;

		word = (word & 0xff00) | *data++;

		ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
		if (ret < 0)
			return ret;

		offset++;
		len--;
		eeprom->len++;
	}

	return 0;
}

1153
static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
1154
{
1155
	return _mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
1156
			       GLOBAL_ATU_OP_BUSY);
1157 1158
}

1159
static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_chip *chip,
1160
					int addr, int regnum)
1161 1162 1163
{
	int ret;

1164
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
1165 1166 1167 1168
				   GLOBAL2_SMI_OP_22_READ | (addr << 5) |
				   regnum);
	if (ret < 0)
		return ret;
1169

1170
	ret = mv88e6xxx_mdio_wait(chip);
1171 1172 1173
	if (ret < 0)
		return ret;

1174
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA);
1175 1176

	return ret;
1177 1178
}

1179
static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_chip *chip,
1180
					 int addr, int regnum, u16 val)
1181
{
1182 1183
	int ret;

1184
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
1185 1186
	if (ret < 0)
		return ret;
1187

1188
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
1189 1190 1191
				   GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
				   regnum);

1192
	return mv88e6xxx_mdio_wait(chip);
1193 1194
}

1195 1196
static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
			     struct ethtool_eee *e)
1197
{
1198
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1199 1200
	int reg;

1201
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
1202 1203
		return -EOPNOTSUPP;

1204
	mutex_lock(&chip->reg_lock);
1205

1206
	reg = mv88e6xxx_mdio_read_indirect(chip, port, 16);
1207
	if (reg < 0)
1208
		goto out;
1209 1210 1211 1212

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

1213
	reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
1214
	if (reg < 0)
1215
		goto out;
1216

1217
	e->eee_active = !!(reg & PORT_STATUS_EEE);
1218
	reg = 0;
1219

1220
out:
1221
	mutex_unlock(&chip->reg_lock);
1222
	return reg;
1223 1224
}

1225 1226
static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
			     struct phy_device *phydev, struct ethtool_eee *e)
1227
{
1228
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1229
	int reg;
1230 1231
	int ret;

1232
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
1233 1234
		return -EOPNOTSUPP;

1235
	mutex_lock(&chip->reg_lock);
1236

1237
	ret = mv88e6xxx_mdio_read_indirect(chip, port, 16);
1238 1239 1240 1241 1242 1243 1244 1245 1246
	if (ret < 0)
		goto out;

	reg = ret & ~0x0300;
	if (e->eee_enabled)
		reg |= 0x0200;
	if (e->tx_lpi_enabled)
		reg |= 0x0100;

1247
	ret = mv88e6xxx_mdio_write_indirect(chip, port, 16, reg);
1248
out:
1249
	mutex_unlock(&chip->reg_lock);
1250 1251

	return ret;
1252 1253
}

1254
static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
1255 1256 1257
{
	int ret;

1258 1259 1260
	if (mv88e6xxx_has_fid_reg(chip)) {
		ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
					   fid);
1261 1262
		if (ret < 0)
			return ret;
1263
	} else if (mv88e6xxx_num_databases(chip) == 256) {
1264
		/* ATU DBNum[7:4] are located in ATU Control 15:12 */
1265
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
1266 1267 1268
		if (ret < 0)
			return ret;

1269
		ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
1270 1271 1272 1273 1274 1275 1276
					   (ret & 0xfff) |
					   ((fid << 8) & 0xf000));
		if (ret < 0)
			return ret;

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

1279
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
1280 1281 1282
	if (ret < 0)
		return ret;

1283
	return _mv88e6xxx_atu_wait(chip);
1284 1285
}

1286
static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
				     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;
	}

1306
	return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
1307 1308
}

1309
static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
1310 1311
				     struct mv88e6xxx_atu_entry *entry,
				     bool static_too)
1312
{
1313 1314
	int op;
	int err;
1315

1316
	err = _mv88e6xxx_atu_wait(chip);
1317 1318
	if (err)
		return err;
1319

1320
	err = _mv88e6xxx_atu_data_write(chip, entry);
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
	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;
	}

1332
	return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
1333 1334
}

1335
static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
1336
				u16 fid, bool static_too)
1337 1338 1339 1340 1341
{
	struct mv88e6xxx_atu_entry entry = {
		.fid = fid,
		.state = 0, /* EntryState bits must be 0 */
	};
1342

1343
	return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1344 1345
}

1346
static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
1347
			       int from_port, int to_port, bool static_too)
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
{
	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;

1361
	return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1362 1363
}

1364
static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
1365
				 int port, bool static_too)
1366 1367
{
	/* Destination port 0xF means remove the entries */
1368
	return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
1369 1370
}

1371 1372 1373 1374 1375 1376 1377
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",
};

1378
static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
1379
				 u8 state)
1380
{
1381
	struct dsa_switch *ds = chip->ds;
1382
	int reg, ret = 0;
1383 1384
	u8 oldstate;

1385
	reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
1386 1387
	if (reg < 0)
		return reg;
1388

1389
	oldstate = reg & PORT_CONTROL_STATE_MASK;
1390

1391 1392 1393 1394 1395
	if (oldstate != state) {
		/* Flush forwarding database if we're moving a port
		 * from Learning or Forwarding state to Disabled or
		 * Blocking or Listening state.
		 */
1396
		if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1397 1398 1399
		     oldstate == PORT_CONTROL_STATE_FORWARDING) &&
		    (state == PORT_CONTROL_STATE_DISABLED ||
		     state == PORT_CONTROL_STATE_BLOCKING)) {
1400
			ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
1401
			if (ret)
1402
				return ret;
1403
		}
1404

1405
		reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1406
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
1407
					   reg);
1408 1409 1410
		if (ret)
			return ret;

1411
		netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
1412 1413
			   mv88e6xxx_port_state_names[state],
			   mv88e6xxx_port_state_names[oldstate]);
1414 1415 1416 1417 1418
	}

	return ret;
}

1419
static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
1420
{
1421 1422 1423
	struct net_device *bridge = chip->ports[port].bridge_dev;
	const u16 mask = (1 << chip->info->num_ports) - 1;
	struct dsa_switch *ds = chip->ds;
1424
	u16 output_ports = 0;
1425
	int reg;
1426 1427 1428 1429 1430 1431
	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 {
1432
		for (i = 0; i < chip->info->num_ports; ++i) {
1433
			/* allow sending frames to every group member */
1434
			if (bridge && chip->ports[i].bridge_dev == bridge)
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
				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);
1445

1446
	reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
1447 1448
	if (reg < 0)
		return reg;
1449

1450 1451
	reg &= ~mask;
	reg |= output_ports & mask;
1452

1453
	return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
1454 1455
}

1456 1457
static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
					 u8 state)
1458
{
1459
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1460
	int stp_state;
1461
	int err;
1462 1463 1464

	switch (state) {
	case BR_STATE_DISABLED:
1465
		stp_state = PORT_CONTROL_STATE_DISABLED;
1466 1467 1468
		break;
	case BR_STATE_BLOCKING:
	case BR_STATE_LISTENING:
1469
		stp_state = PORT_CONTROL_STATE_BLOCKING;
1470 1471
		break;
	case BR_STATE_LEARNING:
1472
		stp_state = PORT_CONTROL_STATE_LEARNING;
1473 1474 1475
		break;
	case BR_STATE_FORWARDING:
	default:
1476
		stp_state = PORT_CONTROL_STATE_FORWARDING;
1477 1478 1479
		break;
	}

1480 1481 1482
	mutex_lock(&chip->reg_lock);
	err = _mv88e6xxx_port_state(chip, port, stp_state);
	mutex_unlock(&chip->reg_lock);
1483 1484

	if (err)
1485 1486
		netdev_err(ds->ports[port].netdev,
			   "failed to update state to %s\n",
1487
			   mv88e6xxx_port_state_names[stp_state]);
1488 1489
}

1490
static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
1491
				u16 *new, u16 *old)
1492
{
1493
	struct dsa_switch *ds = chip->ds;
1494
	u16 pvid;
1495 1496
	int ret;

1497
	ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
1498 1499 1500
	if (ret < 0)
		return ret;

1501 1502 1503 1504 1505 1506
	pvid = ret & PORT_DEFAULT_VLAN_MASK;

	if (new) {
		ret &= ~PORT_DEFAULT_VLAN_MASK;
		ret |= *new & PORT_DEFAULT_VLAN_MASK;

1507
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
1508 1509 1510 1511
					   PORT_DEFAULT_VLAN, ret);
		if (ret < 0)
			return ret;

1512 1513
		netdev_dbg(ds->ports[port].netdev,
			   "DefaultVID %d (was %d)\n", *new, pvid);
1514 1515 1516 1517
	}

	if (old)
		*old = pvid;
1518 1519 1520 1521

	return 0;
}

1522
static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
1523
				    int port, u16 *pvid)
1524
{
1525
	return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
1526 1527
}

1528
static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
1529
				    int port, u16 pvid)
1530
{
1531
	return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
1532 1533
}

1534
static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
1535
{
1536
	return _mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
1537 1538 1539
			       GLOBAL_VTU_OP_BUSY);
}

1540
static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
1541 1542 1543
{
	int ret;

1544
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
1545 1546 1547
	if (ret < 0)
		return ret;

1548
	return _mv88e6xxx_vtu_wait(chip);
1549 1550
}

1551
static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
1552 1553 1554
{
	int ret;

1555
	ret = _mv88e6xxx_vtu_wait(chip);
1556 1557 1558
	if (ret < 0)
		return ret;

1559
	return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
1560 1561
}

1562
static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
1563 1564 1565 1566 1567 1568 1569 1570
					struct mv88e6xxx_vtu_stu_entry *entry,
					unsigned int nibble_offset)
{
	u16 regs[3];
	int i;
	int ret;

	for (i = 0; i < 3; ++i) {
1571
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1572 1573 1574 1575 1576 1577 1578
					  GLOBAL_VTU_DATA_0_3 + i);
		if (ret < 0)
			return ret;

		regs[i] = ret;
	}

1579
	for (i = 0; i < chip->info->num_ports; ++i) {
1580 1581 1582 1583 1584 1585 1586 1587 1588
		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;
}

1589
static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
1590 1591
				   struct mv88e6xxx_vtu_stu_entry *entry)
{
1592
	return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
1593 1594
}

1595
static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
1596 1597
				   struct mv88e6xxx_vtu_stu_entry *entry)
{
1598
	return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
1599 1600
}

1601
static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
1602 1603 1604 1605 1606 1607 1608
					 struct mv88e6xxx_vtu_stu_entry *entry,
					 unsigned int nibble_offset)
{
	u16 regs[3] = { 0 };
	int i;
	int ret;

1609
	for (i = 0; i < chip->info->num_ports; ++i) {
1610 1611 1612 1613 1614 1615 1616
		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) {
1617
		ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
1618 1619 1620 1621 1622 1623 1624 1625
					   GLOBAL_VTU_DATA_0_3 + i, regs[i]);
		if (ret < 0)
			return ret;
	}

	return 0;
}

1626
static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
1627 1628
				    struct mv88e6xxx_vtu_stu_entry *entry)
{
1629
	return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
1630 1631
}

1632
static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
1633 1634
				    struct mv88e6xxx_vtu_stu_entry *entry)
{
1635
	return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
1636 1637
}

1638
static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
1639
{
1640
	return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
1641 1642 1643
				    vid & GLOBAL_VTU_VID_MASK);
}

1644
static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
1645 1646 1647 1648 1649
				  struct mv88e6xxx_vtu_stu_entry *entry)
{
	struct mv88e6xxx_vtu_stu_entry next = { 0 };
	int ret;

1650
	ret = _mv88e6xxx_vtu_wait(chip);
1651 1652 1653
	if (ret < 0)
		return ret;

1654
	ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1655 1656 1657
	if (ret < 0)
		return ret;

1658
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1659 1660 1661 1662 1663 1664 1665
	if (ret < 0)
		return ret;

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

	if (next.valid) {
1666
		ret = mv88e6xxx_vtu_data_read(chip, &next);
1667 1668 1669
		if (ret < 0)
			return ret;

1670 1671
		if (mv88e6xxx_has_fid_reg(chip)) {
			ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1672 1673 1674 1675 1676
						  GLOBAL_VTU_FID);
			if (ret < 0)
				return ret;

			next.fid = ret & GLOBAL_VTU_FID_MASK;
1677
		} else if (mv88e6xxx_num_databases(chip) == 256) {
1678 1679 1680
			/* VTU DBNum[7:4] are located in VTU Operation 11:8, and
			 * VTU DBNum[3:0] are located in VTU Operation 3:0
			 */
1681
			ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1682 1683 1684 1685 1686 1687
						  GLOBAL_VTU_OP);
			if (ret < 0)
				return ret;

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

1690 1691
		if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
			ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
						  GLOBAL_VTU_SID);
			if (ret < 0)
				return ret;

			next.sid = ret & GLOBAL_VTU_SID_MASK;
		}
	}

	*entry = next;
	return 0;
}

1704 1705 1706
static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
				    struct switchdev_obj_port_vlan *vlan,
				    int (*cb)(struct switchdev_obj *obj))
1707
{
1708
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1709 1710 1711 1712
	struct mv88e6xxx_vtu_stu_entry next;
	u16 pvid;
	int err;

1713
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1714 1715
		return -EOPNOTSUPP;

1716
	mutex_lock(&chip->reg_lock);
1717

1718
	err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
1719 1720 1721
	if (err)
		goto unlock;

1722
	err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1723 1724 1725 1726
	if (err)
		goto unlock;

	do {
1727
		err = _mv88e6xxx_vtu_getnext(chip, &next);
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
		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 */
1738 1739
		vlan->vid_begin = next.vid;
		vlan->vid_end = next.vid;
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
		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:
1754
	mutex_unlock(&chip->reg_lock);
1755 1756 1757 1758

	return err;
}

1759
static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
1760 1761
				    struct mv88e6xxx_vtu_stu_entry *entry)
{
1762
	u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
1763 1764 1765
	u16 reg = 0;
	int ret;

1766
	ret = _mv88e6xxx_vtu_wait(chip);
1767 1768 1769 1770 1771 1772 1773
	if (ret < 0)
		return ret;

	if (!entry->valid)
		goto loadpurge;

	/* Write port member tags */
1774
	ret = mv88e6xxx_vtu_data_write(chip, entry);
1775 1776 1777
	if (ret < 0)
		return ret;

1778
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1779
		reg = entry->sid & GLOBAL_VTU_SID_MASK;
1780 1781
		ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
					   reg);
1782 1783
		if (ret < 0)
			return ret;
1784
	}
1785

1786
	if (mv88e6xxx_has_fid_reg(chip)) {
1787
		reg = entry->fid & GLOBAL_VTU_FID_MASK;
1788 1789
		ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
					   reg);
1790 1791
		if (ret < 0)
			return ret;
1792
	} else if (mv88e6xxx_num_databases(chip) == 256) {
1793 1794 1795 1796 1797
		/* 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;
1798 1799 1800 1801 1802
	}

	reg = GLOBAL_VTU_VID_VALID;
loadpurge:
	reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1803
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1804 1805 1806
	if (ret < 0)
		return ret;

1807
	return _mv88e6xxx_vtu_cmd(chip, op);
1808 1809
}

1810
static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
1811 1812 1813 1814 1815
				  struct mv88e6xxx_vtu_stu_entry *entry)
{
	struct mv88e6xxx_vtu_stu_entry next = { 0 };
	int ret;

1816
	ret = _mv88e6xxx_vtu_wait(chip);
1817 1818 1819
	if (ret < 0)
		return ret;

1820
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1821 1822 1823 1824
				   sid & GLOBAL_VTU_SID_MASK);
	if (ret < 0)
		return ret;

1825
	ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1826 1827 1828
	if (ret < 0)
		return ret;

1829
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
1830 1831 1832 1833 1834
	if (ret < 0)
		return ret;

	next.sid = ret & GLOBAL_VTU_SID_MASK;

1835
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1836 1837 1838 1839 1840 1841
	if (ret < 0)
		return ret;

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

	if (next.valid) {
1842
		ret = mv88e6xxx_stu_data_read(chip, &next);
1843 1844 1845 1846 1847 1848 1849 1850
		if (ret < 0)
			return ret;
	}

	*entry = next;
	return 0;
}

1851
static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
1852 1853 1854 1855 1856
				    struct mv88e6xxx_vtu_stu_entry *entry)
{
	u16 reg = 0;
	int ret;

1857
	ret = _mv88e6xxx_vtu_wait(chip);
1858 1859 1860 1861 1862 1863 1864
	if (ret < 0)
		return ret;

	if (!entry->valid)
		goto loadpurge;

	/* Write port states */
1865
	ret = mv88e6xxx_stu_data_write(chip, entry);
1866 1867 1868 1869 1870
	if (ret < 0)
		return ret;

	reg = GLOBAL_VTU_VID_VALID;
loadpurge:
1871
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1872 1873 1874 1875
	if (ret < 0)
		return ret;

	reg = entry->sid & GLOBAL_VTU_SID_MASK;
1876
	ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1877 1878 1879
	if (ret < 0)
		return ret;

1880
	return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1881 1882
}

1883
static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
1884
			       u16 *new, u16 *old)
1885
{
1886
	struct dsa_switch *ds = chip->ds;
1887
	u16 upper_mask;
1888 1889 1890
	u16 fid;
	int ret;

1891
	if (mv88e6xxx_num_databases(chip) == 4096)
1892
		upper_mask = 0xff;
1893
	else if (mv88e6xxx_num_databases(chip) == 256)
1894
		upper_mask = 0xf;
1895 1896 1897
	else
		return -EOPNOTSUPP;

1898
	/* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1899
	ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
1900 1901 1902 1903 1904 1905 1906 1907 1908
	if (ret < 0)
		return ret;

	fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;

	if (new) {
		ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
		ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;

1909
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
1910 1911 1912 1913 1914 1915
					   ret);
		if (ret < 0)
			return ret;
	}

	/* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1916
	ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
1917 1918 1919
	if (ret < 0)
		return ret;

1920
	fid |= (ret & upper_mask) << 4;
1921 1922

	if (new) {
1923 1924
		ret &= ~upper_mask;
		ret |= (*new >> 4) & upper_mask;
1925

1926
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
1927 1928 1929 1930
					   ret);
		if (ret < 0)
			return ret;

1931 1932
		netdev_dbg(ds->ports[port].netdev,
			   "FID %d (was %d)\n", *new, fid);
1933 1934 1935 1936 1937 1938 1939 1940
	}

	if (old)
		*old = fid;

	return 0;
}

1941
static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
1942
				   int port, u16 *fid)
1943
{
1944
	return _mv88e6xxx_port_fid(chip, port, NULL, fid);
1945 1946
}

1947
static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
1948
				   int port, u16 fid)
1949
{
1950
	return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
1951 1952
}

1953
static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
1954 1955 1956
{
	DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
	struct mv88e6xxx_vtu_stu_entry vlan;
1957
	int i, err;
1958 1959 1960

	bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);

1961
	/* Set every FID bit used by the (un)bridged ports */
1962 1963
	for (i = 0; i < chip->info->num_ports; ++i) {
		err = _mv88e6xxx_port_fid_get(chip, i, fid);
1964 1965 1966 1967 1968 1969
		if (err)
			return err;

		set_bit(*fid, fid_bitmap);
	}

1970
	/* Set every FID bit used by the VLAN entries */
1971
	err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1972 1973 1974 1975
	if (err)
		return err;

	do {
1976
		err = _mv88e6xxx_vtu_getnext(chip, &vlan);
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
		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);
1990
	if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
1991 1992 1993
		return -ENOSPC;

	/* Clear the database */
1994
	return _mv88e6xxx_atu_flush(chip, *fid, true);
1995 1996
}

1997
static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
1998
			      struct mv88e6xxx_vtu_stu_entry *entry)
1999
{
2000
	struct dsa_switch *ds = chip->ds;
2001 2002 2003 2004
	struct mv88e6xxx_vtu_stu_entry vlan = {
		.valid = true,
		.vid = vid,
	};
2005 2006
	int i, err;

2007
	err = _mv88e6xxx_fid_new(chip, &vlan.fid);
2008 2009
	if (err)
		return err;
2010

2011
	/* exclude all ports except the CPU and DSA ports */
2012
	for (i = 0; i < chip->info->num_ports; ++i)
2013 2014 2015
		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;
2016

2017 2018
	if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
	    mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
2019 2020 2021 2022 2023 2024 2025
		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;
2026
		err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
2027 2028 2029 2030 2031 2032 2033 2034
		if (err)
			return err;

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

2035
			err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
2036 2037 2038 2039 2040 2041 2042 2043 2044
			if (err)
				return err;
		}
	}

	*entry = vlan;
	return 0;
}

2045
static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
2046 2047 2048 2049 2050 2051 2052
			      struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
{
	int err;

	if (!vid)
		return -EINVAL;

2053
	err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
2054 2055 2056
	if (err)
		return err;

2057
	err = _mv88e6xxx_vtu_getnext(chip, entry);
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
	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.
		 */

2068
		err = _mv88e6xxx_vtu_new(chip, vid, entry);
2069 2070 2071 2072 2073
	}

	return err;
}

2074 2075 2076
static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
					u16 vid_begin, u16 vid_end)
{
2077
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2078 2079 2080 2081 2082 2083
	struct mv88e6xxx_vtu_stu_entry vlan;
	int i, err;

	if (!vid_begin)
		return -EOPNOTSUPP;

2084
	mutex_lock(&chip->reg_lock);
2085

2086
	err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
2087 2088 2089 2090
	if (err)
		goto unlock;

	do {
2091
		err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2092 2093 2094 2095 2096 2097 2098 2099 2100
		if (err)
			goto unlock;

		if (!vlan.valid)
			break;

		if (vlan.vid > vid_end)
			break;

2101
		for (i = 0; i < chip->info->num_ports; ++i) {
2102 2103 2104 2105 2106 2107 2108
			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;

2109 2110
			if (chip->ports[i].bridge_dev ==
			    chip->ports[port].bridge_dev)
2111 2112
				break; /* same bridge, check next VLAN */

2113
			netdev_warn(ds->ports[port].netdev,
2114 2115
				    "hardware VLAN %d already used by %s\n",
				    vlan.vid,
2116
				    netdev_name(chip->ports[i].bridge_dev));
2117 2118 2119 2120 2121 2122
			err = -EOPNOTSUPP;
			goto unlock;
		}
	} while (vlan.vid < vid_end);

unlock:
2123
	mutex_unlock(&chip->reg_lock);
2124 2125 2126 2127

	return err;
}

2128 2129 2130 2131 2132 2133 2134
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",
};

2135 2136
static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
					 bool vlan_filtering)
2137
{
2138
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2139 2140 2141 2142
	u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
		PORT_CONTROL_2_8021Q_DISABLED;
	int ret;

2143
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2144 2145
		return -EOPNOTSUPP;

2146
	mutex_lock(&chip->reg_lock);
2147

2148
	ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
2149 2150 2151 2152 2153
	if (ret < 0)
		goto unlock;

	old = ret & PORT_CONTROL_2_8021Q_MASK;

2154 2155 2156
	if (new != old) {
		ret &= ~PORT_CONTROL_2_8021Q_MASK;
		ret |= new & PORT_CONTROL_2_8021Q_MASK;
2157

2158
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
2159 2160 2161 2162
					   ret);
		if (ret < 0)
			goto unlock;

2163
		netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
2164 2165 2166
			   mv88e6xxx_port_8021q_mode_names[new],
			   mv88e6xxx_port_8021q_mode_names[old]);
	}
2167

2168
	ret = 0;
2169
unlock:
2170
	mutex_unlock(&chip->reg_lock);
2171 2172 2173 2174

	return ret;
}

2175 2176 2177 2178
static int
mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
			    const struct switchdev_obj_port_vlan *vlan,
			    struct switchdev_trans *trans)
2179
{
2180
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2181 2182
	int err;

2183
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2184 2185
		return -EOPNOTSUPP;

2186 2187 2188 2189 2190 2191 2192 2193
	/* 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;

2194 2195 2196 2197 2198 2199
	/* We don't need any dynamic resource from the kernel (yet),
	 * so skip the prepare phase.
	 */
	return 0;
}

2200
static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
2201
				    u16 vid, bool untagged)
2202 2203 2204 2205
{
	struct mv88e6xxx_vtu_stu_entry vlan;
	int err;

2206
	err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
2207
	if (err)
2208
		return err;
2209 2210 2211 2212 2213

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

2214
	return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
2215 2216
}

2217 2218 2219
static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
				    const struct switchdev_obj_port_vlan *vlan,
				    struct switchdev_trans *trans)
2220
{
2221
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2222 2223 2224 2225
	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
	u16 vid;

2226
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2227 2228
		return;

2229
	mutex_lock(&chip->reg_lock);
2230

2231
	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
2232
		if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
2233 2234
			netdev_err(ds->ports[port].netdev,
				   "failed to add VLAN %d%c\n",
2235
				   vid, untagged ? 'u' : 't');
2236

2237
	if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
2238
		netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
2239
			   vlan->vid_end);
2240

2241
	mutex_unlock(&chip->reg_lock);
2242 2243
}

2244
static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
2245
				    int port, u16 vid)
2246
{
2247
	struct dsa_switch *ds = chip->ds;
2248 2249 2250
	struct mv88e6xxx_vtu_stu_entry vlan;
	int i, err;

2251
	err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
2252
	if (err)
2253
		return err;
2254

2255 2256
	/* Tell switchdev if this VLAN is handled in software */
	if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2257
		return -EOPNOTSUPP;
2258 2259 2260 2261

	vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;

	/* keep the VLAN unless all ports are excluded */
2262
	vlan.valid = false;
2263
	for (i = 0; i < chip->info->num_ports; ++i) {
2264
		if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
2265 2266 2267
			continue;

		if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
2268
			vlan.valid = true;
2269 2270 2271 2272
			break;
		}
	}

2273
	err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
2274 2275 2276
	if (err)
		return err;

2277
	return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
2278 2279
}

2280 2281
static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
				   const struct switchdev_obj_port_vlan *vlan)
2282
{
2283
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2284 2285 2286
	u16 pvid, vid;
	int err = 0;

2287
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2288 2289
		return -EOPNOTSUPP;

2290
	mutex_lock(&chip->reg_lock);
2291

2292
	err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
2293 2294 2295
	if (err)
		goto unlock;

2296
	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
2297
		err = _mv88e6xxx_port_vlan_del(chip, port, vid);
2298 2299 2300 2301
		if (err)
			goto unlock;

		if (vid == pvid) {
2302
			err = _mv88e6xxx_port_pvid_set(chip, port, 0);
2303 2304 2305 2306 2307
			if (err)
				goto unlock;
		}
	}

2308
unlock:
2309
	mutex_unlock(&chip->reg_lock);
2310 2311 2312 2313

	return err;
}

2314
static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
2315
				    const unsigned char *addr)
2316 2317 2318 2319
{
	int i, ret;

	for (i = 0; i < 3; i++) {
2320
		ret = _mv88e6xxx_reg_write(
2321
			chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
2322
			(addr[i * 2] << 8) | addr[i * 2 + 1]);
2323 2324 2325 2326 2327 2328 2329
		if (ret < 0)
			return ret;
	}

	return 0;
}

2330
static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
2331
				   unsigned char *addr)
2332 2333 2334 2335
{
	int i, ret;

	for (i = 0; i < 3; i++) {
2336
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
2337
					  GLOBAL_ATU_MAC_01 + i);
2338 2339 2340 2341 2342 2343 2344 2345 2346
		if (ret < 0)
			return ret;
		addr[i * 2] = ret >> 8;
		addr[i * 2 + 1] = ret & 0xff;
	}

	return 0;
}

2347
static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
2348
			       struct mv88e6xxx_atu_entry *entry)
2349
{
2350 2351
	int ret;

2352
	ret = _mv88e6xxx_atu_wait(chip);
2353 2354 2355
	if (ret < 0)
		return ret;

2356
	ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
2357 2358 2359
	if (ret < 0)
		return ret;

2360
	ret = _mv88e6xxx_atu_data_write(chip, entry);
2361
	if (ret < 0)
2362 2363
		return ret;

2364
	return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
2365
}
2366

2367
static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_chip *chip, int port,
2368 2369 2370 2371
				    const unsigned char *addr, u16 vid,
				    u8 state)
{
	struct mv88e6xxx_atu_entry entry = { 0 };
2372 2373 2374
	struct mv88e6xxx_vtu_stu_entry vlan;
	int err;

2375 2376
	/* Null VLAN ID corresponds to the port private database */
	if (vid == 0)
2377
		err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
2378
	else
2379
		err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
2380 2381
	if (err)
		return err;
2382

2383
	entry.fid = vlan.fid;
2384 2385 2386 2387 2388 2389 2390
	entry.state = state;
	ether_addr_copy(entry.mac, addr);
	if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
		entry.trunk = false;
		entry.portv_trunkid = BIT(port);
	}

2391
	return _mv88e6xxx_atu_load(chip, &entry);
2392 2393
}

2394 2395 2396
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 已提交
2397 2398 2399 2400 2401 2402 2403
{
	/* We don't need any dynamic resource from the kernel (yet),
	 * so skip the prepare phase.
	 */
	return 0;
}

2404 2405 2406
static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
				   const struct switchdev_obj_port_fdb *fdb,
				   struct switchdev_trans *trans)
2407
{
2408
	int state = is_multicast_ether_addr(fdb->addr) ?
2409 2410
		GLOBAL_ATU_DATA_STATE_MC_STATIC :
		GLOBAL_ATU_DATA_STATE_UC_STATIC;
2411
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2412

2413 2414
	mutex_lock(&chip->reg_lock);
	if (_mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid, state))
2415 2416
		netdev_err(ds->ports[port].netdev,
			   "failed to load MAC address\n");
2417
	mutex_unlock(&chip->reg_lock);
2418 2419
}

2420 2421
static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
				  const struct switchdev_obj_port_fdb *fdb)
2422
{
2423
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2424 2425
	int ret;

2426 2427
	mutex_lock(&chip->reg_lock);
	ret = _mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid,
2428
				       GLOBAL_ATU_DATA_STATE_UNUSED);
2429
	mutex_unlock(&chip->reg_lock);
2430 2431 2432 2433

	return ret;
}

2434
static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2435
				  struct mv88e6xxx_atu_entry *entry)
2436
{
2437 2438 2439 2440
	struct mv88e6xxx_atu_entry next = { 0 };
	int ret;

	next.fid = fid;
2441

2442
	ret = _mv88e6xxx_atu_wait(chip);
2443 2444
	if (ret < 0)
		return ret;
2445

2446
	ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2447 2448
	if (ret < 0)
		return ret;
2449

2450
	ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
2451 2452
	if (ret < 0)
		return ret;
2453

2454
	ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
2455 2456
	if (ret < 0)
		return ret;
2457

2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473
	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;
	}
2474

2475
	*entry = next;
2476 2477 2478
	return 0;
}

2479
static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_chip *chip,
2480
					u16 fid, u16 vid, int port,
2481 2482 2483 2484 2485 2486 2487 2488
					struct switchdev_obj_port_fdb *fdb,
					int (*cb)(struct switchdev_obj *obj))
{
	struct mv88e6xxx_atu_entry addr = {
		.mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
	};
	int err;

2489
	err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
2490 2491 2492 2493
	if (err)
		return err;

	do {
2494
		err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519
		if (err)
			break;

		if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
			break;

		if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
			bool is_static = addr.state ==
				(is_multicast_ether_addr(addr.mac) ?
				 GLOBAL_ATU_DATA_STATE_MC_STATIC :
				 GLOBAL_ATU_DATA_STATE_UC_STATIC);

			fdb->vid = vid;
			ether_addr_copy(fdb->addr, addr.mac);
			fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;

			err = cb(&fdb->obj);
			if (err)
				break;
		}
	} while (!is_broadcast_ether_addr(addr.mac));

	return err;
}

2520 2521 2522
static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
				   struct switchdev_obj_port_fdb *fdb,
				   int (*cb)(struct switchdev_obj *obj))
2523
{
2524
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2525 2526 2527
	struct mv88e6xxx_vtu_stu_entry vlan = {
		.vid = GLOBAL_VTU_VID_MASK, /* all ones */
	};
2528
	u16 fid;
2529 2530
	int err;

2531
	mutex_lock(&chip->reg_lock);
2532

2533
	/* Dump port's default Filtering Information Database (VLAN ID 0) */
2534
	err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2535 2536 2537
	if (err)
		goto unlock;

2538
	err = _mv88e6xxx_port_fdb_dump_one(chip, fid, 0, port, fdb, cb);
2539 2540 2541
	if (err)
		goto unlock;

2542
	/* Dump VLANs' Filtering Information Databases */
2543
	err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2544 2545 2546 2547
	if (err)
		goto unlock;

	do {
2548
		err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2549
		if (err)
2550
			break;
2551 2552 2553 2554

		if (!vlan.valid)
			break;

2555 2556
		err = _mv88e6xxx_port_fdb_dump_one(chip, vlan.fid, vlan.vid,
						   port, fdb, cb);
2557
		if (err)
2558
			break;
2559 2560 2561
	} while (vlan.vid < GLOBAL_VTU_VID_MASK);

unlock:
2562
	mutex_unlock(&chip->reg_lock);
2563 2564 2565 2566

	return err;
}

2567 2568
static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
				      struct net_device *bridge)
2569
{
2570
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2571
	int i, err = 0;
2572

2573
	mutex_lock(&chip->reg_lock);
2574

2575
	/* Assign the bridge and remap each port's VLANTable */
2576
	chip->ports[port].bridge_dev = bridge;
2577

2578 2579 2580
	for (i = 0; i < chip->info->num_ports; ++i) {
		if (chip->ports[i].bridge_dev == bridge) {
			err = _mv88e6xxx_port_based_vlan_map(chip, i);
2581 2582 2583 2584 2585
			if (err)
				break;
		}
	}

2586
	mutex_unlock(&chip->reg_lock);
2587

2588
	return err;
2589 2590
}

2591
static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
2592
{
2593 2594
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
	struct net_device *bridge = chip->ports[port].bridge_dev;
2595
	int i;
2596

2597
	mutex_lock(&chip->reg_lock);
2598

2599
	/* Unassign the bridge and remap each port's VLANTable */
2600
	chip->ports[port].bridge_dev = NULL;
2601

2602 2603 2604
	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))
2605 2606
				netdev_warn(ds->ports[i].netdev,
					    "failed to remap\n");
2607

2608
	mutex_unlock(&chip->reg_lock);
2609 2610
}

2611
static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_chip *chip,
2612
				      int port, int page, int reg, int val)
2613 2614 2615
{
	int ret;

2616
	ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
2617 2618 2619
	if (ret < 0)
		goto restore_page_0;

2620
	ret = mv88e6xxx_mdio_write_indirect(chip, port, reg, val);
2621
restore_page_0:
2622
	mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
2623 2624 2625 2626

	return ret;
}

2627
static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_chip *chip,
2628
				     int port, int page, int reg)
2629 2630 2631
{
	int ret;

2632
	ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
2633 2634 2635
	if (ret < 0)
		goto restore_page_0;

2636
	ret = mv88e6xxx_mdio_read_indirect(chip, port, reg);
2637
restore_page_0:
2638
	mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
2639 2640 2641 2642

	return ret;
}

2643
static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
2644
{
2645
	bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
2646
	u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2647
	struct gpio_desc *gpiod = chip->reset;
2648 2649 2650 2651 2652
	unsigned long timeout;
	int ret;
	int i;

	/* Set all ports to the disabled state. */
2653 2654
	for (i = 0; i < chip->info->num_ports; i++) {
		ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
2655 2656 2657
		if (ret < 0)
			return ret;

2658
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679
					   ret & 0xfffc);
		if (ret)
			return ret;
	}

	/* 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)
2680
		ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
2681
	else
2682
		ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
2683 2684 2685 2686 2687 2688
	if (ret)
		return ret;

	/* Wait up to one second for reset to complete. */
	timeout = jiffies + 1 * HZ;
	while (time_before(jiffies, timeout)) {
2689
		ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704
		if (ret < 0)
			return ret;

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

	return ret;
}

2705
static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_chip *chip)
2706 2707 2708
{
	int ret;

2709
	ret = _mv88e6xxx_mdio_page_read(chip, REG_FIBER_SERDES,
2710
					PAGE_FIBER_SERDES, MII_BMCR);
2711 2712 2713 2714 2715
	if (ret < 0)
		return ret;

	if (ret & BMCR_PDOWN) {
		ret &= ~BMCR_PDOWN;
2716
		ret = _mv88e6xxx_mdio_page_write(chip, REG_FIBER_SERDES,
2717 2718
						 PAGE_FIBER_SERDES, MII_BMCR,
						 ret);
2719 2720 2721 2722 2723
	}

	return ret;
}

2724
static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
2725
{
2726
	struct dsa_switch *ds = chip->ds;
2727
	int ret;
2728
	u16 reg;
2729

2730 2731 2732 2733
	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)) {
2734 2735 2736 2737 2738 2739
		/* 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.
		 */
2740
		reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
2741
		if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
2742
			reg &= ~PORT_PCS_CTRL_UNFORCED;
2743 2744 2745 2746
			reg |= PORT_PCS_CTRL_FORCE_LINK |
				PORT_PCS_CTRL_LINK_UP |
				PORT_PCS_CTRL_DUPLEX_FULL |
				PORT_PCS_CTRL_FORCE_DUPLEX;
2747
			if (mv88e6xxx_6065_family(chip))
2748 2749 2750 2751 2752 2753 2754
				reg |= PORT_PCS_CTRL_100;
			else
				reg |= PORT_PCS_CTRL_1000;
		} else {
			reg |= PORT_PCS_CTRL_UNFORCED;
		}

2755
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2756 2757
					   PORT_PCS_CTRL, reg);
		if (ret)
2758
			return ret;
2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
	}

	/* 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;
2776 2777 2778 2779
	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))
2780 2781 2782 2783
		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)) {
2784
		if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip))
2785
			reg |= PORT_CONTROL_DSA_TAG;
2786 2787 2788 2789 2790
		if (mv88e6xxx_6352_family(chip) ||
		    mv88e6xxx_6351_family(chip) ||
		    mv88e6xxx_6165_family(chip) ||
		    mv88e6xxx_6097_family(chip) ||
		    mv88e6xxx_6320_family(chip)) {
2791 2792
			reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
				PORT_CONTROL_FORWARD_UNKNOWN |
2793
				PORT_CONTROL_FORWARD_UNKNOWN_MC;
2794 2795
		}

2796 2797 2798 2799 2800 2801 2802 2803
		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)) {
2804
			reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2805 2806
		}
	}
2807
	if (dsa_is_dsa_port(ds, port)) {
2808 2809
		if (mv88e6xxx_6095_family(chip) ||
		    mv88e6xxx_6185_family(chip))
2810
			reg |= PORT_CONTROL_DSA_TAG;
2811 2812 2813 2814 2815
		if (mv88e6xxx_6352_family(chip) ||
		    mv88e6xxx_6351_family(chip) ||
		    mv88e6xxx_6165_family(chip) ||
		    mv88e6xxx_6097_family(chip) ||
		    mv88e6xxx_6320_family(chip)) {
2816
			reg |= PORT_CONTROL_FRAME_MODE_DSA;
2817 2818
		}

2819 2820 2821 2822 2823
		if (port == dsa_upstream_port(ds))
			reg |= PORT_CONTROL_FORWARD_UNKNOWN |
				PORT_CONTROL_FORWARD_UNKNOWN_MC;
	}
	if (reg) {
2824
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2825 2826
					   PORT_CONTROL, reg);
		if (ret)
2827
			return ret;
2828 2829
	}

2830 2831 2832
	/* If this port is connected to a SerDes, make sure the SerDes is not
	 * powered down.
	 */
2833 2834
	if (mv88e6xxx_6352_family(chip)) {
		ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
2835
		if (ret < 0)
2836
			return ret;
2837 2838 2839 2840
		ret &= PORT_STATUS_CMODE_MASK;
		if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
		    (ret == PORT_STATUS_CMODE_1000BASE_X) ||
		    (ret == PORT_STATUS_CMODE_SGMII)) {
2841
			ret = mv88e6xxx_power_on_serdes(chip);
2842
			if (ret < 0)
2843
				return ret;
2844 2845 2846
		}
	}

2847
	/* Port Control 2: don't force a good FCS, set the maximum frame size to
2848
	 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
2849 2850 2851
	 * 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.
2852 2853
	 */
	reg = 0;
2854 2855 2856 2857
	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))
2858 2859
		reg = PORT_CONTROL_2_MAP_DA;

2860 2861
	if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
	    mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
2862 2863
		reg |= PORT_CONTROL_2_JUMBO_10240;

2864
	if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
2865 2866 2867 2868 2869 2870 2871 2872 2873
		/* 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;
	}

2874
	reg |= PORT_CONTROL_2_8021Q_DISABLED;
2875

2876
	if (reg) {
2877
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2878 2879
					   PORT_CONTROL_2, reg);
		if (ret)
2880
			return ret;
2881 2882 2883 2884 2885 2886 2887
	}

	/* 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.
	 */
2888
	reg = 1 << port;
2889 2890
	/* Disable learning for CPU port */
	if (dsa_is_cpu_port(ds, port))
2891
		reg = 0;
2892

2893 2894
	ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
				   reg);
2895
	if (ret)
2896
		return ret;
2897 2898

	/* Egress rate control 2: disable egress rate control. */
2899
	ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
2900 2901
				   0x0000);
	if (ret)
2902
		return ret;
2903

2904 2905 2906
	if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
	    mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
	    mv88e6xxx_6320_family(chip)) {
2907 2908 2909 2910
		/* 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.
		 */
2911
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2912 2913
					   PORT_PAUSE_CTRL, 0x0000);
		if (ret)
2914
			return ret;
2915 2916 2917 2918 2919

		/* Port ATU control: disable limiting the number of
		 * address database entries that this port is allowed
		 * to use.
		 */
2920
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2921 2922 2923 2924
					   PORT_ATU_CONTROL, 0x0000);
		/* Priority Override: disable DA, SA and VTU priority
		 * override.
		 */
2925
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2926 2927
					   PORT_PRI_OVERRIDE, 0x0000);
		if (ret)
2928
			return ret;
2929 2930 2931 2932

		/* Port Ethertype: use the Ethertype DSA Ethertype
		 * value.
		 */
2933
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2934 2935
					   PORT_ETH_TYPE, ETH_P_EDSA);
		if (ret)
2936
			return ret;
2937 2938 2939
		/* Tag Remap: use an identity 802.1p prio -> switch
		 * prio mapping.
		 */
2940
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2941 2942
					   PORT_TAG_REGMAP_0123, 0x3210);
		if (ret)
2943
			return ret;
2944 2945 2946 2947

		/* Tag Remap 2: use an identity 802.1p prio -> switch
		 * prio mapping.
		 */
2948
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2949 2950
					   PORT_TAG_REGMAP_4567, 0x7654);
		if (ret)
2951
			return ret;
2952 2953
	}

2954 2955 2956 2957
	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_6320_family(chip)) {
2958
		/* Rate Control: disable ingress rate limiting. */
2959
		ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2960 2961
					   PORT_RATE_CONTROL, 0x0001);
		if (ret)
2962
			return ret;
2963 2964
	}

2965 2966
	/* Port Control 1: disable trunking, disable sending
	 * learning messages to this port.
2967
	 */
2968 2969
	ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
				   0x0000);
2970
	if (ret)
2971
		return ret;
2972

2973
	/* Port based VLAN map: give each port the same default address
2974 2975
	 * database, and allow bidirectional communication between the
	 * CPU and DSA port(s), and the other ports.
2976
	 */
2977
	ret = _mv88e6xxx_port_fid_set(chip, port, 0);
2978
	if (ret)
2979
		return ret;
2980

2981
	ret = _mv88e6xxx_port_based_vlan_map(chip, port);
2982
	if (ret)
2983
		return ret;
2984 2985 2986 2987

	/* Default VLAN ID and priority: don't set a default VLAN
	 * ID, and set the default packet priority to zero.
	 */
2988
	ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
2989
				   0x0000);
2990 2991
	if (ret)
		return ret;
2992 2993 2994 2995

	return 0;
}

2996
static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
2997
{
2998
	struct dsa_switch *ds = chip->ds;
2999
	u32 upstream_port = dsa_upstream_port(ds);
3000
	u16 reg;
3001
	int err;
3002

3003 3004 3005 3006
	/* Enable the PHY Polling Unit if present, don't discard any packets,
	 * and mask all interrupt sources.
	 */
	reg = 0;
3007 3008
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
	    mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
3009 3010
		reg |= GLOBAL_CONTROL_PPU_ENABLE;

3011
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
3012 3013 3014
	if (err)
		return err;

3015 3016 3017 3018 3019 3020
	/* 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;
3021 3022
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
				   reg);
3023 3024 3025
	if (err)
		return err;

3026
	/* Disable remote management, and set the switch's DSA device number. */
3027
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
3028 3029 3030 3031 3032
				   GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
				   (ds->index & 0x1f));
	if (err)
		return err;

3033 3034 3035 3036
	/* Set the default address aging time to 5 minutes, and
	 * enable address learn messages to be sent to all message
	 * ports.
	 */
3037
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
3038 3039
				   0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
	if (err)
3040
		return err;
3041

3042 3043 3044 3045 3046 3047 3048 3049 3050 3051
	/* Clear all the VTU and STU entries */
	err = _mv88e6xxx_vtu_stu_flush(chip);
	if (err < 0)
		return err;

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

3052
	/* Configure the IP ToS mapping registers. */
3053
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
3054
	if (err)
3055
		return err;
3056
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
3057
	if (err)
3058
		return err;
3059
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
3060
	if (err)
3061
		return err;
3062
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
3063
	if (err)
3064
		return err;
3065
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
3066
	if (err)
3067
		return err;
3068
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
3069
	if (err)
3070
		return err;
3071
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
3072
	if (err)
3073
		return err;
3074
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
3075
	if (err)
3076
		return err;
3077 3078

	/* Configure the IEEE 802.1p priority mapping register. */
3079
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
3080
	if (err)
3081
		return err;
3082

3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102
	/* 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;
}

static int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
{
	struct dsa_switch *ds = chip->ds;
	int err;
	int i;

3103 3104 3105
	/* Send all frames with destination addresses matching
	 * 01:80:c2:00:00:0x to the CPU port.
	 */
3106 3107
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
				   0xffff);
3108
	if (err)
3109
		return err;
3110 3111 3112 3113 3114 3115

	/* Ignore removed tag data on doubly tagged packets, disable
	 * flow control messages, force flow control priority to the
	 * highest, and send all special multicast frames to the CPU
	 * port at the highest priority.
	 */
3116
	err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
3117 3118 3119
				   0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
				   GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
	if (err)
3120
		return err;
3121 3122 3123 3124 3125

	/* Program the DSA routing table. */
	for (i = 0; i < 32; i++) {
		int nexthop = 0x1f;

3126 3127
		if (i != ds->index && i < DSA_MAX_SWITCHES)
			nexthop = ds->rtable[i] & 0x1f;
3128

3129
		err = _mv88e6xxx_reg_write(
3130
			chip, REG_GLOBAL2,
3131 3132 3133 3134
			GLOBAL2_DEVICE_MAPPING,
			GLOBAL2_DEVICE_MAPPING_UPDATE |
			(i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop);
		if (err)
3135
			return err;
3136 3137 3138
	}

	/* Clear all trunk masks. */
3139
	for (i = 0; i < 8; i++) {
3140 3141
		err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
					   GLOBAL2_TRUNK_MASK,
3142 3143
					   0x8000 |
					   (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
3144
					   ((1 << chip->info->num_ports) - 1));
3145
		if (err)
3146
			return err;
3147
	}
3148 3149

	/* Clear all trunk mappings. */
3150 3151
	for (i = 0; i < 16; i++) {
		err = _mv88e6xxx_reg_write(
3152
			chip, REG_GLOBAL2,
3153 3154 3155 3156
			GLOBAL2_TRUNK_MAPPING,
			GLOBAL2_TRUNK_MAPPING_UPDATE |
			(i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
		if (err)
3157
			return err;
3158
	}
3159

3160 3161 3162
	if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
	    mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
	    mv88e6xxx_6320_family(chip)) {
3163 3164 3165
		/* Send all frames with destination addresses matching
		 * 01:80:c2:00:00:2x to the CPU port.
		 */
3166
		err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
3167 3168
					   GLOBAL2_MGMT_EN_2X, 0xffff);
		if (err)
3169
			return err;
3170 3171 3172 3173

		/* Initialise cross-chip port VLAN table to reset
		 * defaults.
		 */
3174
		err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
3175 3176
					   GLOBAL2_PVT_ADDR, 0x9000);
		if (err)
3177
			return err;
3178 3179

		/* Clear the priority override table. */
3180
		for (i = 0; i < 16; i++) {
3181
			err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
3182 3183 3184
						   GLOBAL2_PRIO_OVERRIDE,
						   0x8000 | (i << 8));
			if (err)
3185
				return err;
3186
		}
3187 3188
	}

3189 3190 3191 3192
	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_6320_family(chip)) {
3193 3194 3195 3196
		/* Disable ingress rate limiting by resetting all
		 * ingress rate limit registers to their initial
		 * state.
		 */
3197 3198
		for (i = 0; i < chip->info->num_ports; i++) {
			err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
3199 3200 3201
						   GLOBAL2_INGRESS_OP,
						   0x9000 | (i << 8));
			if (err)
3202
				return err;
3203
		}
3204 3205
	}

3206
	return 0;
3207 3208
}

3209
static int mv88e6xxx_setup(struct dsa_switch *ds)
3210
{
3211
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3212
	int err;
3213 3214
	int i;

3215 3216
	chip->ds = ds;
	ds->slave_mii_bus = chip->mdio_bus;
3217

3218 3219
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
		mutex_init(&chip->eeprom_mutex);
3220

3221
	mutex_lock(&chip->reg_lock);
3222

3223
	err = mv88e6xxx_switch_reset(chip);
3224 3225 3226
	if (err)
		goto unlock;

3227 3228 3229 3230 3231 3232 3233 3234 3235
	/* 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);
3236 3237 3238
	if (err)
		goto unlock;

3239 3240 3241
	/* Setup Switch Global 2 Registers */
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
		err = mv88e6xxx_g2_setup(chip);
3242 3243 3244
		if (err)
			goto unlock;
	}
3245

3246
unlock:
3247
	mutex_unlock(&chip->reg_lock);
3248

3249
	return err;
3250 3251
}

3252 3253
static int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page,
				    int reg)
3254
{
3255
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3256 3257
	int ret;

3258 3259 3260
	mutex_lock(&chip->reg_lock);
	ret = _mv88e6xxx_mdio_page_read(chip, port, page, reg);
	mutex_unlock(&chip->reg_lock);
3261

3262 3263 3264
	return ret;
}

3265 3266
static int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page,
				     int reg, int val)
3267
{
3268
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3269 3270
	int ret;

3271 3272 3273
	mutex_lock(&chip->reg_lock);
	ret = _mv88e6xxx_mdio_page_write(chip, port, page, reg, val);
	mutex_unlock(&chip->reg_lock);
3274

3275 3276 3277
	return ret;
}

3278
static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_chip *chip, int port)
3279
{
3280
	if (port >= 0 && port < chip->info->num_ports)
3281 3282 3283 3284
		return port;
	return -EINVAL;
}

3285
static int mv88e6xxx_mdio_read(struct mii_bus *bus, int port, int regnum)
3286
{
3287 3288
	struct mv88e6xxx_chip *chip = bus->priv;
	int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
3289 3290 3291
	int ret;

	if (addr < 0)
3292
		return 0xffff;
3293

3294
	mutex_lock(&chip->reg_lock);
3295

3296 3297 3298 3299
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
		ret = mv88e6xxx_mdio_read_ppu(chip, addr, regnum);
	else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
		ret = mv88e6xxx_mdio_read_indirect(chip, addr, regnum);
3300
	else
3301
		ret = mv88e6xxx_mdio_read_direct(chip, addr, regnum);
3302

3303
	mutex_unlock(&chip->reg_lock);
3304 3305 3306
	return ret;
}

3307
static int mv88e6xxx_mdio_write(struct mii_bus *bus, int port, int regnum,
3308
				u16 val)
3309
{
3310 3311
	struct mv88e6xxx_chip *chip = bus->priv;
	int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
3312 3313 3314
	int ret;

	if (addr < 0)
3315
		return 0xffff;
3316

3317
	mutex_lock(&chip->reg_lock);
3318

3319 3320 3321 3322
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
		ret = mv88e6xxx_mdio_write_ppu(chip, addr, regnum, val);
	else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
		ret = mv88e6xxx_mdio_write_indirect(chip, addr, regnum, val);
3323
	else
3324
		ret = mv88e6xxx_mdio_write_direct(chip, addr, regnum, val);
3325

3326
	mutex_unlock(&chip->reg_lock);
3327 3328 3329
	return ret;
}

3330
static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
3331 3332 3333 3334 3335 3336
				   struct device_node *np)
{
	static int index;
	struct mii_bus *bus;
	int err;

3337 3338
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
		mv88e6xxx_ppu_state_init(chip);
3339 3340

	if (np)
3341
		chip->mdio_np = of_get_child_by_name(np, "mdio");
3342

3343
	bus = devm_mdiobus_alloc(chip->dev);
3344 3345 3346
	if (!bus)
		return -ENOMEM;

3347
	bus->priv = (void *)chip;
3348 3349 3350 3351 3352 3353 3354 3355 3356 3357
	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;
3358
	bus->parent = chip->dev;
3359

3360 3361
	if (chip->mdio_np)
		err = of_mdiobus_register(bus, chip->mdio_np);
3362 3363 3364
	else
		err = mdiobus_register(bus);
	if (err) {
3365
		dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
3366 3367
		goto out;
	}
3368
	chip->mdio_bus = bus;
3369 3370 3371 3372

	return 0;

out:
3373 3374
	if (chip->mdio_np)
		of_node_put(chip->mdio_np);
3375 3376 3377 3378

	return err;
}

3379
static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
3380 3381

{
3382
	struct mii_bus *bus = chip->mdio_bus;
3383 3384 3385

	mdiobus_unregister(bus);

3386 3387
	if (chip->mdio_np)
		of_node_put(chip->mdio_np);
3388 3389
}

3390 3391 3392 3393
#ifdef CONFIG_NET_DSA_HWMON

static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
{
3394
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3395 3396 3397 3398 3399
	int ret;
	int val;

	*temp = 0;

3400
	mutex_lock(&chip->reg_lock);
3401

3402
	ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x6);
3403 3404 3405 3406
	if (ret < 0)
		goto error;

	/* Enable temperature sensor */
3407
	ret = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
3408 3409 3410
	if (ret < 0)
		goto error;

3411
	ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret | (1 << 5));
3412 3413 3414 3415 3416 3417
	if (ret < 0)
		goto error;

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

3418
	val = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
3419 3420 3421 3422 3423 3424
	if (val < 0) {
		ret = val;
		goto error;
	}

	/* Disable temperature sensor */
3425
	ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret & ~(1 << 5));
3426 3427 3428 3429 3430 3431
	if (ret < 0)
		goto error;

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

error:
3432 3433
	mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x0);
	mutex_unlock(&chip->reg_lock);
3434 3435 3436 3437 3438
	return ret;
}

static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
{
3439 3440
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
	int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3441 3442 3443 3444
	int ret;

	*temp = 0;

3445
	ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27);
3446 3447 3448 3449 3450 3451 3452 3453
	if (ret < 0)
		return ret;

	*temp = (ret & 0xff) - 25;

	return 0;
}

3454
static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3455
{
3456
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3457

3458
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
3459 3460
		return -EOPNOTSUPP;

3461
	if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
3462 3463 3464 3465 3466
		return mv88e63xx_get_temp(ds, temp);

	return mv88e61xx_get_temp(ds, temp);
}

3467
static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3468
{
3469 3470
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
	int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3471 3472
	int ret;

3473
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3474 3475 3476 3477
		return -EOPNOTSUPP;

	*temp = 0;

3478
	ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
3479 3480 3481 3482 3483 3484 3485 3486
	if (ret < 0)
		return ret;

	*temp = (((ret >> 8) & 0x1f) * 5) - 25;

	return 0;
}

3487
static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3488
{
3489 3490
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
	int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3491 3492
	int ret;

3493
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3494 3495
		return -EOPNOTSUPP;

3496
	ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
3497 3498 3499
	if (ret < 0)
		return ret;
	temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3500 3501
	return mv88e6xxx_mdio_page_write(ds, phy, 6, 26,
					 (ret & 0xe0ff) | (temp << 8));
3502 3503
}

3504
static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3505
{
3506 3507
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
	int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3508 3509
	int ret;

3510
	if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3511 3512 3513 3514
		return -EOPNOTSUPP;

	*alarm = false;

3515
	ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
3516 3517 3518 3519 3520 3521 3522 3523 3524
	if (ret < 0)
		return ret;

	*alarm = !!(ret & 0x40);

	return 0;
}
#endif /* CONFIG_NET_DSA_HWMON */

3525 3526 3527 3528 3529 3530 3531
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,
3532
		.port_base_addr = 0x10,
3533 3534 3535 3536 3537 3538 3539 3540 3541
		.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,
3542
		.port_base_addr = 0x10,
3543 3544 3545 3546 3547 3548 3549 3550 3551
		.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,
3552
		.port_base_addr = 0x10,
3553 3554 3555 3556 3557 3558 3559 3560 3561
		.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,
3562
		.port_base_addr = 0x10,
3563 3564 3565 3566 3567 3568 3569 3570 3571
		.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,
3572
		.port_base_addr = 0x10,
3573 3574 3575 3576 3577 3578 3579 3580 3581
		.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,
3582
		.port_base_addr = 0x10,
3583 3584 3585 3586 3587 3588 3589 3590 3591
		.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,
3592
		.port_base_addr = 0x10,
3593 3594 3595 3596 3597 3598 3599 3600 3601
		.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,
3602
		.port_base_addr = 0x10,
3603 3604 3605 3606 3607 3608 3609 3610 3611
		.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,
3612
		.port_base_addr = 0x10,
3613 3614 3615 3616 3617 3618 3619 3620 3621
		.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,
3622
		.port_base_addr = 0x10,
3623 3624 3625 3626 3627 3628 3629 3630 3631
		.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,
3632
		.port_base_addr = 0x10,
3633 3634 3635 3636 3637 3638 3639 3640 3641
		.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,
3642
		.port_base_addr = 0x10,
3643 3644 3645 3646 3647 3648 3649 3650 3651
		.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,
3652
		.port_base_addr = 0x10,
3653 3654 3655 3656 3657 3658 3659 3660 3661
		.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,
3662
		.port_base_addr = 0x10,
3663 3664 3665 3666 3667 3668 3669 3670 3671
		.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,
3672
		.port_base_addr = 0x10,
3673 3674 3675 3676 3677 3678 3679 3680 3681
		.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,
3682
		.port_base_addr = 0x10,
3683 3684 3685 3686 3687 3688 3689 3690 3691
		.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,
3692
		.port_base_addr = 0x10,
3693 3694 3695 3696
		.flags = MV88E6XXX_FLAGS_FAMILY_6352,
	},
};

3697
static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
3698
{
3699
	int i;
3700

3701 3702 3703
	for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
		if (mv88e6xxx_table[i].prod_num == prod_num)
			return &mv88e6xxx_table[i];
3704 3705 3706 3707

	return NULL;
}

3708
static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
3709 3710 3711 3712
{
	const struct mv88e6xxx_info *info;
	int id, prod_num, rev;

3713 3714
	id = mv88e6xxx_reg_read(chip, chip->info->port_base_addr,
				PORT_SWITCH_ID);
3715 3716 3717 3718 3719 3720 3721 3722 3723 3724
	if (id < 0)
		return id;

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

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

3725
	/* Update the compatible info with the probed one */
3726
	chip->info = info;
3727

3728 3729
	dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
		 chip->info->prod_num, chip->info->name, rev);
3730 3731 3732 3733

	return 0;
}

3734
static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
3735
{
3736
	struct mv88e6xxx_chip *chip;
3737

3738 3739
	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
	if (!chip)
3740 3741
		return NULL;

3742
	chip->dev = dev;
3743

3744
	mutex_init(&chip->reg_lock);
3745

3746
	return chip;
3747 3748
}

3749
static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
3750 3751 3752 3753 3754 3755
			      struct mii_bus *bus, int sw_addr)
{
	/* ADDR[0] pin is unavailable externally and considered zero */
	if (sw_addr & 0x1)
		return -EINVAL;

3756
	if (sw_addr == 0)
3757 3758 3759
		chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
	else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_MULTI_CHIP))
		chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
3760 3761 3762
	else
		return -EINVAL;

3763 3764
	chip->bus = bus;
	chip->sw_addr = sw_addr;
3765 3766 3767 3768

	return 0;
}

3769 3770 3771
static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
				       struct device *host_dev, int sw_addr,
				       void **priv)
3772
{
3773
	struct mv88e6xxx_chip *chip;
3774
	struct mii_bus *bus;
3775
	int err;
3776

3777
	bus = dsa_host_dev_to_mii_bus(host_dev);
3778 3779 3780
	if (!bus)
		return NULL;

3781 3782
	chip = mv88e6xxx_alloc_chip(dsa_dev);
	if (!chip)
3783 3784
		return NULL;

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

3788
	err = mv88e6xxx_smi_init(chip, bus, sw_addr);
3789 3790 3791
	if (err)
		goto free;

3792
	err = mv88e6xxx_detect(chip);
3793
	if (err)
3794
		goto free;
3795

3796
	err = mv88e6xxx_mdio_register(chip, NULL);
3797
	if (err)
3798
		goto free;
3799

3800
	*priv = chip;
3801

3802
	return chip->info->name;
3803
free:
3804
	devm_kfree(dsa_dev, chip);
3805 3806

	return NULL;
3807 3808
}

3809
static struct dsa_switch_driver mv88e6xxx_switch_driver = {
3810
	.tag_protocol		= DSA_TAG_PROTO_EDSA,
3811
	.probe			= mv88e6xxx_drv_probe,
3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825
	.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
3826
	.get_eeprom_len		= mv88e6xxx_get_eeprom_len,
3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844
	.get_eeprom		= mv88e6xxx_get_eeprom,
	.set_eeprom		= mv88e6xxx_set_eeprom,
	.get_regs_len		= mv88e6xxx_get_regs_len,
	.get_regs		= mv88e6xxx_get_regs,
	.port_bridge_join	= mv88e6xxx_port_bridge_join,
	.port_bridge_leave	= mv88e6xxx_port_bridge_leave,
	.port_stp_state_set	= mv88e6xxx_port_stp_state_set,
	.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,
};

3845
static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
3846 3847
				     struct device_node *np)
{
3848
	struct device *dev = chip->dev;
3849 3850 3851 3852 3853 3854 3855
	struct dsa_switch *ds;

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

	ds->dev = dev;
3856
	ds->priv = chip;
3857 3858 3859 3860 3861 3862 3863
	ds->drv = &mv88e6xxx_switch_driver;

	dev_set_drvdata(dev, ds);

	return dsa_register_switch(ds, np);
}

3864
static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
3865
{
3866
	dsa_unregister_switch(chip->ds);
3867 3868
}

3869
static int mv88e6xxx_probe(struct mdio_device *mdiodev)
3870
{
3871
	struct device *dev = &mdiodev->dev;
3872
	struct device_node *np = dev->of_node;
3873
	const struct mv88e6xxx_info *compat_info;
3874
	struct mv88e6xxx_chip *chip;
3875
	u32 eeprom_len;
3876
	int err;
3877

3878 3879 3880 3881
	compat_info = of_device_get_match_data(dev);
	if (!compat_info)
		return -EINVAL;

3882 3883
	chip = mv88e6xxx_alloc_chip(dev);
	if (!chip)
3884 3885
		return -ENOMEM;

3886
	chip->info = compat_info;
3887

3888
	err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
3889 3890
	if (err)
		return err;
3891

3892
	err = mv88e6xxx_detect(chip);
3893 3894
	if (err)
		return err;
3895

3896 3897 3898
	chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
	if (IS_ERR(chip->reset))
		return PTR_ERR(chip->reset);
3899

3900
	if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM) &&
3901
	    !of_property_read_u32(np, "eeprom-length", &eeprom_len))
3902
		chip->eeprom_len = eeprom_len;
3903

3904
	err = mv88e6xxx_mdio_register(chip, np);
3905 3906 3907
	if (err)
		return err;

3908
	err = mv88e6xxx_register_switch(chip, np);
3909
	if (err) {
3910
		mv88e6xxx_mdio_unregister(chip);
3911 3912 3913
		return err;
	}

3914 3915
	return 0;
}
3916 3917 3918 3919

static void mv88e6xxx_remove(struct mdio_device *mdiodev)
{
	struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
3920
	struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3921

3922 3923
	mv88e6xxx_unregister_switch(chip);
	mv88e6xxx_mdio_unregister(chip);
3924 3925 3926
}

static const struct of_device_id mv88e6xxx_of_match[] = {
3927 3928 3929 3930
	{
		.compatible = "marvell,mv88e6085",
		.data = &mv88e6xxx_table[MV88E6085],
	},
3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949
	{ /* 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)
{
	register_switch_driver(&mv88e6xxx_switch_driver);
	return mdio_driver_register(&mv88e6xxx_driver);
}
3950 3951 3952 3953
module_init(mv88e6xxx_init);

static void __exit mv88e6xxx_cleanup(void)
{
3954
	mdio_driver_unregister(&mv88e6xxx_driver);
3955
	unregister_switch_driver(&mv88e6xxx_switch_driver);
3956 3957
}
module_exit(mv88e6xxx_cleanup);
3958 3959 3960 3961

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