blob: 6c472cfa1df9903c4f21a9f39ae29eecb4732f73 [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 Lunnc8c1b39a2015-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
Andrew Lunn158bc062016-04-28 21:24:06 -040028static void assert_smi_lock(struct mv88e6xxx_priv_state *ps)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040029{
Vivien Didelot3996a4f2015-10-30 18:56:45 -040030 if (unlikely(!mutex_is_locked(&ps->smi_mutex))) {
Andrew Lunn158bc062016-04-28 21:24:06 -040031 dev_err(ps->dev, "SMI lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040032 dump_stack();
33 }
34}
35
Barry Grussling3675c8d2013-01-08 16:05:53 +000036/* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000037 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
38 * will be directly accessible on some {device address,register address}
39 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
40 * will only respond to SMI transactions to that specific address, and
41 * an indirect addressing mechanism needs to be used to access its
42 * registers.
43 */
44static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
45{
46 int ret;
47 int i;
48
49 for (i = 0; i < 16; i++) {
Neil Armstrong6e899e62015-10-22 10:37:53 +020050 ret = mdiobus_read_nested(bus, sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000051 if (ret < 0)
52 return ret;
53
Andrew Lunncca8b132015-04-02 04:06:39 +020054 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000055 return 0;
56 }
57
58 return -ETIMEDOUT;
59}
60
Vivien Didelotb9b37712015-10-30 19:39:48 -040061static int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr,
62 int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000063{
64 int ret;
65
66 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +020067 return mdiobus_read_nested(bus, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000068
Barry Grussling3675c8d2013-01-08 16:05:53 +000069 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000070 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
71 if (ret < 0)
72 return ret;
73
Barry Grussling3675c8d2013-01-08 16:05:53 +000074 /* Transmit the read command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +020075 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
76 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000077 if (ret < 0)
78 return ret;
79
Barry Grussling3675c8d2013-01-08 16:05:53 +000080 /* Wait for the read command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000081 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
82 if (ret < 0)
83 return ret;
84
Barry Grussling3675c8d2013-01-08 16:05:53 +000085 /* Read the data. */
Neil Armstrong6e899e62015-10-22 10:37:53 +020086 ret = mdiobus_read_nested(bus, sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000087 if (ret < 0)
88 return ret;
89
90 return ret & 0xffff;
91}
92
Andrew Lunn158bc062016-04-28 21:24:06 -040093static int _mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps,
94 int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000095{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000096 int ret;
97
Andrew Lunn158bc062016-04-28 21:24:06 -040098 assert_smi_lock(ps);
Vivien Didelot3996a4f2015-10-30 18:56:45 -040099
Andrew Lunna77d43f2016-04-13 02:40:42 +0200100 ret = __mv88e6xxx_reg_read(ps->bus, ps->sw_addr, addr, reg);
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500101 if (ret < 0)
102 return ret;
103
Andrew Lunn158bc062016-04-28 21:24:06 -0400104 dev_dbg(ps->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500105 addr, reg, ret);
106
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000107 return ret;
108}
109
Andrew Lunn158bc062016-04-28 21:24:06 -0400110int mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps, int addr, int reg)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700111{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700112 int ret;
113
114 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -0400115 ret = _mv88e6xxx_reg_read(ps, addr, reg);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700116 mutex_unlock(&ps->smi_mutex);
117
118 return ret;
119}
120
Vivien Didelotb9b37712015-10-30 19:39:48 -0400121static int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
122 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000123{
124 int ret;
125
126 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +0200127 return mdiobus_write_nested(bus, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000128
Barry Grussling3675c8d2013-01-08 16:05:53 +0000129 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000130 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
131 if (ret < 0)
132 return ret;
133
Barry Grussling3675c8d2013-01-08 16:05:53 +0000134 /* Transmit the data to write. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200135 ret = mdiobus_write_nested(bus, sw_addr, SMI_DATA, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000136 if (ret < 0)
137 return ret;
138
Barry Grussling3675c8d2013-01-08 16:05:53 +0000139 /* Transmit the write command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200140 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
141 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000142 if (ret < 0)
143 return ret;
144
Barry Grussling3675c8d2013-01-08 16:05:53 +0000145 /* Wait for the write command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000146 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
147 if (ret < 0)
148 return ret;
149
150 return 0;
151}
152
Andrew Lunn158bc062016-04-28 21:24:06 -0400153static int _mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr,
154 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000155{
Andrew Lunn158bc062016-04-28 21:24:06 -0400156 assert_smi_lock(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000157
Andrew Lunn158bc062016-04-28 21:24:06 -0400158 dev_dbg(ps->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500159 addr, reg, val);
160
Andrew Lunna77d43f2016-04-13 02:40:42 +0200161 return __mv88e6xxx_reg_write(ps->bus, ps->sw_addr, addr, reg, val);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700162}
163
Andrew Lunn158bc062016-04-28 21:24:06 -0400164int mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr,
165 int reg, u16 val)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700166{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700167 int ret;
168
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000169 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -0400170 ret = _mv88e6xxx_reg_write(ps, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000171 mutex_unlock(&ps->smi_mutex);
172
173 return ret;
174}
175
Vivien Didelot1d13a062016-05-09 13:22:43 -0400176static int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000177{
Andrew Lunn158bc062016-04-28 21:24:06 -0400178 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200179 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000180
Andrew Lunn158bc062016-04-28 21:24:06 -0400181 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_01,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200182 (addr[0] << 8) | addr[1]);
183 if (err)
184 return err;
185
Andrew Lunn158bc062016-04-28 21:24:06 -0400186 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_23,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200187 (addr[2] << 8) | addr[3]);
188 if (err)
189 return err;
190
Andrew Lunn158bc062016-04-28 21:24:06 -0400191 return mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_45,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200192 (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000193}
194
Vivien Didelot1d13a062016-05-09 13:22:43 -0400195static int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000196{
Andrew Lunn158bc062016-04-28 21:24:06 -0400197 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000198 int ret;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200199 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000200
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 Lunn158bc062016-04-28 21:24:06 -0400205 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200206 GLOBAL2_SWITCH_MAC_BUSY |
207 (i << 8) | addr[i]);
208 if (ret)
209 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000210
Barry Grussling3675c8d2013-01-08 16:05:53 +0000211 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000212 for (j = 0; j < 16; j++) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400213 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200214 GLOBAL2_SWITCH_MAC);
215 if (ret < 0)
216 return ret;
217
Andrew Lunncca8b132015-04-02 04:06:39 +0200218 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000219 break;
220 }
221 if (j == 16)
222 return -ETIMEDOUT;
223 }
224
225 return 0;
226}
227
Vivien Didelot1d13a062016-05-09 13:22:43 -0400228int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
229{
230 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
231
232 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SWITCH_MAC))
233 return mv88e6xxx_set_addr_indirect(ds, addr);
234 else
235 return mv88e6xxx_set_addr_direct(ds, addr);
236}
237
Andrew Lunn158bc062016-04-28 21:24:06 -0400238static int _mv88e6xxx_phy_read(struct mv88e6xxx_priv_state *ps, int addr,
239 int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000240{
241 if (addr >= 0)
Andrew Lunn158bc062016-04-28 21:24:06 -0400242 return _mv88e6xxx_reg_read(ps, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000243 return 0xffff;
244}
245
Andrew Lunn158bc062016-04-28 21:24:06 -0400246static int _mv88e6xxx_phy_write(struct mv88e6xxx_priv_state *ps, int addr,
247 int regnum, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000248{
249 if (addr >= 0)
Andrew Lunn158bc062016-04-28 21:24:06 -0400250 return _mv88e6xxx_reg_write(ps, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000251 return 0;
252}
253
Andrew Lunn158bc062016-04-28 21:24:06 -0400254static int mv88e6xxx_ppu_disable(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000255{
256 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000257 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000258
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400259 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200260 if (ret < 0)
261 return ret;
262
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400263 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL,
264 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200265 if (ret)
266 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000267
Barry Grussling19b2f972013-01-08 16:05:54 +0000268 timeout = jiffies + 1 * HZ;
269 while (time_before(jiffies, timeout)) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400270 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200271 if (ret < 0)
272 return ret;
273
Barry Grussling19b2f972013-01-08 16:05:54 +0000274 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200275 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
276 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000277 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000278 }
279
280 return -ETIMEDOUT;
281}
282
Andrew Lunn158bc062016-04-28 21:24:06 -0400283static int mv88e6xxx_ppu_enable(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000284{
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200285 int ret, err;
Barry Grussling19b2f972013-01-08 16:05:54 +0000286 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000287
Andrew Lunn158bc062016-04-28 21:24:06 -0400288 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200289 if (ret < 0)
290 return ret;
291
Andrew Lunn158bc062016-04-28 21:24:06 -0400292 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200293 ret | GLOBAL_CONTROL_PPU_ENABLE);
294 if (err)
295 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000296
Barry Grussling19b2f972013-01-08 16:05:54 +0000297 timeout = jiffies + 1 * HZ;
298 while (time_before(jiffies, timeout)) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400299 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200300 if (ret < 0)
301 return ret;
302
Barry Grussling19b2f972013-01-08 16:05:54 +0000303 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200304 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
305 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000306 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000307 }
308
309 return -ETIMEDOUT;
310}
311
312static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
313{
314 struct mv88e6xxx_priv_state *ps;
315
316 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
317 if (mutex_trylock(&ps->ppu_mutex)) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400318 if (mv88e6xxx_ppu_enable(ps) == 0)
Barry Grussling85686582013-01-08 16:05:56 +0000319 ps->ppu_disabled = 0;
320 mutex_unlock(&ps->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000321 }
322}
323
324static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
325{
326 struct mv88e6xxx_priv_state *ps = (void *)_ps;
327
328 schedule_work(&ps->ppu_work);
329}
330
Andrew Lunn158bc062016-04-28 21:24:06 -0400331static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000332{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000333 int ret;
334
335 mutex_lock(&ps->ppu_mutex);
336
Barry Grussling3675c8d2013-01-08 16:05:53 +0000337 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000338 * we can access the PHY registers. If it was already
339 * disabled, cancel the timer that is going to re-enable
340 * it.
341 */
342 if (!ps->ppu_disabled) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400343 ret = mv88e6xxx_ppu_disable(ps);
Barry Grussling85686582013-01-08 16:05:56 +0000344 if (ret < 0) {
345 mutex_unlock(&ps->ppu_mutex);
346 return ret;
347 }
348 ps->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000349 } else {
Barry Grussling85686582013-01-08 16:05:56 +0000350 del_timer(&ps->ppu_timer);
351 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000352 }
353
354 return ret;
355}
356
Andrew Lunn158bc062016-04-28 21:24:06 -0400357static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000358{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000359 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000360 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
361 mutex_unlock(&ps->ppu_mutex);
362}
363
Andrew Lunn158bc062016-04-28 21:24:06 -0400364void mv88e6xxx_ppu_state_init(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000365{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000366 mutex_init(&ps->ppu_mutex);
367 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
368 init_timer(&ps->ppu_timer);
369 ps->ppu_timer.data = (unsigned long)ps;
370 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
371}
372
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400373static int mv88e6xxx_phy_read_ppu(struct mv88e6xxx_priv_state *ps, int addr,
374 int regnum)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000375{
376 int ret;
377
Andrew Lunn158bc062016-04-28 21:24:06 -0400378 ret = mv88e6xxx_ppu_access_get(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000379 if (ret >= 0) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400380 ret = _mv88e6xxx_reg_read(ps, addr, regnum);
Andrew Lunn158bc062016-04-28 21:24:06 -0400381 mv88e6xxx_ppu_access_put(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000382 }
383
384 return ret;
385}
386
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400387static int mv88e6xxx_phy_write_ppu(struct mv88e6xxx_priv_state *ps, int addr,
388 int regnum, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000389{
390 int ret;
391
Andrew Lunn158bc062016-04-28 21:24:06 -0400392 ret = mv88e6xxx_ppu_access_get(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000393 if (ret >= 0) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400394 ret = _mv88e6xxx_reg_write(ps, addr, regnum, val);
Andrew Lunn158bc062016-04-28 21:24:06 -0400395 mv88e6xxx_ppu_access_put(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000396 }
397
398 return ret;
399}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000400
Andrew Lunn158bc062016-04-28 21:24:06 -0400401static bool mv88e6xxx_6065_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200402{
Vivien Didelot22356472016-04-17 13:24:00 -0400403 return ps->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200404}
405
Andrew Lunn158bc062016-04-28 21:24:06 -0400406static bool mv88e6xxx_6095_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200407{
Vivien Didelot22356472016-04-17 13:24:00 -0400408 return ps->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200409}
410
Andrew Lunn158bc062016-04-28 21:24:06 -0400411static bool mv88e6xxx_6097_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200412{
Vivien Didelot22356472016-04-17 13:24:00 -0400413 return ps->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200414}
415
Andrew Lunn158bc062016-04-28 21:24:06 -0400416static bool mv88e6xxx_6165_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200417{
Vivien Didelot22356472016-04-17 13:24:00 -0400418 return ps->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200419}
420
Andrew Lunn158bc062016-04-28 21:24:06 -0400421static bool mv88e6xxx_6185_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200422{
Vivien Didelot22356472016-04-17 13:24:00 -0400423 return ps->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200424}
425
Andrew Lunn158bc062016-04-28 21:24:06 -0400426static bool mv88e6xxx_6320_family(struct mv88e6xxx_priv_state *ps)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700427{
Vivien Didelot22356472016-04-17 13:24:00 -0400428 return ps->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700429}
430
Andrew Lunn158bc062016-04-28 21:24:06 -0400431static bool mv88e6xxx_6351_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200432{
Vivien Didelot22356472016-04-17 13:24:00 -0400433 return ps->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200434}
435
Andrew Lunn158bc062016-04-28 21:24:06 -0400436static bool mv88e6xxx_6352_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200437{
Vivien Didelot22356472016-04-17 13:24:00 -0400438 return ps->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200439}
440
Andrew Lunn158bc062016-04-28 21:24:06 -0400441static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_priv_state *ps)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400442{
Vivien Didelotcd5a2c82016-04-17 13:24:02 -0400443 return ps->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400444}
445
Andrew Lunn158bc062016-04-28 21:24:06 -0400446static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_priv_state *ps)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400447{
448 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Andrew Lunn158bc062016-04-28 21:24:06 -0400449 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
450 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400451 return true;
452
453 return false;
454}
455
Andrew Lunn158bc062016-04-28 21:24:06 -0400456static bool mv88e6xxx_has_stu(struct mv88e6xxx_priv_state *ps)
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -0400457{
458 /* Does the device have STU and dedicated SID registers for VTU ops? */
Andrew Lunn158bc062016-04-28 21:24:06 -0400459 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
460 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps))
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -0400461 return true;
462
463 return false;
464}
465
Andrew Lunndea87022015-08-31 15:56:47 +0200466/* We expect the switch to perform auto negotiation if there is a real
467 * phy. However, in the case of a fixed link phy, we force the port
468 * settings from the fixed link settings.
469 */
470void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
471 struct phy_device *phydev)
472{
473 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200474 u32 reg;
475 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200476
477 if (!phy_is_pseudo_fixed_link(phydev))
478 return;
479
480 mutex_lock(&ps->smi_mutex);
481
Andrew Lunn158bc062016-04-28 21:24:06 -0400482 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200483 if (ret < 0)
484 goto out;
485
486 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
487 PORT_PCS_CTRL_FORCE_LINK |
488 PORT_PCS_CTRL_DUPLEX_FULL |
489 PORT_PCS_CTRL_FORCE_DUPLEX |
490 PORT_PCS_CTRL_UNFORCED);
491
492 reg |= PORT_PCS_CTRL_FORCE_LINK;
493 if (phydev->link)
494 reg |= PORT_PCS_CTRL_LINK_UP;
495
Andrew Lunn158bc062016-04-28 21:24:06 -0400496 if (mv88e6xxx_6065_family(ps) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200497 goto out;
498
499 switch (phydev->speed) {
500 case SPEED_1000:
501 reg |= PORT_PCS_CTRL_1000;
502 break;
503 case SPEED_100:
504 reg |= PORT_PCS_CTRL_100;
505 break;
506 case SPEED_10:
507 reg |= PORT_PCS_CTRL_10;
508 break;
509 default:
510 pr_info("Unknown speed");
511 goto out;
512 }
513
514 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
515 if (phydev->duplex == DUPLEX_FULL)
516 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
517
Andrew Lunn158bc062016-04-28 21:24:06 -0400518 if ((mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps)) &&
Vivien Didelot009a2b92016-04-17 13:24:01 -0400519 (port >= ps->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200520 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
521 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
522 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
523 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
524 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
525 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
526 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
527 }
Andrew Lunn158bc062016-04-28 21:24:06 -0400528 _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200529
530out:
531 mutex_unlock(&ps->smi_mutex);
532}
533
Andrew Lunn158bc062016-04-28 21:24:06 -0400534static int _mv88e6xxx_stats_wait(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000535{
536 int ret;
537 int i;
538
539 for (i = 0; i < 10; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400540 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200541 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000542 return 0;
543 }
544
545 return -ETIMEDOUT;
546}
547
Andrew Lunn158bc062016-04-28 21:24:06 -0400548static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_priv_state *ps,
549 int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000550{
551 int ret;
552
Andrew Lunn158bc062016-04-28 21:24:06 -0400553 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200554 port = (port + 1) << 5;
555
Barry Grussling3675c8d2013-01-08 16:05:53 +0000556 /* Snapshot the hardware statistics counters for this port. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400557 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200558 GLOBAL_STATS_OP_CAPTURE_PORT |
559 GLOBAL_STATS_OP_HIST_RX_TX | port);
560 if (ret < 0)
561 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000562
Barry Grussling3675c8d2013-01-08 16:05:53 +0000563 /* Wait for the snapshotting to complete. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400564 ret = _mv88e6xxx_stats_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000565 if (ret < 0)
566 return ret;
567
568 return 0;
569}
570
Andrew Lunn158bc062016-04-28 21:24:06 -0400571static void _mv88e6xxx_stats_read(struct mv88e6xxx_priv_state *ps,
572 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000573{
574 u32 _val;
575 int ret;
576
577 *val = 0;
578
Andrew Lunn158bc062016-04-28 21:24:06 -0400579 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200580 GLOBAL_STATS_OP_READ_CAPTURED |
581 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000582 if (ret < 0)
583 return;
584
Andrew Lunn158bc062016-04-28 21:24:06 -0400585 ret = _mv88e6xxx_stats_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000586 if (ret < 0)
587 return;
588
Andrew Lunn158bc062016-04-28 21:24:06 -0400589 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000590 if (ret < 0)
591 return;
592
593 _val = ret << 16;
594
Andrew Lunn158bc062016-04-28 21:24:06 -0400595 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000596 if (ret < 0)
597 return;
598
599 *val = _val | ret;
600}
601
Andrew Lunne413e7e2015-04-02 04:06:38 +0200602static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100603 { "in_good_octets", 8, 0x00, BANK0, },
604 { "in_bad_octets", 4, 0x02, BANK0, },
605 { "in_unicast", 4, 0x04, BANK0, },
606 { "in_broadcasts", 4, 0x06, BANK0, },
607 { "in_multicasts", 4, 0x07, BANK0, },
608 { "in_pause", 4, 0x16, BANK0, },
609 { "in_undersize", 4, 0x18, BANK0, },
610 { "in_fragments", 4, 0x19, BANK0, },
611 { "in_oversize", 4, 0x1a, BANK0, },
612 { "in_jabber", 4, 0x1b, BANK0, },
613 { "in_rx_error", 4, 0x1c, BANK0, },
614 { "in_fcs_error", 4, 0x1d, BANK0, },
615 { "out_octets", 8, 0x0e, BANK0, },
616 { "out_unicast", 4, 0x10, BANK0, },
617 { "out_broadcasts", 4, 0x13, BANK0, },
618 { "out_multicasts", 4, 0x12, BANK0, },
619 { "out_pause", 4, 0x15, BANK0, },
620 { "excessive", 4, 0x11, BANK0, },
621 { "collisions", 4, 0x1e, BANK0, },
622 { "deferred", 4, 0x05, BANK0, },
623 { "single", 4, 0x14, BANK0, },
624 { "multiple", 4, 0x17, BANK0, },
625 { "out_fcs_error", 4, 0x03, BANK0, },
626 { "late", 4, 0x1f, BANK0, },
627 { "hist_64bytes", 4, 0x08, BANK0, },
628 { "hist_65_127bytes", 4, 0x09, BANK0, },
629 { "hist_128_255bytes", 4, 0x0a, BANK0, },
630 { "hist_256_511bytes", 4, 0x0b, BANK0, },
631 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
632 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
633 { "sw_in_discards", 4, 0x10, PORT, },
634 { "sw_in_filtered", 2, 0x12, PORT, },
635 { "sw_out_filtered", 2, 0x13, PORT, },
636 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
637 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
638 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
639 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
640 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
641 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
642 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
643 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
644 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
645 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
646 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
647 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
648 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
649 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
650 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
651 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
652 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
653 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
654 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
655 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
656 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
657 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
658 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
659 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
660 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
661 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200662};
663
Andrew Lunn158bc062016-04-28 21:24:06 -0400664static bool mv88e6xxx_has_stat(struct mv88e6xxx_priv_state *ps,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100665 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200666{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100667 switch (stat->type) {
668 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200669 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100670 case BANK1:
Andrew Lunn158bc062016-04-28 21:24:06 -0400671 return mv88e6xxx_6320_family(ps);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100672 case PORT:
Andrew Lunn158bc062016-04-28 21:24:06 -0400673 return mv88e6xxx_6095_family(ps) ||
674 mv88e6xxx_6185_family(ps) ||
675 mv88e6xxx_6097_family(ps) ||
676 mv88e6xxx_6165_family(ps) ||
677 mv88e6xxx_6351_family(ps) ||
678 mv88e6xxx_6352_family(ps);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200679 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100680 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000681}
682
Andrew Lunn158bc062016-04-28 21:24:06 -0400683static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_priv_state *ps,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100684 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200685 int port)
686{
Andrew Lunn80c46272015-06-20 18:42:30 +0200687 u32 low;
688 u32 high = 0;
689 int ret;
690 u64 value;
691
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100692 switch (s->type) {
693 case PORT:
Andrew Lunn158bc062016-04-28 21:24:06 -0400694 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200695 if (ret < 0)
696 return UINT64_MAX;
697
698 low = ret;
699 if (s->sizeof_stat == 4) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400700 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100701 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200702 if (ret < 0)
703 return UINT64_MAX;
704 high = ret;
705 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100706 break;
707 case BANK0:
708 case BANK1:
Andrew Lunn158bc062016-04-28 21:24:06 -0400709 _mv88e6xxx_stats_read(ps, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200710 if (s->sizeof_stat == 8)
Andrew Lunn158bc062016-04-28 21:24:06 -0400711 _mv88e6xxx_stats_read(ps, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200712 }
713 value = (((u64)high) << 16) | low;
714 return value;
715}
716
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100717void mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
718{
Andrew Lunn158bc062016-04-28 21:24:06 -0400719 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100720 struct mv88e6xxx_hw_stat *stat;
721 int i, j;
722
723 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
724 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400725 if (mv88e6xxx_has_stat(ps, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100726 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
727 ETH_GSTRING_LEN);
728 j++;
729 }
730 }
731}
732
733int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
734{
Andrew Lunn158bc062016-04-28 21:24:06 -0400735 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100736 struct mv88e6xxx_hw_stat *stat;
737 int i, j;
738
739 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
740 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400741 if (mv88e6xxx_has_stat(ps, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100742 j++;
743 }
744 return j;
745}
746
747void
748mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
749 int port, uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000750{
Florian Fainellia22adce2014-04-28 11:14:28 -0700751 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100752 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000753 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100754 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000755
Andrew Lunn31888232015-05-06 01:09:54 +0200756 mutex_lock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000757
Andrew Lunn158bc062016-04-28 21:24:06 -0400758 ret = _mv88e6xxx_stats_snapshot(ps, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000759 if (ret < 0) {
Andrew Lunn31888232015-05-06 01:09:54 +0200760 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000761 return;
762 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100763 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
764 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400765 if (mv88e6xxx_has_stat(ps, stat)) {
766 data[j] = _mv88e6xxx_get_ethtool_stat(ps, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100767 j++;
768 }
769 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000770
Andrew Lunn31888232015-05-06 01:09:54 +0200771 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000772}
Ben Hutchings98e67302011-11-25 14:36:19 +0000773
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700774int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
775{
776 return 32 * sizeof(u16);
777}
778
779void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
780 struct ethtool_regs *regs, void *_p)
781{
Andrew Lunn158bc062016-04-28 21:24:06 -0400782 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700783 u16 *p = _p;
784 int i;
785
786 regs->version = 0;
787
788 memset(p, 0xff, 32 * sizeof(u16));
789
Vivien Didelot23062512016-05-09 13:22:45 -0400790 mutex_lock(&ps->smi_mutex);
791
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700792 for (i = 0; i < 32; i++) {
793 int ret;
794
Vivien Didelot23062512016-05-09 13:22:45 -0400795 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700796 if (ret >= 0)
797 p[i] = ret;
798 }
Vivien Didelot23062512016-05-09 13:22:45 -0400799
800 mutex_unlock(&ps->smi_mutex);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700801}
802
Andrew Lunn158bc062016-04-28 21:24:06 -0400803static int _mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg, int offset,
Andrew Lunn3898c142015-05-06 01:09:53 +0200804 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700805{
806 unsigned long timeout = jiffies + HZ / 10;
807
808 while (time_before(jiffies, timeout)) {
809 int ret;
810
Andrew Lunn158bc062016-04-28 21:24:06 -0400811 ret = _mv88e6xxx_reg_read(ps, reg, offset);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700812 if (ret < 0)
813 return ret;
814 if (!(ret & mask))
815 return 0;
816
817 usleep_range(1000, 2000);
818 }
819 return -ETIMEDOUT;
820}
821
Andrew Lunn158bc062016-04-28 21:24:06 -0400822static int mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg,
823 int offset, u16 mask)
Andrew Lunn3898c142015-05-06 01:09:53 +0200824{
Andrew Lunn3898c142015-05-06 01:09:53 +0200825 int ret;
826
827 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -0400828 ret = _mv88e6xxx_wait(ps, reg, offset, mask);
Andrew Lunn3898c142015-05-06 01:09:53 +0200829 mutex_unlock(&ps->smi_mutex);
830
831 return ret;
832}
833
Andrew Lunn158bc062016-04-28 21:24:06 -0400834static int _mv88e6xxx_phy_wait(struct mv88e6xxx_priv_state *ps)
Andrew Lunn3898c142015-05-06 01:09:53 +0200835{
Andrew Lunn158bc062016-04-28 21:24:06 -0400836 return _mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200837 GLOBAL2_SMI_OP_BUSY);
838}
839
Vivien Didelotd24645b2016-05-09 13:22:41 -0400840static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200841{
Andrew Lunn158bc062016-04-28 21:24:06 -0400842 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
843
844 return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200845 GLOBAL2_EEPROM_OP_LOAD);
846}
847
Vivien Didelotd24645b2016-05-09 13:22:41 -0400848static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200849{
Andrew Lunn158bc062016-04-28 21:24:06 -0400850 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
851
852 return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200853 GLOBAL2_EEPROM_OP_BUSY);
854}
855
Vivien Didelotd24645b2016-05-09 13:22:41 -0400856static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr)
857{
858 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
859 int ret;
860
861 mutex_lock(&ps->eeprom_mutex);
862
863 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
864 GLOBAL2_EEPROM_OP_READ |
865 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
866 if (ret < 0)
867 goto error;
868
869 ret = mv88e6xxx_eeprom_busy_wait(ds);
870 if (ret < 0)
871 goto error;
872
873 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
874error:
875 mutex_unlock(&ps->eeprom_mutex);
876 return ret;
877}
878
879int mv88e6xxx_get_eeprom(struct dsa_switch *ds, struct ethtool_eeprom *eeprom,
880 u8 *data)
881{
882 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
883 int offset;
884 int len;
885 int ret;
886
887 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
888 return -EOPNOTSUPP;
889
890 offset = eeprom->offset;
891 len = eeprom->len;
892 eeprom->len = 0;
893
894 eeprom->magic = 0xc3ec4951;
895
896 ret = mv88e6xxx_eeprom_load_wait(ds);
897 if (ret < 0)
898 return ret;
899
900 if (offset & 1) {
901 int word;
902
903 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
904 if (word < 0)
905 return word;
906
907 *data++ = (word >> 8) & 0xff;
908
909 offset++;
910 len--;
911 eeprom->len++;
912 }
913
914 while (len >= 2) {
915 int word;
916
917 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
918 if (word < 0)
919 return word;
920
921 *data++ = word & 0xff;
922 *data++ = (word >> 8) & 0xff;
923
924 offset += 2;
925 len -= 2;
926 eeprom->len += 2;
927 }
928
929 if (len) {
930 int word;
931
932 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
933 if (word < 0)
934 return word;
935
936 *data++ = word & 0xff;
937
938 offset++;
939 len--;
940 eeprom->len++;
941 }
942
943 return 0;
944}
945
946static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds)
947{
948 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
949 int ret;
950
951 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
952 if (ret < 0)
953 return ret;
954
955 if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
956 return -EROFS;
957
958 return 0;
959}
960
961static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr,
962 u16 data)
963{
964 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
965 int ret;
966
967 mutex_lock(&ps->eeprom_mutex);
968
969 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
970 if (ret < 0)
971 goto error;
972
973 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
974 GLOBAL2_EEPROM_OP_WRITE |
975 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
976 if (ret < 0)
977 goto error;
978
979 ret = mv88e6xxx_eeprom_busy_wait(ds);
980error:
981 mutex_unlock(&ps->eeprom_mutex);
982 return ret;
983}
984
985int mv88e6xxx_set_eeprom(struct dsa_switch *ds, struct ethtool_eeprom *eeprom,
986 u8 *data)
987{
988 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
989 int offset;
990 int ret;
991 int len;
992
993 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
994 return -EOPNOTSUPP;
995
996 if (eeprom->magic != 0xc3ec4951)
997 return -EINVAL;
998
999 ret = mv88e6xxx_eeprom_is_readonly(ds);
1000 if (ret)
1001 return ret;
1002
1003 offset = eeprom->offset;
1004 len = eeprom->len;
1005 eeprom->len = 0;
1006
1007 ret = mv88e6xxx_eeprom_load_wait(ds);
1008 if (ret < 0)
1009 return ret;
1010
1011 if (offset & 1) {
1012 int word;
1013
1014 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1015 if (word < 0)
1016 return word;
1017
1018 word = (*data++ << 8) | (word & 0xff);
1019
1020 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1021 if (ret < 0)
1022 return ret;
1023
1024 offset++;
1025 len--;
1026 eeprom->len++;
1027 }
1028
1029 while (len >= 2) {
1030 int word;
1031
1032 word = *data++;
1033 word |= *data++ << 8;
1034
1035 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1036 if (ret < 0)
1037 return ret;
1038
1039 offset += 2;
1040 len -= 2;
1041 eeprom->len += 2;
1042 }
1043
1044 if (len) {
1045 int word;
1046
1047 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1048 if (word < 0)
1049 return word;
1050
1051 word = (word & 0xff00) | *data++;
1052
1053 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1054 if (ret < 0)
1055 return ret;
1056
1057 offset++;
1058 len--;
1059 eeprom->len++;
1060 }
1061
1062 return 0;
1063}
1064
Andrew Lunn158bc062016-04-28 21:24:06 -04001065static int _mv88e6xxx_atu_wait(struct mv88e6xxx_priv_state *ps)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001066{
Andrew Lunn158bc062016-04-28 21:24:06 -04001067 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_ATU_OP,
Andrew Lunncca8b132015-04-02 04:06:39 +02001068 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001069}
1070
Andrew Lunn158bc062016-04-28 21:24:06 -04001071static int _mv88e6xxx_phy_read_indirect(struct mv88e6xxx_priv_state *ps,
1072 int addr, int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +01001073{
1074 int ret;
1075
Andrew Lunn158bc062016-04-28 21:24:06 -04001076 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001077 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
1078 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +01001079 if (ret < 0)
1080 return ret;
1081
Andrew Lunn158bc062016-04-28 21:24:06 -04001082 ret = _mv88e6xxx_phy_wait(ps);
Andrew Lunn3898c142015-05-06 01:09:53 +02001083 if (ret < 0)
1084 return ret;
1085
Andrew Lunn158bc062016-04-28 21:24:06 -04001086 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA);
1087
1088 return ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001089}
1090
Andrew Lunn158bc062016-04-28 21:24:06 -04001091static int _mv88e6xxx_phy_write_indirect(struct mv88e6xxx_priv_state *ps,
1092 int addr, int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +01001093{
Andrew Lunn3898c142015-05-06 01:09:53 +02001094 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001095
Andrew Lunn158bc062016-04-28 21:24:06 -04001096 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02001097 if (ret < 0)
1098 return ret;
1099
Andrew Lunn158bc062016-04-28 21:24:06 -04001100 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001101 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
1102 regnum);
1103
Andrew Lunn158bc062016-04-28 21:24:06 -04001104 return _mv88e6xxx_phy_wait(ps);
Andrew Lunnf3044682015-02-14 19:17:50 +01001105}
1106
Guenter Roeck11b3b452015-03-06 22:23:51 -08001107int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
1108{
Andrew Lunn2f40c692015-04-02 04:06:37 +02001109 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001110 int reg;
1111
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001112 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1113 return -EOPNOTSUPP;
1114
Andrew Lunn3898c142015-05-06 01:09:53 +02001115 mutex_lock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001116
Andrew Lunn158bc062016-04-28 21:24:06 -04001117 reg = _mv88e6xxx_phy_read_indirect(ps, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001118 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001119 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001120
1121 e->eee_enabled = !!(reg & 0x0200);
1122 e->tx_lpi_enabled = !!(reg & 0x0100);
1123
Andrew Lunn158bc062016-04-28 21:24:06 -04001124 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001125 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001126 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001127
Andrew Lunncca8b132015-04-02 04:06:39 +02001128 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001129 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001130
Andrew Lunn2f40c692015-04-02 04:06:37 +02001131out:
Andrew Lunn3898c142015-05-06 01:09:53 +02001132 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001133 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001134}
1135
1136int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1137 struct phy_device *phydev, struct ethtool_eee *e)
1138{
Andrew Lunn2f40c692015-04-02 04:06:37 +02001139 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1140 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001141 int ret;
1142
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001143 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1144 return -EOPNOTSUPP;
1145
Andrew Lunn3898c142015-05-06 01:09:53 +02001146 mutex_lock(&ps->smi_mutex);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001147
Andrew Lunn158bc062016-04-28 21:24:06 -04001148 ret = _mv88e6xxx_phy_read_indirect(ps, port, 16);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001149 if (ret < 0)
1150 goto out;
1151
1152 reg = ret & ~0x0300;
1153 if (e->eee_enabled)
1154 reg |= 0x0200;
1155 if (e->tx_lpi_enabled)
1156 reg |= 0x0100;
1157
Andrew Lunn158bc062016-04-28 21:24:06 -04001158 ret = _mv88e6xxx_phy_write_indirect(ps, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001159out:
Andrew Lunn3898c142015-05-06 01:09:53 +02001160 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001161
1162 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001163}
1164
Andrew Lunn158bc062016-04-28 21:24:06 -04001165static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_priv_state *ps, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001166{
1167 int ret;
1168
Andrew Lunn158bc062016-04-28 21:24:06 -04001169 if (mv88e6xxx_has_fid_reg(ps)) {
1170 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_FID, fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001171 if (ret < 0)
1172 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001173 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001174 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001175 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -04001176 if (ret < 0)
1177 return ret;
1178
Andrew Lunn158bc062016-04-28 21:24:06 -04001179 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001180 (ret & 0xfff) |
1181 ((fid << 8) & 0xf000));
1182 if (ret < 0)
1183 return ret;
1184
1185 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1186 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001187 }
1188
Andrew Lunn158bc062016-04-28 21:24:06 -04001189 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001190 if (ret < 0)
1191 return ret;
1192
Andrew Lunn158bc062016-04-28 21:24:06 -04001193 return _mv88e6xxx_atu_wait(ps);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001194}
1195
Andrew Lunn158bc062016-04-28 21:24:06 -04001196static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot37705b72015-09-04 14:34:11 -04001197 struct mv88e6xxx_atu_entry *entry)
1198{
1199 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1200
1201 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1202 unsigned int mask, shift;
1203
1204 if (entry->trunk) {
1205 data |= GLOBAL_ATU_DATA_TRUNK;
1206 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1207 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1208 } else {
1209 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1210 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1211 }
1212
1213 data |= (entry->portv_trunkid << shift) & mask;
1214 }
1215
Andrew Lunn158bc062016-04-28 21:24:06 -04001216 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001217}
1218
Andrew Lunn158bc062016-04-28 21:24:06 -04001219static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001220 struct mv88e6xxx_atu_entry *entry,
1221 bool static_too)
1222{
1223 int op;
1224 int err;
1225
Andrew Lunn158bc062016-04-28 21:24:06 -04001226 err = _mv88e6xxx_atu_wait(ps);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001227 if (err)
1228 return err;
1229
Andrew Lunn158bc062016-04-28 21:24:06 -04001230 err = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001231 if (err)
1232 return err;
1233
1234 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001235 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1236 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1237 } else {
1238 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1239 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1240 }
1241
Andrew Lunn158bc062016-04-28 21:24:06 -04001242 return _mv88e6xxx_atu_cmd(ps, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001243}
1244
Andrew Lunn158bc062016-04-28 21:24:06 -04001245static int _mv88e6xxx_atu_flush(struct mv88e6xxx_priv_state *ps,
1246 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001247{
1248 struct mv88e6xxx_atu_entry entry = {
1249 .fid = fid,
1250 .state = 0, /* EntryState bits must be 0 */
1251 };
1252
Andrew Lunn158bc062016-04-28 21:24:06 -04001253 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001254}
1255
Andrew Lunn158bc062016-04-28 21:24:06 -04001256static int _mv88e6xxx_atu_move(struct mv88e6xxx_priv_state *ps, u16 fid,
1257 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001258{
1259 struct mv88e6xxx_atu_entry entry = {
1260 .trunk = false,
1261 .fid = fid,
1262 };
1263
1264 /* EntryState bits must be 0xF */
1265 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1266
1267 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1268 entry.portv_trunkid = (to_port & 0x0f) << 4;
1269 entry.portv_trunkid |= from_port & 0x0f;
1270
Andrew Lunn158bc062016-04-28 21:24:06 -04001271 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001272}
1273
Andrew Lunn158bc062016-04-28 21:24:06 -04001274static int _mv88e6xxx_atu_remove(struct mv88e6xxx_priv_state *ps, u16 fid,
1275 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001276{
1277 /* Destination port 0xF means remove the entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001278 return _mv88e6xxx_atu_move(ps, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001279}
1280
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001281static const char * const mv88e6xxx_port_state_names[] = {
1282 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1283 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1284 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1285 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1286};
1287
Andrew Lunn158bc062016-04-28 21:24:06 -04001288static int _mv88e6xxx_port_state(struct mv88e6xxx_priv_state *ps, int port,
1289 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001290{
Andrew Lunn158bc062016-04-28 21:24:06 -04001291 struct dsa_switch *ds = ps->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001292 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001293 u8 oldstate;
1294
Andrew Lunn158bc062016-04-28 21:24:06 -04001295 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001296 if (reg < 0)
1297 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001298
Andrew Lunncca8b132015-04-02 04:06:39 +02001299 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001300
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001301 if (oldstate != state) {
1302 /* Flush forwarding database if we're moving a port
1303 * from Learning or Forwarding state to Disabled or
1304 * Blocking or Listening state.
1305 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001306 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1307 oldstate == PORT_CONTROL_STATE_FORWARDING)
1308 && (state == PORT_CONTROL_STATE_DISABLED ||
1309 state == PORT_CONTROL_STATE_BLOCKING)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001310 ret = _mv88e6xxx_atu_remove(ps, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001311 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001312 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001313 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001314
Andrew Lunncca8b132015-04-02 04:06:39 +02001315 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Andrew Lunn158bc062016-04-28 21:24:06 -04001316 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001317 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001318 if (ret)
1319 return ret;
1320
1321 netdev_dbg(ds->ports[port], "PortState %s (was %s)\n",
1322 mv88e6xxx_port_state_names[state],
1323 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001324 }
1325
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001326 return ret;
1327}
1328
Andrew Lunn158bc062016-04-28 21:24:06 -04001329static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_priv_state *ps,
1330 int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001331{
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001332 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot009a2b92016-04-17 13:24:01 -04001333 const u16 mask = (1 << ps->info->num_ports) - 1;
Andrew Lunn158bc062016-04-28 21:24:06 -04001334 struct dsa_switch *ds = ps->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001335 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001336 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001337 int i;
1338
1339 /* allow CPU port or DSA link(s) to send frames to every port */
1340 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1341 output_ports = mask;
1342 } else {
Vivien Didelot009a2b92016-04-17 13:24:01 -04001343 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001344 /* allow sending frames to every group member */
1345 if (bridge && ps->ports[i].bridge_dev == bridge)
1346 output_ports |= BIT(i);
1347
1348 /* allow sending frames to CPU port and DSA link(s) */
1349 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1350 output_ports |= BIT(i);
1351 }
1352 }
1353
1354 /* prevent frames from going back out of the port they came in on */
1355 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001356
Andrew Lunn158bc062016-04-28 21:24:06 -04001357 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001358 if (reg < 0)
1359 return reg;
1360
1361 reg &= ~mask;
1362 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001363
Andrew Lunn158bc062016-04-28 21:24:06 -04001364 return _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001365}
1366
Vivien Didelot43c44a92016-04-06 11:55:03 -04001367void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001368{
1369 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1370 int stp_state;
1371
Vivien Didelot936f2342016-05-09 13:22:46 -04001372 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_PORTSTATE))
1373 return;
1374
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001375 switch (state) {
1376 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001377 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001378 break;
1379 case BR_STATE_BLOCKING:
1380 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001381 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001382 break;
1383 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001384 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001385 break;
1386 case BR_STATE_FORWARDING:
1387 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001388 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001389 break;
1390 }
1391
Vivien Didelot43c44a92016-04-06 11:55:03 -04001392 /* mv88e6xxx_port_stp_state_set may be called with softirqs disabled,
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001393 * so we can not update the port state directly but need to schedule it.
1394 */
Vivien Didelotd715fa62016-02-12 12:09:38 -05001395 ps->ports[port].state = stp_state;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001396 set_bit(port, ps->port_state_update_mask);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001397 schedule_work(&ps->bridge_work);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001398}
1399
Andrew Lunn158bc062016-04-28 21:24:06 -04001400static int _mv88e6xxx_port_pvid(struct mv88e6xxx_priv_state *ps, int port,
1401 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001402{
Andrew Lunn158bc062016-04-28 21:24:06 -04001403 struct dsa_switch *ds = ps->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001404 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001405 int ret;
1406
Andrew Lunn158bc062016-04-28 21:24:06 -04001407 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001408 if (ret < 0)
1409 return ret;
1410
Vivien Didelot5da96032016-03-07 18:24:39 -05001411 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1412
1413 if (new) {
1414 ret &= ~PORT_DEFAULT_VLAN_MASK;
1415 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1416
Andrew Lunn158bc062016-04-28 21:24:06 -04001417 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001418 PORT_DEFAULT_VLAN, ret);
1419 if (ret < 0)
1420 return ret;
1421
1422 netdev_dbg(ds->ports[port], "DefaultVID %d (was %d)\n", *new,
1423 pvid);
1424 }
1425
1426 if (old)
1427 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001428
1429 return 0;
1430}
1431
Andrew Lunn158bc062016-04-28 21:24:06 -04001432static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_priv_state *ps,
1433 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001434{
Andrew Lunn158bc062016-04-28 21:24:06 -04001435 return _mv88e6xxx_port_pvid(ps, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001436}
1437
Andrew Lunn158bc062016-04-28 21:24:06 -04001438static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_priv_state *ps,
1439 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001440{
Andrew Lunn158bc062016-04-28 21:24:06 -04001441 return _mv88e6xxx_port_pvid(ps, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001442}
1443
Andrew Lunn158bc062016-04-28 21:24:06 -04001444static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001445{
Andrew Lunn158bc062016-04-28 21:24:06 -04001446 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_VTU_OP,
Vivien Didelot6b17e862015-08-13 12:52:18 -04001447 GLOBAL_VTU_OP_BUSY);
1448}
1449
Andrew Lunn158bc062016-04-28 21:24:06 -04001450static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_priv_state *ps, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001451{
1452 int ret;
1453
Andrew Lunn158bc062016-04-28 21:24:06 -04001454 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001455 if (ret < 0)
1456 return ret;
1457
Andrew Lunn158bc062016-04-28 21:24:06 -04001458 return _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001459}
1460
Andrew Lunn158bc062016-04-28 21:24:06 -04001461static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001462{
1463 int ret;
1464
Andrew Lunn158bc062016-04-28 21:24:06 -04001465 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001466 if (ret < 0)
1467 return ret;
1468
Andrew Lunn158bc062016-04-28 21:24:06 -04001469 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001470}
1471
Andrew Lunn158bc062016-04-28 21:24:06 -04001472static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001473 struct mv88e6xxx_vtu_stu_entry *entry,
1474 unsigned int nibble_offset)
1475{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001476 u16 regs[3];
1477 int i;
1478 int ret;
1479
1480 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001481 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001482 GLOBAL_VTU_DATA_0_3 + i);
1483 if (ret < 0)
1484 return ret;
1485
1486 regs[i] = ret;
1487 }
1488
Vivien Didelot009a2b92016-04-17 13:24:01 -04001489 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001490 unsigned int shift = (i % 4) * 4 + nibble_offset;
1491 u16 reg = regs[i / 4];
1492
1493 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1494 }
1495
1496 return 0;
1497}
1498
Andrew Lunn158bc062016-04-28 21:24:06 -04001499static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001500 struct mv88e6xxx_vtu_stu_entry *entry,
1501 unsigned int nibble_offset)
1502{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001503 u16 regs[3] = { 0 };
1504 int i;
1505 int ret;
1506
Vivien Didelot009a2b92016-04-17 13:24:01 -04001507 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001508 unsigned int shift = (i % 4) * 4 + nibble_offset;
1509 u8 data = entry->data[i];
1510
1511 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1512 }
1513
1514 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001515 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001516 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1517 if (ret < 0)
1518 return ret;
1519 }
1520
1521 return 0;
1522}
1523
Andrew Lunn158bc062016-04-28 21:24:06 -04001524static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_priv_state *ps, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001525{
Andrew Lunn158bc062016-04-28 21:24:06 -04001526 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001527 vid & GLOBAL_VTU_VID_MASK);
1528}
1529
Andrew Lunn158bc062016-04-28 21:24:06 -04001530static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001531 struct mv88e6xxx_vtu_stu_entry *entry)
1532{
1533 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1534 int ret;
1535
Andrew Lunn158bc062016-04-28 21:24:06 -04001536 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001537 if (ret < 0)
1538 return ret;
1539
Andrew Lunn158bc062016-04-28 21:24:06 -04001540 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001541 if (ret < 0)
1542 return ret;
1543
Andrew Lunn158bc062016-04-28 21:24:06 -04001544 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001545 if (ret < 0)
1546 return ret;
1547
1548 next.vid = ret & GLOBAL_VTU_VID_MASK;
1549 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1550
1551 if (next.valid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001552 ret = _mv88e6xxx_vtu_stu_data_read(ps, &next, 0);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001553 if (ret < 0)
1554 return ret;
1555
Andrew Lunn158bc062016-04-28 21:24:06 -04001556 if (mv88e6xxx_has_fid_reg(ps)) {
1557 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001558 GLOBAL_VTU_FID);
1559 if (ret < 0)
1560 return ret;
1561
1562 next.fid = ret & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001563 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001564 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1565 * VTU DBNum[3:0] are located in VTU Operation 3:0
1566 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001567 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001568 GLOBAL_VTU_OP);
1569 if (ret < 0)
1570 return ret;
1571
1572 next.fid = (ret & 0xf00) >> 4;
1573 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001574 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001575
Andrew Lunn158bc062016-04-28 21:24:06 -04001576 if (mv88e6xxx_has_stu(ps)) {
1577 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001578 GLOBAL_VTU_SID);
1579 if (ret < 0)
1580 return ret;
1581
1582 next.sid = ret & GLOBAL_VTU_SID_MASK;
1583 }
1584 }
1585
1586 *entry = next;
1587 return 0;
1588}
1589
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001590int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1591 struct switchdev_obj_port_vlan *vlan,
1592 int (*cb)(struct switchdev_obj *obj))
1593{
1594 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1595 struct mv88e6xxx_vtu_stu_entry next;
1596 u16 pvid;
1597 int err;
1598
Vivien Didelot54d77b52016-05-09 13:22:47 -04001599 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
1600 return -EOPNOTSUPP;
1601
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001602 mutex_lock(&ps->smi_mutex);
1603
Andrew Lunn158bc062016-04-28 21:24:06 -04001604 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001605 if (err)
1606 goto unlock;
1607
Andrew Lunn158bc062016-04-28 21:24:06 -04001608 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001609 if (err)
1610 goto unlock;
1611
1612 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001613 err = _mv88e6xxx_vtu_getnext(ps, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001614 if (err)
1615 break;
1616
1617 if (!next.valid)
1618 break;
1619
1620 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1621 continue;
1622
1623 /* reinit and dump this VLAN obj */
1624 vlan->vid_begin = vlan->vid_end = next.vid;
1625 vlan->flags = 0;
1626
1627 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1628 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1629
1630 if (next.vid == pvid)
1631 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1632
1633 err = cb(&vlan->obj);
1634 if (err)
1635 break;
1636 } while (next.vid < GLOBAL_VTU_VID_MASK);
1637
1638unlock:
1639 mutex_unlock(&ps->smi_mutex);
1640
1641 return err;
1642}
1643
Andrew Lunn158bc062016-04-28 21:24:06 -04001644static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001645 struct mv88e6xxx_vtu_stu_entry *entry)
1646{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001647 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001648 u16 reg = 0;
1649 int ret;
1650
Andrew Lunn158bc062016-04-28 21:24:06 -04001651 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001652 if (ret < 0)
1653 return ret;
1654
1655 if (!entry->valid)
1656 goto loadpurge;
1657
1658 /* Write port member tags */
Andrew Lunn158bc062016-04-28 21:24:06 -04001659 ret = _mv88e6xxx_vtu_stu_data_write(ps, entry, 0);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001660 if (ret < 0)
1661 return ret;
1662
Andrew Lunn158bc062016-04-28 21:24:06 -04001663 if (mv88e6xxx_has_stu(ps)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001664 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001665 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001666 if (ret < 0)
1667 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001668 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001669
Andrew Lunn158bc062016-04-28 21:24:06 -04001670 if (mv88e6xxx_has_fid_reg(ps)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001671 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001672 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_FID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001673 if (ret < 0)
1674 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001675 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001676 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1677 * VTU DBNum[3:0] are located in VTU Operation 3:0
1678 */
1679 op |= (entry->fid & 0xf0) << 8;
1680 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001681 }
1682
1683 reg = GLOBAL_VTU_VID_VALID;
1684loadpurge:
1685 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001686 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001687 if (ret < 0)
1688 return ret;
1689
Andrew Lunn158bc062016-04-28 21:24:06 -04001690 return _mv88e6xxx_vtu_cmd(ps, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001691}
1692
Andrew Lunn158bc062016-04-28 21:24:06 -04001693static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_priv_state *ps, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001694 struct mv88e6xxx_vtu_stu_entry *entry)
1695{
1696 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1697 int ret;
1698
Andrew Lunn158bc062016-04-28 21:24:06 -04001699 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001700 if (ret < 0)
1701 return ret;
1702
Andrew Lunn158bc062016-04-28 21:24:06 -04001703 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001704 sid & GLOBAL_VTU_SID_MASK);
1705 if (ret < 0)
1706 return ret;
1707
Andrew Lunn158bc062016-04-28 21:24:06 -04001708 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001709 if (ret < 0)
1710 return ret;
1711
Andrew Lunn158bc062016-04-28 21:24:06 -04001712 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001713 if (ret < 0)
1714 return ret;
1715
1716 next.sid = ret & GLOBAL_VTU_SID_MASK;
1717
Andrew Lunn158bc062016-04-28 21:24:06 -04001718 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001719 if (ret < 0)
1720 return ret;
1721
1722 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1723
1724 if (next.valid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001725 ret = _mv88e6xxx_vtu_stu_data_read(ps, &next, 2);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001726 if (ret < 0)
1727 return ret;
1728 }
1729
1730 *entry = next;
1731 return 0;
1732}
1733
Andrew Lunn158bc062016-04-28 21:24:06 -04001734static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001735 struct mv88e6xxx_vtu_stu_entry *entry)
1736{
1737 u16 reg = 0;
1738 int ret;
1739
Andrew Lunn158bc062016-04-28 21:24:06 -04001740 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001741 if (ret < 0)
1742 return ret;
1743
1744 if (!entry->valid)
1745 goto loadpurge;
1746
1747 /* Write port states */
Andrew Lunn158bc062016-04-28 21:24:06 -04001748 ret = _mv88e6xxx_vtu_stu_data_write(ps, entry, 2);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001749 if (ret < 0)
1750 return ret;
1751
1752 reg = GLOBAL_VTU_VID_VALID;
1753loadpurge:
Andrew Lunn158bc062016-04-28 21:24:06 -04001754 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001755 if (ret < 0)
1756 return ret;
1757
1758 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001759 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001760 if (ret < 0)
1761 return ret;
1762
Andrew Lunn158bc062016-04-28 21:24:06 -04001763 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001764}
1765
Andrew Lunn158bc062016-04-28 21:24:06 -04001766static int _mv88e6xxx_port_fid(struct mv88e6xxx_priv_state *ps, int port,
1767 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001768{
Andrew Lunn158bc062016-04-28 21:24:06 -04001769 struct dsa_switch *ds = ps->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001770 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001771 u16 fid;
1772 int ret;
1773
Andrew Lunn158bc062016-04-28 21:24:06 -04001774 if (mv88e6xxx_num_databases(ps) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001775 upper_mask = 0xff;
Andrew Lunn158bc062016-04-28 21:24:06 -04001776 else if (mv88e6xxx_num_databases(ps) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001777 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001778 else
1779 return -EOPNOTSUPP;
1780
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001781 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001782 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001783 if (ret < 0)
1784 return ret;
1785
1786 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1787
1788 if (new) {
1789 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1790 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1791
Andrew Lunn158bc062016-04-28 21:24:06 -04001792 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001793 ret);
1794 if (ret < 0)
1795 return ret;
1796 }
1797
1798 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001799 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001800 if (ret < 0)
1801 return ret;
1802
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001803 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001804
1805 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001806 ret &= ~upper_mask;
1807 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001808
Andrew Lunn158bc062016-04-28 21:24:06 -04001809 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001810 ret);
1811 if (ret < 0)
1812 return ret;
1813
1814 netdev_dbg(ds->ports[port], "FID %d (was %d)\n", *new, fid);
1815 }
1816
1817 if (old)
1818 *old = fid;
1819
1820 return 0;
1821}
1822
Andrew Lunn158bc062016-04-28 21:24:06 -04001823static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_priv_state *ps,
1824 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001825{
Andrew Lunn158bc062016-04-28 21:24:06 -04001826 return _mv88e6xxx_port_fid(ps, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001827}
1828
Andrew Lunn158bc062016-04-28 21:24:06 -04001829static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_priv_state *ps,
1830 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001831{
Andrew Lunn158bc062016-04-28 21:24:06 -04001832 return _mv88e6xxx_port_fid(ps, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001833}
1834
Andrew Lunn158bc062016-04-28 21:24:06 -04001835static int _mv88e6xxx_fid_new(struct mv88e6xxx_priv_state *ps, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001836{
1837 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1838 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001839 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001840
1841 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1842
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001843 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001844 for (i = 0; i < ps->info->num_ports; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001845 err = _mv88e6xxx_port_fid_get(ps, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001846 if (err)
1847 return err;
1848
1849 set_bit(*fid, fid_bitmap);
1850 }
1851
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001852 /* Set every FID bit used by the VLAN entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001853 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001854 if (err)
1855 return err;
1856
1857 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001858 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001859 if (err)
1860 return err;
1861
1862 if (!vlan.valid)
1863 break;
1864
1865 set_bit(vlan.fid, fid_bitmap);
1866 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1867
1868 /* The reset value 0x000 is used to indicate that multiple address
1869 * databases are not needed. Return the next positive available.
1870 */
1871 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Andrew Lunn158bc062016-04-28 21:24:06 -04001872 if (unlikely(*fid >= mv88e6xxx_num_databases(ps)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001873 return -ENOSPC;
1874
1875 /* Clear the database */
Andrew Lunn158bc062016-04-28 21:24:06 -04001876 return _mv88e6xxx_atu_flush(ps, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001877}
1878
Andrew Lunn158bc062016-04-28 21:24:06 -04001879static int _mv88e6xxx_vtu_new(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001880 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001881{
Andrew Lunn158bc062016-04-28 21:24:06 -04001882 struct dsa_switch *ds = ps->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001883 struct mv88e6xxx_vtu_stu_entry vlan = {
1884 .valid = true,
1885 .vid = vid,
1886 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001887 int i, err;
1888
Andrew Lunn158bc062016-04-28 21:24:06 -04001889 err = _mv88e6xxx_fid_new(ps, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001890 if (err)
1891 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001892
Vivien Didelot3d131f02015-11-03 10:52:52 -05001893 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001894 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001895 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1896 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1897 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001898
Andrew Lunn158bc062016-04-28 21:24:06 -04001899 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
1900 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001901 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001902
1903 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1904 * implemented, only one STU entry is needed to cover all VTU
1905 * entries. Thus, validate the SID 0.
1906 */
1907 vlan.sid = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04001908 err = _mv88e6xxx_stu_getnext(ps, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001909 if (err)
1910 return err;
1911
1912 if (vstp.sid != vlan.sid || !vstp.valid) {
1913 memset(&vstp, 0, sizeof(vstp));
1914 vstp.valid = true;
1915 vstp.sid = vlan.sid;
1916
Andrew Lunn158bc062016-04-28 21:24:06 -04001917 err = _mv88e6xxx_stu_loadpurge(ps, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001918 if (err)
1919 return err;
1920 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001921 }
1922
1923 *entry = vlan;
1924 return 0;
1925}
1926
Andrew Lunn158bc062016-04-28 21:24:06 -04001927static int _mv88e6xxx_vtu_get(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001928 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1929{
1930 int err;
1931
1932 if (!vid)
1933 return -EINVAL;
1934
Andrew Lunn158bc062016-04-28 21:24:06 -04001935 err = _mv88e6xxx_vtu_vid_write(ps, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001936 if (err)
1937 return err;
1938
Andrew Lunn158bc062016-04-28 21:24:06 -04001939 err = _mv88e6xxx_vtu_getnext(ps, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001940 if (err)
1941 return err;
1942
1943 if (entry->vid != vid || !entry->valid) {
1944 if (!creat)
1945 return -EOPNOTSUPP;
1946 /* -ENOENT would've been more appropriate, but switchdev expects
1947 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1948 */
1949
Andrew Lunn158bc062016-04-28 21:24:06 -04001950 err = _mv88e6xxx_vtu_new(ps, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001951 }
1952
1953 return err;
1954}
1955
Vivien Didelotda9c3592016-02-12 12:09:40 -05001956static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1957 u16 vid_begin, u16 vid_end)
1958{
1959 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1960 struct mv88e6xxx_vtu_stu_entry vlan;
1961 int i, err;
1962
1963 if (!vid_begin)
1964 return -EOPNOTSUPP;
1965
1966 mutex_lock(&ps->smi_mutex);
1967
Andrew Lunn158bc062016-04-28 21:24:06 -04001968 err = _mv88e6xxx_vtu_vid_write(ps, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001969 if (err)
1970 goto unlock;
1971
1972 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001973 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001974 if (err)
1975 goto unlock;
1976
1977 if (!vlan.valid)
1978 break;
1979
1980 if (vlan.vid > vid_end)
1981 break;
1982
Vivien Didelot009a2b92016-04-17 13:24:01 -04001983 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001984 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1985 continue;
1986
1987 if (vlan.data[i] ==
1988 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1989 continue;
1990
1991 if (ps->ports[i].bridge_dev ==
1992 ps->ports[port].bridge_dev)
1993 break; /* same bridge, check next VLAN */
1994
1995 netdev_warn(ds->ports[port],
1996 "hardware VLAN %d already used by %s\n",
1997 vlan.vid,
1998 netdev_name(ps->ports[i].bridge_dev));
1999 err = -EOPNOTSUPP;
2000 goto unlock;
2001 }
2002 } while (vlan.vid < vid_end);
2003
2004unlock:
2005 mutex_unlock(&ps->smi_mutex);
2006
2007 return err;
2008}
2009
Vivien Didelot214cdb92016-02-26 13:16:08 -05002010static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2011 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2012 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2013 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2014 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2015};
2016
2017int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2018 bool vlan_filtering)
2019{
2020 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2021 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2022 PORT_CONTROL_2_8021Q_DISABLED;
2023 int ret;
2024
Vivien Didelot54d77b52016-05-09 13:22:47 -04002025 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2026 return -EOPNOTSUPP;
2027
Vivien Didelot214cdb92016-02-26 13:16:08 -05002028 mutex_lock(&ps->smi_mutex);
2029
Andrew Lunn158bc062016-04-28 21:24:06 -04002030 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002031 if (ret < 0)
2032 goto unlock;
2033
2034 old = ret & PORT_CONTROL_2_8021Q_MASK;
2035
Vivien Didelot5220ef12016-03-07 18:24:52 -05002036 if (new != old) {
2037 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2038 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002039
Andrew Lunn158bc062016-04-28 21:24:06 -04002040 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05002041 ret);
2042 if (ret < 0)
2043 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002044
Vivien Didelot5220ef12016-03-07 18:24:52 -05002045 netdev_dbg(ds->ports[port], "802.1Q Mode %s (was %s)\n",
2046 mv88e6xxx_port_8021q_mode_names[new],
2047 mv88e6xxx_port_8021q_mode_names[old]);
2048 }
2049
2050 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002051unlock:
2052 mutex_unlock(&ps->smi_mutex);
2053
2054 return ret;
2055}
2056
Vivien Didelot76e398a2015-11-01 12:33:55 -05002057int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2058 const struct switchdev_obj_port_vlan *vlan,
2059 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002060{
Vivien Didelot54d77b52016-05-09 13:22:47 -04002061 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002062 int err;
2063
Vivien Didelot54d77b52016-05-09 13:22:47 -04002064 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2065 return -EOPNOTSUPP;
2066
Vivien Didelotda9c3592016-02-12 12:09:40 -05002067 /* If the requested port doesn't belong to the same bridge as the VLAN
2068 * members, do not support it (yet) and fallback to software VLAN.
2069 */
2070 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2071 vlan->vid_end);
2072 if (err)
2073 return err;
2074
Vivien Didelot76e398a2015-11-01 12:33:55 -05002075 /* We don't need any dynamic resource from the kernel (yet),
2076 * so skip the prepare phase.
2077 */
2078 return 0;
2079}
2080
Andrew Lunn158bc062016-04-28 21:24:06 -04002081static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_priv_state *ps, int port,
2082 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002083{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002084 struct mv88e6xxx_vtu_stu_entry vlan;
2085 int err;
2086
Andrew Lunn158bc062016-04-28 21:24:06 -04002087 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002088 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002089 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002090
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002091 vlan.data[port] = untagged ?
2092 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2093 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2094
Andrew Lunn158bc062016-04-28 21:24:06 -04002095 return _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002096}
2097
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002098void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2099 const struct switchdev_obj_port_vlan *vlan,
2100 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002101{
2102 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2103 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2104 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2105 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002106
Vivien Didelot54d77b52016-05-09 13:22:47 -04002107 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2108 return;
2109
Vivien Didelot76e398a2015-11-01 12:33:55 -05002110 mutex_lock(&ps->smi_mutex);
2111
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002112 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Andrew Lunn158bc062016-04-28 21:24:06 -04002113 if (_mv88e6xxx_port_vlan_add(ps, port, vid, untagged))
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002114 netdev_err(ds->ports[port], "failed to add VLAN %d%c\n",
2115 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05002116
Andrew Lunn158bc062016-04-28 21:24:06 -04002117 if (pvid && _mv88e6xxx_port_pvid_set(ps, port, vlan->vid_end))
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002118 netdev_err(ds->ports[port], "failed to set PVID %d\n",
2119 vlan->vid_end);
2120
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002121 mutex_unlock(&ps->smi_mutex);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002122}
2123
Andrew Lunn158bc062016-04-28 21:24:06 -04002124static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_priv_state *ps,
2125 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002126{
Andrew Lunn158bc062016-04-28 21:24:06 -04002127 struct dsa_switch *ds = ps->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002128 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002129 int i, err;
2130
Andrew Lunn158bc062016-04-28 21:24:06 -04002131 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002132 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002133 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002134
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002135 /* Tell switchdev if this VLAN is handled in software */
2136 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05002137 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002138
2139 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2140
2141 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002142 vlan.valid = false;
Vivien Didelot009a2b92016-04-17 13:24:01 -04002143 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05002144 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002145 continue;
2146
2147 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002148 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002149 break;
2150 }
2151 }
2152
Andrew Lunn158bc062016-04-28 21:24:06 -04002153 err = _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002154 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002155 return err;
2156
Andrew Lunn158bc062016-04-28 21:24:06 -04002157 return _mv88e6xxx_atu_remove(ps, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002158}
2159
2160int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2161 const struct switchdev_obj_port_vlan *vlan)
2162{
2163 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2164 u16 pvid, vid;
2165 int err = 0;
2166
Vivien Didelot54d77b52016-05-09 13:22:47 -04002167 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2168 return -EOPNOTSUPP;
2169
Vivien Didelot76e398a2015-11-01 12:33:55 -05002170 mutex_lock(&ps->smi_mutex);
2171
Andrew Lunn158bc062016-04-28 21:24:06 -04002172 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002173 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002174 goto unlock;
2175
Vivien Didelot76e398a2015-11-01 12:33:55 -05002176 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002177 err = _mv88e6xxx_port_vlan_del(ps, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002178 if (err)
2179 goto unlock;
2180
2181 if (vid == pvid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002182 err = _mv88e6xxx_port_pvid_set(ps, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002183 if (err)
2184 goto unlock;
2185 }
2186 }
2187
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002188unlock:
2189 mutex_unlock(&ps->smi_mutex);
2190
2191 return err;
2192}
2193
Andrew Lunn158bc062016-04-28 21:24:06 -04002194static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002195 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002196{
2197 int i, ret;
2198
2199 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002200 ret = _mv88e6xxx_reg_write(
Andrew Lunn158bc062016-04-28 21:24:06 -04002201 ps, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002202 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002203 if (ret < 0)
2204 return ret;
2205 }
2206
2207 return 0;
2208}
2209
Andrew Lunn158bc062016-04-28 21:24:06 -04002210static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_priv_state *ps,
2211 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002212{
2213 int i, ret;
2214
2215 for (i = 0; i < 3; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002216 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002217 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002218 if (ret < 0)
2219 return ret;
2220 addr[i * 2] = ret >> 8;
2221 addr[i * 2 + 1] = ret & 0xff;
2222 }
2223
2224 return 0;
2225}
2226
Andrew Lunn158bc062016-04-28 21:24:06 -04002227static int _mv88e6xxx_atu_load(struct mv88e6xxx_priv_state *ps,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002228 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002229{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002230 int ret;
2231
Andrew Lunn158bc062016-04-28 21:24:06 -04002232 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002233 if (ret < 0)
2234 return ret;
2235
Andrew Lunn158bc062016-04-28 21:24:06 -04002236 ret = _mv88e6xxx_atu_mac_write(ps, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002237 if (ret < 0)
2238 return ret;
2239
Andrew Lunn158bc062016-04-28 21:24:06 -04002240 ret = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002241 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002242 return ret;
2243
Andrew Lunn158bc062016-04-28 21:24:06 -04002244 return _mv88e6xxx_atu_cmd(ps, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002245}
David S. Millercdf09692015-08-11 12:00:37 -07002246
Andrew Lunn158bc062016-04-28 21:24:06 -04002247static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_priv_state *ps, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002248 const unsigned char *addr, u16 vid,
2249 u8 state)
2250{
2251 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002252 struct mv88e6xxx_vtu_stu_entry vlan;
2253 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002254
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002255 /* Null VLAN ID corresponds to the port private database */
2256 if (vid == 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04002257 err = _mv88e6xxx_port_fid_get(ps, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002258 else
Andrew Lunn158bc062016-04-28 21:24:06 -04002259 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002260 if (err)
2261 return err;
2262
2263 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002264 entry.state = state;
2265 ether_addr_copy(entry.mac, addr);
2266 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2267 entry.trunk = false;
2268 entry.portv_trunkid = BIT(port);
2269 }
2270
Andrew Lunn158bc062016-04-28 21:24:06 -04002271 return _mv88e6xxx_atu_load(ps, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002272}
2273
Vivien Didelot146a3202015-10-08 11:35:12 -04002274int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2275 const struct switchdev_obj_port_fdb *fdb,
2276 struct switchdev_trans *trans)
2277{
2278 /* We don't need any dynamic resource from the kernel (yet),
2279 * so skip the prepare phase.
2280 */
2281 return 0;
2282}
2283
Vivien Didelot8497aa62016-04-06 11:55:04 -04002284void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2285 const struct switchdev_obj_port_fdb *fdb,
2286 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002287{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002288 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002289 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2290 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2291 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002292
David S. Millercdf09692015-08-11 12:00:37 -07002293 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04002294 if (_mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, state))
Vivien Didelot8497aa62016-04-06 11:55:04 -04002295 netdev_err(ds->ports[port], "failed to load MAC address\n");
David S. Millercdf09692015-08-11 12:00:37 -07002296 mutex_unlock(&ps->smi_mutex);
David S. Millercdf09692015-08-11 12:00:37 -07002297}
2298
2299int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
Vivien Didelot8057b3e2015-10-08 11:35:14 -04002300 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002301{
2302 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2303 int ret;
2304
2305 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04002306 ret = _mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002307 GLOBAL_ATU_DATA_STATE_UNUSED);
2308 mutex_unlock(&ps->smi_mutex);
2309
2310 return ret;
2311}
2312
Andrew Lunn158bc062016-04-28 21:24:06 -04002313static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_priv_state *ps, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002314 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002315{
Vivien Didelot1d194042015-08-10 09:09:51 -04002316 struct mv88e6xxx_atu_entry next = { 0 };
2317 int ret;
2318
2319 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002320
Andrew Lunn158bc062016-04-28 21:24:06 -04002321 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002322 if (ret < 0)
2323 return ret;
2324
Andrew Lunn158bc062016-04-28 21:24:06 -04002325 ret = _mv88e6xxx_atu_cmd(ps, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002326 if (ret < 0)
2327 return ret;
2328
Andrew Lunn158bc062016-04-28 21:24:06 -04002329 ret = _mv88e6xxx_atu_mac_read(ps, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002330 if (ret < 0)
2331 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002332
Andrew Lunn158bc062016-04-28 21:24:06 -04002333 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002334 if (ret < 0)
2335 return ret;
2336
2337 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2338 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2339 unsigned int mask, shift;
2340
2341 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2342 next.trunk = true;
2343 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2344 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2345 } else {
2346 next.trunk = false;
2347 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2348 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2349 }
2350
2351 next.portv_trunkid = (ret & mask) >> shift;
2352 }
2353
2354 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002355 return 0;
2356}
2357
Andrew Lunn158bc062016-04-28 21:24:06 -04002358static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_priv_state *ps,
2359 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002360 struct switchdev_obj_port_fdb *fdb,
2361 int (*cb)(struct switchdev_obj *obj))
2362{
2363 struct mv88e6xxx_atu_entry addr = {
2364 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2365 };
2366 int err;
2367
Andrew Lunn158bc062016-04-28 21:24:06 -04002368 err = _mv88e6xxx_atu_mac_write(ps, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002369 if (err)
2370 return err;
2371
2372 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002373 err = _mv88e6xxx_atu_getnext(ps, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002374 if (err)
2375 break;
2376
2377 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2378 break;
2379
2380 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2381 bool is_static = addr.state ==
2382 (is_multicast_ether_addr(addr.mac) ?
2383 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2384 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2385
2386 fdb->vid = vid;
2387 ether_addr_copy(fdb->addr, addr.mac);
2388 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2389
2390 err = cb(&fdb->obj);
2391 if (err)
2392 break;
2393 }
2394 } while (!is_broadcast_ether_addr(addr.mac));
2395
2396 return err;
2397}
2398
Vivien Didelotf33475b2015-10-22 09:34:41 -04002399int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2400 struct switchdev_obj_port_fdb *fdb,
2401 int (*cb)(struct switchdev_obj *obj))
2402{
2403 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2404 struct mv88e6xxx_vtu_stu_entry vlan = {
2405 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2406 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002407 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002408 int err;
2409
2410 mutex_lock(&ps->smi_mutex);
2411
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002412 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Andrew Lunn158bc062016-04-28 21:24:06 -04002413 err = _mv88e6xxx_port_fid_get(ps, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002414 if (err)
2415 goto unlock;
2416
Andrew Lunn158bc062016-04-28 21:24:06 -04002417 err = _mv88e6xxx_port_fdb_dump_one(ps, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002418 if (err)
2419 goto unlock;
2420
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002421 /* Dump VLANs' Filtering Information Databases */
Andrew Lunn158bc062016-04-28 21:24:06 -04002422 err = _mv88e6xxx_vtu_vid_write(ps, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002423 if (err)
2424 goto unlock;
2425
2426 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002427 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002428 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002429 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002430
2431 if (!vlan.valid)
2432 break;
2433
Andrew Lunn158bc062016-04-28 21:24:06 -04002434 err = _mv88e6xxx_port_fdb_dump_one(ps, vlan.fid, vlan.vid, port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002435 fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002436 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002437 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002438 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2439
2440unlock:
2441 mutex_unlock(&ps->smi_mutex);
2442
2443 return err;
2444}
2445
Vivien Didelota6692752016-02-12 12:09:39 -05002446int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2447 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002448{
Vivien Didelota6692752016-02-12 12:09:39 -05002449 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002450 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002451
Vivien Didelot936f2342016-05-09 13:22:46 -04002452 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2453 return -EOPNOTSUPP;
2454
Vivien Didelot466dfa02016-02-26 13:16:05 -05002455 mutex_lock(&ps->smi_mutex);
2456
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002457 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002458 ps->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002459
Vivien Didelot009a2b92016-04-17 13:24:01 -04002460 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002461 if (ps->ports[i].bridge_dev == bridge) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002462 err = _mv88e6xxx_port_based_vlan_map(ps, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002463 if (err)
2464 break;
2465 }
2466 }
2467
Vivien Didelot466dfa02016-02-26 13:16:05 -05002468 mutex_unlock(&ps->smi_mutex);
Vivien Didelota6692752016-02-12 12:09:39 -05002469
Vivien Didelot466dfa02016-02-26 13:16:05 -05002470 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002471}
2472
Vivien Didelot16bfa702016-03-13 16:21:33 -04002473void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002474{
Vivien Didelota6692752016-02-12 12:09:39 -05002475 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002476 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002477 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002478
Vivien Didelot936f2342016-05-09 13:22:46 -04002479 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2480 return;
2481
Vivien Didelot466dfa02016-02-26 13:16:05 -05002482 mutex_lock(&ps->smi_mutex);
2483
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002484 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002485 ps->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002486
Vivien Didelot009a2b92016-04-17 13:24:01 -04002487 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot16bfa702016-03-13 16:21:33 -04002488 if (i == port || ps->ports[i].bridge_dev == bridge)
Andrew Lunn158bc062016-04-28 21:24:06 -04002489 if (_mv88e6xxx_port_based_vlan_map(ps, i))
Vivien Didelot16bfa702016-03-13 16:21:33 -04002490 netdev_warn(ds->ports[i], "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002491
Vivien Didelot466dfa02016-02-26 13:16:05 -05002492 mutex_unlock(&ps->smi_mutex);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002493}
2494
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002495static void mv88e6xxx_bridge_work(struct work_struct *work)
2496{
2497 struct mv88e6xxx_priv_state *ps;
2498 struct dsa_switch *ds;
2499 int port;
2500
2501 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
Andrew Lunn7543a6d2016-04-13 02:40:40 +02002502 ds = ps->ds;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002503
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002504 mutex_lock(&ps->smi_mutex);
2505
Vivien Didelot009a2b92016-04-17 13:24:01 -04002506 for (port = 0; port < ps->info->num_ports; ++port)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002507 if (test_and_clear_bit(port, ps->port_state_update_mask) &&
Andrew Lunn158bc062016-04-28 21:24:06 -04002508 _mv88e6xxx_port_state(ps, port, ps->ports[port].state))
2509 netdev_warn(ds->ports[port],
2510 "failed to update state to %s\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002511 mv88e6xxx_port_state_names[ps->ports[port].state]);
2512
2513 mutex_unlock(&ps->smi_mutex);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002514}
2515
Andrew Lunn158bc062016-04-28 21:24:06 -04002516static int _mv88e6xxx_phy_page_write(struct mv88e6xxx_priv_state *ps,
2517 int port, int page, int reg, int val)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002518{
2519 int ret;
2520
Andrew Lunn158bc062016-04-28 21:24:06 -04002521 ret = _mv88e6xxx_phy_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002522 if (ret < 0)
2523 goto restore_page_0;
2524
Andrew Lunn158bc062016-04-28 21:24:06 -04002525 ret = _mv88e6xxx_phy_write_indirect(ps, port, reg, val);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002526restore_page_0:
Andrew Lunn158bc062016-04-28 21:24:06 -04002527 _mv88e6xxx_phy_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002528
2529 return ret;
2530}
2531
Andrew Lunn158bc062016-04-28 21:24:06 -04002532static int _mv88e6xxx_phy_page_read(struct mv88e6xxx_priv_state *ps,
2533 int port, int page, int reg)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002534{
2535 int ret;
2536
Andrew Lunn158bc062016-04-28 21:24:06 -04002537 ret = _mv88e6xxx_phy_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002538 if (ret < 0)
2539 goto restore_page_0;
2540
Andrew Lunn158bc062016-04-28 21:24:06 -04002541 ret = _mv88e6xxx_phy_read_indirect(ps, port, reg);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002542restore_page_0:
Andrew Lunn158bc062016-04-28 21:24:06 -04002543 _mv88e6xxx_phy_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002544
2545 return ret;
2546}
2547
Andrew Lunn158bc062016-04-28 21:24:06 -04002548static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_priv_state *ps)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002549{
2550 int ret;
2551
Andrew Lunn158bc062016-04-28 21:24:06 -04002552 ret = _mv88e6xxx_phy_page_read(ps, REG_FIBER_SERDES, PAGE_FIBER_SERDES,
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002553 MII_BMCR);
2554 if (ret < 0)
2555 return ret;
2556
2557 if (ret & BMCR_PDOWN) {
2558 ret &= ~BMCR_PDOWN;
Andrew Lunn158bc062016-04-28 21:24:06 -04002559 ret = _mv88e6xxx_phy_page_write(ps, REG_FIBER_SERDES,
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002560 PAGE_FIBER_SERDES, MII_BMCR,
2561 ret);
2562 }
2563
2564 return ret;
2565}
2566
Andrew Lunndbde9e62015-05-06 01:09:48 +02002567static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002568{
2569 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002570 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002571 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002572
2573 mutex_lock(&ps->smi_mutex);
2574
Andrew Lunn158bc062016-04-28 21:24:06 -04002575 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2576 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2577 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2578 mv88e6xxx_6065_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002579 /* MAC Forcing register: don't force link, speed,
2580 * duplex or flow control state to any particular
2581 * values on physical ports, but force the CPU port
2582 * and all DSA ports to their maximum bandwidth and
2583 * full duplex.
2584 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002585 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002586 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002587 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002588 reg |= PORT_PCS_CTRL_FORCE_LINK |
2589 PORT_PCS_CTRL_LINK_UP |
2590 PORT_PCS_CTRL_DUPLEX_FULL |
2591 PORT_PCS_CTRL_FORCE_DUPLEX;
Andrew Lunn158bc062016-04-28 21:24:06 -04002592 if (mv88e6xxx_6065_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002593 reg |= PORT_PCS_CTRL_100;
2594 else
2595 reg |= PORT_PCS_CTRL_1000;
2596 } else {
2597 reg |= PORT_PCS_CTRL_UNFORCED;
2598 }
2599
Andrew Lunn158bc062016-04-28 21:24:06 -04002600 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002601 PORT_PCS_CTRL, reg);
2602 if (ret)
2603 goto abort;
2604 }
2605
2606 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2607 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2608 * tunneling, determine priority by looking at 802.1p and IP
2609 * priority fields (IP prio has precedence), and set STP state
2610 * to Forwarding.
2611 *
2612 * If this is the CPU link, use DSA or EDSA tagging depending
2613 * on which tagging mode was configured.
2614 *
2615 * If this is a link to another switch, use DSA tagging mode.
2616 *
2617 * If this is the upstream port for this switch, enable
2618 * forwarding of unknown unicasts and multicasts.
2619 */
2620 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002621 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2622 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2623 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2624 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002625 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2626 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2627 PORT_CONTROL_STATE_FORWARDING;
2628 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002629 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002630 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002631 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2632 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2633 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002634 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2635 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
2636 else
2637 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002638 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2639 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002640 }
2641
Andrew Lunn158bc062016-04-28 21:24:06 -04002642 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2643 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2644 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2645 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002646 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2647 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2648 }
2649 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002650 if (dsa_is_dsa_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002651 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002652 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002653 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2654 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2655 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002656 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002657 }
2658
Andrew Lunn54d792f2015-05-06 01:09:47 +02002659 if (port == dsa_upstream_port(ds))
2660 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2661 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2662 }
2663 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002664 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002665 PORT_CONTROL, reg);
2666 if (ret)
2667 goto abort;
2668 }
2669
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002670 /* If this port is connected to a SerDes, make sure the SerDes is not
2671 * powered down.
2672 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002673 if (mv88e6xxx_6352_family(ps)) {
2674 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002675 if (ret < 0)
2676 goto abort;
2677 ret &= PORT_STATUS_CMODE_MASK;
2678 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2679 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2680 (ret == PORT_STATUS_CMODE_SGMII)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002681 ret = mv88e6xxx_power_on_serdes(ps);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002682 if (ret < 0)
2683 goto abort;
2684 }
2685 }
2686
Vivien Didelot8efdda42015-08-13 12:52:23 -04002687 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002688 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002689 * untagged frames on this port, do a destination address lookup on all
2690 * received packets as usual, disable ARP mirroring and don't send a
2691 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002692 */
2693 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002694 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2695 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2696 mv88e6xxx_6095_family(ps) || mv88e6xxx_6320_family(ps) ||
2697 mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002698 reg = PORT_CONTROL_2_MAP_DA;
2699
Andrew Lunn158bc062016-04-28 21:24:06 -04002700 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2701 mv88e6xxx_6165_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002702 reg |= PORT_CONTROL_2_JUMBO_10240;
2703
Andrew Lunn158bc062016-04-28 21:24:06 -04002704 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002705 /* Set the upstream port this port should use */
2706 reg |= dsa_upstream_port(ds);
2707 /* enable forwarding of unknown multicast addresses to
2708 * the upstream port
2709 */
2710 if (port == dsa_upstream_port(ds))
2711 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2712 }
2713
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002714 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002715
Andrew Lunn54d792f2015-05-06 01:09:47 +02002716 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002717 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002718 PORT_CONTROL_2, reg);
2719 if (ret)
2720 goto abort;
2721 }
2722
2723 /* Port Association Vector: when learning source addresses
2724 * of packets, add the address to the address database using
2725 * a port bitmap that has only the bit for this port set and
2726 * the other bits clear.
2727 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002728 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002729 /* Disable learning for CPU port */
2730 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002731 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002732
Andrew Lunn158bc062016-04-28 21:24:06 -04002733 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002734 if (ret)
2735 goto abort;
2736
2737 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002738 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002739 0x0000);
2740 if (ret)
2741 goto abort;
2742
Andrew Lunn158bc062016-04-28 21:24:06 -04002743 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2744 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2745 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002746 /* Do not limit the period of time that this port can
2747 * be paused for by the remote end or the period of
2748 * time that this port can pause the remote end.
2749 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002750 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002751 PORT_PAUSE_CTRL, 0x0000);
2752 if (ret)
2753 goto abort;
2754
2755 /* Port ATU control: disable limiting the number of
2756 * address database entries that this port is allowed
2757 * to use.
2758 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002759 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002760 PORT_ATU_CONTROL, 0x0000);
2761 /* Priority Override: disable DA, SA and VTU priority
2762 * override.
2763 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002764 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002765 PORT_PRI_OVERRIDE, 0x0000);
2766 if (ret)
2767 goto abort;
2768
2769 /* Port Ethertype: use the Ethertype DSA Ethertype
2770 * value.
2771 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002772 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002773 PORT_ETH_TYPE, ETH_P_EDSA);
2774 if (ret)
2775 goto abort;
2776 /* Tag Remap: use an identity 802.1p prio -> switch
2777 * prio mapping.
2778 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002779 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002780 PORT_TAG_REGMAP_0123, 0x3210);
2781 if (ret)
2782 goto abort;
2783
2784 /* Tag Remap 2: use an identity 802.1p prio -> switch
2785 * prio mapping.
2786 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002787 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002788 PORT_TAG_REGMAP_4567, 0x7654);
2789 if (ret)
2790 goto abort;
2791 }
2792
Andrew Lunn158bc062016-04-28 21:24:06 -04002793 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2794 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2795 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2796 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002797 /* Rate Control: disable ingress rate limiting. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002798 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002799 PORT_RATE_CONTROL, 0x0001);
2800 if (ret)
2801 goto abort;
2802 }
2803
Guenter Roeck366f0a02015-03-26 18:36:30 -07002804 /* Port Control 1: disable trunking, disable sending
2805 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002806 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002807 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002808 if (ret)
2809 goto abort;
2810
Vivien Didelot207afda2016-04-14 14:42:09 -04002811 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002812 * database, and allow bidirectional communication between the
2813 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002814 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002815 ret = _mv88e6xxx_port_fid_set(ps, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002816 if (ret)
2817 goto abort;
2818
Andrew Lunn158bc062016-04-28 21:24:06 -04002819 ret = _mv88e6xxx_port_based_vlan_map(ps, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002820 if (ret)
2821 goto abort;
2822
2823 /* Default VLAN ID and priority: don't set a default VLAN
2824 * ID, and set the default packet priority to zero.
2825 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002826 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002827 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002828abort:
2829 mutex_unlock(&ps->smi_mutex);
2830 return ret;
2831}
2832
Andrew Lunndbde9e62015-05-06 01:09:48 +02002833int mv88e6xxx_setup_ports(struct dsa_switch *ds)
2834{
2835 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2836 int ret;
2837 int i;
2838
Vivien Didelot009a2b92016-04-17 13:24:01 -04002839 for (i = 0; i < ps->info->num_ports; i++) {
Andrew Lunndbde9e62015-05-06 01:09:48 +02002840 ret = mv88e6xxx_setup_port(ds, i);
2841 if (ret < 0)
2842 return ret;
2843 }
2844 return 0;
2845}
2846
Andrew Lunn158bc062016-04-28 21:24:06 -04002847int mv88e6xxx_setup_common(struct mv88e6xxx_priv_state *ps)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002848{
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002849 mutex_init(&ps->smi_mutex);
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002850
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002851 INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
2852
Vivien Didelotd24645b2016-05-09 13:22:41 -04002853 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
2854 mutex_init(&ps->eeprom_mutex);
2855
Vivien Didelot8c9983a2016-05-09 13:22:39 -04002856 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
2857 mv88e6xxx_ppu_state_init(ps);
2858
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002859 return 0;
2860}
2861
Andrew Lunn54d792f2015-05-06 01:09:47 +02002862int mv88e6xxx_setup_global(struct dsa_switch *ds)
2863{
2864 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002865 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002866 int i;
2867
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002868 mutex_lock(&ps->smi_mutex);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002869 /* Set the default address aging time to 5 minutes, and
2870 * enable address learn messages to be sent to all message
2871 * ports.
2872 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002873 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002874 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2875 if (err)
2876 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002877
2878 /* Configure the IP ToS mapping registers. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002879 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002880 if (err)
2881 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002882 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002883 if (err)
2884 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002885 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002886 if (err)
2887 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002888 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002889 if (err)
2890 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002891 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002892 if (err)
2893 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002894 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002895 if (err)
2896 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002897 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002898 if (err)
2899 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002900 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002901 if (err)
2902 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002903
2904 /* Configure the IEEE 802.1p priority mapping register. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002905 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002906 if (err)
2907 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002908
2909 /* Send all frames with destination addresses matching
2910 * 01:80:c2:00:00:0x to the CPU port.
2911 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002912 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002913 if (err)
2914 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002915
2916 /* Ignore removed tag data on doubly tagged packets, disable
2917 * flow control messages, force flow control priority to the
2918 * highest, and send all special multicast frames to the CPU
2919 * port at the highest priority.
2920 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002921 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002922 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
2923 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
2924 if (err)
2925 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002926
2927 /* Program the DSA routing table. */
2928 for (i = 0; i < 32; i++) {
2929 int nexthop = 0x1f;
2930
2931 if (ds->pd->rtable &&
2932 i != ds->index && i < ds->dst->pd->nr_chips)
2933 nexthop = ds->pd->rtable[i] & 0x1f;
2934
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002935 err = _mv88e6xxx_reg_write(
Andrew Lunn158bc062016-04-28 21:24:06 -04002936 ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002937 GLOBAL2_DEVICE_MAPPING,
2938 GLOBAL2_DEVICE_MAPPING_UPDATE |
2939 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop);
2940 if (err)
2941 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002942 }
2943
2944 /* Clear all trunk masks. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002945 for (i = 0; i < 8; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002946 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002947 0x8000 |
2948 (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
Vivien Didelot009a2b92016-04-17 13:24:01 -04002949 ((1 << ps->info->num_ports) - 1));
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002950 if (err)
2951 goto unlock;
2952 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002953
2954 /* Clear all trunk mappings. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002955 for (i = 0; i < 16; i++) {
2956 err = _mv88e6xxx_reg_write(
Andrew Lunn158bc062016-04-28 21:24:06 -04002957 ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002958 GLOBAL2_TRUNK_MAPPING,
2959 GLOBAL2_TRUNK_MAPPING_UPDATE |
2960 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
2961 if (err)
2962 goto unlock;
2963 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002964
Andrew Lunn158bc062016-04-28 21:24:06 -04002965 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2966 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2967 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002968 /* Send all frames with destination addresses matching
2969 * 01:80:c2:00:00:2x to the CPU port.
2970 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002971 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002972 GLOBAL2_MGMT_EN_2X, 0xffff);
2973 if (err)
2974 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002975
2976 /* Initialise cross-chip port VLAN table to reset
2977 * defaults.
2978 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002979 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002980 GLOBAL2_PVT_ADDR, 0x9000);
2981 if (err)
2982 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002983
2984 /* Clear the priority override table. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002985 for (i = 0; i < 16; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002986 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002987 GLOBAL2_PRIO_OVERRIDE,
2988 0x8000 | (i << 8));
2989 if (err)
2990 goto unlock;
2991 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002992 }
2993
Andrew Lunn158bc062016-04-28 21:24:06 -04002994 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2995 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2996 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2997 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002998 /* Disable ingress rate limiting by resetting all
2999 * ingress rate limit registers to their initial
3000 * state.
3001 */
Vivien Didelot009a2b92016-04-17 13:24:01 -04003002 for (i = 0; i < ps->info->num_ports; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04003003 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003004 GLOBAL2_INGRESS_OP,
3005 0x9000 | (i << 8));
3006 if (err)
3007 goto unlock;
3008 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003009 }
3010
Andrew Lunndb687a52015-06-20 21:31:29 +02003011 /* Clear the statistics counters for all ports */
Andrew Lunn158bc062016-04-28 21:24:06 -04003012 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003013 GLOBAL_STATS_OP_FLUSH_ALL);
3014 if (err)
3015 goto unlock;
Andrew Lunndb687a52015-06-20 21:31:29 +02003016
3017 /* Wait for the flush to complete. */
Andrew Lunn158bc062016-04-28 21:24:06 -04003018 err = _mv88e6xxx_stats_wait(ps);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003019 if (err < 0)
Vivien Didelot6b17e862015-08-13 12:52:18 -04003020 goto unlock;
3021
Vivien Didelotc161d0a2015-09-04 14:34:13 -04003022 /* Clear all ATU entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04003023 err = _mv88e6xxx_atu_flush(ps, 0, true);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003024 if (err < 0)
Vivien Didelotc161d0a2015-09-04 14:34:13 -04003025 goto unlock;
3026
Vivien Didelot6b17e862015-08-13 12:52:18 -04003027 /* Clear all the VTU and STU entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04003028 err = _mv88e6xxx_vtu_stu_flush(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04003029unlock:
Vivien Didelot24751e22015-08-03 09:17:44 -04003030 mutex_unlock(&ps->smi_mutex);
Andrew Lunndb687a52015-06-20 21:31:29 +02003031
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003032 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003033}
3034
Andrew Lunn158bc062016-04-28 21:24:06 -04003035int mv88e6xxx_switch_reset(struct mv88e6xxx_priv_state *ps, bool ppu_active)
Andrew Lunn143a8302015-04-02 04:06:34 +02003036{
Andrew Lunn143a8302015-04-02 04:06:34 +02003037 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Andrew Lunn158bc062016-04-28 21:24:06 -04003038 struct gpio_desc *gpiod = ps->ds->pd->reset;
Andrew Lunn143a8302015-04-02 04:06:34 +02003039 unsigned long timeout;
3040 int ret;
3041 int i;
3042
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003043 mutex_lock(&ps->smi_mutex);
3044
Andrew Lunn143a8302015-04-02 04:06:34 +02003045 /* Set all ports to the disabled state. */
Vivien Didelot009a2b92016-04-17 13:24:01 -04003046 for (i = 0; i < ps->info->num_ports; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04003047 ret = _mv88e6xxx_reg_read(ps, REG_PORT(i), PORT_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003048 if (ret < 0)
3049 goto unlock;
3050
Andrew Lunn158bc062016-04-28 21:24:06 -04003051 ret = _mv88e6xxx_reg_write(ps, REG_PORT(i), PORT_CONTROL,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003052 ret & 0xfffc);
3053 if (ret)
3054 goto unlock;
Andrew Lunn143a8302015-04-02 04:06:34 +02003055 }
3056
3057 /* Wait for transmit queues to drain. */
3058 usleep_range(2000, 4000);
3059
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +01003060 /* If there is a gpio connected to the reset pin, toggle it */
3061 if (gpiod) {
3062 gpiod_set_value_cansleep(gpiod, 1);
3063 usleep_range(10000, 20000);
3064 gpiod_set_value_cansleep(gpiod, 0);
3065 usleep_range(10000, 20000);
3066 }
3067
Andrew Lunn143a8302015-04-02 04:06:34 +02003068 /* Reset the switch. Keep the PPU active if requested. The PPU
3069 * needs to be active to support indirect phy register access
3070 * through global registers 0x18 and 0x19.
3071 */
3072 if (ppu_active)
Andrew Lunn158bc062016-04-28 21:24:06 -04003073 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc000);
Andrew Lunn143a8302015-04-02 04:06:34 +02003074 else
Andrew Lunn158bc062016-04-28 21:24:06 -04003075 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc400);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003076 if (ret)
3077 goto unlock;
Andrew Lunn143a8302015-04-02 04:06:34 +02003078
3079 /* Wait up to one second for reset to complete. */
3080 timeout = jiffies + 1 * HZ;
3081 while (time_before(jiffies, timeout)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04003082 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, 0x00);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003083 if (ret < 0)
3084 goto unlock;
3085
Andrew Lunn143a8302015-04-02 04:06:34 +02003086 if ((ret & is_reset) == is_reset)
3087 break;
3088 usleep_range(1000, 2000);
3089 }
3090 if (time_after(jiffies, timeout))
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003091 ret = -ETIMEDOUT;
3092 else
3093 ret = 0;
3094unlock:
3095 mutex_unlock(&ps->smi_mutex);
Andrew Lunn143a8302015-04-02 04:06:34 +02003096
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003097 return ret;
Andrew Lunn143a8302015-04-02 04:06:34 +02003098}
3099
Andrew Lunn491435852015-04-02 04:06:35 +02003100int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
3101{
3102 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3103 int ret;
3104
Andrew Lunn3898c142015-05-06 01:09:53 +02003105 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04003106 ret = _mv88e6xxx_phy_page_read(ps, port, page, reg);
Andrew Lunn3898c142015-05-06 01:09:53 +02003107 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003108
Andrew Lunn491435852015-04-02 04:06:35 +02003109 return ret;
3110}
3111
3112int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
3113 int reg, int val)
3114{
3115 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3116 int ret;
3117
Andrew Lunn3898c142015-05-06 01:09:53 +02003118 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04003119 ret = _mv88e6xxx_phy_page_write(ps, port, page, reg, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02003120 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003121
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003122 return ret;
3123}
3124
Andrew Lunn158bc062016-04-28 21:24:06 -04003125static int mv88e6xxx_port_to_phy_addr(struct mv88e6xxx_priv_state *ps,
3126 int port)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003127{
Vivien Didelot009a2b92016-04-17 13:24:01 -04003128 if (port >= 0 && port < ps->info->num_ports)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003129 return port;
3130 return -EINVAL;
3131}
3132
3133int
3134mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
3135{
3136 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -04003137 int addr = mv88e6xxx_port_to_phy_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003138 int ret;
3139
3140 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003141 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003142
Andrew Lunn3898c142015-05-06 01:09:53 +02003143 mutex_lock(&ps->smi_mutex);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003144
3145 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3146 ret = mv88e6xxx_phy_read_ppu(ps, addr, regnum);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003147 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
3148 ret = _mv88e6xxx_phy_read_indirect(ps, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003149 else
3150 ret = _mv88e6xxx_phy_read(ps, addr, regnum);
3151
Andrew Lunn3898c142015-05-06 01:09:53 +02003152 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003153 return ret;
3154}
3155
3156int
3157mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
3158{
3159 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -04003160 int addr = mv88e6xxx_port_to_phy_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003161 int ret;
3162
3163 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003164 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003165
Andrew Lunn3898c142015-05-06 01:09:53 +02003166 mutex_lock(&ps->smi_mutex);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003167
3168 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3169 ret = mv88e6xxx_phy_write_ppu(ps, addr, regnum, val);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003170 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
3171 ret = _mv88e6xxx_phy_write_indirect(ps, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003172 else
3173 ret = _mv88e6xxx_phy_write(ps, addr, regnum, val);
3174
Andrew Lunn3898c142015-05-06 01:09:53 +02003175 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003176 return ret;
3177}
3178
Guenter Roeckc22995c2015-07-25 09:42:28 -07003179#ifdef CONFIG_NET_DSA_HWMON
3180
3181static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3182{
3183 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3184 int ret;
3185 int val;
3186
3187 *temp = 0;
3188
3189 mutex_lock(&ps->smi_mutex);
3190
Andrew Lunn158bc062016-04-28 21:24:06 -04003191 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003192 if (ret < 0)
3193 goto error;
3194
3195 /* Enable temperature sensor */
Andrew Lunn158bc062016-04-28 21:24:06 -04003196 ret = _mv88e6xxx_phy_read(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003197 if (ret < 0)
3198 goto error;
3199
Andrew Lunn158bc062016-04-28 21:24:06 -04003200 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x1a, ret | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003201 if (ret < 0)
3202 goto error;
3203
3204 /* Wait for temperature to stabilize */
3205 usleep_range(10000, 12000);
3206
Andrew Lunn158bc062016-04-28 21:24:06 -04003207 val = _mv88e6xxx_phy_read(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003208 if (val < 0) {
3209 ret = val;
3210 goto error;
3211 }
3212
3213 /* Disable temperature sensor */
Andrew Lunn158bc062016-04-28 21:24:06 -04003214 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x1a, ret & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003215 if (ret < 0)
3216 goto error;
3217
3218 *temp = ((val & 0x1f) - 5) * 5;
3219
3220error:
Andrew Lunn158bc062016-04-28 21:24:06 -04003221 _mv88e6xxx_phy_write(ps, 0x0, 0x16, 0x0);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003222 mutex_unlock(&ps->smi_mutex);
3223 return ret;
3224}
3225
3226static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3227{
Andrew Lunn158bc062016-04-28 21:24:06 -04003228 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3229 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003230 int ret;
3231
3232 *temp = 0;
3233
3234 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
3235 if (ret < 0)
3236 return ret;
3237
3238 *temp = (ret & 0xff) - 25;
3239
3240 return 0;
3241}
3242
3243int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3244{
Andrew Lunn158bc062016-04-28 21:24:06 -04003245 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3246
Vivien Didelot6594f612016-05-09 13:22:42 -04003247 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP))
3248 return -EOPNOTSUPP;
3249
Andrew Lunn158bc062016-04-28 21:24:06 -04003250 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003251 return mv88e63xx_get_temp(ds, temp);
3252
3253 return mv88e61xx_get_temp(ds, temp);
3254}
3255
3256int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3257{
Andrew Lunn158bc062016-04-28 21:24:06 -04003258 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3259 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003260 int ret;
3261
Vivien Didelot6594f612016-05-09 13:22:42 -04003262 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003263 return -EOPNOTSUPP;
3264
3265 *temp = 0;
3266
3267 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3268 if (ret < 0)
3269 return ret;
3270
3271 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3272
3273 return 0;
3274}
3275
3276int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3277{
Andrew Lunn158bc062016-04-28 21:24:06 -04003278 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3279 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003280 int ret;
3281
Vivien Didelot6594f612016-05-09 13:22:42 -04003282 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003283 return -EOPNOTSUPP;
3284
3285 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3286 if (ret < 0)
3287 return ret;
3288 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3289 return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
3290 (ret & 0xe0ff) | (temp << 8));
3291}
3292
3293int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3294{
Andrew Lunn158bc062016-04-28 21:24:06 -04003295 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3296 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003297 int ret;
3298
Vivien Didelot6594f612016-05-09 13:22:42 -04003299 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003300 return -EOPNOTSUPP;
3301
3302 *alarm = false;
3303
3304 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3305 if (ret < 0)
3306 return ret;
3307
3308 *alarm = !!(ret & 0x40);
3309
3310 return 0;
3311}
3312#endif /* CONFIG_NET_DSA_HWMON */
3313
Vivien Didelotf6271e62016-04-17 13:23:59 -04003314static const struct mv88e6xxx_info *
3315mv88e6xxx_lookup_info(unsigned int prod_num, const struct mv88e6xxx_info *table,
Vivien Didelot0209d142016-04-17 13:23:55 -04003316 unsigned int num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003317{
Vivien Didelota439c062016-04-17 13:23:58 -04003318 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003319
Vivien Didelotb9b37712015-10-30 19:39:48 -04003320 for (i = 0; i < num; ++i)
Vivien Didelotf6271e62016-04-17 13:23:59 -04003321 if (table[i].prod_num == prod_num)
3322 return &table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003323
Vivien Didelotb9b37712015-10-30 19:39:48 -04003324 return NULL;
3325}
3326
Vivien Didelot0209d142016-04-17 13:23:55 -04003327const char *mv88e6xxx_drv_probe(struct device *dsa_dev, struct device *host_dev,
3328 int sw_addr, void **priv,
Vivien Didelotf6271e62016-04-17 13:23:59 -04003329 const struct mv88e6xxx_info *table,
Vivien Didelot0209d142016-04-17 13:23:55 -04003330 unsigned int num)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003331{
Vivien Didelotf6271e62016-04-17 13:23:59 -04003332 const struct mv88e6xxx_info *info;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003333 struct mv88e6xxx_priv_state *ps;
Vivien Didelota439c062016-04-17 13:23:58 -04003334 struct mii_bus *bus;
Vivien Didelot0209d142016-04-17 13:23:55 -04003335 const char *name;
Vivien Didelota439c062016-04-17 13:23:58 -04003336 int id, prod_num, rev;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003337
Vivien Didelota439c062016-04-17 13:23:58 -04003338 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003339 if (!bus)
3340 return NULL;
3341
Vivien Didelota439c062016-04-17 13:23:58 -04003342 id = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
3343 if (id < 0)
3344 return NULL;
3345
3346 prod_num = (id & 0xfff0) >> 4;
3347 rev = id & 0x000f;
3348
Vivien Didelotf6271e62016-04-17 13:23:59 -04003349 info = mv88e6xxx_lookup_info(prod_num, table, num);
3350 if (!info)
Vivien Didelota439c062016-04-17 13:23:58 -04003351 return NULL;
3352
Vivien Didelotf6271e62016-04-17 13:23:59 -04003353 name = info->name;
3354
Vivien Didelota439c062016-04-17 13:23:58 -04003355 ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL);
3356 if (!ps)
3357 return NULL;
3358
3359 ps->bus = bus;
3360 ps->sw_addr = sw_addr;
Vivien Didelotf6271e62016-04-17 13:23:59 -04003361 ps->info = info;
Vivien Didelota439c062016-04-17 13:23:58 -04003362
3363 *priv = ps;
3364
3365 dev_info(&ps->bus->dev, "switch 0x%x probed: %s, revision %u\n",
3366 prod_num, name, rev);
3367
Andrew Lunna77d43f2016-04-13 02:40:42 +02003368 return name;
3369}
3370
Ben Hutchings98e67302011-11-25 14:36:19 +00003371static int __init mv88e6xxx_init(void)
3372{
3373#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
3374 register_switch_driver(&mv88e6131_switch_driver);
3375#endif
Andrew Lunnca3dfa52016-03-12 00:01:36 +01003376#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123)
3377 register_switch_driver(&mv88e6123_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003378#endif
Guenter Roeck3ad50cc2014-10-29 10:44:56 -07003379#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
3380 register_switch_driver(&mv88e6352_switch_driver);
3381#endif
Andrew Lunn42f27252014-09-12 23:58:44 +02003382#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
3383 register_switch_driver(&mv88e6171_switch_driver);
3384#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00003385 return 0;
3386}
3387module_init(mv88e6xxx_init);
3388
3389static void __exit mv88e6xxx_cleanup(void)
3390{
Andrew Lunn42f27252014-09-12 23:58:44 +02003391#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
3392 unregister_switch_driver(&mv88e6171_switch_driver);
3393#endif
Vivien Didelot4212b542015-05-01 10:43:52 -04003394#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
3395 unregister_switch_driver(&mv88e6352_switch_driver);
3396#endif
Andrew Lunnca3dfa52016-03-12 00:01:36 +01003397#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123)
3398 unregister_switch_driver(&mv88e6123_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003399#endif
3400#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
3401 unregister_switch_driver(&mv88e6131_switch_driver);
3402#endif
3403}
3404module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003405
3406MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3407MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3408MODULE_LICENSE("GPL");