blob: 62320fca6712204cdc1828eaf36273a63af224bb [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/mv88e6xxx.c - Marvell 88e6xxx switch chip support
3 * Copyright (c) 2008 Marvell Semiconductor
4 *
Vivien Didelotb8fee952015-08-13 12:52:19 -04005 * Copyright (c) 2015 CMC Electronics, Inc.
6 * Added support for VLAN Table Unit operations
7 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
Barry Grussling19b2f972013-01-08 16:05:54 +000014#include <linux/delay.h>
Guenter Roeckdefb05b2015-03-26 18:36:38 -070015#include <linux/etherdevice.h>
Andrew Lunndea87022015-08-31 15:56:47 +020016#include <linux/ethtool.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070017#include <linux/if_bridge.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000018#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000019#include <linux/list.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000020#include <linux/module.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000021#include <linux/netdevice.h>
Andrew Lunnc8c1b392015-11-20 03:56:24 +010022#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000023#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000024#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040025#include <net/switchdev.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000026#include "mv88e6xxx.h"
27
Vivien Didelot3996a4f2015-10-30 18:56:45 -040028static void assert_smi_lock(struct dsa_switch *ds)
29{
30 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
31
32 if (unlikely(!mutex_is_locked(&ps->smi_mutex))) {
33 dev_err(ds->master_dev, "SMI lock not held!\n");
34 dump_stack();
35 }
36}
37
Barry Grussling3675c8d2013-01-08 16:05:53 +000038/* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000039 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
40 * will be directly accessible on some {device address,register address}
41 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
42 * will only respond to SMI transactions to that specific address, and
43 * an indirect addressing mechanism needs to be used to access its
44 * registers.
45 */
46static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
47{
48 int ret;
49 int i;
50
51 for (i = 0; i < 16; i++) {
Neil Armstrong6e899e62015-10-22 10:37:53 +020052 ret = mdiobus_read_nested(bus, sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000053 if (ret < 0)
54 return ret;
55
Andrew Lunncca8b132015-04-02 04:06:39 +020056 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000057 return 0;
58 }
59
60 return -ETIMEDOUT;
61}
62
Vivien Didelotb9b37712015-10-30 19:39:48 -040063static int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr,
64 int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000065{
66 int ret;
67
68 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +020069 return mdiobus_read_nested(bus, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000070
Barry Grussling3675c8d2013-01-08 16:05:53 +000071 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000072 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
73 if (ret < 0)
74 return ret;
75
Barry Grussling3675c8d2013-01-08 16:05:53 +000076 /* Transmit the read command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +020077 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
78 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000079 if (ret < 0)
80 return ret;
81
Barry Grussling3675c8d2013-01-08 16:05:53 +000082 /* Wait for the read command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000083 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
84 if (ret < 0)
85 return ret;
86
Barry Grussling3675c8d2013-01-08 16:05:53 +000087 /* Read the data. */
Neil Armstrong6e899e62015-10-22 10:37:53 +020088 ret = mdiobus_read_nested(bus, sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000089 if (ret < 0)
90 return ret;
91
92 return ret & 0xffff;
93}
94
Guenter Roeck8d6d09e2015-03-26 18:36:31 -070095static int _mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000096{
Guenter Roeckb184e492014-10-17 12:30:58 -070097 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000098 int ret;
99
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400100 assert_smi_lock(ds);
101
Guenter Roeckb184e492014-10-17 12:30:58 -0700102 if (bus == NULL)
103 return -EINVAL;
104
Guenter Roeckb184e492014-10-17 12:30:58 -0700105 ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg);
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500106 if (ret < 0)
107 return ret;
108
109 dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
110 addr, reg, ret);
111
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000112 return ret;
113}
114
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700115int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
116{
117 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
118 int ret;
119
120 mutex_lock(&ps->smi_mutex);
121 ret = _mv88e6xxx_reg_read(ds, addr, reg);
122 mutex_unlock(&ps->smi_mutex);
123
124 return ret;
125}
126
Vivien Didelotb9b37712015-10-30 19:39:48 -0400127static int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
128 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000129{
130 int ret;
131
132 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +0200133 return mdiobus_write_nested(bus, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000134
Barry Grussling3675c8d2013-01-08 16:05:53 +0000135 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000136 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
137 if (ret < 0)
138 return ret;
139
Barry Grussling3675c8d2013-01-08 16:05:53 +0000140 /* Transmit the data to write. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200141 ret = mdiobus_write_nested(bus, sw_addr, SMI_DATA, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000142 if (ret < 0)
143 return ret;
144
Barry Grussling3675c8d2013-01-08 16:05:53 +0000145 /* Transmit the write command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200146 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
147 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000148 if (ret < 0)
149 return ret;
150
Barry Grussling3675c8d2013-01-08 16:05:53 +0000151 /* Wait for the write command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000152 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
153 if (ret < 0)
154 return ret;
155
156 return 0;
157}
158
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700159static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg,
160 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000161{
Guenter Roeckb184e492014-10-17 12:30:58 -0700162 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000163
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400164 assert_smi_lock(ds);
165
Guenter Roeckb184e492014-10-17 12:30:58 -0700166 if (bus == NULL)
167 return -EINVAL;
168
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500169 dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
170 addr, reg, val);
171
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700172 return __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val);
173}
174
175int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
176{
177 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
178 int ret;
179
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000180 mutex_lock(&ps->smi_mutex);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700181 ret = _mv88e6xxx_reg_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000182 mutex_unlock(&ps->smi_mutex);
183
184 return ret;
185}
186
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000187int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
188{
Andrew Lunncca8b132015-04-02 04:06:39 +0200189 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
190 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
191 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000192
193 return 0;
194}
195
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000196int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
197{
198 int i;
199 int ret;
200
201 for (i = 0; i < 6; i++) {
202 int j;
203
Barry Grussling3675c8d2013-01-08 16:05:53 +0000204 /* Write the MAC address byte. */
Andrew Lunncca8b132015-04-02 04:06:39 +0200205 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
206 GLOBAL2_SWITCH_MAC_BUSY | (i << 8) | addr[i]);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000207
Barry Grussling3675c8d2013-01-08 16:05:53 +0000208 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000209 for (j = 0; j < 16; j++) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200210 ret = REG_READ(REG_GLOBAL2, GLOBAL2_SWITCH_MAC);
211 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000212 break;
213 }
214 if (j == 16)
215 return -ETIMEDOUT;
216 }
217
218 return 0;
219}
220
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200221static int _mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000222{
223 if (addr >= 0)
Andrew Lunn3898c142015-05-06 01:09:53 +0200224 return _mv88e6xxx_reg_read(ds, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000225 return 0xffff;
226}
227
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200228static int _mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum,
229 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000230{
231 if (addr >= 0)
Andrew Lunn3898c142015-05-06 01:09:53 +0200232 return _mv88e6xxx_reg_write(ds, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000233 return 0;
234}
235
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000236#ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
237static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
238{
239 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000240 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000241
Andrew Lunncca8b132015-04-02 04:06:39 +0200242 ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
243 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
244 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000245
Barry Grussling19b2f972013-01-08 16:05:54 +0000246 timeout = jiffies + 1 * HZ;
247 while (time_before(jiffies, timeout)) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200248 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
Barry Grussling19b2f972013-01-08 16:05:54 +0000249 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200250 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
251 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000252 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000253 }
254
255 return -ETIMEDOUT;
256}
257
258static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
259{
260 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000261 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000262
Andrew Lunncca8b132015-04-02 04:06:39 +0200263 ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
264 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, ret | GLOBAL_CONTROL_PPU_ENABLE);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000265
Barry Grussling19b2f972013-01-08 16:05:54 +0000266 timeout = jiffies + 1 * HZ;
267 while (time_before(jiffies, timeout)) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200268 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
Barry Grussling19b2f972013-01-08 16:05:54 +0000269 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200270 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
271 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000272 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000273 }
274
275 return -ETIMEDOUT;
276}
277
278static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
279{
280 struct mv88e6xxx_priv_state *ps;
281
282 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
283 if (mutex_trylock(&ps->ppu_mutex)) {
Barry Grussling85686582013-01-08 16:05:56 +0000284 struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000285
Barry Grussling85686582013-01-08 16:05:56 +0000286 if (mv88e6xxx_ppu_enable(ds) == 0)
287 ps->ppu_disabled = 0;
288 mutex_unlock(&ps->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000289 }
290}
291
292static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
293{
294 struct mv88e6xxx_priv_state *ps = (void *)_ps;
295
296 schedule_work(&ps->ppu_work);
297}
298
299static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
300{
Florian Fainellia22adce2014-04-28 11:14:28 -0700301 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000302 int ret;
303
304 mutex_lock(&ps->ppu_mutex);
305
Barry Grussling3675c8d2013-01-08 16:05:53 +0000306 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000307 * we can access the PHY registers. If it was already
308 * disabled, cancel the timer that is going to re-enable
309 * it.
310 */
311 if (!ps->ppu_disabled) {
Barry Grussling85686582013-01-08 16:05:56 +0000312 ret = mv88e6xxx_ppu_disable(ds);
313 if (ret < 0) {
314 mutex_unlock(&ps->ppu_mutex);
315 return ret;
316 }
317 ps->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000318 } else {
Barry Grussling85686582013-01-08 16:05:56 +0000319 del_timer(&ps->ppu_timer);
320 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000321 }
322
323 return ret;
324}
325
326static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
327{
Florian Fainellia22adce2014-04-28 11:14:28 -0700328 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000329
Barry Grussling3675c8d2013-01-08 16:05:53 +0000330 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000331 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
332 mutex_unlock(&ps->ppu_mutex);
333}
334
335void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
336{
Florian Fainellia22adce2014-04-28 11:14:28 -0700337 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000338
339 mutex_init(&ps->ppu_mutex);
340 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
341 init_timer(&ps->ppu_timer);
342 ps->ppu_timer.data = (unsigned long)ps;
343 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
344}
345
346int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
347{
348 int ret;
349
350 ret = mv88e6xxx_ppu_access_get(ds);
351 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000352 ret = mv88e6xxx_reg_read(ds, addr, regnum);
353 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000354 }
355
356 return ret;
357}
358
359int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
360 int regnum, u16 val)
361{
362 int ret;
363
364 ret = mv88e6xxx_ppu_access_get(ds);
365 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000366 ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
367 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000368 }
369
370 return ret;
371}
372#endif
373
Andrew Lunn54d792f2015-05-06 01:09:47 +0200374static bool mv88e6xxx_6065_family(struct dsa_switch *ds)
375{
376 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
377
378 switch (ps->id) {
379 case PORT_SWITCH_ID_6031:
380 case PORT_SWITCH_ID_6061:
381 case PORT_SWITCH_ID_6035:
382 case PORT_SWITCH_ID_6065:
383 return true;
384 }
385 return false;
386}
387
388static bool mv88e6xxx_6095_family(struct dsa_switch *ds)
389{
390 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
391
392 switch (ps->id) {
393 case PORT_SWITCH_ID_6092:
394 case PORT_SWITCH_ID_6095:
395 return true;
396 }
397 return false;
398}
399
400static bool mv88e6xxx_6097_family(struct dsa_switch *ds)
401{
402 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
403
404 switch (ps->id) {
405 case PORT_SWITCH_ID_6046:
406 case PORT_SWITCH_ID_6085:
407 case PORT_SWITCH_ID_6096:
408 case PORT_SWITCH_ID_6097:
409 return true;
410 }
411 return false;
412}
413
414static bool mv88e6xxx_6165_family(struct dsa_switch *ds)
415{
416 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
417
418 switch (ps->id) {
419 case PORT_SWITCH_ID_6123:
420 case PORT_SWITCH_ID_6161:
421 case PORT_SWITCH_ID_6165:
422 return true;
423 }
424 return false;
425}
426
427static bool mv88e6xxx_6185_family(struct dsa_switch *ds)
428{
429 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
430
431 switch (ps->id) {
432 case PORT_SWITCH_ID_6121:
433 case PORT_SWITCH_ID_6122:
434 case PORT_SWITCH_ID_6152:
435 case PORT_SWITCH_ID_6155:
436 case PORT_SWITCH_ID_6182:
437 case PORT_SWITCH_ID_6185:
438 case PORT_SWITCH_ID_6108:
439 case PORT_SWITCH_ID_6131:
440 return true;
441 }
442 return false;
443}
444
Guenter Roeckc22995c2015-07-25 09:42:28 -0700445static bool mv88e6xxx_6320_family(struct dsa_switch *ds)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700446{
447 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
448
449 switch (ps->id) {
450 case PORT_SWITCH_ID_6320:
451 case PORT_SWITCH_ID_6321:
452 return true;
453 }
454 return false;
455}
456
Andrew Lunn54d792f2015-05-06 01:09:47 +0200457static bool mv88e6xxx_6351_family(struct dsa_switch *ds)
458{
459 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
460
461 switch (ps->id) {
462 case PORT_SWITCH_ID_6171:
463 case PORT_SWITCH_ID_6175:
464 case PORT_SWITCH_ID_6350:
465 case PORT_SWITCH_ID_6351:
466 return true;
467 }
468 return false;
469}
470
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200471static bool mv88e6xxx_6352_family(struct dsa_switch *ds)
472{
473 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
474
475 switch (ps->id) {
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200476 case PORT_SWITCH_ID_6172:
477 case PORT_SWITCH_ID_6176:
Andrew Lunn54d792f2015-05-06 01:09:47 +0200478 case PORT_SWITCH_ID_6240:
479 case PORT_SWITCH_ID_6352:
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200480 return true;
481 }
482 return false;
483}
484
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400485static unsigned int mv88e6xxx_num_databases(struct dsa_switch *ds)
486{
487 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
488
489 /* The following devices have 4-bit identifiers for 16 databases */
490 if (ps->id == PORT_SWITCH_ID_6061)
491 return 16;
492
493 /* The following devices have 6-bit identifiers for 64 databases */
494 if (ps->id == PORT_SWITCH_ID_6065)
495 return 64;
496
497 /* The following devices have 8-bit identifiers for 256 databases */
498 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
499 return 256;
500
501 /* The following devices have 12-bit identifiers for 4096 databases */
502 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
503 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds))
504 return 4096;
505
506 return 0;
507}
508
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400509static bool mv88e6xxx_has_fid_reg(struct dsa_switch *ds)
510{
511 /* Does the device have dedicated FID registers for ATU and VTU ops? */
512 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
513 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds))
514 return true;
515
516 return false;
517}
518
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -0400519static bool mv88e6xxx_has_stu(struct dsa_switch *ds)
520{
521 /* Does the device have STU and dedicated SID registers for VTU ops? */
522 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
523 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds))
524 return true;
525
526 return false;
527}
528
Andrew Lunndea87022015-08-31 15:56:47 +0200529/* We expect the switch to perform auto negotiation if there is a real
530 * phy. However, in the case of a fixed link phy, we force the port
531 * settings from the fixed link settings.
532 */
533void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
534 struct phy_device *phydev)
535{
536 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200537 u32 reg;
538 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200539
540 if (!phy_is_pseudo_fixed_link(phydev))
541 return;
542
543 mutex_lock(&ps->smi_mutex);
544
545 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
546 if (ret < 0)
547 goto out;
548
549 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
550 PORT_PCS_CTRL_FORCE_LINK |
551 PORT_PCS_CTRL_DUPLEX_FULL |
552 PORT_PCS_CTRL_FORCE_DUPLEX |
553 PORT_PCS_CTRL_UNFORCED);
554
555 reg |= PORT_PCS_CTRL_FORCE_LINK;
556 if (phydev->link)
557 reg |= PORT_PCS_CTRL_LINK_UP;
558
559 if (mv88e6xxx_6065_family(ds) && phydev->speed > SPEED_100)
560 goto out;
561
562 switch (phydev->speed) {
563 case SPEED_1000:
564 reg |= PORT_PCS_CTRL_1000;
565 break;
566 case SPEED_100:
567 reg |= PORT_PCS_CTRL_100;
568 break;
569 case SPEED_10:
570 reg |= PORT_PCS_CTRL_10;
571 break;
572 default:
573 pr_info("Unknown speed");
574 goto out;
575 }
576
577 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
578 if (phydev->duplex == DUPLEX_FULL)
579 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
580
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200581 if ((mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds)) &&
582 (port >= ps->num_ports - 2)) {
583 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
584 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
585 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
586 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
587 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
588 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
589 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
590 }
Andrew Lunndea87022015-08-31 15:56:47 +0200591 _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_PCS_CTRL, reg);
592
593out:
594 mutex_unlock(&ps->smi_mutex);
595}
596
Andrew Lunn31888232015-05-06 01:09:54 +0200597static int _mv88e6xxx_stats_wait(struct dsa_switch *ds)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000598{
599 int ret;
600 int i;
601
602 for (i = 0; i < 10; i++) {
Andrew Lunn31888232015-05-06 01:09:54 +0200603 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200604 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000605 return 0;
606 }
607
608 return -ETIMEDOUT;
609}
610
Andrew Lunn31888232015-05-06 01:09:54 +0200611static int _mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000612{
613 int ret;
614
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700615 if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200616 port = (port + 1) << 5;
617
Barry Grussling3675c8d2013-01-08 16:05:53 +0000618 /* Snapshot the hardware statistics counters for this port. */
Andrew Lunn31888232015-05-06 01:09:54 +0200619 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
620 GLOBAL_STATS_OP_CAPTURE_PORT |
621 GLOBAL_STATS_OP_HIST_RX_TX | port);
622 if (ret < 0)
623 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000624
Barry Grussling3675c8d2013-01-08 16:05:53 +0000625 /* Wait for the snapshotting to complete. */
Andrew Lunn31888232015-05-06 01:09:54 +0200626 ret = _mv88e6xxx_stats_wait(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000627 if (ret < 0)
628 return ret;
629
630 return 0;
631}
632
Andrew Lunn31888232015-05-06 01:09:54 +0200633static void _mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000634{
635 u32 _val;
636 int ret;
637
638 *val = 0;
639
Andrew Lunn31888232015-05-06 01:09:54 +0200640 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
641 GLOBAL_STATS_OP_READ_CAPTURED |
642 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000643 if (ret < 0)
644 return;
645
Andrew Lunn31888232015-05-06 01:09:54 +0200646 ret = _mv88e6xxx_stats_wait(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000647 if (ret < 0)
648 return;
649
Andrew Lunn31888232015-05-06 01:09:54 +0200650 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000651 if (ret < 0)
652 return;
653
654 _val = ret << 16;
655
Andrew Lunn31888232015-05-06 01:09:54 +0200656 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000657 if (ret < 0)
658 return;
659
660 *val = _val | ret;
661}
662
Andrew Lunne413e7e2015-04-02 04:06:38 +0200663static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100664 { "in_good_octets", 8, 0x00, BANK0, },
665 { "in_bad_octets", 4, 0x02, BANK0, },
666 { "in_unicast", 4, 0x04, BANK0, },
667 { "in_broadcasts", 4, 0x06, BANK0, },
668 { "in_multicasts", 4, 0x07, BANK0, },
669 { "in_pause", 4, 0x16, BANK0, },
670 { "in_undersize", 4, 0x18, BANK0, },
671 { "in_fragments", 4, 0x19, BANK0, },
672 { "in_oversize", 4, 0x1a, BANK0, },
673 { "in_jabber", 4, 0x1b, BANK0, },
674 { "in_rx_error", 4, 0x1c, BANK0, },
675 { "in_fcs_error", 4, 0x1d, BANK0, },
676 { "out_octets", 8, 0x0e, BANK0, },
677 { "out_unicast", 4, 0x10, BANK0, },
678 { "out_broadcasts", 4, 0x13, BANK0, },
679 { "out_multicasts", 4, 0x12, BANK0, },
680 { "out_pause", 4, 0x15, BANK0, },
681 { "excessive", 4, 0x11, BANK0, },
682 { "collisions", 4, 0x1e, BANK0, },
683 { "deferred", 4, 0x05, BANK0, },
684 { "single", 4, 0x14, BANK0, },
685 { "multiple", 4, 0x17, BANK0, },
686 { "out_fcs_error", 4, 0x03, BANK0, },
687 { "late", 4, 0x1f, BANK0, },
688 { "hist_64bytes", 4, 0x08, BANK0, },
689 { "hist_65_127bytes", 4, 0x09, BANK0, },
690 { "hist_128_255bytes", 4, 0x0a, BANK0, },
691 { "hist_256_511bytes", 4, 0x0b, BANK0, },
692 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
693 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
694 { "sw_in_discards", 4, 0x10, PORT, },
695 { "sw_in_filtered", 2, 0x12, PORT, },
696 { "sw_out_filtered", 2, 0x13, PORT, },
697 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
698 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
699 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
700 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
701 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
702 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
703 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
704 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
705 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
706 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
707 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
708 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
709 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
710 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
711 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
712 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
713 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
714 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
715 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
716 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
717 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
718 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
719 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
720 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
721 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
722 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200723};
724
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100725static bool mv88e6xxx_has_stat(struct dsa_switch *ds,
726 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200727{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100728 switch (stat->type) {
729 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200730 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100731 case BANK1:
732 return mv88e6xxx_6320_family(ds);
733 case PORT:
734 return mv88e6xxx_6095_family(ds) ||
735 mv88e6xxx_6185_family(ds) ||
736 mv88e6xxx_6097_family(ds) ||
737 mv88e6xxx_6165_family(ds) ||
738 mv88e6xxx_6351_family(ds) ||
739 mv88e6xxx_6352_family(ds);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200740 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100741 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000742}
743
Andrew Lunn80c46272015-06-20 18:42:30 +0200744static uint64_t _mv88e6xxx_get_ethtool_stat(struct dsa_switch *ds,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100745 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200746 int port)
747{
Andrew Lunn80c46272015-06-20 18:42:30 +0200748 u32 low;
749 u32 high = 0;
750 int ret;
751 u64 value;
752
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100753 switch (s->type) {
754 case PORT:
755 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200756 if (ret < 0)
757 return UINT64_MAX;
758
759 low = ret;
760 if (s->sizeof_stat == 4) {
761 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100762 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200763 if (ret < 0)
764 return UINT64_MAX;
765 high = ret;
766 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100767 break;
768 case BANK0:
769 case BANK1:
Andrew Lunn80c46272015-06-20 18:42:30 +0200770 _mv88e6xxx_stats_read(ds, s->reg, &low);
771 if (s->sizeof_stat == 8)
772 _mv88e6xxx_stats_read(ds, s->reg + 1, &high);
773 }
774 value = (((u64)high) << 16) | low;
775 return value;
776}
777
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100778void mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
779{
780 struct mv88e6xxx_hw_stat *stat;
781 int i, j;
782
783 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
784 stat = &mv88e6xxx_hw_stats[i];
785 if (mv88e6xxx_has_stat(ds, stat)) {
786 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
787 ETH_GSTRING_LEN);
788 j++;
789 }
790 }
791}
792
793int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
794{
795 struct mv88e6xxx_hw_stat *stat;
796 int i, j;
797
798 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
799 stat = &mv88e6xxx_hw_stats[i];
800 if (mv88e6xxx_has_stat(ds, stat))
801 j++;
802 }
803 return j;
804}
805
806void
807mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
808 int port, uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000809{
Florian Fainellia22adce2014-04-28 11:14:28 -0700810 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100811 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000812 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100813 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000814
Andrew Lunn31888232015-05-06 01:09:54 +0200815 mutex_lock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000816
Andrew Lunn31888232015-05-06 01:09:54 +0200817 ret = _mv88e6xxx_stats_snapshot(ds, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000818 if (ret < 0) {
Andrew Lunn31888232015-05-06 01:09:54 +0200819 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000820 return;
821 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100822 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
823 stat = &mv88e6xxx_hw_stats[i];
824 if (mv88e6xxx_has_stat(ds, stat)) {
825 data[j] = _mv88e6xxx_get_ethtool_stat(ds, stat, port);
826 j++;
827 }
828 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000829
Andrew Lunn31888232015-05-06 01:09:54 +0200830 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000831}
Ben Hutchings98e67302011-11-25 14:36:19 +0000832
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700833int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
834{
835 return 32 * sizeof(u16);
836}
837
838void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
839 struct ethtool_regs *regs, void *_p)
840{
841 u16 *p = _p;
842 int i;
843
844 regs->version = 0;
845
846 memset(p, 0xff, 32 * sizeof(u16));
847
848 for (i = 0; i < 32; i++) {
849 int ret;
850
851 ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i);
852 if (ret >= 0)
853 p[i] = ret;
854 }
855}
856
Andrew Lunn3898c142015-05-06 01:09:53 +0200857static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset,
858 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700859{
860 unsigned long timeout = jiffies + HZ / 10;
861
862 while (time_before(jiffies, timeout)) {
863 int ret;
864
865 ret = _mv88e6xxx_reg_read(ds, reg, offset);
866 if (ret < 0)
867 return ret;
868 if (!(ret & mask))
869 return 0;
870
871 usleep_range(1000, 2000);
872 }
873 return -ETIMEDOUT;
874}
875
Andrew Lunn3898c142015-05-06 01:09:53 +0200876static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask)
877{
878 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
879 int ret;
880
881 mutex_lock(&ps->smi_mutex);
882 ret = _mv88e6xxx_wait(ds, reg, offset, mask);
883 mutex_unlock(&ps->smi_mutex);
884
885 return ret;
886}
887
888static int _mv88e6xxx_phy_wait(struct dsa_switch *ds)
889{
890 return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
891 GLOBAL2_SMI_OP_BUSY);
892}
893
894int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
895{
896 return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
897 GLOBAL2_EEPROM_OP_LOAD);
898}
899
900int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
901{
902 return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
903 GLOBAL2_EEPROM_OP_BUSY);
904}
905
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700906static int _mv88e6xxx_atu_wait(struct dsa_switch *ds)
907{
Andrew Lunncca8b132015-04-02 04:06:39 +0200908 return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP,
909 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700910}
911
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200912static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr,
913 int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +0100914{
915 int ret;
916
Andrew Lunn3898c142015-05-06 01:09:53 +0200917 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
918 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
919 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +0100920 if (ret < 0)
921 return ret;
922
Andrew Lunn3898c142015-05-06 01:09:53 +0200923 ret = _mv88e6xxx_phy_wait(ds);
924 if (ret < 0)
925 return ret;
926
927 return _mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA);
Andrew Lunnf3044682015-02-14 19:17:50 +0100928}
929
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200930static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr,
931 int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +0100932{
Andrew Lunn3898c142015-05-06 01:09:53 +0200933 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +0100934
Andrew Lunn3898c142015-05-06 01:09:53 +0200935 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
936 if (ret < 0)
937 return ret;
938
939 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
940 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
941 regnum);
942
943 return _mv88e6xxx_phy_wait(ds);
Andrew Lunnf3044682015-02-14 19:17:50 +0100944}
945
Guenter Roeck11b3b452015-03-06 22:23:51 -0800946int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
947{
Andrew Lunn2f40c692015-04-02 04:06:37 +0200948 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800949 int reg;
950
Andrew Lunn3898c142015-05-06 01:09:53 +0200951 mutex_lock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200952
953 reg = _mv88e6xxx_phy_read_indirect(ds, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800954 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200955 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800956
957 e->eee_enabled = !!(reg & 0x0200);
958 e->tx_lpi_enabled = !!(reg & 0x0100);
959
Andrew Lunn3898c142015-05-06 01:09:53 +0200960 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800961 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200962 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800963
Andrew Lunncca8b132015-04-02 04:06:39 +0200964 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200965 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800966
Andrew Lunn2f40c692015-04-02 04:06:37 +0200967out:
Andrew Lunn3898c142015-05-06 01:09:53 +0200968 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200969 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800970}
971
972int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
973 struct phy_device *phydev, struct ethtool_eee *e)
974{
Andrew Lunn2f40c692015-04-02 04:06:37 +0200975 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
976 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800977 int ret;
978
Andrew Lunn3898c142015-05-06 01:09:53 +0200979 mutex_lock(&ps->smi_mutex);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800980
Andrew Lunn2f40c692015-04-02 04:06:37 +0200981 ret = _mv88e6xxx_phy_read_indirect(ds, port, 16);
982 if (ret < 0)
983 goto out;
984
985 reg = ret & ~0x0300;
986 if (e->eee_enabled)
987 reg |= 0x0200;
988 if (e->tx_lpi_enabled)
989 reg |= 0x0100;
990
991 ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg);
992out:
Andrew Lunn3898c142015-05-06 01:09:53 +0200993 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200994
995 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800996}
997
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400998static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700999{
1000 int ret;
1001
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001002 if (mv88e6xxx_has_fid_reg(ds)) {
1003 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, fid);
1004 if (ret < 0)
1005 return ret;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001006 } else if (mv88e6xxx_num_databases(ds) == 256) {
1007 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
1008 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_CONTROL);
1009 if (ret < 0)
1010 return ret;
1011
1012 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_CONTROL,
1013 (ret & 0xfff) |
1014 ((fid << 8) & 0xf000));
1015 if (ret < 0)
1016 return ret;
1017
1018 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1019 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001020 }
1021
Andrew Lunncca8b132015-04-02 04:06:39 +02001022 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001023 if (ret < 0)
1024 return ret;
1025
1026 return _mv88e6xxx_atu_wait(ds);
1027}
1028
Vivien Didelot37705b72015-09-04 14:34:11 -04001029static int _mv88e6xxx_atu_data_write(struct dsa_switch *ds,
1030 struct mv88e6xxx_atu_entry *entry)
1031{
1032 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1033
1034 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1035 unsigned int mask, shift;
1036
1037 if (entry->trunk) {
1038 data |= GLOBAL_ATU_DATA_TRUNK;
1039 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1040 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1041 } else {
1042 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1043 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1044 }
1045
1046 data |= (entry->portv_trunkid << shift) & mask;
1047 }
1048
1049 return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA, data);
1050}
1051
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001052static int _mv88e6xxx_atu_flush_move(struct dsa_switch *ds,
1053 struct mv88e6xxx_atu_entry *entry,
1054 bool static_too)
1055{
1056 int op;
1057 int err;
1058
1059 err = _mv88e6xxx_atu_wait(ds);
1060 if (err)
1061 return err;
1062
1063 err = _mv88e6xxx_atu_data_write(ds, entry);
1064 if (err)
1065 return err;
1066
1067 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001068 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1069 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1070 } else {
1071 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1072 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1073 }
1074
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001075 return _mv88e6xxx_atu_cmd(ds, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001076}
1077
1078static int _mv88e6xxx_atu_flush(struct dsa_switch *ds, u16 fid, bool static_too)
1079{
1080 struct mv88e6xxx_atu_entry entry = {
1081 .fid = fid,
1082 .state = 0, /* EntryState bits must be 0 */
1083 };
1084
1085 return _mv88e6xxx_atu_flush_move(ds, &entry, static_too);
1086}
1087
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001088static int _mv88e6xxx_atu_move(struct dsa_switch *ds, u16 fid, int from_port,
1089 int to_port, bool static_too)
1090{
1091 struct mv88e6xxx_atu_entry entry = {
1092 .trunk = false,
1093 .fid = fid,
1094 };
1095
1096 /* EntryState bits must be 0xF */
1097 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1098
1099 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1100 entry.portv_trunkid = (to_port & 0x0f) << 4;
1101 entry.portv_trunkid |= from_port & 0x0f;
1102
1103 return _mv88e6xxx_atu_flush_move(ds, &entry, static_too);
1104}
1105
1106static int _mv88e6xxx_atu_remove(struct dsa_switch *ds, u16 fid, int port,
1107 bool static_too)
1108{
1109 /* Destination port 0xF means remove the entries */
1110 return _mv88e6xxx_atu_move(ds, fid, port, 0x0f, static_too);
1111}
1112
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001113static const char * const mv88e6xxx_port_state_names[] = {
1114 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1115 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1116 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1117 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1118};
1119
1120static int _mv88e6xxx_port_state(struct dsa_switch *ds, int port, u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001121{
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001122 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001123 u8 oldstate;
1124
Andrew Lunncca8b132015-04-02 04:06:39 +02001125 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001126 if (reg < 0)
1127 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001128
Andrew Lunncca8b132015-04-02 04:06:39 +02001129 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001130
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001131 if (oldstate != state) {
1132 /* Flush forwarding database if we're moving a port
1133 * from Learning or Forwarding state to Disabled or
1134 * Blocking or Listening state.
1135 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001136 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1137 oldstate == PORT_CONTROL_STATE_FORWARDING)
1138 && (state == PORT_CONTROL_STATE_DISABLED ||
1139 state == PORT_CONTROL_STATE_BLOCKING)) {
Vivien Didelot2b8157b2015-09-04 14:34:16 -04001140 ret = _mv88e6xxx_atu_remove(ds, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001141 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001142 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001143 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001144
Andrew Lunncca8b132015-04-02 04:06:39 +02001145 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1146 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL,
1147 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001148 if (ret)
1149 return ret;
1150
1151 netdev_dbg(ds->ports[port], "PortState %s (was %s)\n",
1152 mv88e6xxx_port_state_names[state],
1153 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001154 }
1155
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001156 return ret;
1157}
1158
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001159static int _mv88e6xxx_port_based_vlan_map(struct dsa_switch *ds, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001160{
1161 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001162 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelotede80982015-10-11 18:08:35 -04001163 const u16 mask = (1 << ps->num_ports) - 1;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001164 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001165 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001166 int i;
1167
1168 /* allow CPU port or DSA link(s) to send frames to every port */
1169 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1170 output_ports = mask;
1171 } else {
1172 for (i = 0; i < ps->num_ports; ++i) {
1173 /* allow sending frames to every group member */
1174 if (bridge && ps->ports[i].bridge_dev == bridge)
1175 output_ports |= BIT(i);
1176
1177 /* allow sending frames to CPU port and DSA link(s) */
1178 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1179 output_ports |= BIT(i);
1180 }
1181 }
1182
1183 /* prevent frames from going back out of the port they came in on */
1184 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001185
Vivien Didelotede80982015-10-11 18:08:35 -04001186 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN);
1187 if (reg < 0)
1188 return reg;
1189
1190 reg &= ~mask;
1191 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001192
Andrew Lunncca8b132015-04-02 04:06:39 +02001193 return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001194}
1195
Vivien Didelot43c44a92016-04-06 11:55:03 -04001196void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001197{
1198 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1199 int stp_state;
1200
1201 switch (state) {
1202 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001203 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001204 break;
1205 case BR_STATE_BLOCKING:
1206 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001207 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001208 break;
1209 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001210 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001211 break;
1212 case BR_STATE_FORWARDING:
1213 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001214 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001215 break;
1216 }
1217
Vivien Didelot43c44a92016-04-06 11:55:03 -04001218 /* mv88e6xxx_port_stp_state_set may be called with softirqs disabled,
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001219 * so we can not update the port state directly but need to schedule it.
1220 */
Vivien Didelotd715fa62016-02-12 12:09:38 -05001221 ps->ports[port].state = stp_state;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001222 set_bit(port, ps->port_state_update_mask);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001223 schedule_work(&ps->bridge_work);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001224}
1225
Vivien Didelot5da96032016-03-07 18:24:39 -05001226static int _mv88e6xxx_port_pvid(struct dsa_switch *ds, int port, u16 *new,
1227 u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001228{
Vivien Didelot5da96032016-03-07 18:24:39 -05001229 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001230 int ret;
1231
1232 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_DEFAULT_VLAN);
1233 if (ret < 0)
1234 return ret;
1235
Vivien Didelot5da96032016-03-07 18:24:39 -05001236 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1237
1238 if (new) {
1239 ret &= ~PORT_DEFAULT_VLAN_MASK;
1240 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1241
1242 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1243 PORT_DEFAULT_VLAN, ret);
1244 if (ret < 0)
1245 return ret;
1246
1247 netdev_dbg(ds->ports[port], "DefaultVID %d (was %d)\n", *new,
1248 pvid);
1249 }
1250
1251 if (old)
1252 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001253
1254 return 0;
1255}
1256
Vivien Didelot5da96032016-03-07 18:24:39 -05001257static int _mv88e6xxx_port_pvid_get(struct dsa_switch *ds, int port, u16 *pvid)
1258{
1259 return _mv88e6xxx_port_pvid(ds, port, NULL, pvid);
1260}
1261
Vivien Didelot76e398a2015-11-01 12:33:55 -05001262static int _mv88e6xxx_port_pvid_set(struct dsa_switch *ds, int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001263{
Vivien Didelot5da96032016-03-07 18:24:39 -05001264 return _mv88e6xxx_port_pvid(ds, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001265}
1266
Vivien Didelot6b17e862015-08-13 12:52:18 -04001267static int _mv88e6xxx_vtu_wait(struct dsa_switch *ds)
1268{
1269 return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_VTU_OP,
1270 GLOBAL_VTU_OP_BUSY);
1271}
1272
1273static int _mv88e6xxx_vtu_cmd(struct dsa_switch *ds, u16 op)
1274{
1275 int ret;
1276
1277 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_OP, op);
1278 if (ret < 0)
1279 return ret;
1280
1281 return _mv88e6xxx_vtu_wait(ds);
1282}
1283
1284static int _mv88e6xxx_vtu_stu_flush(struct dsa_switch *ds)
1285{
1286 int ret;
1287
1288 ret = _mv88e6xxx_vtu_wait(ds);
1289 if (ret < 0)
1290 return ret;
1291
1292 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_FLUSH_ALL);
1293}
1294
Vivien Didelotb8fee952015-08-13 12:52:19 -04001295static int _mv88e6xxx_vtu_stu_data_read(struct dsa_switch *ds,
1296 struct mv88e6xxx_vtu_stu_entry *entry,
1297 unsigned int nibble_offset)
1298{
1299 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1300 u16 regs[3];
1301 int i;
1302 int ret;
1303
1304 for (i = 0; i < 3; ++i) {
1305 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1306 GLOBAL_VTU_DATA_0_3 + i);
1307 if (ret < 0)
1308 return ret;
1309
1310 regs[i] = ret;
1311 }
1312
1313 for (i = 0; i < ps->num_ports; ++i) {
1314 unsigned int shift = (i % 4) * 4 + nibble_offset;
1315 u16 reg = regs[i / 4];
1316
1317 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1318 }
1319
1320 return 0;
1321}
1322
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001323static int _mv88e6xxx_vtu_stu_data_write(struct dsa_switch *ds,
1324 struct mv88e6xxx_vtu_stu_entry *entry,
1325 unsigned int nibble_offset)
1326{
1327 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1328 u16 regs[3] = { 0 };
1329 int i;
1330 int ret;
1331
1332 for (i = 0; i < ps->num_ports; ++i) {
1333 unsigned int shift = (i % 4) * 4 + nibble_offset;
1334 u8 data = entry->data[i];
1335
1336 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1337 }
1338
1339 for (i = 0; i < 3; ++i) {
1340 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL,
1341 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1342 if (ret < 0)
1343 return ret;
1344 }
1345
1346 return 0;
1347}
1348
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001349static int _mv88e6xxx_vtu_vid_write(struct dsa_switch *ds, u16 vid)
1350{
1351 return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID,
1352 vid & GLOBAL_VTU_VID_MASK);
1353}
1354
1355static int _mv88e6xxx_vtu_getnext(struct dsa_switch *ds,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001356 struct mv88e6xxx_vtu_stu_entry *entry)
1357{
1358 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1359 int ret;
1360
1361 ret = _mv88e6xxx_vtu_wait(ds);
1362 if (ret < 0)
1363 return ret;
1364
Vivien Didelotb8fee952015-08-13 12:52:19 -04001365 ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_GET_NEXT);
1366 if (ret < 0)
1367 return ret;
1368
1369 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1370 if (ret < 0)
1371 return ret;
1372
1373 next.vid = ret & GLOBAL_VTU_VID_MASK;
1374 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1375
1376 if (next.valid) {
1377 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 0);
1378 if (ret < 0)
1379 return ret;
1380
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001381 if (mv88e6xxx_has_fid_reg(ds)) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001382 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1383 GLOBAL_VTU_FID);
1384 if (ret < 0)
1385 return ret;
1386
1387 next.fid = ret & GLOBAL_VTU_FID_MASK;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001388 } else if (mv88e6xxx_num_databases(ds) == 256) {
1389 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1390 * VTU DBNum[3:0] are located in VTU Operation 3:0
1391 */
1392 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1393 GLOBAL_VTU_OP);
1394 if (ret < 0)
1395 return ret;
1396
1397 next.fid = (ret & 0xf00) >> 4;
1398 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001399 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001400
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001401 if (mv88e6xxx_has_stu(ds)) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001402 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1403 GLOBAL_VTU_SID);
1404 if (ret < 0)
1405 return ret;
1406
1407 next.sid = ret & GLOBAL_VTU_SID_MASK;
1408 }
1409 }
1410
1411 *entry = next;
1412 return 0;
1413}
1414
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001415int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1416 struct switchdev_obj_port_vlan *vlan,
1417 int (*cb)(struct switchdev_obj *obj))
1418{
1419 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1420 struct mv88e6xxx_vtu_stu_entry next;
1421 u16 pvid;
1422 int err;
1423
1424 mutex_lock(&ps->smi_mutex);
1425
1426 err = _mv88e6xxx_port_pvid_get(ds, port, &pvid);
1427 if (err)
1428 goto unlock;
1429
1430 err = _mv88e6xxx_vtu_vid_write(ds, GLOBAL_VTU_VID_MASK);
1431 if (err)
1432 goto unlock;
1433
1434 do {
1435 err = _mv88e6xxx_vtu_getnext(ds, &next);
1436 if (err)
1437 break;
1438
1439 if (!next.valid)
1440 break;
1441
1442 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1443 continue;
1444
1445 /* reinit and dump this VLAN obj */
1446 vlan->vid_begin = vlan->vid_end = next.vid;
1447 vlan->flags = 0;
1448
1449 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1450 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1451
1452 if (next.vid == pvid)
1453 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1454
1455 err = cb(&vlan->obj);
1456 if (err)
1457 break;
1458 } while (next.vid < GLOBAL_VTU_VID_MASK);
1459
1460unlock:
1461 mutex_unlock(&ps->smi_mutex);
1462
1463 return err;
1464}
1465
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001466static int _mv88e6xxx_vtu_loadpurge(struct dsa_switch *ds,
1467 struct mv88e6xxx_vtu_stu_entry *entry)
1468{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001469 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001470 u16 reg = 0;
1471 int ret;
1472
1473 ret = _mv88e6xxx_vtu_wait(ds);
1474 if (ret < 0)
1475 return ret;
1476
1477 if (!entry->valid)
1478 goto loadpurge;
1479
1480 /* Write port member tags */
1481 ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 0);
1482 if (ret < 0)
1483 return ret;
1484
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001485 if (mv88e6xxx_has_stu(ds)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001486 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1487 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1488 if (ret < 0)
1489 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001490 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001491
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001492 if (mv88e6xxx_has_fid_reg(ds)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001493 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1494 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_FID, reg);
1495 if (ret < 0)
1496 return ret;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001497 } else if (mv88e6xxx_num_databases(ds) == 256) {
1498 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1499 * VTU DBNum[3:0] are located in VTU Operation 3:0
1500 */
1501 op |= (entry->fid & 0xf0) << 8;
1502 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001503 }
1504
1505 reg = GLOBAL_VTU_VID_VALID;
1506loadpurge:
1507 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1508 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1509 if (ret < 0)
1510 return ret;
1511
Vivien Didelot11ea8092016-03-31 16:53:44 -04001512 return _mv88e6xxx_vtu_cmd(ds, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001513}
1514
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001515static int _mv88e6xxx_stu_getnext(struct dsa_switch *ds, u8 sid,
1516 struct mv88e6xxx_vtu_stu_entry *entry)
1517{
1518 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1519 int ret;
1520
1521 ret = _mv88e6xxx_vtu_wait(ds);
1522 if (ret < 0)
1523 return ret;
1524
1525 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID,
1526 sid & GLOBAL_VTU_SID_MASK);
1527 if (ret < 0)
1528 return ret;
1529
1530 ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_GET_NEXT);
1531 if (ret < 0)
1532 return ret;
1533
1534 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_SID);
1535 if (ret < 0)
1536 return ret;
1537
1538 next.sid = ret & GLOBAL_VTU_SID_MASK;
1539
1540 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1541 if (ret < 0)
1542 return ret;
1543
1544 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1545
1546 if (next.valid) {
1547 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 2);
1548 if (ret < 0)
1549 return ret;
1550 }
1551
1552 *entry = next;
1553 return 0;
1554}
1555
1556static int _mv88e6xxx_stu_loadpurge(struct dsa_switch *ds,
1557 struct mv88e6xxx_vtu_stu_entry *entry)
1558{
1559 u16 reg = 0;
1560 int ret;
1561
1562 ret = _mv88e6xxx_vtu_wait(ds);
1563 if (ret < 0)
1564 return ret;
1565
1566 if (!entry->valid)
1567 goto loadpurge;
1568
1569 /* Write port states */
1570 ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 2);
1571 if (ret < 0)
1572 return ret;
1573
1574 reg = GLOBAL_VTU_VID_VALID;
1575loadpurge:
1576 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1577 if (ret < 0)
1578 return ret;
1579
1580 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1581 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1582 if (ret < 0)
1583 return ret;
1584
1585 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1586}
1587
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001588static int _mv88e6xxx_port_fid(struct dsa_switch *ds, int port, u16 *new,
1589 u16 *old)
1590{
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001591 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001592 u16 fid;
1593 int ret;
1594
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001595 if (mv88e6xxx_num_databases(ds) == 4096)
1596 upper_mask = 0xff;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001597 else if (mv88e6xxx_num_databases(ds) == 256)
1598 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001599 else
1600 return -EOPNOTSUPP;
1601
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001602 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1603 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN);
1604 if (ret < 0)
1605 return ret;
1606
1607 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1608
1609 if (new) {
1610 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1611 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1612
1613 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN,
1614 ret);
1615 if (ret < 0)
1616 return ret;
1617 }
1618
1619 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1620 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL_1);
1621 if (ret < 0)
1622 return ret;
1623
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001624 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001625
1626 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001627 ret &= ~upper_mask;
1628 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001629
1630 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1,
1631 ret);
1632 if (ret < 0)
1633 return ret;
1634
1635 netdev_dbg(ds->ports[port], "FID %d (was %d)\n", *new, fid);
1636 }
1637
1638 if (old)
1639 *old = fid;
1640
1641 return 0;
1642}
1643
1644static int _mv88e6xxx_port_fid_get(struct dsa_switch *ds, int port, u16 *fid)
1645{
1646 return _mv88e6xxx_port_fid(ds, port, NULL, fid);
1647}
1648
1649static int _mv88e6xxx_port_fid_set(struct dsa_switch *ds, int port, u16 fid)
1650{
1651 return _mv88e6xxx_port_fid(ds, port, &fid, NULL);
1652}
1653
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001654static int _mv88e6xxx_fid_new(struct dsa_switch *ds, u16 *fid)
1655{
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001656 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001657 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1658 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001659 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001660
1661 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1662
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001663 /* Set every FID bit used by the (un)bridged ports */
1664 for (i = 0; i < ps->num_ports; ++i) {
1665 err = _mv88e6xxx_port_fid_get(ds, i, fid);
1666 if (err)
1667 return err;
1668
1669 set_bit(*fid, fid_bitmap);
1670 }
1671
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001672 /* Set every FID bit used by the VLAN entries */
1673 err = _mv88e6xxx_vtu_vid_write(ds, GLOBAL_VTU_VID_MASK);
1674 if (err)
1675 return err;
1676
1677 do {
1678 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
1679 if (err)
1680 return err;
1681
1682 if (!vlan.valid)
1683 break;
1684
1685 set_bit(vlan.fid, fid_bitmap);
1686 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1687
1688 /* The reset value 0x000 is used to indicate that multiple address
1689 * databases are not needed. Return the next positive available.
1690 */
1691 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001692 if (unlikely(*fid >= mv88e6xxx_num_databases(ds)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001693 return -ENOSPC;
1694
1695 /* Clear the database */
1696 return _mv88e6xxx_atu_flush(ds, *fid, true);
1697}
1698
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001699static int _mv88e6xxx_vtu_new(struct dsa_switch *ds, u16 vid,
1700 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001701{
1702 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1703 struct mv88e6xxx_vtu_stu_entry vlan = {
1704 .valid = true,
1705 .vid = vid,
1706 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001707 int i, err;
1708
1709 err = _mv88e6xxx_fid_new(ds, &vlan.fid);
1710 if (err)
1711 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001712
Vivien Didelot3d131f02015-11-03 10:52:52 -05001713 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001714 for (i = 0; i < ps->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001715 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1716 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1717 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001718
1719 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1720 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1721 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001722
1723 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1724 * implemented, only one STU entry is needed to cover all VTU
1725 * entries. Thus, validate the SID 0.
1726 */
1727 vlan.sid = 0;
1728 err = _mv88e6xxx_stu_getnext(ds, GLOBAL_VTU_SID_MASK, &vstp);
1729 if (err)
1730 return err;
1731
1732 if (vstp.sid != vlan.sid || !vstp.valid) {
1733 memset(&vstp, 0, sizeof(vstp));
1734 vstp.valid = true;
1735 vstp.sid = vlan.sid;
1736
1737 err = _mv88e6xxx_stu_loadpurge(ds, &vstp);
1738 if (err)
1739 return err;
1740 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001741 }
1742
1743 *entry = vlan;
1744 return 0;
1745}
1746
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001747static int _mv88e6xxx_vtu_get(struct dsa_switch *ds, u16 vid,
1748 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1749{
1750 int err;
1751
1752 if (!vid)
1753 return -EINVAL;
1754
1755 err = _mv88e6xxx_vtu_vid_write(ds, vid - 1);
1756 if (err)
1757 return err;
1758
1759 err = _mv88e6xxx_vtu_getnext(ds, entry);
1760 if (err)
1761 return err;
1762
1763 if (entry->vid != vid || !entry->valid) {
1764 if (!creat)
1765 return -EOPNOTSUPP;
1766 /* -ENOENT would've been more appropriate, but switchdev expects
1767 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1768 */
1769
1770 err = _mv88e6xxx_vtu_new(ds, vid, entry);
1771 }
1772
1773 return err;
1774}
1775
Vivien Didelotda9c3592016-02-12 12:09:40 -05001776static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1777 u16 vid_begin, u16 vid_end)
1778{
1779 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1780 struct mv88e6xxx_vtu_stu_entry vlan;
1781 int i, err;
1782
1783 if (!vid_begin)
1784 return -EOPNOTSUPP;
1785
1786 mutex_lock(&ps->smi_mutex);
1787
1788 err = _mv88e6xxx_vtu_vid_write(ds, vid_begin - 1);
1789 if (err)
1790 goto unlock;
1791
1792 do {
1793 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
1794 if (err)
1795 goto unlock;
1796
1797 if (!vlan.valid)
1798 break;
1799
1800 if (vlan.vid > vid_end)
1801 break;
1802
1803 for (i = 0; i < ps->num_ports; ++i) {
1804 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1805 continue;
1806
1807 if (vlan.data[i] ==
1808 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1809 continue;
1810
1811 if (ps->ports[i].bridge_dev ==
1812 ps->ports[port].bridge_dev)
1813 break; /* same bridge, check next VLAN */
1814
1815 netdev_warn(ds->ports[port],
1816 "hardware VLAN %d already used by %s\n",
1817 vlan.vid,
1818 netdev_name(ps->ports[i].bridge_dev));
1819 err = -EOPNOTSUPP;
1820 goto unlock;
1821 }
1822 } while (vlan.vid < vid_end);
1823
1824unlock:
1825 mutex_unlock(&ps->smi_mutex);
1826
1827 return err;
1828}
1829
Vivien Didelot214cdb92016-02-26 13:16:08 -05001830static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1831 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1832 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1833 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1834 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1835};
1836
1837int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1838 bool vlan_filtering)
1839{
1840 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1841 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1842 PORT_CONTROL_2_8021Q_DISABLED;
1843 int ret;
1844
1845 mutex_lock(&ps->smi_mutex);
1846
1847 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL_2);
1848 if (ret < 0)
1849 goto unlock;
1850
1851 old = ret & PORT_CONTROL_2_8021Q_MASK;
1852
Vivien Didelot5220ef12016-03-07 18:24:52 -05001853 if (new != old) {
1854 ret &= ~PORT_CONTROL_2_8021Q_MASK;
1855 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001856
Vivien Didelot5220ef12016-03-07 18:24:52 -05001857 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_2,
1858 ret);
1859 if (ret < 0)
1860 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001861
Vivien Didelot5220ef12016-03-07 18:24:52 -05001862 netdev_dbg(ds->ports[port], "802.1Q Mode %s (was %s)\n",
1863 mv88e6xxx_port_8021q_mode_names[new],
1864 mv88e6xxx_port_8021q_mode_names[old]);
1865 }
1866
1867 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001868unlock:
1869 mutex_unlock(&ps->smi_mutex);
1870
1871 return ret;
1872}
1873
Vivien Didelot76e398a2015-11-01 12:33:55 -05001874int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1875 const struct switchdev_obj_port_vlan *vlan,
1876 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001877{
Vivien Didelotda9c3592016-02-12 12:09:40 -05001878 int err;
1879
Vivien Didelotda9c3592016-02-12 12:09:40 -05001880 /* If the requested port doesn't belong to the same bridge as the VLAN
1881 * members, do not support it (yet) and fallback to software VLAN.
1882 */
1883 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1884 vlan->vid_end);
1885 if (err)
1886 return err;
1887
Vivien Didelot76e398a2015-11-01 12:33:55 -05001888 /* We don't need any dynamic resource from the kernel (yet),
1889 * so skip the prepare phase.
1890 */
1891 return 0;
1892}
1893
1894static int _mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port, u16 vid,
1895 bool untagged)
1896{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001897 struct mv88e6xxx_vtu_stu_entry vlan;
1898 int err;
1899
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001900 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001901 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001902 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001903
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001904 vlan.data[port] = untagged ?
1905 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1906 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1907
Vivien Didelot76e398a2015-11-01 12:33:55 -05001908 return _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1909}
1910
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001911void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1912 const struct switchdev_obj_port_vlan *vlan,
1913 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001914{
1915 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1916 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1917 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1918 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001919
1920 mutex_lock(&ps->smi_mutex);
1921
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001922 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
1923 if (_mv88e6xxx_port_vlan_add(ds, port, vid, untagged))
1924 netdev_err(ds->ports[port], "failed to add VLAN %d%c\n",
1925 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001926
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001927 if (pvid && _mv88e6xxx_port_pvid_set(ds, port, vlan->vid_end))
1928 netdev_err(ds->ports[port], "failed to set PVID %d\n",
1929 vlan->vid_end);
1930
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001931 mutex_unlock(&ps->smi_mutex);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001932}
1933
Vivien Didelot76e398a2015-11-01 12:33:55 -05001934static int _mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001935{
1936 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1937 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001938 int i, err;
1939
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001940 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001941 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001942 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001943
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001944 /* Tell switchdev if this VLAN is handled in software */
1945 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001946 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001947
1948 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1949
1950 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001951 vlan.valid = false;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001952 for (i = 0; i < ps->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001953 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001954 continue;
1955
1956 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001957 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001958 break;
1959 }
1960 }
1961
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001962 err = _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1963 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001964 return err;
1965
1966 return _mv88e6xxx_atu_remove(ds, vlan.fid, port, false);
1967}
1968
1969int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1970 const struct switchdev_obj_port_vlan *vlan)
1971{
1972 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1973 u16 pvid, vid;
1974 int err = 0;
1975
1976 mutex_lock(&ps->smi_mutex);
1977
1978 err = _mv88e6xxx_port_pvid_get(ds, port, &pvid);
1979 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001980 goto unlock;
1981
Vivien Didelot76e398a2015-11-01 12:33:55 -05001982 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1983 err = _mv88e6xxx_port_vlan_del(ds, port, vid);
1984 if (err)
1985 goto unlock;
1986
1987 if (vid == pvid) {
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05001988 err = _mv88e6xxx_port_pvid_set(ds, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001989 if (err)
1990 goto unlock;
1991 }
1992 }
1993
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001994unlock:
1995 mutex_unlock(&ps->smi_mutex);
1996
1997 return err;
1998}
1999
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002000static int _mv88e6xxx_atu_mac_write(struct dsa_switch *ds,
2001 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002002{
2003 int i, ret;
2004
2005 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002006 ret = _mv88e6xxx_reg_write(
2007 ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
2008 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002009 if (ret < 0)
2010 return ret;
2011 }
2012
2013 return 0;
2014}
2015
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002016static int _mv88e6xxx_atu_mac_read(struct dsa_switch *ds, unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002017{
2018 int i, ret;
2019
2020 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002021 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
2022 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002023 if (ret < 0)
2024 return ret;
2025 addr[i * 2] = ret >> 8;
2026 addr[i * 2 + 1] = ret & 0xff;
2027 }
2028
2029 return 0;
2030}
2031
Vivien Didelotfd231c82015-08-10 09:09:50 -04002032static int _mv88e6xxx_atu_load(struct dsa_switch *ds,
2033 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002034{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002035 int ret;
2036
2037 ret = _mv88e6xxx_atu_wait(ds);
2038 if (ret < 0)
2039 return ret;
2040
Vivien Didelotfd231c82015-08-10 09:09:50 -04002041 ret = _mv88e6xxx_atu_mac_write(ds, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002042 if (ret < 0)
2043 return ret;
2044
Vivien Didelot37705b72015-09-04 14:34:11 -04002045 ret = _mv88e6xxx_atu_data_write(ds, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002046 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002047 return ret;
2048
Vivien Didelotb426e5f2016-03-31 16:53:42 -04002049 return _mv88e6xxx_atu_cmd(ds, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002050}
David S. Millercdf09692015-08-11 12:00:37 -07002051
Vivien Didelotfd231c82015-08-10 09:09:50 -04002052static int _mv88e6xxx_port_fdb_load(struct dsa_switch *ds, int port,
2053 const unsigned char *addr, u16 vid,
2054 u8 state)
2055{
2056 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002057 struct mv88e6xxx_vtu_stu_entry vlan;
2058 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002059
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002060 /* Null VLAN ID corresponds to the port private database */
2061 if (vid == 0)
2062 err = _mv88e6xxx_port_fid_get(ds, port, &vlan.fid);
2063 else
2064 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002065 if (err)
2066 return err;
2067
2068 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002069 entry.state = state;
2070 ether_addr_copy(entry.mac, addr);
2071 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2072 entry.trunk = false;
2073 entry.portv_trunkid = BIT(port);
2074 }
2075
2076 return _mv88e6xxx_atu_load(ds, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002077}
2078
Vivien Didelot146a3202015-10-08 11:35:12 -04002079int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2080 const struct switchdev_obj_port_fdb *fdb,
2081 struct switchdev_trans *trans)
2082{
2083 /* We don't need any dynamic resource from the kernel (yet),
2084 * so skip the prepare phase.
2085 */
2086 return 0;
2087}
2088
Vivien Didelot8497aa62016-04-06 11:55:04 -04002089void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2090 const struct switchdev_obj_port_fdb *fdb,
2091 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002092{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002093 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002094 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2095 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2096 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002097
David S. Millercdf09692015-08-11 12:00:37 -07002098 mutex_lock(&ps->smi_mutex);
Vivien Didelot8497aa62016-04-06 11:55:04 -04002099 if (_mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid, state))
2100 netdev_err(ds->ports[port], "failed to load MAC address\n");
David S. Millercdf09692015-08-11 12:00:37 -07002101 mutex_unlock(&ps->smi_mutex);
David S. Millercdf09692015-08-11 12:00:37 -07002102}
2103
2104int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
Vivien Didelot8057b3e2015-10-08 11:35:14 -04002105 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002106{
2107 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2108 int ret;
2109
2110 mutex_lock(&ps->smi_mutex);
Vivien Didelot8057b3e2015-10-08 11:35:14 -04002111 ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002112 GLOBAL_ATU_DATA_STATE_UNUSED);
2113 mutex_unlock(&ps->smi_mutex);
2114
2115 return ret;
2116}
2117
Vivien Didelot1d194042015-08-10 09:09:51 -04002118static int _mv88e6xxx_atu_getnext(struct dsa_switch *ds, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002119 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002120{
Vivien Didelot1d194042015-08-10 09:09:51 -04002121 struct mv88e6xxx_atu_entry next = { 0 };
2122 int ret;
2123
2124 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002125
2126 ret = _mv88e6xxx_atu_wait(ds);
2127 if (ret < 0)
2128 return ret;
2129
Vivien Didelotb426e5f2016-03-31 16:53:42 -04002130 ret = _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002131 if (ret < 0)
2132 return ret;
2133
Vivien Didelot1d194042015-08-10 09:09:51 -04002134 ret = _mv88e6xxx_atu_mac_read(ds, next.mac);
2135 if (ret < 0)
2136 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002137
Vivien Didelot1d194042015-08-10 09:09:51 -04002138 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
2139 if (ret < 0)
2140 return ret;
2141
2142 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2143 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2144 unsigned int mask, shift;
2145
2146 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2147 next.trunk = true;
2148 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2149 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2150 } else {
2151 next.trunk = false;
2152 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2153 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2154 }
2155
2156 next.portv_trunkid = (ret & mask) >> shift;
2157 }
2158
2159 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002160 return 0;
2161}
2162
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002163static int _mv88e6xxx_port_fdb_dump_one(struct dsa_switch *ds, u16 fid, u16 vid,
2164 int port,
2165 struct switchdev_obj_port_fdb *fdb,
2166 int (*cb)(struct switchdev_obj *obj))
2167{
2168 struct mv88e6xxx_atu_entry addr = {
2169 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2170 };
2171 int err;
2172
2173 err = _mv88e6xxx_atu_mac_write(ds, addr.mac);
2174 if (err)
2175 return err;
2176
2177 do {
2178 err = _mv88e6xxx_atu_getnext(ds, fid, &addr);
2179 if (err)
2180 break;
2181
2182 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2183 break;
2184
2185 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2186 bool is_static = addr.state ==
2187 (is_multicast_ether_addr(addr.mac) ?
2188 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2189 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2190
2191 fdb->vid = vid;
2192 ether_addr_copy(fdb->addr, addr.mac);
2193 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2194
2195 err = cb(&fdb->obj);
2196 if (err)
2197 break;
2198 }
2199 } while (!is_broadcast_ether_addr(addr.mac));
2200
2201 return err;
2202}
2203
Vivien Didelotf33475b2015-10-22 09:34:41 -04002204int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2205 struct switchdev_obj_port_fdb *fdb,
2206 int (*cb)(struct switchdev_obj *obj))
2207{
2208 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2209 struct mv88e6xxx_vtu_stu_entry vlan = {
2210 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2211 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002212 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002213 int err;
2214
2215 mutex_lock(&ps->smi_mutex);
2216
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002217 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2218 err = _mv88e6xxx_port_fid_get(ds, port, &fid);
2219 if (err)
2220 goto unlock;
2221
2222 err = _mv88e6xxx_port_fdb_dump_one(ds, fid, 0, port, fdb, cb);
2223 if (err)
2224 goto unlock;
2225
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002226 /* Dump VLANs' Filtering Information Databases */
Vivien Didelotf33475b2015-10-22 09:34:41 -04002227 err = _mv88e6xxx_vtu_vid_write(ds, vlan.vid);
2228 if (err)
2229 goto unlock;
2230
2231 do {
Vivien Didelotf33475b2015-10-22 09:34:41 -04002232 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
2233 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002234 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002235
2236 if (!vlan.valid)
2237 break;
2238
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002239 err = _mv88e6xxx_port_fdb_dump_one(ds, vlan.fid, vlan.vid, port,
2240 fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002241 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002242 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002243 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2244
2245unlock:
2246 mutex_unlock(&ps->smi_mutex);
2247
2248 return err;
2249}
2250
Vivien Didelota6692752016-02-12 12:09:39 -05002251int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2252 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002253{
Vivien Didelota6692752016-02-12 12:09:39 -05002254 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002255 u16 fid;
2256 int i, err;
2257
2258 mutex_lock(&ps->smi_mutex);
2259
2260 /* Get or create the bridge FID and assign it to the port */
2261 for (i = 0; i < ps->num_ports; ++i)
2262 if (ps->ports[i].bridge_dev == bridge)
2263 break;
2264
2265 if (i < ps->num_ports)
2266 err = _mv88e6xxx_port_fid_get(ds, i, &fid);
2267 else
2268 err = _mv88e6xxx_fid_new(ds, &fid);
2269 if (err)
2270 goto unlock;
2271
2272 err = _mv88e6xxx_port_fid_set(ds, port, fid);
2273 if (err)
2274 goto unlock;
Vivien Didelota6692752016-02-12 12:09:39 -05002275
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002276 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002277 ps->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002278
2279 for (i = 0; i < ps->num_ports; ++i) {
2280 if (ps->ports[i].bridge_dev == bridge) {
2281 err = _mv88e6xxx_port_based_vlan_map(ds, i);
2282 if (err)
2283 break;
2284 }
2285 }
2286
Vivien Didelot466dfa02016-02-26 13:16:05 -05002287unlock:
2288 mutex_unlock(&ps->smi_mutex);
Vivien Didelota6692752016-02-12 12:09:39 -05002289
Vivien Didelot466dfa02016-02-26 13:16:05 -05002290 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002291}
2292
Vivien Didelot16bfa702016-03-13 16:21:33 -04002293void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002294{
Vivien Didelota6692752016-02-12 12:09:39 -05002295 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002296 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002297 u16 fid;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002298 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002299
2300 mutex_lock(&ps->smi_mutex);
2301
2302 /* Give the port a fresh Filtering Information Database */
Vivien Didelot16bfa702016-03-13 16:21:33 -04002303 if (_mv88e6xxx_fid_new(ds, &fid) ||
2304 _mv88e6xxx_port_fid_set(ds, port, fid))
2305 netdev_warn(ds->ports[port], "failed to assign a new FID\n");
Vivien Didelota6692752016-02-12 12:09:39 -05002306
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002307 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002308 ps->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002309
Vivien Didelot16bfa702016-03-13 16:21:33 -04002310 for (i = 0; i < ps->num_ports; ++i)
2311 if (i == port || ps->ports[i].bridge_dev == bridge)
2312 if (_mv88e6xxx_port_based_vlan_map(ds, i))
2313 netdev_warn(ds->ports[i], "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002314
Vivien Didelot466dfa02016-02-26 13:16:05 -05002315 mutex_unlock(&ps->smi_mutex);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002316}
2317
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002318static void mv88e6xxx_bridge_work(struct work_struct *work)
2319{
2320 struct mv88e6xxx_priv_state *ps;
2321 struct dsa_switch *ds;
2322 int port;
2323
2324 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
2325 ds = ((struct dsa_switch *)ps) - 1;
2326
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002327 mutex_lock(&ps->smi_mutex);
2328
2329 for (port = 0; port < ps->num_ports; ++port)
2330 if (test_and_clear_bit(port, ps->port_state_update_mask) &&
2331 _mv88e6xxx_port_state(ds, port, ps->ports[port].state))
2332 netdev_warn(ds->ports[port], "failed to update state to %s\n",
2333 mv88e6xxx_port_state_names[ps->ports[port].state]);
2334
2335 mutex_unlock(&ps->smi_mutex);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002336}
2337
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002338static int _mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
2339 int reg, int val)
2340{
2341 int ret;
2342
2343 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
2344 if (ret < 0)
2345 goto restore_page_0;
2346
2347 ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val);
2348restore_page_0:
2349 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
2350
2351 return ret;
2352}
2353
2354static int _mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page,
2355 int reg)
2356{
2357 int ret;
2358
2359 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
2360 if (ret < 0)
2361 goto restore_page_0;
2362
2363 ret = _mv88e6xxx_phy_read_indirect(ds, port, reg);
2364restore_page_0:
2365 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
2366
2367 return ret;
2368}
2369
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002370static int mv88e6xxx_power_on_serdes(struct dsa_switch *ds)
2371{
2372 int ret;
2373
2374 ret = _mv88e6xxx_phy_page_read(ds, REG_FIBER_SERDES, PAGE_FIBER_SERDES,
2375 MII_BMCR);
2376 if (ret < 0)
2377 return ret;
2378
2379 if (ret & BMCR_PDOWN) {
2380 ret &= ~BMCR_PDOWN;
2381 ret = _mv88e6xxx_phy_page_write(ds, REG_FIBER_SERDES,
2382 PAGE_FIBER_SERDES, MII_BMCR,
2383 ret);
2384 }
2385
2386 return ret;
2387}
2388
Andrew Lunndbde9e62015-05-06 01:09:48 +02002389static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002390{
2391 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002392 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002393 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002394
2395 mutex_lock(&ps->smi_mutex);
2396
Andrew Lunn54d792f2015-05-06 01:09:47 +02002397 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2398 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2399 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002400 mv88e6xxx_6065_family(ds) || mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002401 /* MAC Forcing register: don't force link, speed,
2402 * duplex or flow control state to any particular
2403 * values on physical ports, but force the CPU port
2404 * and all DSA ports to their maximum bandwidth and
2405 * full duplex.
2406 */
2407 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002408 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002409 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002410 reg |= PORT_PCS_CTRL_FORCE_LINK |
2411 PORT_PCS_CTRL_LINK_UP |
2412 PORT_PCS_CTRL_DUPLEX_FULL |
2413 PORT_PCS_CTRL_FORCE_DUPLEX;
2414 if (mv88e6xxx_6065_family(ds))
2415 reg |= PORT_PCS_CTRL_100;
2416 else
2417 reg |= PORT_PCS_CTRL_1000;
2418 } else {
2419 reg |= PORT_PCS_CTRL_UNFORCED;
2420 }
2421
2422 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2423 PORT_PCS_CTRL, reg);
2424 if (ret)
2425 goto abort;
2426 }
2427
2428 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2429 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2430 * tunneling, determine priority by looking at 802.1p and IP
2431 * priority fields (IP prio has precedence), and set STP state
2432 * to Forwarding.
2433 *
2434 * If this is the CPU link, use DSA or EDSA tagging depending
2435 * on which tagging mode was configured.
2436 *
2437 * If this is a link to another switch, use DSA tagging mode.
2438 *
2439 * If this is the upstream port for this switch, enable
2440 * forwarding of unknown unicasts and multicasts.
2441 */
2442 reg = 0;
2443 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2444 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2445 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002446 mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002447 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2448 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2449 PORT_CONTROL_STATE_FORWARDING;
2450 if (dsa_is_cpu_port(ds, port)) {
2451 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
2452 reg |= PORT_CONTROL_DSA_TAG;
2453 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002454 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2455 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002456 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2457 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
2458 else
2459 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002460 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2461 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002462 }
2463
2464 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2465 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2466 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002467 mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002468 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2469 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2470 }
2471 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002472 if (dsa_is_dsa_port(ds, port)) {
2473 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
2474 reg |= PORT_CONTROL_DSA_TAG;
2475 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2476 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2477 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002478 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002479 }
2480
Andrew Lunn54d792f2015-05-06 01:09:47 +02002481 if (port == dsa_upstream_port(ds))
2482 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2483 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2484 }
2485 if (reg) {
2486 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2487 PORT_CONTROL, reg);
2488 if (ret)
2489 goto abort;
2490 }
2491
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002492 /* If this port is connected to a SerDes, make sure the SerDes is not
2493 * powered down.
2494 */
2495 if (mv88e6xxx_6352_family(ds)) {
2496 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS);
2497 if (ret < 0)
2498 goto abort;
2499 ret &= PORT_STATUS_CMODE_MASK;
2500 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2501 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2502 (ret == PORT_STATUS_CMODE_SGMII)) {
2503 ret = mv88e6xxx_power_on_serdes(ds);
2504 if (ret < 0)
2505 goto abort;
2506 }
2507 }
2508
Vivien Didelot8efdda42015-08-13 12:52:23 -04002509 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002510 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002511 * untagged frames on this port, do a destination address lookup on all
2512 * received packets as usual, disable ARP mirroring and don't send a
2513 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002514 */
2515 reg = 0;
2516 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2517 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Vivien Didelotf93dd042016-03-31 16:53:45 -04002518 mv88e6xxx_6095_family(ds) || mv88e6xxx_6320_family(ds) ||
2519 mv88e6xxx_6185_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002520 reg = PORT_CONTROL_2_MAP_DA;
2521
2522 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002523 mv88e6xxx_6165_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002524 reg |= PORT_CONTROL_2_JUMBO_10240;
2525
2526 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) {
2527 /* Set the upstream port this port should use */
2528 reg |= dsa_upstream_port(ds);
2529 /* enable forwarding of unknown multicast addresses to
2530 * the upstream port
2531 */
2532 if (port == dsa_upstream_port(ds))
2533 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2534 }
2535
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002536 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002537
Andrew Lunn54d792f2015-05-06 01:09:47 +02002538 if (reg) {
2539 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2540 PORT_CONTROL_2, reg);
2541 if (ret)
2542 goto abort;
2543 }
2544
2545 /* Port Association Vector: when learning source addresses
2546 * of packets, add the address to the address database using
2547 * a port bitmap that has only the bit for this port set and
2548 * the other bits clear.
2549 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002550 reg = 1 << port;
2551 /* Disable learning for DSA and CPU ports */
2552 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2553 reg = PORT_ASSOC_VECTOR_LOCKED_PORT;
2554
2555 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002556 if (ret)
2557 goto abort;
2558
2559 /* Egress rate control 2: disable egress rate control. */
2560 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2,
2561 0x0000);
2562 if (ret)
2563 goto abort;
2564
2565 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002566 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2567 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002568 /* Do not limit the period of time that this port can
2569 * be paused for by the remote end or the period of
2570 * time that this port can pause the remote end.
2571 */
2572 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2573 PORT_PAUSE_CTRL, 0x0000);
2574 if (ret)
2575 goto abort;
2576
2577 /* Port ATU control: disable limiting the number of
2578 * address database entries that this port is allowed
2579 * to use.
2580 */
2581 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2582 PORT_ATU_CONTROL, 0x0000);
2583 /* Priority Override: disable DA, SA and VTU priority
2584 * override.
2585 */
2586 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2587 PORT_PRI_OVERRIDE, 0x0000);
2588 if (ret)
2589 goto abort;
2590
2591 /* Port Ethertype: use the Ethertype DSA Ethertype
2592 * value.
2593 */
2594 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2595 PORT_ETH_TYPE, ETH_P_EDSA);
2596 if (ret)
2597 goto abort;
2598 /* Tag Remap: use an identity 802.1p prio -> switch
2599 * prio mapping.
2600 */
2601 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2602 PORT_TAG_REGMAP_0123, 0x3210);
2603 if (ret)
2604 goto abort;
2605
2606 /* Tag Remap 2: use an identity 802.1p prio -> switch
2607 * prio mapping.
2608 */
2609 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2610 PORT_TAG_REGMAP_4567, 0x7654);
2611 if (ret)
2612 goto abort;
2613 }
2614
2615 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2616 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002617 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2618 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002619 /* Rate Control: disable ingress rate limiting. */
2620 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2621 PORT_RATE_CONTROL, 0x0001);
2622 if (ret)
2623 goto abort;
2624 }
2625
Guenter Roeck366f0a02015-03-26 18:36:30 -07002626 /* Port Control 1: disable trunking, disable sending
2627 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002628 */
Vivien Didelot614f03f2015-04-20 17:19:23 -04002629 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002630 if (ret)
2631 goto abort;
2632
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002633 /* Port based VLAN map: give each port its own address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002634 * database, and allow bidirectional communication between the
2635 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002636 */
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002637 ret = _mv88e6xxx_port_fid_set(ds, port, port + 1);
2638 if (ret)
2639 goto abort;
2640
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002641 ret = _mv88e6xxx_port_based_vlan_map(ds, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002642 if (ret)
2643 goto abort;
2644
2645 /* Default VLAN ID and priority: don't set a default VLAN
2646 * ID, and set the default packet priority to zero.
2647 */
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002648 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
2649 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002650abort:
2651 mutex_unlock(&ps->smi_mutex);
2652 return ret;
2653}
2654
Andrew Lunndbde9e62015-05-06 01:09:48 +02002655int mv88e6xxx_setup_ports(struct dsa_switch *ds)
2656{
2657 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2658 int ret;
2659 int i;
2660
2661 for (i = 0; i < ps->num_ports; i++) {
2662 ret = mv88e6xxx_setup_port(ds, i);
2663 if (ret < 0)
2664 return ret;
2665 }
2666 return 0;
2667}
2668
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002669int mv88e6xxx_setup_common(struct dsa_switch *ds)
2670{
2671 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2672
2673 mutex_init(&ps->smi_mutex);
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002674
Andrew Lunncca8b132015-04-02 04:06:39 +02002675 ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0;
Andrew Lunna8f064c2015-03-26 18:36:40 -07002676
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002677 INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
2678
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002679 return 0;
2680}
2681
Andrew Lunn54d792f2015-05-06 01:09:47 +02002682int mv88e6xxx_setup_global(struct dsa_switch *ds)
2683{
2684 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot24751e22015-08-03 09:17:44 -04002685 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002686 int i;
2687
2688 /* Set the default address aging time to 5 minutes, and
2689 * enable address learn messages to be sent to all message
2690 * ports.
2691 */
2692 REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
2693 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2694
2695 /* Configure the IP ToS mapping registers. */
2696 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2697 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2698 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2699 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2700 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2701 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2702 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2703 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2704
2705 /* Configure the IEEE 802.1p priority mapping register. */
2706 REG_WRITE(REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2707
2708 /* Send all frames with destination addresses matching
2709 * 01:80:c2:00:00:0x to the CPU port.
2710 */
2711 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
2712
2713 /* Ignore removed tag data on doubly tagged packets, disable
2714 * flow control messages, force flow control priority to the
2715 * highest, and send all special multicast frames to the CPU
2716 * port at the highest priority.
2717 */
2718 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
2719 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
2720 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
2721
2722 /* Program the DSA routing table. */
2723 for (i = 0; i < 32; i++) {
2724 int nexthop = 0x1f;
2725
2726 if (ds->pd->rtable &&
2727 i != ds->index && i < ds->dst->pd->nr_chips)
2728 nexthop = ds->pd->rtable[i] & 0x1f;
2729
2730 REG_WRITE(REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING,
2731 GLOBAL2_DEVICE_MAPPING_UPDATE |
2732 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) |
2733 nexthop);
2734 }
2735
2736 /* Clear all trunk masks. */
2737 for (i = 0; i < 8; i++)
2738 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
2739 0x8000 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
2740 ((1 << ps->num_ports) - 1));
2741
2742 /* Clear all trunk mappings. */
2743 for (i = 0; i < 16; i++)
2744 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING,
2745 GLOBAL2_TRUNK_MAPPING_UPDATE |
2746 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
2747
2748 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002749 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2750 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002751 /* Send all frames with destination addresses matching
2752 * 01:80:c2:00:00:2x to the CPU port.
2753 */
2754 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_2X, 0xffff);
2755
2756 /* Initialise cross-chip port VLAN table to reset
2757 * defaults.
2758 */
2759 REG_WRITE(REG_GLOBAL2, GLOBAL2_PVT_ADDR, 0x9000);
2760
2761 /* Clear the priority override table. */
2762 for (i = 0; i < 16; i++)
2763 REG_WRITE(REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE,
2764 0x8000 | (i << 8));
2765 }
2766
2767 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2768 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002769 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2770 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002771 /* Disable ingress rate limiting by resetting all
2772 * ingress rate limit registers to their initial
2773 * state.
2774 */
2775 for (i = 0; i < ps->num_ports; i++)
2776 REG_WRITE(REG_GLOBAL2, GLOBAL2_INGRESS_OP,
2777 0x9000 | (i << 8));
2778 }
2779
Andrew Lunndb687a52015-06-20 21:31:29 +02002780 /* Clear the statistics counters for all ports */
2781 REG_WRITE(REG_GLOBAL, GLOBAL_STATS_OP, GLOBAL_STATS_OP_FLUSH_ALL);
2782
2783 /* Wait for the flush to complete. */
Vivien Didelot24751e22015-08-03 09:17:44 -04002784 mutex_lock(&ps->smi_mutex);
2785 ret = _mv88e6xxx_stats_wait(ds);
Vivien Didelot6b17e862015-08-13 12:52:18 -04002786 if (ret < 0)
2787 goto unlock;
2788
Vivien Didelotc161d0a2015-09-04 14:34:13 -04002789 /* Clear all ATU entries */
2790 ret = _mv88e6xxx_atu_flush(ds, 0, true);
2791 if (ret < 0)
2792 goto unlock;
2793
Vivien Didelot6b17e862015-08-13 12:52:18 -04002794 /* Clear all the VTU and STU entries */
2795 ret = _mv88e6xxx_vtu_stu_flush(ds);
2796unlock:
Vivien Didelot24751e22015-08-03 09:17:44 -04002797 mutex_unlock(&ps->smi_mutex);
Andrew Lunndb687a52015-06-20 21:31:29 +02002798
Vivien Didelot24751e22015-08-03 09:17:44 -04002799 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002800}
2801
Andrew Lunn143a8302015-04-02 04:06:34 +02002802int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active)
2803{
2804 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2805 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Andrew Lunnc8c1b392015-11-20 03:56:24 +01002806 struct gpio_desc *gpiod = ds->pd->reset;
Andrew Lunn143a8302015-04-02 04:06:34 +02002807 unsigned long timeout;
2808 int ret;
2809 int i;
2810
2811 /* Set all ports to the disabled state. */
2812 for (i = 0; i < ps->num_ports; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002813 ret = REG_READ(REG_PORT(i), PORT_CONTROL);
2814 REG_WRITE(REG_PORT(i), PORT_CONTROL, ret & 0xfffc);
Andrew Lunn143a8302015-04-02 04:06:34 +02002815 }
2816
2817 /* Wait for transmit queues to drain. */
2818 usleep_range(2000, 4000);
2819
Andrew Lunnc8c1b392015-11-20 03:56:24 +01002820 /* If there is a gpio connected to the reset pin, toggle it */
2821 if (gpiod) {
2822 gpiod_set_value_cansleep(gpiod, 1);
2823 usleep_range(10000, 20000);
2824 gpiod_set_value_cansleep(gpiod, 0);
2825 usleep_range(10000, 20000);
2826 }
2827
Andrew Lunn143a8302015-04-02 04:06:34 +02002828 /* Reset the switch. Keep the PPU active if requested. The PPU
2829 * needs to be active to support indirect phy register access
2830 * through global registers 0x18 and 0x19.
2831 */
2832 if (ppu_active)
2833 REG_WRITE(REG_GLOBAL, 0x04, 0xc000);
2834 else
2835 REG_WRITE(REG_GLOBAL, 0x04, 0xc400);
2836
2837 /* Wait up to one second for reset to complete. */
2838 timeout = jiffies + 1 * HZ;
2839 while (time_before(jiffies, timeout)) {
2840 ret = REG_READ(REG_GLOBAL, 0x00);
2841 if ((ret & is_reset) == is_reset)
2842 break;
2843 usleep_range(1000, 2000);
2844 }
2845 if (time_after(jiffies, timeout))
2846 return -ETIMEDOUT;
2847
2848 return 0;
2849}
2850
Andrew Lunn491435852015-04-02 04:06:35 +02002851int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
2852{
2853 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2854 int ret;
2855
Andrew Lunn3898c142015-05-06 01:09:53 +02002856 mutex_lock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002857 ret = _mv88e6xxx_phy_page_read(ds, port, page, reg);
Andrew Lunn3898c142015-05-06 01:09:53 +02002858 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002859
Andrew Lunn491435852015-04-02 04:06:35 +02002860 return ret;
2861}
2862
2863int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
2864 int reg, int val)
2865{
2866 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2867 int ret;
2868
Andrew Lunn3898c142015-05-06 01:09:53 +02002869 mutex_lock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002870 ret = _mv88e6xxx_phy_page_write(ds, port, page, reg, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002871 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002872
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002873 return ret;
2874}
2875
2876static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port)
2877{
2878 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2879
2880 if (port >= 0 && port < ps->num_ports)
2881 return port;
2882 return -EINVAL;
2883}
2884
2885int
2886mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
2887{
2888 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2889 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2890 int ret;
2891
2892 if (addr < 0)
2893 return addr;
2894
Andrew Lunn3898c142015-05-06 01:09:53 +02002895 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002896 ret = _mv88e6xxx_phy_read(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02002897 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002898 return ret;
2899}
2900
2901int
2902mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
2903{
2904 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2905 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2906 int ret;
2907
2908 if (addr < 0)
2909 return addr;
2910
Andrew Lunn3898c142015-05-06 01:09:53 +02002911 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002912 ret = _mv88e6xxx_phy_write(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002913 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002914 return ret;
2915}
2916
2917int
2918mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum)
2919{
2920 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2921 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2922 int ret;
2923
2924 if (addr < 0)
2925 return addr;
2926
Andrew Lunn3898c142015-05-06 01:09:53 +02002927 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002928 ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02002929 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002930 return ret;
2931}
2932
2933int
2934mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum,
2935 u16 val)
2936{
2937 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2938 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2939 int ret;
2940
2941 if (addr < 0)
2942 return addr;
2943
Andrew Lunn3898c142015-05-06 01:09:53 +02002944 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002945 ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002946 mutex_unlock(&ps->smi_mutex);
Andrew Lunn491435852015-04-02 04:06:35 +02002947 return ret;
2948}
2949
Guenter Roeckc22995c2015-07-25 09:42:28 -07002950#ifdef CONFIG_NET_DSA_HWMON
2951
2952static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
2953{
2954 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2955 int ret;
2956 int val;
2957
2958 *temp = 0;
2959
2960 mutex_lock(&ps->smi_mutex);
2961
2962 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
2963 if (ret < 0)
2964 goto error;
2965
2966 /* Enable temperature sensor */
2967 ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
2968 if (ret < 0)
2969 goto error;
2970
2971 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
2972 if (ret < 0)
2973 goto error;
2974
2975 /* Wait for temperature to stabilize */
2976 usleep_range(10000, 12000);
2977
2978 val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
2979 if (val < 0) {
2980 ret = val;
2981 goto error;
2982 }
2983
2984 /* Disable temperature sensor */
2985 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
2986 if (ret < 0)
2987 goto error;
2988
2989 *temp = ((val & 0x1f) - 5) * 5;
2990
2991error:
2992 _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
2993 mutex_unlock(&ps->smi_mutex);
2994 return ret;
2995}
2996
2997static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
2998{
2999 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
3000 int ret;
3001
3002 *temp = 0;
3003
3004 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
3005 if (ret < 0)
3006 return ret;
3007
3008 *temp = (ret & 0xff) - 25;
3009
3010 return 0;
3011}
3012
3013int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3014{
3015 if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
3016 return mv88e63xx_get_temp(ds, temp);
3017
3018 return mv88e61xx_get_temp(ds, temp);
3019}
3020
3021int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3022{
3023 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
3024 int ret;
3025
3026 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
3027 return -EOPNOTSUPP;
3028
3029 *temp = 0;
3030
3031 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3032 if (ret < 0)
3033 return ret;
3034
3035 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3036
3037 return 0;
3038}
3039
3040int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3041{
3042 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
3043 int ret;
3044
3045 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
3046 return -EOPNOTSUPP;
3047
3048 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3049 if (ret < 0)
3050 return ret;
3051 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3052 return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
3053 (ret & 0xe0ff) | (temp << 8));
3054}
3055
3056int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3057{
3058 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
3059 int ret;
3060
3061 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
3062 return -EOPNOTSUPP;
3063
3064 *alarm = false;
3065
3066 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3067 if (ret < 0)
3068 return ret;
3069
3070 *alarm = !!(ret & 0x40);
3071
3072 return 0;
3073}
3074#endif /* CONFIG_NET_DSA_HWMON */
3075
Vivien Didelotb9b37712015-10-30 19:39:48 -04003076char *mv88e6xxx_lookup_name(struct device *host_dev, int sw_addr,
3077 const struct mv88e6xxx_switch_id *table,
3078 unsigned int num)
3079{
3080 struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
3081 int i, ret;
3082
3083 if (!bus)
3084 return NULL;
3085
3086 ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
3087 if (ret < 0)
3088 return NULL;
3089
3090 /* Look up the exact switch ID */
3091 for (i = 0; i < num; ++i)
3092 if (table[i].id == ret)
3093 return table[i].name;
3094
3095 /* Look up only the product number */
3096 for (i = 0; i < num; ++i) {
3097 if (table[i].id == (ret & PORT_SWITCH_ID_PROD_NUM_MASK)) {
3098 dev_warn(host_dev, "unknown revision %d, using base switch 0x%x\n",
3099 ret & PORT_SWITCH_ID_REV_MASK,
3100 ret & PORT_SWITCH_ID_PROD_NUM_MASK);
3101 return table[i].name;
3102 }
3103 }
3104
3105 return NULL;
3106}
3107
Ben Hutchings98e67302011-11-25 14:36:19 +00003108static int __init mv88e6xxx_init(void)
3109{
3110#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
3111 register_switch_driver(&mv88e6131_switch_driver);
3112#endif
Andrew Lunnca3dfa52016-03-12 00:01:36 +01003113#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123)
3114 register_switch_driver(&mv88e6123_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003115#endif
Guenter Roeck3ad50cc2014-10-29 10:44:56 -07003116#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
3117 register_switch_driver(&mv88e6352_switch_driver);
3118#endif
Andrew Lunn42f27252014-09-12 23:58:44 +02003119#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
3120 register_switch_driver(&mv88e6171_switch_driver);
3121#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00003122 return 0;
3123}
3124module_init(mv88e6xxx_init);
3125
3126static void __exit mv88e6xxx_cleanup(void)
3127{
Andrew Lunn42f27252014-09-12 23:58:44 +02003128#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
3129 unregister_switch_driver(&mv88e6171_switch_driver);
3130#endif
Vivien Didelot4212b542015-05-01 10:43:52 -04003131#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
3132 unregister_switch_driver(&mv88e6352_switch_driver);
3133#endif
Andrew Lunnca3dfa52016-03-12 00:01:36 +01003134#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123)
3135 unregister_switch_driver(&mv88e6123_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003136#endif
3137#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
3138 unregister_switch_driver(&mv88e6131_switch_driver);
3139#endif
3140}
3141module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003142
3143MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3144MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3145MODULE_LICENSE("GPL");