blob: c28ad83ee74d909b9bb4325c59ee33eb36000cb6 [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
1372 switch (state) {
1373 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001374 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001375 break;
1376 case BR_STATE_BLOCKING:
1377 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001378 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001379 break;
1380 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001381 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001382 break;
1383 case BR_STATE_FORWARDING:
1384 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001385 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001386 break;
1387 }
1388
Vivien Didelot43c44a92016-04-06 11:55:03 -04001389 /* mv88e6xxx_port_stp_state_set may be called with softirqs disabled,
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001390 * so we can not update the port state directly but need to schedule it.
1391 */
Vivien Didelotd715fa62016-02-12 12:09:38 -05001392 ps->ports[port].state = stp_state;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001393 set_bit(port, ps->port_state_update_mask);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001394 schedule_work(&ps->bridge_work);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001395}
1396
Andrew Lunn158bc062016-04-28 21:24:06 -04001397static int _mv88e6xxx_port_pvid(struct mv88e6xxx_priv_state *ps, int port,
1398 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001399{
Andrew Lunn158bc062016-04-28 21:24:06 -04001400 struct dsa_switch *ds = ps->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001401 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001402 int ret;
1403
Andrew Lunn158bc062016-04-28 21:24:06 -04001404 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001405 if (ret < 0)
1406 return ret;
1407
Vivien Didelot5da96032016-03-07 18:24:39 -05001408 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1409
1410 if (new) {
1411 ret &= ~PORT_DEFAULT_VLAN_MASK;
1412 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1413
Andrew Lunn158bc062016-04-28 21:24:06 -04001414 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001415 PORT_DEFAULT_VLAN, ret);
1416 if (ret < 0)
1417 return ret;
1418
1419 netdev_dbg(ds->ports[port], "DefaultVID %d (was %d)\n", *new,
1420 pvid);
1421 }
1422
1423 if (old)
1424 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001425
1426 return 0;
1427}
1428
Andrew Lunn158bc062016-04-28 21:24:06 -04001429static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_priv_state *ps,
1430 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001431{
Andrew Lunn158bc062016-04-28 21:24:06 -04001432 return _mv88e6xxx_port_pvid(ps, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001433}
1434
Andrew Lunn158bc062016-04-28 21:24:06 -04001435static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_priv_state *ps,
1436 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001437{
Andrew Lunn158bc062016-04-28 21:24:06 -04001438 return _mv88e6xxx_port_pvid(ps, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001439}
1440
Andrew Lunn158bc062016-04-28 21:24:06 -04001441static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001442{
Andrew Lunn158bc062016-04-28 21:24:06 -04001443 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_VTU_OP,
Vivien Didelot6b17e862015-08-13 12:52:18 -04001444 GLOBAL_VTU_OP_BUSY);
1445}
1446
Andrew Lunn158bc062016-04-28 21:24:06 -04001447static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_priv_state *ps, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001448{
1449 int ret;
1450
Andrew Lunn158bc062016-04-28 21:24:06 -04001451 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001452 if (ret < 0)
1453 return ret;
1454
Andrew Lunn158bc062016-04-28 21:24:06 -04001455 return _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001456}
1457
Andrew Lunn158bc062016-04-28 21:24:06 -04001458static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001459{
1460 int ret;
1461
Andrew Lunn158bc062016-04-28 21:24:06 -04001462 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001463 if (ret < 0)
1464 return ret;
1465
Andrew Lunn158bc062016-04-28 21:24:06 -04001466 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001467}
1468
Andrew Lunn158bc062016-04-28 21:24:06 -04001469static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001470 struct mv88e6xxx_vtu_stu_entry *entry,
1471 unsigned int nibble_offset)
1472{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001473 u16 regs[3];
1474 int i;
1475 int ret;
1476
1477 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001478 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001479 GLOBAL_VTU_DATA_0_3 + i);
1480 if (ret < 0)
1481 return ret;
1482
1483 regs[i] = ret;
1484 }
1485
Vivien Didelot009a2b92016-04-17 13:24:01 -04001486 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001487 unsigned int shift = (i % 4) * 4 + nibble_offset;
1488 u16 reg = regs[i / 4];
1489
1490 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1491 }
1492
1493 return 0;
1494}
1495
Andrew Lunn158bc062016-04-28 21:24:06 -04001496static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001497 struct mv88e6xxx_vtu_stu_entry *entry,
1498 unsigned int nibble_offset)
1499{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001500 u16 regs[3] = { 0 };
1501 int i;
1502 int ret;
1503
Vivien Didelot009a2b92016-04-17 13:24:01 -04001504 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001505 unsigned int shift = (i % 4) * 4 + nibble_offset;
1506 u8 data = entry->data[i];
1507
1508 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1509 }
1510
1511 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001512 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001513 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1514 if (ret < 0)
1515 return ret;
1516 }
1517
1518 return 0;
1519}
1520
Andrew Lunn158bc062016-04-28 21:24:06 -04001521static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_priv_state *ps, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001522{
Andrew Lunn158bc062016-04-28 21:24:06 -04001523 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001524 vid & GLOBAL_VTU_VID_MASK);
1525}
1526
Andrew Lunn158bc062016-04-28 21:24:06 -04001527static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001528 struct mv88e6xxx_vtu_stu_entry *entry)
1529{
1530 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1531 int ret;
1532
Andrew Lunn158bc062016-04-28 21:24:06 -04001533 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001534 if (ret < 0)
1535 return ret;
1536
Andrew Lunn158bc062016-04-28 21:24:06 -04001537 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001538 if (ret < 0)
1539 return ret;
1540
Andrew Lunn158bc062016-04-28 21:24:06 -04001541 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001542 if (ret < 0)
1543 return ret;
1544
1545 next.vid = ret & GLOBAL_VTU_VID_MASK;
1546 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1547
1548 if (next.valid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001549 ret = _mv88e6xxx_vtu_stu_data_read(ps, &next, 0);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001550 if (ret < 0)
1551 return ret;
1552
Andrew Lunn158bc062016-04-28 21:24:06 -04001553 if (mv88e6xxx_has_fid_reg(ps)) {
1554 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001555 GLOBAL_VTU_FID);
1556 if (ret < 0)
1557 return ret;
1558
1559 next.fid = ret & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001560 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001561 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1562 * VTU DBNum[3:0] are located in VTU Operation 3:0
1563 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001564 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001565 GLOBAL_VTU_OP);
1566 if (ret < 0)
1567 return ret;
1568
1569 next.fid = (ret & 0xf00) >> 4;
1570 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001571 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001572
Andrew Lunn158bc062016-04-28 21:24:06 -04001573 if (mv88e6xxx_has_stu(ps)) {
1574 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001575 GLOBAL_VTU_SID);
1576 if (ret < 0)
1577 return ret;
1578
1579 next.sid = ret & GLOBAL_VTU_SID_MASK;
1580 }
1581 }
1582
1583 *entry = next;
1584 return 0;
1585}
1586
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001587int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1588 struct switchdev_obj_port_vlan *vlan,
1589 int (*cb)(struct switchdev_obj *obj))
1590{
1591 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1592 struct mv88e6xxx_vtu_stu_entry next;
1593 u16 pvid;
1594 int err;
1595
1596 mutex_lock(&ps->smi_mutex);
1597
Andrew Lunn158bc062016-04-28 21:24:06 -04001598 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001599 if (err)
1600 goto unlock;
1601
Andrew Lunn158bc062016-04-28 21:24:06 -04001602 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001603 if (err)
1604 goto unlock;
1605
1606 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001607 err = _mv88e6xxx_vtu_getnext(ps, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001608 if (err)
1609 break;
1610
1611 if (!next.valid)
1612 break;
1613
1614 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1615 continue;
1616
1617 /* reinit and dump this VLAN obj */
1618 vlan->vid_begin = vlan->vid_end = next.vid;
1619 vlan->flags = 0;
1620
1621 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1622 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1623
1624 if (next.vid == pvid)
1625 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1626
1627 err = cb(&vlan->obj);
1628 if (err)
1629 break;
1630 } while (next.vid < GLOBAL_VTU_VID_MASK);
1631
1632unlock:
1633 mutex_unlock(&ps->smi_mutex);
1634
1635 return err;
1636}
1637
Andrew Lunn158bc062016-04-28 21:24:06 -04001638static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001639 struct mv88e6xxx_vtu_stu_entry *entry)
1640{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001641 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001642 u16 reg = 0;
1643 int ret;
1644
Andrew Lunn158bc062016-04-28 21:24:06 -04001645 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001646 if (ret < 0)
1647 return ret;
1648
1649 if (!entry->valid)
1650 goto loadpurge;
1651
1652 /* Write port member tags */
Andrew Lunn158bc062016-04-28 21:24:06 -04001653 ret = _mv88e6xxx_vtu_stu_data_write(ps, entry, 0);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001654 if (ret < 0)
1655 return ret;
1656
Andrew Lunn158bc062016-04-28 21:24:06 -04001657 if (mv88e6xxx_has_stu(ps)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001658 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001659 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001660 if (ret < 0)
1661 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001662 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001663
Andrew Lunn158bc062016-04-28 21:24:06 -04001664 if (mv88e6xxx_has_fid_reg(ps)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001665 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001666 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_FID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001667 if (ret < 0)
1668 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001669 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001670 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1671 * VTU DBNum[3:0] are located in VTU Operation 3:0
1672 */
1673 op |= (entry->fid & 0xf0) << 8;
1674 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001675 }
1676
1677 reg = GLOBAL_VTU_VID_VALID;
1678loadpurge:
1679 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001680 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001681 if (ret < 0)
1682 return ret;
1683
Andrew Lunn158bc062016-04-28 21:24:06 -04001684 return _mv88e6xxx_vtu_cmd(ps, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001685}
1686
Andrew Lunn158bc062016-04-28 21:24:06 -04001687static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_priv_state *ps, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001688 struct mv88e6xxx_vtu_stu_entry *entry)
1689{
1690 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1691 int ret;
1692
Andrew Lunn158bc062016-04-28 21:24:06 -04001693 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001694 if (ret < 0)
1695 return ret;
1696
Andrew Lunn158bc062016-04-28 21:24:06 -04001697 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001698 sid & GLOBAL_VTU_SID_MASK);
1699 if (ret < 0)
1700 return ret;
1701
Andrew Lunn158bc062016-04-28 21:24:06 -04001702 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001703 if (ret < 0)
1704 return ret;
1705
Andrew Lunn158bc062016-04-28 21:24:06 -04001706 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001707 if (ret < 0)
1708 return ret;
1709
1710 next.sid = ret & GLOBAL_VTU_SID_MASK;
1711
Andrew Lunn158bc062016-04-28 21:24:06 -04001712 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001713 if (ret < 0)
1714 return ret;
1715
1716 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1717
1718 if (next.valid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001719 ret = _mv88e6xxx_vtu_stu_data_read(ps, &next, 2);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001720 if (ret < 0)
1721 return ret;
1722 }
1723
1724 *entry = next;
1725 return 0;
1726}
1727
Andrew Lunn158bc062016-04-28 21:24:06 -04001728static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001729 struct mv88e6xxx_vtu_stu_entry *entry)
1730{
1731 u16 reg = 0;
1732 int ret;
1733
Andrew Lunn158bc062016-04-28 21:24:06 -04001734 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001735 if (ret < 0)
1736 return ret;
1737
1738 if (!entry->valid)
1739 goto loadpurge;
1740
1741 /* Write port states */
Andrew Lunn158bc062016-04-28 21:24:06 -04001742 ret = _mv88e6xxx_vtu_stu_data_write(ps, entry, 2);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001743 if (ret < 0)
1744 return ret;
1745
1746 reg = GLOBAL_VTU_VID_VALID;
1747loadpurge:
Andrew Lunn158bc062016-04-28 21:24:06 -04001748 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001749 if (ret < 0)
1750 return ret;
1751
1752 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001753 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001754 if (ret < 0)
1755 return ret;
1756
Andrew Lunn158bc062016-04-28 21:24:06 -04001757 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001758}
1759
Andrew Lunn158bc062016-04-28 21:24:06 -04001760static int _mv88e6xxx_port_fid(struct mv88e6xxx_priv_state *ps, int port,
1761 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001762{
Andrew Lunn158bc062016-04-28 21:24:06 -04001763 struct dsa_switch *ds = ps->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001764 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001765 u16 fid;
1766 int ret;
1767
Andrew Lunn158bc062016-04-28 21:24:06 -04001768 if (mv88e6xxx_num_databases(ps) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001769 upper_mask = 0xff;
Andrew Lunn158bc062016-04-28 21:24:06 -04001770 else if (mv88e6xxx_num_databases(ps) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001771 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001772 else
1773 return -EOPNOTSUPP;
1774
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001775 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001776 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001777 if (ret < 0)
1778 return ret;
1779
1780 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1781
1782 if (new) {
1783 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1784 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1785
Andrew Lunn158bc062016-04-28 21:24:06 -04001786 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001787 ret);
1788 if (ret < 0)
1789 return ret;
1790 }
1791
1792 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001793 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001794 if (ret < 0)
1795 return ret;
1796
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001797 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001798
1799 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001800 ret &= ~upper_mask;
1801 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001802
Andrew Lunn158bc062016-04-28 21:24:06 -04001803 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001804 ret);
1805 if (ret < 0)
1806 return ret;
1807
1808 netdev_dbg(ds->ports[port], "FID %d (was %d)\n", *new, fid);
1809 }
1810
1811 if (old)
1812 *old = fid;
1813
1814 return 0;
1815}
1816
Andrew Lunn158bc062016-04-28 21:24:06 -04001817static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_priv_state *ps,
1818 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001819{
Andrew Lunn158bc062016-04-28 21:24:06 -04001820 return _mv88e6xxx_port_fid(ps, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001821}
1822
Andrew Lunn158bc062016-04-28 21:24:06 -04001823static int _mv88e6xxx_port_fid_set(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, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001827}
1828
Andrew Lunn158bc062016-04-28 21:24:06 -04001829static int _mv88e6xxx_fid_new(struct mv88e6xxx_priv_state *ps, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001830{
1831 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1832 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001833 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001834
1835 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1836
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001837 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001838 for (i = 0; i < ps->info->num_ports; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001839 err = _mv88e6xxx_port_fid_get(ps, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001840 if (err)
1841 return err;
1842
1843 set_bit(*fid, fid_bitmap);
1844 }
1845
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001846 /* Set every FID bit used by the VLAN entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001847 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001848 if (err)
1849 return err;
1850
1851 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001852 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001853 if (err)
1854 return err;
1855
1856 if (!vlan.valid)
1857 break;
1858
1859 set_bit(vlan.fid, fid_bitmap);
1860 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1861
1862 /* The reset value 0x000 is used to indicate that multiple address
1863 * databases are not needed. Return the next positive available.
1864 */
1865 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Andrew Lunn158bc062016-04-28 21:24:06 -04001866 if (unlikely(*fid >= mv88e6xxx_num_databases(ps)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001867 return -ENOSPC;
1868
1869 /* Clear the database */
Andrew Lunn158bc062016-04-28 21:24:06 -04001870 return _mv88e6xxx_atu_flush(ps, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001871}
1872
Andrew Lunn158bc062016-04-28 21:24:06 -04001873static int _mv88e6xxx_vtu_new(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001874 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001875{
Andrew Lunn158bc062016-04-28 21:24:06 -04001876 struct dsa_switch *ds = ps->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001877 struct mv88e6xxx_vtu_stu_entry vlan = {
1878 .valid = true,
1879 .vid = vid,
1880 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001881 int i, err;
1882
Andrew Lunn158bc062016-04-28 21:24:06 -04001883 err = _mv88e6xxx_fid_new(ps, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001884 if (err)
1885 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001886
Vivien Didelot3d131f02015-11-03 10:52:52 -05001887 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001888 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001889 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1890 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1891 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001892
Andrew Lunn158bc062016-04-28 21:24:06 -04001893 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
1894 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001895 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001896
1897 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1898 * implemented, only one STU entry is needed to cover all VTU
1899 * entries. Thus, validate the SID 0.
1900 */
1901 vlan.sid = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04001902 err = _mv88e6xxx_stu_getnext(ps, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001903 if (err)
1904 return err;
1905
1906 if (vstp.sid != vlan.sid || !vstp.valid) {
1907 memset(&vstp, 0, sizeof(vstp));
1908 vstp.valid = true;
1909 vstp.sid = vlan.sid;
1910
Andrew Lunn158bc062016-04-28 21:24:06 -04001911 err = _mv88e6xxx_stu_loadpurge(ps, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001912 if (err)
1913 return err;
1914 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001915 }
1916
1917 *entry = vlan;
1918 return 0;
1919}
1920
Andrew Lunn158bc062016-04-28 21:24:06 -04001921static int _mv88e6xxx_vtu_get(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001922 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1923{
1924 int err;
1925
1926 if (!vid)
1927 return -EINVAL;
1928
Andrew Lunn158bc062016-04-28 21:24:06 -04001929 err = _mv88e6xxx_vtu_vid_write(ps, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001930 if (err)
1931 return err;
1932
Andrew Lunn158bc062016-04-28 21:24:06 -04001933 err = _mv88e6xxx_vtu_getnext(ps, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001934 if (err)
1935 return err;
1936
1937 if (entry->vid != vid || !entry->valid) {
1938 if (!creat)
1939 return -EOPNOTSUPP;
1940 /* -ENOENT would've been more appropriate, but switchdev expects
1941 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1942 */
1943
Andrew Lunn158bc062016-04-28 21:24:06 -04001944 err = _mv88e6xxx_vtu_new(ps, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001945 }
1946
1947 return err;
1948}
1949
Vivien Didelotda9c3592016-02-12 12:09:40 -05001950static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1951 u16 vid_begin, u16 vid_end)
1952{
1953 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1954 struct mv88e6xxx_vtu_stu_entry vlan;
1955 int i, err;
1956
1957 if (!vid_begin)
1958 return -EOPNOTSUPP;
1959
1960 mutex_lock(&ps->smi_mutex);
1961
Andrew Lunn158bc062016-04-28 21:24:06 -04001962 err = _mv88e6xxx_vtu_vid_write(ps, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001963 if (err)
1964 goto unlock;
1965
1966 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001967 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001968 if (err)
1969 goto unlock;
1970
1971 if (!vlan.valid)
1972 break;
1973
1974 if (vlan.vid > vid_end)
1975 break;
1976
Vivien Didelot009a2b92016-04-17 13:24:01 -04001977 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001978 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1979 continue;
1980
1981 if (vlan.data[i] ==
1982 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1983 continue;
1984
1985 if (ps->ports[i].bridge_dev ==
1986 ps->ports[port].bridge_dev)
1987 break; /* same bridge, check next VLAN */
1988
1989 netdev_warn(ds->ports[port],
1990 "hardware VLAN %d already used by %s\n",
1991 vlan.vid,
1992 netdev_name(ps->ports[i].bridge_dev));
1993 err = -EOPNOTSUPP;
1994 goto unlock;
1995 }
1996 } while (vlan.vid < vid_end);
1997
1998unlock:
1999 mutex_unlock(&ps->smi_mutex);
2000
2001 return err;
2002}
2003
Vivien Didelot214cdb92016-02-26 13:16:08 -05002004static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2005 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2006 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2007 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2008 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2009};
2010
2011int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2012 bool vlan_filtering)
2013{
2014 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2015 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2016 PORT_CONTROL_2_8021Q_DISABLED;
2017 int ret;
2018
2019 mutex_lock(&ps->smi_mutex);
2020
Andrew Lunn158bc062016-04-28 21:24:06 -04002021 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002022 if (ret < 0)
2023 goto unlock;
2024
2025 old = ret & PORT_CONTROL_2_8021Q_MASK;
2026
Vivien Didelot5220ef12016-03-07 18:24:52 -05002027 if (new != old) {
2028 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2029 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002030
Andrew Lunn158bc062016-04-28 21:24:06 -04002031 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05002032 ret);
2033 if (ret < 0)
2034 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002035
Vivien Didelot5220ef12016-03-07 18:24:52 -05002036 netdev_dbg(ds->ports[port], "802.1Q Mode %s (was %s)\n",
2037 mv88e6xxx_port_8021q_mode_names[new],
2038 mv88e6xxx_port_8021q_mode_names[old]);
2039 }
2040
2041 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002042unlock:
2043 mutex_unlock(&ps->smi_mutex);
2044
2045 return ret;
2046}
2047
Vivien Didelot76e398a2015-11-01 12:33:55 -05002048int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2049 const struct switchdev_obj_port_vlan *vlan,
2050 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002051{
Vivien Didelotda9c3592016-02-12 12:09:40 -05002052 int err;
2053
Vivien Didelotda9c3592016-02-12 12:09:40 -05002054 /* If the requested port doesn't belong to the same bridge as the VLAN
2055 * members, do not support it (yet) and fallback to software VLAN.
2056 */
2057 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2058 vlan->vid_end);
2059 if (err)
2060 return err;
2061
Vivien Didelot76e398a2015-11-01 12:33:55 -05002062 /* We don't need any dynamic resource from the kernel (yet),
2063 * so skip the prepare phase.
2064 */
2065 return 0;
2066}
2067
Andrew Lunn158bc062016-04-28 21:24:06 -04002068static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_priv_state *ps, int port,
2069 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002070{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002071 struct mv88e6xxx_vtu_stu_entry vlan;
2072 int err;
2073
Andrew Lunn158bc062016-04-28 21:24:06 -04002074 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002075 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002076 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002077
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002078 vlan.data[port] = untagged ?
2079 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2080 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2081
Andrew Lunn158bc062016-04-28 21:24:06 -04002082 return _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002083}
2084
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002085void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2086 const struct switchdev_obj_port_vlan *vlan,
2087 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002088{
2089 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2090 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2091 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2092 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002093
2094 mutex_lock(&ps->smi_mutex);
2095
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002096 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Andrew Lunn158bc062016-04-28 21:24:06 -04002097 if (_mv88e6xxx_port_vlan_add(ps, port, vid, untagged))
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002098 netdev_err(ds->ports[port], "failed to add VLAN %d%c\n",
2099 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05002100
Andrew Lunn158bc062016-04-28 21:24:06 -04002101 if (pvid && _mv88e6xxx_port_pvid_set(ps, port, vlan->vid_end))
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002102 netdev_err(ds->ports[port], "failed to set PVID %d\n",
2103 vlan->vid_end);
2104
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002105 mutex_unlock(&ps->smi_mutex);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002106}
2107
Andrew Lunn158bc062016-04-28 21:24:06 -04002108static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_priv_state *ps,
2109 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002110{
Andrew Lunn158bc062016-04-28 21:24:06 -04002111 struct dsa_switch *ds = ps->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002112 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002113 int i, err;
2114
Andrew Lunn158bc062016-04-28 21:24:06 -04002115 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002116 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002117 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002118
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002119 /* Tell switchdev if this VLAN is handled in software */
2120 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05002121 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002122
2123 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2124
2125 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002126 vlan.valid = false;
Vivien Didelot009a2b92016-04-17 13:24:01 -04002127 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05002128 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002129 continue;
2130
2131 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002132 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002133 break;
2134 }
2135 }
2136
Andrew Lunn158bc062016-04-28 21:24:06 -04002137 err = _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002138 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002139 return err;
2140
Andrew Lunn158bc062016-04-28 21:24:06 -04002141 return _mv88e6xxx_atu_remove(ps, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002142}
2143
2144int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2145 const struct switchdev_obj_port_vlan *vlan)
2146{
2147 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2148 u16 pvid, vid;
2149 int err = 0;
2150
2151 mutex_lock(&ps->smi_mutex);
2152
Andrew Lunn158bc062016-04-28 21:24:06 -04002153 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002154 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002155 goto unlock;
2156
Vivien Didelot76e398a2015-11-01 12:33:55 -05002157 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002158 err = _mv88e6xxx_port_vlan_del(ps, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002159 if (err)
2160 goto unlock;
2161
2162 if (vid == pvid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002163 err = _mv88e6xxx_port_pvid_set(ps, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002164 if (err)
2165 goto unlock;
2166 }
2167 }
2168
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002169unlock:
2170 mutex_unlock(&ps->smi_mutex);
2171
2172 return err;
2173}
2174
Andrew Lunn158bc062016-04-28 21:24:06 -04002175static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002176 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002177{
2178 int i, ret;
2179
2180 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002181 ret = _mv88e6xxx_reg_write(
Andrew Lunn158bc062016-04-28 21:24:06 -04002182 ps, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002183 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002184 if (ret < 0)
2185 return ret;
2186 }
2187
2188 return 0;
2189}
2190
Andrew Lunn158bc062016-04-28 21:24:06 -04002191static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_priv_state *ps,
2192 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002193{
2194 int i, ret;
2195
2196 for (i = 0; i < 3; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002197 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002198 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002199 if (ret < 0)
2200 return ret;
2201 addr[i * 2] = ret >> 8;
2202 addr[i * 2 + 1] = ret & 0xff;
2203 }
2204
2205 return 0;
2206}
2207
Andrew Lunn158bc062016-04-28 21:24:06 -04002208static int _mv88e6xxx_atu_load(struct mv88e6xxx_priv_state *ps,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002209 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002210{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002211 int ret;
2212
Andrew Lunn158bc062016-04-28 21:24:06 -04002213 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002214 if (ret < 0)
2215 return ret;
2216
Andrew Lunn158bc062016-04-28 21:24:06 -04002217 ret = _mv88e6xxx_atu_mac_write(ps, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002218 if (ret < 0)
2219 return ret;
2220
Andrew Lunn158bc062016-04-28 21:24:06 -04002221 ret = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002222 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002223 return ret;
2224
Andrew Lunn158bc062016-04-28 21:24:06 -04002225 return _mv88e6xxx_atu_cmd(ps, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002226}
David S. Millercdf09692015-08-11 12:00:37 -07002227
Andrew Lunn158bc062016-04-28 21:24:06 -04002228static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_priv_state *ps, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002229 const unsigned char *addr, u16 vid,
2230 u8 state)
2231{
2232 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002233 struct mv88e6xxx_vtu_stu_entry vlan;
2234 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002235
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002236 /* Null VLAN ID corresponds to the port private database */
2237 if (vid == 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04002238 err = _mv88e6xxx_port_fid_get(ps, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002239 else
Andrew Lunn158bc062016-04-28 21:24:06 -04002240 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002241 if (err)
2242 return err;
2243
2244 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002245 entry.state = state;
2246 ether_addr_copy(entry.mac, addr);
2247 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2248 entry.trunk = false;
2249 entry.portv_trunkid = BIT(port);
2250 }
2251
Andrew Lunn158bc062016-04-28 21:24:06 -04002252 return _mv88e6xxx_atu_load(ps, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002253}
2254
Vivien Didelot146a3202015-10-08 11:35:12 -04002255int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2256 const struct switchdev_obj_port_fdb *fdb,
2257 struct switchdev_trans *trans)
2258{
2259 /* We don't need any dynamic resource from the kernel (yet),
2260 * so skip the prepare phase.
2261 */
2262 return 0;
2263}
2264
Vivien Didelot8497aa62016-04-06 11:55:04 -04002265void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2266 const struct switchdev_obj_port_fdb *fdb,
2267 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002268{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002269 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002270 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2271 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2272 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002273
David S. Millercdf09692015-08-11 12:00:37 -07002274 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04002275 if (_mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, state))
Vivien Didelot8497aa62016-04-06 11:55:04 -04002276 netdev_err(ds->ports[port], "failed to load MAC address\n");
David S. Millercdf09692015-08-11 12:00:37 -07002277 mutex_unlock(&ps->smi_mutex);
David S. Millercdf09692015-08-11 12:00:37 -07002278}
2279
2280int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
Vivien Didelot8057b3e2015-10-08 11:35:14 -04002281 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002282{
2283 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2284 int ret;
2285
2286 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04002287 ret = _mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002288 GLOBAL_ATU_DATA_STATE_UNUSED);
2289 mutex_unlock(&ps->smi_mutex);
2290
2291 return ret;
2292}
2293
Andrew Lunn158bc062016-04-28 21:24:06 -04002294static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_priv_state *ps, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002295 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002296{
Vivien Didelot1d194042015-08-10 09:09:51 -04002297 struct mv88e6xxx_atu_entry next = { 0 };
2298 int ret;
2299
2300 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002301
Andrew Lunn158bc062016-04-28 21:24:06 -04002302 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002303 if (ret < 0)
2304 return ret;
2305
Andrew Lunn158bc062016-04-28 21:24:06 -04002306 ret = _mv88e6xxx_atu_cmd(ps, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002307 if (ret < 0)
2308 return ret;
2309
Andrew Lunn158bc062016-04-28 21:24:06 -04002310 ret = _mv88e6xxx_atu_mac_read(ps, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002311 if (ret < 0)
2312 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002313
Andrew Lunn158bc062016-04-28 21:24:06 -04002314 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002315 if (ret < 0)
2316 return ret;
2317
2318 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2319 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2320 unsigned int mask, shift;
2321
2322 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2323 next.trunk = true;
2324 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2325 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2326 } else {
2327 next.trunk = false;
2328 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2329 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2330 }
2331
2332 next.portv_trunkid = (ret & mask) >> shift;
2333 }
2334
2335 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002336 return 0;
2337}
2338
Andrew Lunn158bc062016-04-28 21:24:06 -04002339static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_priv_state *ps,
2340 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002341 struct switchdev_obj_port_fdb *fdb,
2342 int (*cb)(struct switchdev_obj *obj))
2343{
2344 struct mv88e6xxx_atu_entry addr = {
2345 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2346 };
2347 int err;
2348
Andrew Lunn158bc062016-04-28 21:24:06 -04002349 err = _mv88e6xxx_atu_mac_write(ps, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002350 if (err)
2351 return err;
2352
2353 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002354 err = _mv88e6xxx_atu_getnext(ps, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002355 if (err)
2356 break;
2357
2358 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2359 break;
2360
2361 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2362 bool is_static = addr.state ==
2363 (is_multicast_ether_addr(addr.mac) ?
2364 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2365 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2366
2367 fdb->vid = vid;
2368 ether_addr_copy(fdb->addr, addr.mac);
2369 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2370
2371 err = cb(&fdb->obj);
2372 if (err)
2373 break;
2374 }
2375 } while (!is_broadcast_ether_addr(addr.mac));
2376
2377 return err;
2378}
2379
Vivien Didelotf33475b2015-10-22 09:34:41 -04002380int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2381 struct switchdev_obj_port_fdb *fdb,
2382 int (*cb)(struct switchdev_obj *obj))
2383{
2384 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2385 struct mv88e6xxx_vtu_stu_entry vlan = {
2386 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2387 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002388 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002389 int err;
2390
2391 mutex_lock(&ps->smi_mutex);
2392
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002393 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Andrew Lunn158bc062016-04-28 21:24:06 -04002394 err = _mv88e6xxx_port_fid_get(ps, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002395 if (err)
2396 goto unlock;
2397
Andrew Lunn158bc062016-04-28 21:24:06 -04002398 err = _mv88e6xxx_port_fdb_dump_one(ps, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002399 if (err)
2400 goto unlock;
2401
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002402 /* Dump VLANs' Filtering Information Databases */
Andrew Lunn158bc062016-04-28 21:24:06 -04002403 err = _mv88e6xxx_vtu_vid_write(ps, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002404 if (err)
2405 goto unlock;
2406
2407 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002408 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002409 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002410 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002411
2412 if (!vlan.valid)
2413 break;
2414
Andrew Lunn158bc062016-04-28 21:24:06 -04002415 err = _mv88e6xxx_port_fdb_dump_one(ps, vlan.fid, vlan.vid, port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002416 fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002417 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002418 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002419 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2420
2421unlock:
2422 mutex_unlock(&ps->smi_mutex);
2423
2424 return err;
2425}
2426
Vivien Didelota6692752016-02-12 12:09:39 -05002427int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2428 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002429{
Vivien Didelota6692752016-02-12 12:09:39 -05002430 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002431 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002432
2433 mutex_lock(&ps->smi_mutex);
2434
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002435 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002436 ps->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002437
Vivien Didelot009a2b92016-04-17 13:24:01 -04002438 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002439 if (ps->ports[i].bridge_dev == bridge) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002440 err = _mv88e6xxx_port_based_vlan_map(ps, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002441 if (err)
2442 break;
2443 }
2444 }
2445
Vivien Didelot466dfa02016-02-26 13:16:05 -05002446 mutex_unlock(&ps->smi_mutex);
Vivien Didelota6692752016-02-12 12:09:39 -05002447
Vivien Didelot466dfa02016-02-26 13:16:05 -05002448 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002449}
2450
Vivien Didelot16bfa702016-03-13 16:21:33 -04002451void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002452{
Vivien Didelota6692752016-02-12 12:09:39 -05002453 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002454 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002455 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002456
2457 mutex_lock(&ps->smi_mutex);
2458
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002459 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002460 ps->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002461
Vivien Didelot009a2b92016-04-17 13:24:01 -04002462 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot16bfa702016-03-13 16:21:33 -04002463 if (i == port || ps->ports[i].bridge_dev == bridge)
Andrew Lunn158bc062016-04-28 21:24:06 -04002464 if (_mv88e6xxx_port_based_vlan_map(ps, i))
Vivien Didelot16bfa702016-03-13 16:21:33 -04002465 netdev_warn(ds->ports[i], "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002466
Vivien Didelot466dfa02016-02-26 13:16:05 -05002467 mutex_unlock(&ps->smi_mutex);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002468}
2469
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002470static void mv88e6xxx_bridge_work(struct work_struct *work)
2471{
2472 struct mv88e6xxx_priv_state *ps;
2473 struct dsa_switch *ds;
2474 int port;
2475
2476 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
Andrew Lunn7543a6d2016-04-13 02:40:40 +02002477 ds = ps->ds;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002478
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002479 mutex_lock(&ps->smi_mutex);
2480
Vivien Didelot009a2b92016-04-17 13:24:01 -04002481 for (port = 0; port < ps->info->num_ports; ++port)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002482 if (test_and_clear_bit(port, ps->port_state_update_mask) &&
Andrew Lunn158bc062016-04-28 21:24:06 -04002483 _mv88e6xxx_port_state(ps, port, ps->ports[port].state))
2484 netdev_warn(ds->ports[port],
2485 "failed to update state to %s\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002486 mv88e6xxx_port_state_names[ps->ports[port].state]);
2487
2488 mutex_unlock(&ps->smi_mutex);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002489}
2490
Andrew Lunn158bc062016-04-28 21:24:06 -04002491static int _mv88e6xxx_phy_page_write(struct mv88e6xxx_priv_state *ps,
2492 int port, int page, int reg, int val)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002493{
2494 int ret;
2495
Andrew Lunn158bc062016-04-28 21:24:06 -04002496 ret = _mv88e6xxx_phy_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002497 if (ret < 0)
2498 goto restore_page_0;
2499
Andrew Lunn158bc062016-04-28 21:24:06 -04002500 ret = _mv88e6xxx_phy_write_indirect(ps, port, reg, val);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002501restore_page_0:
Andrew Lunn158bc062016-04-28 21:24:06 -04002502 _mv88e6xxx_phy_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002503
2504 return ret;
2505}
2506
Andrew Lunn158bc062016-04-28 21:24:06 -04002507static int _mv88e6xxx_phy_page_read(struct mv88e6xxx_priv_state *ps,
2508 int port, int page, int reg)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002509{
2510 int ret;
2511
Andrew Lunn158bc062016-04-28 21:24:06 -04002512 ret = _mv88e6xxx_phy_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002513 if (ret < 0)
2514 goto restore_page_0;
2515
Andrew Lunn158bc062016-04-28 21:24:06 -04002516 ret = _mv88e6xxx_phy_read_indirect(ps, port, reg);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002517restore_page_0:
Andrew Lunn158bc062016-04-28 21:24:06 -04002518 _mv88e6xxx_phy_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002519
2520 return ret;
2521}
2522
Andrew Lunn158bc062016-04-28 21:24:06 -04002523static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_priv_state *ps)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002524{
2525 int ret;
2526
Andrew Lunn158bc062016-04-28 21:24:06 -04002527 ret = _mv88e6xxx_phy_page_read(ps, REG_FIBER_SERDES, PAGE_FIBER_SERDES,
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002528 MII_BMCR);
2529 if (ret < 0)
2530 return ret;
2531
2532 if (ret & BMCR_PDOWN) {
2533 ret &= ~BMCR_PDOWN;
Andrew Lunn158bc062016-04-28 21:24:06 -04002534 ret = _mv88e6xxx_phy_page_write(ps, REG_FIBER_SERDES,
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002535 PAGE_FIBER_SERDES, MII_BMCR,
2536 ret);
2537 }
2538
2539 return ret;
2540}
2541
Andrew Lunndbde9e62015-05-06 01:09:48 +02002542static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002543{
2544 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002545 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002546 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002547
2548 mutex_lock(&ps->smi_mutex);
2549
Andrew Lunn158bc062016-04-28 21:24:06 -04002550 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2551 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2552 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2553 mv88e6xxx_6065_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002554 /* MAC Forcing register: don't force link, speed,
2555 * duplex or flow control state to any particular
2556 * values on physical ports, but force the CPU port
2557 * and all DSA ports to their maximum bandwidth and
2558 * full duplex.
2559 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002560 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002561 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002562 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002563 reg |= PORT_PCS_CTRL_FORCE_LINK |
2564 PORT_PCS_CTRL_LINK_UP |
2565 PORT_PCS_CTRL_DUPLEX_FULL |
2566 PORT_PCS_CTRL_FORCE_DUPLEX;
Andrew Lunn158bc062016-04-28 21:24:06 -04002567 if (mv88e6xxx_6065_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002568 reg |= PORT_PCS_CTRL_100;
2569 else
2570 reg |= PORT_PCS_CTRL_1000;
2571 } else {
2572 reg |= PORT_PCS_CTRL_UNFORCED;
2573 }
2574
Andrew Lunn158bc062016-04-28 21:24:06 -04002575 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002576 PORT_PCS_CTRL, reg);
2577 if (ret)
2578 goto abort;
2579 }
2580
2581 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2582 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2583 * tunneling, determine priority by looking at 802.1p and IP
2584 * priority fields (IP prio has precedence), and set STP state
2585 * to Forwarding.
2586 *
2587 * If this is the CPU link, use DSA or EDSA tagging depending
2588 * on which tagging mode was configured.
2589 *
2590 * If this is a link to another switch, use DSA tagging mode.
2591 *
2592 * If this is the upstream port for this switch, enable
2593 * forwarding of unknown unicasts and multicasts.
2594 */
2595 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002596 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2597 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2598 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2599 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002600 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2601 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2602 PORT_CONTROL_STATE_FORWARDING;
2603 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002604 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002605 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002606 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2607 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2608 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002609 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2610 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
2611 else
2612 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002613 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2614 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002615 }
2616
Andrew Lunn158bc062016-04-28 21:24:06 -04002617 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2618 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2619 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2620 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002621 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2622 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2623 }
2624 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002625 if (dsa_is_dsa_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002626 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002627 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002628 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2629 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2630 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002631 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002632 }
2633
Andrew Lunn54d792f2015-05-06 01:09:47 +02002634 if (port == dsa_upstream_port(ds))
2635 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2636 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2637 }
2638 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002639 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002640 PORT_CONTROL, reg);
2641 if (ret)
2642 goto abort;
2643 }
2644
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002645 /* If this port is connected to a SerDes, make sure the SerDes is not
2646 * powered down.
2647 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002648 if (mv88e6xxx_6352_family(ps)) {
2649 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002650 if (ret < 0)
2651 goto abort;
2652 ret &= PORT_STATUS_CMODE_MASK;
2653 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2654 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2655 (ret == PORT_STATUS_CMODE_SGMII)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002656 ret = mv88e6xxx_power_on_serdes(ps);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002657 if (ret < 0)
2658 goto abort;
2659 }
2660 }
2661
Vivien Didelot8efdda42015-08-13 12:52:23 -04002662 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002663 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002664 * untagged frames on this port, do a destination address lookup on all
2665 * received packets as usual, disable ARP mirroring and don't send a
2666 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002667 */
2668 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002669 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2670 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2671 mv88e6xxx_6095_family(ps) || mv88e6xxx_6320_family(ps) ||
2672 mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002673 reg = PORT_CONTROL_2_MAP_DA;
2674
Andrew Lunn158bc062016-04-28 21:24:06 -04002675 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2676 mv88e6xxx_6165_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002677 reg |= PORT_CONTROL_2_JUMBO_10240;
2678
Andrew Lunn158bc062016-04-28 21:24:06 -04002679 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002680 /* Set the upstream port this port should use */
2681 reg |= dsa_upstream_port(ds);
2682 /* enable forwarding of unknown multicast addresses to
2683 * the upstream port
2684 */
2685 if (port == dsa_upstream_port(ds))
2686 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2687 }
2688
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002689 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002690
Andrew Lunn54d792f2015-05-06 01:09:47 +02002691 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002692 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002693 PORT_CONTROL_2, reg);
2694 if (ret)
2695 goto abort;
2696 }
2697
2698 /* Port Association Vector: when learning source addresses
2699 * of packets, add the address to the address database using
2700 * a port bitmap that has only the bit for this port set and
2701 * the other bits clear.
2702 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002703 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002704 /* Disable learning for CPU port */
2705 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002706 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002707
Andrew Lunn158bc062016-04-28 21:24:06 -04002708 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002709 if (ret)
2710 goto abort;
2711
2712 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002713 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002714 0x0000);
2715 if (ret)
2716 goto abort;
2717
Andrew Lunn158bc062016-04-28 21:24:06 -04002718 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2719 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2720 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002721 /* Do not limit the period of time that this port can
2722 * be paused for by the remote end or the period of
2723 * time that this port can pause the remote end.
2724 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002725 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002726 PORT_PAUSE_CTRL, 0x0000);
2727 if (ret)
2728 goto abort;
2729
2730 /* Port ATU control: disable limiting the number of
2731 * address database entries that this port is allowed
2732 * to use.
2733 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002734 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002735 PORT_ATU_CONTROL, 0x0000);
2736 /* Priority Override: disable DA, SA and VTU priority
2737 * override.
2738 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002739 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002740 PORT_PRI_OVERRIDE, 0x0000);
2741 if (ret)
2742 goto abort;
2743
2744 /* Port Ethertype: use the Ethertype DSA Ethertype
2745 * value.
2746 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002747 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002748 PORT_ETH_TYPE, ETH_P_EDSA);
2749 if (ret)
2750 goto abort;
2751 /* Tag Remap: use an identity 802.1p prio -> switch
2752 * prio mapping.
2753 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002754 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002755 PORT_TAG_REGMAP_0123, 0x3210);
2756 if (ret)
2757 goto abort;
2758
2759 /* Tag Remap 2: use an identity 802.1p prio -> switch
2760 * prio mapping.
2761 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002762 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002763 PORT_TAG_REGMAP_4567, 0x7654);
2764 if (ret)
2765 goto abort;
2766 }
2767
Andrew Lunn158bc062016-04-28 21:24:06 -04002768 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2769 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2770 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2771 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002772 /* Rate Control: disable ingress rate limiting. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002773 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002774 PORT_RATE_CONTROL, 0x0001);
2775 if (ret)
2776 goto abort;
2777 }
2778
Guenter Roeck366f0a02015-03-26 18:36:30 -07002779 /* Port Control 1: disable trunking, disable sending
2780 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002781 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002782 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002783 if (ret)
2784 goto abort;
2785
Vivien Didelot207afda2016-04-14 14:42:09 -04002786 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002787 * database, and allow bidirectional communication between the
2788 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002789 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002790 ret = _mv88e6xxx_port_fid_set(ps, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002791 if (ret)
2792 goto abort;
2793
Andrew Lunn158bc062016-04-28 21:24:06 -04002794 ret = _mv88e6xxx_port_based_vlan_map(ps, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002795 if (ret)
2796 goto abort;
2797
2798 /* Default VLAN ID and priority: don't set a default VLAN
2799 * ID, and set the default packet priority to zero.
2800 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002801 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002802 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002803abort:
2804 mutex_unlock(&ps->smi_mutex);
2805 return ret;
2806}
2807
Andrew Lunndbde9e62015-05-06 01:09:48 +02002808int mv88e6xxx_setup_ports(struct dsa_switch *ds)
2809{
2810 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2811 int ret;
2812 int i;
2813
Vivien Didelot009a2b92016-04-17 13:24:01 -04002814 for (i = 0; i < ps->info->num_ports; i++) {
Andrew Lunndbde9e62015-05-06 01:09:48 +02002815 ret = mv88e6xxx_setup_port(ds, i);
2816 if (ret < 0)
2817 return ret;
2818 }
2819 return 0;
2820}
2821
Andrew Lunn158bc062016-04-28 21:24:06 -04002822int mv88e6xxx_setup_common(struct mv88e6xxx_priv_state *ps)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002823{
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002824 mutex_init(&ps->smi_mutex);
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002825
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002826 INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
2827
Vivien Didelotd24645b2016-05-09 13:22:41 -04002828 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
2829 mutex_init(&ps->eeprom_mutex);
2830
Vivien Didelot8c9983a2016-05-09 13:22:39 -04002831 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
2832 mv88e6xxx_ppu_state_init(ps);
2833
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002834 return 0;
2835}
2836
Andrew Lunn54d792f2015-05-06 01:09:47 +02002837int mv88e6xxx_setup_global(struct dsa_switch *ds)
2838{
2839 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002840 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002841 int i;
2842
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002843 mutex_lock(&ps->smi_mutex);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002844 /* Set the default address aging time to 5 minutes, and
2845 * enable address learn messages to be sent to all message
2846 * ports.
2847 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002848 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002849 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2850 if (err)
2851 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002852
2853 /* Configure the IP ToS mapping registers. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002854 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002855 if (err)
2856 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002857 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002858 if (err)
2859 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002860 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002861 if (err)
2862 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002863 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002864 if (err)
2865 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002866 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002867 if (err)
2868 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002869 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002870 if (err)
2871 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002872 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002873 if (err)
2874 goto unlock;
Andrew Lunn158bc062016-04-28 21:24:06 -04002875 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002876 if (err)
2877 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002878
2879 /* Configure the IEEE 802.1p priority mapping register. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002880 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002881 if (err)
2882 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002883
2884 /* Send all frames with destination addresses matching
2885 * 01:80:c2:00:00:0x to the CPU port.
2886 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002887 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002888 if (err)
2889 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002890
2891 /* Ignore removed tag data on doubly tagged packets, disable
2892 * flow control messages, force flow control priority to the
2893 * highest, and send all special multicast frames to the CPU
2894 * port at the highest priority.
2895 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002896 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002897 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
2898 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
2899 if (err)
2900 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002901
2902 /* Program the DSA routing table. */
2903 for (i = 0; i < 32; i++) {
2904 int nexthop = 0x1f;
2905
2906 if (ds->pd->rtable &&
2907 i != ds->index && i < ds->dst->pd->nr_chips)
2908 nexthop = ds->pd->rtable[i] & 0x1f;
2909
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002910 err = _mv88e6xxx_reg_write(
Andrew Lunn158bc062016-04-28 21:24:06 -04002911 ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002912 GLOBAL2_DEVICE_MAPPING,
2913 GLOBAL2_DEVICE_MAPPING_UPDATE |
2914 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop);
2915 if (err)
2916 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002917 }
2918
2919 /* Clear all trunk masks. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002920 for (i = 0; i < 8; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002921 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002922 0x8000 |
2923 (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
Vivien Didelot009a2b92016-04-17 13:24:01 -04002924 ((1 << ps->info->num_ports) - 1));
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002925 if (err)
2926 goto unlock;
2927 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002928
2929 /* Clear all trunk mappings. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002930 for (i = 0; i < 16; i++) {
2931 err = _mv88e6xxx_reg_write(
Andrew Lunn158bc062016-04-28 21:24:06 -04002932 ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002933 GLOBAL2_TRUNK_MAPPING,
2934 GLOBAL2_TRUNK_MAPPING_UPDATE |
2935 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
2936 if (err)
2937 goto unlock;
2938 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002939
Andrew Lunn158bc062016-04-28 21:24:06 -04002940 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2941 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2942 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002943 /* Send all frames with destination addresses matching
2944 * 01:80:c2:00:00:2x to the CPU port.
2945 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002946 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002947 GLOBAL2_MGMT_EN_2X, 0xffff);
2948 if (err)
2949 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002950
2951 /* Initialise cross-chip port VLAN table to reset
2952 * defaults.
2953 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002954 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002955 GLOBAL2_PVT_ADDR, 0x9000);
2956 if (err)
2957 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002958
2959 /* Clear the priority override table. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002960 for (i = 0; i < 16; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002961 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002962 GLOBAL2_PRIO_OVERRIDE,
2963 0x8000 | (i << 8));
2964 if (err)
2965 goto unlock;
2966 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002967 }
2968
Andrew Lunn158bc062016-04-28 21:24:06 -04002969 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2970 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2971 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2972 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002973 /* Disable ingress rate limiting by resetting all
2974 * ingress rate limit registers to their initial
2975 * state.
2976 */
Vivien Didelot009a2b92016-04-17 13:24:01 -04002977 for (i = 0; i < ps->info->num_ports; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002978 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002979 GLOBAL2_INGRESS_OP,
2980 0x9000 | (i << 8));
2981 if (err)
2982 goto unlock;
2983 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002984 }
2985
Andrew Lunndb687a52015-06-20 21:31:29 +02002986 /* Clear the statistics counters for all ports */
Andrew Lunn158bc062016-04-28 21:24:06 -04002987 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002988 GLOBAL_STATS_OP_FLUSH_ALL);
2989 if (err)
2990 goto unlock;
Andrew Lunndb687a52015-06-20 21:31:29 +02002991
2992 /* Wait for the flush to complete. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002993 err = _mv88e6xxx_stats_wait(ps);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002994 if (err < 0)
Vivien Didelot6b17e862015-08-13 12:52:18 -04002995 goto unlock;
2996
Vivien Didelotc161d0a2015-09-04 14:34:13 -04002997 /* Clear all ATU entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04002998 err = _mv88e6xxx_atu_flush(ps, 0, true);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002999 if (err < 0)
Vivien Didelotc161d0a2015-09-04 14:34:13 -04003000 goto unlock;
3001
Vivien Didelot6b17e862015-08-13 12:52:18 -04003002 /* Clear all the VTU and STU entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04003003 err = _mv88e6xxx_vtu_stu_flush(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04003004unlock:
Vivien Didelot24751e22015-08-03 09:17:44 -04003005 mutex_unlock(&ps->smi_mutex);
Andrew Lunndb687a52015-06-20 21:31:29 +02003006
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003007 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003008}
3009
Andrew Lunn158bc062016-04-28 21:24:06 -04003010int mv88e6xxx_switch_reset(struct mv88e6xxx_priv_state *ps, bool ppu_active)
Andrew Lunn143a8302015-04-02 04:06:34 +02003011{
Andrew Lunn143a8302015-04-02 04:06:34 +02003012 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Andrew Lunn158bc062016-04-28 21:24:06 -04003013 struct gpio_desc *gpiod = ps->ds->pd->reset;
Andrew Lunn143a8302015-04-02 04:06:34 +02003014 unsigned long timeout;
3015 int ret;
3016 int i;
3017
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003018 mutex_lock(&ps->smi_mutex);
3019
Andrew Lunn143a8302015-04-02 04:06:34 +02003020 /* Set all ports to the disabled state. */
Vivien Didelot009a2b92016-04-17 13:24:01 -04003021 for (i = 0; i < ps->info->num_ports; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04003022 ret = _mv88e6xxx_reg_read(ps, REG_PORT(i), PORT_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003023 if (ret < 0)
3024 goto unlock;
3025
Andrew Lunn158bc062016-04-28 21:24:06 -04003026 ret = _mv88e6xxx_reg_write(ps, REG_PORT(i), PORT_CONTROL,
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003027 ret & 0xfffc);
3028 if (ret)
3029 goto unlock;
Andrew Lunn143a8302015-04-02 04:06:34 +02003030 }
3031
3032 /* Wait for transmit queues to drain. */
3033 usleep_range(2000, 4000);
3034
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +01003035 /* If there is a gpio connected to the reset pin, toggle it */
3036 if (gpiod) {
3037 gpiod_set_value_cansleep(gpiod, 1);
3038 usleep_range(10000, 20000);
3039 gpiod_set_value_cansleep(gpiod, 0);
3040 usleep_range(10000, 20000);
3041 }
3042
Andrew Lunn143a8302015-04-02 04:06:34 +02003043 /* Reset the switch. Keep the PPU active if requested. The PPU
3044 * needs to be active to support indirect phy register access
3045 * through global registers 0x18 and 0x19.
3046 */
3047 if (ppu_active)
Andrew Lunn158bc062016-04-28 21:24:06 -04003048 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc000);
Andrew Lunn143a8302015-04-02 04:06:34 +02003049 else
Andrew Lunn158bc062016-04-28 21:24:06 -04003050 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc400);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003051 if (ret)
3052 goto unlock;
Andrew Lunn143a8302015-04-02 04:06:34 +02003053
3054 /* Wait up to one second for reset to complete. */
3055 timeout = jiffies + 1 * HZ;
3056 while (time_before(jiffies, timeout)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04003057 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, 0x00);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003058 if (ret < 0)
3059 goto unlock;
3060
Andrew Lunn143a8302015-04-02 04:06:34 +02003061 if ((ret & is_reset) == is_reset)
3062 break;
3063 usleep_range(1000, 2000);
3064 }
3065 if (time_after(jiffies, timeout))
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003066 ret = -ETIMEDOUT;
3067 else
3068 ret = 0;
3069unlock:
3070 mutex_unlock(&ps->smi_mutex);
Andrew Lunn143a8302015-04-02 04:06:34 +02003071
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003072 return ret;
Andrew Lunn143a8302015-04-02 04:06:34 +02003073}
3074
Andrew Lunn491435852015-04-02 04:06:35 +02003075int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
3076{
3077 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3078 int ret;
3079
Andrew Lunn3898c142015-05-06 01:09:53 +02003080 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04003081 ret = _mv88e6xxx_phy_page_read(ps, port, page, reg);
Andrew Lunn3898c142015-05-06 01:09:53 +02003082 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003083
Andrew Lunn491435852015-04-02 04:06:35 +02003084 return ret;
3085}
3086
3087int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
3088 int reg, int val)
3089{
3090 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3091 int ret;
3092
Andrew Lunn3898c142015-05-06 01:09:53 +02003093 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04003094 ret = _mv88e6xxx_phy_page_write(ps, port, page, reg, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02003095 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003096
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003097 return ret;
3098}
3099
Andrew Lunn158bc062016-04-28 21:24:06 -04003100static int mv88e6xxx_port_to_phy_addr(struct mv88e6xxx_priv_state *ps,
3101 int port)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003102{
Vivien Didelot009a2b92016-04-17 13:24:01 -04003103 if (port >= 0 && port < ps->info->num_ports)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003104 return port;
3105 return -EINVAL;
3106}
3107
3108int
3109mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
3110{
3111 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -04003112 int addr = mv88e6xxx_port_to_phy_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003113 int ret;
3114
3115 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003116 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003117
Andrew Lunn3898c142015-05-06 01:09:53 +02003118 mutex_lock(&ps->smi_mutex);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003119
3120 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3121 ret = mv88e6xxx_phy_read_ppu(ps, addr, regnum);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003122 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
3123 ret = _mv88e6xxx_phy_read_indirect(ps, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003124 else
3125 ret = _mv88e6xxx_phy_read(ps, addr, regnum);
3126
Andrew Lunn3898c142015-05-06 01:09:53 +02003127 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003128 return ret;
3129}
3130
3131int
3132mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
3133{
3134 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -04003135 int addr = mv88e6xxx_port_to_phy_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003136 int ret;
3137
3138 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003139 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003140
Andrew Lunn3898c142015-05-06 01:09:53 +02003141 mutex_lock(&ps->smi_mutex);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003142
3143 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3144 ret = mv88e6xxx_phy_write_ppu(ps, addr, regnum, val);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003145 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
3146 ret = _mv88e6xxx_phy_write_indirect(ps, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003147 else
3148 ret = _mv88e6xxx_phy_write(ps, addr, regnum, val);
3149
Andrew Lunn3898c142015-05-06 01:09:53 +02003150 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003151 return ret;
3152}
3153
Guenter Roeckc22995c2015-07-25 09:42:28 -07003154#ifdef CONFIG_NET_DSA_HWMON
3155
3156static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3157{
3158 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3159 int ret;
3160 int val;
3161
3162 *temp = 0;
3163
3164 mutex_lock(&ps->smi_mutex);
3165
Andrew Lunn158bc062016-04-28 21:24:06 -04003166 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003167 if (ret < 0)
3168 goto error;
3169
3170 /* Enable temperature sensor */
Andrew Lunn158bc062016-04-28 21:24:06 -04003171 ret = _mv88e6xxx_phy_read(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003172 if (ret < 0)
3173 goto error;
3174
Andrew Lunn158bc062016-04-28 21:24:06 -04003175 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x1a, ret | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003176 if (ret < 0)
3177 goto error;
3178
3179 /* Wait for temperature to stabilize */
3180 usleep_range(10000, 12000);
3181
Andrew Lunn158bc062016-04-28 21:24:06 -04003182 val = _mv88e6xxx_phy_read(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003183 if (val < 0) {
3184 ret = val;
3185 goto error;
3186 }
3187
3188 /* Disable temperature sensor */
Andrew Lunn158bc062016-04-28 21:24:06 -04003189 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x1a, ret & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003190 if (ret < 0)
3191 goto error;
3192
3193 *temp = ((val & 0x1f) - 5) * 5;
3194
3195error:
Andrew Lunn158bc062016-04-28 21:24:06 -04003196 _mv88e6xxx_phy_write(ps, 0x0, 0x16, 0x0);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003197 mutex_unlock(&ps->smi_mutex);
3198 return ret;
3199}
3200
3201static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3202{
Andrew Lunn158bc062016-04-28 21:24:06 -04003203 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3204 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003205 int ret;
3206
3207 *temp = 0;
3208
3209 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
3210 if (ret < 0)
3211 return ret;
3212
3213 *temp = (ret & 0xff) - 25;
3214
3215 return 0;
3216}
3217
3218int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3219{
Andrew Lunn158bc062016-04-28 21:24:06 -04003220 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3221
Vivien Didelot6594f612016-05-09 13:22:42 -04003222 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP))
3223 return -EOPNOTSUPP;
3224
Andrew Lunn158bc062016-04-28 21:24:06 -04003225 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003226 return mv88e63xx_get_temp(ds, temp);
3227
3228 return mv88e61xx_get_temp(ds, temp);
3229}
3230
3231int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3232{
Andrew Lunn158bc062016-04-28 21:24:06 -04003233 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3234 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003235 int ret;
3236
Vivien Didelot6594f612016-05-09 13:22:42 -04003237 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003238 return -EOPNOTSUPP;
3239
3240 *temp = 0;
3241
3242 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3243 if (ret < 0)
3244 return ret;
3245
3246 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3247
3248 return 0;
3249}
3250
3251int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3252{
Andrew Lunn158bc062016-04-28 21:24:06 -04003253 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3254 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003255 int ret;
3256
Vivien Didelot6594f612016-05-09 13:22:42 -04003257 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003258 return -EOPNOTSUPP;
3259
3260 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3261 if (ret < 0)
3262 return ret;
3263 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3264 return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
3265 (ret & 0xe0ff) | (temp << 8));
3266}
3267
3268int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3269{
Andrew Lunn158bc062016-04-28 21:24:06 -04003270 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3271 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003272 int ret;
3273
Vivien Didelot6594f612016-05-09 13:22:42 -04003274 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003275 return -EOPNOTSUPP;
3276
3277 *alarm = false;
3278
3279 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3280 if (ret < 0)
3281 return ret;
3282
3283 *alarm = !!(ret & 0x40);
3284
3285 return 0;
3286}
3287#endif /* CONFIG_NET_DSA_HWMON */
3288
Vivien Didelotf6271e62016-04-17 13:23:59 -04003289static const struct mv88e6xxx_info *
3290mv88e6xxx_lookup_info(unsigned int prod_num, const struct mv88e6xxx_info *table,
Vivien Didelot0209d142016-04-17 13:23:55 -04003291 unsigned int num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003292{
Vivien Didelota439c062016-04-17 13:23:58 -04003293 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003294
Vivien Didelotb9b37712015-10-30 19:39:48 -04003295 for (i = 0; i < num; ++i)
Vivien Didelotf6271e62016-04-17 13:23:59 -04003296 if (table[i].prod_num == prod_num)
3297 return &table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003298
Vivien Didelotb9b37712015-10-30 19:39:48 -04003299 return NULL;
3300}
3301
Vivien Didelot0209d142016-04-17 13:23:55 -04003302const char *mv88e6xxx_drv_probe(struct device *dsa_dev, struct device *host_dev,
3303 int sw_addr, void **priv,
Vivien Didelotf6271e62016-04-17 13:23:59 -04003304 const struct mv88e6xxx_info *table,
Vivien Didelot0209d142016-04-17 13:23:55 -04003305 unsigned int num)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003306{
Vivien Didelotf6271e62016-04-17 13:23:59 -04003307 const struct mv88e6xxx_info *info;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003308 struct mv88e6xxx_priv_state *ps;
Vivien Didelota439c062016-04-17 13:23:58 -04003309 struct mii_bus *bus;
Vivien Didelot0209d142016-04-17 13:23:55 -04003310 const char *name;
Vivien Didelota439c062016-04-17 13:23:58 -04003311 int id, prod_num, rev;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003312
Vivien Didelota439c062016-04-17 13:23:58 -04003313 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003314 if (!bus)
3315 return NULL;
3316
Vivien Didelota439c062016-04-17 13:23:58 -04003317 id = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
3318 if (id < 0)
3319 return NULL;
3320
3321 prod_num = (id & 0xfff0) >> 4;
3322 rev = id & 0x000f;
3323
Vivien Didelotf6271e62016-04-17 13:23:59 -04003324 info = mv88e6xxx_lookup_info(prod_num, table, num);
3325 if (!info)
Vivien Didelota439c062016-04-17 13:23:58 -04003326 return NULL;
3327
Vivien Didelotf6271e62016-04-17 13:23:59 -04003328 name = info->name;
3329
Vivien Didelota439c062016-04-17 13:23:58 -04003330 ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL);
3331 if (!ps)
3332 return NULL;
3333
3334 ps->bus = bus;
3335 ps->sw_addr = sw_addr;
Vivien Didelotf6271e62016-04-17 13:23:59 -04003336 ps->info = info;
Vivien Didelota439c062016-04-17 13:23:58 -04003337
3338 *priv = ps;
3339
3340 dev_info(&ps->bus->dev, "switch 0x%x probed: %s, revision %u\n",
3341 prod_num, name, rev);
3342
Andrew Lunna77d43f2016-04-13 02:40:42 +02003343 return name;
3344}
3345
Ben Hutchings98e67302011-11-25 14:36:19 +00003346static int __init mv88e6xxx_init(void)
3347{
3348#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
3349 register_switch_driver(&mv88e6131_switch_driver);
3350#endif
Andrew Lunnca3dfa52016-03-12 00:01:36 +01003351#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123)
3352 register_switch_driver(&mv88e6123_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003353#endif
Guenter Roeck3ad50cc2014-10-29 10:44:56 -07003354#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
3355 register_switch_driver(&mv88e6352_switch_driver);
3356#endif
Andrew Lunn42f27252014-09-12 23:58:44 +02003357#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
3358 register_switch_driver(&mv88e6171_switch_driver);
3359#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00003360 return 0;
3361}
3362module_init(mv88e6xxx_init);
3363
3364static void __exit mv88e6xxx_cleanup(void)
3365{
Andrew Lunn42f27252014-09-12 23:58:44 +02003366#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
3367 unregister_switch_driver(&mv88e6171_switch_driver);
3368#endif
Vivien Didelot4212b542015-05-01 10:43:52 -04003369#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
3370 unregister_switch_driver(&mv88e6352_switch_driver);
3371#endif
Andrew Lunnca3dfa52016-03-12 00:01:36 +01003372#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123)
3373 unregister_switch_driver(&mv88e6123_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003374#endif
3375#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
3376 unregister_switch_driver(&mv88e6131_switch_driver);
3377#endif
3378}
3379module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003380
3381MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3382MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3383MODULE_LICENSE("GPL");