blob: 5cb06f7673af2fc011dc13ded81ef91a561caffb [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
Vivien Didelot0d3cd4b2016-06-21 12:28:19 -04002 * Marvell 88e6xxx Ethernet switch single-chip support
3 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 * Copyright (c) 2008 Marvell Semiconductor
5 *
Vivien Didelotb8fee952015-08-13 12:52:19 -04006 * Copyright (c) 2015 CMC Electronics, Inc.
7 * Added support for VLAN Table Unit operations
8 *
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02009 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
10 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
Barry Grussling19b2f972013-01-08 16:05:54 +000017#include <linux/delay.h>
Guenter Roeckdefb05b2015-03-26 18:36:38 -070018#include <linux/etherdevice.h>
Andrew Lunndea87022015-08-31 15:56:47 +020019#include <linux/ethtool.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070020#include <linux/if_bridge.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000021#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000022#include <linux/list.h>
Andrew Lunn14c7b3c2016-05-10 23:27:21 +020023#include <linux/mdio.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000024#include <linux/module.h>
Vivien Didelotcaac8542016-06-20 13:14:09 -040025#include <linux/of_device.h>
Andrew Lunnb516d452016-06-04 21:17:06 +020026#include <linux/of_mdio.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000027#include <linux/netdevice.h>
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +010028#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000029#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000030#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040031#include <net/switchdev.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000032#include "mv88e6xxx.h"
33
Vivien Didelotfad09c72016-06-21 12:28:20 -040034static void assert_reg_lock(struct mv88e6xxx_chip *chip)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040035{
Vivien Didelotfad09c72016-06-21 12:28:20 -040036 if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
37 dev_err(chip->dev, "Switch registers lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040038 dump_stack();
39 }
40}
41
Vivien Didelot914b32f2016-06-20 13:14:11 -040042/* The switch ADDR[4:1] configuration pins define the chip SMI device address
43 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
44 *
45 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
46 * is the only device connected to the SMI master. In this mode it responds to
47 * all 32 possible SMI addresses, and thus maps directly the internal devices.
48 *
49 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
50 * multiple devices to share the SMI interface. In this mode it responds to only
51 * 2 registers, used to indirectly access the internal SMI devices.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000052 */
Vivien Didelot914b32f2016-06-20 13:14:11 -040053
Vivien Didelotfad09c72016-06-21 12:28:20 -040054static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040055 int addr, int reg, u16 *val)
56{
Vivien Didelotfad09c72016-06-21 12:28:20 -040057 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040058 return -EOPNOTSUPP;
59
Vivien Didelotfad09c72016-06-21 12:28:20 -040060 return chip->smi_ops->read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040061}
62
Vivien Didelotfad09c72016-06-21 12:28:20 -040063static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040064 int addr, int reg, u16 val)
65{
Vivien Didelotfad09c72016-06-21 12:28:20 -040066 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040067 return -EOPNOTSUPP;
68
Vivien Didelotfad09c72016-06-21 12:28:20 -040069 return chip->smi_ops->write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040070}
71
Vivien Didelotfad09c72016-06-21 12:28:20 -040072static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040073 int addr, int reg, u16 *val)
74{
75 int ret;
76
Vivien Didelotfad09c72016-06-21 12:28:20 -040077 ret = mdiobus_read_nested(chip->bus, addr, reg);
Vivien Didelot914b32f2016-06-20 13:14:11 -040078 if (ret < 0)
79 return ret;
80
81 *val = ret & 0xffff;
82
83 return 0;
84}
85
Vivien Didelotfad09c72016-06-21 12:28:20 -040086static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040087 int addr, int reg, u16 val)
88{
89 int ret;
90
Vivien Didelotfad09c72016-06-21 12:28:20 -040091 ret = mdiobus_write_nested(chip->bus, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040092 if (ret < 0)
93 return ret;
94
95 return 0;
96}
97
98static const struct mv88e6xxx_ops mv88e6xxx_smi_single_chip_ops = {
99 .read = mv88e6xxx_smi_single_chip_read,
100 .write = mv88e6xxx_smi_single_chip_write,
101};
102
Vivien Didelotfad09c72016-06-21 12:28:20 -0400103static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000104{
105 int ret;
106 int i;
107
108 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400109 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000110 if (ret < 0)
111 return ret;
112
Andrew Lunncca8b132015-04-02 04:06:39 +0200113 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000114 return 0;
115 }
116
117 return -ETIMEDOUT;
118}
119
Vivien Didelotfad09c72016-06-21 12:28:20 -0400120static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400121 int addr, int reg, u16 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000122{
123 int ret;
124
Barry Grussling3675c8d2013-01-08 16:05:53 +0000125 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400126 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000127 if (ret < 0)
128 return ret;
129
Barry Grussling3675c8d2013-01-08 16:05:53 +0000130 /* Transmit the read command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400131 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Neil Armstrong6e899e62015-10-22 10:37:53 +0200132 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000133 if (ret < 0)
134 return ret;
135
Barry Grussling3675c8d2013-01-08 16:05:53 +0000136 /* Wait for the read command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400137 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000138 if (ret < 0)
139 return ret;
140
Barry Grussling3675c8d2013-01-08 16:05:53 +0000141 /* Read the data. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400142 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000143 if (ret < 0)
144 return ret;
145
Vivien Didelot914b32f2016-06-20 13:14:11 -0400146 *val = ret & 0xffff;
147
148 return 0;
149}
150
Vivien Didelotfad09c72016-06-21 12:28:20 -0400151static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400152 int addr, int reg, u16 val)
153{
154 int ret;
155
156 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400157 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400158 if (ret < 0)
159 return ret;
160
161 /* Transmit the data to write. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400162 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400163 if (ret < 0)
164 return ret;
165
166 /* Transmit the write command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400167 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400168 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
169 if (ret < 0)
170 return ret;
171
172 /* Wait for the write command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400173 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400174 if (ret < 0)
175 return ret;
176
177 return 0;
178}
179
180static const struct mv88e6xxx_ops mv88e6xxx_smi_multi_chip_ops = {
181 .read = mv88e6xxx_smi_multi_chip_read,
182 .write = mv88e6xxx_smi_multi_chip_write,
183};
184
Vivien Didelotfad09c72016-06-21 12:28:20 -0400185static int mv88e6xxx_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400186 int addr, int reg, u16 *val)
187{
188 int err;
189
Vivien Didelotfad09c72016-06-21 12:28:20 -0400190 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400191
Vivien Didelotfad09c72016-06-21 12:28:20 -0400192 err = mv88e6xxx_smi_read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400193 if (err)
194 return err;
195
Vivien Didelotfad09c72016-06-21 12:28:20 -0400196 dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400197 addr, reg, *val);
198
199 return 0;
200}
201
Vivien Didelotfad09c72016-06-21 12:28:20 -0400202static int mv88e6xxx_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400203 int addr, int reg, u16 val)
204{
205 int err;
206
Vivien Didelotfad09c72016-06-21 12:28:20 -0400207 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400208
Vivien Didelotfad09c72016-06-21 12:28:20 -0400209 err = mv88e6xxx_smi_write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400210 if (err)
211 return err;
212
Vivien Didelotfad09c72016-06-21 12:28:20 -0400213 dev_dbg(chip->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400214 addr, reg, val);
215
216 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000217}
218
Vivien Didelotfad09c72016-06-21 12:28:20 -0400219static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000220{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400221 u16 val;
222 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000223
Vivien Didelotfad09c72016-06-21 12:28:20 -0400224 err = mv88e6xxx_read(chip, addr, reg, &val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400225 if (err)
226 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400227
Vivien Didelot914b32f2016-06-20 13:14:11 -0400228 return val;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000229}
230
Vivien Didelotfad09c72016-06-21 12:28:20 -0400231static int mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700232{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700233 int ret;
234
Vivien Didelotfad09c72016-06-21 12:28:20 -0400235 mutex_lock(&chip->reg_lock);
236 ret = _mv88e6xxx_reg_read(chip, addr, reg);
237 mutex_unlock(&chip->reg_lock);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700238
239 return ret;
240}
241
Vivien Didelotfad09c72016-06-21 12:28:20 -0400242static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn158bc062016-04-28 21:24:06 -0400243 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000244{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400245 return mv88e6xxx_write(chip, addr, reg, val);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700246}
247
Vivien Didelotfad09c72016-06-21 12:28:20 -0400248static int mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
Vivien Didelot57d32312016-06-20 13:13:58 -0400249 int reg, u16 val)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700250{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700251 int ret;
252
Vivien Didelotfad09c72016-06-21 12:28:20 -0400253 mutex_lock(&chip->reg_lock);
254 ret = _mv88e6xxx_reg_write(chip, addr, reg, val);
255 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000256
257 return ret;
258}
259
Vivien Didelot1d13a062016-05-09 13:22:43 -0400260static int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000261{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400262 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200263 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000264
Vivien Didelotfad09c72016-06-21 12:28:20 -0400265 err = mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200266 (addr[0] << 8) | addr[1]);
267 if (err)
268 return err;
269
Vivien Didelotfad09c72016-06-21 12:28:20 -0400270 err = mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200271 (addr[2] << 8) | addr[3]);
272 if (err)
273 return err;
274
Vivien Didelotfad09c72016-06-21 12:28:20 -0400275 return mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200276 (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000277}
278
Vivien Didelot1d13a062016-05-09 13:22:43 -0400279static int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000280{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400281 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000282 int ret;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200283 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000284
285 for (i = 0; i < 6; i++) {
286 int j;
287
Barry Grussling3675c8d2013-01-08 16:05:53 +0000288 /* Write the MAC address byte. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400289 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200290 GLOBAL2_SWITCH_MAC_BUSY |
291 (i << 8) | addr[i]);
292 if (ret)
293 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000294
Barry Grussling3675c8d2013-01-08 16:05:53 +0000295 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000296 for (j = 0; j < 16; j++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400297 ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200298 GLOBAL2_SWITCH_MAC);
299 if (ret < 0)
300 return ret;
301
Andrew Lunncca8b132015-04-02 04:06:39 +0200302 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000303 break;
304 }
305 if (j == 16)
306 return -ETIMEDOUT;
307 }
308
309 return 0;
310}
311
Vivien Didelot57d32312016-06-20 13:13:58 -0400312static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
Vivien Didelot1d13a062016-05-09 13:22:43 -0400313{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400314 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot1d13a062016-05-09 13:22:43 -0400315
Vivien Didelotfad09c72016-06-21 12:28:20 -0400316 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SWITCH_MAC))
Vivien Didelot1d13a062016-05-09 13:22:43 -0400317 return mv88e6xxx_set_addr_indirect(ds, addr);
318 else
319 return mv88e6xxx_set_addr_direct(ds, addr);
320}
321
Vivien Didelotfad09c72016-06-21 12:28:20 -0400322static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200323 int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000324{
325 if (addr >= 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400326 return _mv88e6xxx_reg_read(chip, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000327 return 0xffff;
328}
329
Vivien Didelotfad09c72016-06-21 12:28:20 -0400330static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200331 int addr, int regnum, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000332{
333 if (addr >= 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400334 return _mv88e6xxx_reg_write(chip, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000335 return 0;
336}
337
Vivien Didelotfad09c72016-06-21 12:28:20 -0400338static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000339{
340 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000341 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000342
Vivien Didelotfad09c72016-06-21 12:28:20 -0400343 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200344 if (ret < 0)
345 return ret;
346
Vivien Didelotfad09c72016-06-21 12:28:20 -0400347 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400348 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200349 if (ret)
350 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000351
Barry Grussling19b2f972013-01-08 16:05:54 +0000352 timeout = jiffies + 1 * HZ;
353 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400354 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200355 if (ret < 0)
356 return ret;
357
Barry Grussling19b2f972013-01-08 16:05:54 +0000358 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200359 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
360 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000361 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000362 }
363
364 return -ETIMEDOUT;
365}
366
Vivien Didelotfad09c72016-06-21 12:28:20 -0400367static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000368{
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200369 int ret, err;
Barry Grussling19b2f972013-01-08 16:05:54 +0000370 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000371
Vivien Didelotfad09c72016-06-21 12:28:20 -0400372 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200373 if (ret < 0)
374 return ret;
375
Vivien Didelotfad09c72016-06-21 12:28:20 -0400376 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot762eb672016-06-04 21:16:54 +0200377 ret | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200378 if (err)
379 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000380
Barry Grussling19b2f972013-01-08 16:05:54 +0000381 timeout = jiffies + 1 * HZ;
382 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400383 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200384 if (ret < 0)
385 return ret;
386
Barry Grussling19b2f972013-01-08 16:05:54 +0000387 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200388 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
389 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000390 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000391 }
392
393 return -ETIMEDOUT;
394}
395
396static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
397{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400398 struct mv88e6xxx_chip *chip;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000399
Vivien Didelotfad09c72016-06-21 12:28:20 -0400400 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200401
Vivien Didelotfad09c72016-06-21 12:28:20 -0400402 mutex_lock(&chip->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200403
Vivien Didelotfad09c72016-06-21 12:28:20 -0400404 if (mutex_trylock(&chip->ppu_mutex)) {
405 if (mv88e6xxx_ppu_enable(chip) == 0)
406 chip->ppu_disabled = 0;
407 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000408 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200409
Vivien Didelotfad09c72016-06-21 12:28:20 -0400410 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000411}
412
413static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
414{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400415 struct mv88e6xxx_chip *chip = (void *)_ps;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000416
Vivien Didelotfad09c72016-06-21 12:28:20 -0400417 schedule_work(&chip->ppu_work);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000418}
419
Vivien Didelotfad09c72016-06-21 12:28:20 -0400420static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000421{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000422 int ret;
423
Vivien Didelotfad09c72016-06-21 12:28:20 -0400424 mutex_lock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000425
Barry Grussling3675c8d2013-01-08 16:05:53 +0000426 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000427 * we can access the PHY registers. If it was already
428 * disabled, cancel the timer that is going to re-enable
429 * it.
430 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400431 if (!chip->ppu_disabled) {
432 ret = mv88e6xxx_ppu_disable(chip);
Barry Grussling85686582013-01-08 16:05:56 +0000433 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400434 mutex_unlock(&chip->ppu_mutex);
Barry Grussling85686582013-01-08 16:05:56 +0000435 return ret;
436 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400437 chip->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000438 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400439 del_timer(&chip->ppu_timer);
Barry Grussling85686582013-01-08 16:05:56 +0000440 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000441 }
442
443 return ret;
444}
445
Vivien Didelotfad09c72016-06-21 12:28:20 -0400446static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000447{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000448 /* Schedule a timer to re-enable the PHY polling unit. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400449 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
450 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000451}
452
Vivien Didelotfad09c72016-06-21 12:28:20 -0400453static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000454{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400455 mutex_init(&chip->ppu_mutex);
456 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
457 init_timer(&chip->ppu_timer);
458 chip->ppu_timer.data = (unsigned long)chip;
459 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000460}
461
Vivien Didelotfad09c72016-06-21 12:28:20 -0400462static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200463 int regnum)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000464{
465 int ret;
466
Vivien Didelotfad09c72016-06-21 12:28:20 -0400467 ret = mv88e6xxx_ppu_access_get(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000468 if (ret >= 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400469 ret = _mv88e6xxx_reg_read(chip, addr, regnum);
470 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000471 }
472
473 return ret;
474}
475
Vivien Didelotfad09c72016-06-21 12:28:20 -0400476static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200477 int regnum, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000478{
479 int ret;
480
Vivien Didelotfad09c72016-06-21 12:28:20 -0400481 ret = mv88e6xxx_ppu_access_get(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000482 if (ret >= 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400483 ret = _mv88e6xxx_reg_write(chip, addr, regnum, val);
484 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000485 }
486
487 return ret;
488}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000489
Vivien Didelotfad09c72016-06-21 12:28:20 -0400490static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200491{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400492 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200493}
494
Vivien Didelotfad09c72016-06-21 12:28:20 -0400495static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200496{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400497 return chip->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200498}
499
Vivien Didelotfad09c72016-06-21 12:28:20 -0400500static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200501{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400502 return chip->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200503}
504
Vivien Didelotfad09c72016-06-21 12:28:20 -0400505static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200506{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400507 return chip->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200508}
509
Vivien Didelotfad09c72016-06-21 12:28:20 -0400510static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200511{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400512 return chip->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200513}
514
Vivien Didelotfad09c72016-06-21 12:28:20 -0400515static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700516{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400517 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700518}
519
Vivien Didelotfad09c72016-06-21 12:28:20 -0400520static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200521{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400522 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200523}
524
Vivien Didelotfad09c72016-06-21 12:28:20 -0400525static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200526{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400527 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200528}
529
Vivien Didelotfad09c72016-06-21 12:28:20 -0400530static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400531{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400532 return chip->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400533}
534
Vivien Didelotfad09c72016-06-21 12:28:20 -0400535static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400536{
537 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400538 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
539 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400540 return true;
541
542 return false;
543}
544
Andrew Lunndea87022015-08-31 15:56:47 +0200545/* We expect the switch to perform auto negotiation if there is a real
546 * phy. However, in the case of a fixed link phy, we force the port
547 * settings from the fixed link settings.
548 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400549static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
550 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200551{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400552 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200553 u32 reg;
554 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200555
556 if (!phy_is_pseudo_fixed_link(phydev))
557 return;
558
Vivien Didelotfad09c72016-06-21 12:28:20 -0400559 mutex_lock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200560
Vivien Didelotfad09c72016-06-21 12:28:20 -0400561 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200562 if (ret < 0)
563 goto out;
564
565 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
566 PORT_PCS_CTRL_FORCE_LINK |
567 PORT_PCS_CTRL_DUPLEX_FULL |
568 PORT_PCS_CTRL_FORCE_DUPLEX |
569 PORT_PCS_CTRL_UNFORCED);
570
571 reg |= PORT_PCS_CTRL_FORCE_LINK;
572 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400573 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200574
Vivien Didelotfad09c72016-06-21 12:28:20 -0400575 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200576 goto out;
577
578 switch (phydev->speed) {
579 case SPEED_1000:
580 reg |= PORT_PCS_CTRL_1000;
581 break;
582 case SPEED_100:
583 reg |= PORT_PCS_CTRL_100;
584 break;
585 case SPEED_10:
586 reg |= PORT_PCS_CTRL_10;
587 break;
588 default:
589 pr_info("Unknown speed");
590 goto out;
591 }
592
593 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
594 if (phydev->duplex == DUPLEX_FULL)
595 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
596
Vivien Didelotfad09c72016-06-21 12:28:20 -0400597 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
598 (port >= chip->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200599 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
600 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
601 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
602 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
603 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
604 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
605 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
606 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400607 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200608
609out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400610 mutex_unlock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200611}
612
Vivien Didelotfad09c72016-06-21 12:28:20 -0400613static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000614{
615 int ret;
616 int i;
617
618 for (i = 0; i < 10; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400619 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200620 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000621 return 0;
622 }
623
624 return -ETIMEDOUT;
625}
626
Vivien Didelotfad09c72016-06-21 12:28:20 -0400627static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000628{
629 int ret;
630
Vivien Didelotfad09c72016-06-21 12:28:20 -0400631 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200632 port = (port + 1) << 5;
633
Barry Grussling3675c8d2013-01-08 16:05:53 +0000634 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400635 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200636 GLOBAL_STATS_OP_CAPTURE_PORT |
637 GLOBAL_STATS_OP_HIST_RX_TX | port);
638 if (ret < 0)
639 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000640
Barry Grussling3675c8d2013-01-08 16:05:53 +0000641 /* Wait for the snapshotting to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400642 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000643 if (ret < 0)
644 return ret;
645
646 return 0;
647}
648
Vivien Didelotfad09c72016-06-21 12:28:20 -0400649static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400650 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000651{
652 u32 _val;
653 int ret;
654
655 *val = 0;
656
Vivien Didelotfad09c72016-06-21 12:28:20 -0400657 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200658 GLOBAL_STATS_OP_READ_CAPTURED |
659 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000660 if (ret < 0)
661 return;
662
Vivien Didelotfad09c72016-06-21 12:28:20 -0400663 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000664 if (ret < 0)
665 return;
666
Vivien Didelotfad09c72016-06-21 12:28:20 -0400667 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000668 if (ret < 0)
669 return;
670
671 _val = ret << 16;
672
Vivien Didelotfad09c72016-06-21 12:28:20 -0400673 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000674 if (ret < 0)
675 return;
676
677 *val = _val | ret;
678}
679
Andrew Lunne413e7e2015-04-02 04:06:38 +0200680static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100681 { "in_good_octets", 8, 0x00, BANK0, },
682 { "in_bad_octets", 4, 0x02, BANK0, },
683 { "in_unicast", 4, 0x04, BANK0, },
684 { "in_broadcasts", 4, 0x06, BANK0, },
685 { "in_multicasts", 4, 0x07, BANK0, },
686 { "in_pause", 4, 0x16, BANK0, },
687 { "in_undersize", 4, 0x18, BANK0, },
688 { "in_fragments", 4, 0x19, BANK0, },
689 { "in_oversize", 4, 0x1a, BANK0, },
690 { "in_jabber", 4, 0x1b, BANK0, },
691 { "in_rx_error", 4, 0x1c, BANK0, },
692 { "in_fcs_error", 4, 0x1d, BANK0, },
693 { "out_octets", 8, 0x0e, BANK0, },
694 { "out_unicast", 4, 0x10, BANK0, },
695 { "out_broadcasts", 4, 0x13, BANK0, },
696 { "out_multicasts", 4, 0x12, BANK0, },
697 { "out_pause", 4, 0x15, BANK0, },
698 { "excessive", 4, 0x11, BANK0, },
699 { "collisions", 4, 0x1e, BANK0, },
700 { "deferred", 4, 0x05, BANK0, },
701 { "single", 4, 0x14, BANK0, },
702 { "multiple", 4, 0x17, BANK0, },
703 { "out_fcs_error", 4, 0x03, BANK0, },
704 { "late", 4, 0x1f, BANK0, },
705 { "hist_64bytes", 4, 0x08, BANK0, },
706 { "hist_65_127bytes", 4, 0x09, BANK0, },
707 { "hist_128_255bytes", 4, 0x0a, BANK0, },
708 { "hist_256_511bytes", 4, 0x0b, BANK0, },
709 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
710 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
711 { "sw_in_discards", 4, 0x10, PORT, },
712 { "sw_in_filtered", 2, 0x12, PORT, },
713 { "sw_out_filtered", 2, 0x13, PORT, },
714 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
715 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
716 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
717 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
718 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
719 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
720 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
721 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
722 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
723 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
724 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
725 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
726 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
727 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
728 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
729 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
730 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
731 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
732 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
733 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
734 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
735 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
736 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
737 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
738 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
739 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200740};
741
Vivien Didelotfad09c72016-06-21 12:28:20 -0400742static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100743 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200744{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100745 switch (stat->type) {
746 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200747 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100748 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400749 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100750 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400751 return mv88e6xxx_6095_family(chip) ||
752 mv88e6xxx_6185_family(chip) ||
753 mv88e6xxx_6097_family(chip) ||
754 mv88e6xxx_6165_family(chip) ||
755 mv88e6xxx_6351_family(chip) ||
756 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200757 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100758 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000759}
760
Vivien Didelotfad09c72016-06-21 12:28:20 -0400761static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100762 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200763 int port)
764{
Andrew Lunn80c46272015-06-20 18:42:30 +0200765 u32 low;
766 u32 high = 0;
767 int ret;
768 u64 value;
769
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100770 switch (s->type) {
771 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400772 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200773 if (ret < 0)
774 return UINT64_MAX;
775
776 low = ret;
777 if (s->sizeof_stat == 4) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400778 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100779 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200780 if (ret < 0)
781 return UINT64_MAX;
782 high = ret;
783 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100784 break;
785 case BANK0:
786 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400787 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200788 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400789 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200790 }
791 value = (((u64)high) << 16) | low;
792 return value;
793}
794
Vivien Didelotf81ec902016-05-09 13:22:58 -0400795static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
796 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100797{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400798 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100799 struct mv88e6xxx_hw_stat *stat;
800 int i, j;
801
802 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
803 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400804 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100805 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
806 ETH_GSTRING_LEN);
807 j++;
808 }
809 }
810}
811
Vivien Didelotf81ec902016-05-09 13:22:58 -0400812static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100813{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400814 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100815 struct mv88e6xxx_hw_stat *stat;
816 int i, j;
817
818 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
819 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400820 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100821 j++;
822 }
823 return j;
824}
825
Vivien Didelotf81ec902016-05-09 13:22:58 -0400826static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
827 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000828{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400829 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100830 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000831 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100832 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000833
Vivien Didelotfad09c72016-06-21 12:28:20 -0400834 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000835
Vivien Didelotfad09c72016-06-21 12:28:20 -0400836 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000837 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400838 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000839 return;
840 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100841 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
842 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400843 if (mv88e6xxx_has_stat(chip, stat)) {
844 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100845 j++;
846 }
847 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000848
Vivien Didelotfad09c72016-06-21 12:28:20 -0400849 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000850}
Ben Hutchings98e67302011-11-25 14:36:19 +0000851
Vivien Didelotf81ec902016-05-09 13:22:58 -0400852static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700853{
854 return 32 * sizeof(u16);
855}
856
Vivien Didelotf81ec902016-05-09 13:22:58 -0400857static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
858 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700859{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400860 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700861 u16 *p = _p;
862 int i;
863
864 regs->version = 0;
865
866 memset(p, 0xff, 32 * sizeof(u16));
867
Vivien Didelotfad09c72016-06-21 12:28:20 -0400868 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400869
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700870 for (i = 0; i < 32; i++) {
871 int ret;
872
Vivien Didelotfad09c72016-06-21 12:28:20 -0400873 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700874 if (ret >= 0)
875 p[i] = ret;
876 }
Vivien Didelot23062512016-05-09 13:22:45 -0400877
Vivien Didelotfad09c72016-06-21 12:28:20 -0400878 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700879}
880
Vivien Didelotfad09c72016-06-21 12:28:20 -0400881static int _mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int reg, int offset,
Andrew Lunn3898c142015-05-06 01:09:53 +0200882 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700883{
884 unsigned long timeout = jiffies + HZ / 10;
885
886 while (time_before(jiffies, timeout)) {
887 int ret;
888
Vivien Didelotfad09c72016-06-21 12:28:20 -0400889 ret = _mv88e6xxx_reg_read(chip, reg, offset);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700890 if (ret < 0)
891 return ret;
892 if (!(ret & mask))
893 return 0;
894
895 usleep_range(1000, 2000);
896 }
897 return -ETIMEDOUT;
898}
899
Vivien Didelotfad09c72016-06-21 12:28:20 -0400900static int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int reg,
Andrew Lunn158bc062016-04-28 21:24:06 -0400901 int offset, u16 mask)
Andrew Lunn3898c142015-05-06 01:09:53 +0200902{
Andrew Lunn3898c142015-05-06 01:09:53 +0200903 int ret;
904
Vivien Didelotfad09c72016-06-21 12:28:20 -0400905 mutex_lock(&chip->reg_lock);
906 ret = _mv88e6xxx_wait(chip, reg, offset, mask);
907 mutex_unlock(&chip->reg_lock);
Andrew Lunn3898c142015-05-06 01:09:53 +0200908
909 return ret;
910}
911
Vivien Didelotfad09c72016-06-21 12:28:20 -0400912static int mv88e6xxx_mdio_wait(struct mv88e6xxx_chip *chip)
Andrew Lunn3898c142015-05-06 01:09:53 +0200913{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400914 return _mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200915 GLOBAL2_SMI_OP_BUSY);
916}
917
Vivien Didelotd24645b2016-05-09 13:22:41 -0400918static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200919{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400920 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -0400921
Vivien Didelotfad09c72016-06-21 12:28:20 -0400922 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200923 GLOBAL2_EEPROM_OP_LOAD);
924}
925
Vivien Didelotd24645b2016-05-09 13:22:41 -0400926static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200927{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400928 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -0400929
Vivien Didelotfad09c72016-06-21 12:28:20 -0400930 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200931 GLOBAL2_EEPROM_OP_BUSY);
932}
933
Vivien Didelotd24645b2016-05-09 13:22:41 -0400934static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr)
935{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400936 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotd24645b2016-05-09 13:22:41 -0400937 int ret;
938
Vivien Didelotfad09c72016-06-21 12:28:20 -0400939 mutex_lock(&chip->eeprom_mutex);
Vivien Didelotd24645b2016-05-09 13:22:41 -0400940
Vivien Didelotfad09c72016-06-21 12:28:20 -0400941 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Vivien Didelotd24645b2016-05-09 13:22:41 -0400942 GLOBAL2_EEPROM_OP_READ |
943 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
944 if (ret < 0)
945 goto error;
946
947 ret = mv88e6xxx_eeprom_busy_wait(ds);
948 if (ret < 0)
949 goto error;
950
Vivien Didelotfad09c72016-06-21 12:28:20 -0400951 ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
Vivien Didelotd24645b2016-05-09 13:22:41 -0400952error:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400953 mutex_unlock(&chip->eeprom_mutex);
Vivien Didelotd24645b2016-05-09 13:22:41 -0400954 return ret;
955}
956
Andrew Lunnf8cd8752016-05-10 23:27:25 +0200957static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
958{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400959 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf8cd8752016-05-10 23:27:25 +0200960
Vivien Didelotfad09c72016-06-21 12:28:20 -0400961 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
962 return chip->eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +0200963
964 return 0;
965}
966
Vivien Didelotf81ec902016-05-09 13:22:58 -0400967static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
968 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -0400969{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400970 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotd24645b2016-05-09 13:22:41 -0400971 int offset;
972 int len;
973 int ret;
974
Vivien Didelotfad09c72016-06-21 12:28:20 -0400975 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
Vivien Didelotd24645b2016-05-09 13:22:41 -0400976 return -EOPNOTSUPP;
977
978 offset = eeprom->offset;
979 len = eeprom->len;
980 eeprom->len = 0;
981
982 eeprom->magic = 0xc3ec4951;
983
984 ret = mv88e6xxx_eeprom_load_wait(ds);
985 if (ret < 0)
986 return ret;
987
988 if (offset & 1) {
989 int word;
990
991 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
992 if (word < 0)
993 return word;
994
995 *data++ = (word >> 8) & 0xff;
996
997 offset++;
998 len--;
999 eeprom->len++;
1000 }
1001
1002 while (len >= 2) {
1003 int word;
1004
1005 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1006 if (word < 0)
1007 return word;
1008
1009 *data++ = word & 0xff;
1010 *data++ = (word >> 8) & 0xff;
1011
1012 offset += 2;
1013 len -= 2;
1014 eeprom->len += 2;
1015 }
1016
1017 if (len) {
1018 int word;
1019
1020 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1021 if (word < 0)
1022 return word;
1023
1024 *data++ = word & 0xff;
1025
1026 offset++;
1027 len--;
1028 eeprom->len++;
1029 }
1030
1031 return 0;
1032}
1033
1034static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds)
1035{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001036 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001037 int ret;
1038
Vivien Didelotfad09c72016-06-21 12:28:20 -04001039 ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001040 if (ret < 0)
1041 return ret;
1042
1043 if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
1044 return -EROFS;
1045
1046 return 0;
1047}
1048
1049static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr,
1050 u16 data)
1051{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001052 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001053 int ret;
1054
Vivien Didelotfad09c72016-06-21 12:28:20 -04001055 mutex_lock(&chip->eeprom_mutex);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001056
Vivien Didelotfad09c72016-06-21 12:28:20 -04001057 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001058 if (ret < 0)
1059 goto error;
1060
Vivien Didelotfad09c72016-06-21 12:28:20 -04001061 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Vivien Didelotd24645b2016-05-09 13:22:41 -04001062 GLOBAL2_EEPROM_OP_WRITE |
1063 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
1064 if (ret < 0)
1065 goto error;
1066
1067 ret = mv88e6xxx_eeprom_busy_wait(ds);
1068error:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001069 mutex_unlock(&chip->eeprom_mutex);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001070 return ret;
1071}
1072
Vivien Didelotf81ec902016-05-09 13:22:58 -04001073static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
1074 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -04001075{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001076 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001077 int offset;
1078 int ret;
1079 int len;
1080
Vivien Didelotfad09c72016-06-21 12:28:20 -04001081 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
Vivien Didelotd24645b2016-05-09 13:22:41 -04001082 return -EOPNOTSUPP;
1083
1084 if (eeprom->magic != 0xc3ec4951)
1085 return -EINVAL;
1086
1087 ret = mv88e6xxx_eeprom_is_readonly(ds);
1088 if (ret)
1089 return ret;
1090
1091 offset = eeprom->offset;
1092 len = eeprom->len;
1093 eeprom->len = 0;
1094
1095 ret = mv88e6xxx_eeprom_load_wait(ds);
1096 if (ret < 0)
1097 return ret;
1098
1099 if (offset & 1) {
1100 int word;
1101
1102 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1103 if (word < 0)
1104 return word;
1105
1106 word = (*data++ << 8) | (word & 0xff);
1107
1108 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1109 if (ret < 0)
1110 return ret;
1111
1112 offset++;
1113 len--;
1114 eeprom->len++;
1115 }
1116
1117 while (len >= 2) {
1118 int word;
1119
1120 word = *data++;
1121 word |= *data++ << 8;
1122
1123 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1124 if (ret < 0)
1125 return ret;
1126
1127 offset += 2;
1128 len -= 2;
1129 eeprom->len += 2;
1130 }
1131
1132 if (len) {
1133 int word;
1134
1135 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1136 if (word < 0)
1137 return word;
1138
1139 word = (word & 0xff00) | *data++;
1140
1141 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1142 if (ret < 0)
1143 return ret;
1144
1145 offset++;
1146 len--;
1147 eeprom->len++;
1148 }
1149
1150 return 0;
1151}
1152
Vivien Didelotfad09c72016-06-21 12:28:20 -04001153static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001154{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001155 return _mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
Andrew Lunncca8b132015-04-02 04:06:39 +02001156 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001157}
1158
Vivien Didelotfad09c72016-06-21 12:28:20 -04001159static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001160 int addr, int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +01001161{
1162 int ret;
1163
Vivien Didelotfad09c72016-06-21 12:28:20 -04001164 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001165 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
1166 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +01001167 if (ret < 0)
1168 return ret;
1169
Vivien Didelotfad09c72016-06-21 12:28:20 -04001170 ret = mv88e6xxx_mdio_wait(chip);
Andrew Lunn3898c142015-05-06 01:09:53 +02001171 if (ret < 0)
1172 return ret;
1173
Vivien Didelotfad09c72016-06-21 12:28:20 -04001174 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA);
Andrew Lunn158bc062016-04-28 21:24:06 -04001175
1176 return ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001177}
1178
Vivien Didelotfad09c72016-06-21 12:28:20 -04001179static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001180 int addr, int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +01001181{
Andrew Lunn3898c142015-05-06 01:09:53 +02001182 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001183
Vivien Didelotfad09c72016-06-21 12:28:20 -04001184 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02001185 if (ret < 0)
1186 return ret;
1187
Vivien Didelotfad09c72016-06-21 12:28:20 -04001188 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001189 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
1190 regnum);
1191
Vivien Didelotfad09c72016-06-21 12:28:20 -04001192 return mv88e6xxx_mdio_wait(chip);
Andrew Lunnf3044682015-02-14 19:17:50 +01001193}
1194
Vivien Didelotf81ec902016-05-09 13:22:58 -04001195static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1196 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001197{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001198 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001199 int reg;
1200
Vivien Didelotfad09c72016-06-21 12:28:20 -04001201 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001202 return -EOPNOTSUPP;
1203
Vivien Didelotfad09c72016-06-21 12:28:20 -04001204 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001205
Vivien Didelotfad09c72016-06-21 12:28:20 -04001206 reg = mv88e6xxx_mdio_read_indirect(chip, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001207 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001208 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001209
1210 e->eee_enabled = !!(reg & 0x0200);
1211 e->tx_lpi_enabled = !!(reg & 0x0100);
1212
Vivien Didelotfad09c72016-06-21 12:28:20 -04001213 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001214 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001215 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001216
Andrew Lunncca8b132015-04-02 04:06:39 +02001217 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001218 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001219
Andrew Lunn2f40c692015-04-02 04:06:37 +02001220out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001221 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001222 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001223}
1224
Vivien Didelotf81ec902016-05-09 13:22:58 -04001225static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1226 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001227{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001228 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001229 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001230 int ret;
1231
Vivien Didelotfad09c72016-06-21 12:28:20 -04001232 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001233 return -EOPNOTSUPP;
1234
Vivien Didelotfad09c72016-06-21 12:28:20 -04001235 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001236
Vivien Didelotfad09c72016-06-21 12:28:20 -04001237 ret = mv88e6xxx_mdio_read_indirect(chip, port, 16);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001238 if (ret < 0)
1239 goto out;
1240
1241 reg = ret & ~0x0300;
1242 if (e->eee_enabled)
1243 reg |= 0x0200;
1244 if (e->tx_lpi_enabled)
1245 reg |= 0x0100;
1246
Vivien Didelotfad09c72016-06-21 12:28:20 -04001247 ret = mv88e6xxx_mdio_write_indirect(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001248out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001249 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001250
1251 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001252}
1253
Vivien Didelotfad09c72016-06-21 12:28:20 -04001254static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001255{
1256 int ret;
1257
Vivien Didelotfad09c72016-06-21 12:28:20 -04001258 if (mv88e6xxx_has_fid_reg(chip)) {
1259 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
1260 fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001261 if (ret < 0)
1262 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001263 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001264 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001265 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -04001266 if (ret < 0)
1267 return ret;
1268
Vivien Didelotfad09c72016-06-21 12:28:20 -04001269 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001270 (ret & 0xfff) |
1271 ((fid << 8) & 0xf000));
1272 if (ret < 0)
1273 return ret;
1274
1275 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1276 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001277 }
1278
Vivien Didelotfad09c72016-06-21 12:28:20 -04001279 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001280 if (ret < 0)
1281 return ret;
1282
Vivien Didelotfad09c72016-06-21 12:28:20 -04001283 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001284}
1285
Vivien Didelotfad09c72016-06-21 12:28:20 -04001286static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001287 struct mv88e6xxx_atu_entry *entry)
1288{
1289 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1290
1291 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1292 unsigned int mask, shift;
1293
1294 if (entry->trunk) {
1295 data |= GLOBAL_ATU_DATA_TRUNK;
1296 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1297 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1298 } else {
1299 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1300 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1301 }
1302
1303 data |= (entry->portv_trunkid << shift) & mask;
1304 }
1305
Vivien Didelotfad09c72016-06-21 12:28:20 -04001306 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001307}
1308
Vivien Didelotfad09c72016-06-21 12:28:20 -04001309static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001310 struct mv88e6xxx_atu_entry *entry,
1311 bool static_too)
1312{
1313 int op;
1314 int err;
1315
Vivien Didelotfad09c72016-06-21 12:28:20 -04001316 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001317 if (err)
1318 return err;
1319
Vivien Didelotfad09c72016-06-21 12:28:20 -04001320 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001321 if (err)
1322 return err;
1323
1324 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001325 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1326 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1327 } else {
1328 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1329 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1330 }
1331
Vivien Didelotfad09c72016-06-21 12:28:20 -04001332 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001333}
1334
Vivien Didelotfad09c72016-06-21 12:28:20 -04001335static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001336 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001337{
1338 struct mv88e6xxx_atu_entry entry = {
1339 .fid = fid,
1340 .state = 0, /* EntryState bits must be 0 */
1341 };
1342
Vivien Didelotfad09c72016-06-21 12:28:20 -04001343 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001344}
1345
Vivien Didelotfad09c72016-06-21 12:28:20 -04001346static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001347 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001348{
1349 struct mv88e6xxx_atu_entry entry = {
1350 .trunk = false,
1351 .fid = fid,
1352 };
1353
1354 /* EntryState bits must be 0xF */
1355 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1356
1357 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1358 entry.portv_trunkid = (to_port & 0x0f) << 4;
1359 entry.portv_trunkid |= from_port & 0x0f;
1360
Vivien Didelotfad09c72016-06-21 12:28:20 -04001361 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001362}
1363
Vivien Didelotfad09c72016-06-21 12:28:20 -04001364static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001365 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001366{
1367 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001368 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001369}
1370
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001371static const char * const mv88e6xxx_port_state_names[] = {
1372 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1373 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1374 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1375 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1376};
1377
Vivien Didelotfad09c72016-06-21 12:28:20 -04001378static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001379 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001380{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001381 struct dsa_switch *ds = chip->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001382 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001383 u8 oldstate;
1384
Vivien Didelotfad09c72016-06-21 12:28:20 -04001385 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001386 if (reg < 0)
1387 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001388
Andrew Lunncca8b132015-04-02 04:06:39 +02001389 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001390
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001391 if (oldstate != state) {
1392 /* Flush forwarding database if we're moving a port
1393 * from Learning or Forwarding state to Disabled or
1394 * Blocking or Listening state.
1395 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001396 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
Vivien Didelot57d32312016-06-20 13:13:58 -04001397 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1398 (state == PORT_CONTROL_STATE_DISABLED ||
1399 state == PORT_CONTROL_STATE_BLOCKING)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001400 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001401 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001402 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001403 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001404
Andrew Lunncca8b132015-04-02 04:06:39 +02001405 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001406 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001407 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001408 if (ret)
1409 return ret;
1410
Andrew Lunnc8b09802016-06-04 21:16:57 +02001411 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001412 mv88e6xxx_port_state_names[state],
1413 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001414 }
1415
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001416 return ret;
1417}
1418
Vivien Didelotfad09c72016-06-21 12:28:20 -04001419static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001420{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001421 struct net_device *bridge = chip->ports[port].bridge_dev;
1422 const u16 mask = (1 << chip->info->num_ports) - 1;
1423 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001424 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001425 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001426 int i;
1427
1428 /* allow CPU port or DSA link(s) to send frames to every port */
1429 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1430 output_ports = mask;
1431 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001432 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001433 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001434 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001435 output_ports |= BIT(i);
1436
1437 /* allow sending frames to CPU port and DSA link(s) */
1438 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1439 output_ports |= BIT(i);
1440 }
1441 }
1442
1443 /* prevent frames from going back out of the port they came in on */
1444 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001445
Vivien Didelotfad09c72016-06-21 12:28:20 -04001446 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001447 if (reg < 0)
1448 return reg;
1449
1450 reg &= ~mask;
1451 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001452
Vivien Didelotfad09c72016-06-21 12:28:20 -04001453 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001454}
1455
Vivien Didelotf81ec902016-05-09 13:22:58 -04001456static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1457 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001458{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001459 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001460 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001461 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001462
Vivien Didelotfad09c72016-06-21 12:28:20 -04001463 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_PORTSTATE))
Vivien Didelot936f2342016-05-09 13:22:46 -04001464 return;
1465
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001466 switch (state) {
1467 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001468 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001469 break;
1470 case BR_STATE_BLOCKING:
1471 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001472 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001473 break;
1474 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001475 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001476 break;
1477 case BR_STATE_FORWARDING:
1478 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001479 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001480 break;
1481 }
1482
Vivien Didelotfad09c72016-06-21 12:28:20 -04001483 mutex_lock(&chip->reg_lock);
1484 err = _mv88e6xxx_port_state(chip, port, stp_state);
1485 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001486
1487 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001488 netdev_err(ds->ports[port].netdev,
1489 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001490 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001491}
1492
Vivien Didelotfad09c72016-06-21 12:28:20 -04001493static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001494 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001495{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001496 struct dsa_switch *ds = chip->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001497 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001498 int ret;
1499
Vivien Didelotfad09c72016-06-21 12:28:20 -04001500 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001501 if (ret < 0)
1502 return ret;
1503
Vivien Didelot5da96032016-03-07 18:24:39 -05001504 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1505
1506 if (new) {
1507 ret &= ~PORT_DEFAULT_VLAN_MASK;
1508 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1509
Vivien Didelotfad09c72016-06-21 12:28:20 -04001510 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001511 PORT_DEFAULT_VLAN, ret);
1512 if (ret < 0)
1513 return ret;
1514
Andrew Lunnc8b09802016-06-04 21:16:57 +02001515 netdev_dbg(ds->ports[port].netdev,
1516 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001517 }
1518
1519 if (old)
1520 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001521
1522 return 0;
1523}
1524
Vivien Didelotfad09c72016-06-21 12:28:20 -04001525static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001526 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001527{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001528 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001529}
1530
Vivien Didelotfad09c72016-06-21 12:28:20 -04001531static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001532 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001533{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001534 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001535}
1536
Vivien Didelotfad09c72016-06-21 12:28:20 -04001537static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001538{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001539 return _mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
Vivien Didelot6b17e862015-08-13 12:52:18 -04001540 GLOBAL_VTU_OP_BUSY);
1541}
1542
Vivien Didelotfad09c72016-06-21 12:28:20 -04001543static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001544{
1545 int ret;
1546
Vivien Didelotfad09c72016-06-21 12:28:20 -04001547 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001548 if (ret < 0)
1549 return ret;
1550
Vivien Didelotfad09c72016-06-21 12:28:20 -04001551 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001552}
1553
Vivien Didelotfad09c72016-06-21 12:28:20 -04001554static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001555{
1556 int ret;
1557
Vivien Didelotfad09c72016-06-21 12:28:20 -04001558 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001559 if (ret < 0)
1560 return ret;
1561
Vivien Didelotfad09c72016-06-21 12:28:20 -04001562 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001563}
1564
Vivien Didelotfad09c72016-06-21 12:28:20 -04001565static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001566 struct mv88e6xxx_vtu_stu_entry *entry,
1567 unsigned int nibble_offset)
1568{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001569 u16 regs[3];
1570 int i;
1571 int ret;
1572
1573 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001574 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001575 GLOBAL_VTU_DATA_0_3 + i);
1576 if (ret < 0)
1577 return ret;
1578
1579 regs[i] = ret;
1580 }
1581
Vivien Didelotfad09c72016-06-21 12:28:20 -04001582 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001583 unsigned int shift = (i % 4) * 4 + nibble_offset;
1584 u16 reg = regs[i / 4];
1585
1586 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1587 }
1588
1589 return 0;
1590}
1591
Vivien Didelotfad09c72016-06-21 12:28:20 -04001592static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001593 struct mv88e6xxx_vtu_stu_entry *entry)
1594{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001595 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001596}
1597
Vivien Didelotfad09c72016-06-21 12:28:20 -04001598static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001599 struct mv88e6xxx_vtu_stu_entry *entry)
1600{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001601 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001602}
1603
Vivien Didelotfad09c72016-06-21 12:28:20 -04001604static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001605 struct mv88e6xxx_vtu_stu_entry *entry,
1606 unsigned int nibble_offset)
1607{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001608 u16 regs[3] = { 0 };
1609 int i;
1610 int ret;
1611
Vivien Didelotfad09c72016-06-21 12:28:20 -04001612 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001613 unsigned int shift = (i % 4) * 4 + nibble_offset;
1614 u8 data = entry->data[i];
1615
1616 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1617 }
1618
1619 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001620 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001621 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1622 if (ret < 0)
1623 return ret;
1624 }
1625
1626 return 0;
1627}
1628
Vivien Didelotfad09c72016-06-21 12:28:20 -04001629static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001630 struct mv88e6xxx_vtu_stu_entry *entry)
1631{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001632 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001633}
1634
Vivien Didelotfad09c72016-06-21 12:28:20 -04001635static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001636 struct mv88e6xxx_vtu_stu_entry *entry)
1637{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001638 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001639}
1640
Vivien Didelotfad09c72016-06-21 12:28:20 -04001641static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001642{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001643 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001644 vid & GLOBAL_VTU_VID_MASK);
1645}
1646
Vivien Didelotfad09c72016-06-21 12:28:20 -04001647static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001648 struct mv88e6xxx_vtu_stu_entry *entry)
1649{
1650 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1651 int ret;
1652
Vivien Didelotfad09c72016-06-21 12:28:20 -04001653 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001654 if (ret < 0)
1655 return ret;
1656
Vivien Didelotfad09c72016-06-21 12:28:20 -04001657 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001658 if (ret < 0)
1659 return ret;
1660
Vivien Didelotfad09c72016-06-21 12:28:20 -04001661 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001662 if (ret < 0)
1663 return ret;
1664
1665 next.vid = ret & GLOBAL_VTU_VID_MASK;
1666 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1667
1668 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001669 ret = mv88e6xxx_vtu_data_read(chip, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001670 if (ret < 0)
1671 return ret;
1672
Vivien Didelotfad09c72016-06-21 12:28:20 -04001673 if (mv88e6xxx_has_fid_reg(chip)) {
1674 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001675 GLOBAL_VTU_FID);
1676 if (ret < 0)
1677 return ret;
1678
1679 next.fid = ret & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001680 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001681 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1682 * VTU DBNum[3:0] are located in VTU Operation 3:0
1683 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001684 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001685 GLOBAL_VTU_OP);
1686 if (ret < 0)
1687 return ret;
1688
1689 next.fid = (ret & 0xf00) >> 4;
1690 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001691 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001692
Vivien Didelotfad09c72016-06-21 12:28:20 -04001693 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1694 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001695 GLOBAL_VTU_SID);
1696 if (ret < 0)
1697 return ret;
1698
1699 next.sid = ret & GLOBAL_VTU_SID_MASK;
1700 }
1701 }
1702
1703 *entry = next;
1704 return 0;
1705}
1706
Vivien Didelotf81ec902016-05-09 13:22:58 -04001707static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1708 struct switchdev_obj_port_vlan *vlan,
1709 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001710{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001711 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001712 struct mv88e6xxx_vtu_stu_entry next;
1713 u16 pvid;
1714 int err;
1715
Vivien Didelotfad09c72016-06-21 12:28:20 -04001716 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001717 return -EOPNOTSUPP;
1718
Vivien Didelotfad09c72016-06-21 12:28:20 -04001719 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001720
Vivien Didelotfad09c72016-06-21 12:28:20 -04001721 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001722 if (err)
1723 goto unlock;
1724
Vivien Didelotfad09c72016-06-21 12:28:20 -04001725 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001726 if (err)
1727 goto unlock;
1728
1729 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001730 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001731 if (err)
1732 break;
1733
1734 if (!next.valid)
1735 break;
1736
1737 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1738 continue;
1739
1740 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001741 vlan->vid_begin = next.vid;
1742 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001743 vlan->flags = 0;
1744
1745 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1746 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1747
1748 if (next.vid == pvid)
1749 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1750
1751 err = cb(&vlan->obj);
1752 if (err)
1753 break;
1754 } while (next.vid < GLOBAL_VTU_VID_MASK);
1755
1756unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001757 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001758
1759 return err;
1760}
1761
Vivien Didelotfad09c72016-06-21 12:28:20 -04001762static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001763 struct mv88e6xxx_vtu_stu_entry *entry)
1764{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001765 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001766 u16 reg = 0;
1767 int ret;
1768
Vivien Didelotfad09c72016-06-21 12:28:20 -04001769 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001770 if (ret < 0)
1771 return ret;
1772
1773 if (!entry->valid)
1774 goto loadpurge;
1775
1776 /* Write port member tags */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001777 ret = mv88e6xxx_vtu_data_write(chip, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001778 if (ret < 0)
1779 return ret;
1780
Vivien Didelotfad09c72016-06-21 12:28:20 -04001781 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001782 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001783 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1784 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001785 if (ret < 0)
1786 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001787 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001788
Vivien Didelotfad09c72016-06-21 12:28:20 -04001789 if (mv88e6xxx_has_fid_reg(chip)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001790 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001791 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1792 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001793 if (ret < 0)
1794 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001795 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001796 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1797 * VTU DBNum[3:0] are located in VTU Operation 3:0
1798 */
1799 op |= (entry->fid & 0xf0) << 8;
1800 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001801 }
1802
1803 reg = GLOBAL_VTU_VID_VALID;
1804loadpurge:
1805 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001806 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001807 if (ret < 0)
1808 return ret;
1809
Vivien Didelotfad09c72016-06-21 12:28:20 -04001810 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001811}
1812
Vivien Didelotfad09c72016-06-21 12:28:20 -04001813static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001814 struct mv88e6xxx_vtu_stu_entry *entry)
1815{
1816 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1817 int ret;
1818
Vivien Didelotfad09c72016-06-21 12:28:20 -04001819 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001820 if (ret < 0)
1821 return ret;
1822
Vivien Didelotfad09c72016-06-21 12:28:20 -04001823 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001824 sid & GLOBAL_VTU_SID_MASK);
1825 if (ret < 0)
1826 return ret;
1827
Vivien Didelotfad09c72016-06-21 12:28:20 -04001828 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001829 if (ret < 0)
1830 return ret;
1831
Vivien Didelotfad09c72016-06-21 12:28:20 -04001832 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001833 if (ret < 0)
1834 return ret;
1835
1836 next.sid = ret & GLOBAL_VTU_SID_MASK;
1837
Vivien Didelotfad09c72016-06-21 12:28:20 -04001838 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001839 if (ret < 0)
1840 return ret;
1841
1842 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1843
1844 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001845 ret = mv88e6xxx_stu_data_read(chip, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001846 if (ret < 0)
1847 return ret;
1848 }
1849
1850 *entry = next;
1851 return 0;
1852}
1853
Vivien Didelotfad09c72016-06-21 12:28:20 -04001854static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001855 struct mv88e6xxx_vtu_stu_entry *entry)
1856{
1857 u16 reg = 0;
1858 int ret;
1859
Vivien Didelotfad09c72016-06-21 12:28:20 -04001860 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001861 if (ret < 0)
1862 return ret;
1863
1864 if (!entry->valid)
1865 goto loadpurge;
1866
1867 /* Write port states */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001868 ret = mv88e6xxx_stu_data_write(chip, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001869 if (ret < 0)
1870 return ret;
1871
1872 reg = GLOBAL_VTU_VID_VALID;
1873loadpurge:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001874 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001875 if (ret < 0)
1876 return ret;
1877
1878 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001879 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001880 if (ret < 0)
1881 return ret;
1882
Vivien Didelotfad09c72016-06-21 12:28:20 -04001883 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001884}
1885
Vivien Didelotfad09c72016-06-21 12:28:20 -04001886static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001887 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001888{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001889 struct dsa_switch *ds = chip->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001890 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001891 u16 fid;
1892 int ret;
1893
Vivien Didelotfad09c72016-06-21 12:28:20 -04001894 if (mv88e6xxx_num_databases(chip) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001895 upper_mask = 0xff;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001896 else if (mv88e6xxx_num_databases(chip) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001897 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001898 else
1899 return -EOPNOTSUPP;
1900
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001901 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001902 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001903 if (ret < 0)
1904 return ret;
1905
1906 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1907
1908 if (new) {
1909 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1910 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1911
Vivien Didelotfad09c72016-06-21 12:28:20 -04001912 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001913 ret);
1914 if (ret < 0)
1915 return ret;
1916 }
1917
1918 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001919 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001920 if (ret < 0)
1921 return ret;
1922
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001923 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001924
1925 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001926 ret &= ~upper_mask;
1927 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001928
Vivien Didelotfad09c72016-06-21 12:28:20 -04001929 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001930 ret);
1931 if (ret < 0)
1932 return ret;
1933
Andrew Lunnc8b09802016-06-04 21:16:57 +02001934 netdev_dbg(ds->ports[port].netdev,
1935 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001936 }
1937
1938 if (old)
1939 *old = fid;
1940
1941 return 0;
1942}
1943
Vivien Didelotfad09c72016-06-21 12:28:20 -04001944static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001945 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001946{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001947 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001948}
1949
Vivien Didelotfad09c72016-06-21 12:28:20 -04001950static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001951 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001952{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001953 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001954}
1955
Vivien Didelotfad09c72016-06-21 12:28:20 -04001956static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001957{
1958 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1959 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001960 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001961
1962 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1963
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001964 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001965 for (i = 0; i < chip->info->num_ports; ++i) {
1966 err = _mv88e6xxx_port_fid_get(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001967 if (err)
1968 return err;
1969
1970 set_bit(*fid, fid_bitmap);
1971 }
1972
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001973 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001974 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001975 if (err)
1976 return err;
1977
1978 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001979 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001980 if (err)
1981 return err;
1982
1983 if (!vlan.valid)
1984 break;
1985
1986 set_bit(vlan.fid, fid_bitmap);
1987 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1988
1989 /* The reset value 0x000 is used to indicate that multiple address
1990 * databases are not needed. Return the next positive available.
1991 */
1992 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001993 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001994 return -ENOSPC;
1995
1996 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001997 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001998}
1999
Vivien Didelotfad09c72016-06-21 12:28:20 -04002000static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002001 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002002{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002003 struct dsa_switch *ds = chip->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002004 struct mv88e6xxx_vtu_stu_entry vlan = {
2005 .valid = true,
2006 .vid = vid,
2007 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002008 int i, err;
2009
Vivien Didelotfad09c72016-06-21 12:28:20 -04002010 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002011 if (err)
2012 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002013
Vivien Didelot3d131f02015-11-03 10:52:52 -05002014 /* exclude all ports except the CPU and DSA ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002015 for (i = 0; i < chip->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05002016 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
2017 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
2018 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002019
Vivien Didelotfad09c72016-06-21 12:28:20 -04002020 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
2021 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002022 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002023
2024 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
2025 * implemented, only one STU entry is needed to cover all VTU
2026 * entries. Thus, validate the SID 0.
2027 */
2028 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002029 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002030 if (err)
2031 return err;
2032
2033 if (vstp.sid != vlan.sid || !vstp.valid) {
2034 memset(&vstp, 0, sizeof(vstp));
2035 vstp.valid = true;
2036 vstp.sid = vlan.sid;
2037
Vivien Didelotfad09c72016-06-21 12:28:20 -04002038 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002039 if (err)
2040 return err;
2041 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002042 }
2043
2044 *entry = vlan;
2045 return 0;
2046}
2047
Vivien Didelotfad09c72016-06-21 12:28:20 -04002048static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002049 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
2050{
2051 int err;
2052
2053 if (!vid)
2054 return -EINVAL;
2055
Vivien Didelotfad09c72016-06-21 12:28:20 -04002056 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002057 if (err)
2058 return err;
2059
Vivien Didelotfad09c72016-06-21 12:28:20 -04002060 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002061 if (err)
2062 return err;
2063
2064 if (entry->vid != vid || !entry->valid) {
2065 if (!creat)
2066 return -EOPNOTSUPP;
2067 /* -ENOENT would've been more appropriate, but switchdev expects
2068 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
2069 */
2070
Vivien Didelotfad09c72016-06-21 12:28:20 -04002071 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002072 }
2073
2074 return err;
2075}
2076
Vivien Didelotda9c3592016-02-12 12:09:40 -05002077static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
2078 u16 vid_begin, u16 vid_end)
2079{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002080 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002081 struct mv88e6xxx_vtu_stu_entry vlan;
2082 int i, err;
2083
2084 if (!vid_begin)
2085 return -EOPNOTSUPP;
2086
Vivien Didelotfad09c72016-06-21 12:28:20 -04002087 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002088
Vivien Didelotfad09c72016-06-21 12:28:20 -04002089 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002090 if (err)
2091 goto unlock;
2092
2093 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002094 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002095 if (err)
2096 goto unlock;
2097
2098 if (!vlan.valid)
2099 break;
2100
2101 if (vlan.vid > vid_end)
2102 break;
2103
Vivien Didelotfad09c72016-06-21 12:28:20 -04002104 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05002105 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
2106 continue;
2107
2108 if (vlan.data[i] ==
2109 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2110 continue;
2111
Vivien Didelotfad09c72016-06-21 12:28:20 -04002112 if (chip->ports[i].bridge_dev ==
2113 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05002114 break; /* same bridge, check next VLAN */
2115
Andrew Lunnc8b09802016-06-04 21:16:57 +02002116 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05002117 "hardware VLAN %d already used by %s\n",
2118 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04002119 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05002120 err = -EOPNOTSUPP;
2121 goto unlock;
2122 }
2123 } while (vlan.vid < vid_end);
2124
2125unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002126 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002127
2128 return err;
2129}
2130
Vivien Didelot214cdb92016-02-26 13:16:08 -05002131static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2132 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2133 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2134 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2135 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2136};
2137
Vivien Didelotf81ec902016-05-09 13:22:58 -04002138static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2139 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05002140{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002141 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002142 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2143 PORT_CONTROL_2_8021Q_DISABLED;
2144 int ret;
2145
Vivien Didelotfad09c72016-06-21 12:28:20 -04002146 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002147 return -EOPNOTSUPP;
2148
Vivien Didelotfad09c72016-06-21 12:28:20 -04002149 mutex_lock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002150
Vivien Didelotfad09c72016-06-21 12:28:20 -04002151 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002152 if (ret < 0)
2153 goto unlock;
2154
2155 old = ret & PORT_CONTROL_2_8021Q_MASK;
2156
Vivien Didelot5220ef12016-03-07 18:24:52 -05002157 if (new != old) {
2158 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2159 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002160
Vivien Didelotfad09c72016-06-21 12:28:20 -04002161 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05002162 ret);
2163 if (ret < 0)
2164 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002165
Andrew Lunnc8b09802016-06-04 21:16:57 +02002166 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05002167 mv88e6xxx_port_8021q_mode_names[new],
2168 mv88e6xxx_port_8021q_mode_names[old]);
2169 }
2170
2171 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002172unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002173 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002174
2175 return ret;
2176}
2177
Vivien Didelot57d32312016-06-20 13:13:58 -04002178static int
2179mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2180 const struct switchdev_obj_port_vlan *vlan,
2181 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002182{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002183 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002184 int err;
2185
Vivien Didelotfad09c72016-06-21 12:28:20 -04002186 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002187 return -EOPNOTSUPP;
2188
Vivien Didelotda9c3592016-02-12 12:09:40 -05002189 /* If the requested port doesn't belong to the same bridge as the VLAN
2190 * members, do not support it (yet) and fallback to software VLAN.
2191 */
2192 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2193 vlan->vid_end);
2194 if (err)
2195 return err;
2196
Vivien Didelot76e398a2015-11-01 12:33:55 -05002197 /* We don't need any dynamic resource from the kernel (yet),
2198 * so skip the prepare phase.
2199 */
2200 return 0;
2201}
2202
Vivien Didelotfad09c72016-06-21 12:28:20 -04002203static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04002204 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002205{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002206 struct mv88e6xxx_vtu_stu_entry vlan;
2207 int err;
2208
Vivien Didelotfad09c72016-06-21 12:28:20 -04002209 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002210 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002211 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002212
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002213 vlan.data[port] = untagged ?
2214 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2215 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2216
Vivien Didelotfad09c72016-06-21 12:28:20 -04002217 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002218}
2219
Vivien Didelotf81ec902016-05-09 13:22:58 -04002220static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2221 const struct switchdev_obj_port_vlan *vlan,
2222 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002223{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002224 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002225 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2226 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2227 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002228
Vivien Didelotfad09c72016-06-21 12:28:20 -04002229 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002230 return;
2231
Vivien Didelotfad09c72016-06-21 12:28:20 -04002232 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002233
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002234 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002235 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002236 netdev_err(ds->ports[port].netdev,
2237 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002238 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05002239
Vivien Didelotfad09c72016-06-21 12:28:20 -04002240 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002241 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002242 vlan->vid_end);
2243
Vivien Didelotfad09c72016-06-21 12:28:20 -04002244 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002245}
2246
Vivien Didelotfad09c72016-06-21 12:28:20 -04002247static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002248 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002249{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002250 struct dsa_switch *ds = chip->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002251 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002252 int i, err;
2253
Vivien Didelotfad09c72016-06-21 12:28:20 -04002254 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002255 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002256 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002257
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002258 /* Tell switchdev if this VLAN is handled in software */
2259 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05002260 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002261
2262 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2263
2264 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002265 vlan.valid = false;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002266 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05002267 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002268 continue;
2269
2270 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002271 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002272 break;
2273 }
2274 }
2275
Vivien Didelotfad09c72016-06-21 12:28:20 -04002276 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002277 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002278 return err;
2279
Vivien Didelotfad09c72016-06-21 12:28:20 -04002280 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002281}
2282
Vivien Didelotf81ec902016-05-09 13:22:58 -04002283static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2284 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002285{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002286 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002287 u16 pvid, vid;
2288 int err = 0;
2289
Vivien Didelotfad09c72016-06-21 12:28:20 -04002290 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002291 return -EOPNOTSUPP;
2292
Vivien Didelotfad09c72016-06-21 12:28:20 -04002293 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002294
Vivien Didelotfad09c72016-06-21 12:28:20 -04002295 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002296 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002297 goto unlock;
2298
Vivien Didelot76e398a2015-11-01 12:33:55 -05002299 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002300 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002301 if (err)
2302 goto unlock;
2303
2304 if (vid == pvid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002305 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002306 if (err)
2307 goto unlock;
2308 }
2309 }
2310
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002311unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002312 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002313
2314 return err;
2315}
2316
Vivien Didelotfad09c72016-06-21 12:28:20 -04002317static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002318 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002319{
2320 int i, ret;
2321
2322 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002323 ret = _mv88e6xxx_reg_write(
Vivien Didelotfad09c72016-06-21 12:28:20 -04002324 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002325 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002326 if (ret < 0)
2327 return ret;
2328 }
2329
2330 return 0;
2331}
2332
Vivien Didelotfad09c72016-06-21 12:28:20 -04002333static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002334 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002335{
2336 int i, ret;
2337
2338 for (i = 0; i < 3; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002339 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002340 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002341 if (ret < 0)
2342 return ret;
2343 addr[i * 2] = ret >> 8;
2344 addr[i * 2 + 1] = ret & 0xff;
2345 }
2346
2347 return 0;
2348}
2349
Vivien Didelotfad09c72016-06-21 12:28:20 -04002350static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002351 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002352{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002353 int ret;
2354
Vivien Didelotfad09c72016-06-21 12:28:20 -04002355 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002356 if (ret < 0)
2357 return ret;
2358
Vivien Didelotfad09c72016-06-21 12:28:20 -04002359 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002360 if (ret < 0)
2361 return ret;
2362
Vivien Didelotfad09c72016-06-21 12:28:20 -04002363 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002364 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002365 return ret;
2366
Vivien Didelotfad09c72016-06-21 12:28:20 -04002367 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002368}
David S. Millercdf09692015-08-11 12:00:37 -07002369
Vivien Didelotfad09c72016-06-21 12:28:20 -04002370static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_chip *chip, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002371 const unsigned char *addr, u16 vid,
2372 u8 state)
2373{
2374 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002375 struct mv88e6xxx_vtu_stu_entry vlan;
2376 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002377
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002378 /* Null VLAN ID corresponds to the port private database */
2379 if (vid == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002380 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002381 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002382 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002383 if (err)
2384 return err;
2385
2386 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002387 entry.state = state;
2388 ether_addr_copy(entry.mac, addr);
2389 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2390 entry.trunk = false;
2391 entry.portv_trunkid = BIT(port);
2392 }
2393
Vivien Didelotfad09c72016-06-21 12:28:20 -04002394 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002395}
2396
Vivien Didelotf81ec902016-05-09 13:22:58 -04002397static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2398 const struct switchdev_obj_port_fdb *fdb,
2399 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002400{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002401 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot2672f822016-05-09 13:22:48 -04002402
Vivien Didelotfad09c72016-06-21 12:28:20 -04002403 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_ATU))
Vivien Didelot2672f822016-05-09 13:22:48 -04002404 return -EOPNOTSUPP;
2405
Vivien Didelot146a3202015-10-08 11:35:12 -04002406 /* We don't need any dynamic resource from the kernel (yet),
2407 * so skip the prepare phase.
2408 */
2409 return 0;
2410}
2411
Vivien Didelotf81ec902016-05-09 13:22:58 -04002412static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2413 const struct switchdev_obj_port_fdb *fdb,
2414 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002415{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002416 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002417 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2418 GLOBAL_ATU_DATA_STATE_UC_STATIC;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002419 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002420
Vivien Didelotfad09c72016-06-21 12:28:20 -04002421 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_ATU))
Vivien Didelot2672f822016-05-09 13:22:48 -04002422 return;
2423
Vivien Didelotfad09c72016-06-21 12:28:20 -04002424 mutex_lock(&chip->reg_lock);
2425 if (_mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid, state))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002426 netdev_err(ds->ports[port].netdev,
2427 "failed to load MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002428 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002429}
2430
Vivien Didelotf81ec902016-05-09 13:22:58 -04002431static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2432 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002433{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002434 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
David S. Millercdf09692015-08-11 12:00:37 -07002435 int ret;
2436
Vivien Didelotfad09c72016-06-21 12:28:20 -04002437 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_ATU))
Vivien Didelot2672f822016-05-09 13:22:48 -04002438 return -EOPNOTSUPP;
2439
Vivien Didelotfad09c72016-06-21 12:28:20 -04002440 mutex_lock(&chip->reg_lock);
2441 ret = _mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002442 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002443 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002444
2445 return ret;
2446}
2447
Vivien Didelotfad09c72016-06-21 12:28:20 -04002448static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002449 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002450{
Vivien Didelot1d194042015-08-10 09:09:51 -04002451 struct mv88e6xxx_atu_entry next = { 0 };
2452 int ret;
2453
2454 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002455
Vivien Didelotfad09c72016-06-21 12:28:20 -04002456 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002457 if (ret < 0)
2458 return ret;
2459
Vivien Didelotfad09c72016-06-21 12:28:20 -04002460 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002461 if (ret < 0)
2462 return ret;
2463
Vivien Didelotfad09c72016-06-21 12:28:20 -04002464 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002465 if (ret < 0)
2466 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002467
Vivien Didelotfad09c72016-06-21 12:28:20 -04002468 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002469 if (ret < 0)
2470 return ret;
2471
2472 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2473 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2474 unsigned int mask, shift;
2475
2476 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2477 next.trunk = true;
2478 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2479 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2480 } else {
2481 next.trunk = false;
2482 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2483 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2484 }
2485
2486 next.portv_trunkid = (ret & mask) >> shift;
2487 }
2488
2489 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002490 return 0;
2491}
2492
Vivien Didelotfad09c72016-06-21 12:28:20 -04002493static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002494 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002495 struct switchdev_obj_port_fdb *fdb,
2496 int (*cb)(struct switchdev_obj *obj))
2497{
2498 struct mv88e6xxx_atu_entry addr = {
2499 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2500 };
2501 int err;
2502
Vivien Didelotfad09c72016-06-21 12:28:20 -04002503 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002504 if (err)
2505 return err;
2506
2507 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002508 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002509 if (err)
2510 break;
2511
2512 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2513 break;
2514
2515 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2516 bool is_static = addr.state ==
2517 (is_multicast_ether_addr(addr.mac) ?
2518 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2519 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2520
2521 fdb->vid = vid;
2522 ether_addr_copy(fdb->addr, addr.mac);
2523 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2524
2525 err = cb(&fdb->obj);
2526 if (err)
2527 break;
2528 }
2529 } while (!is_broadcast_ether_addr(addr.mac));
2530
2531 return err;
2532}
2533
Vivien Didelotf81ec902016-05-09 13:22:58 -04002534static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2535 struct switchdev_obj_port_fdb *fdb,
2536 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002537{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002538 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002539 struct mv88e6xxx_vtu_stu_entry vlan = {
2540 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2541 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002542 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002543 int err;
2544
Vivien Didelotfad09c72016-06-21 12:28:20 -04002545 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_ATU))
Vivien Didelot2672f822016-05-09 13:22:48 -04002546 return -EOPNOTSUPP;
2547
Vivien Didelotfad09c72016-06-21 12:28:20 -04002548 mutex_lock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002549
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002550 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002551 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002552 if (err)
2553 goto unlock;
2554
Vivien Didelotfad09c72016-06-21 12:28:20 -04002555 err = _mv88e6xxx_port_fdb_dump_one(chip, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002556 if (err)
2557 goto unlock;
2558
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002559 /* Dump VLANs' Filtering Information Databases */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002560 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002561 if (err)
2562 goto unlock;
2563
2564 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002565 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002566 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002567 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002568
2569 if (!vlan.valid)
2570 break;
2571
Vivien Didelotfad09c72016-06-21 12:28:20 -04002572 err = _mv88e6xxx_port_fdb_dump_one(chip, vlan.fid, vlan.vid,
2573 port, fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002574 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002575 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002576 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2577
2578unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002579 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002580
2581 return err;
2582}
2583
Vivien Didelotf81ec902016-05-09 13:22:58 -04002584static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2585 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002586{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002587 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002588 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002589
Vivien Didelotfad09c72016-06-21 12:28:20 -04002590 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VLANTABLE))
Vivien Didelot936f2342016-05-09 13:22:46 -04002591 return -EOPNOTSUPP;
2592
Vivien Didelotfad09c72016-06-21 12:28:20 -04002593 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002594
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002595 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002596 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002597
Vivien Didelotfad09c72016-06-21 12:28:20 -04002598 for (i = 0; i < chip->info->num_ports; ++i) {
2599 if (chip->ports[i].bridge_dev == bridge) {
2600 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002601 if (err)
2602 break;
2603 }
2604 }
2605
Vivien Didelotfad09c72016-06-21 12:28:20 -04002606 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002607
Vivien Didelot466dfa02016-02-26 13:16:05 -05002608 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002609}
2610
Vivien Didelotf81ec902016-05-09 13:22:58 -04002611static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002612{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002613 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2614 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002615 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002616
Vivien Didelotfad09c72016-06-21 12:28:20 -04002617 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VLANTABLE))
Vivien Didelot936f2342016-05-09 13:22:46 -04002618 return;
2619
Vivien Didelotfad09c72016-06-21 12:28:20 -04002620 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002621
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002622 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002623 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002624
Vivien Didelotfad09c72016-06-21 12:28:20 -04002625 for (i = 0; i < chip->info->num_ports; ++i)
2626 if (i == port || chip->ports[i].bridge_dev == bridge)
2627 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002628 netdev_warn(ds->ports[i].netdev,
2629 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002630
Vivien Didelotfad09c72016-06-21 12:28:20 -04002631 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002632}
2633
Vivien Didelotfad09c72016-06-21 12:28:20 -04002634static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002635 int port, int page, int reg, int val)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002636{
2637 int ret;
2638
Vivien Didelotfad09c72016-06-21 12:28:20 -04002639 ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002640 if (ret < 0)
2641 goto restore_page_0;
2642
Vivien Didelotfad09c72016-06-21 12:28:20 -04002643 ret = mv88e6xxx_mdio_write_indirect(chip, port, reg, val);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002644restore_page_0:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002645 mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002646
2647 return ret;
2648}
2649
Vivien Didelotfad09c72016-06-21 12:28:20 -04002650static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002651 int port, int page, int reg)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002652{
2653 int ret;
2654
Vivien Didelotfad09c72016-06-21 12:28:20 -04002655 ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002656 if (ret < 0)
2657 goto restore_page_0;
2658
Vivien Didelotfad09c72016-06-21 12:28:20 -04002659 ret = mv88e6xxx_mdio_read_indirect(chip, port, reg);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002660restore_page_0:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002661 mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002662
2663 return ret;
2664}
2665
Vivien Didelotfad09c72016-06-21 12:28:20 -04002666static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002667{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002668 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002669 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002670 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002671 unsigned long timeout;
2672 int ret;
2673 int i;
2674
2675 /* Set all ports to the disabled state. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002676 for (i = 0; i < chip->info->num_ports; i++) {
2677 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
Vivien Didelot552238b2016-05-09 13:22:49 -04002678 if (ret < 0)
2679 return ret;
2680
Vivien Didelotfad09c72016-06-21 12:28:20 -04002681 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
Vivien Didelot552238b2016-05-09 13:22:49 -04002682 ret & 0xfffc);
2683 if (ret)
2684 return ret;
2685 }
2686
2687 /* Wait for transmit queues to drain. */
2688 usleep_range(2000, 4000);
2689
2690 /* If there is a gpio connected to the reset pin, toggle it */
2691 if (gpiod) {
2692 gpiod_set_value_cansleep(gpiod, 1);
2693 usleep_range(10000, 20000);
2694 gpiod_set_value_cansleep(gpiod, 0);
2695 usleep_range(10000, 20000);
2696 }
2697
2698 /* Reset the switch. Keep the PPU active if requested. The PPU
2699 * needs to be active to support indirect phy register access
2700 * through global registers 0x18 and 0x19.
2701 */
2702 if (ppu_active)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002703 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002704 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002705 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
Vivien Didelot552238b2016-05-09 13:22:49 -04002706 if (ret)
2707 return ret;
2708
2709 /* Wait up to one second for reset to complete. */
2710 timeout = jiffies + 1 * HZ;
2711 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002712 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
Vivien Didelot552238b2016-05-09 13:22:49 -04002713 if (ret < 0)
2714 return ret;
2715
2716 if ((ret & is_reset) == is_reset)
2717 break;
2718 usleep_range(1000, 2000);
2719 }
2720 if (time_after(jiffies, timeout))
2721 ret = -ETIMEDOUT;
2722 else
2723 ret = 0;
2724
2725 return ret;
2726}
2727
Vivien Didelotfad09c72016-06-21 12:28:20 -04002728static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002729{
2730 int ret;
2731
Vivien Didelotfad09c72016-06-21 12:28:20 -04002732 ret = _mv88e6xxx_mdio_page_read(chip, REG_FIBER_SERDES,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002733 PAGE_FIBER_SERDES, MII_BMCR);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002734 if (ret < 0)
2735 return ret;
2736
2737 if (ret & BMCR_PDOWN) {
2738 ret &= ~BMCR_PDOWN;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002739 ret = _mv88e6xxx_mdio_page_write(chip, REG_FIBER_SERDES,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002740 PAGE_FIBER_SERDES, MII_BMCR,
2741 ret);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002742 }
2743
2744 return ret;
2745}
2746
Vivien Didelotfad09c72016-06-21 12:28:20 -04002747static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002748{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002749 struct dsa_switch *ds = chip->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002750 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002751 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002752
Vivien Didelotfad09c72016-06-21 12:28:20 -04002753 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2754 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2755 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2756 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002757 /* MAC Forcing register: don't force link, speed,
2758 * duplex or flow control state to any particular
2759 * values on physical ports, but force the CPU port
2760 * and all DSA ports to their maximum bandwidth and
2761 * full duplex.
2762 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002763 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002764 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002765 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002766 reg |= PORT_PCS_CTRL_FORCE_LINK |
2767 PORT_PCS_CTRL_LINK_UP |
2768 PORT_PCS_CTRL_DUPLEX_FULL |
2769 PORT_PCS_CTRL_FORCE_DUPLEX;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002770 if (mv88e6xxx_6065_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002771 reg |= PORT_PCS_CTRL_100;
2772 else
2773 reg |= PORT_PCS_CTRL_1000;
2774 } else {
2775 reg |= PORT_PCS_CTRL_UNFORCED;
2776 }
2777
Vivien Didelotfad09c72016-06-21 12:28:20 -04002778 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002779 PORT_PCS_CTRL, reg);
2780 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002781 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002782 }
2783
2784 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2785 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2786 * tunneling, determine priority by looking at 802.1p and IP
2787 * priority fields (IP prio has precedence), and set STP state
2788 * to Forwarding.
2789 *
2790 * If this is the CPU link, use DSA or EDSA tagging depending
2791 * on which tagging mode was configured.
2792 *
2793 * If this is a link to another switch, use DSA tagging mode.
2794 *
2795 * If this is the upstream port for this switch, enable
2796 * forwarding of unknown unicasts and multicasts.
2797 */
2798 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002799 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2800 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2801 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2802 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002803 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2804 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2805 PORT_CONTROL_STATE_FORWARDING;
2806 if (dsa_is_cpu_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002807 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002808 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002809 if (mv88e6xxx_6352_family(chip) ||
2810 mv88e6xxx_6351_family(chip) ||
2811 mv88e6xxx_6165_family(chip) ||
2812 mv88e6xxx_6097_family(chip) ||
2813 mv88e6xxx_6320_family(chip)) {
Andrew Lunn5377b802016-06-04 21:17:02 +02002814 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2815 PORT_CONTROL_FORWARD_UNKNOWN |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002816 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002817 }
2818
Vivien Didelotfad09c72016-06-21 12:28:20 -04002819 if (mv88e6xxx_6352_family(chip) ||
2820 mv88e6xxx_6351_family(chip) ||
2821 mv88e6xxx_6165_family(chip) ||
2822 mv88e6xxx_6097_family(chip) ||
2823 mv88e6xxx_6095_family(chip) ||
2824 mv88e6xxx_6065_family(chip) ||
2825 mv88e6xxx_6185_family(chip) ||
2826 mv88e6xxx_6320_family(chip)) {
Vivien Didelot57d32312016-06-20 13:13:58 -04002827 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002828 }
2829 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002830 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002831 if (mv88e6xxx_6095_family(chip) ||
2832 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002833 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002834 if (mv88e6xxx_6352_family(chip) ||
2835 mv88e6xxx_6351_family(chip) ||
2836 mv88e6xxx_6165_family(chip) ||
2837 mv88e6xxx_6097_family(chip) ||
2838 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002839 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002840 }
2841
Andrew Lunn54d792f2015-05-06 01:09:47 +02002842 if (port == dsa_upstream_port(ds))
2843 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2844 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2845 }
2846 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002847 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002848 PORT_CONTROL, reg);
2849 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002850 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002851 }
2852
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002853 /* If this port is connected to a SerDes, make sure the SerDes is not
2854 * powered down.
2855 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002856 if (mv88e6xxx_6352_family(chip)) {
2857 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002858 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002859 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002860 ret &= PORT_STATUS_CMODE_MASK;
2861 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2862 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2863 (ret == PORT_STATUS_CMODE_SGMII)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002864 ret = mv88e6xxx_power_on_serdes(chip);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002865 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002866 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002867 }
2868 }
2869
Vivien Didelot8efdda42015-08-13 12:52:23 -04002870 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002871 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002872 * untagged frames on this port, do a destination address lookup on all
2873 * received packets as usual, disable ARP mirroring and don't send a
2874 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002875 */
2876 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002877 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2878 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2879 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2880 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002881 reg = PORT_CONTROL_2_MAP_DA;
2882
Vivien Didelotfad09c72016-06-21 12:28:20 -04002883 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2884 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002885 reg |= PORT_CONTROL_2_JUMBO_10240;
2886
Vivien Didelotfad09c72016-06-21 12:28:20 -04002887 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002888 /* Set the upstream port this port should use */
2889 reg |= dsa_upstream_port(ds);
2890 /* enable forwarding of unknown multicast addresses to
2891 * the upstream port
2892 */
2893 if (port == dsa_upstream_port(ds))
2894 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2895 }
2896
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002897 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002898
Andrew Lunn54d792f2015-05-06 01:09:47 +02002899 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002900 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002901 PORT_CONTROL_2, reg);
2902 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002903 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002904 }
2905
2906 /* Port Association Vector: when learning source addresses
2907 * of packets, add the address to the address database using
2908 * a port bitmap that has only the bit for this port set and
2909 * the other bits clear.
2910 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002911 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002912 /* Disable learning for CPU port */
2913 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002914 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002915
Vivien Didelotfad09c72016-06-21 12:28:20 -04002916 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2917 reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002918 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002919 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002920
2921 /* Egress rate control 2: disable egress rate control. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002922 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002923 0x0000);
2924 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002925 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002926
Vivien Didelotfad09c72016-06-21 12:28:20 -04002927 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2928 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2929 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002930 /* Do not limit the period of time that this port can
2931 * be paused for by the remote end or the period of
2932 * time that this port can pause the remote end.
2933 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002934 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002935 PORT_PAUSE_CTRL, 0x0000);
2936 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002937 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002938
2939 /* Port ATU control: disable limiting the number of
2940 * address database entries that this port is allowed
2941 * to use.
2942 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002943 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002944 PORT_ATU_CONTROL, 0x0000);
2945 /* Priority Override: disable DA, SA and VTU priority
2946 * override.
2947 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002948 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002949 PORT_PRI_OVERRIDE, 0x0000);
2950 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002951 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002952
2953 /* Port Ethertype: use the Ethertype DSA Ethertype
2954 * value.
2955 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002956 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002957 PORT_ETH_TYPE, ETH_P_EDSA);
2958 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002959 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002960 /* Tag Remap: use an identity 802.1p prio -> switch
2961 * prio mapping.
2962 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002963 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002964 PORT_TAG_REGMAP_0123, 0x3210);
2965 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002966 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002967
2968 /* Tag Remap 2: use an identity 802.1p prio -> switch
2969 * prio mapping.
2970 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002971 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002972 PORT_TAG_REGMAP_4567, 0x7654);
2973 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002974 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002975 }
2976
Vivien Didelotfad09c72016-06-21 12:28:20 -04002977 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2978 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2979 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2980 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002981 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002982 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002983 PORT_RATE_CONTROL, 0x0001);
2984 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002985 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002986 }
2987
Guenter Roeck366f0a02015-03-26 18:36:30 -07002988 /* Port Control 1: disable trunking, disable sending
2989 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002990 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002991 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2992 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002993 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002994 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002995
Vivien Didelot207afda2016-04-14 14:42:09 -04002996 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002997 * database, and allow bidirectional communication between the
2998 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002999 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003000 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05003001 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003002 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05003003
Vivien Didelotfad09c72016-06-21 12:28:20 -04003004 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07003005 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003006 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07003007
3008 /* Default VLAN ID and priority: don't set a default VLAN
3009 * ID, and set the default packet priority to zero.
3010 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003011 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04003012 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003013 if (ret)
3014 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07003015
Andrew Lunndbde9e62015-05-06 01:09:48 +02003016 return 0;
3017}
3018
Vivien Didelotfad09c72016-06-21 12:28:20 -04003019static int mv88e6xxx_setup_global(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04003020{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003021 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04003022 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04003023 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04003024 int err;
3025 int i;
3026
Vivien Didelot119477b2016-05-09 13:22:51 -04003027 /* Enable the PHY Polling Unit if present, don't discard any packets,
3028 * and mask all interrupt sources.
3029 */
3030 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003031 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
3032 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04003033 reg |= GLOBAL_CONTROL_PPU_ENABLE;
3034
Vivien Didelotfad09c72016-06-21 12:28:20 -04003035 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04003036 if (err)
3037 return err;
3038
Vivien Didelotb0745e872016-05-09 13:22:53 -04003039 /* Configure the upstream port, and configure it as the port to which
3040 * ingress and egress and ARP monitor frames are to be sent.
3041 */
3042 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
3043 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
3044 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003045 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
3046 reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04003047 if (err)
3048 return err;
3049
Vivien Didelot50484ff2016-05-09 13:22:54 -04003050 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003051 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
Vivien Didelot50484ff2016-05-09 13:22:54 -04003052 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
3053 (ds->index & 0x1f));
3054 if (err)
3055 return err;
3056
Vivien Didelot08a01262016-05-09 13:22:50 -04003057 /* Set the default address aging time to 5 minutes, and
3058 * enable address learn messages to be sent to all message
3059 * ports.
3060 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003061 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot08a01262016-05-09 13:22:50 -04003062 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
3063 if (err)
3064 return err;
3065
3066 /* Configure the IP ToS mapping registers. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003067 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04003068 if (err)
3069 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003070 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04003071 if (err)
3072 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003073 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04003074 if (err)
3075 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003076 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04003077 if (err)
3078 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003079 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04003080 if (err)
3081 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003082 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04003083 if (err)
3084 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003085 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04003086 if (err)
3087 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003088 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04003089 if (err)
3090 return err;
3091
3092 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003093 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04003094 if (err)
3095 return err;
3096
3097 /* Send all frames with destination addresses matching
3098 * 01:80:c2:00:00:0x to the CPU port.
3099 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003100 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
3101 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04003102 if (err)
3103 return err;
3104
3105 /* Ignore removed tag data on doubly tagged packets, disable
3106 * flow control messages, force flow control priority to the
3107 * highest, and send all special multicast frames to the CPU
3108 * port at the highest priority.
3109 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003110 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
Vivien Didelot08a01262016-05-09 13:22:50 -04003111 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
3112 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
3113 if (err)
3114 return err;
3115
3116 /* Program the DSA routing table. */
3117 for (i = 0; i < 32; i++) {
3118 int nexthop = 0x1f;
3119
Andrew Lunn66472fc2016-06-04 21:17:00 +02003120 if (i != ds->index && i < DSA_MAX_SWITCHES)
3121 nexthop = ds->rtable[i] & 0x1f;
Vivien Didelot08a01262016-05-09 13:22:50 -04003122
3123 err = _mv88e6xxx_reg_write(
Vivien Didelotfad09c72016-06-21 12:28:20 -04003124 chip, REG_GLOBAL2,
Vivien Didelot08a01262016-05-09 13:22:50 -04003125 GLOBAL2_DEVICE_MAPPING,
3126 GLOBAL2_DEVICE_MAPPING_UPDATE |
3127 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop);
3128 if (err)
3129 return err;
3130 }
3131
3132 /* Clear all trunk masks. */
3133 for (i = 0; i < 8; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003134 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
3135 GLOBAL2_TRUNK_MASK,
Vivien Didelot08a01262016-05-09 13:22:50 -04003136 0x8000 |
3137 (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
Vivien Didelotfad09c72016-06-21 12:28:20 -04003138 ((1 << chip->info->num_ports) - 1));
Vivien Didelot08a01262016-05-09 13:22:50 -04003139 if (err)
3140 return err;
3141 }
3142
3143 /* Clear all trunk mappings. */
3144 for (i = 0; i < 16; i++) {
3145 err = _mv88e6xxx_reg_write(
Vivien Didelotfad09c72016-06-21 12:28:20 -04003146 chip, REG_GLOBAL2,
Vivien Didelot08a01262016-05-09 13:22:50 -04003147 GLOBAL2_TRUNK_MAPPING,
3148 GLOBAL2_TRUNK_MAPPING_UPDATE |
3149 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
3150 if (err)
3151 return err;
3152 }
3153
Vivien Didelotfad09c72016-06-21 12:28:20 -04003154 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
3155 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
3156 mv88e6xxx_6320_family(chip)) {
Vivien Didelot08a01262016-05-09 13:22:50 -04003157 /* Send all frames with destination addresses matching
3158 * 01:80:c2:00:00:2x to the CPU port.
3159 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003160 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
Vivien Didelot08a01262016-05-09 13:22:50 -04003161 GLOBAL2_MGMT_EN_2X, 0xffff);
3162 if (err)
3163 return err;
3164
3165 /* Initialise cross-chip port VLAN table to reset
3166 * defaults.
3167 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003168 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
Vivien Didelot08a01262016-05-09 13:22:50 -04003169 GLOBAL2_PVT_ADDR, 0x9000);
3170 if (err)
3171 return err;
3172
3173 /* Clear the priority override table. */
3174 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003175 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
Vivien Didelot08a01262016-05-09 13:22:50 -04003176 GLOBAL2_PRIO_OVERRIDE,
3177 0x8000 | (i << 8));
3178 if (err)
3179 return err;
3180 }
3181 }
3182
Vivien Didelotfad09c72016-06-21 12:28:20 -04003183 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
3184 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
3185 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
3186 mv88e6xxx_6320_family(chip)) {
Vivien Didelot08a01262016-05-09 13:22:50 -04003187 /* Disable ingress rate limiting by resetting all
3188 * ingress rate limit registers to their initial
3189 * state.
3190 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003191 for (i = 0; i < chip->info->num_ports; i++) {
3192 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
Vivien Didelot08a01262016-05-09 13:22:50 -04003193 GLOBAL2_INGRESS_OP,
3194 0x9000 | (i << 8));
3195 if (err)
3196 return err;
3197 }
3198 }
3199
3200 /* Clear the statistics counters for all ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003201 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Vivien Didelot08a01262016-05-09 13:22:50 -04003202 GLOBAL_STATS_OP_FLUSH_ALL);
3203 if (err)
3204 return err;
3205
3206 /* Wait for the flush to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003207 err = _mv88e6xxx_stats_wait(chip);
Vivien Didelot08a01262016-05-09 13:22:50 -04003208 if (err)
3209 return err;
3210
3211 /* Clear all ATU entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003212 err = _mv88e6xxx_atu_flush(chip, 0, true);
Vivien Didelot08a01262016-05-09 13:22:50 -04003213 if (err)
3214 return err;
3215
3216 /* Clear all the VTU and STU entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003217 err = _mv88e6xxx_vtu_stu_flush(chip);
Vivien Didelot08a01262016-05-09 13:22:50 -04003218 if (err < 0)
3219 return err;
3220
3221 return err;
3222}
3223
Vivien Didelotf81ec902016-05-09 13:22:58 -04003224static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003225{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003226 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot552238b2016-05-09 13:22:49 -04003227 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003228 int i;
3229
Vivien Didelotfad09c72016-06-21 12:28:20 -04003230 chip->ds = ds;
3231 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04003232
Vivien Didelotfad09c72016-06-21 12:28:20 -04003233 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
3234 mutex_init(&chip->eeprom_mutex);
Vivien Didelotd24645b2016-05-09 13:22:41 -04003235
Vivien Didelotfad09c72016-06-21 12:28:20 -04003236 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04003237
Vivien Didelotfad09c72016-06-21 12:28:20 -04003238 err = mv88e6xxx_switch_reset(chip);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003239 if (err)
3240 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003241
Vivien Didelotfad09c72016-06-21 12:28:20 -04003242 err = mv88e6xxx_setup_global(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003243 if (err)
3244 goto unlock;
3245
Vivien Didelotfad09c72016-06-21 12:28:20 -04003246 for (i = 0; i < chip->info->num_ports; i++) {
3247 err = mv88e6xxx_setup_port(chip, i);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003248 if (err)
3249 goto unlock;
3250 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003251
Vivien Didelot6b17e862015-08-13 12:52:18 -04003252unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003253 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02003254
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003255 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003256}
3257
Vivien Didelot57d32312016-06-20 13:13:58 -04003258static int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page,
3259 int reg)
Andrew Lunn491435852015-04-02 04:06:35 +02003260{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003261 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn491435852015-04-02 04:06:35 +02003262 int ret;
3263
Vivien Didelotfad09c72016-06-21 12:28:20 -04003264 mutex_lock(&chip->reg_lock);
3265 ret = _mv88e6xxx_mdio_page_read(chip, port, page, reg);
3266 mutex_unlock(&chip->reg_lock);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003267
Andrew Lunn491435852015-04-02 04:06:35 +02003268 return ret;
3269}
3270
Vivien Didelot57d32312016-06-20 13:13:58 -04003271static int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page,
3272 int reg, int val)
Andrew Lunn491435852015-04-02 04:06:35 +02003273{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003274 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn491435852015-04-02 04:06:35 +02003275 int ret;
3276
Vivien Didelotfad09c72016-06-21 12:28:20 -04003277 mutex_lock(&chip->reg_lock);
3278 ret = _mv88e6xxx_mdio_page_write(chip, port, page, reg, val);
3279 mutex_unlock(&chip->reg_lock);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003280
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003281 return ret;
3282}
3283
Vivien Didelotfad09c72016-06-21 12:28:20 -04003284static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_chip *chip, int port)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003285{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003286 if (port >= 0 && port < chip->info->num_ports)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003287 return port;
3288 return -EINVAL;
3289}
3290
Andrew Lunnb516d452016-06-04 21:17:06 +02003291static int mv88e6xxx_mdio_read(struct mii_bus *bus, int port, int regnum)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003292{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003293 struct mv88e6xxx_chip *chip = bus->priv;
3294 int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003295 int ret;
3296
3297 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003298 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003299
Vivien Didelotfad09c72016-06-21 12:28:20 -04003300 mutex_lock(&chip->reg_lock);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003301
Vivien Didelotfad09c72016-06-21 12:28:20 -04003302 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3303 ret = mv88e6xxx_mdio_read_ppu(chip, addr, regnum);
3304 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
3305 ret = mv88e6xxx_mdio_read_indirect(chip, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003306 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04003307 ret = mv88e6xxx_mdio_read_direct(chip, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003308
Vivien Didelotfad09c72016-06-21 12:28:20 -04003309 mutex_unlock(&chip->reg_lock);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003310 return ret;
3311}
3312
Andrew Lunnb516d452016-06-04 21:17:06 +02003313static int mv88e6xxx_mdio_write(struct mii_bus *bus, int port, int regnum,
Andrew Lunn03a4a542016-06-04 21:17:05 +02003314 u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003315{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003316 struct mv88e6xxx_chip *chip = bus->priv;
3317 int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003318 int ret;
3319
3320 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003321 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003322
Vivien Didelotfad09c72016-06-21 12:28:20 -04003323 mutex_lock(&chip->reg_lock);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003324
Vivien Didelotfad09c72016-06-21 12:28:20 -04003325 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3326 ret = mv88e6xxx_mdio_write_ppu(chip, addr, regnum, val);
3327 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
3328 ret = mv88e6xxx_mdio_write_indirect(chip, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003329 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04003330 ret = mv88e6xxx_mdio_write_direct(chip, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003331
Vivien Didelotfad09c72016-06-21 12:28:20 -04003332 mutex_unlock(&chip->reg_lock);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003333 return ret;
3334}
3335
Vivien Didelotfad09c72016-06-21 12:28:20 -04003336static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02003337 struct device_node *np)
3338{
3339 static int index;
3340 struct mii_bus *bus;
3341 int err;
3342
Vivien Didelotfad09c72016-06-21 12:28:20 -04003343 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3344 mv88e6xxx_ppu_state_init(chip);
Andrew Lunnb516d452016-06-04 21:17:06 +02003345
3346 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003347 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02003348
Vivien Didelotfad09c72016-06-21 12:28:20 -04003349 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02003350 if (!bus)
3351 return -ENOMEM;
3352
Vivien Didelotfad09c72016-06-21 12:28:20 -04003353 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02003354 if (np) {
3355 bus->name = np->full_name;
3356 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3357 } else {
3358 bus->name = "mv88e6xxx SMI";
3359 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3360 }
3361
3362 bus->read = mv88e6xxx_mdio_read;
3363 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003364 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02003365
Vivien Didelotfad09c72016-06-21 12:28:20 -04003366 if (chip->mdio_np)
3367 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003368 else
3369 err = mdiobus_register(bus);
3370 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003371 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02003372 goto out;
3373 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04003374 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003375
3376 return 0;
3377
3378out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003379 if (chip->mdio_np)
3380 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003381
3382 return err;
3383}
3384
Vivien Didelotfad09c72016-06-21 12:28:20 -04003385static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003386
3387{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003388 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003389
3390 mdiobus_unregister(bus);
3391
Vivien Didelotfad09c72016-06-21 12:28:20 -04003392 if (chip->mdio_np)
3393 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003394}
3395
Guenter Roeckc22995c2015-07-25 09:42:28 -07003396#ifdef CONFIG_NET_DSA_HWMON
3397
3398static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3399{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003400 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003401 int ret;
3402 int val;
3403
3404 *temp = 0;
3405
Vivien Didelotfad09c72016-06-21 12:28:20 -04003406 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003407
Vivien Didelotfad09c72016-06-21 12:28:20 -04003408 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003409 if (ret < 0)
3410 goto error;
3411
3412 /* Enable temperature sensor */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003413 ret = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003414 if (ret < 0)
3415 goto error;
3416
Vivien Didelotfad09c72016-06-21 12:28:20 -04003417 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003418 if (ret < 0)
3419 goto error;
3420
3421 /* Wait for temperature to stabilize */
3422 usleep_range(10000, 12000);
3423
Vivien Didelotfad09c72016-06-21 12:28:20 -04003424 val = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003425 if (val < 0) {
3426 ret = val;
3427 goto error;
3428 }
3429
3430 /* Disable temperature sensor */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003431 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003432 if (ret < 0)
3433 goto error;
3434
3435 *temp = ((val & 0x1f) - 5) * 5;
3436
3437error:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003438 mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x0);
3439 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003440 return ret;
3441}
3442
3443static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3444{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003445 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3446 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003447 int ret;
3448
3449 *temp = 0;
3450
Andrew Lunn03a4a542016-06-04 21:17:05 +02003451 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003452 if (ret < 0)
3453 return ret;
3454
3455 *temp = (ret & 0xff) - 25;
3456
3457 return 0;
3458}
3459
Vivien Didelotf81ec902016-05-09 13:22:58 -04003460static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003461{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003462 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -04003463
Vivien Didelotfad09c72016-06-21 12:28:20 -04003464 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003465 return -EOPNOTSUPP;
3466
Vivien Didelotfad09c72016-06-21 12:28:20 -04003467 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003468 return mv88e63xx_get_temp(ds, temp);
3469
3470 return mv88e61xx_get_temp(ds, temp);
3471}
3472
Vivien Didelotf81ec902016-05-09 13:22:58 -04003473static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003474{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003475 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3476 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003477 int ret;
3478
Vivien Didelotfad09c72016-06-21 12:28:20 -04003479 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003480 return -EOPNOTSUPP;
3481
3482 *temp = 0;
3483
Andrew Lunn03a4a542016-06-04 21:17:05 +02003484 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003485 if (ret < 0)
3486 return ret;
3487
3488 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3489
3490 return 0;
3491}
3492
Vivien Didelotf81ec902016-05-09 13:22:58 -04003493static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003494{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003495 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3496 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003497 int ret;
3498
Vivien Didelotfad09c72016-06-21 12:28:20 -04003499 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003500 return -EOPNOTSUPP;
3501
Andrew Lunn03a4a542016-06-04 21:17:05 +02003502 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003503 if (ret < 0)
3504 return ret;
3505 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003506 return mv88e6xxx_mdio_page_write(ds, phy, 6, 26,
3507 (ret & 0xe0ff) | (temp << 8));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003508}
3509
Vivien Didelotf81ec902016-05-09 13:22:58 -04003510static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003511{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003512 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3513 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003514 int ret;
3515
Vivien Didelotfad09c72016-06-21 12:28:20 -04003516 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003517 return -EOPNOTSUPP;
3518
3519 *alarm = false;
3520
Andrew Lunn03a4a542016-06-04 21:17:05 +02003521 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003522 if (ret < 0)
3523 return ret;
3524
3525 *alarm = !!(ret & 0x40);
3526
3527 return 0;
3528}
3529#endif /* CONFIG_NET_DSA_HWMON */
3530
Vivien Didelotf81ec902016-05-09 13:22:58 -04003531static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3532 [MV88E6085] = {
3533 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3534 .family = MV88E6XXX_FAMILY_6097,
3535 .name = "Marvell 88E6085",
3536 .num_databases = 4096,
3537 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003538 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003539 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3540 },
3541
3542 [MV88E6095] = {
3543 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3544 .family = MV88E6XXX_FAMILY_6095,
3545 .name = "Marvell 88E6095/88E6095F",
3546 .num_databases = 256,
3547 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003548 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003549 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3550 },
3551
3552 [MV88E6123] = {
3553 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3554 .family = MV88E6XXX_FAMILY_6165,
3555 .name = "Marvell 88E6123",
3556 .num_databases = 4096,
3557 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003558 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003559 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3560 },
3561
3562 [MV88E6131] = {
3563 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3564 .family = MV88E6XXX_FAMILY_6185,
3565 .name = "Marvell 88E6131",
3566 .num_databases = 256,
3567 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003568 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003569 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3570 },
3571
3572 [MV88E6161] = {
3573 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3574 .family = MV88E6XXX_FAMILY_6165,
3575 .name = "Marvell 88E6161",
3576 .num_databases = 4096,
3577 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003578 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003579 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3580 },
3581
3582 [MV88E6165] = {
3583 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3584 .family = MV88E6XXX_FAMILY_6165,
3585 .name = "Marvell 88E6165",
3586 .num_databases = 4096,
3587 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003588 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003589 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3590 },
3591
3592 [MV88E6171] = {
3593 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3594 .family = MV88E6XXX_FAMILY_6351,
3595 .name = "Marvell 88E6171",
3596 .num_databases = 4096,
3597 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003598 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003599 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3600 },
3601
3602 [MV88E6172] = {
3603 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3604 .family = MV88E6XXX_FAMILY_6352,
3605 .name = "Marvell 88E6172",
3606 .num_databases = 4096,
3607 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003608 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003609 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3610 },
3611
3612 [MV88E6175] = {
3613 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3614 .family = MV88E6XXX_FAMILY_6351,
3615 .name = "Marvell 88E6175",
3616 .num_databases = 4096,
3617 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003618 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003619 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3620 },
3621
3622 [MV88E6176] = {
3623 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3624 .family = MV88E6XXX_FAMILY_6352,
3625 .name = "Marvell 88E6176",
3626 .num_databases = 4096,
3627 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003628 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003629 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3630 },
3631
3632 [MV88E6185] = {
3633 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3634 .family = MV88E6XXX_FAMILY_6185,
3635 .name = "Marvell 88E6185",
3636 .num_databases = 256,
3637 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003638 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003639 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3640 },
3641
3642 [MV88E6240] = {
3643 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3644 .family = MV88E6XXX_FAMILY_6352,
3645 .name = "Marvell 88E6240",
3646 .num_databases = 4096,
3647 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003648 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003649 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3650 },
3651
3652 [MV88E6320] = {
3653 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3654 .family = MV88E6XXX_FAMILY_6320,
3655 .name = "Marvell 88E6320",
3656 .num_databases = 4096,
3657 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003658 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003659 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3660 },
3661
3662 [MV88E6321] = {
3663 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3664 .family = MV88E6XXX_FAMILY_6320,
3665 .name = "Marvell 88E6321",
3666 .num_databases = 4096,
3667 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003668 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003669 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3670 },
3671
3672 [MV88E6350] = {
3673 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3674 .family = MV88E6XXX_FAMILY_6351,
3675 .name = "Marvell 88E6350",
3676 .num_databases = 4096,
3677 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003678 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003679 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3680 },
3681
3682 [MV88E6351] = {
3683 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3684 .family = MV88E6XXX_FAMILY_6351,
3685 .name = "Marvell 88E6351",
3686 .num_databases = 4096,
3687 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003688 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003689 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3690 },
3691
3692 [MV88E6352] = {
3693 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3694 .family = MV88E6XXX_FAMILY_6352,
3695 .name = "Marvell 88E6352",
3696 .num_databases = 4096,
3697 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003698 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003699 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3700 },
3701};
3702
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003703static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003704{
Vivien Didelota439c062016-04-17 13:23:58 -04003705 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003706
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003707 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3708 if (mv88e6xxx_table[i].prod_num == prod_num)
3709 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003710
Vivien Didelotb9b37712015-10-30 19:39:48 -04003711 return NULL;
3712}
3713
Vivien Didelotfad09c72016-06-21 12:28:20 -04003714static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003715{
3716 const struct mv88e6xxx_info *info;
3717 int id, prod_num, rev;
3718
Vivien Didelotfad09c72016-06-21 12:28:20 -04003719 id = mv88e6xxx_reg_read(chip, chip->info->port_base_addr,
3720 PORT_SWITCH_ID);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003721 if (id < 0)
3722 return id;
3723
3724 prod_num = (id & 0xfff0) >> 4;
3725 rev = id & 0x000f;
3726
3727 info = mv88e6xxx_lookup_info(prod_num);
3728 if (!info)
3729 return -ENODEV;
3730
Vivien Didelotcaac8542016-06-20 13:14:09 -04003731 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003732 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003733
Vivien Didelotfad09c72016-06-21 12:28:20 -04003734 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3735 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003736
3737 return 0;
3738}
3739
Vivien Didelotfad09c72016-06-21 12:28:20 -04003740static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003741{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003742 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003743
Vivien Didelotfad09c72016-06-21 12:28:20 -04003744 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3745 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003746 return NULL;
3747
Vivien Didelotfad09c72016-06-21 12:28:20 -04003748 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003749
Vivien Didelotfad09c72016-06-21 12:28:20 -04003750 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003751
Vivien Didelotfad09c72016-06-21 12:28:20 -04003752 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003753}
3754
Vivien Didelotfad09c72016-06-21 12:28:20 -04003755static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003756 struct mii_bus *bus, int sw_addr)
3757{
3758 /* ADDR[0] pin is unavailable externally and considered zero */
3759 if (sw_addr & 0x1)
3760 return -EINVAL;
3761
Vivien Didelot914b32f2016-06-20 13:14:11 -04003762 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003763 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
3764 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_MULTI_CHIP))
3765 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003766 else
3767 return -EINVAL;
3768
Vivien Didelotfad09c72016-06-21 12:28:20 -04003769 chip->bus = bus;
3770 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003771
3772 return 0;
3773}
3774
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003775static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3776 struct device *host_dev, int sw_addr,
3777 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003778{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003779 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003780 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003781 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003782
Vivien Didelota439c062016-04-17 13:23:58 -04003783 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003784 if (!bus)
3785 return NULL;
3786
Vivien Didelotfad09c72016-06-21 12:28:20 -04003787 chip = mv88e6xxx_alloc_chip(dsa_dev);
3788 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003789 return NULL;
3790
Vivien Didelotcaac8542016-06-20 13:14:09 -04003791 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003792 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003793
Vivien Didelotfad09c72016-06-21 12:28:20 -04003794 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003795 if (err)
3796 goto free;
3797
Vivien Didelotfad09c72016-06-21 12:28:20 -04003798 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003799 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003800 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003801
Vivien Didelotfad09c72016-06-21 12:28:20 -04003802 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003803 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003804 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003805
Vivien Didelotfad09c72016-06-21 12:28:20 -04003806 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003807
Vivien Didelotfad09c72016-06-21 12:28:20 -04003808 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003809free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003810 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003811
3812 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003813}
3814
Vivien Didelot57d32312016-06-20 13:13:58 -04003815static struct dsa_switch_driver mv88e6xxx_switch_driver = {
Vivien Didelotf81ec902016-05-09 13:22:58 -04003816 .tag_protocol = DSA_TAG_PROTO_EDSA,
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003817 .probe = mv88e6xxx_drv_probe,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003818 .setup = mv88e6xxx_setup,
3819 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003820 .adjust_link = mv88e6xxx_adjust_link,
3821 .get_strings = mv88e6xxx_get_strings,
3822 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3823 .get_sset_count = mv88e6xxx_get_sset_count,
3824 .set_eee = mv88e6xxx_set_eee,
3825 .get_eee = mv88e6xxx_get_eee,
3826#ifdef CONFIG_NET_DSA_HWMON
3827 .get_temp = mv88e6xxx_get_temp,
3828 .get_temp_limit = mv88e6xxx_get_temp_limit,
3829 .set_temp_limit = mv88e6xxx_set_temp_limit,
3830 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3831#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003832 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003833 .get_eeprom = mv88e6xxx_get_eeprom,
3834 .set_eeprom = mv88e6xxx_set_eeprom,
3835 .get_regs_len = mv88e6xxx_get_regs_len,
3836 .get_regs = mv88e6xxx_get_regs,
3837 .port_bridge_join = mv88e6xxx_port_bridge_join,
3838 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3839 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
3840 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3841 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3842 .port_vlan_add = mv88e6xxx_port_vlan_add,
3843 .port_vlan_del = mv88e6xxx_port_vlan_del,
3844 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3845 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3846 .port_fdb_add = mv88e6xxx_port_fdb_add,
3847 .port_fdb_del = mv88e6xxx_port_fdb_del,
3848 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
3849};
3850
Vivien Didelotfad09c72016-06-21 12:28:20 -04003851static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003852 struct device_node *np)
3853{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003854 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003855 struct dsa_switch *ds;
3856
3857 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3858 if (!ds)
3859 return -ENOMEM;
3860
3861 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003862 ds->priv = chip;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003863 ds->drv = &mv88e6xxx_switch_driver;
3864
3865 dev_set_drvdata(dev, ds);
3866
3867 return dsa_register_switch(ds, np);
3868}
3869
Vivien Didelotfad09c72016-06-21 12:28:20 -04003870static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003871{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003872 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003873}
3874
Vivien Didelot57d32312016-06-20 13:13:58 -04003875static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003876{
3877 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003878 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003879 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003880 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003881 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003882 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003883
Vivien Didelotcaac8542016-06-20 13:14:09 -04003884 compat_info = of_device_get_match_data(dev);
3885 if (!compat_info)
3886 return -EINVAL;
3887
Vivien Didelotfad09c72016-06-21 12:28:20 -04003888 chip = mv88e6xxx_alloc_chip(dev);
3889 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003890 return -ENOMEM;
3891
Vivien Didelotfad09c72016-06-21 12:28:20 -04003892 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003893
Vivien Didelotfad09c72016-06-21 12:28:20 -04003894 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003895 if (err)
3896 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003897
Vivien Didelotfad09c72016-06-21 12:28:20 -04003898 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003899 if (err)
3900 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003901
Vivien Didelotfad09c72016-06-21 12:28:20 -04003902 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
3903 if (IS_ERR(chip->reset))
3904 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02003905
Vivien Didelotfad09c72016-06-21 12:28:20 -04003906 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM) &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003907 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003908 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003909
Vivien Didelotfad09c72016-06-21 12:28:20 -04003910 err = mv88e6xxx_mdio_register(chip, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003911 if (err)
3912 return err;
3913
Vivien Didelotfad09c72016-06-21 12:28:20 -04003914 err = mv88e6xxx_register_switch(chip, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003915 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003916 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003917 return err;
3918 }
3919
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003920 return 0;
3921}
3922
3923static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3924{
3925 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003926 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003927
Vivien Didelotfad09c72016-06-21 12:28:20 -04003928 mv88e6xxx_unregister_switch(chip);
3929 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003930}
3931
3932static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04003933 {
3934 .compatible = "marvell,mv88e6085",
3935 .data = &mv88e6xxx_table[MV88E6085],
3936 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003937 { /* sentinel */ },
3938};
3939
3940MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3941
3942static struct mdio_driver mv88e6xxx_driver = {
3943 .probe = mv88e6xxx_probe,
3944 .remove = mv88e6xxx_remove,
3945 .mdiodrv.driver = {
3946 .name = "mv88e6085",
3947 .of_match_table = mv88e6xxx_of_match,
3948 },
3949};
3950
Ben Hutchings98e67302011-11-25 14:36:19 +00003951static int __init mv88e6xxx_init(void)
3952{
Vivien Didelotf81ec902016-05-09 13:22:58 -04003953 register_switch_driver(&mv88e6xxx_switch_driver);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003954 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003955}
3956module_init(mv88e6xxx_init);
3957
3958static void __exit mv88e6xxx_cleanup(void)
3959{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003960 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelotf81ec902016-05-09 13:22:58 -04003961 unregister_switch_driver(&mv88e6xxx_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003962}
3963module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003964
3965MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3966MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3967MODULE_LICENSE("GPL");